1<?php
2if (!defined('DOKU_INC')) {
3    define('DOKU_INC', /** @scrutinizer ignore-type */ realpath(dirname(__FILE__) . '/../../../') . '/');
4}
5
6if ( file_exists(DOKU_INC . 'inc/plugincontroller.class.php') ) {
7    include_once(DOKU_INC . 'inc/plugincontroller.class.php');
8    class _preload_plugin_siteexport_controller extends Doku_Plugin_Controller {}
9} else if ( file_exists(DOKU_INC . 'inc/Extension/PluginController.php') ) {
10    include_once(DOKU_INC . 'inc/Extension/PluginController.php');
11    class _preload_plugin_siteexport_controller extends \dokuwiki\Extension\PluginController {}
12} else if ( class_exists( "\dokuwiki\Extension\PluginController", true ) ) {
13    class _preload_plugin_siteexport_controller extends \dokuwiki\Extension\PluginController {}
14}
15
16class preload_plugin_siteexport {
17
18    public $error;
19
20    public function __register_template() {
21
22        global $conf;
23        $tempREQUEST = array();
24
25        if (!empty($_REQUEST['q'])) {
26
27            $tempREQUEST = (array) json_decode(stripslashes($_REQUEST['q']), true);
28
29        } else if (array_key_exists('template', $_REQUEST)) {
30            $tempREQUEST = $_REQUEST;
31        } else if (preg_match("/(js|css)\.php$/", $_SERVER['SCRIPT_NAME']) && isset($_SERVER['HTTP_REFERER'])) {
32            // this is a css or script, nothing before matched and we have a referrer.
33            // lets asume we came from the dokuwiki page.
34
35            // Parse the Referrer URL
36            $url = parse_url($_SERVER['HTTP_REFERER']);
37            if (isset($url['query'])) {
38                parse_str($url['query'], $tempREQUEST);
39            }
40        } else {
41            return;
42        }
43
44        // define Template baseURL
45        $newTemplate = array_key_exists('template', $tempREQUEST) ? $tempREQUEST['template'] : null;
46        // Make sure, that the template is set and the basename is equal to the template, alas there are no path definitions. see #48
47        if (empty($newTemplate) || basename($newTemplate) != $newTemplate) { return; }
48        $tplDir = DOKU_INC . 'lib/tpl/' . $newTemplate;
49        // check if the directory is valid, has no more "../" in it and is equal to what we expect. DOKU_INC itself is absolute. see #48
50        if ($tplDir != realpath($tplDir)) { return; }
51
52        // Use fileexists, because realpath is not always right.
53        if (!file_exists($tplDir)) { return; }
54
55        // Set hint for Dokuwiki_Started event
56        if (!defined('SITEEXPORT_TPL'))        define('SITEEXPORT_TPL', $tempREQUEST['template']);
57
58        // define baseURL
59        // This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
60        /* **************************************************************************************** */
61        if (!defined('DOKU_REL')) define('DOKU_REL', getBaseURL(false));
62        if (!defined('DOKU_URL')) define('DOKU_URL', getBaseURL(true));
63        if (!defined('DOKU_BASE')) {
64            if (isset($conf['canonical'])) {
65                define('DOKU_BASE', DOKU_URL);
66            } else {
67                define('DOKU_BASE', DOKU_REL);
68            }
69        }
70
71        // This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
72        if (!defined('DOKU_TPL')) define('DOKU_TPL', (empty($tempREQUEST['base']) ? DOKU_BASE : $tempREQUEST['base']) . 'lib/tpl/' . $tempREQUEST['template'] . '/');
73        if (!defined('DOKU_TPLINC')) define('DOKU_TPLINC', $tplDir);
74        /* **************************************************************************************** */
75    }
76
77    public function __temporary_disable_plugins() {
78
79        // Check for siteexport - otherwise this does not matter.
80        if (empty($_REQUEST['do']) || $_REQUEST['do'] != 'siteexport') {
81            return;
82        }
83
84        // check for css and js  ... only disable in that case.
85        if (!preg_match("/(js|css)\.php$/", $_SERVER['SCRIPT_NAME'])) {
86            return;
87        }
88
89        //        print "removing plugins ";
90        $_GET['purge'] = 'purge'; //activate purging
91        $_POST['purge'] = 'purge'; //activate purging
92        $_REQUEST['purge'] = 'purge'; //activate purging
93
94        $_SERVER['HTTP_HOST'] = 'siteexport.js'; // fake everything in here
95
96        // require_once(DOKU_INC.'inc/plugincontroller.class.php'); // Have to get the pluginutils already
97        // require_once(DOKU_INC.'inc/pluginutils.php'); // Have to get the pluginutils already
98        $this->__disablePlugins();
99    }
100
101    private function __disablePlugins() {
102        global $plugin_controller_class;
103        $plugin_controller_class = 'preload_plugin_siteexport_controller';
104    }
105
106    public function __create_preload_function() {
107
108        $PRELOADFILE = DOKU_INC . 'inc/preload.php';
109        $CURRENTFILE = 'DOKU_INC' . " . 'lib/plugins/siteexport/preload.php'";
110        $CONTENT = <<<OUTPUT
111/* SITE EXPORT *********************************************************** */
112    if ( file_exists($CURRENTFILE) ) {
113        include_once($CURRENTFILE);
114        \$siteexport_preload = new preload_plugin_siteexport();
115        \$siteexport_preload->__register_template();
116        \$siteexport_preload->__temporary_disable_plugins();
117        unset(\$siteexport_preload);
118    }
119/* SITE EXPORT END *********************************************************** */
120
121OUTPUT;
122
123        if (file_exists($PRELOADFILE)) {
124
125            if (!is_readable($PRELOADFILE)) {
126                $this->error = "Preload File locked. It exists, but it can't be read.";
127                msg($this->error, -1);
128                return false;
129            }
130
131            if (!is_writeable($PRELOADFILE)) {
132                $this->error = "Preload File locked. It exists and is readable, but it can't be written.";
133                msg($this->error, -1);
134                return false;
135            }
136
137            $fileContent = file($PRELOADFILE);
138            if (!strstr(implode("", $fileContent ?: array()), $CONTENT)) {
139
140                $fp = fopen($PRELOADFILE, "a");
141                if ( !$fp ) { return false; }
142                if (!strstr(implode("", $fileContent), "<?")) {
143                    fputs($fp, "<?php\n");
144                }
145                fputs($fp, "\n" . $CONTENT);
146                fclose($fp);
147            }
148
149            return true;
150
151        } else if (is_writeable(DOKU_INC . 'inc/')) {
152
153            $fp = fopen($PRELOADFILE, "w");
154            if ( !$fp ) { return false; }
155            fputs($fp, "<?php\n/*\n * Dokuwiki Preload File\n * Auto-generated by Site Export plugin \n * Date: " . (date('Y-m-d H:s:i') ?: "-") . "\n */\n");
156            fputs($fp, $CONTENT);
157            fputs($fp, "// end auto-generated content\n\n");
158            fclose($fp);
159
160            return true;
161        }
162
163        $this->error = "Could not create/modify preload.php. Please check the write permissions for your DokuWiki/inc directory.";
164        msg($this->error, -1);
165        return false;
166    }
167
168}
169
170// return a custom plugin list
171class preload_plugin_siteexport_controller extends _preload_plugin_siteexport_controller {
172
173    protected $tmp_plugins = array();
174
175    /**
176     * Setup disabling
177     */
178    public function __construct() {
179        parent::__construct();
180
181        $disabledPlugins = array();
182
183        // support of old syntax
184        if (is_array($_REQUEST['diPlu'] ?? null)) {
185            $disabledPlugins = $_REQUEST['diPlu'];
186        }
187
188        if (!empty($_REQUEST['diInv']))
189        {
190            $allPlugins = array();
191            foreach ($this->tmp_plugins as $plugin => $enabled) { // All plugins
192                // check for CSS or JS
193                if ($enabled == 1 && !file_exists(DOKU_PLUGIN . "$plugin/script.js") && !file_exists(DOKU_PLUGIN . "$plugin/style.css") && !file_exists(DOKU_PLUGIN . "$plugin/print.css")) { continue; }
194                $allPlugins[] = $plugin;
195            }
196            $disabledPlugins = empty($_REQUEST['diPlu']) ? $allPlugins : array_diff($allPlugins, $_REQUEST['diPlu']);
197        }
198
199        // if this is defined, it overrides the settings made above. obviously.
200        $disabledPlugins = empty($_REQUEST['disableplugin']) ? $disabledPlugins : $_REQUEST['disableplugin'];
201
202        foreach ($disabledPlugins as $plugin) {
203            $this->disable($plugin);
204        }
205
206        // always enabled - JS and CSS will be cut out later.
207        $this->enable('siteexport');
208    }
209
210    /**
211     * Disable the plugin
212     *
213     * @param string $plugin name of plugin
214     * @return bool; true allways.
215     */
216    public function disable($plugin) {
217        $this->tmp_plugins[$plugin] = 0;
218        return true;
219    }
220
221    /**
222     * Enable the plugin
223     *
224     * @param string $plugin name of plugin
225     * @return bool; true allways.
226     */
227    public function enable($plugin) {
228        $this->tmp_plugins[$plugin] = 1;
229        return true;
230    }
231
232    public function hasSiteexportHeaders() {
233        $headers = function_exists('getallheaders') ? getallheaders() : null;
234        return is_array($headers) && array_key_exists('X-Site-Exporter', $headers) /* && $headers['X-Site-Exporter'] = getSecurityToken() */;
235    }
236
237    /**
238     * Filter the List of Plugins for the siteexport plugin
239     */
240    private function isSiteexportPlugin($item) {
241        return $item != 'siteexport';
242    }
243
244    /**
245     * Get the list of plugins, bute remove Siteexport from Style and
246     * JS if in export Mode
247     */
248    public function getList($type = '', $all = false) {
249        $plugins = parent::getList($type, $all);
250
251        list(,, $caller) = debug_backtrace();
252        if ($this->hasSiteexportHeaders() && $caller != null && preg_match("/^(js|css)_/", $caller['function']) && preg_match("/(js|css)\.php$/", $caller['file'])) {
253            $plugins = array_filter($plugins, array($this, 'isSiteexportPlugin'));
254        }
255
256        return $plugins;
257    }
258}
259