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