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