1<?php 2 3/** 4 * DokuWiki template functions 5 * 6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 7 * @author Andreas Gohr <andi@splitbrain.org> 8 */ 9 10use dokuwiki\ActionRouter; 11use dokuwiki\Action\Exception\FatalException; 12use dokuwiki\Extension\PluginInterface; 13use dokuwiki\Ui\Admin; 14use dokuwiki\StyleUtils; 15use dokuwiki\Menu\Item\AbstractItem; 16use dokuwiki\Form\Form; 17use dokuwiki\Menu\MobileMenu; 18use dokuwiki\Ui\Subscribe; 19use dokuwiki\Extension\AdminPlugin; 20use dokuwiki\Extension\Event; 21use dokuwiki\File\PageResolver; 22 23/** 24 * Access a template file 25 * 26 * Returns the path to the given file inside the current template, uses 27 * default template if the custom version doesn't exist. 28 * 29 * @param string $file 30 * @return string 31 * 32 * @author Andreas Gohr <andi@splitbrain.org> 33 */ 34function template($file) 35{ 36 global $conf; 37 38 if (@is_readable(DOKU_INC . 'lib/tpl/' . $conf['template'] . '/' . $file)) 39 return DOKU_INC . 'lib/tpl/' . $conf['template'] . '/' . $file; 40 41 return DOKU_INC . 'lib/tpl/dokuwiki/' . $file; 42} 43 44/** 45 * Convenience function to access template dir from local FS 46 * 47 * This replaces the deprecated DOKU_TPLINC constant 48 * 49 * @param string $tpl The template to use, default to current one 50 * @return string 51 * 52 * @author Andreas Gohr <andi@splitbrain.org> 53 */ 54function tpl_incdir($tpl = '') 55{ 56 global $conf; 57 if (!$tpl) $tpl = $conf['template']; 58 return DOKU_INC . 'lib/tpl/' . $tpl . '/'; 59} 60 61/** 62 * Convenience function to access template dir from web 63 * 64 * This replaces the deprecated DOKU_TPL constant 65 * 66 * @param string $tpl The template to use, default to current one 67 * @return string 68 * 69 * @author Andreas Gohr <andi@splitbrain.org> 70 */ 71function tpl_basedir($tpl = '') 72{ 73 global $conf; 74 if (!$tpl) $tpl = $conf['template']; 75 return DOKU_BASE . 'lib/tpl/' . $tpl . '/'; 76} 77 78/** 79 * Print the content 80 * 81 * This function is used for printing all the usual content 82 * (defined by the global $ACT var) by calling the appropriate 83 * outputfunction(s) from html.php 84 * 85 * Everything that doesn't use the main template file isn't 86 * handled by this function. ACL stuff is not done here either. 87 * 88 * @param bool $prependTOC should the TOC be displayed here? 89 * @return bool true if any output 90 * 91 * @triggers TPL_ACT_RENDER 92 * @triggers TPL_CONTENT_DISPLAY 93 * @author Andreas Gohr <andi@splitbrain.org> 94 */ 95function tpl_content($prependTOC = true) 96{ 97 global $ACT; 98 global $INFO; 99 $INFO['prependTOC'] = $prependTOC; 100 101 ob_start(); 102 Event::createAndTrigger('TPL_ACT_RENDER', $ACT, 'tpl_content_core'); 103 $html_output = ob_get_clean(); 104 Event::createAndTrigger('TPL_CONTENT_DISPLAY', $html_output, 'ptln'); 105 106 return !empty($html_output); 107} 108 109/** 110 * Default Action of TPL_ACT_RENDER 111 * 112 * @return bool 113 */ 114function tpl_content_core() 115{ 116 $router = ActionRouter::getInstance(); 117 try { 118 $router->getAction()->tplContent(); 119 } catch (FatalException $e) { 120 // there was no content for the action 121 msg(hsc($e->getMessage()), -1); 122 return false; 123 } 124 return true; 125} 126 127/** 128 * Places the TOC where the function is called 129 * 130 * If you use this you most probably want to call tpl_content with 131 * a false argument 132 * 133 * @param bool $return Should the TOC be returned instead to be printed? 134 * @return string 135 * 136 * @author Andreas Gohr <andi@splitbrain.org> 137 */ 138function tpl_toc($return = false) 139{ 140 global $TOC; 141 global $ACT; 142 global $ID; 143 global $REV; 144 global $INFO; 145 global $conf; 146 $toc = []; 147 148 if (is_array($TOC)) { 149 // if a TOC was prepared in global scope, always use it 150 $toc = $TOC; 151 } elseif (($ACT == 'show' || substr($ACT, 0, 6) == 'export') && !$REV && $INFO['exists']) { 152 // get TOC from metadata, render if neccessary 153 $meta = p_get_metadata($ID, '', METADATA_RENDER_USING_CACHE); 154 $tocok = $meta['internal']['toc'] ?? true; 155 $toc = $meta['description']['tableofcontents'] ?? null; 156 if (!$tocok || !is_array($toc) || !$conf['tocminheads'] || count($toc) < $conf['tocminheads']) { 157 $toc = []; 158 } 159 } elseif ($ACT == 'admin') { 160 // try to load admin plugin TOC 161 /** @var AdminPlugin $plugin */ 162 if ($plugin = plugin_getRequestAdminPlugin()) { 163 $toc = $plugin->getTOC(); 164 $TOC = $toc; // avoid later rebuild 165 } 166 } 167 168 Event::createAndTrigger('TPL_TOC_RENDER', $toc, null, false); 169 $html = html_TOC($toc); 170 if ($return) return $html; 171 echo $html; 172 return ''; 173} 174 175/** 176 * Handle the admin page contents 177 * 178 * @return bool 179 * 180 * @author Andreas Gohr <andi@splitbrain.org> 181 */ 182function tpl_admin() 183{ 184 global $INFO; 185 global $TOC; 186 global $INPUT; 187 188 $plugin = null; 189 $class = $INPUT->str('page'); 190 if (!empty($class)) { 191 $pluginlist = plugin_list('admin'); 192 193 if (in_array($class, $pluginlist)) { 194 // attempt to load the plugin 195 /** @var AdminPlugin $plugin */ 196 $plugin = plugin_load('admin', $class); 197 } 198 } 199 200 if ($plugin instanceof PluginInterface) { 201 if (!is_array($TOC)) $TOC = $plugin->getTOC(); //if TOC wasn't requested yet 202 if ($INFO['prependTOC']) tpl_toc(); 203 $plugin->html(); 204 } else { 205 $admin = new Admin(); 206 $admin->show(); 207 } 208 return true; 209} 210 211/** 212 * Print the correct HTML meta headers 213 * 214 * This has to go into the head section of your template. 215 * 216 * @param bool $alt Should feeds and alternative format links be added? 217 * @return bool 218 * @throws JsonException 219 * 220 * @author Andreas Gohr <andi@splitbrain.org> 221 * @triggers TPL_METAHEADER_OUTPUT 222 */ 223function tpl_metaheaders($alt = true) 224{ 225 global $ID; 226 global $REV; 227 global $INFO; 228 global $JSINFO; 229 global $ACT; 230 global $QUERY; 231 global $lang; 232 global $conf; 233 global $updateVersion; 234 /** @var Input $INPUT */ 235 global $INPUT; 236 237 // prepare the head array 238 $head = []; 239 240 // prepare seed for js and css 241 $tseed = $updateVersion; 242 $depends = getConfigFiles('main'); 243 $depends[] = DOKU_CONF . "tpl/" . $conf['template'] . "/style.ini"; 244 foreach ($depends as $f) $tseed .= @filemtime($f); 245 $tseed = md5($tseed); 246 247 // the usual stuff 248 $head['meta'][] = ['name' => 'generator', 'content' => 'DokuWiki']; 249 if (actionOK('search')) { 250 $head['link'][] = [ 251 'rel' => 'search', 252 'type' => 'application/opensearchdescription+xml', 253 'href' => DOKU_BASE . 'lib/exe/opensearch.php', 254 'title' => $conf['title'] 255 ]; 256 } 257 258 $head['link'][] = ['rel' => 'start', 'href' => DOKU_BASE]; 259 if (actionOK('index')) { 260 $head['link'][] = [ 261 'rel' => 'contents', 262 'href' => wl($ID, 'do=index', false, '&'), 263 'title' => $lang['btn_index'] 264 ]; 265 } 266 267 if (actionOK('manifest')) { 268 $head['link'][] = [ 269 'rel' => 'manifest', 270 'href' => DOKU_BASE . 'lib/exe/manifest.php' 271 ]; 272 } 273 274 $styleUtil = new StyleUtils(); 275 $styleIni = $styleUtil->cssStyleini(); 276 $replacements = $styleIni['replacements']; 277 if (!empty($replacements['__theme_color__'])) { 278 $head['meta'][] = [ 279 'name' => 'theme-color', 280 'content' => $replacements['__theme_color__'] 281 ]; 282 } 283 284 if ($alt) { 285 if (actionOK('rss')) { 286 $head['link'][] = [ 287 'rel' => 'alternate', 288 'type' => 'application/rss+xml', 289 'title' => $lang['btn_recent'], 290 'href' => DOKU_BASE . 'feed.php' 291 ]; 292 $head['link'][] = [ 293 'rel' => 'alternate', 294 'type' => 'application/rss+xml', 295 'title' => $lang['currentns'], 296 'href' => DOKU_BASE . 'feed.php?mode=list&ns=' . (isset($INFO) ? $INFO['namespace'] : '') 297 ]; 298 } 299 if (($ACT == 'show' || $ACT == 'search') && $INFO['writable']) { 300 $head['link'][] = [ 301 'rel' => 'edit', 302 'title' => $lang['btn_edit'], 303 'href' => wl($ID, 'do=edit', false, '&') 304 ]; 305 } 306 307 if (actionOK('rss') && $ACT == 'search') { 308 $head['link'][] = [ 309 'rel' => 'alternate', 310 'type' => 'application/rss+xml', 311 'title' => $lang['searchresult'], 312 'href' => DOKU_BASE . 'feed.php?mode=search&q=' . $QUERY 313 ]; 314 } 315 316 if (actionOK('export_xhtml')) { 317 $head['link'][] = [ 318 'rel' => 'alternate', 319 'type' => 'text/html', 320 'title' => $lang['plainhtml'], 321 'href' => exportlink($ID, 'xhtml', '', false, '&') 322 ]; 323 } 324 325 if (actionOK('export_raw')) { 326 $head['link'][] = [ 327 'rel' => 'alternate', 328 'type' => 'text/plain', 329 'title' => $lang['wikimarkup'], 330 'href' => exportlink($ID, 'raw', '', false, '&') 331 ]; 332 } 333 } 334 335 // setup robot tags appropriate for different modes 336 if (($ACT == 'show' || $ACT == 'export_xhtml') && !$REV) { 337 if ($INFO['exists']) { 338 //delay indexing: 339 if ((time() - $INFO['lastmod']) >= $conf['indexdelay'] && !isHiddenPage($ID)) { 340 $head['meta'][] = ['name' => 'robots', 'content' => 'index,follow']; 341 } else { 342 $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,nofollow']; 343 } 344 $canonicalUrl = wl($ID, '', true, '&'); 345 if ($ID == $conf['start']) { 346 $canonicalUrl = DOKU_URL; 347 } 348 $head['link'][] = ['rel' => 'canonical', 'href' => $canonicalUrl]; 349 } else { 350 $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,follow']; 351 } 352 } elseif (defined('DOKU_MEDIADETAIL')) { 353 $head['meta'][] = ['name' => 'robots', 'content' => 'index,follow']; 354 } else { 355 $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,nofollow']; 356 } 357 358 // set metadata 359 if ($ACT == 'show' || $ACT == 'export_xhtml') { 360 // keywords (explicit or implicit) 361 if (!empty($INFO['meta']['subject'])) { 362 $head['meta'][] = ['name' => 'keywords', 'content' => implode(',', $INFO['meta']['subject'])]; 363 } else { 364 $head['meta'][] = ['name' => 'keywords', 'content' => str_replace(':', ',', $ID)]; 365 } 366 } 367 368 // load stylesheets 369 $head['link'][] = [ 370 'rel' => 'stylesheet', 371 'href' => DOKU_BASE . 'lib/exe/css.php?t=' . rawurlencode($conf['template']) . '&tseed=' . $tseed 372 ]; 373 374 $script = "var NS='" . (isset($INFO) ? $INFO['namespace'] : '') . "';"; 375 if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) { 376 $script .= "var SIG=" . toolbar_signature() . ";"; 377 } 378 jsinfo(); 379 $script .= 'var JSINFO = ' . json_encode($JSINFO, JSON_THROW_ON_ERROR) . ';'; 380 $head['script'][] = ['_data' => $script]; 381 382 // load jquery 383 $jquery = getCdnUrls(); 384 foreach ($jquery as $src) { 385 $head['script'][] = [ 386 '_data' => '', 387 'src' => $src 388 ] + ($conf['defer_js'] ? ['defer' => 'defer'] : []); 389 } 390 391 // load our javascript dispatcher 392 $head['script'][] = [ 393 '_data' => '', 394 'src' => DOKU_BASE . 'lib/exe/js.php' . '?t=' . rawurlencode($conf['template']) . '&tseed=' . $tseed 395 ] + ($conf['defer_js'] ? ['defer' => 'defer'] : []); 396 397 // trigger event here 398 Event::createAndTrigger('TPL_METAHEADER_OUTPUT', $head, '_tpl_metaheaders_action', true); 399 return true; 400} 401 402/** 403 * prints the array build by tpl_metaheaders 404 * 405 * $data is an array of different header tags. Each tag can have multiple 406 * instances. Attributes are given as key value pairs. Values will be HTML 407 * encoded automatically so they should be provided as is in the $data array. 408 * 409 * For tags having a body attribute specify the body data in the special 410 * attribute '_data'. This field will NOT BE ESCAPED automatically. 411 * 412 * @param array $data 413 * 414 * @author Andreas Gohr <andi@splitbrain.org> 415 */ 416function _tpl_metaheaders_action($data) 417{ 418 foreach ($data as $tag => $inst) { 419 if ($tag == 'script') { 420 echo "<!--[if gte IE 9]><!-->\n"; // no scripts for old IE 421 } 422 foreach ($inst as $attr) { 423 if (empty($attr)) { 424 continue; 425 } 426 echo '<', $tag, ' ', buildAttributes($attr); 427 if (isset($attr['_data']) || $tag == 'script') { 428 if ($tag == 'script' && isset($attr['_data'])) 429 $attr['_data'] = "/*<![CDATA[*/" . 430 $attr['_data'] . 431 "\n/*!]]>*/"; 432 433 echo '>', $attr['_data'] ?? '', '</', $tag, '>'; 434 } else { 435 echo '/>'; 436 } 437 echo "\n"; 438 } 439 if ($tag == 'script') { 440 echo "<!--<![endif]-->\n"; 441 } 442 } 443} 444 445/** 446 * Print a link 447 * 448 * Just builds a link. 449 * 450 * @param string $url 451 * @param string $name 452 * @param string $more 453 * @param bool $return if true return the link html, otherwise print 454 * @return bool|string html of the link, or true if printed 455 * 456 * @author Andreas Gohr <andi@splitbrain.org> 457 */ 458function tpl_link($url, $name, $more = '', $return = false) 459{ 460 $out = '<a href="' . $url . '" '; 461 if ($more) $out .= ' ' . $more; 462 $out .= ">$name</a>"; 463 if ($return) return $out; 464 echo $out; 465 return true; 466} 467 468/** 469 * Prints a link to a WikiPage 470 * 471 * Wrapper around html_wikilink 472 * 473 * @param string $id page id 474 * @param string|null $name the name of the link 475 * @param bool $return 476 * @return true|string 477 * 478 * @author Andreas Gohr <andi@splitbrain.org> 479 */ 480function tpl_pagelink($id, $name = null, $return = false) 481{ 482 $out = '<bdi>' . html_wikilink($id, $name) . '</bdi>'; 483 if ($return) return $out; 484 echo $out; 485 return true; 486} 487 488/** 489 * get the parent page 490 * 491 * Tries to find out which page is parent. 492 * returns false if none is available 493 * 494 * @param string $id page id 495 * @return false|string 496 * 497 * @author Andreas Gohr <andi@splitbrain.org> 498 */ 499function tpl_getparent($id) 500{ 501 $resolver = new PageResolver('root'); 502 503 $parent = getNS($id) . ':'; 504 $parent = $resolver->resolveId($parent); 505 if ($parent == $id) { 506 $pos = strrpos(getNS($id), ':'); 507 $parent = substr($parent, 0, $pos) . ':'; 508 $parent = $resolver->resolveId($parent); 509 if ($parent == $id) return false; 510 } 511 return $parent; 512} 513 514/** 515 * Print one of the buttons 516 * 517 * @param string $type 518 * @param bool $return 519 * @return bool|string html, or false if no data, true if printed 520 * @see tpl_get_action 521 * 522 * @author Adrian Lang <mail@adrianlang.de> 523 * @deprecated 2017-09-01 see devel:menus 524 */ 525function tpl_button($type, $return = false) 526{ 527 dbg_deprecated('see devel:menus'); 528 $data = tpl_get_action($type); 529 if ($data === false) { 530 return false; 531 } elseif (!is_array($data)) { 532 $out = sprintf($data, 'button'); 533 } else { 534 /** 535 * @var string $accesskey 536 * @var string $id 537 * @var string $method 538 * @var array $params 539 */ 540 extract($data); 541 if ($id === '#dokuwiki__top') { 542 $out = html_topbtn(); 543 } else { 544 $out = html_btn($type, $id, $accesskey, $params, $method); 545 } 546 } 547 if ($return) return $out; 548 echo $out; 549 return true; 550} 551 552/** 553 * Like the action buttons but links 554 * 555 * @param string $type action command 556 * @param string $pre prefix of link 557 * @param string $suf suffix of link 558 * @param string $inner innerHML of link 559 * @param bool $return if true it returns html, otherwise prints 560 * @return bool|string html or false if no data, true if printed 561 * 562 * @see tpl_get_action 563 * @author Adrian Lang <mail@adrianlang.de> 564 * @deprecated 2017-09-01 see devel:menus 565 */ 566function tpl_actionlink($type, $pre = '', $suf = '', $inner = '', $return = false) 567{ 568 dbg_deprecated('see devel:menus'); 569 global $lang; 570 $data = tpl_get_action($type); 571 if ($data === false) { 572 return false; 573 } elseif (!is_array($data)) { 574 $out = sprintf($data, 'link'); 575 } else { 576 /** 577 * @var string $accesskey 578 * @var string $id 579 * @var string $method 580 * @var bool $nofollow 581 * @var array $params 582 * @var string $replacement 583 */ 584 extract($data); 585 if (strpos($id, '#') === 0) { 586 $linktarget = $id; 587 } else { 588 $linktarget = wl($id, $params); 589 } 590 $caption = $lang['btn_' . $type]; 591 if (strpos($caption, '%s')) { 592 $caption = sprintf($caption, $replacement); 593 } 594 $akey = ''; 595 $addTitle = ''; 596 if ($accesskey) { 597 $akey = 'accesskey="' . $accesskey . '" '; 598 $addTitle = ' [' . strtoupper($accesskey) . ']'; 599 } 600 $rel = $nofollow ? 'rel="nofollow" ' : ''; 601 $out = tpl_link( 602 $linktarget, 603 $pre . ($inner ?: $caption) . $suf, 604 'class="action ' . $type . '" ' . 605 $akey . $rel . 606 'title="' . hsc($caption) . $addTitle . '"', 607 true 608 ); 609 } 610 if ($return) return $out; 611 echo $out; 612 return true; 613} 614 615/** 616 * Check the actions and get data for buttons and links 617 * 618 * @param string $type 619 * @return array|bool|string 620 * 621 * @author Adrian Lang <mail@adrianlang.de> 622 * @author Andreas Gohr <andi@splitbrain.org> 623 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 624 * @deprecated 2017-09-01 see devel:menus 625 */ 626function tpl_get_action($type) 627{ 628 dbg_deprecated('see devel:menus'); 629 if ($type == 'history') $type = 'revisions'; 630 if ($type == 'subscription') $type = 'subscribe'; 631 if ($type == 'img_backto') $type = 'imgBackto'; 632 633 $class = '\\dokuwiki\\Menu\\Item\\' . ucfirst($type); 634 if (class_exists($class)) { 635 try { 636 /** @var AbstractItem $item */ 637 $item = new $class(); 638 $data = $item->getLegacyData(); 639 $unknown = false; 640 } catch (RuntimeException $ignored) { 641 return false; 642 } 643 } else { 644 global $ID; 645 $data = [ 646 'accesskey' => null, 647 'type' => $type, 648 'id' => $ID, 649 'method' => 'get', 650 'params' => ['do' => $type], 651 'nofollow' => true, 652 'replacement' => '' 653 ]; 654 $unknown = true; 655 } 656 657 $evt = new Event('TPL_ACTION_GET', $data); 658 if ($evt->advise_before()) { 659 //handle unknown types 660 if ($unknown) { 661 $data = '[unknown %s type]'; 662 } 663 } 664 $evt->advise_after(); 665 unset($evt); 666 667 return $data; 668} 669 670/** 671 * Wrapper around tpl_button() and tpl_actionlink() 672 * 673 * @param string $type action command 674 * @param bool $link link or form button? 675 * @param string|bool $wrapper HTML element wrapper 676 * @param bool $return return or print 677 * @param string $pre prefix for links 678 * @param string $suf suffix for links 679 * @param string $inner inner HTML for links 680 * @return bool|string 681 * 682 * @author Anika Henke <anika@selfthinker.org> 683 * @deprecated 2017-09-01 see devel:menus 684 */ 685function tpl_action($type, $link = false, $wrapper = false, $return = false, $pre = '', $suf = '', $inner = '') 686{ 687 dbg_deprecated('see devel:menus'); 688 $out = ''; 689 if ($link) { 690 $out .= tpl_actionlink($type, $pre, $suf, $inner, true); 691 } else { 692 $out .= tpl_button($type, true); 693 } 694 if ($out && $wrapper) $out = "<$wrapper>$out</$wrapper>"; 695 696 if ($return) return $out; 697 echo $out; 698 return (bool)$out; 699} 700 701/** 702 * Print the search form 703 * 704 * If the first parameter is given a div with the ID 'qsearch_out' will 705 * be added which instructs the ajax pagequicksearch to kick in and place 706 * its output into this div. The second parameter controls the propritary 707 * attribute autocomplete. If set to false this attribute will be set with an 708 * value of "off" to instruct the browser to disable it's own built in 709 * autocompletion feature (MSIE and Firefox) 710 * 711 * @param bool $ajax 712 * @param bool $autocomplete 713 * @return bool 714 * 715 * @author Andreas Gohr <andi@splitbrain.org> 716 */ 717function tpl_searchform($ajax = true, $autocomplete = true) 718{ 719 global $lang; 720 global $ACT; 721 global $QUERY; 722 global $ID; 723 724 // don't print the search form if search action has been disabled 725 if (!actionOK('search')) return false; 726 727 $searchForm = new Form([ 728 'action' => wl(), 729 'method' => 'get', 730 'role' => 'search', 731 'class' => 'search', 732 'id' => 'dw__search', 733 ], true); 734 $searchForm->addTagOpen('div')->addClass('no'); 735 $searchForm->setHiddenField('do', 'search'); 736 $searchForm->setHiddenField('id', $ID); 737 $searchForm->addTextInput('q') 738 ->addClass('edit') 739 ->attrs([ 740 'title' => '[F]', 741 'accesskey' => 'f', 742 'placeholder' => $lang['btn_search'], 743 'autocomplete' => $autocomplete ? 'on' : 'off', 744 ]) 745 ->id('qsearch__in') 746 ->val($ACT === 'search' ? $QUERY : '') 747 ->useInput(false); 748 $searchForm->addButton('', $lang['btn_search'])->attrs([ 749 'type' => 'submit', 750 'title' => $lang['btn_search'], 751 ]); 752 if ($ajax) { 753 $searchForm->addTagOpen('div')->id('qsearch__out')->addClass('ajax_qsearch JSpopup'); 754 $searchForm->addTagClose('div'); 755 } 756 $searchForm->addTagClose('div'); 757 758 echo $searchForm->toHTML('QuickSearch'); 759 760 return true; 761} 762 763/** 764 * Print the breadcrumbs trace 765 * 766 * @param string $sep Separator between entries 767 * @param bool $return return or print 768 * @return bool|string 769 * 770 * @author Andreas Gohr <andi@splitbrain.org> 771 */ 772function tpl_breadcrumbs($sep = null, $return = false) 773{ 774 global $lang; 775 global $conf; 776 777 //check if enabled 778 if (!$conf['breadcrumbs']) return false; 779 780 //set default 781 if (is_null($sep)) $sep = '•'; 782 783 $out = ''; 784 785 $crumbs = breadcrumbs(); //setup crumb trace 786 787 $crumbs_sep = ' <span class="bcsep">' . $sep . '</span> '; 788 789 //render crumbs, highlight the last one 790 $out .= '<span class="bchead">' . $lang['breadcrumb'] . '</span>'; 791 $last = count($crumbs); 792 $i = 0; 793 foreach ($crumbs as $id => $name) { 794 $i++; 795 $out .= $crumbs_sep; 796 if ($i == $last) $out .= '<span class="curid">'; 797 $out .= '<bdi>' . tpl_link(wl($id), hsc($name), 'class="breadcrumbs" title="' . $id . '"', true) . '</bdi>'; 798 if ($i == $last) $out .= '</span>'; 799 } 800 if ($return) return $out; 801 echo $out; 802 return (bool)$out; 803} 804 805/** 806 * Hierarchical breadcrumbs 807 * 808 * This code was suggested as replacement for the usual breadcrumbs. 809 * It only makes sense with a deep site structure. 810 * 811 * @param string $sep Separator between entries 812 * @param bool $return return or print 813 * @return bool|string 814 * 815 * @todo May behave strangely in RTL languages 816 * @author <fredrik@averpil.com> 817 * @author Andreas Gohr <andi@splitbrain.org> 818 * @author Nigel McNie <oracle.shinoda@gmail.com> 819 * @author Sean Coates <sean@caedmon.net> 820 */ 821function tpl_youarehere($sep = null, $return = false) 822{ 823 global $conf; 824 global $ID; 825 global $lang; 826 827 // check if enabled 828 if (!$conf['youarehere']) return false; 829 830 //set default 831 if (is_null($sep)) $sep = ' » '; 832 833 $out = ''; 834 835 $parts = explode(':', $ID); 836 $count = count($parts); 837 838 $out .= '<span class="bchead">' . $lang['youarehere'] . ' </span>'; 839 840 // always print the startpage 841 $out .= '<span class="home">' . tpl_pagelink(':' . $conf['start'], null, true) . '</span>'; 842 843 // print intermediate namespace links 844 $part = ''; 845 for ($i = 0; $i < $count - 1; $i++) { 846 $part .= $parts[$i] . ':'; 847 $page = $part; 848 if ($page == $conf['start']) continue; // Skip startpage 849 850 // output 851 $out .= $sep . tpl_pagelink($page, null, true); 852 } 853 854 // print current page, skipping start page, skipping for namespace index 855 if (isset($page)) { 856 $page = (new PageResolver('root'))->resolveId($page); 857 if ($page == $part . $parts[$i]) { 858 if ($return) return $out; 859 echo $out; 860 return true; 861 } 862 } 863 $page = $part . $parts[$i]; 864 if ($page == $conf['start']) { 865 if ($return) return $out; 866 echo $out; 867 return true; 868 } 869 $out .= $sep; 870 $out .= tpl_pagelink($page, null, true); 871 if ($return) return $out; 872 echo $out; 873 return (bool)$out; 874} 875 876/** 877 * Print info if the user is logged in 878 * and show full name in that case 879 * 880 * Could be enhanced with a profile link in future? 881 * 882 * @return bool 883 * 884 * @author Andreas Gohr <andi@splitbrain.org> 885 */ 886function tpl_userinfo() 887{ 888 global $lang; 889 /** @var Input $INPUT */ 890 global $INPUT; 891 892 if ($INPUT->server->str('REMOTE_USER')) { 893 echo $lang['loggedinas'] . ' ' . userlink(); 894 return true; 895 } 896 return false; 897} 898 899/** 900 * Print some info about the current page 901 * 902 * @param bool $ret return content instead of printing it 903 * @return bool|string 904 * 905 * @author Andreas Gohr <andi@splitbrain.org> 906 */ 907function tpl_pageinfo($ret = false) 908{ 909 global $conf; 910 global $lang; 911 global $INFO; 912 global $ID; 913 914 // return if we are not allowed to view the page 915 if (!auth_quickaclcheck($ID)) { 916 return false; 917 } 918 919 // prepare date and path 920 $fn = $INFO['filepath']; 921 if (!$conf['fullpath']) { 922 if ($INFO['rev']) { 923 $fn = str_replace($conf['olddir'] . '/', '', $fn); 924 } else { 925 $fn = str_replace($conf['datadir'] . '/', '', $fn); 926 } 927 } 928 $fn = utf8_decodeFN($fn); 929 $date = dformat($INFO['lastmod']); 930 931 // print it 932 if ($INFO['exists']) { 933 $out = '<bdi>' . $fn . '</bdi>'; 934 $out .= ' · '; 935 $out .= $lang['lastmod']; 936 $out .= ' '; 937 $out .= $date; 938 if ($INFO['editor']) { 939 $out .= ' ' . $lang['by'] . ' '; 940 $out .= '<bdi>' . editorinfo($INFO['editor']) . '</bdi>'; 941 } else { 942 $out .= ' (' . $lang['external_edit'] . ')'; 943 } 944 if ($INFO['locked']) { 945 $out .= ' · '; 946 $out .= $lang['lockedby']; 947 $out .= ' '; 948 $out .= '<bdi>' . editorinfo($INFO['locked']) . '</bdi>'; 949 } 950 if ($ret) { 951 return $out; 952 } else { 953 echo $out; 954 return true; 955 } 956 } 957 return false; 958} 959 960/** 961 * Prints or returns the name of the given page (current one if none given). 962 * 963 * If useheading is enabled this will use the first headline else 964 * the given ID is used. 965 * 966 * @param string $id page id 967 * @param bool $ret return content instead of printing 968 * @return bool|string 969 * 970 * @author Andreas Gohr <andi@splitbrain.org> 971 */ 972function tpl_pagetitle($id = null, $ret = false) 973{ 974 global $ACT, $conf, $lang; 975 976 if (is_null($id)) { 977 global $ID; 978 $id = $ID; 979 } 980 981 $name = $id; 982 if (useHeading('navigation')) { 983 $first_heading = p_get_first_heading($id); 984 if ($first_heading) $name = $first_heading; 985 } 986 987 // default page title is the page name, modify with the current action 988 switch ($ACT) { 989 // admin functions 990 case 'admin': 991 $page_title = $lang['btn_admin']; 992 // try to get the plugin name 993 /** @var AdminPlugin $plugin */ 994 if ($plugin = plugin_getRequestAdminPlugin()) { 995 $plugin_title = $plugin->getMenuText($conf['lang']); 996 $page_title = $plugin_title ?: $plugin->getPluginName(); 997 } 998 break; 999 1000 // user functions 1001 case 'login': 1002 case 'profile': 1003 case 'register': 1004 case 'resendpwd': 1005 $page_title = $lang['btn_' . $ACT]; 1006 break; 1007 1008 // wiki functions 1009 case 'search': 1010 case 'index': 1011 $page_title = $lang['btn_' . $ACT]; 1012 break; 1013 1014 // page functions 1015 case 'edit': 1016 case 'preview': 1017 $page_title = "✎ " . $name; 1018 break; 1019 1020 case 'revisions': 1021 $page_title = $name . ' - ' . $lang['btn_revs']; 1022 break; 1023 1024 case 'backlink': 1025 case 'recent': 1026 case 'subscribe': 1027 $page_title = $name . ' - ' . $lang['btn_' . $ACT]; 1028 break; 1029 1030 default: // SHOW and anything else not included 1031 $page_title = $name; 1032 } 1033 1034 if ($ret) { 1035 return hsc($page_title); 1036 } else { 1037 echo hsc($page_title); 1038 return true; 1039 } 1040} 1041 1042/** 1043 * Returns the requested EXIF/IPTC tag from the current image 1044 * 1045 * If $tags is an array all given tags are tried until a 1046 * value is found. If no value is found $alt is returned. 1047 * 1048 * Which texts are known is defined in the functions _exifTagNames 1049 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC 1050 * to the names of the latter one) 1051 * 1052 * Only allowed in: detail.php 1053 * 1054 * @param array|string $tags tag or array of tags to try 1055 * @param string $alt alternative output if no data was found 1056 * @param null|string $src the image src, uses global $SRC if not given 1057 * @return string 1058 * 1059 * @author Andreas Gohr <andi@splitbrain.org> 1060 */ 1061function tpl_img_getTag($tags, $alt = '', $src = null) 1062{ 1063 // Init Exif Reader 1064 global $SRC, $imgMeta; 1065 1066 if (is_null($src)) $src = $SRC; 1067 if (is_null($src)) return $alt; 1068 1069 if (!isset($imgMeta)) { 1070 $imgMeta = new JpegMeta($src); 1071 } 1072 if ($imgMeta === false) return $alt; 1073 $info = cleanText($imgMeta->getField($tags)); 1074 if (!$info) return $alt; 1075 return $info; 1076} 1077 1078 1079/** 1080 * Garbage collects up the open JpegMeta object. 1081 */ 1082function tpl_img_close() 1083{ 1084 global $imgMeta; 1085 $imgMeta = null; 1086} 1087 1088/** 1089 * Prints a html description list of the metatags of the current image 1090 */ 1091function tpl_img_meta() 1092{ 1093 global $lang; 1094 1095 $tags = tpl_get_img_meta(); 1096 1097 echo '<dl>'; 1098 foreach ($tags as $tag) { 1099 $label = $lang[$tag['langkey']]; 1100 if (!$label) $label = $tag['langkey'] . ':'; 1101 1102 echo '<dt>' . $label . '</dt><dd>'; 1103 if ($tag['type'] == 'date') { 1104 echo dformat($tag['value']); 1105 } else { 1106 echo hsc($tag['value']); 1107 } 1108 echo '</dd>'; 1109 } 1110 echo '</dl>'; 1111} 1112 1113/** 1114 * Returns metadata as configured in mediameta config file, ready for creating html 1115 * 1116 * @return array with arrays containing the entries: 1117 * - string langkey key to lookup in the $lang var, if not found printed as is 1118 * - string type type of value 1119 * - string value tag value (unescaped) 1120 */ 1121function tpl_get_img_meta() 1122{ 1123 1124 $config_files = getConfigFiles('mediameta'); 1125 foreach ($config_files as $config_file) { 1126 if (file_exists($config_file)) { 1127 include($config_file); 1128 } 1129 } 1130 $tags = []; 1131 foreach ($fields as $tag) { 1132 $t = []; 1133 if (!empty($tag[0])) { 1134 $t = [$tag[0]]; 1135 } 1136 if (isset($tag[3]) && is_array($tag[3])) { 1137 $t = array_merge($t, $tag[3]); 1138 } 1139 $value = tpl_img_getTag($t); 1140 if ($value) { 1141 $tags[] = ['langkey' => $tag[1], 'type' => $tag[2], 'value' => $value]; 1142 } 1143 } 1144 return $tags; 1145} 1146 1147/** 1148 * Prints the image with a link to the full sized version 1149 * 1150 * Only allowed in: detail.php 1151 * 1152 * @triggers TPL_IMG_DISPLAY 1153 * @param int $maxwidth - maximal width of the image 1154 * @param int $maxheight - maximal height of the image 1155 * @param bool $link - link to the orginal size? 1156 * @param array $params - additional image attributes 1157 * @return bool Result of TPL_IMG_DISPLAY 1158 */ 1159function tpl_img($maxwidth = 0, $maxheight = 0, $link = true, $params = null) 1160{ 1161 global $IMG; 1162 /** @var Input $INPUT */ 1163 global $INPUT; 1164 global $REV; 1165 $w = (int)tpl_img_getTag('File.Width'); 1166 $h = (int)tpl_img_getTag('File.Height'); 1167 1168 //resize to given max values 1169 $ratio = 1; 1170 if ($w >= $h) { 1171 if ($maxwidth && $w >= $maxwidth) { 1172 $ratio = $maxwidth / $w; 1173 } elseif ($maxheight && $h > $maxheight) { 1174 $ratio = $maxheight / $h; 1175 } 1176 } elseif ($maxheight && $h >= $maxheight) { 1177 $ratio = $maxheight / $h; 1178 } elseif ($maxwidth && $w > $maxwidth) { 1179 $ratio = $maxwidth / $w; 1180 } 1181 if ($ratio) { 1182 $w = floor($ratio * $w); 1183 $h = floor($ratio * $h); 1184 } 1185 1186 //prepare URLs 1187 $url = ml($IMG, ['cache' => $INPUT->str('cache'), 'rev' => $REV], true, '&'); 1188 $src = ml($IMG, ['cache' => $INPUT->str('cache'), 'rev' => $REV, 'w' => $w, 'h' => $h], true, '&'); 1189 1190 //prepare attributes 1191 $alt = tpl_img_getTag('Simple.Title'); 1192 if (is_null($params)) { 1193 $p = []; 1194 } else { 1195 $p = $params; 1196 } 1197 if ($w) $p['width'] = $w; 1198 if ($h) $p['height'] = $h; 1199 $p['class'] = 'img_detail'; 1200 if ($alt) { 1201 $p['alt'] = $alt; 1202 $p['title'] = $alt; 1203 } else { 1204 $p['alt'] = ''; 1205 } 1206 $p['src'] = $src; 1207 1208 $data = ['url' => ($link ? $url : null), 'params' => $p]; 1209 return Event::createAndTrigger('TPL_IMG_DISPLAY', $data, '_tpl_img_action', true); 1210} 1211 1212/** 1213 * Default action for TPL_IMG_DISPLAY 1214 * 1215 * @param array $data 1216 * @return bool 1217 */ 1218function _tpl_img_action($data) 1219{ 1220 global $lang; 1221 $p = buildAttributes($data['params']); 1222 1223 if ($data['url']) echo '<a href="' . hsc($data['url']) . '" title="' . $lang['mediaview'] . '">'; 1224 echo '<img ' . $p . '/>'; 1225 if ($data['url']) echo '</a>'; 1226 return true; 1227} 1228 1229/** 1230 * This function inserts a small gif which in reality is the indexer function. 1231 * 1232 * Should be called somewhere at the very end of the main.php template 1233 * 1234 * @return bool 1235 */ 1236function tpl_indexerWebBug() 1237{ 1238 global $ID; 1239 1240 $p = []; 1241 $p['src'] = DOKU_BASE . 'lib/exe/taskrunner.php?id=' . rawurlencode($ID) . 1242 '&' . time(); 1243 $p['width'] = 2; //no more 1x1 px image because we live in times of ad blockers... 1244 $p['height'] = 1; 1245 $p['alt'] = ''; 1246 $att = buildAttributes($p); 1247 echo "<img $att />"; 1248 return true; 1249} 1250 1251/** 1252 * tpl_getConf($id) 1253 * 1254 * use this function to access template configuration variables 1255 * 1256 * @param string $id name of the value to access 1257 * @param mixed $notset what to return if the setting is not available 1258 * @return mixed 1259 */ 1260function tpl_getConf($id, $notset = false) 1261{ 1262 global $conf; 1263 static $tpl_configloaded = false; 1264 1265 $tpl = $conf['template']; 1266 1267 if (!$tpl_configloaded) { 1268 $tconf = tpl_loadConfig(); 1269 if ($tconf !== false) { 1270 foreach ($tconf as $key => $value) { 1271 if (isset($conf['tpl'][$tpl][$key])) continue; 1272 $conf['tpl'][$tpl][$key] = $value; 1273 } 1274 $tpl_configloaded = true; 1275 } 1276 } 1277 1278 return $conf['tpl'][$tpl][$id] ?? $notset; 1279} 1280 1281/** 1282 * tpl_loadConfig() 1283 * 1284 * reads all template configuration variables 1285 * this function is automatically called by tpl_getConf() 1286 * 1287 * @return false|array 1288 */ 1289function tpl_loadConfig() 1290{ 1291 1292 $file = tpl_incdir() . '/conf/default.php'; 1293 $conf = []; 1294 1295 if (!file_exists($file)) return false; 1296 1297 // load default config file 1298 include($file); 1299 1300 return $conf; 1301} 1302 1303// language methods 1304 1305/** 1306 * tpl_getLang($id) 1307 * 1308 * use this function to access template language variables 1309 * 1310 * @param string $id key of language string 1311 * @return string 1312 */ 1313function tpl_getLang($id) 1314{ 1315 static $lang = []; 1316 1317 if (count($lang) === 0) { 1318 global $conf, $config_cascade; // definitely don't invoke "global $lang" 1319 1320 $path = tpl_incdir() . 'lang/'; 1321 1322 $lang = []; 1323 1324 // don't include once 1325 @include($path . 'en/lang.php'); 1326 foreach ($config_cascade['lang']['template'] as $config_file) { 1327 if (file_exists($config_file . $conf['template'] . '/en/lang.php')) { 1328 include($config_file . $conf['template'] . '/en/lang.php'); 1329 } 1330 } 1331 1332 if ($conf['lang'] != 'en') { 1333 @include($path . $conf['lang'] . '/lang.php'); 1334 foreach ($config_cascade['lang']['template'] as $config_file) { 1335 if (file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) { 1336 include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php'); 1337 } 1338 } 1339 } 1340 } 1341 return $lang[$id] ?? ''; 1342} 1343 1344/** 1345 * Retrieve a language dependent file and pass to xhtml renderer for display 1346 * template equivalent of p_locale_xhtml() 1347 * 1348 * @param string $id id of language dependent wiki page 1349 * @return string parsed contents of the wiki page in xhtml format 1350 */ 1351function tpl_locale_xhtml($id) 1352{ 1353 return p_cached_output(tpl_localeFN($id)); 1354} 1355 1356/** 1357 * Prepends appropriate path for a language dependent filename 1358 * 1359 * @param string $id id of localized text 1360 * @return string wiki text 1361 */ 1362function tpl_localeFN($id) 1363{ 1364 $path = tpl_incdir() . 'lang/'; 1365 global $conf; 1366 $file = DOKU_CONF . 'template_lang/' . $conf['template'] . '/' . $conf['lang'] . '/' . $id . '.txt'; 1367 if (!file_exists($file)) { 1368 $file = $path . $conf['lang'] . '/' . $id . '.txt'; 1369 if (!file_exists($file)) { 1370 //fall back to english 1371 $file = $path . 'en/' . $id . '.txt'; 1372 } 1373 } 1374 return $file; 1375} 1376 1377/** 1378 * prints the "main content" in the mediamanager popup 1379 * 1380 * Depending on the user's actions this may be a list of 1381 * files in a namespace, the meta editing dialog or 1382 * a message of referencing pages 1383 * 1384 * Only allowed in mediamanager.php 1385 * 1386 * @triggers MEDIAMANAGER_CONTENT_OUTPUT 1387 * @param bool $fromajax - set true when calling this function via ajax 1388 * @param string $sort 1389 * 1390 * @author Andreas Gohr <andi@splitbrain.org> 1391 */ 1392function tpl_mediaContent($fromajax = false, $sort = 'natural') 1393{ 1394 global $IMG; 1395 global $AUTH; 1396 global $INUSE; 1397 global $NS; 1398 global $JUMPTO; 1399 /** @var Input $INPUT */ 1400 global $INPUT; 1401 1402 $do = $INPUT->extract('do')->str('do'); 1403 if (in_array($do, ['save', 'cancel'])) $do = ''; 1404 1405 if (!$do) { 1406 if ($INPUT->bool('edit')) { 1407 $do = 'metaform'; 1408 } elseif (is_array($INUSE)) { 1409 $do = 'filesinuse'; 1410 } else { 1411 $do = 'filelist'; 1412 } 1413 } 1414 1415 // output the content pane, wrapped in an event. 1416 if (!$fromajax) echo '<div id="media__content">'; 1417 $data = ['do' => $do]; 1418 $evt = new Event('MEDIAMANAGER_CONTENT_OUTPUT', $data); 1419 if ($evt->advise_before()) { 1420 $do = $data['do']; 1421 if ($do == 'filesinuse') { 1422 media_filesinuse($INUSE, $IMG); 1423 } elseif ($do == 'filelist') { 1424 media_filelist($NS, $AUTH, $JUMPTO, false, $sort); 1425 } elseif ($do == 'searchlist') { 1426 media_searchlist($INPUT->str('q'), $NS, $AUTH); 1427 } else { 1428 msg('Unknown action ' . hsc($do), -1); 1429 } 1430 } 1431 $evt->advise_after(); 1432 unset($evt); 1433 if (!$fromajax) echo '</div>'; 1434} 1435 1436/** 1437 * Prints the central column in full-screen media manager 1438 * Depending on the opened tab this may be a list of 1439 * files in a namespace, upload form or search form 1440 * 1441 * @author Kate Arzamastseva <pshns@ukr.net> 1442 */ 1443function tpl_mediaFileList() 1444{ 1445 global $AUTH; 1446 global $NS; 1447 global $JUMPTO; 1448 global $lang; 1449 /** @var Input $INPUT */ 1450 global $INPUT; 1451 1452 $opened_tab = $INPUT->str('tab_files'); 1453 if (!$opened_tab || !in_array($opened_tab, ['files', 'upload', 'search'])) $opened_tab = 'files'; 1454 if ($INPUT->str('mediado') == 'update') $opened_tab = 'upload'; 1455 1456 echo '<h2 class="a11y">' . $lang['mediaselect'] . '</h2>' . NL; 1457 1458 media_tabs_files($opened_tab); 1459 1460 echo '<div class="panelHeader">' . NL; 1461 echo '<h3>'; 1462 $tabTitle = $NS ?: '[' . $lang['mediaroot'] . ']'; 1463 printf($lang['media_' . $opened_tab], '<strong>' . hsc($tabTitle) . '</strong>'); 1464 echo '</h3>' . NL; 1465 if ($opened_tab === 'search' || $opened_tab === 'files') { 1466 media_tab_files_options(); 1467 } 1468 echo '</div>' . NL; 1469 1470 echo '<div class="panelContent">' . NL; 1471 if ($opened_tab == 'files') { 1472 media_tab_files($NS, $AUTH, $JUMPTO); 1473 } elseif ($opened_tab == 'upload') { 1474 media_tab_upload($NS, $AUTH, $JUMPTO); 1475 } elseif ($opened_tab == 'search') { 1476 media_tab_search($NS, $AUTH); 1477 } 1478 echo '</div>' . NL; 1479} 1480 1481/** 1482 * Prints the third column in full-screen media manager 1483 * Depending on the opened tab this may be details of the 1484 * selected file, the meta editing dialog or 1485 * list of file revisions 1486 * 1487 * @param string $image 1488 * @param boolean $rev 1489 * 1490 * @author Kate Arzamastseva <pshns@ukr.net> 1491 */ 1492function tpl_mediaFileDetails($image, $rev) 1493{ 1494 global $conf, $DEL, $lang; 1495 /** @var Input $INPUT */ 1496 global $INPUT; 1497 1498 $removed = ( 1499 !file_exists(mediaFN($image)) && 1500 file_exists(mediaMetaFN($image, '.changes')) && 1501 $conf['mediarevisions'] 1502 ); 1503 if (!$image || (!file_exists(mediaFN($image)) && !$removed) || $DEL) return; 1504 if ($rev && !file_exists(mediaFN($image, $rev))) $rev = false; 1505 $ns = getNS($image); 1506 $do = $INPUT->str('mediado'); 1507 1508 $opened_tab = $INPUT->str('tab_details'); 1509 1510 $tab_array = ['view']; 1511 [, $mime] = mimetype($image); 1512 if ($mime == 'image/jpeg') { 1513 $tab_array[] = 'edit'; 1514 } 1515 if ($conf['mediarevisions']) { 1516 $tab_array[] = 'history'; 1517 } 1518 1519 if (!$opened_tab || !in_array($opened_tab, $tab_array)) $opened_tab = 'view'; 1520 if ($INPUT->bool('edit')) $opened_tab = 'edit'; 1521 if ($do == 'restore') $opened_tab = 'view'; 1522 1523 media_tabs_details($image, $opened_tab); 1524 1525 echo '<div class="panelHeader"><h3>'; 1526 [$ext] = mimetype($image, false); 1527 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext); 1528 $class = 'select mediafile mf_' . $class; 1529 1530 $attributes = $rev ? ['rev' => $rev] : []; 1531 $tabTitle = sprintf( 1532 '<strong><a href="%s" class="%s" title="%s">%s</a></strong>', 1533 ml($image, $attributes), 1534 $class, 1535 $lang['mediaview'], 1536 $image 1537 ); 1538 if ($opened_tab === 'view' && $rev) { 1539 printf($lang['media_viewold'], $tabTitle, dformat($rev)); 1540 } else { 1541 printf($lang['media_' . $opened_tab], $tabTitle); 1542 } 1543 1544 echo '</h3></div>' . NL; 1545 1546 echo '<div class="panelContent">' . NL; 1547 1548 if ($opened_tab == 'view') { 1549 media_tab_view($image, $ns, null, $rev); 1550 } elseif ($opened_tab == 'edit' && !$removed) { 1551 media_tab_edit($image, $ns); 1552 } elseif ($opened_tab == 'history' && $conf['mediarevisions']) { 1553 media_tab_history($image, $ns); 1554 } 1555 1556 echo '</div>' . NL; 1557} 1558 1559/** 1560 * prints the namespace tree in the mediamanager popup 1561 * 1562 * Only allowed in mediamanager.php 1563 * 1564 * @author Andreas Gohr <andi@splitbrain.org> 1565 */ 1566function tpl_mediaTree() 1567{ 1568 global $NS; 1569 echo '<div id="media__tree">'; 1570 media_nstree($NS); 1571 echo '</div>'; 1572} 1573 1574/** 1575 * Print a dropdown menu with all DokuWiki actions 1576 * 1577 * Note: this will not use any pretty URLs 1578 * 1579 * @param string $empty empty option label 1580 * @param string $button submit button label 1581 * 1582 * @author Andreas Gohr <andi@splitbrain.org> 1583 * @deprecated 2017-09-01 see devel:menus 1584 */ 1585function tpl_actiondropdown($empty = '', $button = '>') 1586{ 1587 dbg_deprecated('see devel:menus'); 1588 $menu = new MobileMenu(); 1589 echo $menu->getDropdown($empty, $button); 1590} 1591 1592/** 1593 * Print a informational line about the used license 1594 * 1595 * @param string $img print image? (|button|badge) 1596 * @param bool $imgonly skip the textual description? 1597 * @param bool $return when true don't print, but return HTML 1598 * @param bool $wrap wrap in div with class="license"? 1599 * @return string 1600 * 1601 * @author Andreas Gohr <andi@splitbrain.org> 1602 */ 1603function tpl_license($img = 'badge', $imgonly = false, $return = false, $wrap = true) 1604{ 1605 global $license; 1606 global $conf; 1607 global $lang; 1608 if (!$conf['license']) return ''; 1609 if (!is_array($license[$conf['license']])) return ''; 1610 $lic = $license[$conf['license']]; 1611 $target = ($conf['target']['extern']) ? ' target="' . $conf['target']['extern'] . '"' : ''; 1612 1613 $out = ''; 1614 if ($wrap) $out .= '<div class="license">'; 1615 if ($img) { 1616 $src = license_img($img); 1617 if ($src) { 1618 $out .= '<a href="' . $lic['url'] . '" rel="license"' . $target; 1619 $out .= '><img src="' . DOKU_BASE . $src . '" alt="' . $lic['name'] . '" /></a>'; 1620 if (!$imgonly) $out .= ' '; 1621 } 1622 } 1623 if (!$imgonly) { 1624 $out .= $lang['license'] . ' '; 1625 $out .= '<bdi><a href="' . $lic['url'] . '" rel="license" class="urlextern"' . $target; 1626 $out .= '>' . $lic['name'] . '</a></bdi>'; 1627 } 1628 if ($wrap) $out .= '</div>'; 1629 1630 if ($return) return $out; 1631 echo $out; 1632 return ''; 1633} 1634 1635/** 1636 * Includes the rendered HTML of a given page 1637 * 1638 * This function is useful to populate sidebars or similar features in a 1639 * template 1640 * 1641 * @param string $pageid The page name you want to include 1642 * @param bool $print Should the content be printed or returned only 1643 * @param bool $propagate Search higher namespaces, too? 1644 * @param bool $useacl Include the page only if the ACLs check out? 1645 * @return bool|null|string 1646 */ 1647function tpl_include_page($pageid, $print = true, $propagate = false, $useacl = true) 1648{ 1649 if ($propagate) { 1650 $pageid = page_findnearest($pageid, $useacl); 1651 } elseif ($useacl && auth_quickaclcheck($pageid) == AUTH_NONE) { 1652 return false; 1653 } 1654 if (!$pageid) return false; 1655 1656 global $TOC; 1657 $oldtoc = $TOC; 1658 $html = p_wiki_xhtml($pageid, '', false); 1659 $TOC = $oldtoc; 1660 1661 if ($print) echo $html; 1662 return $html; 1663} 1664 1665/** 1666 * Display the subscribe form 1667 * 1668 * @author Adrian Lang <lang@cosmocode.de> 1669 * @deprecated 2020-07-23 1670 */ 1671function tpl_subscribe() 1672{ 1673 dbg_deprecated(Subscribe::class . '::show()'); 1674 (new Subscribe())->show(); 1675} 1676 1677/** 1678 * Tries to send already created content right to the browser 1679 * 1680 * Wraps around ob_flush() and flush() 1681 * 1682 * @author Andreas Gohr <andi@splitbrain.org> 1683 */ 1684function tpl_flush() 1685{ 1686 if (ob_get_level() > 0) ob_flush(); 1687 flush(); 1688} 1689 1690/** 1691 * Tries to find a ressource file in the given locations. 1692 * 1693 * If a given location starts with a colon it is assumed to be a media 1694 * file, otherwise it is assumed to be relative to the current template 1695 * 1696 * @param string[] $search locations to look at 1697 * @param bool $abs if to use absolute URL 1698 * @param array &$imginfo filled with getimagesize() 1699 * @param bool $fallback use fallback image if target isn't found or return 'false' if potential 1700 * false result is required 1701 * @return string 1702 * 1703 * @author Andreas Gohr <andi@splitbrain.org> 1704 */ 1705function tpl_getMediaFile($search, $abs = false, &$imginfo = null, $fallback = true) 1706{ 1707 $img = ''; 1708 $file = ''; 1709 $ismedia = false; 1710 // loop through candidates until a match was found: 1711 foreach ($search as $img) { 1712 if (substr($img, 0, 1) == ':') { 1713 $file = mediaFN($img); 1714 $ismedia = true; 1715 } else { 1716 $file = tpl_incdir() . $img; 1717 $ismedia = false; 1718 } 1719 1720 if (file_exists($file)) break; 1721 } 1722 1723 // manage non existing target 1724 if (!file_exists($file)) { 1725 // give result for fallback image 1726 if ($fallback) { 1727 $file = DOKU_INC . 'lib/images/blank.gif'; 1728 // stop process if false result is required (if $fallback is false) 1729 } else { 1730 return false; 1731 } 1732 } 1733 1734 // fetch image data if requested 1735 if (!is_null($imginfo)) { 1736 $imginfo = getimagesize($file); 1737 } 1738 1739 // build URL 1740 if ($ismedia) { 1741 $url = ml($img, '', true, '', $abs); 1742 } else { 1743 $url = tpl_basedir() . $img; 1744 if ($abs) $url = DOKU_URL . substr($url, strlen(DOKU_REL)); 1745 } 1746 1747 return $url; 1748} 1749 1750/** 1751 * PHP include a file 1752 * 1753 * either from the conf directory if it exists, otherwise use 1754 * file in the template's root directory. 1755 * 1756 * The function honours config cascade settings and looks for the given 1757 * file next to the ´main´ config files, in the order protected, local, 1758 * default. 1759 * 1760 * Note: no escaping or sanity checking is done here. Never pass user input 1761 * to this function! 1762 * 1763 * @param string $file 1764 * 1765 * @author Andreas Gohr <andi@splitbrain.org> 1766 * @author Anika Henke <anika@selfthinker.org> 1767 */ 1768function tpl_includeFile($file) 1769{ 1770 global $config_cascade; 1771 foreach (['protected', 'local', 'default'] as $config_group) { 1772 if (empty($config_cascade['main'][$config_group])) continue; 1773 foreach ($config_cascade['main'][$config_group] as $conf_file) { 1774 $dir = dirname($conf_file); 1775 if (file_exists("$dir/$file")) { 1776 include("$dir/$file"); 1777 return; 1778 } 1779 } 1780 } 1781 1782 // still here? try the template dir 1783 $file = tpl_incdir() . $file; 1784 if (file_exists($file)) { 1785 include($file); 1786 } 1787} 1788 1789/** 1790 * Returns <link> tag for various icon types (favicon|mobile|generic) 1791 * 1792 * @param array $types - list of icon types to display (favicon|mobile|generic) 1793 * @return string 1794 * 1795 * @author Anika Henke <anika@selfthinker.org> 1796 */ 1797function tpl_favicon($types = ['favicon']) 1798{ 1799 1800 $return = ''; 1801 1802 foreach ($types as $type) { 1803 switch ($type) { 1804 case 'favicon': 1805 $look = [':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico']; 1806 $return .= '<link rel="shortcut icon" href="' . tpl_getMediaFile($look) . '" />' . NL; 1807 break; 1808 case 'mobile': 1809 $look = [':wiki:apple-touch-icon.png', ':apple-touch-icon.png', 'images/apple-touch-icon.png']; 1810 $return .= '<link rel="apple-touch-icon" href="' . tpl_getMediaFile($look) . '" />' . NL; 1811 break; 1812 case 'generic': 1813 // ideal world solution, which doesn't work in any browser yet 1814 $look = [':wiki:favicon.svg', ':favicon.svg', 'images/favicon.svg']; 1815 $return .= '<link rel="icon" href="' . tpl_getMediaFile($look) . '" type="image/svg+xml" />' . NL; 1816 break; 1817 } 1818 } 1819 1820 return $return; 1821} 1822 1823/** 1824 * Prints full-screen media manager 1825 * 1826 * @author Kate Arzamastseva <pshns@ukr.net> 1827 */ 1828function tpl_media() 1829{ 1830 global $NS, $IMG, $JUMPTO, $REV, $lang, $fullscreen, $INPUT; 1831 $fullscreen = true; 1832 require_once DOKU_INC . 'lib/exe/mediamanager.php'; 1833 1834 $rev = ''; 1835 $image = cleanID($INPUT->str('image')); 1836 if (isset($IMG)) $image = $IMG; 1837 if (isset($JUMPTO)) $image = $JUMPTO; 1838 if (isset($REV) && !$JUMPTO) $rev = $REV; 1839 1840 echo '<div id="mediamanager__page">' . NL; 1841 echo '<h1>' . $lang['btn_media'] . '</h1>' . NL; 1842 html_msgarea(); 1843 1844 echo '<div class="panel namespaces">' . NL; 1845 echo '<h2>' . $lang['namespaces'] . '</h2>' . NL; 1846 echo '<div class="panelHeader">'; 1847 echo $lang['media_namespaces']; 1848 echo '</div>' . NL; 1849 1850 echo '<div class="panelContent" id="media__tree">' . NL; 1851 media_nstree($NS); 1852 echo '</div>' . NL; 1853 echo '</div>' . NL; 1854 1855 echo '<div class="panel filelist">' . NL; 1856 tpl_mediaFileList(); 1857 echo '</div>' . NL; 1858 1859 echo '<div class="panel file">' . NL; 1860 echo '<h2 class="a11y">' . $lang['media_file'] . '</h2>' . NL; 1861 tpl_mediaFileDetails($image, $rev); 1862 echo '</div>' . NL; 1863 1864 echo '</div>' . NL; 1865} 1866 1867/** 1868 * Return useful layout classes 1869 * 1870 * @return string 1871 * 1872 * @author Anika Henke <anika@selfthinker.org> 1873 */ 1874function tpl_classes() 1875{ 1876 global $ACT, $conf, $ID, $INFO; 1877 /** @var Input $INPUT */ 1878 global $INPUT; 1879 1880 $classes = [ 1881 'dokuwiki', 1882 'mode_' . $ACT, 1883 'tpl_' . $conf['template'], 1884 $INPUT->server->bool('REMOTE_USER') ? 'loggedIn' : '', 1885 (isset($INFO['exists']) && $INFO['exists']) ? '' : 'notFound', 1886 ($ID == $conf['start']) ? 'home' : '' 1887 ]; 1888 return implode(' ', $classes); 1889} 1890 1891/** 1892 * Create event for tools menues 1893 * 1894 * @param string $toolsname name of menu 1895 * @param array $items 1896 * @param string $view e.g. 'main', 'detail', ... 1897 * 1898 * @author Anika Henke <anika@selfthinker.org> 1899 * @deprecated 2017-09-01 see devel:menus 1900 */ 1901function tpl_toolsevent($toolsname, $items, $view = 'main') 1902{ 1903 dbg_deprecated('see devel:menus'); 1904 $data = ['view' => $view, 'items' => $items]; 1905 1906 $hook = 'TEMPLATE_' . strtoupper($toolsname) . '_DISPLAY'; 1907 $evt = new Event($hook, $data); 1908 if ($evt->advise_before()) { 1909 foreach ($evt->data['items'] as $html) echo $html; 1910 } 1911 $evt->advise_after(); 1912} 1913