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