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'); 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', '_data'=> $script); 348 349 // load external javascript 350 $head['script'][] = array( 'type'=>'text/javascript', 'charset'=>'utf-8', '_data'=>'', 351 'src'=>DOKU_BASE.'lib/exe/js.php'.'?tseed='.$tseed); 352 353 // trigger event here 354 trigger_event('TPL_METAHEADER_OUTPUT',$head,'_tpl_metaheaders_action',true); 355 return true; 356} 357 358/** 359 * prints the array build by tpl_metaheaders 360 * 361 * $data is an array of different header tags. Each tag can have multiple 362 * instances. Attributes are given as key value pairs. Values will be HTML 363 * encoded automatically so they should be provided as is in the $data array. 364 * 365 * For tags having a body attribute specify the the body data in the special 366 * attribute '_data'. This field will NOT BE ESCAPED automatically. 367 * 368 * @author Andreas Gohr <andi@splitbrain.org> 369 */ 370function _tpl_metaheaders_action($data){ 371 foreach($data as $tag => $inst){ 372 foreach($inst as $attr){ 373 echo '<',$tag,' ',buildAttributes($attr); 374 if(isset($attr['_data']) || $tag == 'script'){ 375 if($tag == 'script' && $attr['_data']) 376 $attr['_data'] = "<!--//--><![CDATA[//><!--\n". 377 $attr['_data']. 378 "\n//--><!]]>"; 379 380 echo '>',$attr['_data'],'</',$tag,'>'; 381 }else{ 382 echo '/>'; 383 } 384 echo "\n"; 385 } 386 } 387} 388 389/** 390 * Print a link 391 * 392 * Just builds a link. 393 * 394 * @author Andreas Gohr <andi@splitbrain.org> 395 */ 396function tpl_link($url,$name,$more='',$return=false){ 397 $out = '<a href="'.$url.'" '; 398 if ($more) $out .= ' '.$more; 399 $out .= ">$name</a>"; 400 if ($return) return $out; 401 print $out; 402 return true; 403} 404 405/** 406 * Prints a link to a WikiPage 407 * 408 * Wrapper around html_wikilink 409 * 410 * @author Andreas Gohr <andi@splitbrain.org> 411 */ 412function tpl_pagelink($id,$name=null){ 413 print html_wikilink($id,$name); 414 return true; 415} 416 417/** 418 * get the parent page 419 * 420 * Tries to find out which page is parent. 421 * returns false if none is available 422 * 423 * @author Andreas Gohr <andi@splitbrain.org> 424 */ 425function tpl_getparent($id){ 426 global $conf; 427 $parent = getNS($id).':'; 428 resolve_pageid('',$parent,$exists); 429 if($parent == $id) { 430 $pos = strrpos (getNS($id),':'); 431 $parent = substr($parent,0,$pos).':'; 432 resolve_pageid('',$parent,$exists); 433 if($parent == $id) return false; 434 } 435 return $parent; 436} 437 438/** 439 * Print one of the buttons 440 * 441 * @author Adrian Lang <mail@adrianlang.de> 442 * @see tpl_get_action 443 */ 444function tpl_button($type,$return=false){ 445 $data = tpl_get_action($type); 446 if ($data === false) { 447 return false; 448 } elseif (!is_array($data)) { 449 $out = sprintf($data, 'button'); 450 } else { 451 extract($data); 452 if ($id === '#dokuwiki__top') { 453 $out = html_topbtn(); 454 } else { 455 $out = html_btn($type, $id, $accesskey, $params, $method); 456 } 457 } 458 if ($return) return $out; 459 echo $out; 460 return true; 461} 462 463/** 464 * Like the action buttons but links 465 * 466 * @author Adrian Lang <mail@adrianlang.de> 467 * @see tpl_get_action 468 */ 469function tpl_actionlink($type,$pre='',$suf='',$inner='',$return=false){ 470 global $lang; 471 $data = tpl_get_action($type); 472 if ($data === false) { 473 return false; 474 } elseif (!is_array($data)) { 475 $out = sprintf($data, 'link'); 476 } else { 477 extract($data); 478 if (strpos($id, '#') === 0) { 479 $linktarget = $id; 480 } else { 481 $linktarget = wl($id, $params); 482 } 483 $caption = $lang['btn_' . $type]; 484 $out = tpl_link($linktarget, $pre.(($inner)?$inner:$caption).$suf, 485 'class="action ' . $type . '" ' . 486 'accesskey="' . $accesskey . '" rel="nofollow" ' . 487 'title="' . hsc($caption) . '"', 1); 488 } 489 if ($return) return $out; 490 echo $out; 491 return true; 492} 493 494/** 495 * Check the actions and get data for buttons and links 496 * 497 * Available actions are 498 * 499 * edit - edit/create/show/draft 500 * history - old revisions 501 * recent - recent changes 502 * login - login/logout - if ACL enabled 503 * profile - user profile (if logged in) 504 * index - The index 505 * admin - admin page - if enough rights 506 * top - back to top 507 * back - back to parent - if available 508 * backlink - links to the list of backlinks 509 * subscribe/subscription- subscribe/unsubscribe 510 * 511 * @author Andreas Gohr <andi@splitbrain.org> 512 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 513 * @author Adrian Lang <mail@adrianlang.de> 514 */ 515function tpl_get_action($type) { 516 global $ID; 517 global $INFO; 518 global $REV; 519 global $ACT; 520 global $conf; 521 global $auth; 522 523 // check disabled actions and fix the badly named ones 524 if($type == 'history') $type='revisions'; 525 if(!actionOK($type)) return false; 526 527 $accesskey = null; 528 $id = $ID; 529 $method = 'get'; 530 $params = array('do' => $type); 531 switch($type){ 532 case 'edit': 533 // most complicated type - we need to decide on current action 534 if($ACT == 'show' || $ACT == 'search'){ 535 $method = 'post'; 536 if($INFO['writable']){ 537 $accesskey = 'e'; 538 if(!empty($INFO['draft'])) { 539 $type = 'draft'; 540 $params['do'] = 'draft'; 541 } else { 542 $params['rev'] = $REV; 543 if(!$INFO['exists']){ 544 $type = 'create'; 545 } 546 } 547 }else{ 548 if(!actionOK('source')) return false; //pseudo action 549 $params['rev'] = $REV; 550 $type = 'source'; 551 $accesskey = 'v'; 552 } 553 }else{ 554 $params = ''; 555 $type = 'show'; 556 $accesskey = 'v'; 557 } 558 break; 559 case 'revisions': 560 $type = 'revs'; 561 $accesskey = 'o'; 562 break; 563 case 'recent': 564 $accesskey = 'r'; 565 break; 566 case 'index': 567 $accesskey = 'x'; 568 break; 569 case 'top': 570 $accesskey = 'x'; 571 $params = ''; 572 $id = '#dokuwiki__top'; 573 break; 574 case 'back': 575 $parent = tpl_getparent($ID); 576 if (!$parent) { 577 return false; 578 } 579 $id = $parent; 580 $params = ''; 581 $accesskey = 'b'; 582 break; 583 case 'login': 584 if(!$conf['useacl'] || !$auth){ 585 return false; 586 } 587 $params['sectok'] = getSecurityToken(); 588 if(isset($_SERVER['REMOTE_USER'])){ 589 if (!$auth->canDo('logout')) { 590 return false; 591 } 592 $params['do'] = 'logout'; 593 $type = 'logout'; 594 } 595 break; 596 case 'admin': 597 if(!$INFO['ismanager']){ 598 return false; 599 } 600 break; 601 case 'revert': 602 if(!$INFO['ismanager'] || !$REV || !$INFO['writable']) { 603 return false; 604 } 605 $params['rev'] = $REV; 606 $params['sectok'] = getSecurityToken(); 607 break; 608 case 'subscription': 609 $type = 'subscribe'; 610 $params['do'] = 'subscribe'; 611 case 'subscribe': 612 if(!$conf['useacl'] || !$auth || $ACT !== 'show' || !$conf['subscribers'] || !$_SERVER['REMOTE_USER']){ 613 return false; 614 } 615 break; 616 case 'backlink': 617 break; 618 case 'profile': 619 if(!$conf['useacl'] || !$auth || !isset($_SERVER['REMOTE_USER']) || 620 !$auth->canDo('Profile') || ($ACT=='profile')){ 621 return false; 622 } 623 break; 624 default: 625 return '[unknown %s type]'; 626 break; 627 } 628 return compact('accesskey', 'type', 'id', 'method', 'params'); 629} 630 631/** 632 * Wrapper around tpl_button() and tpl_actionlink() 633 * 634 * @author Anika Henke <anika@selfthinker.org> 635 */ 636function tpl_action($type,$link=0,$wrapper=false,$return=false,$pre='',$suf='',$inner='') { 637 $out = ''; 638 if ($link) $out .= tpl_actionlink($type,$pre,$suf,$inner,1); 639 else $out .= tpl_button($type,1); 640 if ($out && $wrapper) $out = "<$wrapper>$out</$wrapper>"; 641 642 if ($return) return $out; 643 print $out; 644 return $out ? true : false; 645} 646 647/** 648 * Print the search form 649 * 650 * If the first parameter is given a div with the ID 'qsearch_out' will 651 * be added which instructs the ajax pagequicksearch to kick in and place 652 * its output into this div. The second parameter controls the propritary 653 * attribute autocomplete. If set to false this attribute will be set with an 654 * value of "off" to instruct the browser to disable it's own built in 655 * autocompletion feature (MSIE and Firefox) 656 * 657 * @author Andreas Gohr <andi@splitbrain.org> 658 */ 659function tpl_searchform($ajax=true,$autocomplete=true){ 660 global $lang; 661 global $ACT; 662 global $QUERY; 663 664 // don't print the search form if search action has been disabled 665 if (!actionOk('search')) return false; 666 667 print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">'; 668 print '<input type="hidden" name="do" value="search" />'; 669 print '<input type="text" '; 670 if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" '; 671 if(!$autocomplete) print 'autocomplete="off" '; 672 print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[F]" />'; 673 print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />'; 674 if($ajax) print '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>'; 675 print '</div></form>'; 676 return true; 677} 678 679/** 680 * Print the breadcrumbs trace 681 * 682 * @author Andreas Gohr <andi@splitbrain.org> 683 */ 684function tpl_breadcrumbs($sep='»'){ 685 global $lang; 686 global $conf; 687 688 //check if enabled 689 if(!$conf['breadcrumbs']) return false; 690 691 $crumbs = breadcrumbs(); //setup crumb trace 692 693 //reverse crumborder in right-to-left mode, add RLM character to fix heb/eng display mixups 694 if($lang['direction'] == 'rtl') { 695 $crumbs = array_reverse($crumbs,true); 696 $crumbs_sep = ' ‏<span class="bcsep">'.$sep.'</span>‏ '; 697 } else { 698 $crumbs_sep = ' <span class="bcsep">'.$sep.'</span> '; 699 } 700 701 //render crumbs, highlight the last one 702 print '<span class="bchead">'.$lang['breadcrumb'].':</span>'; 703 $last = count($crumbs); 704 $i = 0; 705 foreach ($crumbs as $id => $name){ 706 $i++; 707 echo $crumbs_sep; 708 if ($i == $last) print '<span class="curid">'; 709 tpl_link(wl($id),hsc($name),'class="breadcrumbs" title="'.$id.'"'); 710 if ($i == $last) print '</span>'; 711 } 712 return true; 713} 714 715/** 716 * Hierarchical breadcrumbs 717 * 718 * This code was suggested as replacement for the usual breadcrumbs. 719 * It only makes sense with a deep site structure. 720 * 721 * @author Andreas Gohr <andi@splitbrain.org> 722 * @author Nigel McNie <oracle.shinoda@gmail.com> 723 * @author Sean Coates <sean@caedmon.net> 724 * @author <fredrik@averpil.com> 725 * @todo May behave strangely in RTL languages 726 */ 727function tpl_youarehere($sep=' » '){ 728 global $conf; 729 global $ID; 730 global $lang; 731 732 // check if enabled 733 if(!$conf['youarehere']) return false; 734 735 $parts = explode(':', $ID); 736 $count = count($parts); 737 738 if($GLOBALS['ACT'] == 'search') 739 { 740 $parts = array($conf['start']); 741 $count = 1; 742 } 743 744 echo '<span class="bchead">'.$lang['youarehere'].': </span>'; 745 746 // always print the startpage 747 $title = useHeading('navigation') ? p_get_first_heading($conf['start']) : $conf['start']; 748 if(!$title) $title = $conf['start']; 749 tpl_link(wl($conf['start']),hsc($title),'title="'.$conf['start'].'"'); 750 751 // print intermediate namespace links 752 $part = ''; 753 for($i=0; $i<$count - 1; $i++){ 754 $part .= $parts[$i].':'; 755 $page = $part; 756 resolve_pageid('',$page,$exists); 757 if ($page == $conf['start']) continue; // Skip startpage 758 759 // output 760 echo $sep; 761 if($exists){ 762 $title = useHeading('navigation') ? p_get_first_heading($page) : $parts[$i]; 763 tpl_link(wl($page),hsc($title),'title="'.$page.'"'); 764 }else{ 765 tpl_link(wl($page),$parts[$i],'title="'.$page.'" class="wikilink2" rel="nofollow"'); 766 } 767 } 768 769 // print current page, skipping start page, skipping for namespace index 770 if(isset($page) && $page==$part.$parts[$i]) return; 771 $page = $part.$parts[$i]; 772 if($page == $conf['start']) return; 773 echo $sep; 774 if(page_exists($page)){ 775 $title = useHeading('navigation') ? p_get_first_heading($page) : $parts[$i]; 776 tpl_link(wl($page),hsc($title),'title="'.$page.'"'); 777 }else{ 778 tpl_link(wl($page),$parts[$i],'title="'.$page.'" class="wikilink2" rel="nofollow"'); 779 } 780 return true; 781} 782 783/** 784 * Print info if the user is logged in 785 * and show full name in that case 786 * 787 * Could be enhanced with a profile link in future? 788 * 789 * @author Andreas Gohr <andi@splitbrain.org> 790 */ 791function tpl_userinfo(){ 792 global $lang; 793 global $INFO; 794 if(isset($_SERVER['REMOTE_USER'])){ 795 print $lang['loggedinas'].': '.$INFO['userinfo']['name'].' ('.$_SERVER['REMOTE_USER'].')'; 796 return true; 797 } 798 return false; 799} 800 801/** 802 * Print some info about the current page 803 * 804 * @author Andreas Gohr <andi@splitbrain.org> 805 */ 806function tpl_pageinfo($ret=false){ 807 global $conf; 808 global $lang; 809 global $INFO; 810 global $ID; 811 812 // return if we are not allowed to view the page 813 if (!auth_quickaclcheck($ID)) { return false; } 814 815 // prepare date and path 816 $fn = $INFO['filepath']; 817 if(!$conf['fullpath']){ 818 if($INFO['rev']){ 819 $fn = str_replace(fullpath($conf['olddir']).'/','',$fn); 820 }else{ 821 $fn = str_replace(fullpath($conf['datadir']).'/','',$fn); 822 } 823 } 824 $fn = utf8_decodeFN($fn); 825 $date = dformat($INFO['lastmod']); 826 827 // print it 828 if($INFO['exists']){ 829 $out = ''; 830 $out .= $fn; 831 $out .= ' · '; 832 $out .= $lang['lastmod']; 833 $out .= ': '; 834 $out .= $date; 835 if($INFO['editor']){ 836 $out .= ' '.$lang['by'].' '; 837 $out .= editorinfo($INFO['editor']); 838 }else{ 839 $out .= ' ('.$lang['external_edit'].')'; 840 } 841 if($INFO['locked']){ 842 $out .= ' · '; 843 $out .= $lang['lockedby']; 844 $out .= ': '; 845 $out .= editorinfo($INFO['locked']); 846 } 847 if($ret){ 848 return $out; 849 }else{ 850 echo $out; 851 return true; 852 } 853 } 854 return false; 855} 856 857/** 858 * Prints or returns the name of the given page (current one if none given). 859 * 860 * If useheading is enabled this will use the first headline else 861 * the given ID is used. 862 * 863 * @author Andreas Gohr <andi@splitbrain.org> 864 */ 865function tpl_pagetitle($id=null, $ret=false){ 866 global $conf; 867 if(is_null($id)){ 868 global $ID; 869 $id = $ID; 870 } 871 872 $name = $id; 873 if (useHeading('navigation')) { 874 $title = p_get_first_heading($id); 875 if ($title) $name = $title; 876 } 877 878 if ($ret) { 879 return hsc($name); 880 } else { 881 print hsc($name); 882 return true; 883 } 884} 885 886/** 887 * Returns the requested EXIF/IPTC tag from the current image 888 * 889 * If $tags is an array all given tags are tried until a 890 * value is found. If no value is found $alt is returned. 891 * 892 * Which texts are known is defined in the functions _exifTagNames 893 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC 894 * to the names of the latter one) 895 * 896 * Only allowed in: detail.php 897 * 898 * @author Andreas Gohr <andi@splitbrain.org> 899 */ 900function tpl_img_getTag($tags,$alt='',$src=null){ 901 // Init Exif Reader 902 global $SRC; 903 904 if(is_null($src)) $src = $SRC; 905 906 static $meta = null; 907 if(is_null($meta)) $meta = new JpegMeta($src); 908 if($meta === false) return $alt; 909 $info = $meta->getField($tags); 910 if($info == false) return $alt; 911 return $info; 912} 913 914/** 915 * Prints the image with a link to the full sized version 916 * 917 * Only allowed in: detail.php 918 * 919 * @param $maxwidth int - maximal width of the image 920 * @param $maxheight int - maximal height of the image 921 * @param $link bool - link to the orginal size? 922 * @param $params array - additional image attributes 923 */ 924function tpl_img($maxwidth=0,$maxheight=0,$link=true,$params=null){ 925 global $IMG; 926 $w = tpl_img_getTag('File.Width'); 927 $h = tpl_img_getTag('File.Height'); 928 929 //resize to given max values 930 $ratio = 1; 931 if($w >= $h){ 932 if($maxwidth && $w >= $maxwidth){ 933 $ratio = $maxwidth/$w; 934 }elseif($maxheight && $h > $maxheight){ 935 $ratio = $maxheight/$h; 936 } 937 }else{ 938 if($maxheight && $h >= $maxheight){ 939 $ratio = $maxheight/$h; 940 }elseif($maxwidth && $w > $maxwidth){ 941 $ratio = $maxwidth/$w; 942 } 943 } 944 if($ratio){ 945 $w = floor($ratio*$w); 946 $h = floor($ratio*$h); 947 } 948 949 //prepare URLs 950 $url=ml($IMG,array('cache'=>$_REQUEST['cache']),true,'&'); 951 $src=ml($IMG,array('cache'=>$_REQUEST['cache'],'w'=>$w,'h'=>$h),true,'&'); 952 953 //prepare attributes 954 $alt=tpl_img_getTag('Simple.Title'); 955 if(is_null($params)){ 956 $p = array(); 957 }else{ 958 $p = $params; 959 } 960 if($w) $p['width'] = $w; 961 if($h) $p['height'] = $h; 962 $p['class'] = 'img_detail'; 963 if($alt){ 964 $p['alt'] = $alt; 965 $p['title'] = $alt; 966 }else{ 967 $p['alt'] = ''; 968 } 969 $p['src'] = $src; 970 971 $data = array('url'=>($link?$url:null), 'params'=>$p); 972 return trigger_event('TPL_IMG_DISPLAY',$data,'_tpl_img_action',true); 973} 974 975/** 976 * Default action for TPL_IMG_DISPLAY 977 */ 978function _tpl_img_action($data, $param=NULL) { 979 $p = buildAttributes($data['params']); 980 981 if($data['url']) print '<a href="'.hsc($data['url']).'">'; 982 print '<img '.$p.'/>'; 983 if($data['url']) print '</a>'; 984 return true; 985} 986 987/** 988 * This function inserts a 1x1 pixel gif which in reality 989 * is the indexer function. 990 * 991 * Should be called somewhere at the very end of the main.php 992 * template 993 */ 994function tpl_indexerWebBug(){ 995 global $ID; 996 global $INFO; 997 if(!$INFO['exists']) return false; 998 999 if(isHiddenPage($ID)) return false; //no need to index hidden pages 1000 1001 $p = array(); 1002 $p['src'] = DOKU_BASE.'lib/exe/indexer.php?id='.rawurlencode($ID). 1003 '&'.time(); 1004 $p['width'] = 1; 1005 $p['height'] = 1; 1006 $p['alt'] = ''; 1007 $att = buildAttributes($p); 1008 print "<img $att />"; 1009 return true; 1010} 1011 1012// configuration methods 1013/** 1014 * tpl_getConf($id) 1015 * 1016 * use this function to access template configuration variables 1017 */ 1018function tpl_getConf($id){ 1019 global $conf; 1020 static $tpl_configloaded = false; 1021 1022 $tpl = $conf['template']; 1023 1024 if (!$tpl_configloaded){ 1025 $tconf = tpl_loadConfig(); 1026 if ($tconf !== false){ 1027 foreach ($tconf as $key => $value){ 1028 if (isset($conf['tpl'][$tpl][$key])) continue; 1029 $conf['tpl'][$tpl][$key] = $value; 1030 } 1031 $tpl_configloaded = true; 1032 } 1033 } 1034 1035 return $conf['tpl'][$tpl][$id]; 1036} 1037 1038/** 1039 * tpl_loadConfig() 1040 * reads all template configuration variables 1041 * this function is automatically called by tpl_getConf() 1042 */ 1043function tpl_loadConfig(){ 1044 1045 $file = DOKU_TPLINC.'/conf/default.php'; 1046 $conf = array(); 1047 1048 if (!@file_exists($file)) return false; 1049 1050 // load default config file 1051 include($file); 1052 1053 return $conf; 1054} 1055 1056// language methods 1057/** 1058 * tpl_getLang($id) 1059 * 1060 * use this function to access template language variables 1061 */ 1062function tpl_getLang($id){ 1063 static $lang = array(); 1064 1065 if (count($lang) === 0){ 1066 $path = DOKU_TPLINC.'lang/'; 1067 1068 $lang = array(); 1069 1070 global $conf; // definitely don't invoke "global $lang" 1071 // don't include once 1072 @include($path.'en/lang.php'); 1073 if ($conf['lang'] != 'en') @include($path.$conf['lang'].'/lang.php'); 1074 } 1075 1076 return $lang[$id]; 1077} 1078 1079/** 1080 * prints the "main content" in the mediamanger popup 1081 * 1082 * Depending on the user's actions this may be a list of 1083 * files in a namespace, the meta editing dialog or 1084 * a message of referencing pages 1085 * 1086 * Only allowed in mediamanager.php 1087 * 1088 * @triggers MEDIAMANAGER_CONTENT_OUTPUT 1089 * @param bool $fromajax - set true when calling this function via ajax 1090 * @author Andreas Gohr <andi@splitbrain.org> 1091 */ 1092function tpl_mediaContent($fromajax=false){ 1093 global $IMG; 1094 global $AUTH; 1095 global $INUSE; 1096 global $NS; 1097 global $JUMPTO; 1098 1099 if(is_array($_REQUEST['do'])){ 1100 $do = array_shift(array_keys($_REQUEST['do'])); 1101 }else{ 1102 $do = $_REQUEST['do']; 1103 } 1104 if(in_array($do,array('save','cancel'))) $do = ''; 1105 1106 if(!$do){ 1107 if($_REQUEST['edit']){ 1108 $do = 'metaform'; 1109 }elseif(is_array($INUSE)){ 1110 $do = 'filesinuse'; 1111 }else{ 1112 $do = 'filelist'; 1113 } 1114 } 1115 1116 // output the content pane, wrapped in an event. 1117 if(!$fromajax) ptln('<div id="media__content">'); 1118 $data = array( 'do' => $do); 1119 $evt = new Doku_Event('MEDIAMANAGER_CONTENT_OUTPUT', $data); 1120 if ($evt->advise_before()) { 1121 $do = $data['do']; 1122 if($do == 'metaform'){ 1123 media_metaform($IMG,$AUTH); 1124 }elseif($do == 'filesinuse'){ 1125 media_filesinuse($INUSE,$IMG); 1126 }elseif($do == 'filelist'){ 1127 media_filelist($NS,$AUTH,$JUMPTO); 1128 }elseif($do == 'searchlist'){ 1129 media_searchlist($_REQUEST['q'],$NS,$AUTH); 1130 }else{ 1131 msg('Unknown action '.hsc($do),-1); 1132 } 1133 } 1134 $evt->advise_after(); 1135 unset($evt); 1136 if(!$fromajax) ptln('</div>'); 1137 1138} 1139 1140/** 1141 * prints the namespace tree in the mediamanger popup 1142 * 1143 * Only allowed in mediamanager.php 1144 * 1145 * @author Andreas Gohr <andi@splitbrain.org> 1146 */ 1147function tpl_mediaTree(){ 1148 global $NS; 1149 1150 ptln('<div id="media__tree">'); 1151 media_nstree($NS); 1152 ptln('</div>'); 1153} 1154 1155 1156/** 1157 * Print a dropdown menu with all DokuWiki actions 1158 * 1159 * Note: this will not use any pretty URLs 1160 * 1161 * @author Andreas Gohr <andi@splitbrain.org> 1162 */ 1163function tpl_actiondropdown($empty='',$button='>'){ 1164 global $ID; 1165 global $INFO; 1166 global $REV; 1167 global $ACT; 1168 global $conf; 1169 global $lang; 1170 global $auth; 1171 1172 echo '<form action="' . DOKU_SCRIPT . '" method="post" accept-charset="utf-8">'; 1173 echo '<input type="hidden" name="id" value="'.$ID.'" />'; 1174 if($REV) echo '<input type="hidden" name="rev" value="'.$REV.'" />'; 1175 echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />'; 1176 1177 echo '<select name="do" id="action__selector" class="edit">'; 1178 echo '<option value="">'.$empty.'</option>'; 1179 1180 echo '<optgroup label=" — ">'; 1181 $act = tpl_get_action('edit'); 1182 if($act) echo '<option value="'.$act['params']['do'].'">'.$lang['btn_'.$act['type']].'</option>'; 1183 1184 $act = tpl_get_action('revisions'); 1185 if($act) echo '<option value="'.$act['params']['do'].'">'.$lang['btn_'.$act['type']].'</option>'; 1186 1187 $act = tpl_get_action('revert'); 1188 if($act) echo '<option value="'.$act['params']['do'].'">'.$lang['btn_'.$act['type']].'</option>'; 1189 1190 $act = tpl_get_action('backlink'); 1191 if($act) echo '<option value="'.$act['params']['do'].'">'.$lang['btn_'.$act['type']].'</option>'; 1192 echo '</optgroup>'; 1193 1194 echo '<optgroup label=" — ">'; 1195 $act = tpl_get_action('recent'); 1196 if($act) echo '<option value="'.$act['params']['do'].'">'.$lang['btn_'.$act['type']].'</option>'; 1197 1198 $act = tpl_get_action('index'); 1199 if($act) echo '<option value="'.$act['params']['do'].'">'.$lang['btn_'.$act['type']].'</option>'; 1200 echo '</optgroup>'; 1201 1202 echo '<optgroup label=" — ">'; 1203 $act = tpl_get_action('login'); 1204 if($act) echo '<option value="'.$act['params']['do'].'">'.$lang['btn_'.$act['type']].'</option>'; 1205 1206 $act = tpl_get_action('profile'); 1207 if($act) echo '<option value="'.$act['params']['do'].'">'.$lang['btn_'.$act['type']].'</option>'; 1208 1209 $act = tpl_get_action('subscribe'); 1210 if($act) echo '<option value="'.$act['params']['do'].'">'.$lang['btn_'.$act['type']].'</option>'; 1211 1212 $act = tpl_get_action('admin'); 1213 if($act) echo '<option value="'.$act['params']['do'].'">'.$lang['btn_'.$act['type']].'</option>'; 1214 echo '</optgroup>'; 1215 1216 echo '</select>'; 1217 echo '<input type="submit" value="'.$button.'" id="action__selectorbtn" />'; 1218 echo '</form>'; 1219} 1220 1221/** 1222 * Print a informational line about the used license 1223 * 1224 * @author Andreas Gohr <andi@splitbrain.org> 1225 * @param string $img - print image? (|button|badge) 1226 * @param bool $return - when true don't print, but return HTML 1227 */ 1228function tpl_license($img='badge',$imgonly=false,$return=false){ 1229 global $license; 1230 global $conf; 1231 global $lang; 1232 if(!$conf['license']) return ''; 1233 if(!is_array($license[$conf['license']])) return ''; 1234 $lic = $license[$conf['license']]; 1235 1236 $out = '<div class="license">'; 1237 if($img){ 1238 $src = license_img($img); 1239 if($src){ 1240 $out .= '<a href="'.$lic['url'].'" rel="license"'; 1241 if($conf['target']['extern']) $out .= ' target="'.$conf['target']['extern'].'"'; 1242 $out .= '><img src="'.DOKU_BASE.$src.'" class="medialeft lic'.$img.'" alt="'.$lic['name'].'" /></a> '; 1243 } 1244 } 1245 if(!$imgonly) { 1246 $out .= $lang['license']; 1247 $out .= '<a href="'.$lic['url'].'" rel="license" class="urlextern"'; 1248 if($conf['target']['extern']) $out .= ' target="'.$conf['target']['extern'].'"'; 1249 $out .= '>'.$lic['name'].'</a>'; 1250 } 1251 $out .= '</div>'; 1252 1253 if($return) return $out; 1254 echo $out; 1255} 1256 1257 1258/** 1259 * Includes the rendered XHTML of a given page 1260 * 1261 * This function is useful to populate sidebars or similar features in a 1262 * template 1263 */ 1264function tpl_include_page($pageid,$print=true){ 1265 global $ID; 1266 $oldid = $ID; 1267 $html = p_wiki_xhtml($pageid,'',false); 1268 $ID = $oldid; 1269 1270 if(!$print) return $html; 1271 echo $html; 1272} 1273 1274/** 1275 * Display the subscribe form 1276 * 1277 * @author Adrian Lang <lang@cosmocode.de> 1278 */ 1279function tpl_subscribe() { 1280 global $INFO; 1281 global $ID; 1282 global $lang; 1283 global $conf; 1284 $stime_days = $conf['subscribe_time']/60/60/24; 1285 1286 echo p_locale_xhtml('subscr_form'); 1287 echo '<h2>' . $lang['subscr_m_current_header'] . '</h2>'; 1288 echo '<div class="level2">'; 1289 if ($INFO['subscribed'] === false) { 1290 echo '<p>' . $lang['subscr_m_not_subscribed'] . '</p>'; 1291 } else { 1292 echo '<ul>'; 1293 foreach($INFO['subscribed'] as $sub) { 1294 echo '<li><div class="li">'; 1295 if ($sub['target'] !== $ID) { 1296 echo '<code class="ns">'.hsc(prettyprint_id($sub['target'])).'</code>'; 1297 } else { 1298 echo '<code class="page">'.hsc(prettyprint_id($sub['target'])).'</code>'; 1299 } 1300 $sstl = sprintf($lang['subscr_style_'.$sub['style']], $stime_days); 1301 if(!$sstl) $sstl = hsc($sub['style']); 1302 echo ' ('.$sstl.') '; 1303 1304 echo '<a href="' . wl($ID, 1305 array('do'=>'subscribe', 1306 'sub_target'=>$sub['target'], 1307 'sub_style'=>$sub['style'], 1308 'sub_action'=>'unsubscribe', 1309 'sectok' => getSecurityToken())) . 1310 '" class="unsubscribe">'.$lang['subscr_m_unsubscribe'] . 1311 '</a></div></li>'; 1312 } 1313 echo '</ul>'; 1314 } 1315 echo '</div>'; 1316 1317 // Add new subscription form 1318 echo '<h2>' . $lang['subscr_m_new_header'] . '</h2>'; 1319 echo '<div class="level2">'; 1320 $ns = getNS($ID).':'; 1321 $targets = array( 1322 $ID => '<code class="page">'.prettyprint_id($ID).'</code>', 1323 $ns => '<code class="ns">'.prettyprint_id($ns).'</code>', 1324 ); 1325 $styles = array( 1326 'every' => $lang['subscr_style_every'], 1327 'digest' => sprintf($lang['subscr_style_digest'], $stime_days), 1328 'list' => sprintf($lang['subscr_style_list'], $stime_days), 1329 ); 1330 1331 $form = new Doku_Form(array('id' => 'subscribe__form')); 1332 $form->startFieldset($lang['subscr_m_subscribe']); 1333 $form->addRadioSet('sub_target', $targets); 1334 $form->startFieldset($lang['subscr_m_receive']); 1335 $form->addRadioSet('sub_style', $styles); 1336 $form->addHidden('sub_action', 'subscribe'); 1337 $form->addHidden('do', 'subscribe'); 1338 $form->addHidden('id', $ID); 1339 $form->endFieldset(); 1340 $form->addElement(form_makeButton('submit', 'subscribe', $lang['subscr_m_subscribe'])); 1341 html_form('SUBSCRIBE', $form); 1342 echo '</div>'; 1343} 1344 1345/** 1346 * Tries to send already created content right to the browser 1347 * 1348 * Wraps around ob_flush() and flush() 1349 * 1350 * @author Andreas Gohr <andi@splitbrain.org> 1351 */ 1352function tpl_flush(){ 1353 ob_flush(); 1354 flush(); 1355} 1356 1357 1358//Setup VIM: ex: et ts=4 enc=utf-8 : 1359 1360