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 * @author Michael Hamann <michael@content-space.de> 8 */ 9 10/** 11 * Helper functions for the include plugin and other plugins that want to include pages. 12 */ 13class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin 14 15 var $defaults = array(); 16 var $sec_close = true; 17 /** @var helper_plugin_tag $taghelper */ 18 var $taghelper = null; 19 var $includes = array(); // deprecated - compatibility code for the blog plugin 20 21 /** 22 * Constructor loads default config settings once 23 */ 24 function __construct() { 25 $this->defaults['noheader'] = $this->getConf('noheader'); 26 $this->defaults['firstsec'] = $this->getConf('firstseconly'); 27 $this->defaults['editbtn'] = $this->getConf('showeditbtn'); 28 $this->defaults['taglogos'] = $this->getConf('showtaglogos'); 29 $this->defaults['footer'] = $this->getConf('showfooter'); 30 $this->defaults['redirect'] = $this->getConf('doredirect'); 31 $this->defaults['date'] = $this->getConf('showdate'); 32 $this->defaults['mdate'] = $this->getConf('showmdate'); 33 $this->defaults['user'] = $this->getConf('showuser'); 34 $this->defaults['comments'] = $this->getConf('showcomments'); 35 $this->defaults['linkbacks'] = $this->getConf('showlinkbacks'); 36 $this->defaults['tags'] = $this->getConf('showtags'); 37 $this->defaults['link'] = $this->getConf('showlink'); 38 $this->defaults['permalink'] = $this->getConf('showpermalink'); 39 $this->defaults['indent'] = $this->getConf('doindent'); 40 $this->defaults['linkonly'] = $this->getConf('linkonly'); 41 $this->defaults['title'] = $this->getConf('title'); 42 $this->defaults['pageexists'] = $this->getConf('pageexists'); 43 $this->defaults['parlink'] = $this->getConf('parlink'); 44 $this->defaults['inline'] = false; 45 $this->defaults['order'] = $this->getConf('order'); 46 $this->defaults['rsort'] = $this->getConf('rsort'); 47 $this->defaults['depth'] = $this->getConf('depth'); 48 $this->defaults['readmore'] = $this->getConf('readmore'); 49 } 50 51 /** 52 * Available methods for other plugins 53 */ 54 function getMethods() { 55 $result = array(); 56 $result[] = array( 57 'name' => 'get_flags', 58 'desc' => 'overrides standard values for showfooter and firstseconly settings', 59 'params' => array('flags' => 'array'), 60 ); 61 return $result; 62 } 63 64 /** 65 * Overrides standard values for showfooter and firstseconly settings 66 */ 67 function get_flags($setflags) { 68 // load defaults 69 $flags = $this->defaults; 70 foreach ($setflags as $flag) { 71 $value = ''; 72 if (strpos($flag, '=') !== false) { 73 list($flag, $value) = explode('=', $flag, 2); 74 } 75 switch ($flag) { 76 case 'footer': 77 $flags['footer'] = 1; 78 break; 79 case 'nofooter': 80 $flags['footer'] = 0; 81 break; 82 case 'firstseconly': 83 case 'firstsectiononly': 84 $flags['firstsec'] = 1; 85 break; 86 case 'fullpage': 87 $flags['firstsec'] = 0; 88 break; 89 case 'showheader': 90 case 'header': 91 $flags['noheader'] = 0; 92 break; 93 case 'noheader': 94 $flags['noheader'] = 1; 95 break; 96 case 'editbtn': 97 case 'editbutton': 98 $flags['editbtn'] = 1; 99 break; 100 case 'noeditbtn': 101 case 'noeditbutton': 102 $flags['editbtn'] = 0; 103 break; 104 case 'permalink': 105 $flags['permalink'] = 1; 106 break; 107 case 'nopermalink': 108 $flags['permalink'] = 0; 109 break; 110 case 'redirect': 111 $flags['redirect'] = 1; 112 break; 113 case 'noredirect': 114 $flags['redirect'] = 0; 115 break; 116 case 'link': 117 $flags['link'] = 1; 118 break; 119 case 'nolink': 120 $flags['link'] = 0; 121 break; 122 case 'user': 123 $flags['user'] = 1; 124 break; 125 case 'nouser': 126 $flags['user'] = 0; 127 break; 128 case 'comments': 129 $flags['comments'] = 1; 130 break; 131 case 'nocomments': 132 $flags['comments'] = 0; 133 break; 134 case 'linkbacks': 135 $flags['linkbacks'] = 1; 136 break; 137 case 'nolinkbacks': 138 $flags['linkbacks'] = 0; 139 break; 140 case 'tags': 141 $flags['tags'] = 1; 142 break; 143 case 'notags': 144 $flags['tags'] = 0; 145 break; 146 case 'date': 147 $flags['date'] = 1; 148 break; 149 case 'nodate': 150 $flags['date'] = 0; 151 break; 152 case 'mdate': 153 $flags['mdate'] = 1; 154 break; 155 case 'nomdate': 156 $flags['mdate'] = 0; 157 break; 158 case 'indent': 159 $flags['indent'] = 1; 160 break; 161 case 'noindent': 162 $flags['indent'] = 0; 163 break; 164 case 'linkonly': 165 $flags['linkonly'] = 1; 166 break; 167 case 'nolinkonly': 168 case 'include_content': 169 $flags['linkonly'] = 0; 170 break; 171 case 'inline': 172 $flags['inline'] = 1; 173 break; 174 case 'title': 175 $flags['title'] = 1; 176 break; 177 case 'notitle': 178 $flags['title'] = 0; 179 break; 180 case 'pageexists': 181 $flags['pageexists'] = 1; 182 break; 183 case 'nopageexists': 184 $flags['pageexists'] = 0; 185 break; 186 case 'existlink': 187 $flags['pageexists'] = 1; 188 $flags['linkonly'] = 1; 189 break; 190 case 'parlink': 191 $flags['parlink'] = 1; 192 break; 193 case 'noparlink': 194 $flags['parlink'] = 0; 195 break; 196 case 'order': 197 $flags['order'] = $value; 198 break; 199 case 'sort': 200 $flags['rsort'] = 0; 201 break; 202 case 'rsort': 203 $flags['rsort'] = 1; 204 break; 205 case 'depth': 206 $flags['depth'] = max(intval($value), 0); 207 break; 208 case 'beforeeach': 209 $flags['beforeeach'] = $value; 210 break; 211 case 'aftereach': 212 $flags['aftereach'] = $value; 213 break; 214 case 'readmore': 215 $flags['readmore'] = 1; 216 break; 217 case 'noreadmore': 218 $flags['readmore'] = 0; 219 break; 220 case 'exclude': 221 $flags['exclude'] = $value; 222 break; 223 } 224 } 225 // the include_content URL parameter overrides flags 226 if (isset($_REQUEST['include_content'])) 227 $flags['linkonly'] = 0; 228 return $flags; 229 } 230 231 /** 232 * Returns the converted instructions of a give page/section 233 * 234 * @author Michael Klier <chi@chimeric.de> 235 * @author Michael Hamann <michael@content-space.de> 236 */ 237 function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $included_pages = array()) { 238 $key = ($sect) ? $page . '#' . $sect : $page; 239 $this->includes[$key] = true; // legacy code for keeping compatibility with other plugins 240 241 // keep compatibility with other plugins that don't know the $root_id parameter 242 if (is_null($root_id)) { 243 global $ID; 244 $root_id = $ID; 245 } 246 247 if ($flags['linkonly']) { 248 if (page_exists($page) || $flags['pageexists'] == 0) { 249 $title = ''; 250 if ($flags['title']) 251 $title = p_get_first_heading($page); 252 if($flags['parlink']) { 253 $ins = array( 254 array('p_open', array()), 255 array('internallink', array(':'.$key, $title)), 256 array('p_close', array()), 257 ); 258 } else { 259 $ins = array(array('internallink', array(':'.$key,$title))); 260 } 261 }else { 262 $ins = array(); 263 } 264 } else { 265 if (page_exists($page)) { 266 global $ID; 267 $backupID = $ID; 268 $ID = $page; // Change the global $ID as otherwise plugins like the discussion plugin will save data for the wrong page 269 $ins = p_cached_instructions(wikiFN($page), false, $page); 270 $ID = $backupID; 271 } else { 272 $ins = array(); 273 } 274 275 $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id, $included_pages); 276 } 277 return $ins; 278 } 279 280 /** 281 * Converts instructions of the included page 282 * 283 * The funcion iterates over the given list of instructions and generates 284 * an index of header and section indicies. It also removes document 285 * start/end instructions, converts links, and removes unwanted 286 * instructions like tags, comments, linkbacks. 287 * 288 * Later all header/section levels are convertet to match the current 289 * inclusion level. 290 * 291 * @author Michael Klier <chi@chimeric.de> 292 */ 293 function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $included_pages = array()) { 294 global $conf; 295 296 // filter instructions if needed 297 if(!empty($sect)) { 298 $this->_get_section($ins, $sect); // section required 299 } 300 301 if($flags['firstsec']) { 302 $this->_get_firstsec($ins, $page, $flags); // only first section 303 } 304 305 $ns = getNS($page); 306 $num = count($ins); 307 308 $conv_idx = array(); // conversion index 309 $lvl_max = false; // max level 310 $first_header = -1; 311 $no_header = false; 312 $sect_title = false; 313 $endpos = null; // end position of the raw wiki text 314 315 $this->adapt_links($ins, $page, $included_pages); 316 317 for($i=0; $i<$num; $i++) { 318 switch($ins[$i][0]) { 319 case 'document_start': 320 case 'document_end': 321 case 'section_edit': 322 unset($ins[$i]); 323 break; 324 case 'header': 325 // get section title of first section 326 if($sect && !$sect_title) { 327 $sect_title = $ins[$i][1][0]; 328 } 329 // check if we need to skip the first header 330 if((!$no_header) && $flags['noheader']) { 331 $no_header = true; 332 } 333 334 $conv_idx[] = $i; 335 // get index of first header 336 if($first_header == -1) $first_header = $i; 337 // get max level of this instructions set 338 if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) { 339 $lvl_max = $ins[$i][1][1]; 340 } 341 break; 342 case 'section_open': 343 if ($flags['inline']) 344 unset($ins[$i]); 345 else 346 $conv_idx[] = $i; 347 break; 348 case 'section_close': 349 if ($flags['inline']) 350 unset($ins[$i]); 351 break; 352 case 'nest': 353 $this->adapt_links($ins[$i][1][0], $page, $included_pages); 354 break; 355 case 'plugin': 356 // FIXME skip other plugins? 357 switch($ins[$i][1][0]) { 358 case 'tag_tag': // skip tags 359 case 'discussion_comments': // skip comments 360 case 'linkback': // skip linkbacks 361 case 'data_entry': // skip data plugin 362 case 'meta': // skip meta plugin 363 case 'indexmenu_tag': // skip indexmenu sort tag 364 case 'include_sorttag': // skip include plugin sort tag 365 unset($ins[$i]); 366 break; 367 // adapt indentation level of nested includes 368 case 'include_include': 369 if (!$flags['inline'] && $flags['indent']) 370 $ins[$i][1][1][4] += $lvl; 371 break; 372 /* 373 * if there is already a closelastsecedit instruction (was added by one of the section 374 * functions), store its position but delete it as it can't be determined yet if it is needed, 375 * i.e. if there is a header which generates a section edit (depends on the levels, level 376 * adjustments, $no_header, ...) 377 */ 378 case 'include_closelastsecedit': 379 $endpos = $ins[$i][1][1][0]; 380 unset($ins[$i]); 381 break; 382 } 383 break; 384 default: 385 break; 386 } 387 } 388 389 // calculate difference between header/section level and include level 390 $diff = 0; 391 if (!isset($lvl_max)) $lvl_max = 0; // if no level found in target, set to 0 392 $diff = $lvl - $lvl_max + 1; 393 if ($no_header) $diff -= 1; // push up one level if "noheader" 394 395 // convert headers and set footer/permalink 396 $hdr_deleted = false; 397 $has_permalink = false; 398 $footer_lvl = false; 399 $contains_secedit = false; 400 $section_close_at = false; 401 foreach($conv_idx as $idx) { 402 if($ins[$idx][0] == 'header') { 403 if ($section_close_at === false && isset($ins[$idx+1]) && $ins[$idx+1][0] == 'section_open') { 404 // store the index of the first heading that is followed by a new section 405 // the wrap plugin creates sections without section_open so the section shouldn't be closed before them 406 $section_close_at = $idx; 407 } 408 409 if($no_header && !$hdr_deleted) { 410 unset ($ins[$idx]); 411 $hdr_deleted = true; 412 continue; 413 } 414 415 if($flags['indent']) { 416 $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff); 417 $ins[$idx][1][1] = $lvl_new; 418 } 419 420 if($ins[$idx][1][1] <= $conf['maxseclevel']) 421 $contains_secedit = true; 422 423 // set permalink 424 if($flags['link'] && !$has_permalink && ($idx == $first_header)) { 425 $this->_permalink($ins[$idx], $page, $sect, $flags); 426 $has_permalink = true; 427 } 428 429 // set footer level 430 if(!$footer_lvl && ($idx == $first_header) && !$no_header) { 431 if($flags['indent'] && isset($lvl_new)) { 432 $footer_lvl = $lvl_new; 433 } else { 434 $footer_lvl = $lvl_max; 435 } 436 } 437 } else { 438 // it's a section 439 if($flags['indent']) { 440 $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff); 441 $ins[$idx][1][0] = $lvl_new; 442 } 443 444 // check if noheader is used and set the footer level to the first section 445 if($no_header && !$footer_lvl) { 446 if($flags['indent'] && isset($lvl_new)) { 447 $footer_lvl = $lvl_new; 448 } else { 449 $footer_lvl = $lvl_max; 450 } 451 } 452 } 453 } 454 455 // close last open section of the included page if there is any 456 if ($contains_secedit) { 457 array_push($ins, array('plugin', array('include_closelastsecedit', array($endpos)))); 458 } 459 460 $include_secid = (isset($flags['include_secid']) ? $flags['include_secid'] : NULL); 461 462 // add edit button 463 if($flags['editbtn']) { 464 $this->_editbtn($ins, $page, $sect, $sect_title, ($flags['redirect'] ? $root_id : false), $include_secid); 465 } 466 467 // add footer 468 if($flags['footer']) { 469 $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id); 470 } 471 472 // wrap content at the beginning of the include that is not in a section in a section 473 if ($lvl > 0 && $section_close_at !== 0 && $flags['indent'] && !$flags['inline']) { 474 if ($section_close_at === false) { 475 $ins[] = array('section_close', array()); 476 array_unshift($ins, array('section_open', array($lvl))); 477 } else { 478 $section_close_idx = array_search($section_close_at, array_keys($ins)); 479 if ($section_close_idx > 0) { 480 $before_ins = array_slice($ins, 0, $section_close_idx); 481 $after_ins = array_slice($ins, $section_close_idx); 482 $ins = array_merge($before_ins, array(array('section_close', array())), $after_ins); 483 array_unshift($ins, array('section_open', array($lvl))); 484 } 485 } 486 } 487 488 // add instructions entry wrapper 489 array_unshift($ins, array('plugin', array('include_wrap', array('open', $page, $flags['redirect'], $include_secid)))); 490 if (isset($flags['beforeeach'])) 491 array_unshift($ins, array('entity', array($flags['beforeeach']))); 492 array_push($ins, array('plugin', array('include_wrap', array('close')))); 493 if (isset($flags['aftereach'])) 494 array_push($ins, array('entity', array($flags['aftereach']))); 495 496 // close previous section if any and re-open after inclusion 497 if($lvl != 0 && $this->sec_close && !$flags['inline']) { 498 array_unshift($ins, array('section_close', array())); 499 $ins[] = array('section_open', array($lvl)); 500 } 501 } 502 503 /** 504 * Appends instruction item for the include plugin footer 505 * 506 * @author Michael Klier <chi@chimeric.de> 507 */ 508 function _footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id) { 509 $footer = array(); 510 $footer[0] = 'plugin'; 511 $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $root_id, $footer_lvl)); 512 return $footer; 513 } 514 515 /** 516 * Appends instruction item for an edit button 517 * 518 * @author Michael Klier <chi@chimeric.de> 519 */ 520 function _editbtn(&$ins, $page, $sect, $sect_title, $root_id, $hid = '') { 521 $title = ($sect) ? $sect_title : $page; 522 $editbtn = array(); 523 $editbtn[0] = 'plugin'; 524 $editbtn[1] = array('include_editbtn', array($title, $hid)); 525 $ins[] = $editbtn; 526 } 527 528 /** 529 * Convert instruction item for a permalink header 530 * 531 * @author Michael Klier <chi@chimeric.de> 532 */ 533 function _permalink(&$ins, $page, $sect, $flags) { 534 $ins[0] = 'plugin'; 535 $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $ins[1][2], $page, $sect, $flags)); 536 } 537 538 /** 539 * Convert internal and local links depending on the included pages 540 * 541 * @param array $ins The instructions that shall be adapted 542 * @param string $page The included page 543 * @param array $included_pages The array of pages that are included 544 */ 545 private function adapt_links(&$ins, $page, $included_pages = null) { 546 $num = count($ins); 547 $ns = getNS($page); 548 549 for($i=0; $i<$num; $i++) { 550 // adjust links with image titles 551 if (strpos($ins[$i][0], 'link') !== false && isset($ins[$i][1][1]) && is_array($ins[$i][1][1]) && $ins[$i][1][1]['type'] == 'internalmedia') { 552 // resolve relative ids, but without cleaning in order to preserve the name 553 $media_id = resolve_id($ns, $ins[$i][1][1]['src']); 554 // make sure that after resolving the link again it will be the same link 555 if ($media_id[0] != ':') $media_id = ':'.$media_id; 556 $ins[$i][1][1]['src'] = $media_id; 557 } 558 switch($ins[$i][0]) { 559 case 'internallink': 560 case 'internalmedia': 561 // make sure parameters aren't touched 562 $link_params = ''; 563 $link_id = $ins[$i][1][0]; 564 $link_parts = explode('?', $link_id, 2); 565 if (count($link_parts) === 2) { 566 $link_id = $link_parts[0]; 567 $link_params = $link_parts[1]; 568 } 569 // resolve the id without cleaning it 570 $link_id = resolve_id($ns, $link_id, false); 571 // this id is internal (i.e. absolute) now, add ':' to make resolve_id work again 572 if ($link_id[0] != ':') $link_id = ':'.$link_id; 573 // restore parameters 574 $ins[$i][1][0] = ($link_params != '') ? $link_id.'?'.$link_params : $link_id; 575 576 if ($ins[$i][0] == 'internallink' && !empty($included_pages)) { 577 // change links to included pages into local links 578 // only adapt links without parameters 579 $link_id = $ins[$i][1][0]; 580 $link_parts = explode('?', $link_id, 2); 581 if (count($link_parts) === 1) { 582 $exists = false; 583 resolve_pageid($ns, $link_id, $exists); 584 585 $link_parts = explode('#', $link_id, 2); 586 $hash = ''; 587 if (count($link_parts) === 2) { 588 list($link_id, $hash) = $link_parts; 589 } 590 if (array_key_exists($link_id, $included_pages)) { 591 if ($hash) { 592 // hopefully the hash is also unique in the including page (otherwise this might be the wrong link target) 593 $ins[$i][0] = 'locallink'; 594 $ins[$i][1][0] = $hash; 595 } else { 596 // the include section ids are different from normal section ids (so they won't conflict) but this 597 // also means that the normal locallink function can't be used 598 $ins[$i][0] = 'plugin'; 599 $ins[$i][1] = array('include_locallink', array($included_pages[$link_id]['hid'], $ins[$i][1][1], $ins[$i][1][0])); 600 } 601 } 602 } 603 } 604 break; 605 case 'locallink': 606 /* Convert local links to internal links if the page hasn't been fully included */ 607 if ($included_pages == null || !array_key_exists($page, $included_pages)) { 608 $ins[$i][0] = 'internallink'; 609 $ins[$i][1][0] = ':'.$page.'#'.$ins[$i][1][0]; 610 } 611 break; 612 } 613 } 614 } 615 616 /** 617 * Get a section including its subsections 618 * 619 * @author Michael Klier <chi@chimeric.de> 620 */ 621 function _get_section(&$ins, $sect) { 622 $num = count($ins); 623 $offset = false; 624 $lvl = false; 625 $end = false; 626 $endpos = null; // end position in the input text, needed for section edit buttons 627 628 $check = array(); // used for sectionID() in order to get the same ids as the xhtml renderer 629 630 for($i=0; $i<$num; $i++) { 631 if ($ins[$i][0] == 'header') { 632 633 // found the right header 634 if (sectionID($ins[$i][1][0], $check) == $sect) { 635 $offset = $i; 636 $lvl = $ins[$i][1][1]; 637 } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { 638 $end = $i - $offset; 639 $endpos = $ins[$i][1][2]; // the position directly after the found section, needed for the section edit button 640 break; 641 } 642 } 643 } 644 $offset = $offset ? $offset : 0; 645 $end = $end ? $end : ($num - 1); 646 if(is_array($ins)) { 647 $ins = array_slice($ins, $offset, $end); 648 // store the end position in the include_closelastsecedit instruction so it can generate a matching button 649 $ins[] = array('plugin', array('include_closelastsecedit', array($endpos))); 650 } 651 } 652 653 /** 654 * Only display the first section of a page and a readmore link 655 * 656 * @author Michael Klier <chi@chimeric.de> 657 */ 658 function _get_firstsec(&$ins, $page, $flags) { 659 $num = count($ins); 660 $first_sect = false; 661 $endpos = null; // end position in the input text 662 for($i=0; $i<$num; $i++) { 663 if($ins[$i][0] == 'section_close') { 664 $first_sect = $i; 665 } 666 if ($ins[$i][0] == 'header') { 667 /* 668 * Store the position of the last header that is encountered. As section_close/open-instruction are 669 * always (unless some plugin modifies this) around a header instruction this means that the last 670 * position that is stored here is exactly the position of the section_close/open at which the content 671 * is truncated. 672 */ 673 $endpos = $ins[$i][1][2]; 674 } 675 // only truncate the content and add the read more link when there is really 676 // more than that first section 677 if(($first_sect) && ($ins[$i][0] == 'section_open')) { 678 $ins = array_slice($ins, 0, $first_sect); 679 if ($flags['readmore']) { 680 $ins[] = array('plugin', array('include_readmore', array($page))); 681 } 682 $ins[] = array('section_close', array()); 683 // store the end position in the include_closelastsecedit instruction so it can generate a matching button 684 $ins[] = array('plugin', array('include_closelastsecedit', array($endpos))); 685 return; 686 } 687 } 688 } 689 690 /** 691 * Gives a list of pages for a given include statement 692 * 693 * @author Michael Hamann <michael@content-space.de> 694 */ 695 function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { 696 global $conf; 697 $pages = array(); 698 switch($mode) { 699 case 'namespace': 700 $page = cleanID($page); 701 $ns = utf8_encodeFN(str_replace(':', '/', $page)); 702 // depth is absolute depth, not relative depth, but 0 has a special meaning. 703 $depth = $flags['depth'] ? $flags['depth'] + substr_count($page, ':') + ($page ? 1 : 0) : 0; 704 search($pagearrays, $conf['datadir'], 'search_allpages', array('depth' => $depth, 'skipacl' => false), $ns); 705 if (is_array($pagearrays)) { 706 foreach ($pagearrays as $pagearray) { 707 if (!isHiddenPage($pagearray['id'])) // skip hidden pages 708 $pages[] = $pagearray['id']; 709 } 710 } 711 break; 712 case 'tagtopic': 713 if (!$this->taghelper) 714 $this->taghelper = plugin_load('helper', 'tag'); 715 if(!$this->taghelper) { 716 msg('You have to install the tag plugin to use this functionality!', -1); 717 return array(); 718 } 719 $tag = $page; 720 $sect = ''; 721 $pagearrays = $this->taghelper->getTopic('', null, $tag); 722 foreach ($pagearrays as $pagearray) { 723 $pages[] = $pagearray['id']; 724 } 725 break; 726 default: 727 $page = $this->_apply_macro($page, $parent_id); 728 resolve_pageid(getNS($parent_id), $page, $exists); // resolve shortcuts and clean ID 729 if (auth_quickaclcheck($page) >= AUTH_READ) 730 $pages[] = $page; 731 } 732 733 if (isset($flags['exclude'])) 734 $pages = array_filter($pages, function ($page) use ($flags) { 735 if (@preg_match($flags['exclude'], $page)) 736 return FALSE; 737 return TRUE; 738 }); 739 740 if (count($pages) > 1) { 741 if ($flags['order'] === 'id') { 742 if ($flags['rsort']) { 743 usort($pages, array($this, '_r_strnatcasecmp')); 744 } else { 745 natcasesort($pages); 746 } 747 } else { 748 $ordered_pages = array(); 749 foreach ($pages as $page) { 750 $key = ''; 751 switch ($flags['order']) { 752 case 'title': 753 $key = p_get_first_heading($page); 754 break; 755 case 'created': 756 $key = p_get_metadata($page, 'date created', METADATA_DONT_RENDER); 757 break; 758 case 'modified': 759 $key = p_get_metadata($page, 'date modified', METADATA_DONT_RENDER); 760 break; 761 case 'indexmenu': 762 $key = p_get_metadata($page, 'indexmenu_n', METADATA_RENDER_USING_SIMPLE_CACHE); 763 if ($key === null) 764 $key = ''; 765 break; 766 case 'custom': 767 $key = p_get_metadata($page, 'include_n', METADATA_RENDER_USING_SIMPLE_CACHE); 768 if ($key === null) 769 $key = ''; 770 break; 771 } 772 $key .= '_'.$page; 773 $ordered_pages[$key] = $page; 774 } 775 if ($flags['rsort']) { 776 uksort($ordered_pages, array($this, '_r_strnatcasecmp')); 777 } else { 778 uksort($ordered_pages, 'strnatcasecmp'); 779 } 780 $pages = $ordered_pages; 781 } 782 } 783 784 $result = array(); 785 foreach ($pages as $page) { 786 $exists = page_exists($page); 787 $result[] = array('id' => $page, 'exists' => $exists, 'parent_id' => $parent_id); 788 } 789 return $result; 790 } 791 792 /** 793 * String comparisons using a "natural order" algorithm in reverse order 794 * 795 * @link http://php.net/manual/en/function.strnatcmp.php 796 * @param string $a First string 797 * @param string $b Second string 798 * @return int Similar to other string comparison functions, this one returns < 0 if 799 * str1 is greater than str2; > 800 * 0 if str1 is lesser than 801 * str2, and 0 if they are equal. 802 */ 803 function _r_strnatcasecmp($a, $b) { 804 return strnatcasecmp($b, $a); 805 } 806 807 /** 808 * This function generates the list of all included pages from a list of metadata 809 * instructions. 810 */ 811 function _get_included_pages_from_meta_instructions($instructions) { 812 $pages = array(); 813 foreach ($instructions as $instruction) { 814 $mode = $instruction['mode']; 815 $page = $instruction['page']; 816 $sect = $instruction['sect']; 817 $parent_id = $instruction['parent_id']; 818 $flags = $instruction['flags']; 819 $pages = array_merge($pages, $this->_get_included_pages($mode, $page, $sect, $parent_id, $flags)); 820 } 821 return $pages; 822 } 823 824 /** 825 * Get wiki language from "HTTP_ACCEPT_LANGUAGE" 826 * We allow the pattern e.g. "ja,en-US;q=0.7,en;q=0.3" 827 */ 828 function _get_language_of_wiki($id, $parent_id) { 829 global $conf; 830 $result = $conf['lang']; 831 if(strpos($id, '@BROWSER_LANG@') !== false){ 832 $brlangp = "/([a-zA-Z]{1,8}(-[a-zA-Z]{1,8})*|\*)(;q=(0(.[0-9]{0,3})?|1(.0{0,3})?))?/"; 833 if(preg_match_all( 834 $brlangp, $_SERVER["HTTP_ACCEPT_LANGUAGE"], 835 $matches, PREG_SET_ORDER 836 )){ 837 $langs = array(); 838 foreach($matches as $match){ 839 $langname = $match[1] == '*' ? $conf['lang'] : $match[1]; 840 $qvalue = $match[4] == '' ? 1.0 : $match[4]; 841 $langs[$langname] = $qvalue; 842 } 843 arsort($langs); 844 foreach($langs as $lang => $langq){ 845 $testpage = $this->_apply_macro(str_replace('@BROWSER_LANG@', $lang, $id), $parent_id); 846 resolve_pageid(getNS($parent_id), $testpage, $exists); 847 if($exists){ 848 $result = $lang; 849 break; 850 } 851 } 852 } 853 } 854 return cleanID($result); 855 } 856 857 /** 858 * Makes user or date dependent includes possible 859 */ 860 function _apply_macro($id, $parent_id) { 861 global $USERINFO; 862 /* @var Input $INPUT */ 863 global $INPUT; 864 865 // The following is basicaly copied from basicinfo() because 866 // this function can be called from within pageinfo() in 867 // p_get_metadata and thus we cannot rely on $INFO being set 868 if($INPUT->server->has('REMOTE_USER')) { 869 $user = $INPUT->server->str('REMOTE_USER'); 870 } else { 871 // no registered user - use IP 872 $user = clientIP(true); 873 } 874 875 // Take user's name if possible, login name otherwise 876 if (!empty($USERINFO['name'])) { 877 $name = $USERINFO['name']; 878 } else { 879 $name = $user; 880 } 881 882 // Take first group if possible 883 if (!empty($USERINFO['grps'])) { 884 $group = $USERINFO['grps'][0]; 885 } else { 886 $group = 'ALL'; 887 } 888 889 $time_stamp = time(); 890 if(preg_match('/@DATE(\w+)@/',$id,$matches)) { 891 switch($matches[1]) { 892 case 'PMONTH': 893 $time_stamp = strtotime("-1 month"); 894 break; 895 case 'NMONTH': 896 $time_stamp = strtotime("+1 month"); 897 break; 898 case 'NWEEK': 899 $time_stamp = strtotime("+1 week"); 900 break; 901 case 'PWEEK': 902 $time_stamp = strtotime("-1 week"); 903 break; 904 case 'TOMORROW': 905 $time_stamp = strtotime("+1 day"); 906 break; 907 case 'YESTERDAY': 908 $time_stamp = strtotime("-1 day"); 909 break; 910 case 'NYEAR': 911 $time_stamp = strtotime("+1 year"); 912 break; 913 case 'PYEAR': 914 $time_stamp = strtotime("-1 year"); 915 break; 916 } 917 $id = preg_replace('/@DATE(\w+)@/','', $id); 918 } 919 920 $replace = array( 921 '@USER@' => cleanID($user), 922 '@NAME@' => cleanID($name), 923 '@GROUP@' => cleanID($group), 924 '@BROWSER_LANG@' => $this->_get_language_of_wiki($id, $parent_id), 925 '@YEAR@' => date('Y',$time_stamp), 926 '@MONTH@' => date('m',$time_stamp), 927 '@WEEK@' => date('W',$time_stamp), 928 '@DAY@' => date('d',$time_stamp), 929 '@YEARPMONTH@' => date('Ym',strtotime("-1 month")), 930 '@PMONTH@' => date('m',strtotime("-1 month")), 931 '@NMONTH@' => date('m',strtotime("+1 month")), 932 '@YEARNMONTH@' => date('Ym',strtotime("+1 month")), 933 '@YEARPWEEK@' => date('YW',strtotime("-1 week")), 934 '@PWEEK@' => date('W',strtotime("-1 week")), 935 '@NWEEK@' => date('W',strtotime("+1 week")), 936 '@YEARNWEEK@' => date('YW',strtotime("+1 week")), 937 ); 938 return str_replace(array_keys($replace), array_values($replace), $id); 939 } 940} 941// vim:ts=4:sw=4:et: 942