xref: /plugin/siteexport/preload.php (revision a041760617ee7b8fff65209b34790d95fd0df3e5)
17d101cc1SGerry Weißbach<?php
2e5c11dbfSScrutinizer Auto-Fixerif (!defined('DOKU_INC')) {
3e5c11dbfSScrutinizer Auto-Fixer    define('DOKU_INC', realpath(dirname(__FILE__) . '/../../../') . '/');
4e5c11dbfSScrutinizer Auto-Fixer}
57d101cc1SGerry Weißbach
67d101cc1SGerry Weißbach@include_once(DOKU_INC . 'inc/plugincontroller.class.php');
77d101cc1SGerry Weißbach
87d101cc1SGerry Weißbachclass preload_plugin_siteexport {
97d101cc1SGerry Weißbach
101d35ab9dSGerry Weißbach    var $error;
111d35ab9dSGerry Weißbach
127d101cc1SGerry Weißbach    function __register_template() {
137d101cc1SGerry Weißbach
1416c5261cSGerry Weißbach        global $conf;
150d554ed2SGerry Weißbach        $tempREQUEST = array();
1616c5261cSGerry Weißbach
177d101cc1SGerry Weißbach        if (!empty($_REQUEST['q'])) {
187d101cc1SGerry Weißbach
197d101cc1SGerry Weißbach            require_once(DOKU_INC . 'inc/JSON.php');
207d101cc1SGerry Weißbach            $json = new JSON();
217d101cc1SGerry Weißbach            $tempREQUEST = (array) $json->dec(stripslashes($_REQUEST['q']));
227d101cc1SGerry Weißbach
230d554ed2SGerry Weißbach        } else if (array_key_exists('template', $_REQUEST)) {
247d101cc1SGerry Weißbach            $tempREQUEST = $_REQUEST;
2516c5261cSGerry Weißbach        } else if (preg_match("/(js|css)\.php$/", $_SERVER['SCRIPT_NAME']) && isset($_SERVER['HTTP_REFERER'])) {
2616c5261cSGerry Weißbach            // this is a css or script, nothing before matched and we have a referrer.
2716c5261cSGerry Weißbach            // lets asume we came from the dokuwiki page.
2816c5261cSGerry Weißbach
2916c5261cSGerry Weißbach            // Parse the Referrer URL
3016c5261cSGerry Weißbach            $url = parse_url($_SERVER['HTTP_REFERER']);
31641ccffdSGerry Weißbach            if (isset($url['query'])) {
3216c5261cSGerry Weißbach                parse_str($url['query'], $tempREQUEST);
33641ccffdSGerry Weißbach            }
347d101cc1SGerry Weißbach        } else {
357d101cc1SGerry Weißbach            return;
367d101cc1SGerry Weißbach        }
377d101cc1SGerry Weißbach
387d101cc1SGerry Weißbach        // define Template baseURL
39cfda2f5bSGerry Weißbach        $newTemplate = array_key_exists('template', $tempREQUEST) ? $tempREQUEST['template'] : null;
4036db4d82SGerry 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
4136db4d82SGerry Weißbach        if (empty($newTemplate) || basename($newTemplate) != $newTemplate) { return; }
4236db4d82SGerry Weißbach        $tplDir = DOKU_INC . 'lib/tpl/' . $newTemplate;
4336db4d82SGerry 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
4436db4d82SGerry Weißbach        if ($tplDir != realpath($tplDir)) { return; }
457d101cc1SGerry Weißbach
4636db4d82SGerry Weißbach        // Use fileexists, because realpath is not always right.
477d101cc1SGerry Weißbach        if (!file_exists($tplDir)) { return; }
487d101cc1SGerry Weißbach
4906337b60SGerry Weißbach        // Set hint for Dokuwiki_Started event
507d101cc1SGerry Weißbach        if (!defined('SITEEXPORT_TPL'))		define('SITEEXPORT_TPL', $tempREQUEST['template']);
5106337b60SGerry Weißbach
5206337b60SGerry Weißbach        // define baseURL
5306337b60SGerry Weißbach        // This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
5406337b60SGerry Weißbach        /* **************************************************************************************** */
5506337b60SGerry Weißbach        if (!defined('DOKU_REL')) define('DOKU_REL', getBaseURL(false));
5606337b60SGerry Weißbach        if (!defined('DOKU_URL')) define('DOKU_URL', getBaseURL(true));
5706337b60SGerry Weißbach        if (!defined('DOKU_BASE')) {
589210c477SGerry Weißbach            if (isset($conf['canonical'])) {
5906337b60SGerry Weißbach                define('DOKU_BASE', DOKU_URL);
6006337b60SGerry Weißbach            } else {
6106337b60SGerry Weißbach                define('DOKU_BASE', DOKU_REL);
6206337b60SGerry Weißbach            }
6306337b60SGerry Weißbach        }
6406337b60SGerry Weißbach
6506337b60SGerry Weißbach        // This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
6606337b60SGerry Weißbach        if (!defined('DOKU_TPL'))			define('DOKU_TPL', (empty($tempREQUEST['base']) ? DOKU_BASE : $tempREQUEST['base']) . 'lib/tpl/' . $tempREQUEST['template'] . '/');
677d101cc1SGerry Weißbach        if (!defined('DOKU_TPLINC'))		define('DOKU_TPLINC', $tplDir);
6806337b60SGerry Weißbach        /* **************************************************************************************** */
697d101cc1SGerry Weißbach    }
707d101cc1SGerry Weißbach
717d101cc1SGerry Weißbach    function __temporary_disable_plugins() {
727d101cc1SGerry Weißbach
737d101cc1SGerry Weißbach        // Check for siteexport - otherwise this does not matter.
747d101cc1SGerry Weißbach        if (empty($_REQUEST['do']) || $_REQUEST['do'] != 'siteexport') {
757d101cc1SGerry Weißbach            return;
767d101cc1SGerry Weißbach        }
777d101cc1SGerry Weißbach
7865e0d1bfSGerry Weißbach        // check for css and js  ... only disable in that case.
797d101cc1SGerry Weißbach        if (!preg_match("/(js|css)\.php$/", $_SERVER['SCRIPT_NAME'])) {
807d101cc1SGerry Weißbach            return;
817d101cc1SGerry Weißbach        }
827d101cc1SGerry Weißbach
837d101cc1SGerry Weißbach        //		print "removing plugins ";
847d101cc1SGerry Weißbach        $_GET['purge'] = 'purge'; //activate purging
857d101cc1SGerry Weißbach        $_POST['purge'] = 'purge'; //activate purging
867d101cc1SGerry Weißbach        $_REQUEST['purge'] = 'purge'; //activate purging
877d101cc1SGerry Weißbach
887d101cc1SGerry Weißbach        $_SERVER['HTTP_HOST'] = 'siteexport.js'; // fake everything in here
897d101cc1SGerry Weißbach
90641ccffdSGerry Weißbach        // require_once(DOKU_INC.'inc/plugincontroller.class.php'); // Have to get the pluginutils already
91641ccffdSGerry Weißbach        // require_once(DOKU_INC.'inc/pluginutils.php'); // Have to get the pluginutils already
927d101cc1SGerry Weißbach        $this->__disablePlugins();
937d101cc1SGerry Weißbach    }
947d101cc1SGerry Weißbach
957d101cc1SGerry Weißbach    function __disablePlugins() {
96641ccffdSGerry Weißbach        global $plugin_controller_class;
977d101cc1SGerry Weißbach        $plugin_controller_class = 'preload_plugin_siteexport_controller';
987d101cc1SGerry Weißbach    }
997d101cc1SGerry Weißbach
1007d101cc1SGerry Weißbach    function __create_preload_function() {
1017d101cc1SGerry Weißbach
1027d101cc1SGerry Weißbach        $PRELOADFILE = DOKU_INC . 'inc/preload.php';
1037d101cc1SGerry Weißbach        $CURRENTFILE = 'DOKU_INC' . " . 'lib/plugins/siteexport/preload.php'";
1047d101cc1SGerry Weißbach        $CONTENT = <<<OUTPUT
1057d101cc1SGerry Weißbach/* SITE EXPORT *********************************************************** */
1067d101cc1SGerry Weißbach	if ( file_exists($CURRENTFILE) ) {
1077d101cc1SGerry Weißbach		include_once($CURRENTFILE);
1087d101cc1SGerry Weißbach		\$siteexport_preload = new preload_plugin_siteexport();
1097d101cc1SGerry Weißbach		\$siteexport_preload->__register_template();
1107d101cc1SGerry Weißbach		\$siteexport_preload->__temporary_disable_plugins();
1117d101cc1SGerry Weißbach		unset(\$siteexport_preload);
1127d101cc1SGerry Weißbach	}
1137d101cc1SGerry Weißbach/* SITE EXPORT END *********************************************************** */
1147d101cc1SGerry Weißbach
1157d101cc1SGerry WeißbachOUTPUT;
1167d101cc1SGerry Weißbach
1177d101cc1SGerry Weißbach        if (file_exists($PRELOADFILE)) {
1187d101cc1SGerry Weißbach
1197d101cc1SGerry Weißbach            if (!is_readable($PRELOADFILE)) {
120996c253aSGerry Weißbach                $this->error = "Preload File locked. It exists, but it can't be read.";
121996c253aSGerry Weißbach                msg($this->error, -1);
1227d101cc1SGerry Weißbach                return false;
1237d101cc1SGerry Weißbach            }
1247d101cc1SGerry Weißbach
1257d101cc1SGerry Weißbach            if (!is_writeable($PRELOADFILE)) {
126996c253aSGerry Weißbach                $this->error = "Preload File locked. It exists and is readable, but it can't be written.";
127996c253aSGerry Weißbach                msg($this->error, -1);
1287d101cc1SGerry Weißbach                return false;
1297d101cc1SGerry Weißbach            }
1307d101cc1SGerry Weißbach
1317d101cc1SGerry Weißbach            $fileContent = file($PRELOADFILE);
1327d101cc1SGerry Weißbach            if (!strstr(implode("", $fileContent), $CONTENT)) {
1337d101cc1SGerry Weißbach
1347d101cc1SGerry Weißbach                $fp = fopen($PRELOADFILE, "a");
135996c253aSGerry Weißbach                if (!strstr(implode("", $fileContent), "<?")) {
136996c253aSGerry Weißbach                    fputs($fp, "<?php\n");
137996c253aSGerry Weißbach                }
1387d101cc1SGerry Weißbach                fputs($fp, "\n" . $CONTENT);
1397d101cc1SGerry Weißbach                fclose($fp);
1407d101cc1SGerry Weißbach            }
1417d101cc1SGerry Weißbach
1427d101cc1SGerry Weißbach            return true;
1437d101cc1SGerry Weißbach
1447d101cc1SGerry Weißbach        } else if (is_writeable(DOKU_INC . 'inc/')) {
1457d101cc1SGerry Weißbach
1467d101cc1SGerry Weißbach            $fp = fopen($PRELOADFILE, "w");
1477d101cc1SGerry Weißbach            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");
1487d101cc1SGerry Weißbach            fputs($fp, $CONTENT);
1497d101cc1SGerry Weißbach            fputs($fp, "// end auto-generated content\n\n");
1507d101cc1SGerry Weißbach            fclose($fp);
1517d101cc1SGerry Weißbach
1527d101cc1SGerry Weißbach            return true;
1537d101cc1SGerry Weißbach        }
1547d101cc1SGerry Weißbach
155996c253aSGerry Weißbach        $this->error = "Could not create/modify preload.php. Please check the write permissions for your DokuWiki/inc directory.";
156996c253aSGerry Weißbach        msg($this->error, -1);
1577d101cc1SGerry Weißbach        return false;
1587d101cc1SGerry Weißbach    }
1597d101cc1SGerry Weißbach
1607d101cc1SGerry Weißbach}
1617d101cc1SGerry Weißbach
1627d101cc1SGerry Weißbach// return a custom plugin list
1637d101cc1SGerry Weißbachclass preload_plugin_siteexport_controller extends Doku_Plugin_Controller {
1647d101cc1SGerry Weißbach
16565e0d1bfSGerry Weißbach    /**
16665e0d1bfSGerry Weißbach     * Setup disabling
16765e0d1bfSGerry Weißbach     */
16865e0d1bfSGerry Weißbach    public function __construct() {
16965e0d1bfSGerry Weißbach        parent::__construct();
1707d101cc1SGerry Weißbach
1712e56ccbaSGerry Weißbach        $disabledPlugins = array();
1722e56ccbaSGerry Weißbach
1732e56ccbaSGerry Weißbach        // support of old syntax
1742e56ccbaSGerry Weißbach        if (is_array($_REQUEST['diPlu'])) {
1752e56ccbaSGerry Weißbach            $disabledPlugins = $_REQUEST['diPlu'];
1762e56ccbaSGerry Weißbach        }
1772e56ccbaSGerry Weißbach
178641ccffdSGerry Weißbach        if (!empty($_REQUEST['diInv']))
179641ccffdSGerry Weißbach        {
180641ccffdSGerry Weißbach            $allPlugins = array();
181641ccffdSGerry Weißbach            foreach ($this->tmp_plugins as $plugin => $enabled) { // All plugins
182641ccffdSGerry Weißbach                // check for CSS or JS
183641ccffdSGerry 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; }
184641ccffdSGerry Weißbach                $allPlugins[] = $plugin;
185641ccffdSGerry Weißbach            }
186641ccffdSGerry Weißbach            $disabledPlugins = empty($_REQUEST['diPlu']) ? $allPlugins : array_diff($allPlugins, $_REQUEST['diPlu']);
1872e56ccbaSGerry Weißbach        }
1882e56ccbaSGerry Weißbach
1892e56ccbaSGerry Weißbach        // if this is defined, it overrides the settings made above. obviously.
1902e56ccbaSGerry Weißbach        $disabledPlugins = empty($_REQUEST['disableplugin']) ? $disabledPlugins : $_REQUEST['disableplugin'];
1912e56ccbaSGerry Weißbach
19265e0d1bfSGerry Weißbach        foreach ($disabledPlugins as $plugin) {
19365e0d1bfSGerry Weißbach            $this->disable($plugin);
1947d101cc1SGerry Weißbach        }
195c7c6980aSGerry Weißbach
196c7c6980aSGerry Weißbach        // always enabled - JS and CSS will be cut out later.
197c7c6980aSGerry Weißbach        $this->enable('siteexport');
1987d101cc1SGerry Weißbach    }
1997d101cc1SGerry Weißbach
20065e0d1bfSGerry Weißbach    /**
20165e0d1bfSGerry Weißbach     * Disable the plugin
20265e0d1bfSGerry Weißbach     *
20365e0d1bfSGerry Weißbach     * @param string $plugin name of plugin
20465e0d1bfSGerry Weißbach     * @return bool; true allways.
20565e0d1bfSGerry Weißbach     */
20665e0d1bfSGerry Weißbach    public function disable($plugin) {
20765e0d1bfSGerry Weißbach        $this->tmp_plugins[$plugin] = 0;
20865e0d1bfSGerry Weißbach        return true;
2097d101cc1SGerry Weißbach    }
210c7c6980aSGerry Weißbach
21168901b7bSGerry Weißbach    /**
21268901b7bSGerry Weißbach     * Enable the plugin
21368901b7bSGerry Weißbach     *
21468901b7bSGerry Weißbach     * @param string $plugin name of plugin
21568901b7bSGerry Weißbach     * @return bool; true allways.
21668901b7bSGerry Weißbach     */
21768901b7bSGerry Weißbach    public function enable($plugin) {
21868901b7bSGerry Weißbach        $this->tmp_plugins[$plugin] = 1;
21968901b7bSGerry Weißbach        return true;
22068901b7bSGerry Weißbach    }
221c7c6980aSGerry Weißbach
222c7c6980aSGerry Weißbach    public function hasSiteexportHeaders() {
223c7c6980aSGerry Weißbach        $headers = function_exists('getallheaders') ? getallheaders() : null;
224*a0417606SGerry Weißbach        return is_array($headers) && array_key_exists('X-Site-Exporter', $headers) /* && $headers['X-Site-Exporter'] = getSecurityToken() */;
225c7c6980aSGerry Weißbach    }
226c7c6980aSGerry Weißbach
227c7c6980aSGerry Weißbach    /**
2287fa31378SGerry Weißbach     * Filter the List of Plugins for the siteexport plugin
2297fa31378SGerry Weißbach     */
2307fa31378SGerry Weißbach    private function isSiteexportPlugin($item) {
2317fa31378SGerry Weißbach        return $item != 'siteexport';
2327fa31378SGerry Weißbach    }
2337fa31378SGerry Weißbach
2347fa31378SGerry Weißbach    /**
235c7c6980aSGerry Weißbach     * Get the list of plugins, bute remove Siteexport from Style and
236c7c6980aSGerry Weißbach     * JS if in export Mode
237c7c6980aSGerry Weißbach     */
238c7c6980aSGerry Weißbach    public function getList($type = '', $all = false) {
239c7c6980aSGerry Weißbach        $plugins = parent::getList($type, $all);
240c7c6980aSGerry Weißbach
241c7c6980aSGerry Weißbach        list(,, $caller) = debug_backtrace(false);
242c7c6980aSGerry Weißbach        if ($this->hasSiteexportHeaders() && $caller != null && preg_match("/^(js|css)_/", $caller['function']) && preg_match("/(js|css)\.php$/", $caller['file'])) {
2437fa31378SGerry Weißbach            $plugins = array_filter($plugins, array($this, 'isSiteexportPlugin'));
244c7c6980aSGerry Weißbach        }
245c7c6980aSGerry Weißbach
246c7c6980aSGerry Weißbach        return $plugins;
247c7c6980aSGerry Weißbach    }
2487d101cc1SGerry Weißbach}
2497d101cc1SGerry Weißbach
2507d101cc1SGerry Weißbach
2517d101cc1SGerry Weißbach?>
252