diff --git a/libs/Daux.php b/libs/Daux.php index cbb2ddc..36ffd30 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -1,6 +1,6 @@ mode = $mode; $this->local_base = dirname(__DIR__); - $this->base_url = ''; - - if ($this->mode == Daux::LIVE_MODE) { - $this->host = $_SERVER['HTTP_HOST']; - $this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); - $t = strrpos($this->base_url, '/index.php'); - if ($t != false) { - $this->base_url = substr($this->base_url, 0, $t); - } - if (substr($this->base_url, -1) !== '/') { - $this->base_url .= '/'; - } - } } public static function initConstants() @@ -45,10 +30,10 @@ class Daux define("DS", DIRECTORY_SEPARATOR); } - public function initialize($global_config_file = null, $config_file = 'config.json') + public function initialize($global_config_file = null, $override_file = 'config.json') { $this->loadConfig($global_config_file); - $this->loadConfigOverrides($config_file); + $this->loadConfigOverrides($override_file); $this->generateTree(); } @@ -84,11 +69,13 @@ class Daux private function loadConfigOverrides($config_file) { - $config_file = $this->docs_path . DS . $config_file; + $this->options = json_decode(file_get_contents($this->local_base . DS . 'default.json'), true); + + $config_file = $this->local_base . DS . $config_file; if (!file_exists($config_file)) { throw new Exception('The local config file is missing. Check path : ' . $config_file); } - $this->options = json_decode(file_get_contents($this->local_base . DS . 'default.json'), true); + if (is_file($config_file)) { $config = json_decode(file_get_contents($config_file), true); if (!isset($config)) { @@ -96,6 +83,7 @@ class Daux } $this->options = array_merge($this->options, $config); } + if (isset($this->options['timezone'])) { date_default_timezone_set($this->options['timezone']); } elseif (!ini_get('date.timezone')) { @@ -139,21 +127,8 @@ class Daux } } - if ($this->mode == self::LIVE_MODE) { - $params['index_key'] = 'index'; - $params['host'] = $this->host; - $params['base_page'] = $params['base_url'] = '//' . $this->base_url; - if (!$this->options['clean_urls']) { - $params['base_page'] .= 'index.php/'; - } - - if ($params['image'] !== '') { - $params['image'] = str_replace('', $params['base_url'], $params['image']); - } - } else { - $params['index_key'] = 'index.html'; - $params['base_page'] = $params['base_url'] = ''; - } + $params['index_key'] = 'index.html'; + $params['base_page'] = $params['base_url'] = ''; $params['theme'] = DauxHelper::getTheme( $this->options['theme-name'], diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index 0abde93..aca6901 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -1,5 +1,7 @@ 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; + } } diff --git a/libs/Format/Base/MarkdownPage.php b/libs/Format/Base/MarkdownPage.php new file mode 100644 index 0000000..1506175 --- /dev/null +++ b/libs/Format/Base/MarkdownPage.php @@ -0,0 +1,45 @@ +initializePage($title, $content); + } + + public function setFile(Content $file) + { + $this->file = $file; + } + + public function setParams(array $params) + { + $this->params = $params; + } + + protected function generatePage() + { + return (new \Parsedown())->text($this->content); + } + + public static function fromFile(Content $file, $params) + { + $page = new static($file->title, file_get_contents($file->getPath())); + $page->setFile($file); + $page->setParams($params); + + return $page; + } +} diff --git a/libs/Format/Base/Page.php b/libs/Format/Base/Page.php new file mode 100644 index 0000000..3ee5d23 --- /dev/null +++ b/libs/Format/Base/Page.php @@ -0,0 +1,6 @@ +file = $filename; + } + + public function getFile() + { + return $this->file; + } + + public function getContent() + { + throw new Exception("you should not use this method to show a raw content"); + } +} diff --git a/libs/Format/Base/SimplePage.php b/libs/Format/Base/SimplePage.php new file mode 100644 index 0000000..6e783a4 --- /dev/null +++ b/libs/Format/Base/SimplePage.php @@ -0,0 +1,33 @@ +initializePage($title, $content); + } + + public function getContent() + { + if (is_null($this->generated)) { + $this->generated = $this->generatePage(); + } + + return $this->generated; + } + + protected function initializePage($title, $content) + { + $this->title = $title; + $this->content = $content; + } + + protected function generatePage() + { + return $this->content; + } +} diff --git a/libs/MarkdownPage.php b/libs/Format/HTML/MarkdownPage.php similarity index 67% rename from libs/MarkdownPage.php rename to libs/Format/HTML/MarkdownPage.php index 6cb4d46..784ba83 100644 --- a/libs/MarkdownPage.php +++ b/libs/Format/HTML/MarkdownPage.php @@ -1,48 +1,28 @@ -initialize($file, $params); - return $instance; - } - - private function initialize(Content $file, $params) - { - $this->file = $file; - $this->params = $params; - $this->title = $file->title; - $this->homepage = false; if ($this->title === 'index') { - $minimum_parent_dir_size = ($params['multilanguage']) ? 2 : 1; - if (count($file->getParents()) >= $minimum_parent_dir_size) { - $parents = $file->getParents(); + $minimum_parent_dir_size = ($this->params['multilanguage']) ? 2 : 1; + if (count($this->file->getParents()) >= $minimum_parent_dir_size) { + $parents = $this->file->getParents(); $this->title = end($parents)->getTitle(); } else { $this->homepage = ($this->file->getName() === '_index'); - $this->title = $params['title']; + $this->title = $this->params['title']; } } $this->language = ''; - if ($params['multilanguage'] && count($file->getParents())) { - reset($file->getParents()); - $language_dir = current($file->getParents()); + if ($this->params['multilanguage'] && count($this->file->getParents())) { + reset($this->file->getParents()); + $language_dir = current($this->file->getParents()); $this->language = $language_dir->name; } } @@ -61,18 +41,9 @@ class MarkdownPage extends SimplePage return $breadcrumb_trail; } - public function getContent() - { - if (is_null($this->html)) { - $this->content = file_get_contents($this->file->getPath()); - $this->html = $this->generatePage(); - } - - return $this->html; - } - - private function generatePage() + protected function generatePage() { + $this->initialize(); $params = $this->params; $entry_page = []; diff --git a/libs/Format/HTML/RawPage.php b/libs/Format/HTML/RawPage.php new file mode 100644 index 0000000..fcff274 --- /dev/null +++ b/libs/Format/HTML/RawPage.php @@ -0,0 +1,6 @@ +file = $filename; - } - - public function getContent() - { - throw new Exception("you should not use this method to show a raw content"); - } - - public function display() - { - header('Content-type: ' . MimeType::get($this->file)); - - // Transfer file in 1024 byte chunks to save memory usage. - if ($fd = fopen($this->file, 'rb')) { - while (!feof($fd)) { - print fread($fd, 1024); - } - fclose($fd); - } - } -} diff --git a/libs/Server/ErrorPage.php b/libs/Server/ErrorPage.php index 7520877..bf1ef15 100644 --- a/libs/Server/ErrorPage.php +++ b/libs/Server/ErrorPage.php @@ -1,7 +1,7 @@ params = $params; } - public function display() - { - http_response_code(404); - parent::display(); - } - - public function getContent() - { - if (is_null($this->html)) { - $this->html = $this->generatePage(); - } - - return $this->html; - } - - private function generatePage() + protected function generatePage() { $params = $this->params; $page['title'] = $this->title; diff --git a/libs/Server/Server.php b/libs/Server/Server.php index 4565092..38923ec 100644 --- a/libs/Server/Server.php +++ b/libs/Server/Server.php @@ -1,18 +1,20 @@ handle($_REQUEST); } catch (NotFoundException $e) { + http_response_code(404); $page = new ErrorPage("An error occured", $e->getMessage(), $daux->getParams()); } - $page->display(); + if ($page instanceof RawPage) { + header('Content-type: ' . MimeType::get($page->getFile())); + + // Transfer file in 1024 byte chunks to save memory usage. + if ($fd = fopen($page->getFile(), 'rb')) { + while (!feof($fd)) { + print fread($fd, 1024); + } + fclose($fd); + } + + return; + } + + header('Content-type: text/html; charset=utf-8'); + echo $page->getContent(); } public function __construct(Daux $daux) { $this->daux = $daux; + + $this->host = $_SERVER['HTTP_HOST']; + $this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); + $t = strrpos($this->base_url, '/index.php'); + if ($t != false) { + $this->base_url = substr($this->base_url, 0, $t); + } + if (substr($this->base_url, -1) !== '/') { + $this->base_url .= '/'; + } + } + + public function getParams() + { + $params = $this->daux->getParams(); + + $params['index_key'] = 'index'; + $params['host'] = $this->host; + $params['base_page'] = $params['base_url'] = '//' . $this->base_url; + if (!$this->daux->options['clean_urls']) { + $params['base_page'] .= 'index.php/'; + } + + if ($params['image'] !== '') { + $params['image'] = str_replace('', $params['base_url'], $params['image']); + } + + return $params; } public function handle($query = []) { - $this->params = $this->daux->getParams(); + $this->params = $this->getParams(); $request = $this->getRequest(); $request = urldecode($request); @@ -75,50 +121,9 @@ class Server 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) { - $file = $this->getFile($request); + $file = DauxHelper::getFile($this->daux->tree, $request); if ($file === false) { throw new NotFoundException('The Page you requested is yet to be made. Try again later.'); } diff --git a/libs/SimplePage.php b/libs/SimplePage.php deleted file mode 100644 index 6f41488..0000000 --- a/libs/SimplePage.php +++ /dev/null @@ -1,39 +0,0 @@ -initializePage($title, $content); - } - - public function display() - { - header('Content-type: text/html; charset=utf-8'); - echo $this->getContent(); - } - - public function getContent() - { - if (is_null($this->html)) { - $this->html = $this->generatePage(); - } - - return $this->html; - } - - private function initializePage($title, $content) - { - $this->title = $title; - $this->content = $content; - } - - private function generatePage() - { - return $this->content; - } -} diff --git a/libs/Tree/Builder.php b/libs/Tree/Builder.php index b538846..baa1e46 100644 --- a/libs/Tree/Builder.php +++ b/libs/Tree/Builder.php @@ -1,7 +1,6 @@ value as $node) { if ($node instanceof Content) { - if (!count($node->getParents()) && $node->title == 'index') { - //the homepage should not count as first page - continue; - } - + if (!count($node->getParents()) && $node->title == 'index') { + //the homepage should not count as first page + continue; + } + $this->first_page = $node; return $node; }