1<?php
2/**
3 * Wiki farm installer, only used for first config
4 *
5 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author   Etienne MELEARD <etienne.meleard@cru.fr>
7 * @desc     Process and render first steps of farm installation
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
14if(!defined('DOKU_FARM_PLUGIN')) define('DOKU_FARM_PLUGIN', DOKU_PLUGIN.'farm/');
15
16if(!defined('DOKU_FARMPLUGINLOADED')) define('DOKU_FARMPLUGINLOADED', true);
17
18class dokuwiki_farm_install {
19	var $manager = null;
20
21	var $webroot = null;
22	var $fsroot = null;
23	var $farmer = null;
24	var $barn = null;
25
26	/**
27	 * @param $manager object that must handle error(), success(), nicesize(), getLang() ... calls
28	 */
29	function __construct($manager) {
30		$this->manager = & $manager;
31	}
32
33	/**
34	 * Process requests
35	 */
36	function process() {
37		if(isset($this->manager->opt['next'])) {
38			// Parameters checks
39			if(
40				!isset($_POST['install_field_farmwebroot'])
41				|| !isset($_POST['install_field_farmfsroot'])
42				|| !isset($_POST['install_field_farmer'])
43				|| !isset($_POST['install_field_barn'])
44			) {
45				$this->manager->error('install_errors', 'postparametermissing_failure');
46				return;
47			}
48
49			$ok = true;
50			$this->webroot = $_POST['install_field_farmwebroot'];
51			$this->fsroot = $_POST['install_field_farmfsroot'];
52			$this->farmer = $_POST['install_field_farmer'];
53			$this->barn = $_POST['install_field_barn'];
54
55
56			if(empty($this->webroot)) {
57				$this->manager->error('install_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmwebroot'))));
58				$ok = false;
59			}
60			if(empty($this->fsroot)) {
61				$this->manager->error('install_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmfsroot'))));
62				$ok = false;
63			}
64			if(empty($this->farmer)) {
65				$this->manager->error('install_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmer'))));
66				$ok = false;
67			}
68			/* Barn can be empty since the current folder can be the abrn (barnatroot mode)
69			if(empty($this->barn)) {
70				$this->manager->error('install_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('install_field_name_barn'))));
71				$ok = false;
72			}
73			*/
74			if(!$ok) return;
75
76			if(substr($this->webroot, -1) != '/') {
77				$this->manager->error('install_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmwebroot'))));
78				$ok = false;
79			}
80			if((substr($this->fsroot, -1) != '/') || !@is_dir($this->fsroot)) {
81				$this->manager->error('install_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmfsroot'))));
82				$ok = false;
83			}
84			if(substr($this->farmer, -1) != '/') {
85				$this->manager->error('install_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmer'))));
86				$ok = false;
87			}
88			if($this ->barn && substr($this->barn, -1) != '/') {
89				$this->manager->error('install_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('install_field_name_barn'))));
90				$ok = false;
91			}
92			if(!$ok) return;
93
94			$conf = explode("\n", @file_get_contents(DOKU_FARM_PLUGIN.'install/config.php'));
95			if(count($conf)) {
96				$r = 0;
97				for($i=0; $i<count($conf); $i++) {
98					if(preg_match('`^\$farmconf\[[\'"]([a-z0-9_-]+)[\'"]\]\s*=\s*([^;]+);(.*)$`i', trim($conf[$i]), $m)) {
99						switch(trim($m[1])) {
100							case 'farmwebroot' : $r++; $conf[$i] = '$farmconf[\'farmwebroot\'] = \''.$this->webroot.'\';'.$m[3]; break;
101							case 'farmfsroot' : $r++; $conf[$i] = '$farmconf[\'farmfsroot\'] = \''.$this->fsroot.'\';'.$m[3]; break;
102							case 'farmer' : $r++; $conf[$i] = '$farmconf[\'farmer\'] = \''.$this->farmer.'\';'.$m[3]; break;
103							case 'barn' : $r++; $conf[$i] = '$farmconf[\'barn\'] = \''.$this->barn.'\';'.$m[3]; break;
104						}
105					}
106				}
107				if($r != 4) return $this->manager->error('install_errors', 'install_configcopy_failure');
108
109				if($fp = fopen(DOKU_FARM_PLUGIN.'config.php', 'w')) {
110					fwrite($fp, implode("\n", $conf));
111					fclose($fp);
112					if(!@touch(DOKU_FARM_PLUGIN.'installed', 'w')) return $this->manager->error('install_errors', 'install_stepupdate_failure');
113					$sc = @file_get_contents(DOKU_FARM_PLUGIN.'install/farmstructurecreator.php');
114					if($sc != '') {
115						global $conf;
116						$l = isset($conf['lang']) ? $conf['lang'] : 'en';
117						$l = @file_exists(DOKU_FARM_PLUGIN.'lang/'.$l.'/lang.php') ? DOKU_FARM_PLUGIN.'lang/'.$l.'/lang.php' : DOKU_FARM_PLUGIN.'lang/en/lang.php';
118						$c = DOKU_FARM_PLUGIN.'config.php';
119						$sc = str_replace('exit(); // NO EXECUTION SECURITY', 'include \''.$c.'\';'."\n".'include \''.$l.'\';'."\n", $sc);
120						if($fp = fopen($this->fsroot.'farmstructurecreator.php', 'w')) {
121							fwrite($fp, $sc);
122							fclose($fp);
123							header('Location: '.$this->webroot.'farmstructurecreator.php');
124							exit();
125						}else $this->manager->error('install_errors', 'install_cannotsavecreator_failure');
126					}else $this->manager->error('install_errors', 'install_nocreatorfile_failure');
127				}else $this->manager->error('install_errors', 'install_configcopy_failure');
128			}else $this->manager->error('install_errors', 'install_configcopy_failure');
129		}
130	}
131
132	/**
133	 * Renders
134	 */
135	function html() {
136		ptln('<div class="farm_cmd_title">'.$this->manager->getLang('install_title').'</div>');
137		ptln('<div class="farm_cmd_info">'.$this->manager->getLang('install_info').'</div>');
138
139		// Weird css inclusion bug fix
140		echo '<style type="text/css">'.@file_get_contents(DOKU_FARM_PLUGIN.'style.css').'</style>';
141
142		$this->manager->formHead(array('farm_cmd' => 'farminstall'));
143
144		if(is_null($this->webroot)) $this->webroot = preg_replace('`doku\.php$`', '', wl('', array(), true));
145		if(is_null($this->fsroot)) $this->fsroot = substr(__FILE__, 0, -1 * strlen('lib/plugins/farm/install.class.php'));
146		if(is_null($this->farmer)) $this->farmer = 'farmer/';
147		if(is_null($this->barn)) $this->barn = 'barn/';
148
149		ptln('	<fieldset>');
150		ptln('		'.$this->manager->getLang('install_field_name_farmwebroot').' : <input type="text" name="install_field_farmwebroot" value="'.$this->webroot.'"/><br />');
151		ptln('<small>'.$this->manager->getLang('install_field_desc_farmwebroot').'</small>');
152		ptln('	</fieldset>');
153
154		ptln('	<fieldset>');
155		ptln('		'.$this->manager->getLang('install_field_name_farmfsroot').' : <input type="text" name="install_field_farmfsroot" value="'.$this->fsroot.'"/><br />');
156		ptln('<small>'.$this->manager->getLang('install_field_desc_farmfsroot').'</small>');
157		ptln('	</fieldset>');
158
159		ptln('	<fieldset>');
160		ptln('		'.$this->manager->getLang('install_field_name_farmer').' : <input type="text" name="install_field_farmer" value="'.$this->farmer.'"/><br />');
161		ptln('<small>'.$this->manager->getLang('install_field_desc_farmer').'</small>');
162		ptln('	</fieldset>');
163
164		ptln('	<fieldset>');
165		ptln('		'.$this->manager->getLang('install_field_name_barn').' : <input type="text" name="install_field_barn" value="'.$this->barn.'"/><br />');
166		ptln('<small>'.$this->manager->getLang('install_field_desc_barn').'</small>');
167		ptln('	</fieldset>');
168
169		ptln('	<fieldset class="save">');
170		ptln('		<input type="submit" class="button" name="farm_opt[next]" value="'.$this->manager->getLang('btn_next').'" />');
171		ptln('	</fieldset>');
172		ptln('</form>');
173	}
174}
175?>
176