* * */ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'syntax.php'); /** * All DokuWiki plugins to extend the parser/rendering mechanism * need to inherit from this class */ class syntax_plugin_isauth extends DokuWiki_Syntax_Plugin { /** * return some info */ /* function getInfo(){ return array( 'author' => 'Otto Vainio', 'email' => 'oiv-ifauth@valjakko.net', 'date' => '2005-09-23', 'name' => 'ifauth plugin', 'desc' => 'Show content at this time', 'url' => 'http://wiki.splitbrain.org/wiki:plugins', ); } */ /** * What kind of syntax are we? */ function getType(){ return 'substition'; } /** * Paragraph Type * * Defines how this syntax is handled regarding paragraphs. This is important * for correct XHTML nesting. Should return one of the following: * * 'normal' - The plugin can be used inside paragraphs * 'block' - Open paragraphs need to be closed before plugin output * 'stack' - Special case. Plugin wraps other paragraphs. * * @see Doku_Handler_Block */ function getPType() { return 'normal'; } function getSort(){ return 360; } function connectTo($mode) { $this->Lexer->addEntryPattern('\[isauth.*?\](?=.*?\x5B/isauth\x5D)',$mode,'plugin_isauth'); /* the use of '<' and '>' is deprecated and just for backwards compatibility */ $this->Lexer->addEntryPattern('(?=.*?\x3C/isauth\x3E)',$mode,'plugin_isauth'); } function postConnect() { $this->Lexer->addExitPattern('\[/isauth\]','plugin_isauth'); $this->Lexer->addExitPattern('','plugin_isauth'); } /** * Handle the match */ function handle($match, $state, $pos, Doku_Handler $handler){ switch ($state) { case DOKU_LEXER_ENTER : // remove '' $auth = trim(substr($match, 8, -1)); // explode wanted auths $aauth = explode(",",$auth); return array($state, $aauth); case DOKU_LEXER_UNMATCHED : return array($state, $match); case DOKU_LEXER_EXIT : return array($state, ''); } return array(); } /** * Create output */ function render($mode, Doku_Renderer $renderer, $data) { // isauth stores wanted user/group array global $isauth; // grps hold current user groups and userid global $grps; global $INFO; if($mode == 'xhtml'){ list($state, $match) = $data; switch ($state) { case DOKU_LEXER_ENTER : // Store wanted groups/userid $isauth=$match; // Store current user info. Add '@' to the group names $grps=array(); if (isset($INFO['userinfo'])) { if (is_array($INFO['userinfo'])) { foreach($INFO['userinfo']['grps'] as $val) { $grps[]="@" . $val; } } } if (isset($_SERVER['REMOTE_USER'])) { $grps[]=$_SERVER['REMOTE_USER']; } break; case DOKU_LEXER_UNMATCHED : $rend=0; // Loop through each wanted user / group foreach($isauth as $val) { $not=0; // Check negation if (substr($val,0,1)=="!") { $not=1; $val=substr($val,1); } // FIXME More complicated rules may be wanted. Currently any rule that matches for render overrides others. // If current user/group found in wanted groups/userid, then render. if ($not==0 && in_array($val,$grps)) { $rend=1; } // If user set as not wanted (!) or not found from current user/group then render. if ($not==1 && !in_array($val,$grps)) { $rend=1; } } if ($rend>0) { $r = p_render('xhtml',p_get_instructions($match),$info); // Remove '\n\n' from start and '\n\n' from the end. if (stristr(substr($r,0,5),"\n

\n")) { $r = substr($r,5); } // if (stristr(substr($r,-7),"\n

\n")) { // $r = substr($r,0,strlen($r)-6); // } // --> from Chris 2017-08-19 (https://www.dokuwiki.org/plugin:ifauth?s[]=ifauth) if (stristr(substr($r,-7),"\n\n

\n")) { $r = substr($r,0,-7); } if (stristr(substr($r,-6),"\n

\n")) { $r = substr($r,0,-6); } // end Chris $renderer->doc .= $r; } $renderer->nocache(); break; case DOKU_LEXER_EXIT : break; } return true; } return false; } } ?>