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