4 public $raw, $title, $body; # file contents
5 public $meta = []; # head metadata properties
6 public $place = []; # template variables replaced in render()
7 public $api = FALSE; # requested programming interface
9 function __construct($path)
11 $this->file = preg_replace('{^\.(?:/|$)}', '', $path);
12 $this->link = preg_replace('{(?:(?:/|^)index)?\.html$}', '', $this->file);
13 $this->raw($this->file);
18 if (!file_exists($page)) {
21 $this->raw = file_get_contents($page);
24 \G <meta \s+ property="( [^"]+ )" \s+ content="( [^"]* )" > \s*
25 }x', $this->raw, $meta)) {
26 $matchlen = array_sum(array_map('strlen', $meta[0]));
27 $this->raw = substr($this->raw, $matchlen); # delete matched contents
28 $this->meta = array_combine($meta[1], $meta[2]); # [property => content]
31 // find significant contents
32 $this->body = preg_replace('{<aside\b.*?</aside>}s', '', $this->raw);
33 if (preg_match('{<h2>(.*?)</h2>\s*(.*)}s', $this->body, $titlematch)) {
34 list (, $this->title, $this->body) = $titlematch;
42 return $this->$col = $this->$col(); # run method and cache
49 $this->restricted = FALSE;
51 if (file_exists("$path/.private") and !$this->restricted) {
52 $this->restricted = $path;
55 if (file_exists("$path/index.php")) {
59 $up = strrpos($path, '/');
60 $this->path = substr($path, $up) . $this->path;
61 $path = substr($path, 0, $up);
69 function index($api = TRUE)
72 if (empty($this->handler)) {
78 return require "./{$this->handler}/index.php";
84 return $this->restricted;
89 return trim($this->meta['og:title'] ?? strip_tags($this->title));
93 return $this->safetitle ?: htmlspecialchars($this->link);
98 return filemtime($this->file);
102 return date(DATE_ATOM, $this->last);
107 preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->file, $ymd);
113 return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
119 (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
120 }x', $this->body, $img, PREG_OFFSET_CAPTURE) ) {
121 # strip part after matching divider (image)
122 if (isset($img[1])) {
123 $this->img = $img[1][0];
125 return substr($this->body, 0, $img[0][1]);
132 if ($override = @$this->meta['og:description']) {
133 # prefer specific page description if found in metadata
137 # paragraph contents following the page header if any
139 \G (?> \s+ | <div [^>]*> | \[\[[^]]*\]\] )* <p> \s* (.*?) </p>
140 }sx', $this->body, $bodyp, 0)) {
153 if ($override = @$this->meta['og:image']) {
154 # prefer specific page image if found in metadata
158 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
162 function thumb($size = '300x')
164 if (!$this->image or $this->image[0] !== '/') return;
165 if (preg_match('{^/thumb/\D}', $this->image)) {
166 return ltrim($this->image, '/');
169 ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/', '/(?:\.jpg)?$/'],
170 [ "thumb/$size", '', '.jpg' ],
175 function widget($name, $params = [])
177 $path = stream_resolve_include_path("widget/$name.php");
178 if (!file_exists($path)) {
179 return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
184 if (is_array($params)) {
185 $Page->place += $params;
188 foreach (explode(' ', $params) as $param) {
189 if ($set = strpos($param, '=')) {
190 $Page->place[ substr($param, 0, $set) ] = urldecode(substr($param, $set + 1));
192 elseif (!empty($param)) {
193 $Page->place[] = $param;
199 include "widget/$name.php";
200 return ob_get_clean();
202 catch (Exception $e) {
203 return sprintf('<strong class="warn">%s</strong>',
204 "fout in <em>$name</em>: {$e->getMessage()}"
213 if (!empty($this->place['warn'])) {
214 $warn = '<p class="warn">[[warn]]</p>';
215 if ($offset = strpos($doc, '</h2>')) {
216 $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
219 $doc = $warn . "\n\n" . $doc;
223 # keep either login or logout parts depending on user level
225 $userexists = $User && property_exists($User, 'login') && $User->login;
226 if (! ($userexists and !empty($this->editable)) ) {
227 # remove matching elements until first corresponding closing tag
228 $hideclass = $userexists ? 'logout' : 'login';
229 $tagmatch = '<([a-z]+) class="'.$hideclass.'"[^>]*>';
230 $doc = preg_replace("{\s*{$tagmatch}.*?</\\1>}s", '', $doc);
233 return preg_replace_callback(
234 '{ \[\[ ([^] ]+) ([^]]*) \]\] }x',
236 list ($placeholder, $name, $params) = $sub;
237 $html = $this->place[$name] ??
238 $this->widget($name, $params);
239 if (empty($html) or $html[0] != '<') {
240 $html = "<span>$html</span>";
242 $attr = sprintf(' data-dyn="%s"', is_numeric($name) ? '' : $name.$params);
243 # contents with identifier in first tag
244 return preg_replace( '/(?=>)/', $attr, $html, 1);
253 public $handlers = [];
255 function __construct($path = '.')
257 $this->iterator = new RecursiveCallbackFilterIterator(
258 new RecursiveDirectoryIterator($path),
259 function ($current) {
260 if ($current->getFilename()[0] === '.') {
261 # skip hidden files and directories
264 if (file_exists($current->getFilename() . '/index.php')) {
265 # contents better provided by handler code
266 $this->handlers[ $current->getPathname() ] = $current;
269 if ($current->isLink()) {
270 # ignore symlinks, original contents only
273 if ($current->isDir()) {
274 # traverse subdirectories unless untracked in any amount
275 return !file_exists("$current/.gitignore");
278 return preg_match('/(?<!\.inc)\.html$/', $current->getFilename());
285 # order alphabetically by link
287 foreach (new RecursiveIteratorIterator($this->iterator) as $name) {
288 $article = new ArchiveArticle($name);
289 $dir[$article->link] = $article;