1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Andreas Gohr <andi@splitbrain.org>
5 */
6// must be run within Dokuwiki
7if(!defined('DOKU_INC')) die();
8
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10require_once(DOKU_PLUGIN.'syntax.php');
11
12/**
13 * All DokuWiki plugins to extend the parser/rendering mechanism
14 * need to inherit from this class
15 */
16class syntax_plugin_aclinfo extends DokuWiki_Syntax_Plugin {
17
18    /**
19     * What kind of syntax are we?
20     */
21    function getType(){
22        return 'substition';
23    }
24
25    /**
26     * What about paragraphs?
27     */
28    function getPType(){
29        return 'block';
30    }
31
32    /**
33     * Where to sort in?
34     */
35    function getSort(){
36        return 155;
37    }
38
39
40    /**
41     * Connect pattern to lexer
42     */
43    function connectTo($mode) {
44        $this->Lexer->addSpecialPattern('~~ACLINFO!?[^~]*?~~',$mode,'plugin_aclinfo');
45    }
46
47
48    /**
49     * Handle the match
50     */
51    function handle($match, $state, $pos, Doku_Handler $handler){
52        $match = substr($match,10,-2);
53        return array($match);
54    }
55
56    /**
57     * Create output
58     */
59    function render($format, Doku_Renderer $R, $data) {
60        global $INFO;
61        if($format != 'xhtml') return false;
62
63        if(!$data[0]) {
64            $page = $INFO['id'];
65        } else {
66            $page = $data[0];
67        }
68
69        $perms = $this->_aclcheck($page);
70        $R->listu_open();
71        foreach((array)$perms as $who => $p){
72            $R->listitem_open(1);
73            $R->listcontent_open();
74            $R->cdata(sprintf($this->getLang('perm'.$p), urldecode($who)));
75            $R->listcontent_close();
76            $R->listitem_close();
77        }
78        $R->listu_close();
79        return true;
80    }
81
82    function _aclcheck($id){
83        global $conf;
84        global $AUTH_ACL;
85
86        $id    = cleanID($id);
87        $ns    = getNS($id);
88        $perms = array();
89
90        //check exact match first
91        $matches = preg_grep('/^'.preg_quote($id,'/').'\s+/',$AUTH_ACL);
92        if(count($matches)){
93            foreach($matches as $match){
94                $match = preg_replace('/#.*$/','',$match); //ignore comments
95                $acl   = preg_split('/\s+/',$match);
96                if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
97                if(!isset($perms[$acl[1]])) $perms[$acl[1]] = $acl[2];
98            }
99        }
100
101        //still here? do the namespace checks
102        if($ns){
103            $path = $ns.':\*';
104        }else{
105            $path = '\*'; //root document
106        }
107
108        do{
109            $matches = preg_grep('/^'.$path.'\s+/',$AUTH_ACL);
110            if(count($matches)){
111                foreach($matches as $match){
112                    $match = preg_replace('/#.*$/','',$match); //ignore comments
113                    $acl   = preg_split('/\s+/',$match);
114                    if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
115                    if(!isset($perms[$acl[1]])) $perms[$acl[1]] = $acl[2];
116                }
117            }
118
119            //get next higher namespace
120            $ns   = getNS($ns);
121
122            if($path != '\*'){
123                $path = $ns.':\*';
124                if($path == ':\*') $path = '\*';
125            }else{
126                //we did this already
127                //break here
128                break;
129            }
130        }while(1); //this should never loop endless
131
132        return $perms;
133    }
134}
135
136//Setup VIM: ex: et ts=4 enc=utf-8 :
137