1<?php 2if (!defined('DOKU_INC')) { 3 define('DOKU_INC', realpath(dirname(__FILE__) . '/../../../') . '/'); 4} 5 6@include_once(DOKU_INC . 'inc/plugincontroller.class.php'); 7 8class preload_plugin_siteexport { 9 10 var $error; 11 12 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 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 function __disablePlugins() { 96 global $plugin_controller_class; 97 $plugin_controller_class = 'preload_plugin_siteexport_controller'; 98 } 99 100 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), $CONTENT)) { 133 134 $fp = fopen($PRELOADFILE, "a"); 135 if (!strstr(implode("", $fileContent), "<?")) { 136 fputs($fp, "<?php\n"); 137 } 138 fputs($fp, "\n" . $CONTENT); 139 fclose($fp); 140 } 141 142 return true; 143 144 } else if (is_writeable(DOKU_INC . 'inc/')) { 145 146 $fp = fopen($PRELOADFILE, "w"); 147 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"); 148 fputs($fp, $CONTENT); 149 fputs($fp, "// end auto-generated content\n\n"); 150 fclose($fp); 151 152 return true; 153 } 154 155 $this->error = "Could not create/modify preload.php. Please check the write permissions for your DokuWiki/inc directory."; 156 msg($this->error, -1); 157 return false; 158 } 159 160} 161 162// return a custom plugin list 163class preload_plugin_siteexport_controller extends Doku_Plugin_Controller { 164 165 /** 166 * Setup disabling 167 */ 168 public function __construct() { 169 parent::__construct(); 170 171 $disabledPlugins = array(); 172 173 // support of old syntax 174 if (is_array($_REQUEST['diPlu'])) { 175 $disabledPlugins = $_REQUEST['diPlu']; 176 } 177 178 if (!empty($_REQUEST['diInv'])) 179 { 180 $allPlugins = array(); 181 foreach ($this->tmp_plugins as $plugin => $enabled) { // All plugins 182 // check for CSS or JS 183 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; } 184 $allPlugins[] = $plugin; 185 } 186 $disabledPlugins = empty($_REQUEST['diPlu']) ? $allPlugins : array_diff($allPlugins, $_REQUEST['diPlu']); 187 } 188 189 // if this is defined, it overrides the settings made above. obviously. 190 $disabledPlugins = empty($_REQUEST['disableplugin']) ? $disabledPlugins : $_REQUEST['disableplugin']; 191 192 foreach ($disabledPlugins as $plugin) { 193 $this->disable($plugin); 194 } 195 196 // always enabled - JS and CSS will be cut out later. 197 $this->enable('siteexport'); 198 } 199 200 /** 201 * Disable the plugin 202 * 203 * @param string $plugin name of plugin 204 * @return bool; true allways. 205 */ 206 public function disable($plugin) { 207 $this->tmp_plugins[$plugin] = 0; 208 return true; 209 } 210 211 /** 212 * Enable the plugin 213 * 214 * @param string $plugin name of plugin 215 * @return bool; true allways. 216 */ 217 public function enable($plugin) { 218 $this->tmp_plugins[$plugin] = 1; 219 return true; 220 } 221 222 public function hasSiteexportHeaders() { 223 $headers = function_exists('getallheaders') ? getallheaders() : null; 224 return is_array($headers) && array_key_exists('X-Site-Exporter', $headers) && $headers['X-Site-Exporter'] = getSecurityToken(); 225 } 226 227 /** 228 * Filter the List of Plugins for the siteexport plugin 229 */ 230 private function isSiteexportPlugin($item) { 231 return $item != 'siteexport'; 232 } 233 234 /** 235 * Get the list of plugins, bute remove Siteexport from Style and 236 * JS if in export Mode 237 */ 238 public function getList($type = '', $all = false) { 239 $plugins = parent::getList($type, $all); 240 241 list(,, $caller) = debug_backtrace(false); 242 if ($this->hasSiteexportHeaders() && $caller != null && preg_match("/^(js|css)_/", $caller['function']) && preg_match("/(js|css)\.php$/", $caller['file'])) { 243 $plugins = array_filter($plugins, array($this, 'isSiteexportPlugin')); 244 } 245 246 return $plugins; 247 } 248} 249 250 251?> 252