xref: /plugin/siteexport/preload.php (revision 16c5261c0fde0fb3489ddf60a6b277484e1df04a)
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			parse_str($url['query'], $tempREQUEST);
27		} else {
28			return;
29		}
30
31		// define Template baseURL
32		if ( empty($tempREQUEST['template']) ) { return; }
33		$tplDir = DOKU_INC.'lib/tpl/'.$tempREQUEST['template'].'/';
34
35		if ( !file_exists($tplDir) ) { return; }
36
37		// Set hint for Dokuwiki_Started event
38		if (!defined('SITEEXPORT_TPL'))		define('SITEEXPORT_TPL', $tempREQUEST['template']);
39
40		// define baseURL
41		// This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
42		/* **************************************************************************************** */
43		if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
44		if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
45		if(!defined('DOKU_BASE')){
46			if( isset($conf['canonical']) ){
47				define('DOKU_BASE',DOKU_URL);
48			}else{
49				define('DOKU_BASE',DOKU_REL);
50			}
51		}
52
53		// This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
54		if (!defined('DOKU_TPL'))			define('DOKU_TPL', (empty($tempREQUEST['base']) ? DOKU_BASE : $tempREQUEST['base']) . 'lib/tpl/'.$tempREQUEST['template'].'/');
55		if (!defined('DOKU_TPLINC'))		define('DOKU_TPLINC', $tplDir);
56		/* **************************************************************************************** */
57	}
58
59	function __temporary_disable_plugins() {
60
61		// Check for siteexport - otherwise this does not matter.
62		if ( empty($_REQUEST['do']) || $_REQUEST['do'] != 'siteexport' ) {
63			return;
64		}
65
66		// check for css and js  ... only disable in that case.
67		if ( !preg_match("/(js|css)\.php$/", $_SERVER['SCRIPT_NAME']) ) {
68			return;
69		}
70
71		//		print "removing plugins ";
72		$_GET['purge'] = 'purge'; //activate purging
73		$_POST['purge'] = 'purge'; //activate purging
74		$_REQUEST['purge'] = 'purge'; //activate purging
75
76		$_SERVER['HTTP_HOST'] = 'siteexport.js'; // fake everything in here
77
78		require_once(DOKU_INC.'inc/plugincontroller.class.php'); // Have to get the pluginutils already
79		require_once(DOKU_INC.'inc/pluginutils.php'); // Have to get the pluginutils already
80		$this->__disablePlugins();
81	}
82
83	function __disablePlugins() {
84		global $plugin_controller_class, $plugin_controller;
85		$plugin_controller_class = 'preload_plugin_siteexport_controller';
86	}
87
88	function __create_preload_function() {
89
90		$PRELOADFILE = DOKU_INC.'inc/preload.php';
91		$CURRENTFILE = 'DOKU_INC' . " . 'lib/plugins/siteexport/preload.php'";
92		$CONTENT = <<<OUTPUT
93/* SITE EXPORT *********************************************************** */
94	if ( file_exists($CURRENTFILE) ) {
95		include_once($CURRENTFILE);
96		\$siteexport_preload = new preload_plugin_siteexport();
97		\$siteexport_preload->__register_template();
98		\$siteexport_preload->__temporary_disable_plugins();
99		unset(\$siteexport_preload);
100	}
101/* SITE EXPORT END *********************************************************** */
102
103OUTPUT;
104
105		if ( file_exists($PRELOADFILE) ) {
106
107			if ( ! is_readable($PRELOADFILE) ) {
108				msg("Preload File locked. It exists, but it can't be read.", -1);
109				return false;
110			}
111
112			if ( !is_writeable($PRELOADFILE) ) {
113				msg("Preload File locked. It exists and is readable, but it can't be written.", -1);
114				return false;
115			}
116
117			$fileContent = file($PRELOADFILE);
118			if ( !strstr(implode("", $fileContent), $CONTENT) ) {
119
120				$fp = fopen($PRELOADFILE, "a");
121				fputs($fp, "\n".$CONTENT);
122				fclose($fp);
123			}
124
125			return true;
126
127		} else if ( is_writeable(DOKU_INC . 'inc/') ) {
128
129			$fp = fopen($PRELOADFILE,"w");
130			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");
131			fputs($fp, $CONTENT);
132			fputs($fp, "// end auto-generated content\n\n");
133			fclose($fp);
134
135			return true;
136		}
137
138		msg("Could not create/modify preload.php. Please check the write permissions for your DokuWiki/inc directory.", -1);
139		return false;
140	}
141
142}
143
144// return a custom plugin list
145class preload_plugin_siteexport_controller extends Doku_Plugin_Controller {
146
147	/**
148	 * Setup disabling
149	 */
150	public function __construct() {
151		parent::__construct();
152
153		$disabledPlugins = array();
154
155		// support of old syntax
156		if ( is_array($_REQUEST['diPlu']) ) {
157			$disabledPlugins = $_REQUEST['diPlu'];
158		}
159
160		if ( !empty($_REQUEST['diInv']) ) {
161			// Invert disabled plugins!
162			$disabledPlugins = array_diff(array_keys($this->tmp_plugins), $_REQUEST['diPlu']);
163		}
164
165		// if this is defined, it overrides the settings made above. obviously.
166		$disabledPlugins = empty($_REQUEST['disableplugin']) ? $disabledPlugins : $_REQUEST['disableplugin'];
167
168		foreach( $disabledPlugins as $plugin ) {
169			$this->disable($plugin);
170		}
171	}
172
173   /**
174    * Disable the plugin
175    *
176    * @param string $plugin name of plugin
177    * @return bool; true allways.
178    */
179   public function disable($plugin) {
180	   $this->tmp_plugins[$plugin] = 0;
181	   return true;
182   }
183}
184
185
186?>