1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-12-23 01:55:01 +00:00
Pepperminty-Wiki/modules/page-edit.php

178 lines
6.2 KiB
PHP
Raw Normal View History

2015-09-19 09:19:56 +00:00
<?php
register_module([
"name" => "Page editor",
2015-10-04 08:00:10 +00:00
"version" => "0.11",
2015-09-19 09:19:56 +00:00
"author" => "Starbeamrainbowlabs",
"description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
"id" => "page-edit",
"code" => function() {
/*
* _ _ _
* ___ __| (_) |_
* / _ \/ _` | | __|
* | __/ (_| | | |_
* \___|\__,_|_|\__|
* %edit%
*/
add_action("edit", function() {
global $pageindex, $settings, $env;
2015-09-19 09:19:56 +00:00
$filename = "$env->page.md";
2015-09-22 13:48:59 +00:00
$page = $env->page;
2015-09-19 09:19:56 +00:00
$creatingpage = !isset($pageindex->$page);
if((isset($_GET["newpage"]) and $_GET["newpage"] == "true") or $creatingpage)
{
$title = "Creating $env->page";
2015-09-19 09:19:56 +00:00
}
else
{
$title = "Editing $env->page";
2015-09-19 09:19:56 +00:00
}
$pagetext = "";
if(isset($pageindex->$page))
{
$pagetext = file_get_contents($filename);
}
2015-09-30 10:26:22 +00:00
if((!$env->is_logged_in and !$settings->anonedits) or // if we aren't logged in and anonymous edits are disbled
!$settings->editing or// or editing is disabled
2015-10-03 11:41:35 +00:00
(
isset($pageindex->$page) and // the page exists
isset($pageindex->$page->protect) and // the protect property exists
$pageindex->$page->protect and // the protect property is true
!$env->is_admin // the user isn't an admin
)
)
2015-09-19 09:19:56 +00:00
{
if(!$creatingpage)
{
// The page already exists - let the user view the page source
2015-09-30 10:26:22 +00:00
exit(page_renderer::render_main("Viewing source for $env->page", "<p>$settings->sitename does not allow anonymous users to make edits. If you are in fact logged in, then this page is probably protected, and you aren't an administrator or moderator. You can view the source of $env->page below, but you can't edit it.</p><textarea name='content' readonly>$pagetext</textarea>"));
2015-09-19 09:19:56 +00:00
}
else
{
http_response_code(404);
exit(page_renderer::render_main("404 - $env->page", "<p>The page <code>$env->page</code> does not exist, but you do not have permission to create it.</p><p>If you haven't already, perhaps you should try <a href='index.php?action=login'>logging in</a>.</p>"));
2015-09-19 09:19:56 +00:00
}
}
$content = "<h1>$title</h1>";
if(!$env->is_logged_in and $settings->anonedits)
2015-09-19 09:19:56 +00:00
{
$content .= "<p><strong>Warning: You are not logged in! Your IP address <em>may</em> be recorded.</strong></p>";
}
$content .= "<form method='post' action='index.php?action=save&page=" . rawurlencode($page) . "&action=save'>
<textarea name='content'>$pagetext</textarea>
<p>$settings->editing_message</p>
2015-09-19 09:19:56 +00:00
<input type='submit' value='Save Page' />
</form>";
exit(page_renderer::render_main("$title - $settings->sitename", $content));
});
/*
*
* ___ __ ___ _____
* / __|/ _` \ \ / / _ \
* \__ \ (_| |\ V / __/
* |___/\__,_| \_/ \___|
* %save%
*/
add_action("save", function() {
global $pageindex, $settings, $env, $save_preprocessors;
2015-09-19 09:19:56 +00:00
if(!$settings->editing)
{
header("location: index.php?page=$env->page");
2015-09-19 09:19:56 +00:00
exit(page_renderer::render_main("Error saving edit", "<p>Editing is currently disabled on this wiki.</p>"));
}
if(!$env->is_logged_in and !$settings->anonedits)
2015-09-19 09:19:56 +00:00
{
http_response_code(403);
header("refresh: 5; url=index.php?page=$env->page");
2015-09-19 09:19:56 +00:00
exit("You are not logged in, so you are not allowed to save pages on $settings->sitename. Redirecting in 5 seconds....");
}
2015-09-30 10:26:22 +00:00
$page = $env->page;
2015-10-03 11:41:35 +00:00
if((
isset($pageindex->$page) and
isset($pageindex->page->protect) and
$pageindex->$page->protect
) and !$env->is_admin)
2015-09-30 10:26:22 +00:00
{
http_response_code(403);
header("refresh: 5; url=index.php?page=$env->page");
exit("$env->page is protected, and you aren't logged in as an administrastor or moderator. Your edit was not saved. Redirecting in 5 seconds...");
}
2015-09-19 09:19:56 +00:00
if(!isset($_POST["content"]))
{
http_response_code(400);
header("refresh: 5; url=index.php?page=$env->page");
2015-09-19 09:19:56 +00:00
exit("Bad request: No content specified.");
}
// Make sure that the directory in which the page needs to be saved exists
if(!is_dir(dirname("$env->page.md")))
2015-09-19 09:19:56 +00:00
{
// Recursively create the directory if needed
mkdir(dirname("$env->page.md"), null, true);
2015-09-19 09:19:56 +00:00
}
$pagedata = htmlentities($_POST["content"], ENT_QUOTES);
2015-09-19 09:19:56 +00:00
if(file_put_contents("$env->page.md", $pagedata) !== false)
2015-09-19 09:19:56 +00:00
{
$page = $env->page;
2015-09-19 09:19:56 +00:00
// Make sure that this page's parents exist
check_subpage_parents($page);
//update the page index
if(!isset($pageindex->$page))
{
$pageindex->$page = new stdClass();
$pageindex->$page->filename = "$env->page.md";
2015-09-19 09:19:56 +00:00
}
$pageindex->$page->size = strlen($_POST["content"]);
$pageindex->$page->lastmodified = time();
2015-09-30 10:26:22 +00:00
if($env->is_logged_in)
$pageindex->$page->lasteditor = utf8_encode($env->user);
2015-09-19 09:19:56 +00:00
else
$pageindex->$page->lasteditor = utf8_encode("anonymous");
// A hack to resave the pagedata if the preprocessors have
// changed it. We need this because the preprocessors *must*
// run _after_ the pageindex has been updated.
$pagedata_orig = $pagedata;
// Execute all the preprocessors
foreach($save_preprocessors as $func)
{
$func($pageindex->$page, $pagedata);
}
if($pagedata !== $pagedata_orig)
file_put_contents("$env->page.md", $pagedata);
2015-09-19 09:19:56 +00:00
file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT));
if(isset($_GET["newpage"]))
http_response_code(201);
else
http_response_code(200);
header("location: index.php?page=$env->page&edit_status=success&redirect=no");
2015-09-19 09:19:56 +00:00
exit();
}
else
{
http_response_code(507);
exit(page_renderer::render_main("Error saving page - $settings->sitename", "<p>$settings->sitename failed to write your changes to the disk. Your changes have not been saved, but you might be able to recover your edit by pressing the back button in your browser.</p>
<p>Please tell the administrator of this wiki (" . $settings->admindetails["name"] . ") about this problem.</p>"));
}
});
}
]);
?>