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