1<?php
2/**
3* DokuWiki Plugin sneakyindexfix (Action Component)
4*
5*
6*
7* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8* @author lisps
9*/
10if (!defined('DOKU_INC')) die();
11if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12require_once (DOKU_PLUGIN . 'action.php');
13
14class action_plugin_sneakyindexfix extends DokuWiki_Action_Plugin {
15
16    /**
17     * Register the eventhandlers
18     */
19    function register(Doku_Event_Handler $controller) {
20        $controller->register_hook('AUTH_ACL_CHECK', 'AFTER', $this, '_acl_check');
21    }
22
23
24    /**
25     *
26     * only if sneaky_index is enabled
27     * if namspace with AUTH_NONE -> check for acl definition
28     * with ACL > AUTH_NONE in deeper namespaces and manipulate ACL
29     * for this namespace to AUTH_READ
30     *
31     */
32    function _acl_check(Doku_Event $event) {
33        if($event->result !== AUTH_NONE) return;
34        $data = $event->data;
35
36        /*copy auth_aclcheck_cb start*/
37        $id     = & $data['id'];
38        $user   = & $data['user'];
39        $groups = & $data['groups'];
40
41        global $conf;
42        global $AUTH_ACL;
43        /* @var DokuWiki_Auth_Plugin $auth */
44        global $auth;
45        /*copy end*/
46
47
48        if(noNS($id) !== '*') return; //only namespacecheck
49        if(!$conf['sneaky_index']) return; //we only need this when sneaky_index is enabled
50
51        /*copy auth_aclcheck_cb start*/
52        if(!$auth) return AUTH_NONE;
53
54        //make sure groups is an array
55        if(!is_array($groups)) $groups = array();
56
57        if(!$auth->isCaseSensitive()) {
58            $user   = utf8_strtolower($user);
59            $groups = array_map('utf8_strtolower', $groups);
60        }
61        $user   = auth_nameencode($auth->cleanUser($user));
62        $groups = array_map(array($auth, 'cleanGroup'), (array) $groups);
63
64
65        //prepend groups with @ and nameencode
66        foreach($groups as &$group) {
67            $group = '@'.auth_nameencode($group);
68        }
69
70        $ns   = getNS($id);
71        $perm = -1;
72
73        //add ALL group
74        $groups[] = '@ALL';
75
76        //add User
77        if($user) $groups[] = $user;
78        /*copy end*/
79
80        //check for deeper acl definition
81        $matches = preg_grep('/^'.preg_quote($ns, '/').':[\w:\*]+[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL);
82        if(count($matches)) {
83            foreach($matches as $match) {
84                $match = preg_replace('/#.*$/', '', $match); //ignore comments
85                $acl   = preg_split('/[ \t]+/', $match);
86                if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') {
87                    $acl[1] = utf8_strtolower($acl[1]);
88                }
89                if(!in_array($acl[1], $groups)) {
90                    continue;
91                }
92                if($acl[2] > AUTH_NONE) {
93                    $event->result =  true;
94                    return true; //set read access for this namespace
95                }
96            }
97
98        }
99    }
100
101}
102