<?php
session_start();

header('Content-Type: text/html; charset=utf-8');

// kontrola, jestli ěnjaký vtipálek nepřeskočil polovinu instalace
if (isset($_SESSION['last_installation_step'])) {

	$last_step = "mysql";
	$current_step = "create_user";

	if ($_SESSION['last_installation_step'] == $last_step) {
		$_SESSION['last_installation_step'] = $current_step;
	}

	if ($_SESSION['last_installation_step'] != $current_step) {
		header("location: ./".$last_step.".php");
		die();
	}
} else {
	header("location: ./install_index.php");
}

if (!empty($_POST)) {

	if (isset($_POST['login']) && isset($_POST['password']) && isset($_POST['password_again']) && isset($_POST['email']) && isset($_POST['about']) && isset($_POST['name'])) {

		if ($_POST['password'] == $_POST['password_again']) {

			// přihlásíme se k databázi (protože už můžeme)
			include "./include/settings.inc.php";

			// vybereme si přístupová práva
			$perms_select = $db->sql_query("SELECT `authorization` FROM `".DB_PREFIX."_permissions`");

			$perms = array();

			while (list ($auth) = $db->fetch($perms_select)) {
				$perms[] = $auth;
			}

			// vytvoříme uživatele
			$user = new UserManagement;

			$status = $user->create_users($_POST['login'], $_POST['password'], $perms, $_POST['name'], $_POST['email']);

			if ($status == "ok") {

				mysql_query("CREATE TRIGGER `".DB_PREFIX."_permissions_ai` AFTER INSERT ON `".DB_PREFIX."_permissions`
					FOR EACH ROW INSERT INTO `".DB_PREFIX."_users_permissions` VALUES('1', NEW.`id`)");

				$db->sql_query("UPDATE `".DB_PREFIX."_users` SET `about_author`='".$parsing->text($_POST['about'])."' WHERE `id`='1'");

				header("location: finish_install.php");
				exit();
			} else {
				$error = "<span class='error'>Nelze vytvořit nového administrátora. Zkus to znovu s jinými údaji</span>\n";
			}
		}
	} else {
		$error = "<span class='error'>Vyplň všechna pole</span>\n";
	}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Instalace kRS 3.2 - vytvoření uživatele</title>

<meta name='author' content='Michal Kváček' />
<meta name='description' content='' />
<meta name='keywords' content='' />
<meta name='robots' content='noindex, nofollow' />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='./admin/js/admin_functions.js'></script>

<style type='text/css'>
html {
	background: #CACACA;
	padding-top: 5em;
}

a {
	color: #353535;
	font-weight: bold;
}

.ok, .error, .not_recommanded {
	font-weight: bold;
	font-size: big;
}

.ok {
	color: #008A00;
}

.error {
	color: #810000;
}

.not_recommanded {
	color: #BD7D00;
}

#container, #logo, #installation_progress {
	width: 80%;
	border: solid 1px #C6C6C6;
	background: #DADADA;
	color: #424242;
	margin: 10px auto;
	padding: 10px;
}

#footer {
	width: 80%;
	margin: 10px auto;
	text-align: center;
	font-size: small;
}
</style>

<script type='text/javascript'>

function check_form() {

	var login = $("#login").val();
	var password = $("#password").val();
	var password_again = $("#password_again").val();
	var name = $("#name").val();
	var about = $("#about").val();
	var email = $("#email").val();

	if (login != "" && password != "" && password_again != "" && name != "" && about != "" && email != "") {

		if (password == password_again) {
			return true;
		} else {
			$("#tmp").html("<span class='error'>Zadaná hesla se neshodují!</span>\n");
			return false;
		}
	} else {
		$("#tmp").html("<span class='error'>Vyplň prosím všechna pole!</span>\n");
		return false;
	}
};

</script>
</head>
<body>

	<div id='logo'>
		<h1>Instalace kRS 3.2</h1>
	</div>

	<div id='installation_progress'><span style='color: green'>Zahájení instalace » Kontrola požadavků » Stažení a rozbalení systému » Přihlášení do databáze</span> » <em>Vytvoření uživatele</em> » Dokončení instalace</div>

	<div id='container'>

		<p>
			<h3>Vytvoření administrátora</h3>
		</p>
		<p>

			<div id='tmp'></div>

			Síla hesla: <span id='password_strength'><span style='color: red'>Nezadáno</span></span>
			<div id='passwords'></div>

			<p style='clear: both'></p>

			<form action='./create_user.php' method='post' onsubmit='if (check_form()) return true; else return false;'>
				<table>
					<tr>
						<td>Přihlašovací jméno: </td>
						<td><input type='text' name='login' id='login' /></td>
					</tr>
					<tr>
						<td>Celé jméno: </td>
						<td><input type='text' name='name' id='name' /></td>
					</tr>
					<tr>
						<td>Heslo: </td>
						<td><input type='password' onkeyup='check_password_strength("password", "password_strength");check_passwords("password", "password_again", "passwords");' name='password' id='password' /></td>
					</tr>
					<tr>
						<td>Heslo (ověření): </td>
						<td><input type='password' onkeyup='check_passwords("password", "password_again", "passwords");' name='password_again' id='password_again' /></td>
					</tr>
					<tr>
						<td>E-mail: </td>
						<td><input type='text' name='email' id='email' /></td>
					</tr>
					<tr>
						<td>Pár slov o sobě: </td>
						<td><textarea name='about' id='about' rows='5' cols='50'></textarea></td>
					</tr>
					<tr>
						<td></td>
						<td><input type='submit' value='Vytvořit' /></td>
					</tr>
				</table>
			</form>
		</p>
	</div>

	<div id='footer'>
		&copy; Michal Kváček 2012
	</div>

</body>
</html>
