xref: /plugin/siteexport/preload.php (revision 68901b7b30fbc248a771a69b27ffcf2d72ea6d09)
17d101cc1SGerry Weißbach<?php
27d101cc1SGerry Weißbachif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../../../').'/');
37d101cc1SGerry Weißbach
47d101cc1SGerry Weißbach@include_once(DOKU_INC . 'inc/plugincontroller.class.php');
57d101cc1SGerry Weißbach
67d101cc1SGerry Weißbachclass preload_plugin_siteexport {
77d101cc1SGerry Weißbach
87d101cc1SGerry Weißbach	function __register_template() {
97d101cc1SGerry Weißbach
1016c5261cSGerry Weißbach		global $conf;
1116c5261cSGerry Weißbach
127d101cc1SGerry Weißbach		if ( !empty($_REQUEST['q']) ) {
137d101cc1SGerry Weißbach
147d101cc1SGerry Weißbach			require_once( DOKU_INC . 'inc/JSON.php');
157d101cc1SGerry Weißbach			$json = new JSON();
167d101cc1SGerry Weißbach			$tempREQUEST = (array)$json->dec(stripslashes($_REQUEST['q']));
177d101cc1SGerry Weißbach
187d101cc1SGerry Weißbach		} else if ( !empty( $_REQUEST['template'] ) ) {
197d101cc1SGerry Weißbach			$tempREQUEST = $_REQUEST;
2016c5261cSGerry Weißbach		} else if ( preg_match("/(js|css)\.php$/", $_SERVER['SCRIPT_NAME']) && isset($_SERVER['HTTP_REFERER']) ) {
2116c5261cSGerry Weißbach			// this is a css or script, nothing before matched and we have a referrer.
2216c5261cSGerry Weißbach			// lets asume we came from the dokuwiki page.
2316c5261cSGerry Weißbach
2416c5261cSGerry Weißbach			// Parse the Referrer URL
2516c5261cSGerry Weißbach			$url = parse_url($_SERVER['HTTP_REFERER']);
26641ccffdSGerry Weißbach			if ( isset($url['query']) ) {
2716c5261cSGerry Weißbach				parse_str($url['query'], $tempREQUEST);
28641ccffdSGerry Weißbach			}
297d101cc1SGerry Weißbach		} else {
307d101cc1SGerry Weißbach			return;
317d101cc1SGerry Weißbach		}
327d101cc1SGerry Weißbach
337d101cc1SGerry Weißbach		// define Template baseURL
347d101cc1SGerry Weißbach		if ( empty($tempREQUEST['template']) ) { return; }
357d101cc1SGerry Weißbach		$tplDir = DOKU_INC.'lib/tpl/'.$tempREQUEST['template'].'/';
367d101cc1SGerry Weißbach
377d101cc1SGerry Weißbach		if ( !file_exists($tplDir) ) { return; }
387d101cc1SGerry Weißbach
3906337b60SGerry Weißbach		// Set hint for Dokuwiki_Started event
407d101cc1SGerry Weißbach		if (!defined('SITEEXPORT_TPL'))		define('SITEEXPORT_TPL', $tempREQUEST['template']);
4106337b60SGerry Weißbach
4206337b60SGerry Weißbach		// define baseURL
4306337b60SGerry Weißbach		// This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
4406337b60SGerry Weißbach		/* **************************************************************************************** */
4506337b60SGerry Weißbach		if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
4606337b60SGerry Weißbach		if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
4706337b60SGerry Weißbach		if(!defined('DOKU_BASE')){
489210c477SGerry Weißbach			if( isset($conf['canonical']) ){
4906337b60SGerry Weißbach				define('DOKU_BASE',DOKU_URL);
5006337b60SGerry Weißbach			}else{
5106337b60SGerry Weißbach				define('DOKU_BASE',DOKU_REL);
5206337b60SGerry Weißbach			}
5306337b60SGerry Weißbach		}
5406337b60SGerry Weißbach
5506337b60SGerry Weißbach		// This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
5606337b60SGerry Weißbach		if (!defined('DOKU_TPL'))			define('DOKU_TPL', (empty($tempREQUEST['base']) ? DOKU_BASE : $tempREQUEST['base']) . 'lib/tpl/'.$tempREQUEST['template'].'/');
577d101cc1SGerry Weißbach		if (!defined('DOKU_TPLINC'))		define('DOKU_TPLINC', $tplDir);
5806337b60SGerry Weißbach		/* **************************************************************************************** */
597d101cc1SGerry Weißbach	}
607d101cc1SGerry Weißbach
617d101cc1SGerry Weißbach	function __temporary_disable_plugins() {
627d101cc1SGerry Weißbach
637d101cc1SGerry Weißbach		// Check for siteexport - otherwise this does not matter.
647d101cc1SGerry Weißbach		if ( empty($_REQUEST['do']) || $_REQUEST['do'] != 'siteexport' ) {
657d101cc1SGerry Weißbach			return;
667d101cc1SGerry Weißbach		}
677d101cc1SGerry Weißbach
6865e0d1bfSGerry Weißbach		// check for css and js  ... only disable in that case.
697d101cc1SGerry Weißbach		if ( !preg_match("/(js|css)\.php$/", $_SERVER['SCRIPT_NAME']) ) {
707d101cc1SGerry Weißbach			return;
717d101cc1SGerry Weißbach		}
727d101cc1SGerry Weißbach
737d101cc1SGerry Weißbach		//		print "removing plugins ";
747d101cc1SGerry Weißbach		$_GET['purge'] = 'purge'; //activate purging
757d101cc1SGerry Weißbach		$_POST['purge'] = 'purge'; //activate purging
767d101cc1SGerry Weißbach		$_REQUEST['purge'] = 'purge'; //activate purging
777d101cc1SGerry Weißbach
787d101cc1SGerry Weißbach		$_SERVER['HTTP_HOST'] = 'siteexport.js'; // fake everything in here
797d101cc1SGerry Weißbach
80641ccffdSGerry Weißbach		// require_once(DOKU_INC.'inc/plugincontroller.class.php'); // Have to get the pluginutils already
81641ccffdSGerry Weißbach		// require_once(DOKU_INC.'inc/pluginutils.php'); // Have to get the pluginutils already
827d101cc1SGerry Weißbach		$this->__disablePlugins();
837d101cc1SGerry Weißbach	}
847d101cc1SGerry Weißbach
857d101cc1SGerry Weißbach	function __disablePlugins() {
86641ccffdSGerry Weißbach		global $plugin_controller_class;
877d101cc1SGerry Weißbach		$plugin_controller_class = 'preload_plugin_siteexport_controller';
887d101cc1SGerry Weißbach	}
897d101cc1SGerry Weißbach
907d101cc1SGerry Weißbach	function __create_preload_function() {
917d101cc1SGerry Weißbach
927d101cc1SGerry Weißbach		$PRELOADFILE = DOKU_INC.'inc/preload.php';
937d101cc1SGerry Weißbach		$CURRENTFILE = 'DOKU_INC' . " . 'lib/plugins/siteexport/preload.php'";
947d101cc1SGerry Weißbach		$CONTENT = <<<OUTPUT
957d101cc1SGerry Weißbach/* SITE EXPORT *********************************************************** */
967d101cc1SGerry Weißbach	if ( file_exists($CURRENTFILE) ) {
977d101cc1SGerry Weißbach		include_once($CURRENTFILE);
987d101cc1SGerry Weißbach		\$siteexport_preload = new preload_plugin_siteexport();
997d101cc1SGerry Weißbach		\$siteexport_preload->__register_template();
1007d101cc1SGerry Weißbach		\$siteexport_preload->__temporary_disable_plugins();
1017d101cc1SGerry Weißbach		unset(\$siteexport_preload);
1027d101cc1SGerry Weißbach	}
1037d101cc1SGerry Weißbach/* SITE EXPORT END *********************************************************** */
1047d101cc1SGerry Weißbach
1057d101cc1SGerry WeißbachOUTPUT;
1067d101cc1SGerry Weißbach
1077d101cc1SGerry Weißbach		if ( file_exists($PRELOADFILE) ) {
1087d101cc1SGerry Weißbach
1097d101cc1SGerry Weißbach			if ( ! is_readable($PRELOADFILE) ) {
1107d101cc1SGerry Weißbach				msg("Preload File locked. It exists, but it can't be read.", -1);
1117d101cc1SGerry Weißbach				return false;
1127d101cc1SGerry Weißbach			}
1137d101cc1SGerry Weißbach
1147d101cc1SGerry Weißbach			if ( !is_writeable($PRELOADFILE) ) {
1157d101cc1SGerry Weißbach				msg("Preload File locked. It exists and is readable, but it can't be written.", -1);
1167d101cc1SGerry Weißbach				return false;
1177d101cc1SGerry Weißbach			}
1187d101cc1SGerry Weißbach
1197d101cc1SGerry Weißbach			$fileContent = file($PRELOADFILE);
1207d101cc1SGerry Weißbach			if ( !strstr(implode("", $fileContent), $CONTENT) ) {
1217d101cc1SGerry Weißbach
1227d101cc1SGerry Weißbach				$fp = fopen($PRELOADFILE, "a");
1237d101cc1SGerry Weißbach				fputs($fp, "\n".$CONTENT);
1247d101cc1SGerry Weißbach				fclose($fp);
1257d101cc1SGerry Weißbach			}
1267d101cc1SGerry Weißbach
1277d101cc1SGerry Weißbach			return true;
1287d101cc1SGerry Weißbach
1297d101cc1SGerry Weißbach		} else if ( is_writeable(DOKU_INC . 'inc/') ) {
1307d101cc1SGerry Weißbach
1317d101cc1SGerry Weißbach			$fp = fopen($PRELOADFILE,"w");
1327d101cc1SGerry Weißbach			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");
1337d101cc1SGerry Weißbach			fputs($fp, $CONTENT);
1347d101cc1SGerry Weißbach			fputs($fp, "// end auto-generated content\n\n");
1357d101cc1SGerry Weißbach			fclose($fp);
1367d101cc1SGerry Weißbach
1377d101cc1SGerry Weißbach			return true;
1387d101cc1SGerry Weißbach		}
1397d101cc1SGerry Weißbach
1407d101cc1SGerry Weißbach		msg("Could not create/modify preload.php. Please check the write permissions for your DokuWiki/inc directory.", -1);
1417d101cc1SGerry Weißbach		return false;
1427d101cc1SGerry Weißbach	}
1437d101cc1SGerry Weißbach
1447d101cc1SGerry Weißbach}
1457d101cc1SGerry Weißbach
1467d101cc1SGerry Weißbach// return a custom plugin list
1477d101cc1SGerry Weißbachclass preload_plugin_siteexport_controller extends Doku_Plugin_Controller {
1487d101cc1SGerry Weißbach
14965e0d1bfSGerry Weißbach	/**
15065e0d1bfSGerry Weißbach	 * Setup disabling
15165e0d1bfSGerry Weißbach	 */
15265e0d1bfSGerry Weißbach	public function __construct() {
15365e0d1bfSGerry Weißbach		parent::__construct();
1547d101cc1SGerry Weißbach
1552e56ccbaSGerry Weißbach		$disabledPlugins = array();
1562e56ccbaSGerry Weißbach
1572e56ccbaSGerry Weißbach		// support of old syntax
1582e56ccbaSGerry Weißbach		if ( is_array($_REQUEST['diPlu']) ) {
1592e56ccbaSGerry Weißbach			$disabledPlugins = $_REQUEST['diPlu'];
1602e56ccbaSGerry Weißbach		}
1612e56ccbaSGerry Weißbach
162641ccffdSGerry Weißbach		if ( !empty($_REQUEST['diInv']) )
163641ccffdSGerry Weißbach		{
164641ccffdSGerry Weißbach		    $allPlugins = array();
165641ccffdSGerry Weißbach		    foreach($this->tmp_plugins as $plugin => $enabled) { // All plugins
166641ccffdSGerry Weißbach		    	// check for CSS or JS
167641ccffdSGerry Weißbach		    	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; }
168641ccffdSGerry Weißbach		    	$allPlugins[] = $plugin;
169641ccffdSGerry Weißbach		    }
170641ccffdSGerry Weißbach			$disabledPlugins = empty($_REQUEST['diPlu']) ? $allPlugins : array_diff($allPlugins, $_REQUEST['diPlu']);
1712e56ccbaSGerry Weißbach		}
1722e56ccbaSGerry Weißbach
1732e56ccbaSGerry Weißbach		// if this is defined, it overrides the settings made above. obviously.
1742e56ccbaSGerry Weißbach		$disabledPlugins = empty($_REQUEST['disableplugin']) ? $disabledPlugins : $_REQUEST['disableplugin'];
1752e56ccbaSGerry Weißbach
17665e0d1bfSGerry Weißbach		foreach( $disabledPlugins as $plugin ) {
17765e0d1bfSGerry Weißbach			$this->disable($plugin);
1787d101cc1SGerry Weißbach		}
179c7c6980aSGerry Weißbach
180c7c6980aSGerry Weißbach        // always enabled - JS and CSS will be cut out later.
181c7c6980aSGerry Weißbach        $this->enable('siteexport');
1827d101cc1SGerry Weißbach	}
1837d101cc1SGerry Weißbach
18465e0d1bfSGerry Weißbach   /**
18565e0d1bfSGerry Weißbach    * Disable the plugin
18665e0d1bfSGerry Weißbach    *
18765e0d1bfSGerry Weißbach    * @param string $plugin name of plugin
18865e0d1bfSGerry Weißbach    * @return bool; true allways.
18965e0d1bfSGerry Weißbach    */
19065e0d1bfSGerry Weißbach   public function disable($plugin) {
19165e0d1bfSGerry Weißbach	   $this->tmp_plugins[$plugin] = 0;
19265e0d1bfSGerry Weißbach	   return true;
1937d101cc1SGerry Weißbach   }
194c7c6980aSGerry Weißbach
195*68901b7bSGerry Weißbach   /**
196*68901b7bSGerry Weißbach    * Enable the plugin
197*68901b7bSGerry Weißbach    *
198*68901b7bSGerry Weißbach    * @param string $plugin name of plugin
199*68901b7bSGerry Weißbach    * @return bool; true allways.
200*68901b7bSGerry Weißbach    */
201*68901b7bSGerry Weißbach   public function enable($plugin) {
202*68901b7bSGerry Weißbach	   $this->tmp_plugins[$plugin] = 1;
203*68901b7bSGerry Weißbach	   return true;
204*68901b7bSGerry Weißbach   }
205c7c6980aSGerry Weißbach
206c7c6980aSGerry Weißbach   public function hasSiteexportHeaders() {
207c7c6980aSGerry Weißbach       $headers = function_exists('getallheaders') ? getallheaders() : null;
208c7c6980aSGerry Weißbach       return is_array($headers) && array_key_exists('X-Site-Exporter', $headers) && $headers['X-Site-Exporter'] = getSecurityToken();
209c7c6980aSGerry Weißbach   }
210c7c6980aSGerry Weißbach
211c7c6980aSGerry Weißbach    /**
212c7c6980aSGerry Weißbach     * Get the list of plugins, bute remove Siteexport from Style and
213c7c6980aSGerry Weißbach     * JS if in export Mode
214c7c6980aSGerry Weißbach     */
215c7c6980aSGerry Weißbach   public function getList($type='',$all=false) {
216c7c6980aSGerry Weißbach       $plugins = parent::getList($type, $all);
217c7c6980aSGerry Weißbach
218c7c6980aSGerry Weißbach       list(,, $caller) = debug_backtrace(false);
219c7c6980aSGerry Weißbach       if ( $this->hasSiteexportHeaders() && $caller != null && preg_match("/^(js|css)_/", $caller['function']) && preg_match("/(js|css)\.php$/", $caller['file']) ) {
220c7c6980aSGerry Weißbach           $plugins = array_filter($plugins, function ($item) { return $item != 'siteexport' ;});
221c7c6980aSGerry Weißbach       }
222c7c6980aSGerry Weißbach
223c7c6980aSGerry Weißbach       return $plugins;
224c7c6980aSGerry Weißbach   }
2257d101cc1SGerry Weißbach}
2267d101cc1SGerry Weißbach
2277d101cc1SGerry Weißbach
2287d101cc1SGerry Weißbach?>
229