You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1913 lines
52 KiB

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