diff --git a/libs/Daux.php b/libs/Daux.php index 3421f30..65e74eb 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -73,7 +73,9 @@ class Daux $params = $this->getParams(); // Validate and set theme path - $params->setDocumentationDirectory($docs_path = $this->normalizeDocumentationPath()); + $params->setDocumentationDirectory( + $docs_path = $this->normalizeDocumentationPath($this->getParams()->getDocumentationDirectory()) + ); // Read documentation overrides $this->loadConfiguration($docs_path . DIRECTORY_SEPARATOR . 'config.json'); @@ -109,6 +111,10 @@ class Daux } if (file_exists($override_file)) { + if (DauxHelper::isAbsolutePath($override_file)) { + return $override_file; + } + return getcwd() . '/' . $override_file; } @@ -129,6 +135,10 @@ class Daux } if (is_dir($path)) { + if (DauxHelper::isAbsolutePath($path)) { + return $path; + } + return getcwd() . '/' . $path; } @@ -140,7 +150,7 @@ class Daux throw new Exception('The Themes directory does not exist. Check the path again : ' . $path); } - public function normalizeDocumentationPath() + public function normalizeDocumentationPath($path) { // When running through `daux --serve` we set an environment variable to know where we started from $env = getenv('DAUX_SOURCE'); @@ -148,8 +158,11 @@ class Daux return $env; } - $path = $this->getParams()->getDocumentationDirectory(); if (is_dir($path)) { + if (DauxHelper::isAbsolutePath($path)) { + return $path; + } + return getcwd() . '/' . $path; } diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index e6864d2..afd6ba8 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -24,7 +24,7 @@ class DauxHelper $config['image'] = str_replace('', $base_url, $config['image']); } - public static function resolveVariant(Config $params) + protected static function resolveVariant(Config $params) { if (array_key_exists('theme-variant', $params['html'])) { return; @@ -54,7 +54,7 @@ class DauxHelper * @param string $current_url * @return array */ - public static function getTheme(Config $params, $current_url) + protected static function getTheme(Config $params, $current_url) { self::resolveVariant($params); @@ -438,4 +438,34 @@ class DauxHelper return implode('/', $relPath); } + + public static function isAbsolutePath($path) + { + if (!is_string($path)) { + $mess = sprintf('String expected but was given %s', gettype($path)); + throw new \InvalidArgumentException($mess); + } + + if (!ctype_print($path)) { + $mess = 'Path can NOT have non-printable characters or be empty'; + throw new \DomainException($mess); + } + + // Optional wrapper(s). + $regExp = '%^(?(?:[[:print:]]{2,}://)*)'; + + // Optional root prefix. + $regExp .= '(?(?:[[:alpha:]]:/|/)?)'; + + // Actual path. + $regExp .= '(?(?:[[:print:]]*))$%'; + + $parts = []; + if (!preg_match($regExp, $path, $parts)) { + $mess = sprintf('Path is NOT valid, was given %s', $path); + throw new \DomainException($mess); + } + + return '' !== $parts['root']; + } }