From 62310039ec82da177799ab6693b4f55321000021 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 7 Apr 2016 10:18:58 +0100
Subject: [PATCH] Add untested etag code. Also tweak help page some more.

---
 build/index.php            | 25 ++++++++++++++++++++++++-
 module_index.json          |  2 +-
 modules/feature-upload.php | 23 +++++++++++++++++++++++
 settings.fragment.php      |  2 +-
 4 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/build/index.php b/build/index.php
index 7d7c3f5..b936c73 100644
--- a/build/index.php
+++ b/build/index.php
@@ -351,7 +351,7 @@ textarea ~ input[type=submit] { margin: 0.5rem 0.8rem; padding: 0.5rem; font-wei
 .tag-list li { display: inline-block; margin: 1rem; }
 .mini-tag { background: #d2c3dd; padding: 0.2rem 0.4rem; color: #fb701a; text-decoration: none; }
 
-.help-section-header::after { content: attr(id); float: right; color: rgba(0, 0, 0, 0.4); font-size: 0.8rem; font-weight: normal; }
+.help-section-header::after { content: \"#\" attr(id); float: right; color: rgba(0, 0, 0, 0.4); font-size: 0.8rem; font-weight: normal; }
 
 .cursor-query { cursor: help; }
 
@@ -2628,6 +2628,29 @@ register_module([
 			if(isset($_GET["type"]) and in_array($_GET["type"], [ "image/png", "image/jpeg", "image/webp" ]))
 				$output_mime = $_GET["type"];
 			
+			/// ETag handling ///
+			// TODO: Test this
+			error_log("Note to self: Please remember to test this etag code!");
+			// Generate the etag and send it to the client
+			$preview_etag = sha1("$output_mime|$target_size|$filepath|$mime_type");
+			$allheaders = getallheaders();
+			$allheaders = array_change_key_case($allheaders, CASE_LOWER);
+			error_log(var_export($allheaders, true));
+			if(!isset($allheaders["if-none-match"]))
+			{
+				header("etag: $preview_etag");
+			}
+			else
+			{
+				if($allheaders["if-none-match"] === $preview_etag)
+				{
+					http_response_code(304);
+					header("x-generation-time: " . (microtime(true) - $start_time));
+					exit();
+				}
+			}
+			/// ETag handling end ///
+			
 			$preview_image = new Imagick();
 			switch(substr($mime_type, 0, strpos($mime_type, "/")))
 			{
diff --git a/module_index.json b/module_index.json
index 1c27984..373f070 100644
--- a/module_index.json
+++ b/module_index.json
@@ -68,7 +68,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File:' prefix.",
         "id": "feature-upload",
-        "lastupdate": 1459963545,
+        "lastupdate": 1459973604,
         "optional": false
     },
     {
diff --git a/modules/feature-upload.php b/modules/feature-upload.php
index 84cae5f..c296064 100644
--- a/modules/feature-upload.php
+++ b/modules/feature-upload.php
@@ -215,6 +215,29 @@ register_module([
 			if(isset($_GET["type"]) and in_array($_GET["type"], [ "image/png", "image/jpeg", "image/webp" ]))
 				$output_mime = $_GET["type"];
 			
+			/// ETag handling ///
+			// TODO: Test this
+			error_log("Note to self: Please remember to test this etag code!");
+			// Generate the etag and send it to the client
+			$preview_etag = sha1("$output_mime|$target_size|$filepath|$mime_type");
+			$allheaders = getallheaders();
+			$allheaders = array_change_key_case($allheaders, CASE_LOWER);
+			error_log(var_export($allheaders, true));
+			if(!isset($allheaders["if-none-match"]))
+			{
+				header("etag: $preview_etag");
+			}
+			else
+			{
+				if($allheaders["if-none-match"] === $preview_etag)
+				{
+					http_response_code(304);
+					header("x-generation-time: " . (microtime(true) - $start_time));
+					exit();
+				}
+			}
+			/// ETag handling end ///
+			
 			$preview_image = new Imagick();
 			switch(substr($mime_type, 0, strpos($mime_type, "/")))
 			{
diff --git a/settings.fragment.php b/settings.fragment.php
index e670698..dbce0f1 100644
--- a/settings.fragment.php
+++ b/settings.fragment.php
@@ -348,7 +348,7 @@ textarea ~ input[type=submit] { margin: 0.5rem 0.8rem; padding: 0.5rem; font-wei
 .tag-list li { display: inline-block; margin: 1rem; }
 .mini-tag { background: #d2c3dd; padding: 0.2rem 0.4rem; color: #fb701a; text-decoration: none; }
 
-.help-section-header::after { content: attr(id); float: right; color: rgba(0, 0, 0, 0.4); font-size: 0.8rem; font-weight: normal; }
+.help-section-header::after { content: \"#\" attr(id); float: right; color: rgba(0, 0, 0, 0.4); font-size: 0.8rem; font-weight: normal; }
 
 .cursor-query { cursor: help; }
 
