From 4cabf42666dc36aa814a189c4c220ad7d5b53bfc Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 22 Sep 2015 14:34:18 +0100 Subject: [PATCH] Convert all existing modules over to new $env --- core.php | 4 ++-- modules/action-raw.php | 8 ++++---- modules/page-delete.php | 23 +++++++++++----------- modules/page-edit.php | 40 ++++++++++++++++++++------------------ modules/page-export.php | 6 +++--- modules/page-login.php | 6 +++--- modules/page-logout.php | 10 +++++----- modules/page-move.php | 28 +++++++++++++------------- modules/page-update.php | 6 +++--- modules/page-view.php | 17 ++++++++-------- modules/parser-default.php | 2 -- 11 files changed, 77 insertions(+), 73 deletions(-) diff --git a/core.php b/core.php index d44e60d..2119dbe 100644 --- a/core.php +++ b/core.php @@ -49,9 +49,9 @@ else // and get out of here $env->is_logged_in = false; $env->user = "Anonymous"; - unset($pass); + $env->pass = ""; // Clear the session data - $_SESSION = []; //delete al lthe variables + $_SESSION = []; //delete all the variables session_destroy(); //destroy the session } } diff --git a/modules/action-raw.php b/modules/action-raw.php index 2530b83..b0e84e1 100644 --- a/modules/action-raw.php +++ b/modules/action-raw.php @@ -1,18 +1,18 @@ "Raw page source", - "version" => "0.2", + "version" => "0.3", "author" => "Starbeamrainbowlabs", "description" => "Adds a 'raw' action that shows you the raw source of a page.", "id" => "action-raw", "code" => function() { add_action("raw", function() { - global $page; + global $env; http_response_code(307); - header("x-filename: " . rawurlencode($page) . ".md"); + header("x-filename: " . rawurlencode($env->page) . ".md"); header("content-type: text/markdown"); - exit(file_get_contents("$page.md")); + exit(file_get_contents("$env->page.md")); exit(); }); } diff --git a/modules/page-delete.php b/modules/page-delete.php index d4d2e77..3c84894 100644 --- a/modules/page-delete.php +++ b/modules/page-delete.php @@ -1,36 +1,37 @@ "Page deleter", - "version" => "0.5", + "version" => "0.6", "author" => "Starbeamrainbowlabs", "description" => "Adds an action to allow administrators to delete pages.", "id" => "page-delete", "code" => function() { add_action("delete", function() { - global $pageindex, $settings, $page, $isadmin; + global $pageindex, $settings, $env; if(!$settings->editing) { - exit(page_renderer::render_main("Deleting $page - error", "

You tried to delete $page, but editing is disabled on this wiki.

+ exit(page_renderer::render_main("Deleting $env->page - error", "

You tried to delete $env->page, but editing is disabled on this wiki.

If you wish to delete this page, please re-enable editing on this wiki first.

-

Go back to $page.

+

Go back to $env->page.

Nothing has been changed.

")); } - if(!$isadmin) + if(!$env->is_admin) { - exit(page_renderer::render_main("Deleting $page - error", "

You tried to delete $page, but you are not an admin so you don't have permission to do that.

+ exit(page_renderer::render_main("Deleting $env->page - error", "

You tried to delete $env->page, but you are not an admin so you don't have permission to do that.

You should try logging in as an admin.

")); } if(!isset($_GET["delete"]) or $_GET["delete"] !== "yes") { - exit(page_renderer::render_main("Deleting $page", "

You are about to delete $page. You can't undo this!

-

Click here to delete $page.

-

Click here to go back.")); + exit(page_renderer::render_main("Deleting $env->page", "

You are about to delete $env->page. You can't undo this!

+

Click here to delete $env->page.

+

Click here to go back.")); } + $page = $env->page unset($pageindex->$page); //delete the page from the page index file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT)); //save the new page index - unlink("./$page.md"); //delete the page from the disk + unlink("./$env->page.md"); //delete the page from the disk - exit(page_renderer::render_main("Deleting $page - $settings->sitename", "

$page has been deleted. Go back to the main page.

")); + exit(page_renderer::render_main("Deleting $env->page - $settings->sitename", "

$env->page has been deleted. Go back to the main page.

")); }); } ]); diff --git a/modules/page-edit.php b/modules/page-edit.php index 79ee5e7..dd22429 100644 --- a/modules/page-edit.php +++ b/modules/page-edit.php @@ -1,7 +1,7 @@ "Page editor", - "version" => "0.8", + "version" => "0.9", "author" => "Starbeamrainbowlabs", "description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.", "id" => "page-edit", @@ -17,20 +17,21 @@ register_module([ * %edit% */ add_action("edit", function() { - global $pageindex, $settings, $page, $isloggedin; + global $pageindex, $settings, $env; - $filename = "$page.md"; + $filename = "$env->page.md"; $creatingpage = !isset($pageindex->$page); if((isset($_GET["newpage"]) and $_GET["newpage"] == "true") or $creatingpage) { - $title = "Creating $page"; + $title = "Creating $env->page"; } else { - $title = "Editing $page"; + $title = "Editing $env->page"; } $pagetext = ""; + $page = $env->$page; if(isset($pageindex->$page)) { $pagetext = file_get_contents($filename); @@ -41,17 +42,17 @@ register_module([ if(!$creatingpage) { // The page already exists - let the user view the page source - exit(page_renderer::render_main("Viewing source for $page", "

$settings->sitename does not allow anonymous users to make edits. You can view the source of $page below, but you can't edit it.

")); + exit(page_renderer::render_main("Viewing source for $env->page", "

$settings->sitename does not allow anonymous users to make edits. You can view the source of $env->page below, but you can't edit it.

")); } else { http_response_code(404); - exit(page_renderer::render_main("404 - $page", "

The page $page does not exist, but you do not have permission to create it.

If you haven't already, perhaps you should try logging in.

")); + exit(page_renderer::render_main("404 - $env->page", "

The page $env->page does not exist, but you do not have permission to create it.

If you haven't already, perhaps you should try logging in.

")); } } $content = "

$title

"; - if(!$isloggedin and $settings->anonedits) + if(!$env->is_logged_in and $settings->anonedits) { $content .= "

Warning: You are not logged in! Your IP address may be recorded.

"; } @@ -71,35 +72,36 @@ register_module([ * %save% */ add_action("save", function() { - global $pageindex, $settings, $page, $isloggedin, $user; + global $pageindex, $settings, $env; if(!$settings->editing) { - header("location: index.php?page=$page"); + header("location: index.php?page=$env->page"); exit(page_renderer::render_main("Error saving edit", "

Editing is currently disabled on this wiki.

")); } - if(!$isloggedin and !$settings->anonedits) + if(!$env->is_logged_in and !$settings->anonedits) { http_response_code(403); - header("refresh: 5; url=index.php?page=$page"); + header("refresh: 5; url=index.php?page=$env->page"); exit("You are not logged in, so you are not allowed to save pages on $settings->sitename. Redirecting in 5 seconds...."); } if(!isset($_POST["content"])) { http_response_code(400); - header("refresh: 5; url=index.php?page=$page"); + header("refresh: 5; url=index.php?page=$env->page"); exit("Bad request: No content specified."); } // Make sure that the directory in which the page needs to be saved exists - if(!is_dir(dirname("$page.md"))) + if(!is_dir(dirname("$env->page.md"))) { // Recursively create the directory if needed - mkdir(dirname("$page.md"), null, true); + mkdir(dirname("$env->page.md"), null, true); } - if(file_put_contents("$page.md", htmlentities($_POST["content"]), ENT_QUOTES) !== false) + if(file_put_contents("$env->page.md", htmlentities($_POST["content"]), ENT_QUOTES) !== false) { + $page = $env->page; // Make sure that this page's parents exist check_subpage_parents($page); @@ -107,12 +109,12 @@ register_module([ if(!isset($pageindex->$page)) { $pageindex->$page = new stdClass(); - $pageindex->$page->filename = "$page.md"; + $pageindex->$page->filename = "$env->page.md"; } $pageindex->$page->size = strlen($_POST["content"]); $pageindex->$page->lastmodified = time(); if($isloggedin) - $pageindex->$page->lasteditor = utf8_encode($user); + $pageindex->$page->lasteditor = utf8_encode($env->user); else $pageindex->$page->lasteditor = utf8_encode("anonymous"); @@ -123,7 +125,7 @@ register_module([ else http_response_code(200); - header("location: index.php?page=$page&edit_status=success"); + header("location: index.php?page=$env->page&edit_status=success"); exit(); } else diff --git a/modules/page-export.php b/modules/page-export.php index ebd61c9..46657a8 100644 --- a/modules/page-export.php +++ b/modules/page-export.php @@ -1,15 +1,15 @@ "Export", - "version" => "0.1", + "version" => "0.2", "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", "code" => function() { add_action("export", function() { - global $settings, $pageindex, $isadmin; + global $settings, $pageindex, $env; - if($settings->export_allow_only_admins && !$isadmin) + if($settings->export_allow_only_admins && !$env->is_admin) { http_response_code(401); exit(page_renderer::render("Export error - $settings->sitename", "Only administrators of $settings->sitename are allowed to export the wiki as a zip. Return to the $settings->defaultpage.")); diff --git a/modules/page-login.php b/modules/page-login.php index 9f9553a..a1631a8 100644 --- a/modules/page-login.php +++ b/modules/page-login.php @@ -1,7 +1,7 @@ "Login", - "version" => "0.5", + "version" => "0.6", "author" => "Starbeamrainbowlabs", "description" => "Adds a pair of actions (login and checklogin) that allow users to login. You need this one if you want your users to be able to login.", "id" => "page-login", @@ -41,7 +41,7 @@ register_module([ * %checklogin% |___/ */ add_action("checklogin", function() { - global $settings; + global $settings, $env; //actually do the login if(isset($_POST["user"]) and isset($_POST["pass"])) @@ -51,7 +51,7 @@ register_module([ $pass = $_POST["pass"]; if($settings->users[$user] == hash("sha256", $pass)) { - $isloggedin = true; + $env->is_logged_in = true; $expiretime = time() + 60*60*24*30; //30 days from now $_SESSION["$settings->sessionprefix-user"] = $user; $_SESSION["$settings->sessionprefix-pass"] = hash("sha256", $pass); diff --git a/modules/page-logout.php b/modules/page-logout.php index 1450ecf..bfda77d 100644 --- a/modules/page-logout.php +++ b/modules/page-logout.php @@ -1,16 +1,16 @@ "Logout", - "version" => "0.5", + "version" => "0.6", "author" => "Starbeamrainbowlabs", "description" => "Adds an action to let users user out. For security reasons it is wise to add this module since logging in automatically opens a session that is valid for 30 days.", "id" => "page-logout", "code" => function() { add_action("logout", function() { - global $user, $pass, $isloggedin; - $isloggedin = false; - unset($user); - unset($pass); + global $env; + $env->is_logged_in = false; + unset($env->user); + unset($env->pass); //clear the session variables $_SESSION = []; session_destroy(); diff --git a/modules/page-move.php b/modules/page-move.php index 3b61eed..26ea3a3 100644 --- a/modules/page-move.php +++ b/modules/page-move.php @@ -1,32 +1,32 @@ "Page mover", - "version" => "0.5", + "version" => "0.6", "author" => "Starbeamrainbowlabs", "description" => "Adds an action to allow administrators to move pages.", "id" => "page-move", "code" => function() { add_action("move", function() { - global $pageindex, $settings, $page, $isadmin; + global $pageindex, $settings, $env; if(!$settings->editing) { - exit(page_renderer::render_main("Moving $page - error", "

You tried to move $page, but editing is disabled on this wiki.

+ exit(page_renderer::render_main("Moving $env->page - error", "

You tried to move $env->page, but editing is disabled on this wiki.

If you wish to move this page, please re-enable editing on this wiki first.

-

Go back to $page.

+

Go back to $env->page.

Nothing has been changed.

")); } - if(!$isadmin) + if(!$env->is_admin) { - exit(page_renderer::render_main("Moving $page - Error", "

You tried to move $page, but you do not have permission to do that.

+ exit(page_renderer::render_main("Moving $env->page - Error", "

You tried to move $env->page, but you do not have permission to do that.

You should try logging in as an admin.

")); } if(!isset($_GET["new_name"]) or strlen($_GET["new_name"]) == 0) - exit(page_renderer::render_main("Moving $page", "

Moving $page

+ exit(page_renderer::render_main("Moving $env->page", "

Moving $env->page

- +
@@ -36,12 +36,13 @@ register_module([ $new_name = makepathsafe($_GET["new_name"]); + $page = $env->page; if(!isset($pageindex->$page)) - exit(page_renderer::render_main("Moving $page - Error", "

You tried to move $page to $new_name, but the page with the name $page does not exist in the first place.

+ exit(page_renderer::render_main("Moving $env->page - Error", "

You tried to move $env->page to $new_name, but the page with the name $env->page does not exist in the first place.

Nothing has been changed.

")); - if($page == $new_name) - exit(page_renderer::render_main("Moving $page - Error", "

You tried to move $page, but the new name you gave is the same as it's current name.

+ if($env->page == $new_name) + exit(page_renderer::render_main("Moving $env->page - Error", "

You tried to move $page, but the new name you gave is the same as it's current name.

It is possible that you tried to use some characters in the new name that are not allowed and were removed.

Page names may only contain alphanumeric characters, dashes, and underscores.

")); @@ -55,10 +56,11 @@ register_module([ file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT)); //move the page on the disk - rename("$page.md", "$new_name.md"); + rename("$env->page.md", "$new_name.md"); - exit(page_renderer::render_main("Moving $page", "

$page has been moved to $new_name successfully.

")); + exit(page_renderer::render_main("Moving $env->page", "

$env->page has been moved to $new_name successfully.

")); }); } ]); + ?> diff --git a/modules/page-update.php b/modules/page-update.php index 766bab6..442abb8 100644 --- a/modules/page-update.php +++ b/modules/page-update.php @@ -1,15 +1,15 @@ "Update", - "version" => "0.6", + "version" => "0.6.1", "author" => "Starbeamrainbowlabs", "description" => "Adds an update page that downloads the latest stable version of Pepperminty Wiki. This module is currently outdated as it doesn't save your module preferences.", "id" => "page-update", "code" => function() { add_action("update", function() { - global $settings, $isadmin; + global $settings, $env; - if(!$isadmin) + if(!$env->is_admin) { http_response_code(401); exit(page_renderer::render_main("Update - Error", "

You must be an administrator to do that.

")); diff --git a/modules/page-view.php b/modules/page-view.php index 646c06b..363ad3a 100644 --- a/modules/page-view.php +++ b/modules/page-view.php @@ -1,15 +1,16 @@ "Page viewer", - "version" => "0.8", + "version" => "0.9", "author" => "Starbeamrainbowlabs", "description" => "Allows you to view pages. You should include this one.", "id" => "page-view", "code" => function() { add_action("view", function() { - global $pageindex, $settings, $page, $parse_page_source; + global $pageindex, $settings, $env, $parse_page_source; // Check to make sure that the page exists + $page = $env->page; if(!isset($pageindex->$page)) { // todo make this intelligent so we only redirect if the user is acutally able to create the page @@ -17,26 +18,26 @@ register_module([ { // Editing is enabled, redirect to the editing page http_response_code(307); // Temporary redirect - header("location: index.php?action=edit&newpage=yes&page=" . rawurlencode($page)); + header("location: index.php?action=edit&newpage=yes&page=" . rawurlencode($env->page)); exit(); } else { // Editing is disabled, show an error message http_response_code(404); - exit(page_renderer::render_main("$page - 404 - $settings->sitename", "

$page does not exist.

Since editing is currently disabled on this wiki, you may not create this page. If you feel that this page should exist, try contacting this wiki's Administrator.

")); + exit(page_renderer::render_main("$env->page - 404 - $settings->sitename", "

$env->page does not exist.

Since editing is currently disabled on this wiki, you may not create this page. If you feel that this page should exist, try contacting this wiki's Administrator.

")); } } - $title = "$page - $settings->sitename"; - $content = "

$page

"; + $title = "$env->page - $settings->sitename"; + $content = "

$env->page

"; $parsing_start = microtime(true); - $content .= $parse_page_source(file_get_contents("$page.md")); + $content .= $parse_page_source(file_get_contents("$env->page.md")); if($settings->show_subpages) { - $subpages = get_object_vars(get_subpages($pageindex, $page)); + $subpages = get_object_vars(get_subpages($pageindex, $env->page)); if(count($subpages) > 0) { diff --git a/modules/parser-default.php b/modules/parser-default.php index f0ffdbe..93bff24 100644 --- a/modules/parser-default.php +++ b/modules/parser-default.php @@ -54,11 +54,9 @@ class Slimdown { '/(\*)(.*?)\1/' => '\2', // bold '/(_)(.*?)\1/' => '\2', // emphasis - // todo test these '/!\[(.*)\]\(([^\s]+)\s(\d+.+)\s(left|right)\)/' => '\1', // images with size '/!\[(.*)\]\(([^\s]+)\s(\d+.+)\)/' => '\1', // images with size '/!\[(.*)\]\((.*)\)/' => '\1', // basic images - // todo end '/\[\[([a-zA-Z0-9\_\- ]+)\|([a-zA-Z0-9\_\- ]+)\]\]/' => '\2', //internal links with display text '/\[\[([a-zA-Z0-9\_\- ]+)\]\]/' => '\1', //internal links