X-Git-Url: http://git.shiar.nl/minimedit.git/blobdiff_plain/1801380d62ab693e819f17706a9530679a1add1e..HEAD:/article.inc.php diff --git a/article.inc.php b/article.inc.php index 2396f2b..093852f 100644 --- a/article.inc.php +++ b/article.inc.php @@ -4,12 +4,13 @@ class ArchiveArticle public $raw, $title, $body; # file contents public $meta = []; # head metadata properties public $place = []; # template variables replaced in render() + public $api = FALSE; # requested programming interface function __construct($path) { - $this->page = preg_replace('{^\.(?:/|$)}', '', $path); - $this->link = preg_replace('{(?:(?:/|^)index)?\.html$}', '', $this->page); - $this->raw($this->page); + $this->file = preg_replace('{^\.(?:/|$)}', '', $path); + $this->link = preg_replace('{(?:(?:/|^)index)?\.html$}', '', $this->file); + $this->raw($this->file); } function raw($page) @@ -47,7 +48,7 @@ class ArchiveArticle $this->path = ''; $this->restricted = FALSE; while (TRUE) { - if (file_exists("$path/.private")) { + if (file_exists("$path/.private") and !$this->restricted) { $this->restricted = $path; } @@ -65,6 +66,18 @@ class ArchiveArticle return; } + function index($api = TRUE) + { + $this->handler; + if (empty($this->handler)) { + return; + } + $this->api = $api; + $Page = $this; + global $User; + return require "./{$this->handler}/index.php"; + } + function restricted() { $this->handler; @@ -77,12 +90,12 @@ class ArchiveArticle } function name() { - return $this->safetitle ?: $this->link; + return $this->safetitle ?: htmlspecialchars($this->link); } function last() { - return filemtime($this->page); + return filemtime($this->file); } function lastiso() { @@ -91,7 +104,7 @@ class ArchiveArticle function dateparts() { - preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->page, $ymd); + preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->file, $ymd); array_shift($ymd); return $ymd; } @@ -153,8 +166,9 @@ class ArchiveArticle return ltrim($this->image, '/'); } return preg_replace( - ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''], - $this->image + ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/', '/(?:\.jpg)?$/'], + [ "thumb/$size", '', '.jpg' ], + $this->image, 1 ); } @@ -173,7 +187,7 @@ class ArchiveArticle else { foreach (explode(' ', $params) as $param) { if ($set = strpos($param, '=')) { - $Page->place[ substr($param, 0, $set) ] = substr($param, $set + 1); + $Page->place[ substr($param, 0, $set) ] = urldecode(substr($param, $set + 1)); } elseif (!empty($param)) { $Page->place[] = $param; @@ -208,8 +222,13 @@ class ArchiveArticle # keep either login or logout parts depending on user level global $User; - $hideclass = $User && property_exists($User, 'login') && $User->login ? 'logout' : 'login'; - $doc = preg_replace('{\s*<([a-z]+) class="'.$hideclass.'">.*?}s', '', $doc); + $userexists = $User && property_exists($User, 'login') && $User->login; + if (! ($userexists and !empty($this->editable)) ) { + # remove matching elements until first corresponding closing tag + $hideclass = $userexists ? 'logout' : 'login'; + $tagmatch = '<([a-z]+) class="'.$hideclass.'"[^>]*>'; + $doc = preg_replace("{\s*{$tagmatch}.*?}s", '', $doc); + } return preg_replace_callback( '{ \[\[ ([^] ]+) ([^]]*) \]\] }x', @@ -264,12 +283,12 @@ class PageSearch function files() { # order alphabetically by link - $dir = iterator_to_array(new RecursiveIteratorIterator($this->iterator)); - array_walk($dir, function (&$row, $name) { - # prepare values for sorting (directory index first) - $row = preg_replace('{/index\.html$}', '', $name); - }); - asort($dir); + $dir = []; + foreach (new RecursiveIteratorIterator($this->iterator) as $name) { + $article = new ArchiveArticle($name); + $dir[$article->link] = $article; + } + ksort($dir); return $dir; } }