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
9exit(); // NO EXECUTION SECURITY
10
11function cp_r($src, $dest, $mv=false, $forbid=array(), $lvl=0) {
12	if(in_array($src, $forbid)) return true;
13	if(@is_file($src)) {
14		$p = fileperms($src);
15		$r = copy($src, $dest);
16		chmod($dest, $p);
17		if($mv) $r &= unlink($src);
18		return $r;
19	}
20
21	if(!@is_dir($dest)) {
22		$p = fileperms($src);
23		mkdir($dest, $p);
24	}
25
26	$r = true;
27	foreach(scandir($src) as $i) {
28		if($i == '.' || $i == '..') continue;
29		$r &= cp_r($src.'/'.$i, $dest.'/'.$i, $mv, $forbid, $lvl+1);
30	}
31
32	if($mv && $lvl) $r &= rmdir($src);
33
34	return $r;
35}
36
37function installStep($s = null, $infarmer = true) {
38	global $farmconf;
39	$file = null;
40	if(@file_exists($farmconf['farmer'].'lib/plugins/farm/installed')) $file = $farmconf['farmer'].'lib/plugins/farm/installed';
41	if(@file_exists('./lib/plugins/farm/installed')) $file = './lib/plugins/farm/installed';
42	if(!$file) return '';
43	if(!$s) return trim(@file_get_contents($file));
44	if($fp = fopen($file, 'w')) {
45		fwrite($fp, $s);
46		fclose($fp);
47	}else die('Could not update install step');
48}
49
50// Load config
51if(!@file_exists('config.php')) {
52	if(!@file_exists('./lib/plugins/farm/config.php')) die('Cannot load config');
53	if(!copy('./lib/plugins/farm/config.php', 'config.php')) die('Cannot copy temp config');
54}
55global $farmconf;
56$farmconf = array();
57include 'config.php';
58
59switch(installStep(null, false)) {
60	case '' : die('Corrupted install index');
61
62	case '1' :
63		// Create directories
64		if(!@mkdir($farmconf['farmer'], 0755)) die('Cannot create farmer dir');;
65		if(!@mkdir($farmconf['barn'], 0755)) die('Cannot create barn');
66		installStep('1.01', false);
67
68	case '1.01' :
69		// Move dokuwiki
70		if(!cp_r(
71			'.',
72			$farmconf['farmer'],
73			true,
74			array('./'.basename(__FILE__), './config.php', './'.trim($farmconf['farmer'], '/'), './'.trim($farmconf['barn'], '/'))
75		)) die('Cannot move structure');
76		installStep('1.02');
77
78	case '1.02' :
79		// Create main htaccess if needed
80		if($farmconf['userewrite']) {
81			if($htaccess = @file_get_contents($farmconf['farmer'].'lib/plugins/farm/install/farm.htaccess')) {
82				$farm = $farmconf['farmwebroot'];
83				if(preg_match('`^([^:]+://)?([^/]+)?/?(.*)$`i', $farm, $m)) $farm = $m[3];
84				$htaccess = str_replace('{farm}', trim($farm, '/'), $htaccess);
85				$htaccess = str_replace('{farmer}', trim($farmconf['farmer'], '/'), $htaccess);
86				$htaccess = str_replace('{barn}', trim($farmconf['barn'], '/'), $htaccess);
87				if($fp = fopen('.htaccess', 'w')) {
88					fwrite($fp, $htaccess);
89					fclose($fp);
90				}else die('Could not create farm rewrite file');
91			}else die('Could not load farm rewrite template');
92		}
93		installStep('1.03');
94
95	case '1.03' :
96		// Create animals htaccess
97		if($htaccess = @file_get_contents($farmconf['farmer'].'lib/plugins/farm/install/barn.htaccess')) {
98			$farm = $farmconf['farmwebroot'];
99			if(preg_match('`^([^:]+://)?([^/]+)?/?(.*)$`i', $farm, $m)) $farm = $m[3];
100			$htaccess = str_replace('{farm}', trim($farm, '/'), $htaccess);
101			$htaccess = str_replace('{farmer}', trim($farmconf['farmer'], '/'), $htaccess);
102			$htaccess = str_replace('{barn}', trim($farmconf['barn'], '/'), $htaccess);
103			if($fp = fopen($farmconf['barn'].'.htaccess', 'w')) {
104				fwrite($fp, $htaccess);
105				fclose($fp);
106			}else die('Could not create barn rewrite file');
107		}else die('Could not load barn rewrite template');
108		installStep('1.04');
109
110	case '1.04' :
111		// Move animal default template
112		if(!@mkdir($farmconf['barn'].$farmconf['animaltemplate'], 0755)) die('Cannot create animal default template directory');
113		if(!cp_r(
114			$farmconf['farmer'].'lib/plugins/farm/install/animaltemplate',
115			$farmconf['barn'].$farmconf['animaltemplate']
116		)) die('Cannot copy animal default template');
117		installStep('1.05');
118
119	case '1.05' :
120		// Create animal default template meta
121		if($fp = fopen($farmconf['barn'].'/'.$farmconf['animaltemplate'].'/animal.meta', 'w')) {
122			fwrite($fp, 'creation_date : '.time());
123			fclose($fp);
124		}else die('Could not create animal template metafile');
125		installStep('1.06');
126
127	case '1.06' :
128		// Copy preload file
129		if(!copy($farmconf['farmer'].'lib/plugins/farm/install/preload.php', $farmconf['farmer'].'inc/preload.php')) die('Could not copy preload file');
130		installStep('1.07');
131
132	case '1.07' :
133		// Create farmer/wsdl.php
134		$wsdl = @file_get_contents($farmconf['farmer'].'lib/plugins/farm/install/wsdl.base');
135		if(!$wsdl) die('Could not load wsdl base');
136		$file = $farmconf['farmwebroot'].'farm.wsdl';
137		$wsdl = str_replace('{wsdl}', $file, $wsdl);
138		$wsdl = str_replace('{location}', $farmconf['farmwebroot'].$farmconf['farmer'].'lib/plugins/farm/soapserver.php', $wsdl);
139		if($fp = fopen('farm.wsdl', 'w')) {
140			fwrite($fp, $wsdl);
141			fclose($fp);
142		}else die('Could not save farm.wsdl');
143		installStep('1.08');
144
145	case '1.08' :
146		// Copy trusted application config file
147		if(!copy($farmconf['farmer'].'lib/plugins/farm/install/trusted_apps.php', $farmconf['farmer'].'lib/plugins/farm/trusted_apps.php')) die('Could not copy trusted applications config file');
148		installStep('1.09');
149
150	case '1.09' :
151		// Updates DokuWiki htaccess
152		if($htaccess = @file_get_contents($farmconf['farmfsroot'].$farmconf['farmer'].'.htaccess')) {
153			$wr = $farmconf['farmwebroot'].$farmconf['farmer'];
154			if(preg_match('`^([^:]+://)?([^/]+)?/?(.*)$`i', $wr, $m)) $wr = $m[3];
155			$htaccess = preg_replace('`RewriteBase\s+[a-zA-Z0-9/._-]+`', 'RewriteBase /'.trim($wr, '/'), $htaccess);
156			if($fp = fopen($farmconf['farmfsroot'].$farmconf['farmer'].'.htaccess', 'w')) {
157				fwrite($fp, $htaccess);
158				fclose($fp);
159			}else die('Could not update rewrite file');
160		}else die('Could not load rewrite file');
161		installStep('1.09');
162
163	case '1.09' :
164		if(!@unlink(__FILE__)) die('Could not remove farm structure creator script file');
165		if(!@unlink('config.php')) die('Could not remove farm config temp file');
166		installStep('2'); // installation successfull !
167}
168
169header('Location: '.$farmconf['farmwebroot'].$farmconf['farmer'].'?do=admin&page=farm');
170
171?>
172