xref: /plugin/siteexport/preload.php (revision 1fb44fd637149ebabb965227f56759eaacd3af8e)
17d101cc1SGerry Weißbach<?php
2e5c11dbfSScrutinizer Auto-Fixerif (!defined('DOKU_INC')) {
3a8c17ab5Si-net /// software    define('DOKU_INC', /** @scrutinizer ignore-type */ realpath(dirname(__FILE__) . '/../../../') . '/');
4e5c11dbfSScrutinizer Auto-Fixer}
57d101cc1SGerry Weißbach
68c436215SGerry Weißbachif ( file_exists(DOKU_INC . 'inc/plugincontroller.class.php') ) {
7a8c17ab5Si-net /// software    include_once(DOKU_INC . 'inc/plugincontroller.class.php');
88c436215SGerry Weißbach    class _preload_plugin_siteexport_controller extends Doku_Plugin_Controller {}
98c436215SGerry Weißbach} else if ( file_exists(DOKU_INC . 'inc/Extension/PluginController.php') ) {
108c436215SGerry Weißbach    include_once(DOKU_INC . 'inc/Extension/PluginController.php');
118c436215SGerry Weißbach    class _preload_plugin_siteexport_controller extends \dokuwiki\Extension\PluginController {}
128c436215SGerry Weißbach} else if ( class_exists( "\dokuwiki\Extension\PluginController", true ) ) {
138c436215SGerry Weißbach    class _preload_plugin_siteexport_controller extends \dokuwiki\Extension\PluginController {}
148c436215SGerry Weißbach}
157d101cc1SGerry Weißbach
167d101cc1SGerry Weißbachclass preload_plugin_siteexport {
177d101cc1SGerry Weißbach
18d98cce67SGerry Weißbach    public $error;
191d35ab9dSGerry Weißbach
20a8c17ab5Si-net /// software    public function __register_template() {
217d101cc1SGerry Weißbach
2216c5261cSGerry Weißbach        global $conf;
230d554ed2SGerry Weißbach        $tempREQUEST = array();
2416c5261cSGerry Weißbach
257d101cc1SGerry Weißbach        if (!empty($_REQUEST['q'])) {
267d101cc1SGerry Weißbach
271a086b3fSGerry Weißbach            $tempREQUEST = (array) json_decode(stripslashes($_REQUEST['q']), true);
287d101cc1SGerry Weißbach
290d554ed2SGerry Weißbach        } else if (array_key_exists('template', $_REQUEST)) {
307d101cc1SGerry Weißbach            $tempREQUEST = $_REQUEST;
3116c5261cSGerry Weißbach        } else if (preg_match("/(js|css)\.php$/", $_SERVER['SCRIPT_NAME']) && isset($_SERVER['HTTP_REFERER'])) {
3216c5261cSGerry Weißbach            // this is a css or script, nothing before matched and we have a referrer.
3316c5261cSGerry Weißbach            // lets asume we came from the dokuwiki page.
3416c5261cSGerry Weißbach
3516c5261cSGerry Weißbach            // Parse the Referrer URL
3616c5261cSGerry Weißbach            $url = parse_url($_SERVER['HTTP_REFERER']);
37641ccffdSGerry Weißbach            if (isset($url['query'])) {
3816c5261cSGerry Weißbach                parse_str($url['query'], $tempREQUEST);
39641ccffdSGerry Weißbach            }
407d101cc1SGerry Weißbach        } else {
417d101cc1SGerry Weißbach            return;
427d101cc1SGerry Weißbach        }
437d101cc1SGerry Weißbach
447d101cc1SGerry Weißbach        // define Template baseURL
45cfda2f5bSGerry Weißbach        $newTemplate = array_key_exists('template', $tempREQUEST) ? $tempREQUEST['template'] : null;
4636db4d82SGerry Weißbach        // Make sure, that the template is set and the basename is equal to the template, alas there are no path definitions. see #48
4736db4d82SGerry Weißbach        if (empty($newTemplate) || basename($newTemplate) != $newTemplate) { return; }
4836db4d82SGerry Weißbach        $tplDir = DOKU_INC . 'lib/tpl/' . $newTemplate;
4936db4d82SGerry Weißbach        // 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*1fb44fd6SGerry Weißbach        // here we will have issues using farmer plugin or docker container, since the path is very different.
51*1fb44fd6SGerry Weißbach        // the tpldir can be different when the realpath is looked up, but should still exist.
52*1fb44fd6SGerry Weißbach        $tplDir = realpath($tplDir);
53*1fb44fd6SGerry Weißbach        if ($tplDir === false || !is_dir($tplDir)) { return; }
547d101cc1SGerry Weißbach
5506337b60SGerry Weißbach        // Set hint for Dokuwiki_Started event
567d101cc1SGerry Weißbach        if (!defined('SITEEXPORT_TPL'))        define('SITEEXPORT_TPL', $tempREQUEST['template']);
5706337b60SGerry Weißbach
5806337b60SGerry Weißbach        // define baseURL
5906337b60SGerry Weißbach        // This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
6006337b60SGerry Weißbach        /* **************************************************************************************** */
6106337b60SGerry Weißbach        if (!defined('DOKU_REL')) define('DOKU_REL', getBaseURL(false));
6206337b60SGerry Weißbach        if (!defined('DOKU_URL')) define('DOKU_URL', getBaseURL(true));
6306337b60SGerry Weißbach        if (!defined('DOKU_BASE')) {
649210c477SGerry Weißbach            if (isset($conf['canonical'])) {
6506337b60SGerry Weißbach                define('DOKU_BASE', DOKU_URL);
6606337b60SGerry Weißbach            } else {
6706337b60SGerry Weißbach                define('DOKU_BASE', DOKU_REL);
6806337b60SGerry Weißbach            }
6906337b60SGerry Weißbach        }
7006337b60SGerry Weißbach
7106337b60SGerry Weißbach        // This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
7206337b60SGerry Weißbach        if (!defined('DOKU_TPL')) define('DOKU_TPL', (empty($tempREQUEST['base']) ? DOKU_BASE : $tempREQUEST['base']) . 'lib/tpl/' . $tempREQUEST['template'] . '/');
737d101cc1SGerry Weißbach        if (!defined('DOKU_TPLINC')) define('DOKU_TPLINC', $tplDir);
7406337b60SGerry Weißbach        /* **************************************************************************************** */
757d101cc1SGerry Weißbach    }
767d101cc1SGerry Weißbach
77a8c17ab5Si-net /// software    public function __temporary_disable_plugins() {
787d101cc1SGerry Weißbach
797d101cc1SGerry Weißbach        // Check for siteexport - otherwise this does not matter.
807d101cc1SGerry Weißbach        if (empty($_REQUEST['do']) || $_REQUEST['do'] != 'siteexport') {
817d101cc1SGerry Weißbach            return;
827d101cc1SGerry Weißbach        }
837d101cc1SGerry Weißbach
8465e0d1bfSGerry Weißbach        // check for css and js  ... only disable in that case.
857d101cc1SGerry Weißbach        if (!preg_match("/(js|css)\.php$/", $_SERVER['SCRIPT_NAME'])) {
867d101cc1SGerry Weißbach            return;
877d101cc1SGerry Weißbach        }
887d101cc1SGerry Weißbach
897d101cc1SGerry Weißbach        //        print "removing plugins ";
907d101cc1SGerry Weißbach        $_GET['purge'] = 'purge'; //activate purging
917d101cc1SGerry Weißbach        $_POST['purge'] = 'purge'; //activate purging
927d101cc1SGerry Weißbach        $_REQUEST['purge'] = 'purge'; //activate purging
937d101cc1SGerry Weißbach
947d101cc1SGerry Weißbach        $_SERVER['HTTP_HOST'] = 'siteexport.js'; // fake everything in here
957d101cc1SGerry Weißbach
96641ccffdSGerry Weißbach        // require_once(DOKU_INC.'inc/plugincontroller.class.php'); // Have to get the pluginutils already
97641ccffdSGerry Weißbach        // require_once(DOKU_INC.'inc/pluginutils.php'); // Have to get the pluginutils already
987d101cc1SGerry Weißbach        $this->__disablePlugins();
997d101cc1SGerry Weißbach    }
1007d101cc1SGerry Weißbach
101a8c17ab5Si-net /// software    private function __disablePlugins() {
102641ccffdSGerry Weißbach        global $plugin_controller_class;
1037d101cc1SGerry Weißbach        $plugin_controller_class = 'preload_plugin_siteexport_controller';
1047d101cc1SGerry Weißbach    }
1057d101cc1SGerry Weißbach
106a8c17ab5Si-net /// software    public function __create_preload_function() {
1077d101cc1SGerry Weißbach
1087d101cc1SGerry Weißbach        $PRELOADFILE = DOKU_INC . 'inc/preload.php';
1097d101cc1SGerry Weißbach        $CURRENTFILE = 'DOKU_INC' . " . 'lib/plugins/siteexport/preload.php'";
1107d101cc1SGerry Weißbach        $CONTENT = <<<OUTPUT
1117d101cc1SGerry Weißbach/* SITE EXPORT *********************************************************** */
1127d101cc1SGerry Weißbach    if ( file_exists($CURRENTFILE) ) {
1137d101cc1SGerry Weißbach        include_once($CURRENTFILE);
1147d101cc1SGerry Weißbach        \$siteexport_preload = new preload_plugin_siteexport();
1157d101cc1SGerry Weißbach        \$siteexport_preload->__register_template();
1167d101cc1SGerry Weißbach        \$siteexport_preload->__temporary_disable_plugins();
1177d101cc1SGerry Weißbach        unset(\$siteexport_preload);
1187d101cc1SGerry Weißbach    }
1197d101cc1SGerry Weißbach/* SITE EXPORT END *********************************************************** */
1207d101cc1SGerry Weißbach
1217d101cc1SGerry WeißbachOUTPUT;
1227d101cc1SGerry Weißbach
1237d101cc1SGerry Weißbach        if (file_exists($PRELOADFILE)) {
1247d101cc1SGerry Weißbach
1257d101cc1SGerry Weißbach            if (!is_readable($PRELOADFILE)) {
126996c253aSGerry Weißbach                $this->error = "Preload File locked. It exists, but it can't be read.";
127996c253aSGerry Weißbach                msg($this->error, -1);
1287d101cc1SGerry Weißbach                return false;
1297d101cc1SGerry Weißbach            }
1307d101cc1SGerry Weißbach
1317d101cc1SGerry Weißbach            if (!is_writeable($PRELOADFILE)) {
132996c253aSGerry Weißbach                $this->error = "Preload File locked. It exists and is readable, but it can't be written.";
133996c253aSGerry Weißbach                msg($this->error, -1);
1347d101cc1SGerry Weißbach                return false;
1357d101cc1SGerry Weißbach            }
1367d101cc1SGerry Weißbach
1377d101cc1SGerry Weißbach            $fileContent = file($PRELOADFILE);
138a8c17ab5Si-net /// software            if (!strstr(implode("", $fileContent ?: array()), $CONTENT)) {
1397d101cc1SGerry Weißbach
1407d101cc1SGerry Weißbach                $fp = fopen($PRELOADFILE, "a");
141a8c17ab5Si-net /// software                if ( !$fp ) { return false; }
142996c253aSGerry Weißbach                if (!strstr(implode("", $fileContent), "<?")) {
143996c253aSGerry Weißbach                    fputs($fp, "<?php\n");
144996c253aSGerry Weißbach                }
1457d101cc1SGerry Weißbach                fputs($fp, "\n" . $CONTENT);
1467d101cc1SGerry Weißbach                fclose($fp);
1477d101cc1SGerry Weißbach            }
1487d101cc1SGerry Weißbach
1497d101cc1SGerry Weißbach            return true;
1507d101cc1SGerry Weißbach
1517d101cc1SGerry Weißbach        } else if (is_writeable(DOKU_INC . 'inc/')) {
1527d101cc1SGerry Weißbach
1537d101cc1SGerry Weißbach            $fp = fopen($PRELOADFILE, "w");
154a8c17ab5Si-net /// software            if ( !$fp ) { return false; }
155a8c17ab5Si-net /// software            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");
1567d101cc1SGerry Weißbach            fputs($fp, $CONTENT);
1577d101cc1SGerry Weißbach            fputs($fp, "// end auto-generated content\n\n");
1587d101cc1SGerry Weißbach            fclose($fp);
1597d101cc1SGerry Weißbach
1607d101cc1SGerry Weißbach            return true;
1617d101cc1SGerry Weißbach        }
1627d101cc1SGerry Weißbach
163996c253aSGerry Weißbach        $this->error = "Could not create/modify preload.php. Please check the write permissions for your DokuWiki/inc directory.";
164996c253aSGerry Weißbach        msg($this->error, -1);
1657d101cc1SGerry Weißbach        return false;
1667d101cc1SGerry Weißbach    }
1677d101cc1SGerry Weißbach
1687d101cc1SGerry Weißbach}
1697d101cc1SGerry Weißbach
1707d101cc1SGerry Weißbach// return a custom plugin list
1718c436215SGerry Weißbachclass preload_plugin_siteexport_controller extends _preload_plugin_siteexport_controller {
1727d101cc1SGerry Weißbach
173a8c17ab5Si-net /// software    protected $tmp_plugins = array();
174a8c17ab5Si-net /// software
17565e0d1bfSGerry Weißbach    /**
17665e0d1bfSGerry Weißbach     * Setup disabling
17765e0d1bfSGerry Weißbach     */
17865e0d1bfSGerry Weißbach    public function __construct() {
179f161d4fbSatisne        global $INPUT;
180f161d4fbSatisne
18165e0d1bfSGerry Weißbach        parent::__construct();
1827d101cc1SGerry Weißbach
1832e56ccbaSGerry Weißbach        $disabledPlugins = array();
1842e56ccbaSGerry Weißbach
1852e56ccbaSGerry Weißbach        // support of old syntax
186f161d4fbSatisne        if (is_array($INPUT->arr('diPlu'))) {
187f161d4fbSatisne            $disabledPlugins = $INPUT->arr('diPlu');
1882e56ccbaSGerry Weißbach        }
1892e56ccbaSGerry Weißbach
190641ccffdSGerry Weißbach        if (!empty($_REQUEST['diInv']))
191641ccffdSGerry Weißbach        {
192641ccffdSGerry Weißbach            $allPlugins = array();
193641ccffdSGerry Weißbach            foreach ($this->tmp_plugins as $plugin => $enabled) { // All plugins
194641ccffdSGerry Weißbach                // check for CSS or JS
195641ccffdSGerry Weißbach                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; }
196641ccffdSGerry Weißbach                $allPlugins[] = $plugin;
197641ccffdSGerry Weißbach            }
198f161d4fbSatisne            $disabledPlugins = empty($INPUT->arr('diPlu')) ? $allPlugins : array_diff($allPlugins, $INPUT->arr('diPlu'));
1992e56ccbaSGerry Weißbach        }
2002e56ccbaSGerry Weißbach
2012e56ccbaSGerry Weißbach        // if this is defined, it overrides the settings made above. obviously.
202f161d4fbSatisne        $disabledPlugins = $INPUT->arr('disableplugin', $disabledPlugins, true);
2032e56ccbaSGerry Weißbach
20465e0d1bfSGerry Weißbach        foreach ($disabledPlugins as $plugin) {
20565e0d1bfSGerry Weißbach            $this->disable($plugin);
2067d101cc1SGerry Weißbach        }
207c7c6980aSGerry Weißbach
208c7c6980aSGerry Weißbach        // always enabled - JS and CSS will be cut out later.
209c7c6980aSGerry Weißbach        $this->enable('siteexport');
2107d101cc1SGerry Weißbach    }
2117d101cc1SGerry Weißbach
21265e0d1bfSGerry Weißbach    /**
21365e0d1bfSGerry Weißbach     * Disable the plugin
21465e0d1bfSGerry Weißbach     *
21565e0d1bfSGerry Weißbach     * @param string $plugin name of plugin
21665e0d1bfSGerry Weißbach     * @return bool; true allways.
21765e0d1bfSGerry Weißbach     */
21865e0d1bfSGerry Weißbach    public function disable($plugin) {
21965e0d1bfSGerry Weißbach        $this->tmp_plugins[$plugin] = 0;
22065e0d1bfSGerry Weißbach        return true;
2217d101cc1SGerry Weißbach    }
222c7c6980aSGerry Weißbach
22368901b7bSGerry Weißbach    /**
22468901b7bSGerry Weißbach     * Enable the plugin
22568901b7bSGerry Weißbach     *
22668901b7bSGerry Weißbach     * @param string $plugin name of plugin
22768901b7bSGerry Weißbach     * @return bool; true allways.
22868901b7bSGerry Weißbach     */
22968901b7bSGerry Weißbach    public function enable($plugin) {
23068901b7bSGerry Weißbach        $this->tmp_plugins[$plugin] = 1;
23168901b7bSGerry Weißbach        return true;
23268901b7bSGerry Weißbach    }
233c7c6980aSGerry Weißbach
234c7c6980aSGerry Weißbach    public function hasSiteexportHeaders() {
235c7c6980aSGerry Weißbach        $headers = function_exists('getallheaders') ? getallheaders() : null;
236a0417606SGerry Weißbach        return is_array($headers) && array_key_exists('X-Site-Exporter', $headers) /* && $headers['X-Site-Exporter'] = getSecurityToken() */;
237c7c6980aSGerry Weißbach    }
238c7c6980aSGerry Weißbach
239c7c6980aSGerry Weißbach    /**
2407fa31378SGerry Weißbach     * Filter the List of Plugins for the siteexport plugin
2417fa31378SGerry Weißbach     */
2427fa31378SGerry Weißbach    private function isSiteexportPlugin($item) {
2437fa31378SGerry Weißbach        return $item != 'siteexport';
2447fa31378SGerry Weißbach    }
2457fa31378SGerry Weißbach
2467fa31378SGerry Weißbach    /**
247c7c6980aSGerry Weißbach     * Get the list of plugins, bute remove Siteexport from Style and
248c7c6980aSGerry Weißbach     * JS if in export Mode
249c7c6980aSGerry Weißbach     */
250c7c6980aSGerry Weißbach    public function getList($type = '', $all = false) {
251c7c6980aSGerry Weißbach        $plugins = parent::getList($type, $all);
252c7c6980aSGerry Weißbach
253a8c17ab5Si-net /// software        list(,, $caller) = debug_backtrace();
254c7c6980aSGerry Weißbach        if ($this->hasSiteexportHeaders() && $caller != null && preg_match("/^(js|css)_/", $caller['function']) && preg_match("/(js|css)\.php$/", $caller['file'])) {
2557fa31378SGerry Weißbach            $plugins = array_filter($plugins, array($this, 'isSiteexportPlugin'));
256c7c6980aSGerry Weißbach        }
257c7c6980aSGerry Weißbach
258c7c6980aSGerry Weißbach        return $plugins;
259c7c6980aSGerry Weißbach    }
2607d101cc1SGerry Weißbach}
261