template.php 51 KB

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