4 public $raw, $title, $body; # file contents
5 public $meta = []; # head metadata properties
6 public $place = []; # template variables replaced in render()
8 function __construct($path)
10 $this->file = preg_replace('{^\.(?:/|$)}', '', $path);
11 $this->link = preg_replace('{(?:(?:/|^)index)?\.html$}', '', $this->file);
12 $this->raw($this->file);
17 if (!file_exists($page)) {
20 $this->raw = file_get_contents($page);
23 \G <meta \s+ property="( [^"]+ )" \s+ content="( [^"]* )" > \s*
24 }x', $this->raw, $meta)) {
25 $matchlen = array_sum(array_map('strlen', $meta[0]));
26 $this->raw = substr($this->raw, $matchlen); # delete matched contents
27 $this->meta = array_combine($meta[1], $meta[2]); # [property => content]
30 // find significant contents
31 $this->body = preg_replace('{<aside\b.*?</aside>}s', '', $this->raw);
32 if (preg_match('{<h2>(.*?)</h2>\s*(.*)}s', $this->body, $titlematch)) {
33 list (, $this->title, $this->body) = $titlematch;
41 return $this->$col = $this->$col(); # run method and cache
48 $this->restricted = FALSE;
50 if (file_exists("$path/.private")) {
51 $this->restricted = $path;
54 if (file_exists("$path/index.php")) {
58 $up = strrpos($path, '/');
59 $this->path = substr($path, $up) . $this->path;
60 $path = substr($path, 0, $up);
71 if (empty($this->handler)) {
76 $res = include "./{$this->handler}/index.php";
83 return $this->restricted;
88 return trim($this->meta['og:title'] ?? strip_tags($this->title));
92 return $this->safetitle ?: htmlspecialchars($this->link);
97 return filemtime($this->file);
101 return date(DATE_ATOM, $this->last);
106 preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->file, $ymd);
112 return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
118 (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
119 }x', $this->body, $img, PREG_OFFSET_CAPTURE) ) {
120 # strip part after matching divider (image)
121 if (isset($img[1])) {
122 $this->img = $img[1][0];
124 return substr($this->body, 0, $img[0][1]);
131 if ($override = @$this->meta['og:description']) {
132 # prefer specific page description if found in metadata
136 # paragraph contents following the page header if any
138 \G (?> \s+ | <div [^>]*> | \[\[[^]]*\]\] )* <p> \s* (.*?) </p>
139 }sx', $this->body, $bodyp, 0)) {
152 if ($override = @$this->meta['og:image']) {
153 # prefer specific page image if found in metadata
157 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
161 function thumb($size = '300x')
163 if (!$this->image or $this->image[0] !== '/') return;
164 if (preg_match('{^/thumb/\D}', $this->image)) {
165 return ltrim($this->image, '/');
168 ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
173 function widget($name, $params = [])
175 $path = stream_resolve_include_path("widget/$name.php");
176 if (!file_exists($path)) {
177 return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
182 if (is_array($params)) {
183 $Page->place += $params;
186 foreach (explode(' ', $params) as $param) {
187 if ($set = strpos($param, '=')) {
188 $Page->place[ substr($param, 0, $set) ] = substr($param, $set + 1);
190 elseif (!empty($param)) {
191 $Page->place[] = $param;
197 include "widget/$name.php";
198 return ob_get_clean();
200 catch (Exception $e) {
201 return sprintf('<strong class="warn">%s</strong>',
202 "fout in <em>$name</em>: {$e->getMessage()}"
211 if (!empty($this->place['warn'])) {
212 $warn = '<p class="warn">[[warn]]</p>';
213 if ($offset = strpos($doc, '</h2>')) {
214 $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
217 $doc = $warn . "\n\n" . $doc;
221 # keep either login or logout parts depending on user level
223 $userexists = $User && property_exists($User, 'login') && $User->login;
224 if (! ($userexists and $User->admin("edit {$this->link}")) ) {
225 # remove matching elements until first corresponding closing tag
226 $hideclass = $userexists ? 'logout' : 'login';
227 $tagmatch = '<([a-z]+) class="'.$hideclass.'"[^>]*>';
228 $doc = preg_replace("{\s*{$tagmatch}.*?</\\1>}s", '', $doc);
231 return preg_replace_callback(
232 '{ \[\[ ([^] ]+) ([^]]*) \]\] }x',
234 list ($placeholder, $name, $params) = $sub;
235 $html = $this->place[$name] ??
236 $this->widget($name, $params);
237 if (empty($html) or $html[0] != '<') {
238 $html = "<span>$html</span>";
240 $attr = sprintf(' data-dyn="%s"', is_numeric($name) ? '' : $name.$params);
241 # contents with identifier in first tag
242 return preg_replace( '/(?=>)/', $attr, $html, 1);
251 public $handlers = [];
253 function __construct($path = '.')
255 $this->iterator = new RecursiveCallbackFilterIterator(
256 new RecursiveDirectoryIterator($path),
257 function ($current) {
258 if ($current->getFilename()[0] === '.') {
259 # skip hidden files and directories
262 if (file_exists($current->getFilename() . '/index.php')) {
263 # contents better provided by handler code
264 $this->handlers[ $current->getPathname() ] = $current;
267 if ($current->isLink()) {
268 # ignore symlinks, original contents only
271 if ($current->isDir()) {
272 # traverse subdirectories unless untracked in any amount
273 return !file_exists("$current/.gitignore");
276 return preg_match('/(?<!\.inc)\.html$/', $current->getFilename());
283 # order alphabetically by link
285 foreach (new RecursiveIteratorIterator($this->iterator) as $name) {
286 $article = new ArchiveArticle($name);
287 $dir[$article->link] = $article;