1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-11-22 16:33:00 +00:00
Pepperminty-Wiki/modules/page-view.php

189 lines
7.5 KiB
PHP
Raw Normal View History

2015-09-19 09:19:56 +00:00
<?php
register_module([
"name" => "Page viewer",
2016-08-19 18:54:27 +00:00
"version" => "0.15",
2015-09-19 09:19:56 +00:00
"author" => "Starbeamrainbowlabs",
"description" => "Allows you to view pages. You really should include this one.",
2015-09-19 09:19:56 +00:00
"id" => "page-view",
"code" => function() {
2016-06-15 18:20:03 +00:00
/**
* @api {get} ?action=view[&page={pageName}][&revision=rid][&printable=yes] View a page
* @apiName View
* @apiGroup Page
* @apiPermission Anonymous
*
* @apiUse PageParameter
* @apiParam {number} revision The revision number to display.
* @apiParam {string} printable Set to 'yes' to get a printable version of the specified page instead.
*
* @apiError NonExistentPageError The page doesn't exist and editing is disabled in the wiki's settings. If editing isn't disabled, you will be redirected to the edit page instead.
2016-06-15 18:20:03 +00:00
* @apiError NonExistentRevisionError The specified revision was not found.
*/
/*
* ██ ██ ██ ███████ ██ ██
* ██ ██ ██ ██ ██ ██
* ██ ██ ██ █████ ██ ██
* ██ ██ ██ ██ ██ ███ ██
* ████ ██ ███████ ███ ███
*/
2015-09-19 09:19:56 +00:00
add_action("view", function() {
global $pageindex, $settings, $env;
2015-09-19 09:19:56 +00:00
// Check to make sure that the page exists
$page = $env->page;
2015-09-19 09:19:56 +00:00
if(!isset($pageindex->$page))
{
// todo make this intelligent so we only redirect if the user is acutally able to create the page
if($settings->editing)
{
// Editing is enabled, redirect to the editing page
http_response_code(307); // Temporary redirect
header("location: index.php?action=edit&newpage=yes&page=" . rawurlencode($env->page));
2015-09-19 09:19:56 +00:00
exit();
}
else
{
// Editing is disabled, show an error message
http_response_code(404);
exit(page_renderer::render_main("404: Page not found - $env->page - $settings->sitename", "<p>$env->page does not exist.</p><p>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.</p>"));
2015-09-19 09:19:56 +00:00
}
}
2015-10-03 11:26:46 +00:00
// Perform a redirect if the requested page is a redirect page
if(isset($pageindex->$page->redirect) &&
2015-10-03 12:29:07 +00:00
$pageindex->$page->redirect === true)
2015-10-03 11:26:46 +00:00
{
2015-10-03 12:29:07 +00:00
$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);
$redirectUrl = "?action=$env->action&redirected_from=$env->page";
$hashCode = "";
$newPage = $pageindex->$page->redirect_target;
if(strpos($newPage, "#") !== false)
{
2016-08-21 13:10:40 +00:00
// Extract the part after the hash symbol
$hashCode = substr($newPage, strpos($newPage, "#") + 1);
2016-08-21 13:10:40 +00:00
// Remove the hash from the new page name
$newPage = substr($newPage, 0, strpos($newPage, "#"));
}
2016-08-21 13:10:40 +00:00
$redirectUrl .= "&page=$newPage";
if(!empty($pageindex->$newPage->redirect))
$redirectUrl .= "&redirect=no";
if(strlen($hashCode) > 0)
2016-08-21 13:10:40 +00:00
$redirectUrl .= "#$hashCode";
header("location: $redirectUrl");
2015-10-03 12:29:07 +00:00
exit();
}
2015-10-03 11:26:46 +00:00
}
$isHistoryRevision = false;
if(isset($_GET["revision"]) and is_numeric($_GET["revision"]))
{
// We have a revision number!
$isHistoryRevision = true;
$revisionNumber = intval($_GET["revision"]);
$revisionData = $pageindex->{$env->page}->history[$revisionNumber];
// Make sure that the revision exists for later on
if(!isset($pageindex->{$env->page}->history[$revisionNumber]))
{
http_response_code(404);
exit(page_renderer::render_main("404: Revision Not Found - $env->page - $settings->sitename", "<p>Revision #$revisionNumer of $env->page doesn't appear to exist. Try viewing the <a href='?action=history&page=" . rawurlencode($env->page) . "'>list of revisions for $env->page</a>, or viewing <a href='?page=" . rawurlencode($env->page) . "'>the latest revision</a> instead.</p>"));
}
}
$title = "$env->page - $settings->sitename";
2015-09-30 06:08:03 +00:00
if(isset($pageindex->$page->protect) && $pageindex->$page->protect === true)
$title = $settings->protectedpagechar . $title;
$content = "";
if(!$isHistoryRevision)
$content .= "<h1>$env->page</h1>\n";
else
{
$content .= "<h1>Revision #$revisionNumber of $env->page</h1>\n";
2016-06-26 17:08:35 +00:00
$content .= "<p class='revision-note'><em>(Revision saved by $revisionData->editor " . render_timestamp($revisionData->timestamp) . ". <a href='?page=" . rawurlencode($env->page) . "'>Jump to the current revision</a> or see a <a href='?action=history&page=" . rawurlencode($env->page) . "'>list of all revisions</a> for this page.)</em></p>\n";
}
2015-10-25 14:10:56 +00:00
// Add an extra message if the requester was redirected from another page
if(isset($_GET["redirected_from"]))
$content .= "<p><em>Redirected from <a href='?page=" . rawurlencode($_GET["redirected_from"]) . "&redirect=no'>" . $_GET["redirected_from"] . "</a>.</em></p>";
2015-09-19 09:19:56 +00:00
// Construct the filename
$pageFilename = "$env->storage_prefix";
if($isHistoryRevision)
{
$pageFilename .= $pageindex->{$env->page}->history[$revisionNumber]->filename;
}
else
$pageFilename .= $pageindex->{$env->page}->filename;
2015-09-19 09:19:56 +00:00
$parsing_start = microtime(true);
$content .= parse_page_source(file_get_contents($pageFilename));
2015-09-19 09:19:56 +00:00
2015-10-25 14:10:56 +00:00
if(!empty($pageindex->$page->tags))
{
$content .= "<ul class='page-tags-display'>\n";
foreach($pageindex->$page->tags as $tag)
{
$content .= "<li><a href='?action=list-tags&tag=$tag'>$tag</a></li>\n";
}
$content .= "\n</ul>\n";
2015-10-25 14:10:56 +00:00
}
/*else
2015-10-25 14:10:56 +00:00
{
$content .= "<aside class='page-tags-display'><small><em>(No tags yet! Add some by <a href='?action=edit&page=" . rawurlencode($env->page) . "'>editing this page</a>!)</em></small></aside>\n";
}*/
2015-10-25 14:10:56 +00:00
2015-09-19 09:19:56 +00:00
if($settings->show_subpages)
{
$subpages = get_object_vars(get_subpages($pageindex, $env->page));
2015-09-19 09:19:56 +00:00
if(count($subpages) > 0)
{
$content .= "<hr />";
$content .= "Subpages: ";
foreach($subpages as $subpage => $times_removed)
{
if($times_removed <= $settings->subpages_display_depth)
{
$content .= "<a href='?action=view&page=" . rawurlencode($subpage) . "'>$subpage</a>, ";
}
}
// Remove the last comma from the content
$content = substr($content, 0, -2);
}
}
$content .= "\n\t\t<!-- Took " . round((microtime(true) - $parsing_start) * 1000, 2) . "ms to parse page source -->\n";
2015-09-19 09:19:56 +00:00
2016-04-30 10:40:18 +00:00
// Prevent indexing of this page if it's still within the noindex
// time period
if(isset($settings->delayed_indexing_time) and
time() - $pageindex->{$env->page}->lastmodified < $settings->delayed_indexing_time)
header("x-robots-tag: noindex");
// Content only mode: Send only the raw rendered page
if(isset($_GET["contentonly"]) and $_GET["contentonly"] === "yes")
exit(parse_page_source($content));
// Printable: Sends a printable version of the page
2015-09-19 09:19:56 +00:00
if(isset($_GET["printable"]) and $_GET["printable"] === "yes")
exit(page_renderer::render_minimal($title, $content));
// Normal page
2016-04-30 11:25:50 +00:00
$settings->footer_message = "Last edited at " . date('h:ia T \o\n j F Y', $pageindex->{$env->page}->lastmodified) . ".</p>\n<p>"; // Add the last edited time to the footer
exit(page_renderer::render_main($title, $content));
2015-09-19 09:19:56 +00:00
});
}
]);
?>