xref: /plugin/combo/action/js.php (revision 8b8569b7384a60bf4f631e3aa3bd7f9aab461558)
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                $pos = strpos($script['src'], 'js.php');
50                if ($pos !== false) {
51                    $script['src'] = $script['src'] . '&' . self::ACCESS_PROPERTY_KEY . '=' . self::ACCESS_PROPERTY_VALUE_PUBLIC . '';
52                }
53            }
54        }
55
56
57    }
58
59    /**
60     * This is to handle the HTTP call
61     * /lib/exe/js.php?t=strap&tseed=2eb19bd2d8991a9bb11366d787d4225e&wcacc=public
62     * @param Doku_Event $event
63     * @param            $param
64     */
65    public function handle_js(Doku_Event &$event, $param)
66    {
67
68        /**
69         * The directory separator is hard written
70         * in the list {@link js_out()}
71         * We then use it here also as hardcoded
72         */
73        $directorySeparatorInDokuwikiList = "/";
74
75        // It was added by the TPL_METAHEADER_OUTPUT handler (ie function handle_header)
76        $access = $_GET[self::ACCESS_PROPERTY_KEY];
77        if ($access == self::ACCESS_PROPERTY_VALUE_PUBLIC) {
78
79            // The directory path for the internal dokuwiki script
80            $dokuScriptPath = $directorySeparatorInDokuwikiList . 'lib' . $directorySeparatorInDokuwikiList . 'scripts' . $directorySeparatorInDokuwikiList;
81            // The directory path for the plugin script (we need to keep them)
82            $dokuPluginPath = $directorySeparatorInDokuwikiList . 'lib' . $directorySeparatorInDokuwikiList . 'plugins' . $directorySeparatorInDokuwikiList;
83
84            // Script that we want on the show page
85            $showPageScripts =
86                [
87                    0 => $dokuScriptPath . "qsearch.js", // for the search bar
88                    1 => $dokuScriptPath . "script.js",
89                    2 => $dokuScriptPath . "hotkeys.js",
90                    3 => $dokuScriptPath . "locktimer.js", // Use in js.php - dw_locktimer
91                    4 => $dokuScriptPath . "helpers.js", // substr_replace use in qsearch.php
92                    5 => 'conf' . $directorySeparatorInDokuwikiList . 'userscript.js',
93                    6 => $dokuScriptPath . "cookie.js", // plugin may depend on this library such as styling (lib/plugins/styling/script.js)
94                    7 => $dokuScriptPath . "jquery/jquery.cookie.js" // cookie.js depends on it
95                ];
96
97            $scriptsToKeep = array();
98
99            foreach ($event->data as $scriptPath) {
100                $posPluginPath = strpos($scriptPath, $dokuPluginPath);
101                if ($posPluginPath !== false) {
102                    // This is a plugin script, we keep it
103                    $scriptsToKeep[] = $scriptPath;
104                } else {
105
106                    foreach ($showPageScripts as $showPageScript) {
107
108                        $posShowPage = strpos($scriptPath, $showPageScript);
109                        if ($posShowPage !== false) {
110                            // This is a script needed on the show page
111                            $scriptsToKeep[] = $scriptPath;
112                        }
113                    }
114                }
115            }
116
117            $event->data = $scriptsToKeep;
118
119        }
120    }
121
122}
123
124