2015-04-21 17:11:43 +02:00
|
|
|
<?php namespace Todaymade\Daux;
|
2014-07-12 12:31:57 +02:00
|
|
|
|
2019-11-30 20:40:42 +01:00
|
|
|
use Todaymade\Daux\Exception\LinkNotFoundException;
|
2016-07-04 20:33:44 +02:00
|
|
|
use Todaymade\Daux\Tree\Builder;
|
2015-05-18 14:26:29 +02:00
|
|
|
use Todaymade\Daux\Tree\Directory;
|
2019-11-30 22:24:10 +01:00
|
|
|
use Todaymade\Daux\Tree\Entry;
|
2015-05-18 14:26:29 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
class DauxHelper
|
|
|
|
{
|
2015-07-20 15:59:52 +02:00
|
|
|
/**
|
|
|
|
* Set a new base_url for the configuration
|
|
|
|
*
|
|
|
|
* @param Config $config
|
|
|
|
* @param string $base_url
|
|
|
|
*/
|
2015-07-19 14:05:12 +02:00
|
|
|
public static function rebaseConfiguration(Config $config, $base_url)
|
|
|
|
{
|
|
|
|
// Avoid changing the url if it is already correct
|
2019-12-05 21:25:58 +01:00
|
|
|
if ($config->getBaseUrl() == $base_url && !empty($config->getTheme())) {
|
2015-07-19 14:05:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change base url for all links on the pages
|
2019-12-05 21:25:58 +01:00
|
|
|
$config['base_url'] = $base_url;
|
2015-07-19 14:05:12 +02:00
|
|
|
$config['theme'] = static::getTheme($config, $base_url);
|
2019-12-05 21:25:58 +01:00
|
|
|
$config['image'] = str_replace('<base_url>', $base_url, $config->getImage());
|
2015-08-09 23:06:50 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
2019-12-05 21:25:58 +01:00
|
|
|
* @param Config $config
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param string $current_url
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-12-05 21:25:58 +01:00
|
|
|
protected static function getTheme(Config $config, $current_url)
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2019-12-05 21:25:58 +01:00
|
|
|
$htmlTheme = $config->getHTML()->getTheme();
|
2015-08-09 23:06:50 +02:00
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$theme_folder = $config->getThemesPath() . DIRECTORY_SEPARATOR . $htmlTheme;
|
|
|
|
$theme_url = $config->getBaseUrl() . 'themes/' . $htmlTheme . '/';
|
2014-10-29 08:44:20 +01:00
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
$theme = [];
|
|
|
|
if (is_file($theme_folder . DIRECTORY_SEPARATOR . 'config.json')) {
|
|
|
|
$theme = json_decode(file_get_contents($theme_folder . DIRECTORY_SEPARATOR . 'config.json'), true);
|
2015-04-23 00:32:30 +02:00
|
|
|
if (!$theme) {
|
2016-07-27 21:32:51 +02:00
|
|
|
$theme = [];
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2015-04-22 14:05:37 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
//Default parameters for theme
|
|
|
|
$theme += [
|
2019-12-05 21:25:58 +01:00
|
|
|
'name' => $htmlTheme,
|
2015-04-23 00:32:30 +02:00
|
|
|
'css' => [],
|
|
|
|
'js' => [],
|
|
|
|
'fonts' => [],
|
2015-08-06 22:47:38 +02:00
|
|
|
'favicon' => '<base_url>themes/daux/img/favicon.png',
|
2015-07-20 15:59:52 +02:00
|
|
|
'templates' => $theme_folder . DIRECTORY_SEPARATOR . 'templates',
|
2015-08-09 23:06:50 +02:00
|
|
|
'variants' => [],
|
2015-04-23 00:32:30 +02:00
|
|
|
];
|
2015-04-22 14:05:37 +02:00
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
if ($config->getHTML()->hasThemeVariant()) {
|
|
|
|
$variant = $config->getHTML()->getThemeVariant();
|
2015-08-09 23:06:50 +02:00
|
|
|
if (!array_key_exists($variant, $theme['variants'])) {
|
|
|
|
throw new Exception("Variant '$variant' not found for theme '$theme[name]'");
|
|
|
|
}
|
|
|
|
|
|
|
|
// These will be replaced
|
|
|
|
foreach (['templates', 'favicon'] as $element) {
|
|
|
|
if (array_key_exists($element, $theme['variants'][$variant])) {
|
|
|
|
$theme[$element] = $theme['variants'][$variant][$element];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These will be merged
|
|
|
|
foreach (['css', 'js', 'fonts'] as $element) {
|
|
|
|
if (array_key_exists($element, $theme['variants'][$variant])) {
|
|
|
|
$theme[$element] = array_merge($theme[$element], $theme['variants'][$variant][$element]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:05:16 +02:00
|
|
|
$substitutions = [
|
2019-12-05 21:25:58 +01:00
|
|
|
'<local_base>' => $config->getLocalBase(),
|
2015-05-22 15:05:16 +02:00
|
|
|
'<base_url>' => $current_url,
|
2016-07-27 21:32:51 +02:00
|
|
|
'<theme_url>' => $theme_url,
|
2015-05-22 15:05:16 +02:00
|
|
|
];
|
2015-04-22 14:05:37 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
// Substitute some placeholders
|
2015-04-23 10:24:50 +02:00
|
|
|
$theme['templates'] = strtr($theme['templates'], $substitutions);
|
2015-04-23 00:32:30 +02:00
|
|
|
$theme['favicon'] = utf8_encode(strtr($theme['favicon'], $substitutions));
|
2014-10-29 08:44:20 +01:00
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
foreach (['css', 'js', 'fonts'] as $element) {
|
|
|
|
foreach ($theme[$element] as $key => $value) {
|
|
|
|
$theme[$element][$key] = utf8_encode(strtr($value, $substitutions));
|
|
|
|
}
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
return $theme;
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
2016-07-29 23:46:57 +02:00
|
|
|
* Remove all '/./' and '/../' in a path, without actually checking the path
|
|
|
|
*
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-05-22 14:48:09 +02:00
|
|
|
public static function getCleanPath($path)
|
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
|
2015-05-21 17:39:11 +02:00
|
|
|
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
|
2016-07-27 21:32:51 +02:00
|
|
|
$absolutes = [];
|
2015-05-21 17:39:11 +02:00
|
|
|
foreach ($parts as $part) {
|
2015-05-22 14:48:09 +02:00
|
|
|
if ('.' == $part) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-21 17:39:11 +02:00
|
|
|
if ('..' == $part) {
|
|
|
|
array_pop($absolutes);
|
|
|
|
} else {
|
|
|
|
$absolutes[] = $part;
|
|
|
|
}
|
|
|
|
}
|
2016-07-27 21:32:51 +02:00
|
|
|
|
2015-05-21 17:39:11 +02:00
|
|
|
return implode(DIRECTORY_SEPARATOR, $absolutes);
|
|
|
|
}
|
|
|
|
|
2016-07-29 23:46:57 +02:00
|
|
|
/**
|
|
|
|
* Get the possible output file names for a source file.
|
|
|
|
*
|
|
|
|
* @param Config $config
|
|
|
|
* @param string $part
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2016-07-04 20:33:44 +02:00
|
|
|
public static function getFilenames(Config $config, $part)
|
|
|
|
{
|
2019-12-05 21:25:58 +01:00
|
|
|
$extensions = implode('|', array_map('preg_quote', $config->getValidContentExtensions())) . '|html';
|
2016-07-04 20:33:44 +02:00
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
$raw = preg_replace('/(.*)?\\.(' . $extensions . ')$/', '$1', $part);
|
2016-07-04 20:33:44 +02:00
|
|
|
$raw = Builder::removeSortingInformations($raw);
|
|
|
|
|
|
|
|
return ["$raw.html", $raw];
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* Locate a file in the tree. Returns the file if found or false
|
|
|
|
*
|
|
|
|
* @param Directory $tree
|
|
|
|
* @param string $request
|
2015-07-18 21:45:38 +02:00
|
|
|
* @return Tree\Content|Tree\Raw|false
|
2015-07-17 23:38:06 +02:00
|
|
|
*/
|
2015-05-18 14:26:29 +02:00
|
|
|
public static function getFile($tree, $request)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2016-07-04 20:33:44 +02:00
|
|
|
// Some relative paths may start with ./
|
|
|
|
if ($node == '.') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node == '..') {
|
|
|
|
$tree = $tree->getParent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-18 14:26:29 +02:00
|
|
|
// 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
|
2015-07-18 14:37:18 +02:00
|
|
|
if (isset($tree->getEntries()[$node])) {
|
|
|
|
$tree = $tree->getEntries()[$node];
|
2015-05-18 14:26:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-26 22:08:18 +01:00
|
|
|
// We try a second time by decoding the url
|
|
|
|
$node = DauxHelper::slug(urldecode($node));
|
|
|
|
if (isset($tree->getEntries()[$node])) {
|
|
|
|
$tree = $tree->getEntries()[$node];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-04 20:33:44 +02:00
|
|
|
// if the node doesn't exist, we can try
|
|
|
|
// two variants of the requested file:
|
|
|
|
// with and w/o the .html extension
|
|
|
|
foreach (static::getFilenames($tree->getConfig(), $node) as $filename) {
|
|
|
|
if (isset($tree->getEntries()[$filename])) {
|
|
|
|
$tree = $tree->getEntries()[$filename];
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 14:26:29 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-07-18 23:13:02 +02:00
|
|
|
if ($index = $tree->getIndexPage()) {
|
|
|
|
return $index;
|
2015-05-18 14:26:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-17 18:30:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a URL friendly "slug" from a given string.
|
|
|
|
*
|
|
|
|
* Taken from Stringy
|
|
|
|
*
|
2016-07-04 20:33:44 +02:00
|
|
|
* @param string $title
|
2015-07-17 18:30:10 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function slug($title)
|
|
|
|
{
|
2019-05-28 21:26:27 +02:00
|
|
|
// Convert to ASCII
|
2019-12-29 20:54:17 +01:00
|
|
|
if (function_exists("transliterator_transliterate")) {
|
|
|
|
$title = transliterator_transliterate("Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC;", $title);
|
|
|
|
}
|
|
|
|
|
2019-12-29 20:41:13 +01:00
|
|
|
$title = iconv("utf-8", "ASCII//TRANSLIT//IGNORE", $title);
|
2019-12-29 20:54:17 +01:00
|
|
|
|
2019-05-28 21:26:27 +02:00
|
|
|
// Remove unsupported characters
|
2015-07-17 18:30:10 +02:00
|
|
|
$title = preg_replace('/[^\x20-\x7E]/u', '', $title);
|
|
|
|
|
|
|
|
$separator = '_';
|
|
|
|
// Convert all dashes into underscores
|
2016-07-27 21:32:51 +02:00
|
|
|
$title = preg_replace('![' . preg_quote('-') . ']+!u', $separator, $title);
|
2015-07-17 23:38:06 +02:00
|
|
|
|
2020-04-22 21:55:53 +02:00
|
|
|
// Remove all characters that are not valid in a URL:
|
2019-05-28 21:26:27 +02:00
|
|
|
// $-_.+!*'(), separator, letters, numbers, or whitespace.
|
|
|
|
$title = preg_replace('![^-' . preg_quote($separator) . '\!\'\(\),\.\+\*\$\pL\pN\s]+!u', '', $title);
|
2015-07-17 23:38:06 +02:00
|
|
|
|
2015-07-17 18:30:10 +02:00
|
|
|
// Replace all separator characters and whitespace by a single separator
|
2015-07-17 23:38:06 +02:00
|
|
|
$title = preg_replace('![' . preg_quote($separator) . '\s]+!u', $separator, $title);
|
|
|
|
|
2015-07-17 18:30:10 +02:00
|
|
|
return trim($title, $separator);
|
|
|
|
}
|
|
|
|
|
2016-07-29 23:46:57 +02:00
|
|
|
/**
|
|
|
|
* @param string $from
|
|
|
|
* @param string $to
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-04 20:33:44 +02:00
|
|
|
public static function getRelativePath($from, $to)
|
|
|
|
{
|
|
|
|
// some compatibility fixes for Windows paths
|
|
|
|
$from = is_dir($from) ? rtrim($from, '\/') . '/' : $from;
|
|
|
|
$to = is_dir($to) ? rtrim($to, '\/') . '/' : $to;
|
|
|
|
$from = str_replace('\\', '/', $from);
|
|
|
|
$to = str_replace('\\', '/', $to);
|
|
|
|
|
|
|
|
$from = explode('/', $from);
|
|
|
|
$to = explode('/', $to);
|
|
|
|
$relPath = $to;
|
|
|
|
|
|
|
|
foreach ($from as $depth => $dir) {
|
|
|
|
// find first non-matching dir
|
|
|
|
if ($dir === $to[$depth]) {
|
|
|
|
// ignore this directory
|
|
|
|
array_shift($relPath);
|
|
|
|
} else {
|
|
|
|
// get number of remaining dirs to $from
|
|
|
|
$remaining = count($from) - $depth;
|
|
|
|
if ($remaining > 1) {
|
|
|
|
// add traversals up to first matching dir
|
|
|
|
$padLength = (count($relPath) + $remaining - 1) * -1;
|
|
|
|
$relPath = array_pad($relPath, $padLength, '..');
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
//$relPath[0] = './' . $relPath[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-27 21:32:51 +02:00
|
|
|
|
2016-07-04 20:33:44 +02:00
|
|
|
return implode('/', $relPath);
|
|
|
|
}
|
2016-09-21 23:10:10 +02:00
|
|
|
|
|
|
|
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 = '%^(?<wrappers>(?:[[:print:]]{2,}://)*)';
|
|
|
|
|
|
|
|
// Optional root prefix.
|
|
|
|
$regExp .= '(?<root>(?:[[:alpha:]]:/|/)?)';
|
|
|
|
|
|
|
|
// Actual path.
|
|
|
|
$regExp .= '(?<path>(?:[[: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'];
|
|
|
|
}
|
2017-11-07 22:44:27 +01:00
|
|
|
|
2020-04-22 21:55:53 +02:00
|
|
|
public static function getAbsolutePath($path)
|
|
|
|
{
|
2017-11-07 23:17:05 +01:00
|
|
|
if (DauxHelper::isAbsolutePath($path)) {
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getcwd() . '/' . $path;
|
|
|
|
}
|
|
|
|
|
2020-04-22 21:55:53 +02:00
|
|
|
public static function is($path, $type)
|
|
|
|
{
|
2017-11-07 22:44:27 +01:00
|
|
|
return ($type == 'dir') ? is_dir($path) : file_exists($path);
|
|
|
|
}
|
2019-11-30 20:40:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Config $config
|
|
|
|
* @param string $url
|
|
|
|
* @return Entry
|
|
|
|
* @throws LinkNotFoundException
|
|
|
|
*/
|
|
|
|
public static function resolveInternalFile($config, $url)
|
|
|
|
{
|
|
|
|
$triedAbsolute = false;
|
|
|
|
|
|
|
|
// Legacy absolute paths could start with
|
|
|
|
// "!" In this case we will try to find
|
|
|
|
// the file starting at the root
|
|
|
|
if ($url[0] == '!' || $url[0] == '/') {
|
|
|
|
$url = ltrim($url, '!/');
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
if ($file = DauxHelper::getFile($config->getTree(), $url)) {
|
2019-11-30 20:40:42 +01:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
$triedAbsolute = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seems it's not an absolute path or not found,
|
|
|
|
// so we'll continue with the current folder
|
|
|
|
if ($file = DauxHelper::getFile($config->getCurrentPage()->getParent(), $url)) {
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't already try it, we'll
|
|
|
|
// do a pass starting at the root
|
2019-12-05 21:25:58 +01:00
|
|
|
if (!$triedAbsolute && $file = DauxHelper::getFile($config->getTree(), $url)) {
|
2019-11-30 20:40:42 +01:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new LinkNotFoundException("Could not locate file '$url'");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isValidUrl($url)
|
|
|
|
{
|
|
|
|
return !empty($url) && $url[0] != '#';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isExternalUrl($url)
|
|
|
|
{
|
|
|
|
return preg_match('#^(?:[a-z]+:)?//|^mailto:#', $url);
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|