xref: /template/ad-hominem/my_template.php (revision 200aa3d28686b1ce64ef059e352d5687f1d67f70)
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('name' => '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(
96			'name' => 'theme-color',
97			'content' => $replacements['__theme_color__']
98		);
99    }
100
101    if($alt) {
102        if(actionOK('rss')) {
103            $head['link'][] = array(
104                'rel'  => 'alternate', 'type'=> 'application/rss+xml',
105                'title'=> $lang['btn_recent'], 'href'=> DOKU_BASE.'feed.php'
106            );
107            $head['link'][] = array(
108                'rel'  => 'alternate', 'type'=> 'application/rss+xml',
109                'title'=> $lang['currentns'],
110                'href' => DOKU_BASE.'feed.php?mode=list&ns='.(isset($INFO) ? $INFO['namespace'] : '')
111            );
112        }
113        if(($ACT == 'show' || $ACT == 'search') && $INFO['writable']) {
114            $head['link'][] = array(
115                'rel'  => 'edit',
116                'title'=> $lang['btn_edit'],
117                'href' => wl($ID, 'do=edit', false, '&')
118            );
119        }
120
121        if(actionOK('rss') && $ACT == 'search') {
122            $head['link'][] = array(
123                'rel'  => 'alternate', 'type'=> 'application/rss+xml',
124                'title'=> $lang['searchresult'],
125                'href' => DOKU_BASE.'feed.php?mode=search&q='.$QUERY
126            );
127        }
128
129        if(actionOK('export_xhtml')) {
130            $head['link'][] = array(
131                'rel' => 'alternate', 'type'=> 'text/html', 'title'=> $lang['plainhtml'],
132                'href'=> exportlink($ID, 'xhtml', '', false, '&')
133            );
134        }
135
136        if(actionOK('export_raw')) {
137            $head['link'][] = array(
138                'rel' => 'alternate', 'type'=> 'text/plain', 'title'=> $lang['wikimarkup'],
139                'href'=> exportlink($ID, 'raw', '', false, '&')
140            );
141        }
142    }
143
144    // setup robot tags apropriate for different modes
145    if(($ACT == 'show' || $ACT == 'export_xhtml') && !$REV) {
146        if($INFO['exists']) {
147            //delay indexing:
148            if((time() - $INFO['lastmod']) >= $conf['indexdelay'] && !isHiddenPage($ID) ) {
149                $head['meta'][] = array('name'=> 'robots', 'content'=> 'index,follow');
150            } else {
151                $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,nofollow');
152            }
153            $canonicalUrl = wl($ID, '', true, '&');
154            if ($ID == $conf['start']) {
155                $canonicalUrl = DOKU_URL;
156            }
157            $head['link'][] = array('rel'=> 'canonical', 'href'=> $canonicalUrl);
158        } else {
159            $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,follow');
160        }
161    } elseif(defined('DOKU_MEDIADETAIL')) {
162        $head['meta'][] = array('name'=> 'robots', 'content'=> 'index,follow');
163    } else {
164        $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,nofollow');
165    }
166
167    // set metadata
168    if($ACT == 'show' || $ACT == 'export_xhtml') {
169        // keywords (explicit or implicit)
170        if(!empty($INFO['meta']['subject'])) {
171            $head['meta'][] = array('name'=> 'keywords', 'content'=> join(',', $INFO['meta']['subject']));
172        } else {
173            $head['meta'][] = array('name'=> 'keywords', 'content'=> str_replace(':', ',', $ID));
174        }
175    }
176
177    // load stylesheets
178    $head['link'][] = array(
179        'rel' => 'stylesheet',
180        'href'=> DOKU_BASE . 'lib/exe/css.php?t='.rawurlencode($conf['template']).'&tseed='.$tseed,
181		'defer' => 'defer'
182    );
183
184    $script = "var NS='".(isset($INFO)?$INFO['namespace']:'')."';\n\t\t";
185    if($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) {
186        $script .= "var SIG=".toolbar_signature().";\n\t\t";
187    }
188
189    if($conf['basedir']) {
190        $script .= 'var BASEDIR="'.$conf['basedir']."\";\n\t\t";
191    }
192
193    jsinfo();
194    $script .= 'var JSINFO = ' . json_encode($JSINFO).';';
195    $head['script'][] = array('_data'=> $script);
196
197    // load jquery
198    $jquery = getCdnUrls();
199    foreach($jquery as $src) {
200        $head['script'][] = array(
201            /* 'charset' => 'utf-8', -- obsolete */
202            '_data' => '',
203            'src' => $src,
204        ) + ($conf['defer_js'] ? [ 'defer' => 'defer'] : []);
205    }
206
207    // load our javascript dispatcher
208    $head['script'][] = array(
209        /* 'charset'=> 'utf-8', -- obsolete */
210		'_data'=> '',
211        'src' => DOKU_BASE . 'lib/exe/js.php'.'?t='.rawurlencode($conf['template']).'&tseed='.$tseed,
212    ) + ($conf['defer_js'] ? [ 'defer' => 'defer'] : []);
213
214    // trigger event here
215    Event::createAndTrigger('TPL_METAHEADER_OUTPUT', $head, '_my_metaheaders_action', true);
216    return true;
217}
218
219/**
220 * prints the array build by my_metaheaders
221 *
222 * Overrides the original version by adding a tab before each line for neater HTML code
223 *
224 * @author Sascha Leib <sascha@leib.be>
225 * @author Andreas Gohr <andi@splitbrain.org>
226 *
227 * @param array $data
228 */
229function _my_metaheaders_action($data) {
230    foreach($data as $tag => $inst) {
231        foreach($inst as $attr) {
232            if ( empty($attr) ) { continue; }
233            echo "\t<", $tag, ' ', buildAttributes($attr);
234            if(isset($attr['_data']) || $tag == 'script') {
235                if($tag == 'script' && $attr['_data'])
236                    $attr['_data'] = "/*<![CDATA[*/".
237                        $attr['_data'].
238                        "\n/*!]]>*/";
239
240                echo '>', $attr['_data'], '</', $tag, '>';
241            } else {
242                echo '/>';
243            }
244            echo "\n";
245        }
246    }
247}
248
249/**
250 * get a link to the homepage.
251 *
252 * wraps the original wl() function to allow overriding in the options
253 *
254 * @author Sascha Leib <sascha@leib.be>
255 *
256 * @returns string (link)
257 */
258function my_homelink() {
259    global $conf;
260
261	$hl = trim(tpl_getConf('homelink'));
262
263	if ( $hl !== '' ) {
264		return $hl;
265	} else {
266		return wl(); // default homelink
267	}
268}
269
270/**
271 * Print the breadcrumbs trace
272 *
273 * Cleanup of the original code to create neater and more accessible HTML
274 *
275 * @author Sascha Leib <sascha@leib.be>
276 * @author Andreas Gohr <andi@splitbrain.org>
277 *
278 * @param string $prefix inserted before each line
279 *
280 * @return void
281 */
282function my_breadcrumbs($prefix = '') {
283    global $lang;
284    global $conf;
285
286    //check if enabled
287    if(!$conf['breadcrumbs']) return false;
288
289    $crumbs = breadcrumbs(); //setup crumb trace
290
291	/* begin listing */
292	echo $prefix . "<nav id=\"navBreadCrumbs\">\n";
293	echo $prefix . "\t<h4>" . $lang['breadcrumb'] . "</h4>\n";
294	echo $prefix . "\t<ol reversed>\n";
295
296    $last = count($crumbs);
297    $i    = 0;
298    foreach($crumbs as $id => $name) {
299        $i++;
300		echo $prefix . "\t\t<li" . ($i == $last ? ' class="current"' : '') . '><bdi>' . tpl_link(wl($id), hsc($name), '', true) .  "</bdi></li>\n";
301    }
302	echo $prefix . "\t</ol>\n";
303	echo $prefix . "</nav>\n";
304}
305
306/**
307 * Hierarchical breadcrumbs
308 *
309 * Cleanup of the original code to create neater and more accessible HTML
310 *
311 * @author Sascha Leib <sascha@leib.be>
312 * @author Andreas Gohr <andi@splitbrain.org>
313 * @author Nigel McNie <oracle.shinoda@gmail.com>
314 * @author Sean Coates <sean@caedmon.net>
315 * @author <fredrik@averpil.com>
316 *
317 * @param  string $prefix to be added before each line
318 *
319 */
320function my_youarehere($prefix = '') {
321    global $conf;
322    global $ID;
323    global $lang;
324
325    // check if enabled
326    if(!$conf['youarehere']) return false;
327
328    $parts = explode(':', $ID);
329    $count = count($parts);
330	$isdir = ( $parts[$count-1] == $conf['start']);
331
332	$hl = trim(tpl_getConf('homelink'));
333
334	echo $prefix . "<nav id=\"navYouAreHere\">\n";
335	echo $prefix . "\t<h4>" . $lang['youarehere'] . "</h4>\n";
336	echo $prefix . "\t<ol>\n";
337
338    // always print the startpage
339	if ( $hl !== '' ) {
340		echo $prefix . "\t\t<li class=\"home\">" . tpl_link( $hl, htmlentities(tpl_getLang('homepage')), ' title="' . htmlentities(tpl_getLang('homepage')) . '"', true) . "</li>\n";
341		echo $prefix . "\t\t<li>" . tpl_pagelink(':'.$conf['start'], null, true) . "</li>\n";
342	} else {
343		echo $prefix . "\t\t<li class=\"home\">" . tpl_pagelink(':'.$conf['start'], null, true) . "</li>\n";
344	}
345
346    // print intermediate namespace links
347    $page = '';
348    for($i = 0; $i < $count - 1; $i++) {
349        $part = $parts[$i];
350        $page .= $part . ':';
351
352		if ($i == $count-2 && $isdir)  break; // Skip last if it is an index page
353
354		echo $prefix . "\t\t<li>" . tpl_pagelink($page, null, true) . "</li>\n";
355    }
356
357    // chould the current page be included in the listing?
358	$trail = tpl_getConf('navtrail');
359
360	if ($trail !== 'none' && $trail !== '') {
361
362		echo $prefix . "\t\t<li class=\"current\">";
363		if ($trail == 'text') {
364			echo tpl_pagetitle($page . $parts[$count-1], true);
365		} else if ($trail == 'link') {
366			echo tpl_pagelink($page . $parts[$count-1], null, true);
367		}
368		echo "</li>\n";
369	}
370
371	echo $prefix . "\t</ol>\n";
372	echo $prefix . "</nav>\n";
373}
374
375/**
376 * My implementation of the basic userinfo (in the global banner)
377 *
378 *
379 * @author Sascha Leib <sascha@leib.be>
380 *
381 * @param  string $prefix to be added before each line
382 *
383 * @return void
384 */
385function my_userinfo($prefix = '') {
386    global $lang;
387    global $INPUT;
388
389	// add login/logout button:
390	$items = (new \dokuwiki\Menu\UserMenu())->getItems();
391	foreach($items as $it) {
392		$typ = $it->getType();
393
394		if ($typ === 'profile') { // special case for user profile:
395
396			echo $prefix . '<li class="action profile"><span class="sronly">' . $lang['loggedinas'] .
397				' </span><a href="' . htmlentities($it->getLink()) . '" title="' . $it->getTitle() . '">' .
398				userlink() . "</a></li>\n";
399
400		} else {
401
402			echo $prefix . "<li class=\"action $typ\"><a href=\"" . htmlentities($it->getLink()) .
403				'" title="' . $it->getTitle() . '">' . ($typ === 'profile'? userlink() : $it->getLabel() ) .
404				"</a></li>\n";
405		}
406	}
407}
408
409/**
410 *Inserts a cleaner version of the TOC
411 *
412 * This is an update of the original function that renders the TOC directly.
413 *
414 * @author Sascha Leib <sascha@leib.be>
415 * @author Andreas Gohr <andi@splitbrain.org>
416 *
417 * @param  string $prefix to be added before each line
418 *
419 * @return void
420 */
421function my_toc($prefix = '') {
422    global $TOC;
423    global $ACT;
424    global $ID;
425    global $REV;
426    global $INFO;
427    global $conf;
428    global $lang;
429    $toc = array();
430
431    if(is_array($TOC)) {
432        // if a TOC was prepared in global scope, always use it
433        $toc = $TOC;
434    } elseif(($ACT == 'show' || substr($ACT, 0, 6) == 'export') && !$REV && $INFO['exists']) {
435        // get TOC from metadata, render if neccessary
436        $meta = p_get_metadata($ID, '', METADATA_RENDER_USING_CACHE);
437        if(isset($meta['internal']['toc'])) {
438            $tocok = $meta['internal']['toc'];
439        } else {
440            $tocok = true;
441        }
442        $toc = isset($meta['description']['tableofcontents']) ? $meta['description']['tableofcontents'] : null;
443        if(!$tocok || !is_array($toc) || !$conf['tocminheads'] || count($toc) < $conf['tocminheads']) {
444            $toc = array();
445        }
446    } elseif($ACT == 'admin') {
447        // try to load admin plugin TOC
448        /** @var $plugin AdminPlugin */
449        if ($plugin = plugin_getRequestAdminPlugin()) {
450            $toc = $plugin->getTOC();
451            $TOC = $toc; // avoid later rebuild
452        }
453    }
454
455	/* Build the hierarchical list of headline links: */
456	if (count($toc) >= intval($conf['tocminheads'])) {
457		echo $prefix . "<aside id=\"toc\" class=\"toggle hide\">\n";
458		echo $prefix . "\t<button type=\"button\" id=\"toc-menubutton\" class=\"tg_button\" title=\"" . htmlentities($lang['toc']) . '" aria-haspopup="true" aria-controls="toc-menu"><span>' . htmlentities($lang['toc']) . "</span></button>\n" . $prefix . "\t<div id=\"toc-menu\" class=\"tg_content\" role=\"menu\" aria-labelledby=\"toc-menubutton\">";
459		$level = 0;
460		foreach($toc as $it) {
461
462			$nl = intval($it['level']);
463			$cp = ($nl <=> $level);
464
465			if ($cp > 0) {
466				while ($level < $nl) {
467					echo "\n" . $prefix . str_repeat("\t", $level*2 + 2) . "<ol>\n";
468					$level++;
469				}
470			} else if ($cp < 0) {
471				while ($level > $nl) {
472					echo "\n" . $prefix . str_repeat("\t", $level*2) . "</ol>\n" . $prefix . str_repeat("\t", $level*2-1) . "</li>\n";
473					$level--;
474				}
475			} else {
476				echo "</li>\n";
477			}
478
479			$href = ( array_key_exists('link', $it ) ? $it['link'] : '' ) . ( array_key_exists('hid', $it) && $it['hid'] !== '' ? '#' . $it['hid'] : '' );
480
481			echo $prefix . str_repeat("\t", $nl*2 + 1) . '<li  role="presentation"><a role="menuitem" href="' . $href . '">' . htmlentities($it['title']) . "</a>";
482			$level = $nl;
483		}
484
485		for ($i = $level-1; $i > 0; $i--) {
486			echo "</li>\n" . $prefix . str_repeat("\t", $i*2 + 1) . "</ol>";
487		}
488
489		echo "</li>\n" . $prefix . "\t\t</ol>\n" . $prefix . "\t</div>\n" . $prefix . "</aside>\n";
490	}
491}
492
493/**
494 * Print last change date
495 *
496 * @author Sascha Leib <sascha@leib.be>
497 *
498 * @param  string $prefix to be added before each line
499 *
500 * @return void
501 */
502function my_lastchange($prefix = '') {
503
504    global $lang;
505    global $INFO;
506    global $conf;
507
508	$lastmod = $INFO['lastmod'];
509
510	if (intval($lastmod) > 0) { // is valid date?
511
512		$longDate = htmlentities(dformat($lastmod));
513
514		echo $prefix . "<p class=\"docInfo\">\n";
515		echo $prefix . "\t<bdi>" . $lang['lastmod'] . "</bdi>\n";
516		echo $prefix . "\t<time datetime=\"" . date('c', $lastmod) . '" title="' . $longDate . '"><span class="print-only">' . $longDate . '</span><span class="noprint">' . datetime_h($lastmod) . "</span></time>\n";
517		echo $prefix . "</p>\n";
518	}
519
520	/* user name for last change (is this really interesting to the visitor?) */
521	/* echo $prefix .'<span class="editorname" tabindex="0">' . $lang['by'] . ' <bdi>' . editorinfo($INFO['editor']) . "</bdi></span>\n"; */
522}
523
524/**
525 * Returns a description list of the metatags of the current image
526 *
527 * @return string html of description list
528 */
529function my_img_meta($prefix = '') {
530    global $lang;
531
532	$format = '%Y-%m-%dT%T%z';	/* e.g. 2021-21-05T16:45:12+02:00 */
533
534    $tags = tpl_get_img_meta();
535
536    foreach($tags as $tag) {
537        $label = $lang[$tag['langkey']];
538        if(!$label) $label = $tag['langkey'] . ':';
539
540        echo $prefix . '<tr><th>'.$label.'</th><td>';
541        if ($tag['type'] == 'date') {
542            echo '<time datetime="' . strftime($format, $tag['value']) . '">' . dformat($tag['value']) . '</time>';
543        } else {
544            echo hsc($tag['value']);
545        }
546        echo "</td></tr>\n";
547    }
548}
549
550/**
551 * Creates the Site logo image link
552 *
553 */
554function my_sitelogo() {
555    global $conf;
556
557	// get logo either out of the template images folder or data/media folder
558	$logoSize = array();
559	$logo = tpl_getMediaFile(array(':logo.svg', ':wiki:logo.svg', ':logo.png', ':wiki:logo.png', 'images/sitelogo.svg'), false, $logoSize);
560	tpl_link( my_homelink(),
561		'<img src="'.$logo.'" ' . (is_array($logoSize) && array_key_exists(3, $logoSize) ? $logoSize[3] : '') . ' alt="' . htmlentities($conf['title']) . '" />', 'accesskey="h" title="[H]" class="logo"');
562}
563
564/**
565 * Creates the various favicon and similar links:
566 *
567 * @param  string $color overwrite the theme color.
568 *
569 * @return null
570 */
571function my_favicons($color = null) {
572
573	$logoSize = array();
574
575	/* Theme color:
576	if ($color == null) {
577
578		// get the style config:
579		$styleUtil = new \dokuwiki\StyleUtils();
580		$styleIni = $styleUtil->cssStyleini();
581		$replacements = $styleIni['replacements'];
582		$color = $replacements['__theme_color__'];
583
584		if ($color== null) { $color = '#2b73b7'; }
585	}
586	echo "\t<meta name=\"theme-color\" content=\"" . $color . "\" />\n"; */
587
588	// get the favicon:
589	$link = tpl_getMediaFile(array(':favicon.ico', ':favicon.png', ':favicon.svg', ':wiki:favicon.ico', ':wiki:favicon.png', ':wiki:favicon.svg'), false, $logoSize);
590	echo "\t<link rel=\"icon\" href=\"" . $link . "\" />\n";
591
592	// Apple Touch Icon
593	$logoSize = array();
594	$link = tpl_getMediaFile(array(':apple-touch-icon.png', ':wiki:apple-touch-icon.png', 'images/apple-touch-icon.png'), false, $logoSize);
595	echo "\t<link rel=\"apple-touch-icon\" href=\"" . $link . "\" />\n";
596
597}
598
599/**
600 * inserts the Cookies banner, if appropriate.
601 * This is based on Michal Koutny’s "cookielaw" plugin
602 *
603 * @param  string $prefix to be added before each line
604 */
605function my_cookiebanner($prefix = '') {
606
607	// get the configuration settings:
608	$msg = tpl_getConf('cookiemsg', '(no message configured)');
609	$position = tpl_getConf('cookiepos', 'bottom');
610	$link = tpl_getConf('cookielink', 'about:cookies');
611
612	// if the cookie is already set or position is set to hide, do nothing.
613	if ( isset($_COOKIE['cookielaw']) or $position == 'hide') {
614		return;
615	}
616
617	// output the HTML code:
618	echo $prefix . "<div id=\"cookiebanner\" class=\"cb_" . $position . "\">\n";
619	echo $prefix . "\t<p class=\"cb_info\"><span class=\"cb_icon\"></span>\n";
620	echo $prefix . "\t\t<span class=\"cb_msg\">". $msg . "</span>\r";
621	echo $prefix . "\t</p>\n";
622	echo $prefix . "\t<p class=\"cb_action\">\n";
623	echo $prefix . "\t\t<button>" . hsc(tpl_getLang('cookie_consent')) . "</button>\n";
624	echo $prefix . "\t\t";
625	if ( substr($link, 0, 7) == 'http://' || substr($link, 0, 8) == 'https://') {
626		echo '<a href="' . $link . '" target="_blank">' . hsc(tpl_getLang('cookie_linktext')) . '</a>';
627	} else {
628		tpl_pagelink($link, tpl_getLang('cookie_linktext'));
629	}
630	echo $prefix . "\n\t</p>\n" . $prefix . "</div>\n";
631
632}
633
634/**
635 * inserts the Languages menu, if appropriate.
636 *
637 * @author Sascha Leib <sascha@leib.be>
638 * @author Andreas Gohr <andi@splitbrain.org>
639 *
640 * @param  string $prefix to be added before each line
641 * @param  string $place the location from where it is called
642 * @param  string $checkage should the age of the translation be checked?
643 */
644function my_langmenu($prefix, $place, $checkage = true) {
645
646	global $INFO;
647	global $conf;
648
649	// the current page language:
650	$lang = $conf['lang'];
651
652	/* get the config option: */
653	$config = tpl_getConf('langmenu', 'none');
654
655	/* collect the output: */
656	$out = '';
657
658	if ($config == $place) {
659		$trans = plugin_load('helper','translation');
660		if ($trans) {
661
662			if (!$trans->istranslatable($INFO['id'])) return '';
663			if ($checkage) $trans->checkage();
664
665			[, $idpart] = $trans->getTransParts($INFO['id']);
666
667			$asMenu = ($place == 'tb'); // display as menu only in toolbar!
668
669			$out .= "{$prefix}<div id=\"{$place}Languages\">\n";
670
671			// create the header item
672
673			if ($asMenu) { // menu only
674
675				$langName = htmlentities($trans->getLocalName($lang));
676
677				$svg = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><title>{$langName}</title><path d='M20,2H4A2,2 0 0,0 2,4V22L6,18H20A2,2 0 0,0 22,16V4C22,2.89 21.1,2 20,2Z' /><text lengthAdjust='spacingAndGlyphs' x='50%' y='47%' dominant-baseline='middle' text-anchor='middle' style='font-size:50%'>{$lang}</text></svg>";
678
679				$out .= "{$prefix}\t<button id=\"langButton\" aria-haspopup=\"menu\" aria-controls=\"langMenuWrapper\" aria-expanded=\"false\">\n";
680				$out .= "{$prefix}\t\t{$svg}\n";
681				$out .= "{$prefix}\t\t<span class=\"sronly\">" . $trans->getLang('translations') . "</span>\n{$prefix}\t</button>\n";
682
683			} else { // not a menu
684
685				if (isset($trans->opts['title'])) {
686					$out .= "{$prefix}\t<h4><span>" . htmlentities(tpl_getLang('languages'));
687
688					/* show the about text? */
689					if ($trans->getConf('about')) {
690						$out .= $trans->showAbout();
691					}
692					/* note: special case for French version: */
693					$out .= ( $lang == 'fr' ? '&#8239;:' : ':') . "</span></h4>\n";
694				}
695			}
696
697			/* build the menu content */
698			$out .= "{$prefix}\t<div id=\"langMenu" . ( $asMenu ? 'Wrapper" role="menu" style="display: none"' : 'List"') . ">\n"
699				 .	"{$prefix}\t\t<ul id=\"lang" . ( $asMenu ? 'Menu" role="group"' : 'List"' ) . ">\n";
700
701			foreach ($trans->translations as $t) {
702				$l = ( $t !== '' ? $t : $lang );
703
704				[$trg, $lng] = $trans->buildTransID($t, $idpart);
705				$trg = cleanID($trg);
706				$exists = page_exists($trg, '', false);
707				$filter = tpl_getConf('langfilter', 'all');
708
709				/* only show if translation exists */
710				if ($exists || $filter === 'all') {
711					$class = 'wikilink' . ( $exists ? '1' : '2');
712					$link = wl($trg);
713					$current = ($lng == $lang);
714
715					$out .= "{$prefix}\t\t\t<li" . ( $asMenu ? ' role="presentation"' : '' ). ( $current ? ' class="current"' : '' ) . ">\n";
716					$out .= "{$prefix}\t\t\t\t<a href=\"{$link}\" lang=\"{$lng}\" hreflang=\"{$lng}\" class=\"{$class}\"" . ( $asMenu ? ' role="menuitem"' : '' ) . '><span>'. $trans->getLocalName($lng) . "</span></a>\n";
717					$out .= "{$prefix}\t\t\t</li>\n";
718				}
719			}
720
721			$out .= "{$prefix}\t\t</ul>\n"
722				 .	"{$prefix}\t</div>\n"
723				 .	"{$prefix}</div>\n";
724
725		}
726
727	}
728	echo $out;
729}