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