From 63d0bdd5d62a514ffb0307088ce17dd28768ac7d Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 19 Aug 2016 19:49:39 +0100
Subject: [PATCH] Allow hashes in redirects. Also fix bug in page moving.

---
 build/index.php       | 19 ++++++++++++++++---
 module_index.json     |  4 ++--
 modules/page-view.php | 17 +++++++++++++++--
 3 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/build/index.php b/build/index.php
index d9a5509..bf85570 100755
--- a/build/index.php
+++ b/build/index.php
@@ -4434,7 +4434,7 @@ register_module([
 				$pageindex->$new_name->$key = $value;
 			}
 			unset($pageindex->$page);
-			$pageindex->$new_name->filename = $new_name;
+			$pageindex->$new_name->filename = "$new_name.md";
 			
 			// If this page has an associated file, then we should move that too
 			if(!empty($pageindex->$new_name->uploadedfile))
@@ -4589,7 +4589,7 @@ register_module([
 		 * @apiParam	{number}	revision	The revision number to display.
 		 * @apiParam	{string}	printable	Set to 'yes' to get a printable version of the specified page instead.
 		 *
-		 * @apiError	NonExistentPageError	The page doesn't exist and editing is disabled in the wiki's settings. If editing isn't disabled, you will be redirected to the edit apge instead.
+		 * @apiError	NonExistentPageError	The page doesn't exist and editing is disabled in the wiki's settings. If editing isn't disabled, you will be redirected to the edit page instead.
 		 * @apiError	NonExistentRevisionError	The specified revision was not found.
 		 */
 		
@@ -4635,7 +4635,20 @@ register_module([
 				{
 					// Todo send an explanatory page along with the redirect
 					http_response_code(307);
-					header("location: ?action=$env->action&page=" . $pageindex->$page->redirect_target . "&redirected_from=$env->page");
+					$redirectUrl = "?action=$env->action&redirected_from=$env->page";
+					
+					$hashCode = "";
+					$newPage = $pageindex->$page->redirect_target;
+					if(strpos($newPage, "#") !== false)
+					{
+						$hashCode = substr($newPage, strpos($newPage, "#") + 1);
+						$newPage = substr($newPage, 0, strpos($newPage, "#"));
+					}
+					$newPage .= "&page=" . $pageindex->$page->redirect_target;
+					if(strlen($hashCode) > 0)
+						$newPage .= "#$hashCode";
+					
+					header("location: $redirectUrl");
 					exit();
 				}
 			}
diff --git a/module_index.json b/module_index.json
index 12cbbc7..85775e6 100755
--- a/module_index.json
+++ b/module_index.json
@@ -167,7 +167,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds an action to allow administrators to move pages.",
         "id": "page-move",
-        "lastupdate": 1466582736,
+        "lastupdate": 1471631964,
         "optional": false
     },
     {
@@ -185,7 +185,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Allows you to view pages. You really should include this one.",
         "id": "page-view",
-        "lastupdate": 1466969288,
+        "lastupdate": 1471632495,
         "optional": false
     },
     {
diff --git a/modules/page-view.php b/modules/page-view.php
index 688ee55..206440d 100644
--- a/modules/page-view.php
+++ b/modules/page-view.php
@@ -16,7 +16,7 @@ register_module([
 		 * @apiParam	{number}	revision	The revision number to display.
 		 * @apiParam	{string}	printable	Set to 'yes' to get a printable version of the specified page instead.
 		 *
-		 * @apiError	NonExistentPageError	The page doesn't exist and editing is disabled in the wiki's settings. If editing isn't disabled, you will be redirected to the edit apge instead.
+		 * @apiError	NonExistentPageError	The page doesn't exist and editing is disabled in the wiki's settings. If editing isn't disabled, you will be redirected to the edit page instead.
 		 * @apiError	NonExistentRevisionError	The specified revision was not found.
 		 */
 		
@@ -62,7 +62,20 @@ register_module([
 				{
 					// Todo send an explanatory page along with the redirect
 					http_response_code(307);
-					header("location: ?action=$env->action&page=" . $pageindex->$page->redirect_target . "&redirected_from=$env->page");
+					$redirectUrl = "?action=$env->action&redirected_from=$env->page";
+					
+					$hashCode = "";
+					$newPage = $pageindex->$page->redirect_target;
+					if(strpos($newPage, "#") !== false)
+					{
+						$hashCode = substr($newPage, strpos($newPage, "#") + 1);
+						$newPage = substr($newPage, 0, strpos($newPage, "#"));
+					}
+					$newPage .= "&page=" . $pageindex->$page->redirect_target;
+					if(strlen($hashCode) > 0)
+						$newPage .= "#$hashCode";
+					
+					header("location: $redirectUrl");
 					exit();
 				}
 			}
