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