1<?php
2
3/**
4 * This actually looks like the better implementation but I run into some caching issues on Angua with this. On Adora Belle it seemed ok.
5 * It is possible that the problems were spurious and only due to an unnatural situation when switching identities and rights in testing
6 * Test before adopting this code!
7 */
8
9
10/**
11 * All DokuWiki plugins to extend the parser/rendering mechanism
12 * need to inherit from this class
13 */
14
15// must be run within DokuWiki
16if(!defined('DOKU_INC')) die();
17
18if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
19require_once(DOKU_PLUGIN.'syntax.php');
20
21/**
22 * All DokuWiki plugins to extend the parser/rendering mechanism
23 * need to inherit from this class
24 */
25class syntax_plugin_showif extends DokuWiki_Syntax_Plugin {
26
27/**
28 * return some info
29 */
30function getInfo(){
31    return array(
32        'author' => 'Harald Ronge',
33        'email'  => 'harald[at]turtur[.]nl',
34        'date'   => '2013-10-15',
35        'name'   => 'showif Plugin',
36        'desc'   =>
37'Shows text only if all of some conditions are true. Basic markup will be shown, but heading-markup is ignored.
38Based on plugin nodisp from Myron Turner.
39
40Admins and Managers will always see everything.
41
42Syntax is <showif [condition1], [condition2], ...>[text]</showif>
43
44Supported conditions are:
45
461. isloggedin
472. isnotloggedin
483. mayonlyread
494. mayatleastread
505. mayedit
516. isadmin
52
53Administrators will always see everything except mayonlyread.
54Not all combinations are useful ;-)
55
56',
57        'url'    => 'http://www.turtur.nl',
58    );
59}
60
61//new function
62function accepts($mode){
63	return true;
64}
65
66function getType(){ return 'container'; } //was formatting
67function getPType(){ return 'stack'; }
68function getAllowedTypes() { return array(
69            'container',
70            'formatting',
71            'substition',
72            'protected',
73            'disabled',
74            'paragraphs',
75            'baseonly' //new
76);
77}
78function getSort(){ return 196; } //was 168
79function connectTo($mode) {
80	$this->Lexer->addEntryPattern('<showif.*?>(?=.*?</showif>)',$mode,'plugin_showif');
81}
82function postConnect() { $this->Lexer->addExitPattern('</showif>','plugin_showif'); }
83
84
85/**
86 * Handle the match
87     */
88    function handle($match, $state, $pos, &$handler){
89
90        switch ($state) {
91          case DOKU_LEXER_ENTER :
92		    // remove <showif and >
93			$conditions = trim(substr($match, 8, -1));
94			// explode wanted auths
95			$this->conditions = explode(",",$conditions);
96
97			// FIXME remember conditions here
98
99			$ReWriter = new Doku_Handler_Nest($handler->CallWriter,'plugin_showif');
100			$handler->CallWriter = & $ReWriter;
101			// don't add any plugin instruction:
102            return false;
103
104          case DOKU_LEXER_UNMATCHED :
105                // unmatched data is cdata
106                $handler->_addCall('cdata', array($match), $pos);
107                // don't add any plugin instruction:
108                return false;
109
110          case DOKU_LEXER_EXIT :
111               // get all calls we intercepted
112                $calls = $handler->CallWriter->calls;
113
114                // switch back to the old call writer
115                $ReWriter = & $handler->CallWriter;
116                $handler->CallWriter = & $ReWriter->CallWriter;
117
118                // return a plugin instruction
119                return array($state, $calls, $this->conditions);
120        }
121
122        return false;
123    }
124
125    /**
126     * Create output
127     */
128    function render($mode, &$renderer, $data) {
129        global $INFO;
130
131        if($mode == 'xhtml'){
132            $renderer->nocache(); // disable caching
133	        list($state, $calls, $conditions) = $data;
134    	    if($state != DOKU_LEXER_EXIT) return true;
135
136			$show = FALSE;
137			//$i = 0;
138			// Loop through conditions
139			foreach($conditions as $val) {
140				// All conditions have to be true
141				if
142				(
143					(($val == "mayedit") && (auth_quickaclcheck($INFO['id'])) >= AUTH_EDIT)
144					||
145					//mayonlyread will be hidden for an administrator!
146					(($val == "mayonlyread") && (auth_quickaclcheck($INFO['id'])) == AUTH_READ)
147					||
148					(($val == "mayatleastread") && (auth_quickaclcheck($INFO['id'])) >= AUTH_READ)
149					||
150					($val == "isloggedin" && ($_SERVER['REMOTE_USER']))
151					||
152					($val == "isnotloggedin" && !($_SERVER['REMOTE_USER']))
153					||
154					(($val == "isadmin") && ($INFO['isadmin'] || $INFO['ismanager'] ))
155				) $show = TRUE;
156				else {$show = FALSE; break;}
157			}
158
159			if ($show) {
160				foreach($calls as $i){
161					if(method_exists($renderer,$i[0])){
162						call_user_func_array(array($renderer,$i[0]),$i[1]);
163					}
164				}
165			}
166            return true;
167        }
168        return false;
169    }
170
171
172
173}
174?>
175