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