xref: /plugin/siteexport/preload.php (revision 88c86a269034fc404c62da4d7c5c82f1792b7bcf)
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		if ( !empty($_REQUEST['q']) ) {
11
12			require_once( DOKU_INC . 'inc/JSON.php');
13			$json = new JSON();
14			$tempREQUEST = (array)$json->dec(stripslashes($_REQUEST['q']));
15
16		} else if ( !empty( $_REQUEST['template'] ) ) {
17			$tempREQUEST = $_REQUEST;
18		} else {
19			return;
20		}
21
22		// define Template baseURL
23		if ( empty($tempREQUEST['template']) ) { return; }
24		$tplDir = DOKU_INC.'lib/tpl/'.$tempREQUEST['template'].'/';
25
26		if ( !file_exists($tplDir) ) { return; }
27
28		// Set hint for Dokuwiki_Started event
29		if (!defined('SITEEXPORT_TPL'))		define('SITEEXPORT_TPL', $tempREQUEST['template']);
30
31		// define baseURL
32		// This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
33		/* **************************************************************************************** */
34		if(!defined('DOKU_REL')) define('DOKU_REL',getBaseURL(false));
35		if(!defined('DOKU_URL')) define('DOKU_URL',getBaseURL(true));
36		if(!defined('DOKU_BASE')){
37			if($conf['canonical']){
38				define('DOKU_BASE',DOKU_URL);
39			}else{
40				define('DOKU_BASE',DOKU_REL);
41			}
42		}
43
44		// This should be DEPRECATED - as it is in init.php which suggest tpl_basedir and tpl_incdir
45		if (!defined('DOKU_TPL'))			define('DOKU_TPL', (empty($tempREQUEST['base']) ? DOKU_BASE : $tempREQUEST['base']) . 'lib/tpl/'.$tempREQUEST['template'].'/');
46		if (!defined('DOKU_TPLINC'))		define('DOKU_TPLINC', $tplDir);
47		/* **************************************************************************************** */
48	}
49
50	function __temporary_disable_plugins() {
51
52		// Check for siteexport - otherwise this does not matter.
53		if ( empty($_REQUEST['do']) || $_REQUEST['do'] != 'siteexport' ) {
54			return;
55		}
56
57		// check for css and js as well ...
58		if ( !preg_match("/(js|css)\.php$/", $_SERVER['SCRIPT_NAME']) ) {
59			return;
60		}
61
62		//		print "removing plugins ";
63		$_GET['purge'] = 'purge'; //activate purging
64		$_POST['purge'] = 'purge'; //activate purging
65		$_REQUEST['purge'] = 'purge'; //activate purging
66
67		$_SERVER['HTTP_HOST'] = 'siteexport.js'; // fake everything in here
68
69		require_once(DOKU_INC.'inc/plugincontroller.class.php'); // Have to get the pluginutils already
70		require_once(DOKU_INC.'inc/pluginutils.php'); // Have to get the pluginutils already
71		$this->__disablePlugins();
72	}
73
74	function __disablePlugins() {
75		global $plugin_controller_class, $plugin_controller;
76
77		$plugin_controller_class = 'preload_plugin_siteexport_controller';
78	}
79
80	function __create_preload_function() {
81
82		$PRELOADFILE = DOKU_INC.'inc/preload.php';
83		$CURRENTFILE = 'DOKU_INC' . " . 'lib/plugins/siteexport/preload.php'";
84		$CONTENT = <<<OUTPUT
85/* SITE EXPORT *********************************************************** */
86	if ( file_exists($CURRENTFILE) ) {
87		include_once($CURRENTFILE);
88		\$siteexport_preload = new preload_plugin_siteexport();
89		\$siteexport_preload->__register_template();
90		\$siteexport_preload->__temporary_disable_plugins();
91		unset(\$siteexport_preload);
92	}
93/* SITE EXPORT END *********************************************************** */
94
95OUTPUT;
96
97		if ( file_exists($PRELOADFILE) ) {
98
99			if ( ! is_readable($PRELOADFILE) ) {
100				msg("Preload File locked. It exists, but it can't be read.", -1);
101				return false;
102			}
103
104			if ( !is_writeable($PRELOADFILE) ) {
105				msg("Preload File locked. It exists and is readable, but it can't be written.", -1);
106				return false;
107			}
108
109			$fileContent = file($PRELOADFILE);
110			if ( !strstr(implode("", $fileContent), $CONTENT) ) {
111
112				$fp = fopen($PRELOADFILE, "a");
113				fputs($fp, "\n".$CONTENT);
114				fclose($fp);
115			}
116
117			return true;
118
119		} else if ( is_writeable(DOKU_INC . 'inc/') ) {
120
121			$fp = fopen($PRELOADFILE,"w");
122			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");
123			fputs($fp, $CONTENT);
124			fputs($fp, "// end auto-generated content\n\n");
125			fclose($fp);
126
127			return true;
128		}
129
130		msg("Could not create/modify preload.php. Please check the write permissions for your DokuWiki/inc directory.", -1);
131		return false;
132	}
133
134}
135
136// return a custom plugin list
137class preload_plugin_siteexport_controller extends Doku_Plugin_Controller {
138
139	function getList($type='',$all=false){
140
141		$allPlugin = parent::getList(null, true);
142		$oldPluginsEnabled = parent::getList(null, false);
143		$currentPluginsDisabled = empty($_REQUEST['diPlu']) ? ( empty($_REQUEST['disableplugin']) ? array() : $_REQUEST['disableplugin']) : $_REQUEST['diPlu'];
144		$pluginsDisabledInverse = !empty($_REQUEST['diInv']) || !empty($_REQUEST['disableall']);
145
146		if ( !$this->list_enabled ) {
147			$this->list_enabled = array();
148		}
149
150		// All plugins that are not already disabled are to be disabled
151		$toDisable = !$pluginsDisabledInverse ? array_diff($currentPluginsDisabled, array_diff($allPlugin, $oldPluginsEnabled)) : array_diff(array_diff($allPlugin, $currentPluginsDisabled), array_diff($allPlugin, $oldPluginsEnabled));
152
153	    foreach ( $toDisable as $plugin ) {
154
155			if ( !in_array($plugin, $allPlugin) ) { continue; }
156			$this->list_enabled = array_diff($this->tmp_plugins, array($plugin));
157			$this->list_disabled[] = $plugin;
158		}
159
160	    foreach($this->list_enabled as $plugin ) {
161	    	// check for CSS or JS
162	    	if ( !file_exists(DOKU_PLUGIN."$plugin/script.js") && !file_exists(DOKU_PLUGIN."$plugin/style.css") ) {
163	    		unset($this->tmp_plugins[$plugin]);
164				$this->list_disabled[] = $plugin;
165	    	}
166	    }
167
168	    return parent::getList($type,$all);
169	}
170}
171
172
173?>