* Copyright (C) 2023 speedie * * 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>@si', '', $nhtml); $nhtml = preg_replace( '@<(li)[^>]*?>.*?@si', '', $nhtml); $nhtml = preg_replace( '@<(ul)[^>]*?>.*?@si', '', $nhtml); $nhtml = strip_tags($nhtml); $nhtml = preg_replace('/\s*$^\s*/m', "\n", $nhtml); $nhtml = truncate($nhtml, 512); } print "\n"; print "\n"; if (ENABLE_HEAD) { print " \n"; if (META_ENC) { print " \n"; } if (META_DESC) { print " \n"; } if (META_VIEWPORT) { print " \n"; } if (ENABLE_FAVICON) { print " \n"; } if (ENABLE_CSS) { print " \n"; } if (ENABLE_TITLE) { print " ".PAGE_TITLE."$title\n"; } print " \n"; } print " \n"; } function printFooter() { if (ENABLE_FOOTER_HARDCODED) { print " \n"; print "

Wiki licensed under the MIT license. Articles and images licensed under the CC-BY-SA 4.0 license. Source code

"; print ""; } else if (ENABLE_FOOTER_FILE && FOOTER_FILE != '') { print " \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 "$title"; } function checkedExecute(&$msg, $cmd) { $returnValue = 0; $output = ''; exec($cmd, $output, $returnValue); if ($returnValue != 0) { $msg .= "
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("/\{\{(.*?)\}\}/", "\"\\1\"", $outHTML); preg_match_all("/(.*?)<\/h\\1>/", $outHTML, $matches, PREG_PATTERN_ORDER); for ($i = 0; $i < count($matches[0]); $i++) { $prefix = ""; $caption = $matches[2][$i]; $suffix = substr_replace($prefix, "/", 1, 0); /* $outHTML = str_replace("$prefix$caption$suffix", "$prefix$caption$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 .= "

".__('Total').": ".count($pageNames)." ".__("articles")."

"; $html .= ""; $html .= "". "". "". ""; $date_format = __('date_format', TITLE_DATE); foreach ($filelist as $pageName => $pageDate) { $html .= "". "". "". "\n"; } $html .= "
".(($sortBy!='name')?("Name"):"".__('Name')."")."".(($sortBy!='recent')?("".__('Modified').""):"".__('Modified')."")."
".pageLink($pageName, $pageName)."".date( $date_format, $pageDate)."
\n"; } else if ( $action === 'search' && ENABLE_SEARCH ) { $matches = 0; $q = $_REQUEST['q']; $html .= "

Search: $q

\n"; if ( trim($q) != "" ) { $html .= "
    \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 .= "
  • $link
  • \n"; } $html .= "
\n"; } $html .= "

$matches ".__('matches')."

\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 = "" . date($date_format, @filemtime($filename)) . ""; } } 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 "
$title$datetime"; print "
\n"; } if (ENABLE_TOOLBAR) { print "
\n"; if (ENABLE_HOME_ICON) { print " \"".\n"; } if (ENABLE_ALL_ICON) { print " \"".\n"; } if (ENABLE_SEARCH) { print "
\n"; print " \n
\n"; } print "
\n"; } if (SIDEBAR_PAGE != '' && ENABLE_SIDEBAR) { print "
\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 "
\n"; } print "
\n\n"; if(isset($_SESSION['msg']) && $_SESSION['msg'] != '') { print "
".$_SESSION['msg']."
"; unset($_SESSION['msg']); } print "$html\n"; print "
\n"; printFooter();