"Page editor",
"version" => "0.18.1",
"author" => "Starbeamrainbowlabs",
"description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
"id" => "page-edit",
"extra_data" => [
"diff.min.js" => "https://cdnjs.cloudflare.com/ajax/libs/jsdiff/2.2.2/diff.min.js",
"awesomplete.min.js" => "https://cdnjs.cloudflare.com/ajax/libs/awesomplete/1.1.5/awesomplete.min.js",
"awesomplete.min.css" => "https://cdnjs.cloudflare.com/ajax/libs/awesomplete/1.1.5/awesomplete.min.css"
],
"code" => function() {
global $settings, $env;
/**
* @api {get} ?action=edit&page={pageName}[&newpage=yes] Get an editing page
* @apiDescription Gets an editing page for a given page. If you don't have permission to edit the page in question, a view source pagee is returned instead.
* @apiName EditPage
* @apiGroup Editing
* @apiPermission Anonymous
*
* @apiUse PageParameter
* @apiParam {string} newpage Optional. Set to 'yes' if a new page is being created. Only affects a few bits of text here and there, and the HTTP response code recieved on success from the `save` action.
* @apiParam {string} unknownpagename Optional. Set to 'yes' if the name of the page to be created is currently unknown. If set, a page name box will be shown too.
*/
/*
* _ _ _
* ___ __| (_) |_
* / _ \/ _` | | __|
* | __/ (_| | | |_
* \___|\__,_|_|\__|
* %edit%
*/
add_action("edit", function() {
global $pageindex, $settings, $env, $paths;
$unknownpagename = isset($_GET["unknownpagename"]) && strlen(trim($_GET["unknownpagename"])) > 0;
$filename = "$env->storage_prefix$env->page.md";
$creatingpage = !isset($pageindex->{$env->page});
if((isset($_GET["newpage"]) and $_GET["newpage"] == "true") or $creatingpage)
$title = "Creating $env->page";
else if(isset($_POST['preview-edit']) && isset($_POST['content']))
$title = "Preview Edits for $env->page";
else if($unknownpagename)
$title = "Creating new page";
else
$title = "Editing $env->page";
$pagetext = ""; $page_tags = "";
if(isset($pageindex->{$env->page}) && !$unknownpagename)
$pagetext = file_get_contents($filename);
if(!$unknownpagename)
$page_tags = htmlentities(implode(", ", (!empty($pageindex->{$env->page}->tags)) ? $pageindex->{$env->page}->tags : []));
$isOtherUsersPage = false;
if(
$settings->user_page_prefix == mb_substr($env->page, 0, mb_strlen($settings->user_page_prefix)) and // The current page is a user page of some sort
(
!$env->is_logged_in or // the user isn't logged in.....
extract_user_from_userpage($env->page) !== $env->user // ...or it's not under this user's own name
)
) {
$isOtherUsersPage = true;
}
if((!$env->is_logged_in and !$settings->anonedits) or // if we aren't logged in and anonymous edits are disabled
!$settings->editing or // or editing is disabled
(
isset($pageindex->{$env->page}) and // or if the page exists
isset($pageindex->{$env->page}->protect) and // the protect property exists
$pageindex->{$env->page}->protect and // the protect property is true
!$env->is_admin // the user isn't an admin
) or
$isOtherUsersPage // this page actually belongs to another user
)
{
if(!$creatingpage) {
// The page already exists - let the user view the page source
$sourceViewContent = "
$settings->sitename does not allow anonymous users to make edits. You can view the source of $env->page_safe below, but you can't edit it. You could, however, try logging in.
$settings->sitename currently has editing disabled, so you can't make changes to this page at this time. Please contact ".htmlentities($settings->admindetails_name).", $settings->sitename's administrator for more information - their contact details can be found at the bottom of this page. Even so, you can still view the source of this page. It's disabled below:
";
if($isOtherUsersPage)
$sourceViewContent = "
$env->page_safe is a special user page which acutally belongs to " . htmlentities(extract_user_from_userpage($env->page)) . ", another user on $settings->sitename. Because of this, you are not allowed to edit it (though you can always edit your own page and any pages under it if you're logged in). You can, however, vieww it's source below.
";
// Append a view of the page's source
$sourceViewContent .= "";
exit(page_renderer::render_main("Viewing source for $env->page", $sourceViewContent));
}
else {
$errorMessage = "
The page $env->page_safe does not exist, but you do not have permission to create it.
If you haven't already, perhaps you should try logging in.
\n";
if($isOtherUsersPage) {
$errorMessage = "
The page $env->page_safe doesn't exist, but you can't create it because it's a page belonging to another user.
\n";
}
// Include preview, if set
if(isset($_POST['preview-edit']) && isset($_POST['content'])) {
// Need this for the prev-content-hash to prevent the conflict page from appearing
$old_pagetext = $pagetext;
// set the page content to the newly edited content
$pagetext = $_POST['content'];
// Set the tags to the new tags, if needed
if(isset($_POST['tags']))
$page_tags = $_POST['tags'];
// Insert the "view" part of the page we're editing
$content .= "
This is only a preview, so your edits haven't been saved! Scroll down to continue editing.