xref: /template/ad-hominem/my_template.php (revision 53dcb6a92128465b96fcf56b8e95bf45d5129c12)
1<?php
2/**
3 * Overwriting DokuWiki template functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Sascha Leib <sascha@leib.be>
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 */
9
10use dokuwiki\Extension\Event;
11use dokuwiki\File\PageResolver;
12
13/**
14 * Print the specific HTML meta headers
15 *
16 * Overrides the original version by modifying the headers and the way it is printed
17 *
18 * @author Sascha Leib <sascha@leib.be>
19 * @author Andreas Gohr <andi@splitbrain.org>
20 *
21 * @triggers TPL_METAHEADER_OUTPUT
22 * @param  bool $alt Should feeds and alternative format links be added?
23 * @return bool
24 */
25function my_metaheaders($alt = true) {
26    global $ID;
27    global $REV;
28    global $INFO;
29    global $JSINFO;
30    global $ACT;
31    global $QUERY;
32    global $lang;
33    global $conf;
34    global $updateVersion;
35    /** @var Input $INPUT */
36    global $INPUT;
37
38    // prepare the head array
39    $head = array();
40
41    // prepare seed for js and css
42    $tseed   = $updateVersion;
43    $depends = getConfigFiles('main');
44    $depends[] = DOKU_CONF."tpl/".$conf['template']."/style.ini";
45    foreach($depends as $f) $tseed .= @filemtime($f);
46    $tseed   = md5($tseed);
47
48	// Open Graph information
49	$meta = p_get_metadata($ID);
50	if (is_array($meta) && array_key_exists('title', $meta) && $meta['title'] !== null) {
51		$head['meta'][] = array('property' => 'og:title', 'content' => tpl_pagetitle($ID, true));
52		$head['meta'][] = array('property' => 'og:site_name ', 'content' => $conf['title']);
53		$head['meta'][] = array('property' => 'og:type', 'content' => 'website');
54		$head['meta'][] = array('property' => 'og:url', 'content' => wl($ID, '', true, '&'));
55
56		if (array_key_exists('description', $meta) && is_array($meta['description'])) {
57			if (array_key_exists('abstract', $meta['description'])) {
58				$parts = explode("\n", $meta['description']['abstract']);
59
60				if (is_array($parts) && array_key_exists(2, $parts)) {
61					$head['meta'][] = array('property' => 'og:description', 'content' => $parts[2]);
62
63					// Bing insists in a non-og description:
64					$head['meta'][] = array('property' => 'description', 'content' => $parts[2]);
65				}
66			}
67		}
68	}
69
70    // the usual stuff
71    $head['meta'][] = array('name'=> 'generator', 'content'=> 'DokuWiki');
72    if(actionOK('search')) {
73        $head['link'][] = array(
74            'rel' => 'search', 'type'=> 'application/opensearchdescription+xml',
75            'href'=> DOKU_BASE.'lib/exe/opensearch.php', 'title'=> $conf['title']
76        );
77    }
78
79    $head['link'][] = array('rel'=> 'start', 'href'=> DOKU_BASE);
80    if(actionOK('index')) {
81        $head['link'][] = array(
82            'rel'  => 'contents', 'href'=> wl($ID, 'do=index', false, '&'),
83            'title'=> $lang['btn_index']
84        );
85    }
86
87    if (actionOK('manifest')) {
88        $head['link'][] = array('rel'=> 'manifest', 'href'=> DOKU_BASE.'lib/exe/manifest.php');
89    }
90
91    $styleUtil = new \dokuwiki\StyleUtils();
92    $styleIni = $styleUtil->cssStyleini();
93    $replacements = $styleIni['replacements'];
94    if (!empty($replacements['__theme_color__'])) {
95        $head['meta'][] = array('name' => 'theme-color', 'content' => $replacements['__theme_color__']);
96    }
97
98    if($alt) {
99        if(actionOK('rss')) {
100            $head['link'][] = array(
101                'rel'  => 'alternate', 'type'=> 'application/rss+xml',
102                'title'=> $lang['btn_recent'], 'href'=> DOKU_BASE.'feed.php'
103            );
104            $head['link'][] = array(
105                'rel'  => 'alternate', 'type'=> 'application/rss+xml',
106                'title'=> $lang['currentns'],
107                'href' => DOKU_BASE.'feed.php?mode=list&ns='.(isset($INFO) ? $INFO['namespace'] : '')
108            );
109        }
110        if(($ACT == 'show' || $ACT == 'search') && $INFO['writable']) {
111            $head['link'][] = array(
112                'rel'  => 'edit',
113                'title'=> $lang['btn_edit'],
114                'href' => wl($ID, 'do=edit', false, '&')
115            );
116        }
117
118        if(actionOK('rss') && $ACT == 'search') {
119            $head['link'][] = array(
120                'rel'  => 'alternate', 'type'=> 'application/rss+xml',
121                'title'=> $lang['searchresult'],
122                'href' => DOKU_BASE.'feed.php?mode=search&q='.$QUERY
123            );
124        }
125
126        if(actionOK('export_xhtml')) {
127            $head['link'][] = array(
128                'rel' => 'alternate', 'type'=> 'text/html', 'title'=> $lang['plainhtml'],
129                'href'=> exportlink($ID, 'xhtml', '', false, '&')
130            );
131        }
132
133        if(actionOK('export_raw')) {
134            $head['link'][] = array(
135                'rel' => 'alternate', 'type'=> 'text/plain', 'title'=> $lang['wikimarkup'],
136                'href'=> exportlink($ID, 'raw', '', false, '&')
137            );
138        }
139    }
140
141    // setup robot tags apropriate for different modes
142    if(($ACT == 'show' || $ACT == 'export_xhtml') && !$REV) {
143        if($INFO['exists']) {
144            //delay indexing:
145            if((time() - $INFO['lastmod']) >= $conf['indexdelay'] && !isHiddenPage($ID) ) {
146                $head['meta'][] = array('name'=> 'robots', 'content'=> 'index,follow');
147            } else {
148                $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,nofollow');
149            }
150            $canonicalUrl = wl($ID, '', true, '&');
151            if ($ID == $conf['start']) {
152                $canonicalUrl = DOKU_URL;
153            }
154            $head['link'][] = array('rel'=> 'canonical', 'href'=> $canonicalUrl);
155        } else {
156            $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,follow');
157        }
158    } elseif(defined('DOKU_MEDIADETAIL')) {
159        $head['meta'][] = array('name'=> 'robots', 'content'=> 'index,follow');
160    } else {
161        $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,nofollow');
162    }
163
164    // set metadata
165    if($ACT == 'show' || $ACT == 'export_xhtml') {
166        // keywords (explicit or implicit)
167        if(!empty($INFO['meta']['subject'])) {
168            $head['meta'][] = array('name'=> 'keywords', 'content'=> join(',', $INFO['meta']['subject']));
169        } else {
170            $head['meta'][] = array('name'=> 'keywords', 'content'=> str_replace(':', ',', $ID));
171        }
172    }
173
174    // load stylesheets
175    $head['link'][] = array(
176        'rel' => 'stylesheet',
177        'href'=> DOKU_BASE . 'lib/exe/css.php?t='.rawurlencode($conf['template']).'&tseed='.$tseed
178    );
179
180    $script = "var NS='".(isset($INFO)?$INFO['namespace']:'')."';\n\t\t";
181    if($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) {
182        $script .= "var SIG=".toolbar_signature().";\n\t\t";
183    }
184
185    if($conf['basedir']) {
186        $script .= 'var BASEDIR="'.$conf['basedir']."\";\n\t\t";
187    }
188
189    jsinfo();
190    $script .= 'var JSINFO = ' . json_encode($JSINFO).';';
191    $head['script'][] = array('_data'=> $script);
192
193    // load jquery
194    $jquery = getCdnUrls();
195    foreach($jquery as $src) {
196        $head['script'][] = array(
197            /* 'charset' => 'utf-8', -- obsolete */
198            '_data' => '',
199            'src' => $src,
200        ) + ($conf['defer_js'] ? [ 'defer' => 'defer'] : []);
201    }
202
203    // load our javascript dispatcher
204    $head['script'][] = array(
205        /* 'charset'=> 'utf-8', -- obsolete */
206		'_data'=> '',
207        'src' => DOKU_BASE . 'lib/exe/js.php'.'?t='.rawurlencode($conf['template']).'&tseed='.$tseed,
208    ) + ($conf['defer_js'] ? [ 'defer' => 'defer'] : []);
209
210    // trigger event here
211    Event::createAndTrigger('TPL_METAHEADER_OUTPUT', $head, '_my_metaheaders_action', true);
212    return true;
213}
214
215/**
216 * prints the array build by my_metaheaders
217 *
218 * Overrides the original version by adding a tab before each line for neater HTML code
219 *
220 * @author Sascha Leib <sascha@leib.be>
221 * @author Andreas Gohr <andi@splitbrain.org>
222 *
223 * @param array $data
224 */
225function _my_metaheaders_action($data) {
226    foreach($data as $tag => $inst) {
227        foreach($inst as $attr) {
228            if ( empty($attr) ) { continue; }
229            echo "\t<", $tag, ' ', buildAttributes($attr);
230            if(isset($attr['_data']) || $tag == 'script') {
231                if($tag == 'script' && $attr['_data'])
232                    $attr['_data'] = "/*<![CDATA[*/".
233                        $attr['_data'].
234                        "\n/*!]]>*/";
235
236                echo '>', $attr['_data'], '</', $tag, '>';
237            } else {
238                echo '/>';
239            }
240            echo "\n";
241        }
242    }
243}
244
245/**
246 * get a link to the homepage.
247 *
248 * wraps the original wl() function to allow overriding in the options
249 *
250 * @author Sascha Leib <sascha@leib.be>
251 *
252 * @returns string (link)
253 */
254function my_homelink() {
255    global $conf;
256
257	$hl = trim(tpl_getConf('homelink'));
258
259	if ( $hl !== '' ) {
260		return $hl;
261	} else {
262		return wl(); // default homelink
263	}
264}
265
266/**
267 * Print the breadcrumbs trace
268 *
269 * Cleanup of the original code to create neater and more accessible HTML
270 *
271 * @author Sascha Leib <sascha@leib.be>
272 * @author Andreas Gohr <andi@splitbrain.org>
273 *
274 * @param string $prefix inserted before each line
275 *
276 * @return void
277 */
278function my_breadcrumbs($prefix = '') {
279    global $lang;
280    global $conf;
281
282    //check if enabled
283    if(!$conf['breadcrumbs']) return false;
284
285    $crumbs = breadcrumbs(); //setup crumb trace
286
287	/* begin listing */
288	echo $prefix . "<nav id=\"navBreadCrumbs\">\n";
289	echo $prefix . "\t<h4>" . $lang['breadcrumb'] . "</h4>\n";
290	echo $prefix . "\t<ol reversed>\n";
291
292    $last = count($crumbs);
293    $i    = 0;
294    foreach($crumbs as $id => $name) {
295        $i++;
296		echo $prefix . "\t\t<li" . ($i == $last ? ' class="current"' : '') . '><bdi>' . tpl_link(wl($id), hsc($name), '', true) .  "</bdi></li>\n";
297    }
298	echo $prefix . "\t</ol>\n";
299	echo $prefix . "</nav>\n";
300}
301
302/**
303 * Hierarchical breadcrumbs
304 *
305 * Cleanup of the original code to create neater and more accessible HTML
306 *
307 * @author Sascha Leib <sascha@leib.be>
308 * @author Andreas Gohr <andi@splitbrain.org>
309 * @author Nigel McNie <oracle.shinoda@gmail.com>
310 * @author Sean Coates <sean@caedmon.net>
311 * @author <fredrik@averpil.com>
312 *
313 * @param  string $prefix to be added before each line
314 *
315 */
316function my_youarehere($prefix = '') {
317    global $conf;
318    global $ID;
319    global $lang;
320
321    // check if enabled
322    if(!$conf['youarehere']) return false;
323
324    $parts = explode(':', $ID);
325    $count = count($parts);
326	$isdir = ( $parts[$count-1] == $conf['start']);
327
328	$hl = trim(tpl_getConf('homelink'));
329
330	echo $prefix . "<nav id=\"navYouAreHere\">\n";
331	echo $prefix . "\t<h4>" . $lang['youarehere'] . "</h4>\n";
332	echo $prefix . "\t<ol>\n";
333
334    // always print the startpage
335	if ( $hl !== '' ) {
336		echo $prefix . "\t\t<li class=\"home\">" . tpl_link( $hl, htmlentities(tpl_getLang('homepage')), ' title="' . htmlentities(tpl_getLang('homepage')) . '"', true) . "</li>\n";
337		echo $prefix . "\t\t<li>" . tpl_pagelink(':'.$conf['start'], null, true) . "</li>\n";
338	} else {
339		echo $prefix . "\t\t<li class=\"home\">" . tpl_pagelink(':'.$conf['start'], null, true) . "</li>\n";
340	}
341
342    // print intermediate namespace links
343    $page = '';
344    for($i = 0; $i < $count - 1; $i++) {
345        $part = $parts[$i];
346        $page .= $part . ':';
347
348		if ($i == $count-2 && $isdir)  break; // Skip last if it is an index page
349
350		echo $prefix . "\t\t<li>" . tpl_pagelink($page, null, true) . "</li>\n";
351    }
352
353    // chould the current page be included in the listing?
354	$trail = tpl_getConf('navtrail');
355
356	if ($trail !== 'none' && $trail !== '') {
357
358		echo $prefix . "\t\t<li class=\"current\">";
359		if ($trail == 'text') {
360			echo tpl_pagetitle($page . $parts[$count-1], true);
361		} else if ($trail == 'link') {
362			echo tpl_pagelink($page . $parts[$count-1], null, true);
363		}
364		echo "</li>\n";
365	}
366
367	echo $prefix . "\t</ol>\n";
368	echo $prefix . "</nav>\n";
369}
370
371/**
372 * My implementation of the basic userinfo (in the global banner)
373 *
374 *
375 * @author Sascha Leib <sascha@leib.be>
376 *
377 * @param  string $prefix to be added before each line
378 *
379 * @return void
380 */
381function my_userinfo($prefix = '') {
382    global $lang;
383    global $INPUT;
384
385	// add login/logout button:
386	$items = (new \dokuwiki\Menu\UserMenu())->getItems();
387	foreach($items as $it) {
388		$typ = $it->getType();
389
390		if ($typ === 'profile') { // special case for user profile:
391
392			echo $prefix . '<li class="action profile"><span class="sronly">' . $lang['loggedinas'] .
393				' </span><a href="' . htmlentities($it->getLink()) . '" title="' . $it->getTitle() . '">' .
394				userlink() . "</a></li>\n";
395
396		} else {
397
398			echo $prefix . "<li class=\"action $typ\"><a href=\"" . htmlentities($it->getLink()) .
399				'" title="' . $it->getTitle() . '">' . ($typ === 'profile'? userlink() : $it->getLabel() ) .
400				"</a></li>\n";
401		}
402	}
403}
404
405/**
406 *Inserts a cleaner version of the TOC
407 *
408 * This is an update of the original function that renders the TOC directly.
409 *
410 * @author Sascha Leib <sascha@leib.be>
411 * @author Andreas Gohr <andi@splitbrain.org>
412 *
413 * @param  string $prefix to be added before each line
414 *
415 * @return void
416 */
417function my_toc($prefix = '') {
418    global $TOC;
419    global $ACT;
420    global $ID;
421    global $REV;
422    global $INFO;
423    global $conf;
424    global $lang;
425    $toc = array();
426
427    if(is_array($TOC)) {
428        // if a TOC was prepared in global scope, always use it
429        $toc = $TOC;
430    } elseif(($ACT == 'show' || substr($ACT, 0, 6) == 'export') && !$REV && $INFO['exists']) {
431        // get TOC from metadata, render if neccessary
432        $meta = p_get_metadata($ID, '', METADATA_RENDER_USING_CACHE);
433        if(isset($meta['internal']['toc'])) {
434            $tocok = $meta['internal']['toc'];
435        } else {
436            $tocok = true;
437        }
438        $toc = isset($meta['description']['tableofcontents']) ? $meta['description']['tableofcontents'] : null;
439        if(!$tocok || !is_array($toc) || !$conf['tocminheads'] || count($toc) < $conf['tocminheads']) {
440            $toc = array();
441        }
442    } elseif($ACT == 'admin') {
443        // try to load admin plugin TOC
444        /** @var $plugin AdminPlugin */
445        if ($plugin = plugin_getRequestAdminPlugin()) {
446            $toc = $plugin->getTOC();
447            $TOC = $toc; // avoid later rebuild
448        }
449    }
450
451	/* Build the hierarchical list of headline links: */
452	if (count($toc) >= intval($conf['tocminheads'])) {
453		echo $prefix . "<aside id=\"toc\" class=\"toggle hide\">\n";
454		echo $prefix . "\t<h3 class=\"tg_button\" title=\"" . htmlentities($lang['toc']) . '"><span>' . htmlentities($lang['toc']) . "</span></h3>\n" . $prefix . "\t<div class=\"tg_content\">";
455		$level = intval("0");
456		foreach($toc as $it) {
457
458			$nl = intval($it['level']);
459			$cp = ($nl <=> $level);
460
461			if ($cp > 0) {
462				echo "\n" . $prefix . str_repeat("\t", $level*2 + 2) . "<ol>\n";
463			} else if ($cp < 0) {
464				echo "\n" . $prefix . str_repeat("\t", $level*2) . "</ol></li>\n";
465			} else {
466				echo "</li>\n";
467			}
468
469			$href = ( array_key_exists('link', $it) ? $it['link'] : '');
470			$href .= (array_key_exists('hid', $it) ? '#' . $it['hid'] : '');
471
472			echo $prefix . str_repeat("\t", $nl*2 + 1) . '<li><a href="' . $href . '">' . htmlentities($it['title']) . "</a>";
473			$level = $nl;
474		}
475
476		for ($i = $level-1; $i > 0; $i--) {
477			echo "</li>\n" . $prefix . str_repeat("\t", $i*2 + 1) . "</ol>";
478		}
479
480		echo "</li>\n" . $prefix . "\t\t</ol>\n" . $prefix . "\t</div>\n" . $prefix . "</aside>\n";
481	}
482}
483
484/**
485 * Print last change date
486 *
487 * @author Sascha Leib <sascha@leib.be>
488 *
489 * @param  string $prefix to be added before each line
490 *
491 * @return void
492 */
493function my_lastchange($prefix = '') {
494
495    global $lang;
496    global $INFO;
497
498	$format = '%Y-%m-%dT%T%z';	/* e.g. 2021-21-05T16:45:12+02:00 */
499
500	$date = $INFO['lastmod'];
501
502	echo $prefix . '<bdi>' . $lang['lastmod'] . "</bdi>\n";
503	echo $prefix . '<time datetime="' . strftime($format, $date) . '">' . dformat($date) . "</time>\n";
504
505	/* user name for last change (is this really interesting to the visitor?) */
506	/* echo $prefix .'<span class="editorname" tabindex="0">' . $lang['by'] . ' <bdi>' . editorinfo($INFO['editor']) . "</bdi></span>\n"; */
507}
508
509/**
510 * Returns a description list of the metatags of the current image
511 *
512 * @return string html of description list
513 */
514function my_img_meta($prefix = '') {
515    global $lang;
516
517	$format = '%Y-%m-%dT%T%z';	/* e.g. 2021-21-05T16:45:12+02:00 */
518
519    $tags = tpl_get_img_meta();
520
521    foreach($tags as $tag) {
522        $label = $lang[$tag['langkey']];
523        if(!$label) $label = $tag['langkey'] . ':';
524
525        echo $prefix . '<tr><th>'.$label.'</th><td>';
526        if ($tag['type'] == 'date') {
527            echo '<time datetime="' . strftime($format, $tag['value']) . '">' . dformat($tag['value']) . '</time>';
528        } else {
529            echo hsc($tag['value']);
530        }
531        echo "</td></tr>\n";
532    }
533}
534
535/**
536 * Creates the Site logo image link
537 *
538 */
539function my_sitelogo() {
540    global $conf;
541
542	// get logo either out of the template images folder or data/media folder
543	$logoSize = array();
544	$logo = tpl_getMediaFile(array(':logo.svg', ':wiki:logo.svg', ':logo.png', ':wiki:logo.png', 'images/sitelogo.svg'), false, $logoSize);
545	tpl_link( my_homelink(),
546		'<img src="'.$logo.'" ' . (is_array($logoSize) && array_key_exists(3, $logoSize) ? $logoSize[3] : '') . ' alt="' . htmlentities($conf['title']) . '" />', 'accesskey="h" title="[H]" class="logo"');
547}
548
549/**
550 * Creates the various favicon and similar links:
551 *
552 * @param  string $color overwrite the theme color.
553 *
554 * @return null
555 */
556function my_favicons($color = null) {
557
558	$logoSize = array();
559
560	// Theme color:
561	if ($color == null) {
562
563		/* get the style config */
564		$styleUtil = new \dokuwiki\StyleUtils();
565		$styleIni = $styleUtil->cssStyleini();
566		$replacements = $styleIni['replacements'];
567		$color = $replacements['__theme_color__'];
568
569		if ($color== null) { $color = '#2b73b7'; }
570	}
571	echo "\t<meta name=\"theme-color\" content=\"" . $color . "\" />\n";
572
573	// get the favicon:
574	$link = tpl_getMediaFile(array(':favicon.ico', ':favicon.png', ':favicon.svg', ':wiki:favicon.ico', ':wiki:favicon.png', ':wiki:favicon.svg'), false, $logoSize);
575	echo "\t<link rel=\"icon\" href=\"" . $link . "\" />\n";
576
577	// Apple Touch Icon
578	$logoSize = array();
579	$link = tpl_getMediaFile(array(':apple-touch-icon.png', ':wiki:apple-touch-icon.png', 'images/apple-touch-icon.png'), false, $logoSize);
580	echo "\t<link rel=\"apple-touch-icon\" href=\"" . $link . "\" />\n";
581
582}
583
584/**
585 * inserts the Cookies banner, if appropriate.
586 * This is based on Michal Koutny’s "cookielaw" plugin
587 *
588 * @param  string $prefix to be added before each line
589 */
590function my_cookiebanner($prefix = '') {
591
592	// get the configuration settings:
593	$msg = tpl_getConf('cookiemsg', '(no message configured)');
594	$position = tpl_getConf('cookiepos', 'bottom');
595	$link = tpl_getConf('cookielink', 'about:cookies');
596
597	// if the cookie is already set or position is set to hide, do nothing.
598	if ( isset($_COOKIE['cookielaw']) or $position == 'hide') {
599		return;
600	}
601
602	// output the HTML code:
603	echo $prefix . "<div id=\"cookiebanner\" class=\"cb_" . $position . "\">\n";
604	echo $prefix . "\t<p class=\"cb_info\"><span class=\"cb_icon\"></span>\n";
605	echo $prefix . "\t\t<span class=\"cb_msg\">". $msg . "</span>\r";
606	echo $prefix . "\t</p>\n";
607	echo $prefix . "\t<p class=\"cb_action\">\n";
608	echo $prefix . "\t\t<button>" . hsc(tpl_getLang('cookie_consent')) . "</button>\n";
609	echo $prefix . "\t\t";
610	if ( substr($link, 0, 7) == 'http://' || substr($link, 0, 8) == 'https://') {
611		echo '<a href="' . $link . '" target="_blank">' . hsc(tpl_getLang('cookie_linktext')) . '</a>';
612	} else {
613		tpl_pagelink($link, tpl_getLang('cookie_linktext'));
614	}
615	echo $prefix . "\n\t</p>\n" . $prefix . "</div>\n";
616
617}
618