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