From 1813fe73e2335c8f7a205f32bf992305d212873f Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 7 Jul 2020 21:10:38 +0100 Subject: [PATCH] Add absolute redirects --- Changelog.md | 1 + core/05-functions.php | 2 +- modules/feature-redirect.php | 14 +++++++++++++- modules/page-view.php | 11 +++++++++-- peppermint.guiconfig.json | 1 + 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Changelog.md b/Changelog.md index e38ef10..be0a1f6 100644 --- a/Changelog.md +++ b/Changelog.md @@ -30,6 +30,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t - Squashed a warning when using the fenced code block syntax - If a redirect page sends you to create a page that doesn't exist, a link back to the redirect page itself is now displayed - Really fix bots getting into infinite loops on the login page this time by marking all login pages as `noindex, nofollow` with a robots `` tag + - Navigating to a redirect page from a page list will no longer cause you to automatically follow the redirect ## v0.21 diff --git a/core/05-functions.php b/core/05-functions.php index 406a2b3..895d8c6 100644 --- a/core/05-functions.php +++ b/core/05-functions.php @@ -61,7 +61,7 @@ function url_stem( $s = false, bool $use_forwarded_host = false) : string { */ function human_filesize($bytes, $decimals = 2) { - $sz = ["b", "kb", "mb", "gb", "tb", "pb", "eb", "yb", "zb"]; + $sz = ["b", "kib", "mib", "gib", "tib", "pib", "eib", "yib", "zib"]; $factor = floor((strlen($bytes) - 1) / 3); $result = round($bytes / pow(1024, $factor), $decimals); return $result . @$sz[$factor]; diff --git a/modules/feature-redirect.php b/modules/feature-redirect.php index f422bfe..ad50dd8 100644 --- a/modules/feature-redirect.php +++ b/modules/feature-redirect.php @@ -10,8 +10,11 @@ register_module([ register_save_preprocessor("update_redirect_metadata"); + $help_html = "

$settings->sitename supports redirect pages. To create a redirect page, enter something like # REDIRECT [[pagename]] on the first line of the redirect page's content. This must appear as the first line of the page, with no whitespace before it. You can include content beneath the redirect if you want, too (such as a reason for redirecting the page).

"; + if($settings->redirect_absolute_enabled == true) $help_html .= "

$settings->sitename also has absolute redirects enabled (e.g. if you want to make your main page point to the all pages list). To make a page an absolute redirect page, enter the following on the first line: # REDIRECT [all pages](?action=list). This example will cause the page to become a redirect to the all pages list. Of course, you can change the ?action=list bit to be any regular URL you like (relative or absolute)

"; + // Register a help section - add_help_section("25-redirect", "Redirect Pages", "

$settings->sitename supports redirect pages. To create a redirect page, enter something like # REDIRECT [[pagename]] on the first line of the redirect page's content. This must appear as the first line of the page, with no whitespace before it. You can include content beneath the redirect if you want, too (such as a reason for redirecting the page).

"); + add_help_section("25-redirect", "Redirect Pages", $help_html); } ]); @@ -31,6 +34,13 @@ function update_redirect_metadata(&$index_entry, &$pagedata) { // Update the metadata to reflect this. $index_entry->redirect = true; $index_entry->redirect_target = $matches[1]; + $index_entry->redirect_absolute = false; + } + // We don't disable absolute redirects here, because it's the view action that processes them - we only register them here. Checking here would result in pages that are supposed to be redirects being missed if redirect_absolute_enabled is turned on after such a page is created. + elseif(preg_match("/^# ?REDIRECT ?\[[^\]]+\]\(([^)]+)\)/", $pagedata, $matches) === 1) { + $index_entry->redirect = true; + $index_entry->redirect_target = $matches[1]; + $index_entry->redirect_absolute = true; } else { @@ -39,6 +49,8 @@ function update_redirect_metadata(&$index_entry, &$pagedata) { unset($index_entry->redirect); if(isset($index_entry->redirect_target)) unset($index_entry->redirect_target); + if(isset($index_entry->redirect_absolute)) + unset($index_entry->redirect_absolute); } } diff --git a/modules/page-view.php b/modules/page-view.php index f1205e6..e6f69c1 100644 --- a/modules/page-view.php +++ b/modules/page-view.php @@ -56,8 +56,11 @@ register_module([ header("last-modified: " . gmdate('D, d M Y H:i:s T', $pageindex->{$env->page}->lastmodified)); // Perform a redirect if the requested page is a redirect page - if(isset($pageindex->$page->redirect) && - $pageindex->$page->redirect === true) + if(isset($pageindex->$page->redirect) && + $pageindex->$page->redirect === true && // If this is a redirect page..... + (isset($pageindex->$page->redirect_absolute) && + $pageindex->$page->redirect_absolute == true && // ...and it's absolute.... + $settings->redirect_absolute_enable === true)) // ...and absolute reedirects are enabled { $send_redirect = true; if(isset($_GET["redirect"]) && $_GET["redirect"] == "no") @@ -82,6 +85,10 @@ register_module([ if(strlen($hashCode) > 0) $redirectUrl .= "#$hashCode"; + // Support absolute redirect URLs + if(isset($pageindex->$page->redirect_absolute) && $pageindex->$page->redirect_absolute === true) + $redirectUrl = $pageindex->$page->redirect_target; + header("location: $redirectUrl"); exit(); } diff --git a/peppermint.guiconfig.json b/peppermint.guiconfig.json index b5e5486..021fe0f 100644 --- a/peppermint.guiconfig.json +++ b/peppermint.guiconfig.json @@ -11,6 +11,7 @@ "subpages_display_depth": { "type": "text", "description": "The depth to which we should display when listing subpages at the bottom the page.", "default": 3}, "random_page_exclude": { "type": "text", "description": "The pages names matching this regular expression won't be chosen when a random page is being picked to send you to by the random action.", "default": "/^Files\\/.*$/i" }, "random_page_exclude_redirects": { "type": "checkbox", "description": "Causes the random action to avoid sending the user to a redirect page.", "default": true }, + "redirect_absolute_enable": { "type": "checkbox", "description": "Whether to enable absolute redirects or not. Enable only if you trust everyone who has edit access to your wiki, as it is possible to redirect a page to anywhere on the Internet - including a malicious website - hence the reason why this is disabled by default for safety.", "default": false }, "footer_message": { "type": "textarea", "description": "A message that will appear at the bottom of every page. May contain HTML.", "default": "All content is under this license. Please make sure that you read and understand the license, especially if you are thinking about copying some (or all) of this site's content, as it may restrict you from doing so." }, "editing_message": { "type": "textarea", "description": "A message that will appear just before the submit button on the editing page. May contain HTML.", "default": "Formatting help (Markdown Cheatsheet)
\nBy submitting your edit or uploading your file, you are agreeing to release your changes under this license. Also note that if you don't want your work to be edited by other users of this site, please don't submit it here!" }, "editing_tags_autocomplete": { "type": "checkbox", "description": "Whether to enable autocomplete for the tags box in the page editor.", "default": true },