From d51c3f163fbaeed63e8b0109e684a810d2a4604a Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 31 Jan 2019 21:41:13 +0000
Subject: [PATCH] Bugfix: Include uploaded files when exporting

---
 Changelog.md            |  1 +
 build/index.php         | 20 +++++++++++++-------
 core.php                | 11 ++++++++---
 module_index.json       |  4 ++--
 modules/page-export.php |  7 ++++---
 5 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 7b07156..f62b384 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -26,6 +26,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
  - Standardise line endings to `\n` (linux)
  - Enhanced setup instructions in README.
  - Long lines in code blocks now wrap correctly.
+ - The `export` action now correctly includes uploaded files alongside their descriptions
 
 ## v0.17.1
 
diff --git a/build/index.php b/build/index.php
index be40404..10655b2 100644
--- a/build/index.php
+++ b/build/index.php
@@ -411,7 +411,7 @@ if($settings->sessionprefix == "auto")
 /////////////////////////////////////////////////////////////////////////////
 /** The version of Pepperminty Wiki currently running. */
 $version = "v0.18-dev";
-$commit = "dc860acfd3db082023b34fd769be7885c521b4cd";
+$commit = "c120902cda82e3efcd32d071ad8993c06fdc79ac";
 /// Environment ///
 /** Holds information about the current request environment. */
 $env = new stdClass();
@@ -457,6 +457,8 @@ $paths->idindex = "idindex.json";
 $paths->statsindex = "statsindex.json";
 /** The interwiki index cache */
 $paths->interwiki_index = "interwiki_index.json";
+/** The cache directory, minus the trailing slash. Contains cached rendered versions of pages. If things don't update, try deleting this folder.  */
+$paths->cache_directory = "._cache";
 
 // Prepend the storage data directory to all the defined paths.
 foreach ($paths as &$path) {
@@ -468,6 +470,10 @@ $paths->settings_file = $settingsFilename;
 /** The prefix to add to uploaded files */
 $paths->upload_file_prefix = "Files/";
 
+// Create the cache directory if it doesn't exist
+if(!is_dir($paths->cache_directory))
+	mkdir($paths->cache_directory, 0700);
+
 session_start();
 // Make sure that the login cookie lasts beyond the end of the user's session
 setcookie(session_name(), session_id(), time() + $settings->sessionlifetime, "", "", false, true);
@@ -2136,13 +2142,12 @@ function add_parser($name, $parser_code)
 /**
  * Parses the specified page source using the parser specified in the settings
  * into HTML.
- * The specified parser may (though it's unilkely) render it to other things.
+ * The specified parser may (though it's unlikely) render it to other things.
  * @package core
  * @param	string	$source	The source to render.
  * @return	string			The source rendered to HTML.
  */
-function parse_page_source($source)
-{
+function parse_page_source($source) {
 	global $settings, $parsers;
 	if(!isset($parsers[$settings->parser]))
 		exit(page_renderer::render_main("Parsing error - $settings->sitename", "<p>Parsing some page source data failed. This is most likely because $settings->sitename has the parser setting set incorrectly. Please contact <a href='mailto:" . hide_email($settings->admindetails_email) . "'>" . $settings->admindetails_name . "</a>, your $settings->sitename Administrator."));
@@ -7665,7 +7670,7 @@ function generate_page_hash($page_data) {
 
 register_module([
 	"name" => "Export",
-	"version" => "0.4",
+	"version" => "0.5",
 	"author" => "Starbeamrainbowlabs",
 	"description" => "Adds a page that you can use to export your wiki as a .zip file. Uses \$settings->export_only_allow_admins, which controls whether only admins are allowed to export the wiki.",
 	"id" => "page-export",
@@ -7710,9 +7715,10 @@ register_module([
 				exit(page_renderer::render("Export error - $settings->sitename", "Pepperminty Wiki was unable to open a temporary file to store the exported data in. Please contact $settings->sitename's administrator (" . $settings->admindetails_name . " at " . hide_email($settings->admindetails_email) . ") for assistance."));
 			}
 			
-			foreach($pageindex as $entry)
-			{
+			foreach($pageindex as $entry) {
 				$zip->addFile("$env->storage_prefix$entry->filename", $entry->filename);
+				if(isset($entry->uploadedfilepath))
+					$zip->addFile($entry->uploadedfilepath);
 			}
 			
 			if($zip->close() !== true)
diff --git a/core.php b/core.php
index 33b5131..1ea2476 100644
--- a/core.php
+++ b/core.php
@@ -56,6 +56,8 @@ $paths->idindex = "idindex.json";
 $paths->statsindex = "statsindex.json";
 /** The interwiki index cache */
 $paths->interwiki_index = "interwiki_index.json";
+/** The cache directory, minus the trailing slash. Contains cached rendered versions of pages. If things don't update, try deleting this folder.  */
+$paths->cache_directory = "._cache";
 
 // Prepend the storage data directory to all the defined paths.
 foreach ($paths as &$path) {
@@ -67,6 +69,10 @@ $paths->settings_file = $settingsFilename;
 /** The prefix to add to uploaded files */
 $paths->upload_file_prefix = "Files/";
 
+// Create the cache directory if it doesn't exist
+if(!is_dir($paths->cache_directory))
+	mkdir($paths->cache_directory, 0700);
+
 session_start();
 // Make sure that the login cookie lasts beyond the end of the user's session
 setcookie(session_name(), session_id(), time() + $settings->sessionlifetime, "", "", false, true);
@@ -1735,13 +1741,12 @@ function add_parser($name, $parser_code)
 /**
  * Parses the specified page source using the parser specified in the settings
  * into HTML.
- * The specified parser may (though it's unilkely) render it to other things.
+ * The specified parser may (though it's unlikely) render it to other things.
  * @package core
  * @param	string	$source	The source to render.
  * @return	string			The source rendered to HTML.
  */
-function parse_page_source($source)
-{
+function parse_page_source($source) {
 	global $settings, $parsers;
 	if(!isset($parsers[$settings->parser]))
 		exit(page_renderer::render_main("Parsing error - $settings->sitename", "<p>Parsing some page source data failed. This is most likely because $settings->sitename has the parser setting set incorrectly. Please contact <a href='mailto:" . hide_email($settings->admindetails_email) . "'>" . $settings->admindetails_name . "</a>, your $settings->sitename Administrator."));
diff --git a/module_index.json b/module_index.json
index 83425e6..fd538f1 100755
--- a/module_index.json
+++ b/module_index.json
@@ -190,11 +190,11 @@
     },
     {
         "name": "Export",
-        "version": "0.4",
+        "version": "0.5",
         "author": "Starbeamrainbowlabs",
         "description": "Adds a page that you can use to export your wiki as a .zip file. Uses $settings->export_only_allow_admins, which controls whether only admins are allowed to export the wiki.",
         "id": "page-export",
-        "lastupdate": 1472230366,
+        "lastupdate": 1548970758,
         "optional": false
     },
     {
diff --git a/modules/page-export.php b/modules/page-export.php
index 27201ac..b319932 100644
--- a/modules/page-export.php
+++ b/modules/page-export.php
@@ -1,7 +1,7 @@
 <?php
 register_module([
 	"name" => "Export",
-	"version" => "0.4",
+	"version" => "0.5",
 	"author" => "Starbeamrainbowlabs",
 	"description" => "Adds a page that you can use to export your wiki as a .zip file. Uses \$settings->export_only_allow_admins, which controls whether only admins are allowed to export the wiki.",
 	"id" => "page-export",
@@ -46,9 +46,10 @@ register_module([
 				exit(page_renderer::render("Export error - $settings->sitename", "Pepperminty Wiki was unable to open a temporary file to store the exported data in. Please contact $settings->sitename's administrator (" . $settings->admindetails_name . " at " . hide_email($settings->admindetails_email) . ") for assistance."));
 			}
 			
-			foreach($pageindex as $entry)
-			{
+			foreach($pageindex as $entry) {
 				$zip->addFile("$env->storage_prefix$entry->filename", $entry->filename);
+				if(isset($entry->uploadedfilepath))
+					$zip->addFile($entry->uploadedfilepath);
 			}
 			
 			if($zip->close() !== true)
