spmenu-wiki/index.php
2023-07-02 01:47:17 +02:00

369 lines
10 KiB
PHP
Executable file

<?php
/*
* spmenu-wiki
*
* Copyright (C) 2007-2011 Steven Frank <http://stevenf.com/>
* Copyright (C) 2023 speedie <speedie@speedie.site>
*
* See LICENSE file for copyright and license details.
*/
spl_autoload_register(function($class){
require str_replace('\\', DIRECTORY_SEPARATOR, ltrim($class, '\\')).'.php';
});
use md\MarkdownExtra;
require_once "config.php";
function __( $label, $alt_word = null ) {
return is_null($alt_word) ? $label : $alt_word;
}
function truncate($text, $chars) {
if (strlen($text) <= $chars) {
return $text;
}
$text = $text." ";
$text = substr($text,0,$chars);
$text = substr($text,0,strrpos($text,' '));
$text = $text."...";
return $text;
}
function printHeader($title, $action, $html, $bodyclass="") {
// TODO: Improve this garbage, I suck at PHP and this is just stuff I found on the internet.
if (META_DESC) {
$nhtml = $html;
$nhtml = preg_replace( '@<p\b[^>]*>(?=.*?<a\b[^>]*>).*?<\@p>@si', '', $nhtml);
$nhtml = preg_replace( '@<(li)[^>]*?>.*?</\\1>@si', '', $nhtml);
$nhtml = preg_replace( '@<(ul)[^>]*?>.*?</\\1>@si', '', $nhtml);
$nhtml = strip_tags($nhtml);
$nhtml = preg_replace('/\s*$^\s*/m', "\n", $nhtml);
$nhtml = truncate($nhtml, 512);
}
print "<!DOCTYPE html>\n";
print "<html lang=\"en\">\n";
if (ENABLE_HEAD) {
print " <head>\n";
if (META_ENC) {
print " <meta charset=\"UTF-8\">\n";
}
if (META_DESC) {
print " <meta name=\"description\" content=\"$nhtml\">\n";
}
if (META_VIEWPORT) {
print " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n";
}
if (ENABLE_FAVICON) {
print " <link rel=\"icon\" href=\"/icons/fav.svg\"/>\n";
}
if (ENABLE_CSS) {
print " <link type=\"text/css\" rel=\"stylesheet\" href=\"" . BASE_URI . "/" . CSS_FILE ."\" />\n";
}
if (ENABLE_TITLE) {
print " <title>".PAGE_TITLE."$title</title>\n";
}
print " </head>\n";
}
print " <body".($bodyclass != "" ? " class=\"$bodyclass\"":"").">\n";
}
function printFooter() {
if (ENABLE_FOOTER_HARDCODED) {
print " </body>\n";
print "<p class=\"footer\">Wiki licensed under the <a href=\"https://git.speedie.site/speedie/spmenu-wiki/raw/branch/master/LICENSE\">MIT</a> license. Articles and images licensed under the CC-BY-SA 4.0 license. <a href=\"https://git.speedie.site/speedie/spmenu-wiki\">Source code</a></p>";
print "</html>";
} else if (ENABLE_FOOTER_FILE && FOOTER_FILE != '') {
print " </body>\n";
include(FOOTER_FILE);
}
}
function getAllPageNames($path = "") {
$filenames = array();
$dir = opendir(PAGES_PATH . "/$path" );
while ( $filename = readdir($dir) ) {
if ( $filename[0] == "." ) {
continue;
}
if ( is_dir( PAGES_PATH . "/$path/$filename" ) ) {
array_push($filenames, ...getAllPageNames( "$path/$filename" ) );
continue;
}
if ( preg_match("/".PAGES_EXT."$/", $filename) != 1) {
continue;
}
$filename = substr($filename, 0, -(strlen(PAGES_EXT)+1) );
$filenames[] = substr("$path/$filename", 1);
}
closedir($dir);
return $filenames;
}
function fileNameForPage($page) {
return PAGES_PATH . "/$page." . PAGES_EXT;
}
function sanitizeFilename($inFileName) {
return str_replace(array('~', '..', '\\', ':', '|', '&'), '-', $inFileName);
}
function pageURL($page) {
return SELF . VIEW . "/".str_replace("%2F", "/", str_replace("%23", "#", urlencode(sanitizeFilename($page))));
}
function pageLink($page, $title, $attributes="") {
return "<a href=\"" . pageURL($page) ."\"$attributes>$title</a>";
}
function checkedExecute(&$msg, $cmd) {
$returnValue = 0;
$output = '';
exec($cmd, $output, $returnValue);
if ($returnValue != 0) {
$msg .= "<br/>Error executing command ".$cmd." (return value: ".$returnValue."): ".implode(" ", $output);
}
return ($returnValue == 0);
}
function gitChangeHandler($commitmsg, &$msg) {
if (!GIT_COMMIT_ENABLED) {
return;
}
if (checkedExecute($msg, "cd ".PAGES_PATH." && git add -A && git commit -m ".escapeshellarg($commitmsg))) {
checkedExecute($msg, "cd ".PAGES_PATH." && git push");
}
}
function toHTMLID($noid) {
return str_replace(" ", "-", $noid);
}
function toHTML($inText) {
$parser = new MarkdownExtra;
$parser->no_markup = true;
$outHTML = $parser->transform($inText);
preg_match_all("/\[\[(.*?)\]\]/", $outHTML, $matches, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($matches[0]); $i++) {
$fullLinkText = $matches[1][$i];
$linkTitleSplit = explode('|', $fullLinkText);
$linkedPage = $linkTitleSplit[0]; // split away an eventual link text
$linkText = (count($linkTitleSplit) > 1) ? $linkTitleSplit[1] : $linkedPage;
$pagePart = explode('#', $linkedPage)[0]; // split away an eventual anchor part
$linkedFilename = fileNameForPage(sanitizeFilename($pagePart));
$exists = file_exists($linkedFilename);
$outHTML = str_replace("[[$fullLinkText]]",
pageLink($linkedPage, $linkText, ($exists? "" : " class=\"noexist\"")), $outHTML);
}
$outHTML = preg_replace("/\{\{(.*?)\}\}/", "<img src=\"" . BASE_URI . "/images/\\1\" alt=\"\\1\" />", $outHTML);
preg_match_all("/<h([1-4])>(.*?)<\/h\\1>/", $outHTML, $matches, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($matches[0]); $i++) {
$prefix = "<h".$matches[1][$i].">";
$caption = $matches[2][$i];
$suffix = substr_replace($prefix, "/", 1, 0);
/*
$outHTML = str_replace("$prefix$caption$suffix",
"$prefix<a id=\"".toHTMLID($caption)."\">$caption</a>$suffix", $outHTML);
*/
}
return $outHTML;
}
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
$newPage = "";
$text = "";
$html = "";
if ($action === 'view') {
$page = preg_match('@^/@', @$_SERVER["PATH_INFO"]) ?
urldecode(substr($_SERVER["PATH_INFO"], 1)) : urldecode(@$_REQUEST['page']);
$page = sanitizeFilename($page);
if ( $page == "" ) {
$page = DEFAULT_PAGE;
}
$filename = fileNameForPage($page);
if ( file_exists($filename) ) {
$text = file_get_contents($filename);
} else {
$newPage = NULL;
include('404.php');
die();
}
}
$oldgitmsg = "";
$triedSave = false;
if ( $action === 'all' && ENABLE_ALL ) {
$pageNames = getAllPageNames();
$filelist = array();
$sortBy = isset($_REQUEST['sortBy']) ? $_REQUEST['sortBy'] : 'name';
if (!in_array($sortBy, array('name', 'recent'))) {
$sortBy = 'name';
}
if ($sortBy === 'name') {
natcasesort($pageNames);
foreach($pageNames as $page) {
$filelist[$page] = filemtime(fileNameForPage($page));
}
} else {
foreach($pageNames as $page) {
$filelist[$page] = filemtime(fileNameForPage($page));
}
arsort($filelist, SORT_NUMERIC);
}
$html .= "<p>".__('Total').": ".count($pageNames)." ".__("articles")."</p>";
$html .= "<table><thead>";
$html .= "<tr>".
"<td>".(($sortBy!='name')?("<a href=\"".SELF."?action=all&sortBy=name\">Name</a>"):"<span class=\"sortBy\">".__('Name')."</span>")."</td>".
"<td>".(($sortBy!='recent')?("<a href=\"".SELF."?action=all&sortBy=recent\">".__('Modified')."</a>"):"<span class=\"sortBy\">".__('Modified')."</span>")."</td>".
"</tr></thead><tbody>";
$date_format = __('date_format', TITLE_DATE);
foreach ($filelist as $pageName => $pageDate) {
$html .= "<tr>".
"<td>".pageLink($pageName, $pageName)."</td>".
"<td valign=\"top\"><nobr>".date( $date_format, $pageDate)."</nobr></td>".
"</tr>\n";
}
$html .= "</tbody></table>\n";
} else if ( $action === 'search' && ENABLE_SEARCH ) {
$matches = 0;
$q = $_REQUEST['q'];
$html .= " <h1>Search: $q</h1>\n";
if ( trim($q) != "" ) {
$html .= " <ul>\n";
$pagenames = getAllPageNames();
$found = FALSE;
$matchingPages = array();
foreach ($pagenames as $searchPage) {
if ($searchPage === $q) {
$found = TRUE;
}
if (preg_match("/{$q}/i", $searchPage)) {
array_unshift($matchingPages, $searchPage);
++$matches;
} else {
$text = file_get_contents(fileNameForPage($searchPage));
if ( preg_match("/{$q}/i", $text) ) {
$matchingPages[] = $searchPage;
++$matches;
}
}
}
foreach ($matchingPages as $page) {
$link = pageLink($page, $page, ($page === $q)? " class=\"literalMatch\"": "");
$html .= " <li>$link</li>\n";
}
$html .= " </ul>\n";
}
$html .= " <p>$matches ".__('matches')."</p>\n";
} else {
$html .= empty($text) ? '' : toHTML($text);
}
$datetime = '';
if (($action === 'all') && ENABLE_ALL) {
$title = __("All");
} else if ( $action === 'search' && ENABLE_SEARCH ) {
$title = __("Search");
} else if ($filename != '') {
$title = $page;
$date_format = __('date_format', TITLE_DATE);
if ( $date_format ) {
$datetime = "<span class=\"titledate\">" . date($date_format, @filemtime($filename)) . "</span>";
}
} else {
$title = __($action);
}
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
printHeader($title, $action, $html);
if (ENABLE_TITLEBAR) {
print " <div class=\"titlebar\"><span class=\"title\">$title</span>$datetime";
print " </div>\n";
}
if (ENABLE_TOOLBAR) {
print " <div class=\"toolbar\">\n";
if (ENABLE_HOME_ICON) {
print " <a href=\"" . SELF . "\"><img src=\"/icons/home.svg\" alt=\"". __(DEFAULT_PAGE) . "\" title=\"". __(DEFAULT_PAGE) . "\" class=\"icon\"></a>\n";
}
if (ENABLE_ALL_ICON) {
print " <a href=\"" . SELF . "?action=all\"><img src=\"/icons/list.svg\" alt=\"". __('All') . "\" title=\"". __('All') . "\" class=\"icon\"></a>\n";
}
if (ENABLE_SEARCH) {
print " <form method=\"post\" action=\"" . SELF . "?action=search\">\n";
print " <input class=\"search\" placeholder=\"". __('Search') ."\" size=\"20\" id=\"search\" type=\"text\" name=\"q\" />\n </form>\n";
}
print " </div>\n";
}
if (SIDEBAR_PAGE != '' && ENABLE_SIDEBAR) {
print " <div class=\"sidebar\">\n\n";
$sidebarFile = fileNameForPage(SIDEBAR_PAGE);
if (file_exists($sidebarFile)) {
$text = file_get_contents($sidebarFile);
} else {
$text = __('Sidebar file could not be found')." ($sidebarFile)";
}
print toHTML($text);
print " </div>\n";
}
print " <div class=\"main\">\n\n";
if(isset($_SESSION['msg']) && $_SESSION['msg'] != '') {
print " <div class=\"note\">".$_SESSION['msg']."</div>";
unset($_SESSION['msg']);
}
print "$html\n";
print " </div>\n";
printFooter();