xref: /plugin/combo/action/js.php (revision 5f891b7e09648e05e78f5882f3fdde1e9df9b0f1)
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        // It was added by the TPL_METAHEADER_OUTPUT handler (ie function handle_header)
70        $access = $_GET[self::ACCESS_PROPERTY_KEY];
71        if ($access == self::ACCESS_PROPERTY_VALUE_PUBLIC) {
72
73            // The directory path for the internal dokuwiki script
74            $dokuScriptPath = DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR;
75            // The directory path for the plugin script (we need to keep them)
76            $dokuPluginPath = DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR;
77
78            // Script that we want on the show page
79            $showPageScripts =
80                [
81                    0 => $dokuScriptPath . "qsearch.js", // for the search bar
82                    1 => $dokuScriptPath . "script.js",
83                    2 => $dokuScriptPath . "hotkeys.js",
84                    3 => $dokuScriptPath . "locktimer.js", // Use in js.php - dw_locktimer
85                    4 => $dokuScriptPath . "helpers.js", // substr_replace use in qsearch.php
86                    5 => 'conf' . DIRECTORY_SEPARATOR . 'userscript.js'
87                ];
88
89            $scriptsToKeep = array();
90
91            foreach ($event->data as $scriptPath) {
92                $posPluginPath = strpos($scriptPath, $dokuPluginPath);
93                if ($posPluginPath !== false) {
94                    // This is a plugin script, we keep it
95                    $scriptsToKeep[] = $scriptPath;
96                } else {
97
98                    foreach ($showPageScripts as $showPageScript) {
99
100                        $posShowPage = strpos($scriptPath, $showPageScript);
101                        if ($posShowPage !== false) {
102                            // This is a script needed on the show page
103                            $scriptsToKeep[] = $scriptPath;
104                        }
105                    }
106                }
107            }
108
109            $event->data = $scriptsToKeep;
110
111        }
112    }
113
114}
115
116