From eb8daf2ce68125f06c45560bd8da3ca463596776 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 3 Jun 2016 08:45:17 +0100
Subject: [PATCH] Initial history implementation. Save prev. revs of pages.

---
 build/index.php             | 79 +++++++++++++++++++++++++++++++------
 module_index.json           | 11 +++++-
 modules/feature-history.php | 60 ++++++++++++++++++++++++++++
 modules/page-edit.php       | 18 +++------
 4 files changed, 141 insertions(+), 27 deletions(-)
 create mode 100644 modules/feature-history.php

diff --git a/build/index.php b/build/index.php
index 2e37c75..ea6d56c 100644
--- a/build/index.php
+++ b/build/index.php
@@ -1740,6 +1740,67 @@ function render_sidebar($pageindex, $root_pagename = "")
 
 
 
+
+register_module([
+	"name" => "Page History",
+	"version" => "0.1",
+	"author" => "Starbeamrainbowlabs",
+	"description" => "Adds the ability to keep unlimited page history, limited only by your disk space. Note that this doesn't store file history (yet).",
+	"id" => "feature-history",
+	"code" => function() {
+		
+		/*
+		 * ██   ██ ██ ███████ ████████  ██████  ██████  ██    ██
+		 * ██   ██ ██ ██         ██    ██    ██ ██   ██  ██  ██
+		 * ███████ ██ ███████    ██    ██    ██ ██████    ████
+		 * ██   ██ ██      ██    ██    ██    ██ ██   ██    ██
+		 * ██   ██ ██ ███████    ██     ██████  ██   ██    ██
+		 */
+		add_action("history", function() {
+			global $settings;
+			
+			http_response_code(501);
+			exit(page_renderer::render_main("Coming soon", "<p>Page history is coming soon!</p>"));
+		});
+		
+		
+		register_save_preprocessor(function(&$pageinfo, &$newsource, &$oldsource) {
+			global $pageindex, $paths, $env;
+			if(!isset($pageinfo->history))
+				$pageinfo->history = [];
+			
+			// Save the *new source* as a revision
+			// This results in 2 copies of the current source, but this is ok
+			// since any time someone changes something, it create a new
+			// revision
+			// Note that we can't save the old source here because we'd have no
+			// clue who edited it since $pageinfo has already been updated by
+			// this point
+			
+			// TODO Store tag changes here
+			$nextRid = count($pageinfo->history); // The next revision id
+			$ridFilename = "$pageinfo->filename.r$nextRid";
+			// Insert a new entry into the history
+			$pageinfo->history[] = [
+				"type" => "edit", // We might want to store other types later (e.g. page moves)
+				"rid" => $nextRid,
+				"timestamp" => time(),
+				"filename" => $ridFilename,
+				"bytechange" => strlen($newsource) - strlen($oldsource),
+				"editor" => $pageinfo->lasteditor
+			];
+			
+			// Save the new source as a revision
+			file_put_contents("$env->storage_prefix$ridFilename", $newsource);
+			
+			// Save the edited pageindex
+			file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
+		});
+	}
+]);
+
+
+
 
 register_module([
 	"name" => "Recent Changes",
@@ -3428,20 +3489,12 @@ register_module([
 			
 			// Read in the new page content
 			$pagedata = $_POST["content"];
-			/*** Note needed anymore as Parsedown has an option that does ***
-			 *** this for us, and is _way_ more intelligent about it.	  ***
-			// Santise it if necessary
-			if($settings->clean_raw_html)
-			{
-				$pagedata = htmlentities($pagedata, ENT_QUOTES);
-				// Un-sanitize greater than signs ('>') as these are commonly
-				// used for blockquotes. This should be a security risk as it is
-				// the less than sign ('<') that is used to open HTML tags.
-				$pagedata = str_replace("&gt;", ">", $pagedata);
-			}
-			 ***/
+			// We don't need to santise the input here as Parsedown has an
+			// option that does this for us, and is _way_ more intelligent about
+			// it.
 			
-			// Read in the new page tags, so long as there are actually some tags to read in
+			// Read in the new page tags, so long as there are actually some
+			// tags to read in
 			$page_tags = [];
 			if(strlen(trim($_POST["tags"])) > 0)
 			{
diff --git a/module_index.json b/module_index.json
index 28a9817..c5eb53e 100644
--- a/module_index.json
+++ b/module_index.json
@@ -35,6 +35,15 @@
         "lastupdate": 1450704211,
         "optional": false
     },
+    {
+        "name": "Page History",
+        "version": "0.1",
+        "author": "Starbeamrainbowlabs",
+        "description": "Adds the ability to keep unlimited page history, limited only by your disk space. Note that this doesn't store file history (yet).",
+        "id": "feature-history",
+        "lastupdate": 1464939567,
+        "optional": false
+    },
     {
         "name": "Recent Changes",
         "version": "0.3.2",
@@ -104,7 +113,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
         "id": "page-edit",
-        "lastupdate": 1462111205,
+        "lastupdate": 1464851031,
         "optional": false
     },
     {
diff --git a/modules/feature-history.php b/modules/feature-history.php
new file mode 100644
index 0000000..3fe25a9
--- /dev/null
+++ b/modules/feature-history.php
@@ -0,0 +1,60 @@
+<?php
+register_module([
+	"name" => "Page History",
+	"version" => "0.1",
+	"author" => "Starbeamrainbowlabs",
+	"description" => "Adds the ability to keep unlimited page history, limited only by your disk space. Note that this doesn't store file history (yet).",
+	"id" => "feature-history",
+	"code" => function() {
+		
+		/*
+		 * ██   ██ ██ ███████ ████████  ██████  ██████  ██    ██
+		 * ██   ██ ██ ██         ██    ██    ██ ██   ██  ██  ██
+		 * ███████ ██ ███████    ██    ██    ██ ██████    ████
+		 * ██   ██ ██      ██    ██    ██    ██ ██   ██    ██
+		 * ██   ██ ██ ███████    ██     ██████  ██   ██    ██
+		 */
+		add_action("history", function() {
+			global $settings;
+			
+			http_response_code(501);
+			exit(page_renderer::render_main("Coming soon", "<p>Page history is coming soon!</p>"));
+		});
+		
+		
+		register_save_preprocessor(function(&$pageinfo, &$newsource, &$oldsource) {
+			global $pageindex, $paths, $env;
+			if(!isset($pageinfo->history))
+				$pageinfo->history = [];
+			
+			// Save the *new source* as a revision
+			// This results in 2 copies of the current source, but this is ok
+			// since any time someone changes something, it create a new
+			// revision
+			// Note that we can't save the old source here because we'd have no
+			// clue who edited it since $pageinfo has already been updated by
+			// this point
+			
+			// TODO Store tag changes here
+			$nextRid = count($pageinfo->history); // The next revision id
+			$ridFilename = "$pageinfo->filename.r$nextRid";
+			// Insert a new entry into the history
+			$pageinfo->history[] = [
+				"type" => "edit", // We might want to store other types later (e.g. page moves)
+				"rid" => $nextRid,
+				"timestamp" => time(),
+				"filename" => $ridFilename,
+				"bytechange" => strlen($newsource) - strlen($oldsource),
+				"editor" => $pageinfo->lasteditor
+			];
+			
+			// Save the new source as a revision
+			file_put_contents("$env->storage_prefix$ridFilename", $newsource);
+			
+			// Save the edited pageindex
+			file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
+		});
+	}
+]);
+
+?>
diff --git a/modules/page-edit.php b/modules/page-edit.php
index 0e2225f..f972e88 100644
--- a/modules/page-edit.php
+++ b/modules/page-edit.php
@@ -129,20 +129,12 @@ register_module([
 			
 			// Read in the new page content
 			$pagedata = $_POST["content"];
-			/*** Note needed anymore as Parsedown has an option that does ***
-			 *** this for us, and is _way_ more intelligent about it.	  ***
-			// Santise it if necessary
-			if($settings->clean_raw_html)
-			{
-				$pagedata = htmlentities($pagedata, ENT_QUOTES);
-				// Un-sanitize greater than signs ('>') as these are commonly
-				// used for blockquotes. This should be a security risk as it is
-				// the less than sign ('<') that is used to open HTML tags.
-				$pagedata = str_replace("&gt;", ">", $pagedata);
-			}
-			 ***/
+			// We don't need to santise the input here as Parsedown has an
+			// option that does this for us, and is _way_ more intelligent about
+			// it.
 			
-			// Read in the new page tags, so long as there are actually some tags to read in
+			// Read in the new page tags, so long as there are actually some
+			// tags to read in
 			$page_tags = [];
 			if(strlen(trim($_POST["tags"])) > 0)
 			{
