<?php
/**
 * Wiki farm installer, only used for first config
 *
 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author   Etienne MELEARD <etienne.meleard@cru.fr>
 * @desc     Process and render first steps of farm installation
 */

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
if(!defined('DOKU_FARM_PLUGIN')) define('DOKU_FARM_PLUGIN', DOKU_PLUGIN.'farm/');

if(!defined('DOKU_FARMPLUGINLOADED')) define('DOKU_FARMPLUGINLOADED', true);

class dokuwiki_farm_install {
	var $manager = null;
	
	var $webroot = null;
	var $fsroot = null;
	var $farmer = null;
	var $barn = null;
	
	/**
	 * @param $manager object that must handle error(), success(), nicesize(), getLang() ... calls
	 */
	function __construct($manager) {
		$this->manager = & $manager;
	}
	
	/**
	 * Process requests
	 */
	function process() {
		if(isset($this->manager->opt['next'])) {
			// Parameters checks
			if(
				!isset($_POST['install_field_farmwebroot'])
				|| !isset($_POST['install_field_farmfsroot'])
				|| !isset($_POST['install_field_farmer'])
				|| !isset($_POST['install_field_barn'])
			) {
				$this->manager->error('install_errors', 'postparametermissing_failure');
				return;
			}
			
			$ok = true;
			$this->webroot = $_POST['install_field_farmwebroot'];
			$this->fsroot = $_POST['install_field_farmfsroot'];
			$this->farmer = $_POST['install_field_farmer'];
			$this->barn = $_POST['install_field_barn'];
			
			
			if(empty($this->webroot)) {
				$this->manager->error('install_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmwebroot'))));
				$ok = false;
			}
			if(empty($this->fsroot)) {
				$this->manager->error('install_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmfsroot'))));
				$ok = false;
			}
			if(empty($this->farmer)) {
				$this->manager->error('install_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmer'))));
				$ok = false;
			}
			/* Barn can be empty since the current folder can be the abrn (barnatroot mode)
			if(empty($this->barn)) {
				$this->manager->error('install_errors', array('code' => 'emptyfield_failure', 'data' => array($this->manager->getLang('install_field_name_barn'))));
				$ok = false;
			}
			*/
			if(!$ok) return;
			
			if(substr($this->webroot, -1) != '/') {
				$this->manager->error('install_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmwebroot'))));
				$ok = false;
			}
			if((substr($this->fsroot, -1) != '/') || !@is_dir($this->fsroot)) {
				$this->manager->error('install_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmfsroot'))));
				$ok = false;
			}
			if(substr($this->farmer, -1) != '/') {
				$this->manager->error('install_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('install_field_name_farmer'))));
				$ok = false;
			}
			if($this ->barn && substr($this->barn, -1) != '/') {
				$this->manager->error('install_errors', array('code' => 'invalidfield_failure', 'data' => array($this->manager->getLang('install_field_name_barn'))));
				$ok = false;
			}
			if(!$ok) return;
			
			$conf = explode("\n", @file_get_contents(DOKU_FARM_PLUGIN.'install/config.php'));
			if(count($conf)) {
				$r = 0;
				for($i=0; $i<count($conf); $i++) {
					if(preg_match('`^\$farmconf\[[\'"]([a-z0-9_-]+)[\'"]\]\s*=\s*([^;]+);(.*)$`i', trim($conf[$i]), $m)) {
						switch(trim($m[1])) {
							case 'farmwebroot' : $r++; $conf[$i] = '$farmconf[\'farmwebroot\'] = \''.$this->webroot.'\';'.$m[3]; break;
							case 'farmfsroot' : $r++; $conf[$i] = '$farmconf[\'farmfsroot\'] = \''.$this->fsroot.'\';'.$m[3]; break;
							case 'farmer' : $r++; $conf[$i] = '$farmconf[\'farmer\'] = \''.$this->farmer.'\';'.$m[3]; break;
							case 'barn' : $r++; $conf[$i] = '$farmconf[\'barn\'] = \''.$this->barn.'\';'.$m[3]; break;
						}
					}
				}
				if($r != 4) return $this->manager->error('install_errors', 'install_configcopy_failure');
				
				if($fp = fopen(DOKU_FARM_PLUGIN.'config.php', 'w')) {
					fwrite($fp, implode("\n", $conf));
					fclose($fp);
					if(!@touch(DOKU_FARM_PLUGIN.'installed', 'w')) return $this->manager->error('install_errors', 'install_stepupdate_failure');
					$sc = @file_get_contents(DOKU_FARM_PLUGIN.'install/farmstructurecreator.php');
					if($sc != '') {
						global $conf;
						$l = isset($conf['lang']) ? $conf['lang'] : 'en';
						$l = @file_exists(DOKU_FARM_PLUGIN.'lang/'.$l.'/lang.php') ? DOKU_FARM_PLUGIN.'lang/'.$l.'/lang.php' : DOKU_FARM_PLUGIN.'lang/en/lang.php';
						$c = DOKU_FARM_PLUGIN.'config.php';
						$sc = str_replace('exit(); // NO EXECUTION SECURITY', 'include \''.$c.'\';'."\n".'include \''.$l.'\';'."\n", $sc);
						if($fp = fopen($this->fsroot.'farmstructurecreator.php', 'w')) {
							fwrite($fp, $sc);
							fclose($fp);
							header('Location: '.$this->webroot.'farmstructurecreator.php');
							exit();
						}else $this->manager->error('install_errors', 'install_cannotsavecreator_failure');
					}else $this->manager->error('install_errors', 'install_nocreatorfile_failure');
				}else $this->manager->error('install_errors', 'install_configcopy_failure');
			}else $this->manager->error('install_errors', 'install_configcopy_failure');
		}
	}
	
	/**
	 * Renders
	 */
	function html() {
		ptln('<div class="farm_cmd_title">'.$this->manager->getLang('install_title').'</div>');
		ptln('<div class="farm_cmd_info">'.$this->manager->getLang('install_info').'</div>');
		
		// Weird css inclusion bug fix
		echo '<style type="text/css">'.@file_get_contents(DOKU_FARM_PLUGIN.'style.css').'</style>';
		
		$this->manager->formHead(array('farm_cmd' => 'farminstall'));
		
		if(is_null($this->webroot)) $this->webroot = preg_replace('`doku\.php$`', '', wl('', array(), true));
		if(is_null($this->fsroot)) $this->fsroot = substr(__FILE__, 0, -1 * strlen('lib/plugins/farm/install.class.php'));
		if(is_null($this->farmer)) $this->farmer = 'farmer/';
		if(is_null($this->barn)) $this->barn = 'barn/';
		
		ptln('	<fieldset>');
		ptln('		'.$this->manager->getLang('install_field_name_farmwebroot').' : <input type="text" name="install_field_farmwebroot" value="'.$this->webroot.'"/><br />');
		ptln('<small>'.$this->manager->getLang('install_field_desc_farmwebroot').'</small>');
		ptln('	</fieldset>');
		
		ptln('	<fieldset>');
		ptln('		'.$this->manager->getLang('install_field_name_farmfsroot').' : <input type="text" name="install_field_farmfsroot" value="'.$this->fsroot.'"/><br />');
		ptln('<small>'.$this->manager->getLang('install_field_desc_farmfsroot').'</small>');
		ptln('	</fieldset>');
		
		ptln('	<fieldset>');
		ptln('		'.$this->manager->getLang('install_field_name_farmer').' : <input type="text" name="install_field_farmer" value="'.$this->farmer.'"/><br />');
		ptln('<small>'.$this->manager->getLang('install_field_desc_farmer').'</small>');
		ptln('	</fieldset>');
		
		ptln('	<fieldset>');
		ptln('		'.$this->manager->getLang('install_field_name_barn').' : <input type="text" name="install_field_barn" value="'.$this->barn.'"/><br />');
		ptln('<small>'.$this->manager->getLang('install_field_desc_barn').'</small>');
		ptln('	</fieldset>');
		
		ptln('	<fieldset class="save">');
		ptln('		<input type="submit" class="button" name="farm_opt[next]" value="'.$this->manager->getLang('btn_next').'" />');
		ptln('	</fieldset>');
		ptln('</form>');
	}
}
?>
