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