initialize(); $server = new static($daux); $page = $server->handle($_REQUEST); } catch (NotFoundException $e) { $page = new ErrorPage("An error occured", $e->getMessage(), $daux->getParams()); } $page->display(); } public function __construct(Daux $daux) { $this->daux = $daux; } public function handle($query = []) { $this->params = $this->daux->getParams(); $request = Helper::getRequest(); $request = urldecode($request); $request_type = isset($query['method']) ? $query['method'] : ''; if ($request == 'first_page') { $request = $this->daux->tree->first_page->uri; } switch ($request_type) { case 'DauxEdit': if (!$this->daux->options['file_editor']) { throw new Exception('Editing is currently disabled in config'); } $content = isset($query['markdown']) ? $query['markdown'] : ''; return $this->saveFile($request, $content); default: return $this->getPage($request); } } private function saveFile($request, $content) { $file = $this->getFile($request); if ($file === false) { throw new NotFoundException('The Page you requested is yet to be made. Try again later.'); } if (!$file->write($content)) { throw new Exception('The file you wish to write to is not writable.'); } return new SimplePage('Success', 'Successfully Edited'); } private function getFile($request) { $tree = $this->daux->tree; $request = explode('/', $request); foreach ($request as $node) { // If the element we're in currently is not a // directory, we failed to find the requested file if (!$tree instanceof Directory) { return false; } // if the node exists in the current request tree, // change the $tree variable to reference the new // node and proceed to the next url part if (isset($tree->value[$node])) { $tree = $tree->value[$node]; continue; } // At this stage, we're in a directory, but no // sub-item matches, so the current node must // be an index page or we failed if ($node !== 'index' && $node !== 'index.html') { return false; } return $tree->getIndexPage(); } // If the entry we found is not a directory, we're done if (!$tree instanceof Directory) { return $tree; } if ($tree->getIndexPage()) { return $tree->getIndexPage(); } return false; } private function getPage($request) { $params = $this->params; $file = $this->getFile($request); if ($file === false) { throw new NotFoundException('The Page you requested is yet to be made. Try again later.'); } $params['request'] = $request; $params['file_uri'] = $file->value; if ($request !== 'index') { $params['entry_page'] = $file->first_page; } return MarkdownPage::fromFile($file, $params); } }