login/pass: error messages below page title
[minimedit.git] / article.inc.php
index c63089843c5d89f3da48fbb165683a2cda47491b..093852f96753e712014e0f07961f6c8a34cb8552 100644 (file)
@@ -1,29 +1,16 @@
 <?php
-global $monthname;
-$monthname = ['?',
-       'januari', 'februari', 'maart', 'april', 'mei', 'juni',
-       'juli', 'augustus', 'september', 'oktober', 'november', 'december',
-];
-
-function showdate($parts)
-{
-       global $monthname;
-       return implode(' ', array_filter([
-               intval(@$parts[2]), $parts[1] > 0 ? $monthname[intval($parts[1])] : '', $parts[0],
-               count($parts) > 5 ? "$parts[3]:$parts[4]" : '',
-       ]));
-}
-
 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)
@@ -61,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;
                        }
 
@@ -79,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;
@@ -91,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()
        {
@@ -105,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;
        }
@@ -113,10 +112,6 @@ class ArchiveArticle
        {
                return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
        }
-       function date()
-       {
-               return showdate($this->dateparts);
-       }
 
        function story()
        {
@@ -167,9 +162,13 @@ class ArchiveArticle
        function thumb($size = '300x')
        {
                if (!$this->image or $this->image[0] !== '/') return;
+               if (preg_match('{^/thumb/\D}', $this->image)) {
+                       return ltrim($this->image, '/');
+               }
                return preg_replace(
-                       ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
-                       $this->image
+                       ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/', '/(?:\.jpg)?$/'],
+                       [      "thumb/$size",    '',                         '.jpg'    ],
+                       $this->image, 1
                );
        }
 
@@ -182,18 +181,20 @@ class ArchiveArticle
 
                ob_start();
                $Page = clone $this;
-               $Page->handler = $Page->handler . $Page->path; // .= with explicit getter
-               $Page->path = '';
-               $Place = $GLOBALS['Place'];
-               foreach ($params as $param) {
-                       if ($set = strpos($param, '=')) {
-                               $Place[ substr($param, 0, $set) ] = substr($param, $set + 1);
-                       }
-                       elseif (!empty($param)) {
-                               $Page->path .= '/'.$param;
+               if (is_array($params)) {
+                       $Page->place += $params;
+               }
+               else {
+                       foreach (explode(' ', $params) as $param) {
+                               if ($set = strpos($param, '=')) {
+                                       $Page->place[ substr($param, 0, $set) ] = urldecode(substr($param, $set + 1));
+                               }
+                               elseif (!empty($param)) {
+                                       $Page->place[] = $param;
+                               }
                        }
                }
-               $Page->link .= $Page->path;
+
                try {
                        include "widget/$name.php";
                        return ob_get_clean();
@@ -205,11 +206,11 @@ class ArchiveArticle
                }
        }
 
-       function render($blocks = [])
+       function render()
        {
-               $doc = ob_get_clean();
+               $doc = $this->raw;
 
-               if (!empty($blocks['warn'])) {
+               if (!empty($this->place['warn'])) {
                        $warn = '<p class="warn">[[warn]]</p>';
                        if ($offset = strpos($doc, '</h2>')) {
                                $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
@@ -221,15 +222,20 @@ 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.'">.*?</\1>}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}.*?</\\1>}s", '', $doc);
+               }
 
                return preg_replace_callback(
                        '{ \[\[ ([^] ]+) ([^]]*) \]\] }x',
-                       function ($sub) use ($blocks) {
+                       function ($sub) {
                                list ($placeholder, $name, $params) = $sub;
-                               $html = $blocks[$name] ??
-                                       $this->widget($name, explode(' ', $params));
+                               $html = $this->place[$name] ??
+                                       $this->widget($name, $params);
                                if (empty($html) or $html[0] != '<') {
                                        $html = "<span>$html</span>";
                                }
@@ -244,6 +250,8 @@ class ArchiveArticle
 
 class PageSearch
 {
+       public $handlers = [];
+
        function __construct($path = '.')
        {
                $this->iterator = new RecursiveCallbackFilterIterator(
@@ -253,6 +261,11 @@ class PageSearch
                                        # skip hidden files and directories
                                        return FALSE;
                                }
+                               if (file_exists($current->getFilename() . '/index.php')) {
+                                       # contents better provided by handler code
+                                       $this->handlers[ $current->getPathname() ] = $current;
+                                       return FALSE;
+                               }
                                if ($current->isLink()) {
                                        # ignore symlinks, original contents only
                                        return FALSE;
@@ -270,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;
        }
 }