xref: /plugin/combo/action/pageprotection.php (revision 32b85071e019dd3646a67c17fac4051338e495eb)
1<?php
2
3
4use ComboStrap\Auth;
5use ComboStrap\LowQualityPage;
6use ComboStrap\Page;
7use ComboStrap\PageProtection;
8
9require_once(__DIR__ . '/../class/LowQualityPage.php');
10require_once(__DIR__ . '/../class/PageProtection.php');
11
12/**
13 *
14 */
15class action_plugin_combo_pageprotection extends DokuWiki_Action_Plugin
16{
17
18
19    public function register(Doku_Event_Handler $controller)
20    {
21
22
23        $securityConf = $this->getConf(PageProtection::CONF_PAGE_PROTECTION_MODE);
24        if (empty($securityConf)) {
25            $securityConf = $this->getConf(LowQualityPage::CONF_LOW_QUALITY_PAGE_PROTECTION_MODE);
26        }
27        if ($securityConf == PageProtection::CONF_VALUE_HIDDEN) {
28            /**
29             * https://www.dokuwiki.org/devel:event:pageutils_id_hidepage
30             */
31            $controller->register_hook('PAGEUTILS_ID_HIDEPAGE', 'BEFORE', $this, 'handleHiddenCheck', array());
32        } else {
33            /**
34             * https://www.dokuwiki.org/devel:event:auth_acl_check
35             */
36            $controller->register_hook('AUTH_ACL_CHECK', 'AFTER', $this, 'handleAclCheck', array());
37        }
38
39        /**
40         * https://www.dokuwiki.org/devel:event:search_query_pagelookup
41         */
42        $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'handleSearchPageLookup', array());
43
44        /**
45         * https://www.dokuwiki.org/devel:event:search_query_fullpage
46         */
47        $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'handleSearchFullPage', array());
48        /**
49         * https://www.dokuwiki.org/devel:event:feed_data_process
50         */
51        $controller->register_hook('FEED_DATA_PROCESS', 'AFTER', $this, 'handleRssFeed', array());
52
53        /**
54         * Add logged in
55         */
56        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'handleAnonymousJsIndicator');
57
58
59    }
60
61    /**
62     * Set a low page has hidden
63     * @param $event
64     * @param $param
65     */
66    function handleHiddenCheck(&$event, $param)
67    {
68
69        $id = $event->data['id'];
70        $page = new Page($id);
71
72        if ($page->isProtected()) {
73            $event->data['hidden'] = true;
74        }
75
76    }
77
78    /**
79     * Make the authorization to NONE for low page
80     * @param $event
81     * @param $param
82     */
83    function handleAclCheck(&$event, $param)
84    {
85
86        $id = $event->data['id'];
87        /**
88         * ACL ID have the root form
89         */
90        $cleanId = cleanID($id);
91        if (Page::isDirectoryId($id)){
92
93            return;
94
95        } else {
96
97            $user = $event->data['user'];
98            $page = new Page($cleanId);
99            if ($page->isProtected($user)) {
100                $event->result = AUTH_NONE;
101            }
102
103        }
104
105    }
106
107    /**
108     * @param $event
109     * @param $param
110     * The autocomplete do a search on page name
111     */
112    function handleSearchPageLookup(&$event, $param)
113    {
114        $this->excludePageFromSearch($event);
115    }
116
117    /**
118     * @param $event
119     * @param $param
120     * The search page do a search on page name
121     */
122    function handleSearchFullPage(&$event, $param)
123    {
124
125        $this->excludePageFromSearch($event);
126    }
127
128    /**
129     *
130     * @param $event
131     * @param $param
132     * The Rss
133     * https://www.dokuwiki.org/syndication
134     * Example
135     * https://example.com/feed.php?type=rss2&num=5
136     */
137    function handleRssFeed(&$event, $param)
138    {
139        $this->excludePageFromSearch($event);
140    }
141
142    /**
143     * @param $event
144     */
145    private
146    function excludePageFromSearch(&$event)
147    {
148
149        $result = $event->result;
150        /**
151         * The value is always an array
152         * but as we got this error:
153         * ```
154         * array_keys() expects parameter 1 to be array
155         * ```
156         */
157        if (is_array($result)) {
158            foreach (array_keys($result) as $idx) {
159                $page = new Page($idx);
160                if ($page->isProtected()) {
161                    unset($result[$idx]);
162                }
163            }
164        }
165
166    }
167
168    function handleAnonymousJsIndicator(&$event, $param)
169    {
170
171        global $JSINFO;
172        $JSINFO[PageProtection::JS_IS_PUBLIC_NAVIGATION_INDICATOR] = !Auth::isLoggedIn();
173
174
175    }
176
177}
178