1<?php
2
3/**
4 * All DokuWiki plugins to extend the parser/rendering mechanism
5 * need to inherit from this class
6 */
7
8// must be run within DokuWiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_showif extends DokuWiki_Syntax_Plugin {
19
20/**
21 * return some info
22 */
23function getInfo(){
24    return array(
25        'author' => 'Harald Ronge',
26        'email'  => 'harald[at]turtur[.]nl',
27        'date'   => '2013-10-15',
28        'name'   => 'showif Plugin',
29        'desc'   =>
30'Shows text only if all of some conditions are true.
31Lazy hiding based on plugin nodisp from Myron Turner.
32
33Syntax is <showif [condition1], [condition2], ...>[text]</showif>
34
35Supported conditions are:
36
371. isloggedin
382. isnotloggedin
393. mayonlyread
404. mayatleastread
415. mayedit
426. isadmin
43
44Administrators will always see everything except mayonlyread.
45Not all combinations are useful ;-)
46
47',
48        'url'    => 'https://www.dokuwiki.org/plugin:showif',
49    );
50}
51
52function getType(){ return 'container'; }
53function getPType(){ return 'stack'; }
54function getAllowedTypes() { return array(
55            'container',
56            'formatting',
57            'substition',
58            'protected',
59            'disabled',
60            'paragraphs',
61            'baseonly' //new
62            ); }
63function getSort(){ return 168; } //196? I have no clue ...
64function connectTo($mode) { $this->Lexer->addEntryPattern('<showif.*?>(?=.*?</showif>)',$mode,'plugin_showif'); }
65function postConnect() { $this->Lexer->addExitPattern('</showif>','plugin_showif'); }
66
67
68/**
69 * Handle the match
70     */
71    function handle($match, $state, $pos, &$handler){
72
73
74        switch ($state) {
75          case DOKU_LEXER_ENTER :
76		  // remove <showif and >
77          $args  = trim(substr($match, 8, -1)); // $arg will be loggedin or mayedit
78          return array($state, explode(",",$args));
79
80          case DOKU_LEXER_UNMATCHED : return array($state, $match);
81          case DOKU_LEXER_EXIT : return array($state, '');
82        }
83
84        return array();
85    }
86
87    /**
88     * Create output
89     */
90    function render($mode, &$renderer, $data) {
91        global $INFO;
92
93        if($mode == 'xhtml'){
94            $renderer->nocache(); // disable caching
95            list($state, $match) = $data;
96
97            switch ($state) {
98              case DOKU_LEXER_ENTER :
99				$show = 0;
100				//$i = 0;
101				$conditions = $match;
102				// Loop through conditions
103				foreach($conditions as $val) {
104					// All conditions have to be true
105					if
106					(
107						(($val == "mayedit") && (auth_quickaclcheck($INFO['id'])) >= AUTH_EDIT)
108						||
109						//mayonlyread will be hidden for an administrator!
110						(($val == "mayonlyread") && (auth_quickaclcheck($INFO['id'])) == AUTH_READ)
111						||
112						(($val == "mayatleastread") && (auth_quickaclcheck($INFO['id'])) >= AUTH_READ)
113						||
114						($val == "isloggedin" && ($_SERVER['REMOTE_USER']))
115						||
116						($val == "isnotloggedin" && !($_SERVER['REMOTE_USER']))
117						||
118						(($val == "isadmin") && ($INFO['isadmin'] || $INFO['ismanager'] ))
119					) $show = 1;
120					else {$show = 0; break;}
121				}
122                //always open a div so DOKU_LEXER_EXIT can close it without checking state
123                // perhaps display:inline?
124				if ($show == 1) $renderer->doc .= "<div>";
125				elseif ($show == 0) $renderer->doc .= "<div style='display:none'>";
126
127                break;
128
129                case DOKU_LEXER_UNMATCHED : $renderer->doc .= $renderer->_xmlEntities($match); break;
130                case DOKU_LEXER_EXIT : $renderer->doc .= "</div>"; break;
131            }
132            return true;
133        }
134        return false;
135    }
136
137
138
139}
140
141?>
142