<?php
/*
                                  ____   _____
                                 / __ \ / ____|
                  ___ _   _  ___| |  | | (___
                 / _ \ | | |/ _ \ |  | |\___ \
                |  __/ |_| |  __/ |__| |____) |
                 \___|\__, |\___|\____/|_____/
                       __/ |
                      |___/              1.8

                     Web Operating System
                           eyeOS.org

             eyeOS Engineering Team - www.eyeos.org/team

     eyeOS is released under the GNU Affero General Public License Version 3 (AGPL3)
            provided with this release in license.txt
             or via web at gnu.org/licenses/agpl-3.0.txt

        Copyright 2005-2009 eyeOS Team (team@eyeos.org)
*/

if ($_REQUEST['hostname'] && strlen($_REQUEST['password']) > 2 && $_REQUEST['password'] == $_REQUEST['password2']) {
	install_do($_REQUEST['install'],$_REQUEST['allow_register'],$_REQUEST['hostname'],$_REQUEST['password']);
} else {
	install_start($_REQUEST['allow_register'],$_REQUEST['hostname'],$_REQUEST['password'],$_REQUEST['password2']);
}

function install_start($allow_register,$hostname,$password,$password2) {
	if (!$_REQUEST['hostname']) {
		if (isset($hostname)) {
			$errorHostname = lang_translate('installer-install-systemname-error','You must specify a systemname!');
		}
		$hostname = 'eyeOS';
	}
	if (isset($password) || isset($_REQUEST['password2'])) {
		if (!$password) {
			$errorPassword = lang_translate('installer-install-passwords-required','The root password is required!');
			$password2 = '';
		} elseif (strlen($password) < 3) {
			$errorPassword = lang_translate('installer-install-passwords-characters','The root password needs to be more than three characters!');
			$password = '';
			$password2 = '';
		} elseif ($password != $password2) {
			$errorPassword = lang_translate('installer-install-passwords-notmatch','The root passwords do not match!');
			$password2 = '';
		}
	}
	
	$content = '<form action="./index.php" method="post">
	<div class="passwords">
		<div class="password_text">' . lang_translate('installer-install-password-root','Root password') . '</div>
		<input class="password" name="password" type="password" value="' . htmlspecialchars($password,ENT_QUOTES,'UTF-8') . '" />
		<div class="errorPassword">' . $errorPassword . '</div>
		<div class="password2_text">' . lang_translate('installer-install-password-retype','Retype password') . '</div>
		<input class="password2" name="password2" type="password" value="' . htmlspecialchars($password2,ENT_QUOTES,'UTF-8') . '" />
	</div>
	<div class="options">
		<div class="hostname_text">' . lang_translate('installer-install-systemname','Systemname') . '</div>
		<input class="hostname" name="hostname" type="text" value="' . htmlspecialchars($hostname,ENT_QUOTES,'UTF-8') . '" />
		<div class="errorHostname">' . $errorHostname . '</div>
		<div class="allow_register_text">' . lang_translate('installer-install-allow-creation','Allow users to create accounts') . '</div>
		<input ';
	if ($allow_register == 'on') {
		$content .= 'checked="checked" ';
	}
	$content .= 'class="allow_register" name="allow_register" type="checkbox" />
	</div>
	<div class="button"><input class="iButton" type="submit" value="' . lang_translate('installer-install-do','Install eyeOS!') . '" /></div>
</form>';
	output_do($content,'install',lang_translate('installer-install-title','Please fill out the following form to install eyeOS:'));
}

function install_do($install,$allow_register,$hostname,$password) {
	if ($allow_register == 'on') {
		$allow_register = 1;
	} else {
		$allow_register = 0;
	}
	
	if ($install != '2') {
		// Untar install.eyepackage
		if (TYPE_INSTALL === 1) {
			include_once(INSTALL_DIR . 'pcl-tar.eyecode');
			if (!PclTarExtract(INSTALL_PACKAGE,'./','','tgz')) {
				output_errors(array(lang_translate('installer-install-error-extract','The install files could not be extracted. To install eyeOS manually, rename "[package]" to "package.tar.gz" and extract the contents into the same directory.',array('[package]' => INSTALL_PACKAGE))));
			}
		}
		
		// Rename dir
		$hash = 'eyeOS' . substr(md5(uniqid(rand())),0,10);
		if (!path_move('./eyeOS/','./' . $hash . '/')) {
			output_errors(array(lang_translate('installer-install-error-renaming','Could not rename the directory "./eyeOS/". Please, rename it manually to "[hash]".',array('[hash]' => $hash))),array('./index.php?install=2&amp;hash=' . $hash,lang_translate('installer-install-continue','Continue...')),0);
		}
	} else {
		$hash = $_REQUEST['hash'];
	}
	
	// Create module files
	$xml = xml_xml2array(file_get_contents(INSTALL_DIR . 'files/modules.xml'));
	foreach ($xml['modules'][0] as $module => $files) {
		@mkdir('./' . $module . '/');
		foreach ($files[0] as $file => $content) {
			if (strpos($file,'/')) {
				path_create('./' . $module . '/' . substr($file,0,strrpos($file,'/')));
			}
			file_put_contents('./' . $module . '/' . $file,base64_decode($content[0]));
		}
	}
	
	// system.xml
	$systemxml = xml_xml2array(file_get_contents('./' . $hash . '/system/conf/system.xml'));
	$systemxml['CONFIG'][0]['ALLOW_USER_REGISTER'][0] = $allow_register;
	$systemxml['CONFIG'][0]['EYEOS_HOSTNAME'][0] = $hostname;
	if (strpos(realpath('./'),'\\')) {
		$systemxml['CONFIG'][0]['OFFICE_COMMAND'][0] = 'C:\\Program Files\\OpenOffice.org 2.4\\program\\';
		$systemxml['CONFIG'][0]['OFFICE_SEPARATOR_ARG'][0] = '"';
	}
	file_put_contents('./' . $hash . '/system/conf/system.xml',xml_array2xml($systemxml));
	// root.xml
	$rootxml = xml_xml2array(file_get_contents('./' . $hash . '/accounts/rt4/root.xml'));
	$rootxml['eyeUser'][0]['password'][0] = md5($password . md5($password));
	$rootxml['eyeUser'][0]['createDate'][0] = time();
	file_put_contents('./' . $hash . '/accounts/rt4/root.xml',xml_array2xml($rootxml));
	
	// index.php
	copy(INSTALL_DIR . 'files/index.txt','./index.php');
	// settings.php
	$settingsphp = file_get_contents(INSTALL_DIR . 'files/settings.txt');
	$settingsphp = str_replace("define('REAL_EYE_ROOT','eyeOS');","define('REAL_EYE_ROOT','" . $hash . "');",$settingsphp);
	file_put_contents('./settings.php',$settingsphp);
	
	// Remove the installer
	output_remove();
}
?>