xref: /plugin/aclinfo/syntax.php (revision 8769b165848b15463685dac63605df954c377d06) !
1a2f0e577SAndreas Gohr<?php
2*8769b165SAndreas Gohr
3*8769b165SAndreas Gohruse dokuwiki\Extension\SyntaxPlugin;
4*8769b165SAndreas Gohr
5a2f0e577SAndreas Gohr/**
6a2f0e577SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7a2f0e577SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
8a2f0e577SAndreas Gohr */
9*8769b165SAndreas Gohrclass syntax_plugin_aclinfo extends SyntaxPlugin
10*8769b165SAndreas Gohr{
11*8769b165SAndreas Gohr    /** @inheritdoc */
12*8769b165SAndreas Gohr    public function getType()
13*8769b165SAndreas Gohr    {
14a2f0e577SAndreas Gohr        return 'substition';
15a2f0e577SAndreas Gohr    }
16a2f0e577SAndreas Gohr
17*8769b165SAndreas Gohr    /** @inheritdoc */
18*8769b165SAndreas Gohr    public function getPType()
19*8769b165SAndreas Gohr    {
20a2f0e577SAndreas Gohr        return 'block';
21a2f0e577SAndreas Gohr    }
22a2f0e577SAndreas Gohr
23*8769b165SAndreas Gohr    /** @inheritdoc */
24*8769b165SAndreas Gohr    public function getSort()
25*8769b165SAndreas Gohr    {
26a2f0e577SAndreas Gohr        return 155;
27a2f0e577SAndreas Gohr    }
28a2f0e577SAndreas Gohr
29*8769b165SAndreas Gohr    /** @inheritdoc */
30*8769b165SAndreas Gohr    public function connectTo($mode)
31*8769b165SAndreas Gohr    {
3216f7e10fSAndreas Gohr        $this->Lexer->addSpecialPattern('~~ACLINFO!?[^~]*?~~', $mode, 'plugin_aclinfo');
33a2f0e577SAndreas Gohr    }
34a2f0e577SAndreas Gohr
35*8769b165SAndreas Gohr    /** @inheritdoc */
36*8769b165SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
37*8769b165SAndreas Gohr    {
3816f7e10fSAndreas Gohr        $match = substr($match, 10, -2);
39*8769b165SAndreas Gohr        return [$match];
40a2f0e577SAndreas Gohr    }
41a2f0e577SAndreas Gohr
42*8769b165SAndreas Gohr    /** @inheritdoc */
43*8769b165SAndreas Gohr    public function render($format, Doku_Renderer $R, $data)
44*8769b165SAndreas Gohr    {
45a2f0e577SAndreas Gohr        global $INFO;
46a2f0e577SAndreas Gohr        if ($format != 'xhtml') return false;
47a2f0e577SAndreas Gohr
4816f7e10fSAndreas Gohr        if (!$data[0]) {
4916f7e10fSAndreas Gohr            $page = $INFO['id'];
5016f7e10fSAndreas Gohr        } else {
5116f7e10fSAndreas Gohr            $page = $data[0];
5216f7e10fSAndreas Gohr        }
5316f7e10fSAndreas Gohr
54*8769b165SAndreas Gohr        $perms = $this->aclCheck($page);
55a2f0e577SAndreas Gohr        $R->listu_open();
56a2f0e577SAndreas Gohr        foreach ((array)$perms as $who => $p) {
57a2f0e577SAndreas Gohr            $R->listitem_open(1);
58a2f0e577SAndreas Gohr            $R->listcontent_open();
59a2f0e577SAndreas Gohr            $R->cdata(sprintf($this->getLang('perm' . $p), urldecode($who)));
60a2f0e577SAndreas Gohr            $R->listcontent_close();
61a2f0e577SAndreas Gohr            $R->listitem_close();
62a2f0e577SAndreas Gohr        }
63a2f0e577SAndreas Gohr        $R->listu_close();
64a2f0e577SAndreas Gohr        return true;
65a2f0e577SAndreas Gohr    }
66a2f0e577SAndreas Gohr
67*8769b165SAndreas Gohr    /**
68*8769b165SAndreas Gohr     * Parse the ACL setup and return the permissions for the given page ID.
69*8769b165SAndreas Gohr     *
70*8769b165SAndreas Gohr     * @param string $id The page ID to check
71*8769b165SAndreas Gohr     * @return array
72*8769b165SAndreas Gohr     */
73*8769b165SAndreas Gohr    protected function aclCheck($id)
74*8769b165SAndreas Gohr    {
75a2f0e577SAndreas Gohr        global $AUTH_ACL;
76a2f0e577SAndreas Gohr
7716f7e10fSAndreas Gohr        $id    = cleanID($id);
78a2f0e577SAndreas Gohr        $ns    = getNS($id);
79*8769b165SAndreas Gohr        $perms = [];
80a2f0e577SAndreas Gohr
81a2f0e577SAndreas Gohr        //check exact match first
82a2f0e577SAndreas Gohr        $matches = preg_grep('/^' . preg_quote($id, '/') . '\s+/', $AUTH_ACL);
83a2f0e577SAndreas Gohr        if (count($matches)) {
84a2f0e577SAndreas Gohr            foreach ($matches as $match) {
85a2f0e577SAndreas Gohr                $match = preg_replace('/#.*$/', '', $match); //ignore comments
86a2f0e577SAndreas Gohr                $acl   = preg_split('/\s+/', $match);
87a2f0e577SAndreas Gohr                if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
88a2f0e577SAndreas Gohr                if (!isset($perms[$acl[1]])) $perms[$acl[1]] = $acl[2];
89a2f0e577SAndreas Gohr            }
90a2f0e577SAndreas Gohr        }
91a2f0e577SAndreas Gohr
92a2f0e577SAndreas Gohr        //still here? do the namespace checks
93a2f0e577SAndreas Gohr        if ($ns) {
94a2f0e577SAndreas Gohr            $path = $ns . ':\*';
95a2f0e577SAndreas Gohr        } else {
96a2f0e577SAndreas Gohr            $path = '\*'; //root document
97a2f0e577SAndreas Gohr        }
98a2f0e577SAndreas Gohr
99a2f0e577SAndreas Gohr        do {
100a2f0e577SAndreas Gohr            $matches = preg_grep('/^' . $path . '\s+/', $AUTH_ACL);
101a2f0e577SAndreas Gohr            if (count($matches)) {
102a2f0e577SAndreas Gohr                foreach ($matches as $match) {
103a2f0e577SAndreas Gohr                    $match = preg_replace('/#.*$/', '', $match); //ignore comments
104a2f0e577SAndreas Gohr                    $acl   = preg_split('/\s+/', $match);
105a2f0e577SAndreas Gohr                    if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
106a2f0e577SAndreas Gohr                    if (!isset($perms[$acl[1]])) $perms[$acl[1]] = $acl[2];
107a2f0e577SAndreas Gohr                }
108a2f0e577SAndreas Gohr            }
109a2f0e577SAndreas Gohr
110a2f0e577SAndreas Gohr            //get next higher namespace
111a2f0e577SAndreas Gohr            $ns   = getNS($ns);
112a2f0e577SAndreas Gohr
113a2f0e577SAndreas Gohr            if ($path != '\*') {
114a2f0e577SAndreas Gohr                $path = $ns . ':\*';
115a2f0e577SAndreas Gohr                if ($path == ':\*') $path = '\*';
116a2f0e577SAndreas Gohr            } else {
117a2f0e577SAndreas Gohr                //we did this already
118a2f0e577SAndreas Gohr                //break here
119a2f0e577SAndreas Gohr                break;
120a2f0e577SAndreas Gohr            }
121a2f0e577SAndreas Gohr        } while (1); //this should never loop endless
122a2f0e577SAndreas Gohr
123a2f0e577SAndreas Gohr        return $perms;
124a2f0e577SAndreas Gohr    }
125a2f0e577SAndreas Gohr}
126