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