<?php
	session_name('library');
	session_start();
	
	include("functions.php");
	
	head('Registration Page');
?>

<body>
<div id="main">

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

		
	

<div id="entire_panel">
		<h1>Zaregistruj sa</h1>
		<?php
			 

	if(!isset($_POST['submit'])){ ?>
	<table border=1 class="center_page">
	<form name=register method=post action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
		<tr>
			<td>Username:</td>
			<td><input type=text name=username maxlength=32></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></td>
		</tr>
		<tr>
			<td>Name & Surname:</td>
			<td>
				<input type=text name=name maxlength=20>
				<input type=text name=surname maxlength=32>
			</td>
		</tr>
		<tr>
			<td>Photo:</td>
			<td>
				<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
				<input type="file" accept="image/gif" name="image" />
				<br />(podporované formáty JPEG, GIF, PNG (do 30 kB))
			</td>
		</tr>		
		<tr>
			<td colspan=2 class="button"><input name="submit" type="submit" id="submit" value="Registruj"></td>
		</tr>
	</form>
	</table>
	
		<?php

        }else{
		
			$username = $_POST['username'];
			$password = $_POST['password'];
			$passconf = $_POST['passconf'];
			$email = $_POST['email'];
			$name = $_POST['name'];
			$surname = $_POST['surname'];
			
			protect($username);
			protect($passwrod);
			protect($passconf);
			protect($email);
			protect($name);
			protect($surname);
		
			$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 />';
				}
				
				$link = connect_db();
							
				$sql = "SELECT * FROM users WHERE username ='". $username ."'";
				$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 ."'";
				$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);
					$date = date("y-m-d");
					
					$sql = "INSERT INTO users SET username='$username', password='$password', name='$name', surname='$surname', email='$email', photo='default.gif', date_registered='$date'";
					$result = mysql_query($sql, $link) or die(mysql_error());
					echo 'Thank you for registering, you may now log in' . '<br />';
					
					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>';
				}
			}
		}
    
		?>				
</div>

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

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