<?php
/*
  ___  _ __   ___ _   _  ___
 / _ \| '_ \ / _ \ | | |/ _ \
| (_) | | | |  __/ |_| |  __/
 \___/|_| |_|\___|\__, |\___|
                  |___/

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

Copyright © 2005 - 2010 eyeos Team (team@eyeos.org)
             since 2010 Lars Knickrehm (mail@lars-sh.de)
*/

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

function install_compat() {
	$content = '<div style="height: 285px; left: 50px; overflow: auto; position: absolute; top: 100px; width: 625px;">
<table style="width: 100%;">
	<tr>
		<th colspan="2">' . lang_translate('installer-install-required','Required') . '</th>
		<th>Yours</th>
	</tr>';
	$content .= private_install_compat('mbstring.func_overload', '0', 'red');
	$content .= private_install_compat('safe_mode', '0', 'red', version_compare(PHP_VERSION, '5.4.0', '<'));
	$content .= private_install_compat('session.use_cookies', '1', 'red');
	$content .= private_install_compat('zend.ze1_compatibility_mode', '0', 'red', version_compare(PHP_VERSION, '5.3.0', '<'));
	$content .= '<tr><td colspan="3">&nbsp;</td></tr><tr><th colspan="2">' . lang_translate('installer-install-recommended','Recommended') . '</th><th>Yours</th></tr>';
	$content .= private_install_compat('allow_url_fopen', '1', 'orange');
	$content .= private_install_compat('arg_separator.output', '&amp;', 'orange');
	$content .= private_install_compat('disable_classes', '', 'orange');
	$content .= private_install_compat('disable_functions', '', 'orange');
	$content .= private_install_compat('error_reporting', '0', 'orange');
	$content .= private_install_compat('file_uploads', '1', 'orange');
	$content .= private_install_compat('magic_quotes_gpc', '0', 'orange', version_compare(PHP_VERSION, '5.4.0', '<'));
	$content .= private_install_compat('magic_quotes_runtime', '0', 'orange', version_compare(PHP_VERSION, '5.4.0', '<'));
	$content .= private_install_compat('magic_quotes_sybase', '0', 'orange', version_compare(PHP_VERSION, '5.4.0', '<'));
	$content .= private_install_compat('register_globals', '0', 'orange', version_compare(PHP_VERSION, '5.4.0', '<'));
	$content .= '</table>
	</div>
<form action="./index.php?continue=1" method="post">
	<div class="button"><input class="iButton" type="submit" value="' . lang_translate('installer-install-continue','Continue') . '" /></div>
</form>';
	output_do($content, 'install', lang_translate('installer-install-compat','Please check your PHP settings before installing oneye.'));
}

function private_install_compat($varname, $default, $color, $condition = true) {
	$return = '';
	if ($condition === true) {
		$return .= '<tr><td style="width: 1px;">' . htmlspecialchars($varname, ENT_NOQUOTES, 'UTF-8') . '</td><td style="width: 50%;">= ' . htmlspecialchars($default, ENT_NOQUOTES, 'UTF-8') . '</td>';
		$style = '';
		$varname = ini_get($varname);
		if ($varname !== $default) {
			$style .= ' background-color: ' . $color . ';';
		}
		$return .= '<td style="width: 50%;' . $style . '">' . htmlspecialchars($varname, ENT_NOQUOTES, 'UTF-8') . '</td></tr>';
	}
	return $return;
}

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 system name!');
		}
		$hostname = 'oneye';
	}
	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','System Name') . '</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 oneye!') . '" /></div>
</form>';
	output_do($content,'install',lang_translate('installer-install-title','Please fill out the following form to install oneye:'));
}

function install_do($install,$allow_register,$hostname,$password) {
	if ($allow_register == 'on') {
		$allow_register = 1;
	} else {
		$allow_register = 0;
	}
	
	$hash = $_REQUEST['hash'];
	if ($install != '2' || !$hash || !is_dir('./' . $hash . '/')) {
		// 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 oneye manually, rename "[package]" to "package.tar.gz" and extract the contents into the same directory.',array('[package]' => INSTALL_PACKAGE))));
			}
		}
		// Create hash
		if (!$hash) {
			$hash = 'system_' . substr(md5(uniqid(rand())),0,10);
		}
		// Rename dir
		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;allow_register=' . $allow_register . '&amp;hostname=' . $hostname . '&amp;password=' . $password . '&amp;password2=' . $password . '&amp;hash=' . $hash,lang_translate('installer-install-continue','Continue...')),0);
		}
	}
	
	// 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();
}
?>