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