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