<?php
require 'mailer/src/Exception.php';         //knihovna mail
require 'mailer/src/PHPMailer.php';
require 'mailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

session_start();
ob_start();
if (isset($_SESSION['us_name']) AND isset($_SESSION['us_password'])AND $_SESSION['admin']==1){
	require_once("db_connect.php");
	$us_name=htmlspecialchars($_POST["us_name"]);
	$us_mail=htmlspecialchars($_POST["us_mail"]);
	$us_password=htmlspecialchars($_POST["us_password"]);
	$us_password2=htmlspecialchars($_POST["us_password2"]);


	if(isset($_POST["mail"])){
		$mail=htmlspecialchars($_POST["mail"]);
	}else{
		$mail=0;
	}

	if(isset($_POST["admin"])){
		$admin=htmlspecialchars($_POST["admin"]);
	}else{
		$admin=0;
	}






	if($us_password!=$us_password2){
		$_SESSION['error']="Hesla se neshodují";
		header("Location: new_user.php");
	}elseif (strlen($us_password)<=6) {
		$_SESSION['error']="Heslo musí obsahovat minimálně 6 znaků";
		header("Location: new_user.php");
	}else{
		$sql="SELECT us_mail from users WHERE us_mail='$us_mail'";
		$sql2="SELECT us_name from users WHERE us_name='$us_name'";
		$result = $conn->query($sql);
		$result2 = $conn->query($sql2);
        if ($result->num_rows > 0) {
                $_SESSION['error']="E-mail je jíž zaregistrovaný";
                header("Location: new_user.php");
        }elseif($result2->num_rows > 0){
        		$_SESSION['error']="Uživatelské jméno je již zaregistrováno";
                header("Location: new_user.php");
        }else{
        		$us_password=md5($us_password);
        		$sql="INSERT INTO users (us_name, us_mail, us_password, admin, send_mail) VALUES ('$us_name','$us_mail', '$us_password', '$admin', '$mail')";
        		if ($conn->query($sql) === TRUE) {
				    
								$mail = new PHPMailer;                                          //mail start
					            $mail->isSMTP();    
					            $mail->Host = "smtp.gmail.com";
					            $mail->SMTPAuth = true;
					            $mail->Username = $mailuser;
					            $mail->Password = $mailpassword;
					            $mail->SMTPSecure = "tls";
					            $mail->Port = 587;
					            $mail->CharSet = "utf-8"; 
					            $mail->isHTML(TRUE);
					            $mail->setFrom($mailuser, "Be secured"); 
								$mail->addAddress($us_mail);
					                 

					            $mail->Subject = "Nový účet";      
					            $html="Vítejte v aplikaci Be secured, <br> Uživatelské jméno: " . $us_name . "<br>Heslo: " . $us_password2 . "<br><br>K přihlášení použijte Váš E-mail.";
					            $mail->Body = $html;
					            if($mail->send()){
					            	$_SESSION['error']="Úspěšně vytvořeno";
					              	header("Location: new_user.php");
					            }else{
					            	//$_SESSION['error']="Chyba v odeslaní E-mailu";
					            	$_SESSION['error']=$mail->ErrorInfo;
					                header("Location: new_user.php");
					            }      
					                                                                 //mail end




				    header("Location: new_user.php");
				} else {
				    $_SESSION['error']="Chyba";
				    header("Location: new_user.php");
				}
        }


	}

	


}else{
  header ("Location: user.php"); 
}
ob_end_flush();
?>