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

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

// connecting to db
$db1 = connect("iitech_universite");

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

require_once 'include/user.php';

$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)){
	$hashed_password = crypt($password , 'superhashcode');
	$json_registration = $userObject->createNewRegisterUser($username, $hashed_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)){
	$hashed_password = crypt($password, 'superhashcode');	
    $json_array = $userObject->loginUsers($username, $password, $db1, $db);
    echo json_encode($json_array);
}

?>