<?php
	session_name('library');
	session_start();
	
	include("functions.php");
	
	head('Change Account Informations');
?>

<body>
<div id="main">

	<?php
		include("header.php");
		include("menu.php");
	?>

		
	

<div id="entire_panel">
		<h1>Uprav informácie</h1>
		
		<?php
		
		$user_id = $_GET['id'];
		protect($user_id);
		$_SESSION['user_edit'] = $user_id;	 


if (($_SESSION['admin'] == 1) || ($_SESSION['uid'] == $user_id) ) {
	if(!isset($_POST['submit'])){ 
	
		$link = connect_db();
							
		$sql = "SELECT * FROM users WHERE uid ='". $user_id ."'";
		$result = mysql_query($sql) or die(mysql_error());
		$row = mysql_fetch_assoc($result);
	
	?>
	<table border=1 class="center_page">
	<form name=register method=post action="change_info.php?id=<?php echo $user_id ?>" enctype="multipart/form-data">
	<?php
	if ($user_id == $_SESSION['uid']) {
	?>
		<tr>
			<td>Username:</td>
			<td><input type=text name=username maxlength=32 value="<?php echo $row['username']; ?>"></td>
		<tr>
		<tr>
			<td>Password:</td>
			<td><input type=password name=password maxlength=64></td>
		</tr>
		<tr>
			<td>Re-enter password:</td>
			<td><input type=password name=passconf maxlength=64></td>
		</tr>
		<tr>
			<td>Email:</td>
			<td><input type=text name=email value="<?php echo $row['email']; ?>"></td>
		</tr>
		<tr>
			<td>Name & Surname:</td>
			<td>
				<input type=text name=name maxlength=20 value="<?php echo $row['name']; ?>">
				<input type=text name=surname maxlength=32 value="<?php echo $row['surname']; ?>">
			</td>
		</tr>
		<?php } if ($_SESSION['admin'] == 1) { ?>
		
		<tr>
			<td>Status:</td>
			<td>
			<?php

				echo '<select name="status" size="1">';
				
				echo '<option value="0"';
				if ($row['status'] == 0) { echo 'selected="selected"'; }
				echo '>Blokovaný</option>';
						
				echo '<option value="1"';
				if ($row['status'] == 1) { echo 'selected="selected"'; } 
				echo '>Štandardný</option>';
							
				echo '</select> ';
			?>
			</td>
		</tr>
		<tr>
			<td>Admin:</td>
			<td>
			<?php
				echo '<select name="admin" size="1">';
				
				echo '<option value="0"';
				if ($row['admin'] == 0) { echo 'selected="selected"'; }
				echo '>Bežný používateľ</option>';
				
				echo '<option value="1"';	
				if ($row['admin'] == 1) { echo 'selected="selected"'; }
				echo '>Administrátor</option>';
				
				echo '</select> ';
					 
			?>
			</td>
		</tr>
		
		<?php } if ($user_id == $_SESSION['uid']) { ?>
		
		<tr>
			<td>Photo:</td>
			<td>
				<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
				<input type="file" accept="image/jpeg, image/gif, image/png" name="image" />
				<br />(podporované formáty JPEG, GIF, PNG (do 30 kB))
			</td>
		</tr>
		
		<?php } ?>
				
		<tr>
			<td colspan=2 class="button"><input name="submit" type="submit" id="submit" value="Aktualizuj"></td>
		</tr>
	</form>
	</table>
	
		<?php

	}else{
		
		if ($_SESSION['user_edit'] == $_SESSION['uid']) {
			$username = $_POST['username'];
			$password = $_POST['password'];
			$passconf = $_POST['passconf'];
			$email = $_POST['email'];
			$name = $_POST['name'];
			$surname = $_POST['surname'];
			$image = $_POST['image'];
		}
		
		if ($_SESSION['admin'] == 1) {
			$status = $_POST['status'];
			$admin = $_POST['admin'];
		}
		
		if ($_SESSION['user_edit'] == $_SESSION['uid']) {
			protect($username);
			protect($passwrod);
			protect($passconf);
			protect($email);
			protect($name);		
			protect($surname);
			protect($image);
		}
		
		if ($_SESSION['admin'] == 1) {
		protect($status);
		protect($admin);			
		}
		
	$link = connect_db();	
		
	if ($_SESSION['user_edit'] != $_SESSION['uid']) {
		$sql ="UPDATE users SET status='$status', admin='$admin' WHERE uid='" . $_SESSION['user_edit'] . "'";
		$result = mysql_query($sql, $link) or die(mysql_error());
		echo '<p id="bold">The status and privileges were changed successfully.</p>';
	} else {
		
		$msg="";
		
		if (isset($username) && isset($password) && isset($passconf) && isset($email) && isset($name) && isset($surname)){
			if(strlen($username) < 3 || strlen($username) > 32){
				$msg .= 'Username is either too short or too long' . '<br />';
			}
			
			if(strlen($password) < 3 || strlen($password) > 64){
				$msg .= 'Password is either too short ot too long' . '<br />';
			}
			
			if(strlen($email) < 3 || strlen($email) > 64){
				$msg .= 'Email is either too short ot too long' . '<br />';
			}
			
			if(strlen($name) < 2 || strlen($name) > 64){
				$msg .= 'Your name is either too short or too long' . '<br />';
			}
			
			if(strlen($surname) < 2 || strlen($surname) > 64){
				$msg .= 'Your surname is either too short or too long' . '<br />'; 
			}
			
			if($password != $passconf){
				$msg .= 'Your password do not match' . '<br />';
			}
							
			if(!check_email($email)){
				$msg .= 'The email you entered is incorrect' . '<br />';
			}
						
			$sql = "SELECT * FROM users WHERE username ='". $username ."' AND uid !='". $_SESSION['user_edit'] ."'";
			$result = mysql_query($sql) or die(mysql_error());
			if(mysql_num_rows($result) > 0){
				$msg .= 'This username already exists' . '<br />';
			}
			
			$sql = "SELECT * FROM users WHERE email ='". $email ."' AND uid !='". $_SESSION['user_edit'] ."'";
			$result = mysql_query($sql) or die(mysql_error());
			if(mysql_num_rows($result) > 0){
				$msg .= 'The email you supplied is already in use' . '<br />';
			}
	
			if(empty($msg)){
				$password = md5($password);
				
				if ($_SESSION['admin'] == 1) {
					$sql ="UPDATE users SET username='$username', password='$password', name='$name', surname='$surname', email='$email', status='$status', admin='$admin' WHERE uid='" . $_SESSION['user_edit'] . "'";
				} else {
					$sql ="UPDATE users SET username='$username', password='$password', name='$name', surname='$surname', email='$email' WHERE uid='" . $_SESSION['user_edit'] . "'";
				}
				
				$result = mysql_query($sql, $link) or die(mysql_error());
				echo '<p id="bold">The information details was changed successfully.</p>';
				
				if (isset($_FILES['image'])) {
					if ($_FILES['image']['error'] == UPLOAD_ERR_OK) {
						if (is_uploaded_file($_FILES['image']['tmp_name'])) {
							$nazov = $_FILES['image']['name'];
							$successful = move_uploaded_file($_FILES['image']['tmp_name'], 'images/profiles/' . $nazov);
							if ($successful) {
								echo '<p>Obrázok knihy bol nahratý na server.</p>';
							} else {
								echo '<p>Obrázok knihy NEbol nahratý na server.</p>';
							}

							$sql2 = "UPDATE users SET photo='" . $nazov. "' WHERE name='$name' LIMIT 1;";
							$result2 = mysql_query($sql2, $link);
							if ($result2) {
								echo '<p><strong>Obrázok knihy bol úspešne nahratý</strong></p>';
							} else {
								echo '<p id="red">Obrázok knihy NEbol nahratý, lebo nastal problém pri zápise do databázy - ' . mysql_errno($link) . ': ' . mysql_error($link) . '</p>';
							}
							mysql_close($link);
						}
					} else { // nastane, ak bol uploadovaný súbor väcší ako upload_max_filesize alebo ako post_max_size (chyba 2)
						echo '<p id="red">Obrázok knihy NEbol aktualizovaný, lebo nastal problém pri uploadovaní súboru ' . $_FILES['image']['name'] . ' - ' . $_FILES['image']['error'] . '</p>';
					}
				} else {
					echo '<p id="red">Problem s obrazkom</p>';
				}
				
			} else {
				echo '<p id="red">' . $msg . '</p>';
			}
			
		}
		}
		
	}
} else {
	echo '<p id="red">K tejto stránke nemáte dostatočné oprávnenia</p>';
}
    
		?>				
</div>

	<?php include("footer.php"); ?>

</div>
</body>
</html>