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