1<?php
2
3require_once(__DIR__ . '/../vendor/autoload.php');
4
5use ComboStrap\Identity;
6
7
8/**
9 * Suppress the Javascript of Dokuwiki not needed on a page show
10 */
11class action_plugin_combo_js extends DokuWiki_Action_Plugin
12{
13
14    // Because the javascript is a second request call
15    // we don't known if we are in the admin or public interface
16    // we add them to the js href the below query key
17    const ACCESS_PROPERTY_KEY = 'wcacc';
18    // For the public (no user, no admin)
19    const ACCESS_PROPERTY_VALUE_PUBLIC = 'public';
20
21    /**
22     * Registers our handler for the MANIFEST_SEND event
23     * https://www.dokuwiki.org/devel:event:js_script_list
24     * manipulate the list of JavaScripts that will be concatenated
25     * @param Doku_Event_Handler $controller
26     */
27    public function register(Doku_Event_Handler $controller)
28    {
29
30        // To add the act query string to tweak the cache at the end. ie http://localhost:81/lib/exe/js.php?t=strap&tseed=3cf44e7&act=show
31        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_header');
32
33        // To get only the script that we need
34        $controller->register_hook('JS_SCRIPT_LIST', 'AFTER', $this, 'handle_js');
35
36    }
37
38    /**
39     * Add a query string to the js.php link when it's a SHOW action
40     * @param Doku_Event $event
41     */
42    public function handle_header(Doku_Event &$event, $param)
43    {
44
45        // If there is no user
46        if (Identity::isAnonymous()) {
47            $scripts = &$event->data['script'];
48            foreach ($scripts as &$script) {
49                $src = $script['src'] ?? null;
50                if ($src === null) {
51                    continue;
52                }
53                $pos = strpos($src, 'js.php');
54                if ($pos !== false) {
55                    $script['src'] = $src . '&' . self::ACCESS_PROPERTY_KEY . '=' . self::ACCESS_PROPERTY_VALUE_PUBLIC;
56                }
57            }
58        }
59
60
61    }
62
63    /**
64     * This is to handle the HTTP call
65     * /lib/exe/js.php?t=strap&tseed=2eb19bd2d8991a9bb11366d787d4225e&wcacc=public
66     * @param Doku_Event $event
67     * @param            $param
68     */
69    public function handle_js(Doku_Event &$event, $param)
70    {
71
72        /**
73         * The directory separator is hard written
74         * in the list {@link js_out()}
75         * We then use it here also as hardcoded
76         */
77        $directorySeparatorInDokuwikiList = "/";
78
79        // It was added by the TPL_METAHEADER_OUTPUT handler (ie function handle_header)
80        $access = $_GET[self::ACCESS_PROPERTY_KEY] ?? null;
81        if ($access == self::ACCESS_PROPERTY_VALUE_PUBLIC) {
82
83            // The directory path for the internal dokuwiki script
84            $dokuScriptPath = $directorySeparatorInDokuwikiList . 'lib' . $directorySeparatorInDokuwikiList . 'scripts' . $directorySeparatorInDokuwikiList;
85            // The directory path for the plugin script (we need to keep them)
86            $dokuPluginPath = $directorySeparatorInDokuwikiList . 'lib' . $directorySeparatorInDokuwikiList . 'plugins' . $directorySeparatorInDokuwikiList;
87
88            // Script that we want on the show page
89            $showPageScripts =
90                [
91                    0 => $dokuScriptPath . "qsearch.js", // for the search bar
92                    1 => $dokuScriptPath . "script.js",
93                    2 => $dokuScriptPath . "hotkeys.js",
94                    3 => $dokuScriptPath . "locktimer.js", // Use in js.php - dw_locktimer
95                    4 => $dokuScriptPath . "helpers.js", // substr_replace use in qsearch.php
96                    5 => 'conf' . $directorySeparatorInDokuwikiList . 'userscript.js',
97                    6 => $dokuScriptPath . "cookie.js", // plugin may depend on this library such as styling (lib/plugins/styling/script.js)
98                    7 => $dokuScriptPath . "jquery/jquery.cookie.js", // cookie.js depends on it
99                    8 => $dokuScriptPath . "behaviour.js", // contains dw_show that is used by the tree.js tree
100                    9 => $dokuScriptPath . "tree.js", // used by the index.js tree
101                    10 => $dokuScriptPath . "index.js" // index page
102                ];
103
104            $scriptsToKeep = array();
105
106            foreach ($event->data as $scriptPath) {
107                $posPluginPath = strpos($scriptPath, $dokuPluginPath);
108                if ($posPluginPath !== false) {
109                    // This is a plugin script, we keep it
110                    $scriptsToKeep[] = $scriptPath;
111                } else {
112
113                    foreach ($showPageScripts as $showPageScript) {
114
115                        $posShowPage = strpos($scriptPath, $showPageScript);
116                        if ($posShowPage !== false) {
117                            // This is a script needed on the show page
118                            $scriptsToKeep[] = $scriptPath;
119                        }
120                    }
121                }
122            }
123
124            $event->data = $scriptsToKeep;
125
126        }
127    }
128
129}
130