From f1824243f53504c75eebb90c127d893cee9b0042 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 3 Oct 2015 13:29:07 +0100
Subject: [PATCH] Fix redirect sending conditions

---
 build/index.php       | 30 ++++++++++++++++--------------
 module_index.json     |  6 +++---
 modules/page-view.php | 21 ++++++++++++---------
 3 files changed, 31 insertions(+), 26 deletions(-)

diff --git a/build/index.php b/build/index.php
index 351ffd6..3e0332d 100644
--- a/build/index.php
+++ b/build/index.php
@@ -838,7 +838,7 @@ $save_preprocessors = [];
 function register_save_preprocessor($func)
 {
 	global $save_preprocessors;
-	$save_processessors[] = $func;
+	$save_preprocessors[] = $func;
 }
 
 //////////////////////////////////////////////////////////////////
@@ -1055,7 +1055,6 @@ function render_sidebar($pageindex, $root_pagename = "")
 
 
 
-error_log("Initialising redirect page support");
 register_module([
 	"name" => "Redirect pages",
 	"version" => "0.1",
@@ -1064,10 +1063,10 @@ register_module([
 	"id" => "feature-redirect",
 	"code" => function() {
 		register_save_preprocessor(function(&$index_entry, &$pagedata) {
-			error_log("Running redirect check");
 			$matches = [];
-			if(preg_match("/^# ?REDIRECT ?\[\[([^\]]+)\]\]/i", $pagedata) === 1)
+			if(preg_match("/^# ?REDIRECT ?\[\[([^\]]+)\]\]/i", $pagedata, $matches) === 1)
 			{
+				error_log("matches: " . var_export($matches, true));
 				// We have found a redirect page!
 				// Update the metadata to reflect this.
 				$index_entry->redirect = true;
@@ -1347,7 +1346,7 @@ register_module([
 				else
 					http_response_code(200);
 				
-				header("location: index.php?page=$env->page&edit_status=success");
+				header("location: index.php?page=$env->page&edit_status=success&redirect=no");
 				exit();
 			}
 			else
@@ -1793,16 +1792,19 @@ register_module([
 			
 			// Perform a redirect if the requested page is a redirect page
 			if(isset($pageindex->$page->redirect) &&
-			   $pageindex->$page->redirect === true &&
-			   ( // Make sure that the redirect GET paramter isn'tset to 'no'
-				   isset($_GET["redirect"]) &&
-				   $_GET["redirect"] !== "no"
-			   ))
+			   $pageindex->$page->redirect === true)
 			{
-				// Todo send an explanatory page along with the redirect
-				http_response_code(307);
-				header("location: ?action=view&page=" . $pageindex->$page->redirect_target . "&redirected_from=$env->page");
-				exit();
+				$send_redirect = true;
+				if(isset($_GET["redirect"]) && $_GET["redirect"] == "no")
+					$send_redirect = false;
+				
+				if($send_redirect)
+				{
+					// Todo send an explanatory page along with the redirect
+					http_response_code(307);
+					header("location: ?action=view&page=" . $pageindex->$page->redirect_target . "&redirected_from=$env->page");
+					exit();
+				}
 			}
 			
 			$title = "$env->page - $settings->sitename";
diff --git a/module_index.json b/module_index.json
index 1ba1523..7bad021 100644
--- a/module_index.json
+++ b/module_index.json
@@ -41,7 +41,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds support for redirect pages. Uses the same syntax that Mediawiki does.",
         "id": "feature-redirect",
-        "lastupdate": 1443872212,
+        "lastupdate": 1443874989,
         "optional": false
     },
     {
@@ -68,7 +68,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": 1443872342,
+        "lastupdate": 1443874970,
         "optional": false
     },
     {
@@ -140,7 +140,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Allows you to view pages. You reallyshould include this one.",
         "id": "page-view",
-        "lastupdate": 1443871241,
+        "lastupdate": 1443875269,
         "optional": false
     },
     {
diff --git a/modules/page-view.php b/modules/page-view.php
index a8f02a4..afbecc0 100644
--- a/modules/page-view.php
+++ b/modules/page-view.php
@@ -31,16 +31,19 @@ register_module([
 			
 			// Perform a redirect if the requested page is a redirect page
 			if(isset($pageindex->$page->redirect) &&
-			   $pageindex->$page->redirect === true &&
-			   ( // Make sure that the redirect GET paramter isn'tset to 'no'
-				   isset($_GET["redirect"]) &&
-				   $_GET["redirect"] !== "no"
-			   ))
+			   $pageindex->$page->redirect === true)
 			{
-				// Todo send an explanatory page along with the redirect
-				http_response_code(307);
-				header("location: ?action=view&page=" . $pageindex->$page->redirect_target . "&redirected_from=$env->page");
-				exit();
+				$send_redirect = true;
+				if(isset($_GET["redirect"]) && $_GET["redirect"] == "no")
+					$send_redirect = false;
+				
+				if($send_redirect)
+				{
+					// Todo send an explanatory page along with the redirect
+					http_response_code(307);
+					header("location: ?action=view&page=" . $pageindex->$page->redirect_target . "&redirected_from=$env->page");
+					exit();
+				}
 			}
 			
 			$title = "$env->page - $settings->sitename";
