1<?php
2/**
3 * Wiki farm structure creator
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Etienne MELEARD <etienne.meleard@cru.fr>
7 */
8
9global $farmconf;
10$farmconf = array();
11
12global $lang;
13$lang = array();
14
15exit(); // NO EXECUTION SECURITY
16
17// Copy or move recursively one folder's content, avoid items specified in $avoid
18function cp_r($src, $dest, $mv=false, $avoid=array(), $lvl=0) {
19	if(in_array($src, $avoid)) return true;
20	if(@is_file($src)) {
21		$r = copy($src, $dest);
22		chmod($dest, fileperms($src));
23		if($mv) $r &= unlink($src);
24		return $r;
25	}
26
27	if(!@is_dir($dest)) mkdir($dest, fileperms($src));
28
29	$r = true;
30	foreach(scandir($src) as $i) {
31		if($i == '.' || $i == '..') continue;
32		$r &= cp_r($src.'/'.$i, $dest.'/'.$i, $mv, $avoid, $lvl+1);
33	}
34
35	if($mv && $lvl) $r &= rmdir($src);
36
37	return $r;
38}
39
40// Get or updates version
41function version($s = null) {
42	global $farmconf;
43	$file = null;
44	if(@file_exists($farmconf['farmer'].'lib/plugins/farm/installed')) $file = $farmconf['farmer'].'lib/plugins/farm/installed';
45	if(@file_exists('./lib/plugins/farm/installed')) $file = './lib/plugins/farm/installed';
46	if(!$file) return '';
47	if(!$s) return trim(@file_get_contents($file));
48	if($fp = fopen($file, 'w')) {
49		fwrite($fp, $s);
50		fclose($fp);
51	}else die('Could not update install step');
52}
53
54// Get lang
55function getLang($code, $data = null) {
56	global $lang;
57	if(!isset($lang[$code])) return '{'.strtoupper($code).'}';
58	$str = $lang[$code];
59	if(!is_null($data)) if(!is_array($data)) $data = array($data);
60	if(is_array($data)) foreach($data as $d) $str = preg_replace('`%s`', $d, $str, 1);
61	return $str;
62}
63
64// Print messages / exit
65global $out;
66$out = array();
67function out($error = null) {
68	global $farmconf;
69	global $out;
70	echo '<html>'."\n";
71	echo '	<head><title>'.getLang('structure_title').'</title></head>'."\n";
72	echo '	<body>'."\n";
73	echo '		<ul>'."\n";
74	foreach($out as $o) echo '			<li>'.getLang($o).'</li>'."\n";
75	echo '		</ul>'."\n";
76	if($error) {
77		echo '		<div style="border:2px solid red;background-color:tomato;padding:15px;margin:25px;font-size:1.5em;font-weight:bold">'."\n";
78		echo '			'.getLang($error)."\n";
79		echo '		</div>'."\n";
80	}else{
81		echo '		<div style="border:2px solid lime;padding:15px;margin:25px;font-weight:bold">'."\n";
82		echo '			'.getLang('structure_success', $farmconf['farmwebroot'].$farmconf['farmer'].'?do=admin&page=farm')."\n";
83		echo '		</div>'."\n";
84	}
85	echo '	</body>'."\n";
86	echo '</html>';
87	exit();
88}
89
90function aconvform($l) {
91	global $out;
92	echo '<html>'."\n";
93	echo '	<head><title>'.getLang('structure_title').'</title></head>'."\n";
94	echo '	<body>'."\n";
95	echo '		<form action="?aconv" method="post">'."\n";
96	echo '			<h2>'.getLang('structure_convert_to_animals')."</h2><br />\n";
97	foreach($l as $i) echo '			<input id="id_'.$i.'" type="checkbox" name="aconv_'.$i.'" /><label for="id_'.$i.'"> <b>'.$i.'</b></label> : configure <select onchange="document.getElementById(\'id_'.$i.'\').checked=true;" name="aconf_'.$i.'"><option selected="selected" value="0">local.php</option><option value="1">local.protected.php</option></select><br />'."\n";
98	echo '			<input type="submit" name="doaconv" value="'.getLang('btn_next').'" />'."\n";
99	echo '			<input type="hidden" name="out" value="'.implode(',', $out).'" />'."\n";
100	echo '		</form>'."\n";
101	echo '	</body>'."\n";
102	echo '</html>';
103	exit();
104}
105
106switch(version(null)) {
107	case '' :
108		// Create directories
109		$mvfarmer = false;
110		if(!@is_dir(realpath($farmconf['farmfsroot'].$farmconf['farmer']))) {
111			if(!@mkdir($farmconf['farmfsroot'].$farmconf['farmer'], 0755)) out('structure_cannotcreatefarmerdir_failure');
112			$out[] = 'structure_farmerdircreated';
113			$mvfarmer = true;
114		}
115
116		if((realpath($farmconf['farmfsroot'].$farmconf['barn']) != realpath($farmconf['farmfsroot'])) && !@is_dir(realpath($farmconf['farmfsroot'].$farmconf['barn']))) {
117			if(!@mkdir($farmconf['farmfsroot'].$farmconf['barn'], 0755)) out('structure_cannotcreatebarndir_failure');
118			$out[] = 'structure_barndircreated';
119		}
120
121		version('0.1');
122
123	case '0.1' :
124		// Move dokuwiki
125		if($mvfarmer) {
126			if(!cp_r(
127				'.',
128				$farmconf['farmfsroot'].$farmconf['farmer'],
129				true,
130				array('./'.basename(__FILE__), './'.trim($farmconf['farmer'], '/'), './'.trim($farmconf['barn'], '/'))
131			)) out('structure_cannotmovedw_failure');
132			$out[] = 'structure_dwmoved';
133		}
134
135		version('0.2');
136
137	case '0.2' :
138		// Updates DokuWiki htaccess
139		$file = $farmconf['farmfsroot'].$farmconf['farmer'].'.htaccess';
140		if(!@file_exists($file)) $file = $farmconf['farmfsroot'].$farmconf['farmer'].'.htaccess.dist';
141		if(@file_exists($file)) {
142			copy($file, $file.'.orig');
143			if($htaccess = @file_get_contents($file)) {
144				$wr = $farmconf['farmwebroot'].$farmconf['farmer'];
145				if(preg_match('`^([^:]+://)?([^/]+)?/?(.*)$`i', $wr, $m)) $wr = $m[3];
146				$htaccess = preg_replace('`RewriteBase\s+[a-zA-Z0-9/._-]+`', 'RewriteBase /'.trim($wr, '/'), $htaccess);
147				if($fp = fopen($file, 'w')) {
148					fwrite($fp, $htaccess);
149					fclose($fp);
150					$out[] = 'structure_rewritefileupdated';
151				}else out('structure_couldnotupdaterewritefile');
152			}else out('structure_couldnotloadrewritefile');
153		}
154
155		version('0.3');
156
157	case '0.3' :
158		// Copy preload file
159		if(@file_exists($farmconf['farmfsroot'].$farmconf['farmer'].'inc/preload.php')) copy($farmconf['farmfsroot'].$farmconf['farmer'].'inc/preload.php', $farmconf['farmfsroot'].$farmconf['farmer'].'inc/preload.php.orig');
160		if(!copy($farmconf['farmfsroot'].$farmconf['farmer'].'lib/plugins/farm/install/preload.php', $farmconf['farmfsroot'].$farmconf['farmer'].'inc/preload.php')) out('structure_couldnotcopypreloadfile');
161		$out[] = 'structure_preloadfilecopied';
162
163		version('0.4');
164
165	case '0.4' :
166		if(!@is_dir($farmconf['farmfsroot'].$farmconf['barn'].$farmconf['animaltemplate'])) {
167			// Move animal default template
168			if(!@mkdir($farmconf['farmfsroot'].$farmconf['barn'].$farmconf['animaltemplate'], 0755)) out('structure_couldnotcreateanimaltpldirectory');
169			if(!cp_r(
170				$farmconf['farmfsroot'].$farmconf['farmer'].'lib/plugins/farm/install/animaltemplate',
171				$farmconf['farmfsroot'].$farmconf['barn'].$farmconf['animaltemplate']
172			)) out('structure_couldnotcopyanimaltpl');
173			$out[] = 'structure_animaltplcopied';
174
175			// Create animal default template meta
176			if($fp = fopen($farmconf['farmfsroot'].$farmconf['barn'].'/'.$farmconf['animaltemplate'].'/animal.meta', 'w')) {
177				fwrite($fp, 'creation_date : '.time());
178				fclose($fp);
179				$out[] = 'structure_animaltplmetafilecreated';
180			}else out('structure_couldnotcreateanimaltplmetafile');
181		}
182
183		$d = array();
184		foreach(scandir($farmconf['farmfsroot'].$farmconf['barn']) as $i) {
185			if($i == '.' || $i == '..') continue;
186			if($i == $farmconf['animaltemplate']) continue;
187			if(!@is_dir($farmconf['farmfsroot'].$farmconf['barn'].$i)) continue;
188			if((realpath($farmconf['farmfsroot'].$farmconf['barn']) == realpath($farmconf['farmfsroot'])) && ($i == trim($farmconf['farmer'], '/'))) continue;
189			$d[] = $i;
190		}
191		if(count($d)) { // barn already existed with animals directories ?
192			if(isset($_POST['doaconv'])) {
193				if(isset($_POST['out'])) $out = explode(',', $_POST['out']);
194				foreach($d as $i) {
195					if(isset($_POST['aconv_'.$i])) {
196						if($fp = fopen($farmconf['farmfsroot'].$farmconf['barn'].'/'.$i.'/animal.meta', 'w')) {
197							fwrite($fp, 'creation_date : '.time());
198							fclose($fp);
199						}
200						$c = isset($_POST['aconf_'.$i]) ? ($_POST['aconf_'.$i] ? 'local.protected.php' : 'local.php') : 'local.php';
201
202						$ctn = @file_get_contents($farmconf['farmfsroot'].$farmconf['barn'].'/'.$i.'/conf/'.$c);
203						$fp = fopen($farmconf['farmfsroot'].$farmconf['barn'].'/'.$i.'/conf/'.$c, 'w');
204						if($fp) {
205							$added = false;
206							foreach(explode("\n", $ctn) as $l) {
207								if(preg_match('`^\s*\$conf\[\'basedir\'\]\s*\=\s*DOKU_ANIMAL_BASEDIR\s*\.\s*DOKU_FARM_ANIMAL;`', $l)) $added = true;
208								if(!$added) {
209									if(preg_match('`^\s*(include|require|\?>)`', $l)) {
210										fwrite($fp, '$conf[\'basedir\'] = DOKU_ANIMAL_BASEDIR.DOKU_FARM_ANIMAL;');
211										$added = true;
212									}
213								}
214								fwrite($fp, $l."\n");
215							}
216							if(!$added) fwrite($fp, '$conf[\'basedir\'] = DOKU_ANIMAL_BASEDIR.DOKU_FARM_ANIMAL;');
217							fclose($fp);
218						}
219					}
220				}
221			}else aconvform($d);
222		}
223
224		version('0.5');
225
226	case '0.5' :
227		// Remove structure creator script
228		if(!@unlink(__FILE__)) out('structure_couldnotremovestructurecreatorscript');
229
230		version('0.99'); // installation successfull !
231}
232
233out();
234
235?>
236