<?php
$DB_DATABASE = "";
$prefix = "iitech_";
if(isset($_POST['database'])){
	$DB_DATABASE = $prefix.$_POST['database'];
}

// include db connect class
require_once __DIR__ . '/include/new_db_connect.php';

// connecting to db
$db = connect($DB_DATABASE);

require_once 'include/user.php';
require_once 'include/cryptage.php';
$chiffrer=new chiffrement();

$username = "";
$password = "";
$email = "";
$matricule = "";
$phone  = "";
$phoneDad = "";
$tokenId = "";

if(isset($_POST['username'])){
	$username = $_POST['username'];
}
if(isset($_POST['password'])){
    $password = $_POST['password'];
}
if(isset($_POST['email'])){
	$email = $_POST['email'];
}

if(isset($_POST['matricule'])){
    $matricule = $_POST['matricule'];
}
if(isset($_POST['phone'])){
	$phone = $_POST['phone'];
}
if(isset($_POST['date_naiss'])){
	$date_nais = $_POST['date_naiss'];
}
if(isset($_POST['phoneDad'])){
	$phoneDad = $_POST['phoneDad'];
}

if(isset($_POST['tokenId'])){
	$tokenId = $_POST['tokenId'];
}

// Instance of a User class
$userObject = new User();

// Registration of new user
if(!empty($username) && !empty($password) && !empty($email) && !empty($matricule) && !empty($phone) && !empty($phoneDad)){
	$password=htmlspecialchars($password);
	$password=$chiffrer->encrypt($password);	
	$json_registration = $userObject->createNewRegisterUserR($username, $password, $email, $matricule, $phone, $date_nais, $phoneDad, $db1, $db);
	
	echo json_encode($json_registration);
}

// User Login
if(!empty($username) && !empty($password) && empty($email) && empty($matricule) && empty($phone) && empty($phoneDad)){
	$password=htmlspecialchars($password);
	$password=$chiffrer->encrypt($password);	
    $json_array = $userObject->loginUsersR($username, $password, $db1, $db);
    echo json_encode($json_array);
}

?>
