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