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