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 // here we will have issues using farmer plugin or docker container, since the path is very different. 51 // the tpldir can be different when the realpath is looked up, but should still exist. 52 $tplDir = realpath($tplDir); 53 if ($tplDir === false || !is_dir($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 global $INPUT; 180 181 parent::__construct(); 182 183 $disabledPlugins = array(); 184 185 // support of old syntax 186 if (is_array($INPUT->arr('diPlu'))) { 187 $disabledPlugins = $INPUT->arr('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($INPUT->arr('diPlu')) ? $allPlugins : array_diff($allPlugins, $INPUT->arr('diPlu')); 199 } 200 201 // if this is defined, it overrides the settings made above. obviously. 202 $disabledPlugins = $INPUT->arr('disableplugin', $disabledPlugins, true); 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