Allow Pico::$requestFile to point to somewhere outside content_dir

Bugfix; Refactoring Pico::load404Content() and Pico::discoverCurrentPage()
This commit is contained in:
Daniel Rudolf 2016-03-03 00:03:53 +01:00
parent a2aa46fd0e
commit 8426a53f63

View File

@ -687,18 +687,28 @@ class Pico
*/ */
public function load404Content($file) public function load404Content($file)
{ {
$errorFileDir = substr($file, strlen($this->getConfig('content_dir'))); $contentDir = $this->getConfig('content_dir');
do { $contentDirLength = strlen($contentDir);
if (substr($file, 0, $contentDirLength) === $contentDir) {
$errorFileDir = substr($file, $contentDirLength);
while ($errorFileDir !== '.') {
$errorFileDir = dirname($errorFileDir); $errorFileDir = dirname($errorFileDir);
$errorFile = $errorFileDir . '/404' . $this->getConfig('content_ext'); $errorFile = $errorFileDir . '/404' . $this->getConfig('content_ext');
} while (!file_exists($this->getConfig('content_dir') . $errorFile) && ($errorFileDir !== '.'));
if (!file_exists($this->getConfig('content_dir') . $errorFile)) { if (file_exists($this->getConfig('content_dir') . $errorFile)) {
$errorFile = ($errorFileDir === '.') ? '404' . $this->getConfig('content_ext') : $errorFile; return $this->loadFileContent($this->getConfig('content_dir') . $errorFile);
throw new RuntimeException('Required "' . $this->getConfig('content_dir') . $errorFile . '" not found'); }
}
} elseif (file_exists($this->getConfig('content_dir') . '404' . $this->getConfig('content_ext'))) {
// provided that the requested file is not in the regular
// content directory, fallback to Pico's global `404.md`
return $this->loadFileContent($this->getConfig('content_dir') . '404' . $this->getConfig('content_ext'));
} }
return $this->loadFileContent($this->getConfig('content_dir') . $errorFile); $errorFile = $this->getConfig('content_dir') . '404' . $this->getConfig('content_ext');
throw new RuntimeException('Required "' . $errorFile . '" not found');
} }
/** /**
@ -1060,8 +1070,16 @@ class Pico
$pageIds = array_keys($this->pages); $pageIds = array_keys($this->pages);
$contentDir = $this->getConfig('content_dir'); $contentDir = $this->getConfig('content_dir');
$contentDirLength = strlen($contentDir);
// the requested file is not in the regular content directory, therefore its ID
// isn't specified and it's impossible to determine the current page automatically
if (substr($this->requestFile, 0, $contentDirLength) !== $contentDir) {
return;
}
$contentExt = $this->getConfig('content_ext'); $contentExt = $this->getConfig('content_ext');
$currentPageId = substr($this->requestFile, strlen($contentDir), -strlen($contentExt)); $currentPageId = substr($this->requestFile, $contentDirLength, -strlen($contentExt));
$currentPageIndex = array_search($currentPageId, $pageIds); $currentPageIndex = array_search($currentPageId, $pageIds);
if ($currentPageIndex !== false) { if ($currentPageIndex !== false) {
$this->currentPage = &$this->pages[$currentPageId]; $this->currentPage = &$this->pages[$currentPageId];