<?php
include_once('php/php_headers.php');
if($_SESSION['ekdhlos_level']<>0)header('Location: insert_messages.php');
session_null('insert_users');
$scripts = '<script src="js/formfunctions.js" type="text/javascript"></script>';
html_header('Εισαγωγή χρηστών',$scripts);
$f_name=$no_f_name=$l_name=$no_l_name=$email=$no_email=$pass=$no_pass=$pass2=$no_pass2=$level2=$no_level=$enable2=$no_enable=$msg='';
$level1=$enable1='checked';
if(isset($_POST['submit'])){
	$insert1=$insert2=$insert3=$insert4=$insert5=false;
	$f_name  = clean($_POST['f_name']);
	$l_name  = clean($_POST['l_name']);
	$email   = clean($_POST['email']);
	$pass    = clean($_POST['pass']);
	$pass2   = clean($_POST['pass2']);
	
	if($_POST['level'] == 1){$level = 1; $level1 = 'checked';$level2 = '';}
	else{$level = 0; $level1 = '';$level2 = 'checked';}
	
	if($_POST['enable'] == 1){$enable = "yes"; $enable1 = 'checked';$enable2 = '';}
	else{$enable = "no"; $enable1 = '';$enable2 = 'checked';}
	
	if(!isset($_POST['f_name']) || empty($_POST['f_name'])) $no_f_name = '<h3 class="triangle-border left more_top">Το όνομα απαιτείται!</h3>';
	else if ((invalid_char($_POST['f_name']) <> null)){$no_f_name = invalid_char($_POST['f_name']);}
	else if (preg_match("/[^A-Za-z0-9]/", $_POST['f_name']))$no_f_name = '<h3 class="triangle-border left more_top">Μη αποδεκτοί χαρακτήρες!</h3>';
	else if(mb_strlen($_POST['f_name'], 'UTF-8') <2) $no_f_name = '<h3 class="triangle-border left more_top">Εισάγετε πάνω από δύο χαρακτήρες!</h3>';
	else if(mb_strlen($_POST['f_name'], 'UTF-8') >=11) $no_f_name = '<h3 class="triangle-border left more_top">Εισάγετε μέχρι 10 χαρακτήρες!</h3>';
	else $insert1 = true;
	
	if(!isset($_POST['l_name']) || empty($_POST['l_name'])) $no_l_name = '<h3 class="triangle-border left more_top">Το επώνυμο απαιτείται!</h3>';
	else if ((invalid_char($_POST['l_name']) <> null)){$no_l_name = invalid_char($_POST['l_name']);}
	else if (preg_match("/[^A-Za-z0-9]/", $_POST['l_name']))$no_l_name = '<h3 class="triangle-border left more_top">Μη αποδεκτοί χαρακτήρες!</h3>';
	else if(mb_strlen($_POST['l_name'], 'UTF-8') <2) $no_l_name = '<h3 class="triangle-border left more_top">Εισάγετε πάνω από δύο χαρακτήρες!</h3>';
	else if(mb_strlen($_POST['l_name'], 'UTF-8') >=11) $no_l_name = '<h3 class="triangle-border left more_top">Εισάγετε μέχρι 10 χαρακτήρες!</h3>';
	else $insert2 = true;
	
	if(!isset($_POST['email']) || empty($_POST['email'])) $no_email = '<h3 class="triangle-border left more_top">Το email απαιτείται!</h3>';
	else if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $_POST['email'])) $no_email = '<h3 class="triangle-border left more_top">Το email δεν είναι έγκυρο!</h3>';
	else if (check("users",$_POST['email'],"email"))$no_email = '<h3 class="triangle-border left more_top">Το email χρησιμοποιείται!</h3>';
	else $insert3 = true;
	
	if(!isset($_POST['pass'] ) || empty($_POST['pass'] )) $no_pass = '<h3 class="triangle-border left more_top">Το password απαιτείται!</h3>';
	else if(mb_strlen($_POST['pass'],ENCODING)<5) $no_pass = '<h3 class="triangle-border left more_top">Απαιτούνται τουλάχιστον 5 χαρακτήρες!</h3>';
	else if(mb_strlen($_POST['pass'],ENCODING)>50) $no_pass = '<h3 class="triangle-border left more_top">Απαιτούνται μέχρι 50 χαρακτήρες!</h3>';
	else $insert4 = true;
	
	if(!isset($_POST['pass2'] ) || empty($_POST['pass2'] )) $no_pass2 = '<h3 class="triangle-border left more_top">Η επιβεβαίωση password απαιτείται!</h3>';
	else if(mb_strlen($_POST['pass2'],ENCODING)<5) $no_pass2 = '<h3 class="triangle-border left more_top">Απαιτούνται τουλάχιστον 5 χαρακτήρες!</h3>';
	else if(mb_strlen($_POST['pass2'],ENCODING)>50) $no_pass2 = '<h3 class="triangle-border left more_top">Απαιτούνται μέχρι 50 χαρακτήρες!</h3>';
	else if(isset($_POST['pass']) && ($_POST['pass'] != $_POST['pass2'])) $no_pass2 = '<h3 class="triangle-border left more_top">Τα password δεν ταιριάζουν!</h3>';
	else $insert5 = true;
	
	if($insert1 && $insert2 && $insert3 && $insert4 && $insert5){
		if (insert('users',$f_name,$l_name,$email,sha1($pass.'ekdhlos'),$level,$enable)){ 
			$msg = '<div id="msg_ok">Η εισαγωγή έγινε με επιτυχία!</div><br />'; 
			$f_name=$l_name=$email=$pass=$pass2=$level=$enable=$level2=$enable2="";
			$level1=$enable1="checked";
			echo "<script>setTimeout('window.location.href=\"".$_SERVER['REQUEST_URI']."\"', 3000)</script>";
		}
		else $msg = '<div id="msg_error">Σφάλμα κατά την εισαγωγή!</div><br />';
	}
}
$form = '
<h1 class="more_left_2">Εισαγωγή νέου χρήστη</h1>

<hr /><br />
<form method="post" accept-charset="utf-8" class="more_left_8 width">
<div id="tools">
<a href="insert_users.php" title="Εισαγωγή χρηστών"><img alt="Εισαγωγή χρηστών" src="images/icon-insert-big.png" /></a>
<a href="edit_users.php" title="Επεξεργασία χρηστών"><img alt="Επεξεργασία χρηστών" src="images/Write2.png" /></a>
<a href="logs.php" title="Τελευταίες ενέργειες"><img alt="Τελευταίες ενέργειες" src="images/logs-big.png" /></a>
</div>
    <ol class="hh2">
      <li><label for="field1">Όνομα χρήστη:</label></li>
      <li><input type="text" onkeyup="f_name_validator(); l_name_validator(); email_validator(); pass1_validator(); pass2_validator();" id="field1" class="field" name="f_name" value="'.$f_name.'" required/></li>
      <li id="f_msg"><span id="f_validate">'.$no_f_name.'</span></li>
    </ol>
    <ol class="hh2">
      <li><label for="field2">Επώνυμο χρήστη:</label></li>
      <li><input type="text" onkeyup="f_name_validator(); l_name_validator(); email_validator(); pass1_validator(); pass2_validator();" id="field2" class="field" name="l_name" value="'.$l_name.'" required/></li>
      <li id="l_msg"><span id="l_validate">'.$no_l_name.'</span></li>
    </ol>
    <ol class="hh2">
      <li><label for="field3">Email:</label></li>
      <li><input type="text" onkeyup="f_name_validator(); l_name_validator(); email_validator(); pass1_validator(); pass2_validator();" id="field3" class="field" name="email" value="'.$email.'" required/></li>
      <li id="e_msg"><span id="e1_validate">'.$no_email.'</span></li>
    </ol>
    <ol class="hh2">
      <li><label for="field4">Password:</label></li>
      <li><input type="password" onkeyup="f_name_validator(); l_name_validator(); email_validator(); pass1_validator(); pass2_validator();" id="field4" class="field" name="pass" value="'.$pass.'" required/></li>
      <li id="p1_msg"><span id="p1_validate">'.$no_pass.'</span></li>
    </ol>
    <ol class="hh2">
      <li><label for="field5">Επιβεβαίωση:</label></li>
      <li><input type="password" onkeyup="f_name_validator(); l_name_validator(); email_validator(); pass1_validator(); pass2_validator();" id="field5" class="field" name="pass2" value="'.$pass2.'" required/></li>
      <li id="p2_msg"><span id="p2_validate">'.$no_pass2.'</span></li>
    </ol>
	<ol class="hh2">
      <li><label for="field6">Επίπεδο χρήστη:</label><h5>
			<input type="radio" onclick="f_name_validator(); l_name_validator(); email_validator(); pass1_validator(); pass2_validator();" id="field6" name="level" value="1" '.$level1.'/>Απλός&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<input type="radio" onclick="f_name_validator(); l_name_validator(); email_validator(); pass1_validator(); pass2_validator();" id="field6b" name="level" value="2" '.$level2.'/>Administrator
			'.$no_level.'</h5></li>
    </ol>
	<ol class="hh2">
      <li><label for="field7">Κατάσταση:</label><h5>
			<input type="radio" onclick="checkall(); f_name_validator(); l_name_validator(); email_validator(); pass1_validator(); pass2_validator();" id="field7" name="enable" value="1" '.$enable1.'/>Ενεργός&nbsp;&nbsp;&nbsp;
			<input type="radio" onclick="checkall(); f_name_validator(); l_name_validator(); email_validator(); pass1_validator(); pass2_validator();" id="field7b" name="enable" value="0" '.$enable2.'/>Ανενεργός
			'.$no_enable.'</h5></li>
    </ol>
	<input type="submit" disabled="disabled" name="submit" onklick="f_name_validator(); l_name_validator(); email_validator(); pass1_validator(); pass2_validator();" id="submit" class="disable" value="Εισαγωγή">
'.$msg.'
</form><br />';
echo $form;
html_footer();
?>