[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/ -> template.php (source)

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