<?php
/*
ini_set('display_errors',1);
error_reporting( E_ALL );
*/

require 'acdet.php';

//$spam = $_POST['today'];
$realName = $_POST['realName'];
$userName = $_POST['userName'];
$userPass = $_POST['userPass'];
$userEmail = $_POST['userEmail'];

//response variable for javascript to use
$response = "";

//create variables with user folders paths for imgs and other uploads
$main_dir = "../datBundle/".$userName;
$dashCam_dir = $main_dir."/dashCam";
$detectCam_dir = $main_dir."/detectCam";
$myShapes_dir = $main_dir."/myShapes";
$myLoc_dir = $main_dir."/myLoc";
$arrDirs = array($main_dir, $dashCam_dir, $detectCam_dir, $myShapes_dir, $myLoc_dir);

//create custom xml users folder
$customXML = "../shapeDetect/".$userName."/myCustomXML";

//the first conditions of if is for honney trapping bots
if (!empty($_POST['today'])) {
    $response = "Registration FULLY successfull.";
    echo $response;
    //if the honney pot field isn't filled the registration continues
} else {
    //Mysql connection
    $mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

    $realName = $mysqli->real_escape_string($realName);
    $userName = $mysqli->real_escape_string($userName);
    $userPass = $mysqli->real_escape_string($userPass);
    $userEmail = $mysqli->real_escape_string($userEmail);

    //authentification key sent to mail for validation
    //not used because the emails can take time to send
    //$vKey = md5(time().$userName);

    //pass algo
    $iterations = 1000;
    $salt = count($userName) * 23;
    $hashPass = hash_pbkdf2("sha256", $userPass, $salt, $iterations, 25);

    // Check connection
    /*
    if ($mysqli->connect_error) {
        die("Connection failed: " . $mysqli->connect_error);
    } else {
        echo "Connected successfully";
    }
    */
    $sqli = "INSERT INTO accounts (realname, username, userpass, email) VALUES ('$realName', '$userName', '$hashPass', '$userEmail')";

    if($mysqli->query($sqli) === true){
        $response = "Registration successfull.";
        //create user folders
        for ($i = 0; $i < count($arrDirs); $i++) {
            $target_dir = $arrDirs[$i];
            if(!file_exists($target_dir)) {
                //echo "folder does not exists";
                mkdir($target_dir,0751, true);
            }
        }

        if(!file_exists($customXML)) {
            //echo "folder does not exists";
            mkdir($customXML,0755, true);
        }
        
        $subject = "myGuardCam registration successful";
        $message = "Thank you, ".$realName.", for registering as ".$userName." to myguardcam.com ! "
        ."\r\n"
        ."\r\n"."By registering, you accepted the terms and conditions of myguardcam website"
        ."\r\n"."http://www.myguardcam.com/terms-and-conditions.html"
        ."\r\n"
        ."\r\n"."This is an automated email, do not reply!";
        $headers = "From: " . "register@myguardcam.com";
        mail($userEmail, $subject, $message, $headers);
    } else{
        $response .= "ERROR: Could not able to execute connect to database" . $mysqli->error;
    }

    $mysqli->close();

    echo $response;
}
?>