2015-09-19 09:19:56 +00:00
< ? php
register_module ([
" name " => " Page viewer " ,
2020-07-12 13:45:56 +00:00
" version " => " 0.16.10 " ,
2015-09-19 09:19:56 +00:00
" author " => " Starbeamrainbowlabs " ,
2016-03-25 11:41:30 +00:00
" description " => " Allows you to view pages. You really should include this one. " ,
2015-09-19 09:19:56 +00:00
" id " => " page-view " ,
2020-03-11 23:51:49 +00:00
// Another random protection to ensure the credits are included
// The aim is to make it as annoying as possible to disentangle the credits from the rest of Pepperminty Wiki
// If you're reading this, you should know that credits are really important - lots of people have put in a huge amount of time and effort to make Pepperminty Wiki what it is today!
" depends " => [ " page-credits " ],
2015-09-19 09:19:56 +00:00
" code " => function () {
2016-06-15 18:20:03 +00:00
/**
2018-02-14 22:36:29 +00:00
* @ api { get } ? action = view [ & page = { pageName }][ & revision = rid ][ & printable = yes ][ & mode = { mode }] View a page
2016-06-15 18:20:03 +00:00
* @ apiName View
* @ apiGroup Page
* @ apiPermission Anonymous
*
* @ apiUse PageParameter
* @ apiParam { number } revision The revision number to display .
2018-02-14 22:36:29 +00:00
* @ apiParam { string } mode Optional . The display mode to use . Can hold the following values : 'normal' - The default . Sends a normal page . 'printable' - Sends a printable version of the page . 'contentonly' - Sends only the content of the page , not the extra stuff around it . 'parsedsourceonly' - Sends only the raw rendered source of the page , as it appears just after it has come out of the page parser . Useful for writing external tools ( see also the `raw` action ) .
2016-06-15 18:20:03 +00:00
*
2016-08-19 18:49:39 +00:00
* @ 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-12-26 12:55:19 +00:00
/*
* ██ ██ ██ ███████ ██ ██
* ██ ██ ██ ██ ██ ██
* ██ ██ ██ █████ ██ █ ██
* ██ ██ ██ ██ ██ ███ ██
* ████ ██ ███████ ███ ███
*/
2015-09-19 09:19:56 +00:00
add_action ( " view " , function () {
2015-10-04 08:10:14 +00:00
global $pageindex , $settings , $env ;
2015-09-19 09:19:56 +00:00
// Check to make sure that the page exists
2015-09-22 13:34:18 +00:00
$page = $env -> page ;
2019-01-30 21:34:24 +00:00
if ( ! isset ( $pageindex -> $page )) {
// TODO: make this intelligent so we only redirect if the user is actually able to create the page
if ( $settings -> editing ) {
2015-09-19 09:19:56 +00:00
// Editing is enabled, redirect to the editing page
2020-06-04 18:11:29 +00:00
$redirectUrl = " index.php?action=edit&newpage=yes&page= " . rawurlencode ( $env -> page );
if ( isset ( $_GET [ " redirected_from " ]))
$redirectUrl .= " &redirected_from= " . rawurlencode ( $_GET [ " redirected_from " ]);
2015-09-19 09:19:56 +00:00
http_response_code ( 307 ); // Temporary redirect
2020-06-04 18:11:29 +00:00
header ( " location: $redirectUrl " );
2015-09-19 09:19:56 +00:00
exit ();
2019-01-30 21:34:24 +00:00
} else {
2015-09-19 09:19:56 +00:00
// Editing is disabled, show an error message
http_response_code ( 404 );
2016-06-04 18:13:40 +00:00
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
2017-06-18 13:54:52 +00:00
header ( " last-modified: " . gmdate ( 'D, d M Y H:i:s T' , $pageindex -> { $env -> page } -> lastmodified ));
2015-10-03 11:26:46 +00:00
// Perform a redirect if the requested page is a redirect page
2020-07-07 20:10:38 +00:00
if ( isset ( $pageindex -> $page -> redirect ) &&
2020-07-12 13:45:56 +00:00
$pageindex -> $page -> redirect === true ) // If this is a redirect page.....
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 ;
2020-07-12 13:45:56 +00:00
if (( isset ( $pageindex -> $page -> redirect_absolute ) &&
$pageindex -> $page -> redirect_absolute == true && // ...and it's absolute....
$settings -> redirect_absolute_enable === false )) // ...and absolute reedirects are enabled
$send_redirect = false ;
2015-10-03 12:29:07 +00:00
2019-01-30 21:34:24 +00:00
if ( $send_redirect ) {
// TODO: Send an explanatory page along with the redirect
2015-10-03 12:29:07 +00:00
http_response_code ( 307 );
2017-06-28 10:42:11 +00:00
$redirectUrl = " ?action= $env->action &redirected_from= " . rawurlencode ( $env -> page );
2016-08-19 18:49:39 +00:00
$hashCode = " " ;
$newPage = $pageindex -> $page -> redirect_target ;
2019-01-30 21:34:24 +00:00
if ( strpos ( $newPage , " # " ) !== false ) {
2016-08-21 13:10:40 +00:00
// Extract the part after the hash symbol
2016-08-19 18:49:39 +00:00
$hashCode = substr ( $newPage , strpos ( $newPage , " # " ) + 1 );
2016-08-21 13:10:40 +00:00
// Remove the hash from the new page name
2016-08-19 18:49:39 +00:00
$newPage = substr ( $newPage , 0 , strpos ( $newPage , " # " ));
}
2017-06-28 10:42:11 +00:00
$redirectUrl .= " &page= " . rawurlencode ( $newPage );
2016-08-21 13:10:40 +00:00
if ( ! empty ( $pageindex -> $newPage -> redirect ))
$redirectUrl .= " &redirect=no " ;
2016-08-19 18:49:39 +00:00
if ( strlen ( $hashCode ) > 0 )
2016-08-21 13:10:40 +00:00
$redirectUrl .= " # $hashCode " ;
2016-08-19 18:49:39 +00:00
2020-07-07 20:10:38 +00:00
// Support absolute redirect URLs
if ( isset ( $pageindex -> $page -> redirect_absolute ) && $pageindex -> $page -> redirect_absolute === true )
$redirectUrl = $pageindex -> $page -> redirect_target ;
2016-08-19 18:49:39 +00:00
header ( " location: $redirectUrl " );
2015-10-03 12:29:07 +00:00
exit ();
}
2015-10-03 11:26:46 +00:00
}
2015-09-22 13:34:18 +00:00
$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 ;
2016-06-04 18:04:50 +00:00
$content = " " ;
2016-10-18 17:34:27 +00:00
if ( ! $env -> is_history_revision )
2016-06-04 18:04:50 +00:00
$content .= " <h1> $env->page </h1> \n " ;
2019-01-30 21:34:24 +00:00
else {
2016-10-18 17:34:27 +00:00
$content .= " <h1>Revision # { $env -> history -> revision_number } of $env->page </h1> \n " ;
2020-05-24 20:47:40 +00:00
$content .= " <p class='system-text-insert revision-note'><em>(Revision saved by { $env -> history -> revision_data -> editor } " . render_timestamp ( $env -> history -> revision_data -> 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 " ;
2016-06-04 18:04:50 +00:00
}
2015-10-03 12:35:44 +00:00
2017-09-15 16:15:11 +00:00
// Add a visit parent page link if we're a subpage
2019-01-30 21:34:24 +00:00
if ( get_page_parent ( $env -> page ) !== false )
2020-05-24 20:47:40 +00:00
$content .= " <p class='system-text-insert link-parent-page'><em><a href='?action=view&page= " . rawurlencode ( get_page_parent ( $env -> page )) . " '>« " . htmlentities ( get_page_parent ( $env -> page )) . " </a></em></p> \n " ;
2017-09-15 16:15:11 +00:00
2015-10-25 14:10:56 +00:00
// Add an extra message if the requester was redirected from another page
2015-10-03 12:35:44 +00:00
if ( isset ( $_GET [ " redirected_from " ]))
2020-05-24 20:47:40 +00:00
$content .= " <p class='system-text-insert'><em>Redirected from <a href='?page= " . rawurlencode ( $_GET [ " redirected_from " ]) . " &redirect=no'> " . $_GET [ " redirected_from " ] . " </a>.</em></p> \n " ;
2015-09-19 09:19:56 +00:00
$parsing_start = microtime ( true );
2016-10-18 17:34:27 +00:00
$rawRenderedSource = parse_page_source ( file_get_contents ( $env -> page_filename ));
$content .= $rawRenderedSource ;
2015-09-19 09:19:56 +00:00
2019-01-30 21:34:24 +00:00
if ( ! empty ( $pageindex -> $page -> tags )) {
2015-10-25 15:45:25 +00:00
$content .= " <ul class='page-tags-display'> \n " ;
foreach ( $pageindex -> $page -> tags as $tag )
2016-11-06 20:51:32 +00:00
$content .= " <li><a href='?action=list-tags&tag= " . rawurlencode ( $tag ) . " '> $tag </a></li> \n " ;
2015-10-25 15:45:25 +00:00
$content .= " \n </ul> \n " ;
2015-10-25 14:10:56 +00:00
}
2015-10-25 16:06:47 +00:00
/* else
2015-10-25 14:10:56 +00:00
{
2015-10-25 16:06:47 +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
2019-01-30 21:34:24 +00:00
if ( $settings -> show_subpages ) {
2015-09-22 13:34:18 +00:00
$subpages = get_object_vars ( get_subpages ( $pageindex , $env -> page ));
2015-09-19 09:19:56 +00:00
2019-01-30 21:34:24 +00:00
if ( count ( $subpages ) > 0 ) {
2015-09-19 09:19:56 +00:00
$content .= " <hr /> " ;
$content .= " Subpages: " ;
2019-01-30 21:34:24 +00:00
foreach ( $subpages as $subpage => $times_removed ) {
if ( $times_removed <= $settings -> subpages_display_depth ) {
2015-09-19 09:19:56 +00:00
$content .= " <a href='?action=view&page= " . rawurlencode ( $subpage ) . " '> $subpage </a>, " ;
}
}
// Remove the last comma from the content
$content = substr ( $content , 0 , - 2 );
}
}
2016-04-30 11:00:48 +00:00
$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 " );
2016-12-17 20:58:59 +00:00
$settings -> footer_message = " $env->page was last edited by { $pageindex -> { $env -> page } ->lasteditor} at " . date ( 'h:ia T \o\n j F Y' , $pageindex -> { $env -> page } -> lastmodified ) . " .</p> \n <p> " . $settings -> footer_message ; // Add the last edited time to the footer
2016-10-18 17:34:27 +00:00
2016-10-18 18:32:06 +00:00
$mode = isset ( $_GET [ " mode " ]) ? strtolower ( trim ( $_GET [ " mode " ])) : " normal " ;
2019-01-30 21:34:24 +00:00
switch ( $mode ) {
2016-10-18 17:34:27 +00:00
case " contentonly " :
// Content only mode: Send only the content of the page
exit ( $content );
case " parsedsourceonly " :
// Parsed source only mode: Send only the raw rendered source
exit ( $rawRenderedSource );
case " printable " :
// Printable mode: Sends a printable version of the page
exit ( page_renderer :: render_minimal ( $title , $content ));
case " normal " :
default :
// Normal mode: Send a normal page
exit ( page_renderer :: render_main ( $title , $content ));
}
2015-09-19 09:19:56 +00:00
});
}
]);
?>