diff --git a/generate.php b/generate.php index 13afe2d..2b5589e 100644 --- a/generate.php +++ b/generate.php @@ -1,6 +1,4 @@ initialize(); - if (isset($argv[2])) $Daux->generate_static($argv[2]); - else $Daux->generate_static(); -?> + +require_once("vendor/autoload.php"); + +$global_config = (isset($argv[1]))? $argv[1] : null; +$destination = (isset($argv[2]))? $argv[2] : null; + +$generator = new \Todaymade\Daux\Generator\Generator(); + +$generator->generate($global_config, $destination); diff --git a/index.php b/index.php index bd3769f..3f52913 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,4 @@ initialize(); - $page = $Daux->handle_request($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST); - $page->display(); -?> + +require_once("vendor/autoload.php"); + +\Todaymade\Daux\Server\Server::serve($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST); diff --git a/libs/Daux.php b/libs/Daux.php index dccd143..176ab18 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -1,108 +1,61 @@ initial_setup($global_config_file); - } + public function __construct($mode) { + $this->mode = $mode; - public function initialize($config_file = 'config.json') { - if ($this->error) return; - $this->load_docs_config($config_file); - $this->generate_directory_tree(); - if (!$this->error) $this->params = $this->get_page_params(); - } - - public function generate_static($output_dir = NULL) { - if (is_null($output_dir)) $output_dir = $this->local_base . DIRECTORY_SEPARATOR . 'static'; - DauxHelper::clean_copy_assets($output_dir, $this->local_base); - $this->recursive_generate_static($this->tree, $output_dir, $this->params); - } - - public function handle_request($url, $query = array()) { - if ($this->error) return $this->error_page; - if (!$this->params['clean_urls']) $this->params['base_page'] .= 'index.php/'; - $request = DauxHelper::get_request(); - $request = urldecode($request); - $request_type = isset($query['method']) ? $query['method'] : ''; - if($request == 'first_page') { - $request = $this->tree->first_page->uri; - } - switch ($request_type) { - case 'DauxEdit': - if ($this->options['file_editor']) { - $content = isset($query['markdown']) ? $query['markdown'] : ''; - return $this->save_file($request, $content); - } - return $this->generate_error_page('Editing Disabled', 'Editing is currently disabled in config', - ErrorPage::FATAL_ERROR_TYPE); - default: - return $this->get_page($request); - } - } - - private function initial_setup($global_config_file) { - $this->setup_environment_variables(); - $this->load_global_config($global_config_file); - } - - private function setup_environment_variables() { - global $argc; $this->local_base = dirname(dirname(__FILE__)); $this->base_url = ''; - if (isset($argc)) { - $this->mode = Daux::STATIC_MODE; - return; + + 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 .= '/'; } - $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 function initialize($global_config_file = null, $config_file = 'config.json') { + $this->load_global_config($global_config_file); + $this->load_docs_config($config_file); + $this->generate_directory_tree(); } private function load_global_config($global_config_file) { if (is_null($global_config_file)) $global_config_file = $this->local_base . DIRECTORY_SEPARATOR . 'global.json'; if (!file_exists($global_config_file)) { - $this->generate_error_page('Global Config File Missing', - 'The Global Config file is missing. Requested File : ' . $global_config_file, ErrorPage::FATAL_ERROR_TYPE); - return; + throw new Exception('The Global Config file is missing. Requested File : ' . $global_config_file); } $global_config = json_decode(file_get_contents($global_config_file), true); if (!isset($global_config)) { - $this->generate_error_page('Corrupt Global Config File', - 'The Global Config file is corrupt. Check that the JSON encoding is correct', ErrorPage::FATAL_ERROR_TYPE); - return; + throw new Exception('The Global Config file is corrupt. Check that the JSON encoding is correct'); } if (!isset($global_config['docs_directory'])) { - $this->generate_error_page('Docs Directory not set', 'The Global Config file does not have the docs directory set.', - ErrorPage::FATAL_ERROR_TYPE); - return; + throw new Exception('The Global Config file does not have the docs directory set.'); } $this->docs_path = $global_config['docs_directory']; if (!is_dir($this->docs_path) && !is_dir($this->docs_path = $this->local_base . DIRECTORY_SEPARATOR . $this->docs_path)) { - $this->generate_error_page('Docs Directory not found', - 'The Docs directory does not exist. Check the path again : ' . $this->docs_path, ErrorPage::FATAL_ERROR_TYPE); - return; + throw new Exception('The Docs directory does not exist. Check the path again : ' . $this->docs_path); } if (!isset($global_config['valid_markdown_extensions'])) static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown'); @@ -112,17 +65,13 @@ private function load_docs_config($config_file) { $config_file = $this->docs_path . DIRECTORY_SEPARATOR . $config_file; if (!file_exists($config_file)) { - $this->generate_error_page('Config File Missing', - 'The local config file is missing. Check path : ' . $config_file, ErrorPage::FATAL_ERROR_TYPE); - return; + throw new Exception('The local config file is missing. Check path : ' . $config_file); } $this->options = json_decode(file_get_contents($this->local_base . DIRECTORY_SEPARATOR . 'default.json'), true); if (is_file($config_file)) { $config = json_decode(file_get_contents($config_file), true); if (!isset($config)) { - $this->generate_error_page('Invalid Config File', - 'There was an error parsing the Config file. Please review', ErrorPage::FATAL_ERROR_TYPE); - return; + throw new Exception('There was an error parsing the Config file. Please review'); } $this->options = array_merge($this->options, $config); } @@ -139,197 +88,105 @@ } } - private function recursive_generate_static($tree, $output_dir, $params, $base_url = '') { - $params['base_url'] = $params['base_page'] = $base_url; - $new_params = $params; - //changed this as well in order for the templates to be put in the right place - $params['theme'] = DauxHelper::rebase_theme($params['theme'], $base_url, $params['base_url'] . "templates/default/themes/" . $params['theme']['name'] . '/'); - // - $params['image'] = str_replace('', $base_url, $params['image']); - if ($base_url !== '') $params['entry_page'] = $tree->first_page; - foreach ($tree->value as $key => $node) { - if ($node->type === Entry::DIRECTORY_TYPE) { - $new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key; - @mkdir($new_output_dir); - $this->recursive_generate_static($node, $new_output_dir, $new_params, '../' . $base_url); - } else { - $params['request'] = $node->get_url(); - $params['file_uri'] = $node->name; + public function get_base_params() { + return array( + 'local_base' => $this->local_base, - $page = MarkdownPage::fromFile($node, $params); - file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $page->get_page_content()); - } - } + 'tagline' => $this->options['tagline'], + 'title' => $this->options['title'], + 'author' => $this->options['author'], + 'image' => $this->options['image'], + 'repo' => $this->options['repo'], + 'links' => $this->options['links'], + 'twitter' => $this->options['twitter'], + 'google_analytics' => ($g = $this->options['google_analytics']) ? DauxHelper::google_analytics($g, $this->host) : '', + 'piwik_analytics' => ($p = $this->options['piwik_analytics']) ? DauxHelper::piwik_analytics($p, $this->options['piwik_analytics_id']) : '', + + 'docs_path' => $this->docs_path, + 'tree' => $this->tree, + 'index' => ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page, + 'template' => $this->options['template'], + ); } - private function save_file($request, $content) { - $file = $this->get_file_from_request($request); - if ($file === false) return $this->generate_error_page('Page Not Found', - 'The Page you requested is yet to be made. Try again later.', ErrorPage::MISSING_PAGE_ERROR_TYPE); - if ($file->write($content)) return new SimplePage('Success', 'Successfully Edited'); - else return $this->generate_error_page('File Not Writable', 'The file you wish to write to is not writable.', - ErrorPage::FATAL_ERROR_TYPE); - } - - private function generate_error_page($title, $content, $type) { - $this->error_page = new ErrorPage($title, $content, $this->get_page_params($type)); - $this->error = true; - return $this->error_page; - } - - private function get_page($request) { - $params = $this->params; - $file = $this->get_file_from_request($request); - if ($file === false) return $this->generate_error_page('Page Not Found', - 'The Page you requested is yet to be made. Try again later.', ErrorPage::MISSING_PAGE_ERROR_TYPE); - $params['request'] = $request; - $params['file_uri'] = $file->value; - if ($request !== 'index') $params['entry_page'] = $file->first_page; - return MarkdownPage::fromFile($file, $params); - } - - private function get_page_params($mode = '') { - $params = array(); - $params['local_base'] = $this->local_base; + //TODO :: move to generator + public function get_page_params($mode = '') { if ($mode === '') $mode = $this->mode; + + $params = $this->get_base_params(); $params['mode'] = $mode; - switch ($mode) { - case ErrorPage::FATAL_ERROR_TYPE: - $params['error_type'] = ErrorPage::FATAL_ERROR_TYPE; - break; - case ErrorPage::NORMAL_ERROR_TYPE: - case ErrorPage::MISSING_PAGE_ERROR_TYPE: - $params['error_type'] = $mode; - $params['index_key'] = 'index'; - $params['docs_path'] = $this->docs_path; - $protocol = '//'; - $params['base_url'] = $protocol . $this->base_url; - $params['base_page'] = $params['base_url']; - $params['host'] = $this->host; - $params['tree'] = $this->tree; - $params['index'] = ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page; - $params['clean_urls'] = $this->options['clean_urls']; + $params['index_key'] = 'index.html'; + $params['base_url'] = ''; + $params['base_page'] = $params['base_url']; - $params['tagline'] = $this->options['tagline']; - $params['title'] = $this->options['title']; - $params['author'] = $this->options['author']; - $params['image'] = $this->options['image']; - if ($params['image'] !== '') $params['image'] = str_replace('', $params['base_url'], $params['image']); - $params['repo'] = $this->options['repo']; - $params['links'] = $this->options['links']; - $params['twitter'] = $this->options['twitter']; - $params['google_analytics'] = ($g = $this->options['google_analytics']) ? - DauxHelper::google_analytics($g, $this->host) : ''; - $params['piwik_analytics'] = ($p = $this->options['piwik_analytics']) ? - DauxHelper::piwik_analytics($p, $this->options['piwik_analytics_id']) : ''; - - $params['template'] = $this->options['template']; - $params['theme'] = DauxHelper::configure_theme($this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . - $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'] . '/config.json', $params['base_url'], - $this->local_base, $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/'); - break; - - case Daux::LIVE_MODE: - $params['docs_path'] = $this->docs_path; - $params['index_key'] = 'index'; - $protocol = '//'; - $params['base_url'] = $protocol . $this->base_url; - $params['base_page'] = $params['base_url']; - $params['host'] = $this->host; - $params['tree'] = $this->tree; - $params['index'] = ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page; - $params['clean_urls'] = $this->options['clean_urls']; - - $params['tagline'] = $this->options['tagline']; - $params['title'] = $this->options['title']; - $params['author'] = $this->options['author']; - $params['image'] = $this->options['image']; - if ($params['image'] !== '') $params['image'] = str_replace('', $params['base_url'], $params['image']); - $params['repo'] = $this->options['repo']; - $params['links'] = $this->options['links']; - $params['twitter'] = $this->options['twitter']; - $params['google_analytics'] = ($g = $this->options['google_analytics']) ? - DauxHelper::google_analytics($g, $this->host) : ''; - $params['piwik_analytics'] = ($p = $this->options['piwik_analytics']) ? - DauxHelper::piwik_analytics($p, $this->options['piwik_analytics_id']) : ''; - - $params['template'] = $this->options['template']; - $params['theme'] = DauxHelper::configure_theme($this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . - $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'] . '/config.json', $params['base_url'], - $this->local_base, $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/', $mode); - - - if ($params['breadcrumbs'] = $this->options['breadcrumbs']) - $params['breadcrumb_separator'] = $this->options['breadcrumb_separator']; - $params['multilanguage'] = !empty($this->options['languages']); - $params['languages'] = $this->options['languages']; - if (empty($this->options['languages'])) { - $params['entry_page'] = $this->tree->first_page; - } else { - foreach ($this->options['languages'] as $key => $name) { - $params['entry_page'][$key] = $this->tree->value[$key]->first_page; - } - } - - $params['toggle_code'] = $this->options['toggle_code']; - $params['float'] = $this->options['float']; - $params['date_modified'] = $this->options['date_modified']; - $params['file_editor'] = $this->options['file_editor']; - break; - - case Daux::STATIC_MODE: - $params['docs_path'] = $this->docs_path; - $params['index_key'] = 'index.html'; - $params['base_url'] = ''; - $params['base_page'] = $params['base_url']; - $params['tree'] = $this->tree; - $params['index'] = ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page; - - $params['tagline'] = $this->options['tagline']; - $params['title'] = $this->options['title']; - $params['author'] = $this->options['author']; - $params['image'] = $this->options['image']; - $params['repo'] = $this->options['repo']; - $params['links'] = $this->options['links']; - $params['twitter'] = $this->options['twitter']; - $params['google_analytics'] = ($g = $this->options['google_analytics']) ? - DauxHelper::google_analytics($g, $this->host) : ''; - $params['piwik_analytics'] = ($p = $this->options['piwik_analytics']) ? - DauxHelper::piwik_analytics($p, $this->options['piwik_analytics_id']) : ''; - - $params['template'] = $this->options['template']; - $params['theme'] = DauxHelper::configure_theme($this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . - $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'] . '/config.json', $params['base_url'], - $this->local_base, $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/', $mode); - - if ($params['breadcrumbs'] = $this->options['breadcrumbs']) - $params['breadcrumb_separator'] = $this->options['breadcrumb_separator']; - $params['multilanguage'] = !empty($this->options['languages']); - $params['languages'] = $this->options['languages']; - if (empty($this->options['languages'])) { - $params['entry_page'] = $this->tree->first_page; - } else { - foreach ($this->options['languages'] as $key => $name) { - $params['entry_page'][$key] = $this->tree->value[$key]->first_page; - } - } - - $params['toggle_code'] = $this->options['toggle_code']; - $params['float'] = $this->options['float']; - $params['date_modified'] = $this->options['date_modified']; - $params['file_editor'] = false; - break; + if ($params['breadcrumbs'] = $this->options['breadcrumbs']) + $params['breadcrumb_separator'] = $this->options['breadcrumb_separator']; + $params['multilanguage'] = !empty($this->options['languages']); + $params['languages'] = $this->options['languages']; + if (empty($this->options['languages'])) { + $params['entry_page'] = $this->tree->first_page; + } else { + foreach ($this->options['languages'] as $key => $name) { + $params['entry_page'][$key] = $this->tree->value[$key]->first_page; + } } + + $params['toggle_code'] = $this->options['toggle_code']; + $params['float'] = $this->options['float']; + $params['date_modified'] = $this->options['date_modified']; + $params['file_editor'] = false; + + $params['theme'] = GeneratorHelper::configure_theme( + $this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'], + $params['base_url'], + $this->local_base, + $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/' + ); + return $params; } - private function get_file_from_request($request) { - $file = $this->tree->retrieve_file($request); - return $file; + //TODO :: move to server + public function get_live_page_params() { + $params = $this->get_base_params(); + + $params['mode'] = Daux::LIVE_MODE; + + $params['index_key'] = 'index'; + $protocol = '//'; + $params['base_url'] = $protocol . $this->base_url; + $params['base_page'] = $params['base_url']; + $params['host'] = $this->host; + $params['clean_urls'] = $this->options['clean_urls']; + + if ($params['image'] !== '') $params['image'] = str_replace('', $params['base_url'], $params['image']); + + if ($params['breadcrumbs'] = $this->options['breadcrumbs']) + $params['breadcrumb_separator'] = $this->options['breadcrumb_separator']; + $params['multilanguage'] = !empty($this->options['languages']); + $params['languages'] = $this->options['languages']; + if (empty($this->options['languages'])) { + $params['entry_page'] = $this->tree->first_page; + } else { + foreach ($this->options['languages'] as $key => $name) { + $params['entry_page'][$key] = $this->tree->value[$key]->first_page; + } + } + + $params['toggle_code'] = $this->options['toggle_code']; + $params['float'] = $this->options['float']; + $params['date_modified'] = $this->options['date_modified']; + $params['file_editor'] = $this->options['file_editor']; + + $params['theme'] = ServerHelper::configure_theme( + $this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'], + $params['base_url'], + $this->local_base, + $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/' + ); + + return $params; } - } - - ?> diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index 3c7bee3..6bf5f4b 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -54,127 +54,34 @@ return $filename; } - public static function build_directory_tree($dir, $ignore, $mode) { - return static::directory_tree_builder($dir, $ignore, $mode); - } + public static function get_theme($theme_folder, $local_base) { + $name = static::pathinfo($theme_folder); - - //Depreciated - public static function get_request_from_url($url, $base_url) { - $url = substr($url, strlen($base_url)); - if (strpos($url, 'index.php') === 0) { - $request = (($i = strpos($url, 'request=')) !== false) ? $request = substr($url, $i + 8) : ''; - if ($end = strpos($request, '&')) $request = substr($request, 0, $end); - $request = ($request === '') ? 'index' : $request; - } else { - $request = ($url == '') ? 'index' : $url; - $request = ($end = strpos($request, '?')) ? substr($request, 0, $end) : $request; - } - return $request; - } - - - - public static function get_request($prefix_slash = false) - { - if (isset($_SERVER['PATH_INFO'])) $uri = $_SERVER['PATH_INFO']; - else if (isset($_SERVER['REQUEST_URI'])) { - $uri = $_SERVER['REQUEST_URI']; - if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME'])); - else if (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); - if (strncmp($uri, '?/', 2) === 0) $uri = substr($uri, 2); - $parts = preg_split('#\?#i', $uri, 2); - $uri = $parts[0]; - if (isset($parts[1])) { - $_SERVER['QUERY_STRING'] = $parts[1]; - parse_str($_SERVER['QUERY_STRING'], $_GET); - } else { - $_SERVER['QUERY_STRING'] = ''; - $_GET = array(); - } - $uri = parse_url($uri, PHP_URL_PATH); - } - else return false; - $uri = str_replace(array('//', '../'), '/', trim($uri, '/')); - if ($uri == "") $uri = "first_page"; - return $uri; - } - - public static function configure_theme($theme, $base_url, $local_base, $theme_url, $mode = Daux::LIVE_MODE) { - $name = static::pathinfo($theme); - if (is_file($theme)) { - $theme = file_get_contents($theme); - $theme = json_decode($theme, true); + $theme = array(); + if (is_file($theme_folder . DIRECTORY_SEPARATOR . "config.json")) { + $theme = json_decode(file_get_contents($theme_folder . DIRECTORY_SEPARATOR . "config.json"), true); if (!$theme) $theme = array(); - } else $theme = array(); + } $theme['name'] = $name['filename']; - if ($mode === Daux::LIVE_MODE) { - if (!isset($theme['favicon'])) $theme['favicon'] = utf8_encode($base_url . 'img/favicon.png'); - else { - $theme['favicon'] = utf8_encode(str_replace('', $base_url, $theme['favicon'])); - $theme['favicon'] = str_replace('', $theme_url, $theme['favicon']); - } + $theme += ['css' => [], 'js' => [], 'fonts' => [], 'require-jquery' => false, 'bootstrap-js' => false]; - if (!isset($theme['css'])) $theme['css'] = array(); - else { - foreach ($theme['css'] as $key => $css) { - $theme['css'][$key] = utf8_encode(str_replace('', $base_url, $css)); - $theme['css'][$key] = utf8_encode(str_replace('', $theme_url, $css)); - } - } - if (!isset($theme['fonts'])) $theme['fonts'] = array(); - else { - foreach ($theme['fonts'] as $key => $font) { - $theme['fonts'][$key] = utf8_encode(str_replace('', $base_url, $font)); - $theme['fonts'][$key] = utf8_encode(str_replace('', $theme_url, $font)); - } - } - if (!isset($theme['js'])) $theme['js'] = array(); - else { - foreach ($theme['js'] as $key => $js) { - $theme['js'][$key] = utf8_encode(str_replace('', $base_url, $js)); - $theme['js'][$key] = utf8_encode(str_replace('', $theme_url, $js)); - } - } + + if (!isset($theme['template'])){ + $theme['template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/default.tpl'; + } else{ + $theme['template'] = str_replace('', $local_base, $theme['template']); + } + if (!isset($theme['error-template'])) { + $theme['error-template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/error.tpl'; } else { - if (!isset($theme['favicon'])) $theme['favicon'] = 'img/favicon.png'; - if (!isset($theme['css'])) $theme['css'] = array(); - if (!isset($theme['fonts'])) $theme['fonts'] = array(); - if (!isset($theme['js'])) $theme['js'] = array(); + $theme['error-template'] = str_replace('', $local_base, $theme['error-template']); } - if (!isset($theme['template'])) $theme['template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' . - DIRECTORY_SEPARATOR . 'default/default.tpl'; - else $theme['template'] = str_replace('', $local_base, $theme['template']); - if (!isset($theme['error-template'])) $theme['error-template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' . - DIRECTORY_SEPARATOR . 'default/error.tpl'; - else $theme['error-template'] = str_replace('', $local_base, $theme['error-template']); - if (!isset($theme['require-jquery'])) $theme['require-jquery'] = false; - if (!isset($theme['bootstrap-js'])) $theme['bootstrap-js'] = false; - return $theme; } - public static function rebase_theme($theme, $base_url, $theme_url) { - $theme['favicon'] = utf8_encode(str_replace('', $base_url, $theme['favicon'])); - $theme['favicon'] = str_replace('', $theme_url, $theme['favicon']); - foreach ($theme['css'] as $key => $css) { - $theme['css'][$key] = utf8_encode(str_replace('', $base_url, $css)); - $theme['css'][$key] = utf8_encode(str_replace('', $theme_url, $css)); - } - foreach ($theme['fonts'] as $key => $font) { - $theme['fonts'][$key] = utf8_encode(str_replace('', $base_url, $font)); - $theme['fonts'][$key] = utf8_encode(str_replace('', $theme_url, $font)); - - } - foreach ($theme['js'] as $key => $js) { - $theme['js'][$key] = utf8_encode(str_replace('', $base_url, $js)); - $theme['js'][$key] = utf8_encode(str_replace('', $theme_url, $js)); - } - return $theme; - } public static function google_analytics($analytics, $host) { $ga = <<getFilename() === '.' || $file->getFilename() === '..') continue; - if ($file->isDir()) rmdir($file->getRealPath()); - else unlink($file->getRealPath()); - } - } - - private static function copy_recursive($src,$dst) { - $dir = opendir($src); - @mkdir($dst); - while(false !== ( $file = readdir($dir)) ) { - if (( $file != '.' ) && ( $file != '..' )) { - if ( is_dir($src . '/' . $file) ) { - static::copy_recursive($src . '/' . $file,$dst . '/' . $file); - } - else { - copy($src . '/' . $file,$dst . '/' . $file); - } - } - } - closedir($dir); - } - } diff --git a/libs/Entry.php b/libs/Entry.php index c31d0b7..cffe0b9 100644 --- a/libs/Entry.php +++ b/libs/Entry.php @@ -3,6 +3,7 @@ { const FILE_TYPE = 'FILE_TYPE'; const DIRECTORY_TYPE = 'DIRECTORY_TYPE'; + public $name; public $title; public $type; diff --git a/libs/Exception.php b/libs/Exception.php new file mode 100644 index 0000000..9ccaa45 --- /dev/null +++ b/libs/Exception.php @@ -0,0 +1,5 @@ +initialize($global_config); + + $this->generate_static($daux, $destination); + } + + public function generate_static(Daux $daux, $output_dir = NULL) { + $params = $daux->get_page_params(); + if (is_null($output_dir)) $output_dir = $daux->local_base . DIRECTORY_SEPARATOR . 'static'; + Helper::clean_copy_assets($output_dir, $daux->local_base); + $this->recursive_generate_static($daux->tree, $output_dir, $params); + } + + private function recursive_generate_static($tree, $output_dir, $params, $base_url = '') { + $params['base_url'] = $params['base_page'] = $base_url; + $new_params = $params; + //changed this as well in order for the templates to be put in the right place + $params['theme'] = Helper::rebase_theme($params['theme'], $base_url, $params['base_url'] . "templates/default/themes/" . $params['theme']['name'] . '/'); + // + $params['image'] = str_replace('', $base_url, $params['image']); + if ($base_url !== '') $params['entry_page'] = $tree->first_page; + foreach ($tree->value as $key => $node) { + if ($node->type === Entry::DIRECTORY_TYPE) { + $new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key; + @mkdir($new_output_dir); + $this->recursive_generate_static($node, $new_output_dir, $new_params, '../' . $base_url); + } else { + $params['request'] = $node->get_url(); + $params['file_uri'] = $node->name; + + $page = MarkdownPage::fromFile($node, $params); + file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $page->get_page_content()); + } + } + } + + +} diff --git a/libs/Generator/Helper.php b/libs/Generator/Helper.php new file mode 100644 index 0000000..b54f3e3 --- /dev/null +++ b/libs/Generator/Helper.php @@ -0,0 +1,79 @@ +getFilename() === '.' || $file->getFilename() === '..') continue; + if ($file->isDir()) rmdir($file->getRealPath()); + else unlink($file->getRealPath()); + } + } + + private static function copy_recursive($src,$dst) { + $dir = opendir($src); + @mkdir($dst); + while(false !== ( $file = readdir($dir)) ) { + if (( $file != '.' ) && ( $file != '..' )) { + if ( is_dir($src . '/' . $file) ) { + static::copy_recursive($src . '/' . $file,$dst . '/' . $file); + } + else { + copy($src . '/' . $file,$dst . '/' . $file); + } + } + } + closedir($dir); + } + + public static function rebase_theme($theme, $base_url, $theme_url) { + $theme['favicon'] = utf8_encode(str_replace('', $base_url, $theme['favicon'])); + $theme['favicon'] = str_replace('', $theme_url, $theme['favicon']); + + foreach ($theme['css'] as $key => $css) { + $theme['css'][$key] = utf8_encode(str_replace('', $base_url, $css)); + $theme['css'][$key] = utf8_encode(str_replace('', $theme_url, $css)); + } + foreach ($theme['fonts'] as $key => $font) { + $theme['fonts'][$key] = utf8_encode(str_replace('', $base_url, $font)); + $theme['fonts'][$key] = utf8_encode(str_replace('', $theme_url, $font)); + + } + foreach ($theme['js'] as $key => $js) { + $theme['js'][$key] = utf8_encode(str_replace('', $base_url, $js)); + $theme['js'][$key] = utf8_encode(str_replace('', $theme_url, $js)); + } + return $theme; + } + + public static function configure_theme($theme, $base_url, $local_base, $theme_url) { + $theme = DauxHelper::get_theme($theme, $local_base); + + if (!isset($theme['favicon'])) $theme['favicon'] = 'img/favicon.png'; + + return $theme; + } +} diff --git a/libs/ErrorPage.php b/libs/Server/ErrorPage.php similarity index 95% rename from libs/ErrorPage.php rename to libs/Server/ErrorPage.php index 4a15388..e112c73 100644 --- a/libs/ErrorPage.php +++ b/libs/Server/ErrorPage.php @@ -1,4 +1,6 @@ -', $base_url, $theme['favicon'])); + $theme['favicon'] = str_replace('', $theme_url, $theme['favicon']); + } + + foreach ($theme['css'] as $key => $css) { + $theme['css'][$key] = utf8_encode(str_replace('', $base_url, $css)); + $theme['css'][$key] = utf8_encode(str_replace('', $theme_url, $css)); + } + + foreach ($theme['fonts'] as $key => $font) { + $theme['fonts'][$key] = utf8_encode(str_replace('', $base_url, $font)); + $theme['fonts'][$key] = utf8_encode(str_replace('', $theme_url, $font)); + } + + foreach ($theme['js'] as $key => $js) { + $theme['js'][$key] = utf8_encode(str_replace('', $base_url, $js)); + $theme['js'][$key] = utf8_encode(str_replace('', $theme_url, $js)); + } + + return $theme; + } + + public static function get_error_params(Daux $daux) + { + $params = $daux->get_base_params(); + $params['theme'] = Helper::configure_theme( + $daux->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $daux->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $daux->options['theme'], + $params['base_url'], + $daux->local_base, + $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $daux->options['theme'] . '/' + ); + + $params['index_key'] = 'index'; + + $protocol = '//'; + $params['base_url'] = $protocol . $daux->base_url; + $params['base_page'] = $params['base_url']; + $params['host'] = $daux->host; + + $params['clean_urls'] = $daux->options['clean_urls']; + + if ($params['image'] !== '') $params['image'] = str_replace('', $params['base_url'], $params['image']); + + return $params; + } + + public static function get_request() + { + if (isset($_SERVER['PATH_INFO'])) $uri = $_SERVER['PATH_INFO']; + else if (isset($_SERVER['REQUEST_URI'])) { + $uri = $_SERVER['REQUEST_URI']; + if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME'])); + else if (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); + if (strncmp($uri, '?/', 2) === 0) $uri = substr($uri, 2); + $parts = preg_split('#\?#i', $uri, 2); + $uri = $parts[0]; + if (isset($parts[1])) { + $_SERVER['QUERY_STRING'] = $parts[1]; + parse_str($_SERVER['QUERY_STRING'], $_GET); + } else { + $_SERVER['QUERY_STRING'] = ''; + $_GET = array(); + } + $uri = parse_url($uri, PHP_URL_PATH); + } + else return false; + $uri = str_replace(array('//', '../'), '/', trim($uri, '/')); + if ($uri == "") $uri = "first_page"; + return $uri; + } +} diff --git a/libs/Server/NotFoundException.php b/libs/Server/NotFoundException.php new file mode 100644 index 0000000..f8f9a1c --- /dev/null +++ b/libs/Server/NotFoundException.php @@ -0,0 +1,7 @@ +initialize(); + $server = new static($daux); + + $page = $server->handle($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST); + } + catch( NotFoundException $e ) + { + $page = new ErrorPage("An error occured", $e->getMessage(), Helper::get_error_params($daux)); + } + + $page->display(); + } + + public function __construct(Daux $daux) { + $this->daux = $daux; + } + + public function handle($url, $query = []) { + + $this->params = $this->daux->get_live_page_params(); + + if (!$this->params['clean_urls']) $this->params['base_page'] .= 'index.php/'; + $request = Helper::get_request(); + $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->save_file($request, $content); + + default: + return $this->get_page($request); + } + } + + private function save_file($request, $content) { + $file = $this->get_file_from_request($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 get_file_from_request($request) { + $file = $this->daux->tree->retrieve_file($request); + return $file; + } + + private function get_page($request) { + $params = $this->params; + + $file = $this->get_file_from_request($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); + } +}