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