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