1<?php 2/** 3 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 4 * @author Esther Brunner <wikidesign@gmail.com> 5 * @author Christopher Smith <chris@jalakai.co.uk> 6 * @author Gina Häußge, Michael Klier <dokuwiki@chimeric.de> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) die(); 11 12if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/'); 15 16require_once(DOKU_INC.'inc/search.php'); 17 18class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin 19 20 var $includes = array(); 21 var $hasparts = array(); 22 var $toplevel_id = NULL; 23 var $toplevel = 0; 24 var $defaults = array(); 25 var $include_key = ''; 26 27 /** 28 * Constructor loads default config settings once 29 */ 30 function helper_plugin_include() { 31 $this->defaults['firstsec'] = $this->getConf('firstseconly'); 32 $this->defaults['editbtn'] = $this->getConf('showeditbtn'); 33 $this->defaults['taglogos'] = $this->getConf('showtaglogos'); 34 $this->defaults['footer'] = $this->getConf('showfooter'); 35 $this->defaults['redirect'] = $this->getConf('doredirect'); 36 $this->defaults['date'] = $this->getConf('showdate'); 37 $this->defaults['user'] = $this->getConf('showuser'); 38 $this->defaults['comments'] = $this->getConf('showcomments'); 39 $this->defaults['linkbacks'] = $this->getConf('showlinkbacks'); 40 $this->defaults['tags'] = $this->getConf('showtags'); 41 $this->defaults['link'] = $this->getConf('showlink'); 42 $this->defaults['permalink'] = $this->getConf('showpermalink'); 43 $this->defaults['indent'] = $this->getConf('doindent'); 44 } 45 46 function getInfo() { 47 return array( 48 'author' => 'Gina Häußge, Michael Klier, Esther Brunner', 49 'email' => 'dokuwiki@chimeric.de', 50 'date' => @file_get_contents(DOKU_PLUGIN . 'blog/VERSION'), 51 'name' => 'Include Plugin (helper class)', 52 'desc' => 'Functions to include another page in a wiki page', 53 'url' => 'http://dokuwiki.org/plugin:include', 54 ); 55 } 56 57 /** 58 * Available methods for other plugins 59 */ 60 function getMethods() { 61 $result = array(); 62 $result[] = array( 63 'name' => 'get_flags', 64 'desc' => 'overrides standard values for showfooter and firstseconly settings', 65 'params' => array('flags' => 'array'), 66 ); 67 return $result; 68 } 69 70 /** 71 * Overrides standard values for showfooter and firstseconly settings 72 */ 73 function get_flags($setflags) { 74 // load defaults 75 $flags = array(); 76 $flags = $this->defaults; 77 foreach ($setflags as $flag) { 78 switch ($flag) { 79 case 'footer': 80 $flags['footer'] = 1; 81 break; 82 case 'nofooter': 83 $flags['footer'] = 0; 84 break; 85 case 'firstseconly': 86 case 'firstsectiononly': 87 $flags['firstsec'] = 1; 88 break; 89 case 'fullpage': 90 $flags['firstsec'] = 0; 91 break; 92 case 'noheader': 93 $flags['noheader'] = 1; 94 break; 95 case 'editbtn': 96 case 'editbutton': 97 $flags['editbtn'] = 1; 98 break; 99 case 'noeditbtn': 100 case 'noeditbutton': 101 $flags['editbtn'] = 0; 102 break; 103 case 'permalink': 104 $flags['permalink'] = 1; 105 break; 106 case 'nopermalink': 107 $flags['permalink'] = 0; 108 break; 109 case 'redirect': 110 $flags['redirect'] = 1; 111 break; 112 case 'noredirect': 113 $flags['redirect'] = 0; 114 break; 115 case 'link': 116 $flags['link'] = 1; 117 break; 118 case 'nolink': 119 $flags['link'] = 0; 120 break; 121 case 'user': 122 $flags['user'] = 1; 123 break; 124 case 'nouser': 125 $flags['user'] = 0; 126 break; 127 case 'comments': 128 $flags['comments'] = 1; 129 break; 130 case 'nocomments': 131 $flags['comments'] = 0; 132 break; 133 case 'linkbacks': 134 $flags['linkbacks'] = 1; 135 break; 136 case 'nolinkbacks': 137 $flags['linkbacks'] = 0; 138 break; 139 case 'tags': 140 $flags['tags'] = 1; 141 break; 142 case 'notags': 143 $flags['tags'] = 0; 144 break; 145 case 'date': 146 $flags['date'] = 1; 147 break; 148 case 'nodate': 149 $flags['date'] = 0; 150 break; 151 case 'indent': 152 $flags['indent'] = 1; 153 break; 154 case 'noindent': 155 $flags['indent'] = 0; 156 break; 157 } 158 } 159 return $flags; 160 } 161 162 /** 163 * Parses the instructions list of the page which contains the includes 164 * 165 * @author Michael Klier <chi@chimeric.de> 166 */ 167 function parse_instructions($id, &$ins) { 168 global $conf; 169 global $INFO; 170 171 $num = count($ins); 172 173 $lvl = false; 174 $prev_lvl = false; 175 $mode = ''; 176 $page = ''; 177 $flags = array(); 178 $range = false; 179 $scope = $id; 180 181 for($i=0; $i<$num; $i++) { 182 // set current level 183 if($ins[$i][0] == 'section_open') { 184 $lvl = $ins[$i][1][0]; 185 if($i > $range) $prev_lvl = $lvl; 186 } 187 188 if($ins[$i][0] == 'plugin' && $ins[$i][1][0] == 'include_include' ) { 189 // found no previous section set lvl to 0 190 if(!$lvl) $lvl = 0; 191 192 $mode = $ins[$i][1][1][0]; 193 194 if($mode == 'namespace') { 195 $ns = str_replace(':', '/', cleanID($ins[$i][1][1][1])); 196 $sect = ''; 197 $flags = $ins[$i][1][1][3]; 198 199 $pages = array(); 200 search($pages, $conf['datadir'], 'search_list', '', $ns); 201 sort($pages); 202 203 if(!empty($pages)) { 204 $ins_inc = array(); 205 foreach($pages as $page) { 206 $perm = auth_quickaclcheck($page['id']); 207 array_push($this->hasparts, $page['id']); 208 if($perm < AUTH_READ) continue; 209 $ins_tmp[0] = 'plugin'; 210 $ins_tmp[1][0] = 'include_include'; 211 $ins_tmp[1][1][0] = 'page'; 212 $ins_tmp[1][1][1] = $page['id']; 213 $ins_tmp[1][1][2] = ''; 214 $ins_tmp[1][1][3] = $flags; 215 $ins_inc = array_merge($ins_inc, array($ins_tmp)); 216 } 217 $ins_start = array_slice($ins, 0, $i+1); 218 $ins_end = array_slice($ins, $i+1); 219 $ins = array_merge($ins_start, $ins_inc, $ins_end); 220 } 221 unset($ins[$i]); 222 $i--; 223 } 224 225 if($mode == 'page' || $mode == 'section') { 226 $page = $ins[$i][1][1][1]; 227 $perm = auth_quickaclcheck($page); 228 229 array_push($this->hasparts, $page); 230 if($perm < AUTH_READ) continue; 231 232 $sect = $ins[$i][1][1][2]; 233 $flags = $ins[$i][1][1][3]; 234 235 $page = $this->_apply_macro($page); 236 resolve_pageid(getNS($scope), $page, $exists); // resolve shortcuts 237 $ins[$i][1][1][4] = $scope; 238 $scope = $page; 239 $flags = $this->get_flags($flags); 240 241 if(!page_exists($page)) { 242 if($flags['footer']) { 243 $ins[$i] = $this->_footer($page, $sect, '', $flags, 0); 244 } else { 245 unset($ins[$i]); 246 } 247 } else { 248 $ins_inc = $this->_get_instructions($page, $sect, $mode, $lvl, $flags); 249 if(!empty($ins_inc)) { 250 // combine instructions and reset counter 251 $ins_start = array_slice($ins, 0, $i+1); 252 $ins_end = array_slice($ins, $i+1); 253 $range = $i + count($ins_inc); 254 $ins = array_merge($ins_start, $ins_inc, $ins_end); 255 $num = count($ins); 256 } 257 } 258 } 259 } 260 261 // check if we left the range of possible sub includes and reset lvl and scope to toplevel_id 262 if($range && ($i >= $range)) { 263 $lvl = ($prev_lvl == 0) ? 1 : $prev_lvl; 264 $prev_lvl = false; 265 $range = false; 266 // reset scope to toplevel_id 267 $scope = $this->toplevel_id; 268 } 269 } 270 271 if(!empty($INFO['userinfo'])) { 272 $include_key = $INFO['userinfo']['name'] . '|' . implode('|', $INFO['userinfo']['grps']); 273 } else { 274 $include_key = '@ALL'; 275 } 276 277 $meta = array(); 278 $meta = p_get_metadata($id, 'plugin_include'); 279 $meta['pages'] = array_unique($this->hasparts); 280 $meta['keys'][$include_key] = true; 281 p_set_metadata($id, array('plugin_include' => $meta), true, true); 282 } 283 284 /** 285 * Returns the converted instructions of a give page/section 286 * 287 * @author Michael Klier <chi@chimeric.de> 288 */ 289 function _get_instructions($page, $sect, $mode, $lvl, $flags) { 290 global $ID; 291 292 $key = ($sect) ? $page . '#' . $sect : $page; 293 294 // prevent recursion 295 if(!$this->includes[$key]) { 296 $ins = p_cached_instructions(wikiFN($page)); 297 $this->includes[$key] = true; 298 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags); 299 return $ins; 300 } 301 } 302 303 /** 304 * Converts instructions of the included page 305 * 306 * The funcion iterates over the given list of instructions and generates 307 * an index of header and section indicies. It also removes document 308 * start/end instructions, converts links, and removes unwanted 309 * instructions like tags, comments, linkbacks. 310 * 311 * Later all header/section levels are convertet to match the current 312 * inclusion level. 313 * 314 * @author Michael Klier <chi@chimeric.de> 315 */ 316 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags) { 317 318 // filter instructions if needed 319 if(!empty($sect)) { 320 $this->_get_section($ins, $sect); // section required 321 } 322 323 if($flags['firstsec']) { 324 $this->_get_firstsec($ins, $page); // only first section 325 } 326 327 $ns = getNS($page); 328 $num = count($ins); 329 330 $conv_idx = array(); // conversion index 331 $lvl_max = false; // max level 332 $first_header = -1; 333 $no_header = false; 334 $sect_title = false; 335 336 for($i=0; $i<$num; $i++) { 337 switch($ins[$i][0]) { 338 case 'document_start': 339 case 'document_end': 340 case 'section_edit': 341 unset($ins[$i]); 342 break; 343 case 'header': 344 // get section title of first section 345 if($sect && !$sect_title) { 346 $sect_title = $ins[$i][1][0]; 347 } 348 // check if we need to skip the first header 349 if((!$no_header) && $flags['noheader']) { 350 $no_header = true; 351 } 352 353 $conv_idx[] = $i; 354 // get index of first header 355 if($first_header == -1) $first_header = $i; 356 // get max level of this instructions set 357 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 358 $lvl_max = $ins[$i][1][1]; 359 } 360 break; 361 case 'section_open': 362 $conv_idx[] = $i; 363 break; 364 case 'internallink': 365 case 'internalmedia': 366 if($ins[$i][1][0]{0} == '.') { 367 if($ins[$i][1][0]{1} == '.') { 368 $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace 369 } else { 370 $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace 371 } 372 } elseif (strpos($ins[$i][1][0], ':') === false) { 373 $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links 374 } 375 break; 376 case 'plugin': 377 // FIXME skip other plugins? 378 switch($ins[$i][1][0]) { 379 case 'tag_tag': // skip tags 380 case 'discussion_comments': // skip comments 381 case 'linkback': // skip linkbacks 382 case 'data_entry': // skip data plugin 383 case 'meta': // skip meta plugin 384 unset($ins[$i]); 385 break; 386 } 387 break; 388 default: 389 break; 390 } 391 } 392 393 // calculate difference between header/section level and include level 394 $diff = 0; 395 if (!$lvl_max) $lvl_max = 0; // if no level found in target, set to 0 396 $diff = $lvl - $lvl_max + 1; 397 if ($no_header) $diff -= 1; // push up one level if "noheader" 398 399 // convert headers and set footer/permalink 400 $hdr_deleted = false; 401 $has_permalink = false; 402 $footer_lvl = false; 403 foreach($conv_idx as $idx) { 404 if($ins[$idx][0] == 'header') { 405 if($no_header && !$hdr_deleted) { 406 unset ($ins[$idx]); 407 $hdr_deleted = true; 408 continue; 409 } 410 411 if($flags['indent']) { 412 $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 413 $ins[$idx][1][1] = $lvl_new; 414 } 415 416 // set permalink 417 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 418 $this->_permalink($ins[$idx], $page, $sect, $flags); 419 $has_permalink = true; 420 } 421 422 // set footer level 423 if(!$footer_lvl && ($idx == $first_header) && !$no_header) { 424 if($flags['indent']) { 425 $footer_lvl = $lvl_new; 426 } else { 427 $footer_lvl = $lvl_max; 428 } 429 } 430 } else { 431 // it's a section 432 if($flags['indent']) { 433 $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 434 $ins[$idx][1][0] = $lvl_new; 435 } 436 437 // check if noheader is used and set the footer level to the first section 438 if($no_header && !$footer_lvl) { 439 if($flags['indent']) { 440 $footer_lvl = $lvl_new; 441 } else { 442 $footer_lvl = $lvl_max; 443 } 444 } 445 } 446 } 447 448 // add edit button 449 if($flags['editbtn'] && (auth_quickaclcheck($page) >= AUTH_EDIT)) { 450 $this->_editbtn($ins, $page, $sect, $sect_title); 451 } 452 453 // add footer 454 if($flags['footer']) { 455 $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl); 456 } 457 458 // add instructions entry divs 459 array_unshift($ins, array('plugin', array('include_div', array('open', $page)))); 460 array_push($ins, array('plugin', array('include_div', array('close')))); 461 462 // close previous section if any and re-open after inclusion 463 if($lvl != 0) { 464 array_unshift($ins, array('section_close')); 465 $ins[] = array('section_open', array($lvl)); 466 } 467 } 468 469 /** 470 * Appends instruction item for the include plugin footer 471 * 472 * @author Michael Klier <chi@chimeric.de> 473 */ 474 function _footer($page, $sect, $sect_title, $flags, $footer_lvl) { 475 $footer = array(); 476 $footer[0] = 'plugin'; 477 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $this->toplevel_id, $footer_lvl)); 478 return $footer; 479 } 480 481 /** 482 * Appends instruction item for an edit button 483 * 484 * @author Michael Klier <chi@chimeric.de> 485 */ 486 function _editbtn(&$ins, $page, $sect, $sect_title) { 487 $editbtn = array(); 488 $editbtn[0] = 'plugin'; 489 $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $this->toplevel_id)); 490 $ins[] = $editbtn; 491 } 492 493 /** 494 * Convert instruction item for a permalink header 495 * 496 * @author Michael Klier <chi@chimeric.de> 497 */ 498 function _permalink(&$ins, $page, $sect, $flags) { 499 $ins[0] = 'plugin'; 500 $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags)); 501 } 502 503 /** 504 * Get a section including its subsections 505 * 506 * @author Michael Klier <chi@chimeric.de> 507 */ 508 function _get_section(&$ins, $sect) { 509 $num = count($ins); 510 $offset = false; 511 $lvl = false; 512 $end = false; 513 514 for($i=0; $i<$num; $i++) { 515 if ($ins[$i][0] == 'header') { 516 517 // found the right header 518 if (cleanID($ins[$i][1][0]) == $sect) { 519 $offset = $i; 520 $lvl = $ins[$i][1][1]; 521 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 522 $end = $i - $offset; 523 break; 524 } 525 } 526 } 527 $offset = $offset ? $offset : 0; 528 $end = $end ? $end : ($num - 1); 529 if(is_array($ins)) { 530 $ins = array_slice($ins, $offset, $end); 531 } 532 } 533 534 /** 535 * Only display the first section of a page and a readmore link 536 * 537 * @author Michael Klier <chi@chimeric.de> 538 */ 539 function _get_firstsec(&$ins, $page) { 540 $num = count($ins); 541 $first_sect = false; 542 for($i=0; $i<$num; $i++) { 543 if($ins[$i][0] == 'section_close') { 544 $first_sect = $i; 545 } 546 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 547 $ins = array_slice($ins, 0, $first_sect); 548 $ins[] = array('p_open', array()); 549 $ins[] = array('internallink',array($page, $this->getLang('readmore'))); 550 $ins[] = array('p_close', array()); 551 $ins[] = array('section_close'); 552 return; 553 } 554 } 555 } 556 557 /** 558 * Makes user or date dependent includes possible 559 */ 560 function _apply_macro($id) { 561 global $INFO; 562 global $auth; 563 564 // if we don't have an auth object, do nothing 565 if (!$auth) return $id; 566 567 $user = $_SERVER['REMOTE_USER']; 568 $group = $INFO['userinfo']['grps'][0]; 569 570 $replace = array( 571 '@USER@' => cleanID($user), 572 '@NAME@' => cleanID($INFO['userinfo']['name']), 573 '@GROUP@' => cleanID($group), 574 '@YEAR@' => date('Y'), 575 '@MONTH@' => date('m'), 576 '@DAY@' => date('d'), 577 ); 578 return str_replace(array_keys($replace), array_values($replace), $id); 579 } 580} 581//vim:ts=4:sw=4:et:enc=utf-8: 582