1<?php
2  /**
3  * Plugin ifauth: Displays content at given time. (After next cache update)
4  *
5  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6  * @author     Otto Vainio <oiv-ifauth@valjakko.net>
7  */
8
9  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10  if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11  require_once(DOKU_PLUGIN.'syntax.php');
12
13  /**
14  * All DokuWiki plugins to extend the parser/rendering mechanism
15  * need to inherit from this class
16  */
17  class syntax_plugin_ifauth extends DokuWiki_Syntax_Plugin {
18
19    /**
20    * return some info
21    */
22    function getInfo(){
23      return array(
24        'author' => 'Otto Vainio',
25        'email'  => 'oiv-ifauth@valjakko.net',
26        'date'   => '2005-09-23',
27        'name'   => 'ifauth plugin',
28        'desc'   => 'Show content at this time',
29        'url'    => 'http://wiki.splitbrain.org/wiki:plugins',
30      );
31    }
32
33    /**
34    * What kind of syntax are we?
35    */
36    function getType(){
37      return 'substition';
38    }
39   /**
40    * Paragraph Type
41    *
42    * Defines how this syntax is handled regarding paragraphs. This is important
43    * for correct XHTML nesting. Should return one of the following:
44    *
45    * 'normal' - The plugin can be used inside paragraphs
46    * 'block'  - Open paragraphs need to be closed before plugin output
47    * 'stack'  - Special case. Plugin wraps other paragraphs.
48    *
49    * @see Doku_Handler_Block
50    */
51    function getPType() {
52      return 'normal';
53    }
54
55    function getSort(){
56      return 360;
57    }
58    function connectTo($mode) {
59      $this->Lexer->addEntryPattern('<ifauth.*?>(?=.*?\x3C/ifauth\x3E)',$mode,'plugin_ifauth');
60    }
61    function postConnect() {
62      $this->Lexer->addExitPattern('</ifauth>','plugin_ifauth');
63    }
64
65
66    /**
67    * Handle the match
68    */
69    function handle($match, $state, $pos, &$handler){
70      switch ($state) {
71        case DOKU_LEXER_ENTER :
72
73// remove <ifauth and >
74          $auth  = trim(substr($match, 8, -1));
75
76// explode wanted auths
77          $aauth = explode(",",$auth);
78          return array($state, $aauth);
79        case DOKU_LEXER_UNMATCHED :  return array($state, $match);
80        case DOKU_LEXER_EXIT :       return array($state, '');
81      }
82      return array();
83    }
84
85    /**
86    * Create output
87    */
88    function render($mode, &$renderer, $data) {
89
90// ifauth stoes wanted user/group array
91      global $ifauth;
92
93// grps hold curren user groups and userid
94      global $grps;
95      global $INFO;
96      if($mode == 'xhtml'){
97        list($state, $match) = $data;
98        switch ($state) {
99        case DOKU_LEXER_ENTER :
100
101// Store wanted groups/userid
102          $ifauth=$match;
103
104// Store current user info. Add '@' to the group names
105          $grps=array();
106          if (is_array($INFO['userinfo'])) {
107            foreach($INFO['userinfo']['grps'] as $val) {
108              $grps[]="@" . $val;
109            }
110          }
111          $grps[]=$_SERVER['REMOTE_USER'];
112          break;
113        case DOKU_LEXER_UNMATCHED :
114          $rend=0;
115
116// Loop through each wanted user / group
117          foreach($ifauth as $val) {
118            $not=0;
119
120// Check negation
121            if (substr($val,0,1)=="!") {
122              $not=1;
123              $val=substr($val,1);
124            }
125// FIXME More complicated rules may be wanted. Currently any rule that matches for render overrides others.
126
127// If current user/group found in wanted groups/userid, then render.
128            if ($not==0 && in_array($val,$grps)) {
129              $rend=1;
130            }
131
132// If user set as not wanted (!) or not found from current user/group then render.
133            if ($not==1 && !in_array($val,$grps)) {
134              $rend=1;
135            }
136          }
137          if ($rend>0) {
138            $r = p_render('xhtml',p_get_instructions($match),$info);
139// Remove '\n<b>\n' from start and '\n</b>\n' from the end.
140            if (stristr(substr($r,0,5),"\n<p>\n")) {
141              $r = substr($r,5);
142            }
143            if (stristr(substr($r,-7)," \n</p>\n")) {
144              $r = substr($r,0,-7);
145            }
146            $renderer->doc .= $r;
147          }
148          $renderer->nocache();
149          break;
150        case DOKU_LEXER_EXIT :
151          break;
152        }
153        return true;
154      }
155      return false;
156    }
157  }
158?>