mirror of
https://github.com/til-schneider/slim-wiki.git
synced 2025-08-05 08:07:35 +02:00
Moved finding the article file into a helper method and corrected headers
This commit is contained in:
@@ -10,19 +10,12 @@ class Main {
|
|||||||
// - $requestPathArray: E.g. array('myfolder', 'mypage')
|
// - $requestPathArray: E.g. array('myfolder', 'mypage')
|
||||||
public function dispatch($baseUrl, $basePath, $requestPathArray) {
|
public function dispatch($baseUrl, $basePath, $requestPathArray) {
|
||||||
$articleBaseDir = realpath(__DIR__ . '/../../articles') . '/';
|
$articleBaseDir = realpath(__DIR__ . '/../../articles') . '/';
|
||||||
$articleFilename = $articleBaseDir . implode('/', $requestPathArray);
|
$articleFilename = $this->getArticleFilename($articleBaseDir, $requestPathArray);
|
||||||
|
if ($articleFilename == null) {
|
||||||
// Support `index.md` for directories
|
header('HTTP/1.0 404 Not Found');
|
||||||
if (is_dir($articleFilename)) {
|
header('Content-Type:text/html; charset=utf-8');
|
||||||
$articleFilename = rtrim($articleFilename, '/') . '/index.md';
|
echo '<h1>File not found</h1>'; // TODO: Show error page
|
||||||
}
|
} else {
|
||||||
|
|
||||||
// Make the extension `.md` optional
|
|
||||||
if (! file_exists($articleFilename) && file_exists($articleFilename . '.md')) {
|
|
||||||
$articleFilename .= '.md';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (($articleFilename == realpath($articleFilename)) && file_exists($articleFilename) && is_readable($articleFilename)) {
|
|
||||||
$wikiName = 'Slim Wiki'; // TODO: Make this configurable
|
$wikiName = 'Slim Wiki'; // TODO: Make this configurable
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
@@ -36,14 +29,33 @@ class Main {
|
|||||||
$data['articleHtml'] = Parsedown::instance()->text($articleContent);
|
$data['articleHtml'] = Parsedown::instance()->text($articleContent);
|
||||||
|
|
||||||
$this->renderPage($data);
|
$this->renderPage($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getArticleFilename($articleBaseDir, $requestPathArray) {
|
||||||
|
$articleFilename = $articleBaseDir . implode('/', $requestPathArray);
|
||||||
|
|
||||||
|
// Support `index.md` for directories
|
||||||
|
if (is_dir($articleFilename)) {
|
||||||
|
$articleFilename = rtrim($articleFilename, '/') . '/index.md';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make the extension `.md` optional
|
||||||
|
if (! file_exists($articleFilename) && file_exists($articleFilename . '.md')) {
|
||||||
|
$articleFilename .= '.md';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($articleFilename != realpath($articleFilename)) {
|
||||||
|
// Attempt to break out of article base directory (e.g. `../../outside.ext`)
|
||||||
|
return null;
|
||||||
|
} else if (file_exists($articleFilename) && is_readable($articleFilename)) {
|
||||||
|
return $articleFilename;
|
||||||
} else {
|
} else {
|
||||||
// TODO: Show error page
|
return null;
|
||||||
echo '<p style="color:#990000">File does not exist or is not readable: '.$articleFilename.'</p>';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function createBreadcrumbs($articleBaseDir, $requestPathArray, $wikiName) {
|
private function createBreadcrumbs($articleBaseDir, $requestPathArray, $wikiName) {
|
||||||
$pathCount = count($requestPathArray);
|
$pathCount = count($requestPathArray);
|
||||||
$breadcrumbArray = array(array('name' => $wikiName, 'path' => '', 'active' => ($pathCount == 0)));
|
$breadcrumbArray = array(array('name' => $wikiName, 'path' => '', 'active' => ($pathCount == 0)));
|
||||||
@@ -66,8 +78,9 @@ class Main {
|
|||||||
return $breadcrumbArray;
|
return $breadcrumbArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function renderPage($data) {
|
private function renderPage($data) {
|
||||||
|
header('Content-Type:text/html; charset=utf-8');
|
||||||
|
|
||||||
include(__DIR__ . '/../layout/page.php');
|
include(__DIR__ . '/../layout/page.php');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user