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