<?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
$bdd = connect("iitech_universite");

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

require_once 'include/user.php';
require_once 'include/cryptage.php';
require_once '/home/iitech/progiciel.geststudent.net/utiles.php';
$chiffrer=new chiffrement();

$username = "";

$password1 = "";


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

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


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

// User Login
if(!empty($username) && !empty($password1)){

    $query = "select matricule  from users_student where username = :username Limit 1";
    $statement = $bdd->prepare($query);
    $statement->bindParam(':username', $username, PDO::PARAM_STR);
    $statement->execute();
    $tab1=$statement->fetch(PDO::FETCH_ASSOC);
    $matricule		 	= $tab1['matricule'] ;//adressepersonnelle du parent
    
    $query = "select email, emailp  from tabetudiant where matricule = :matricule Limit 1";
    $statement = $bdd1->prepare($query);
    $statement->bindParam(':matricule', $matricule, PDO::PARAM_STR);
    $statement->execute();
    $tab1=$statement->fetch(PDO::FETCH_ASSOC);
    $email		 	= $tab1['email'] ;//adressepersonnelle de l'étudiant
    $emailp		 	= $tab1['emailp'] ;//adressepersonnelle du parent

    $message= "Bonjour, <br /><br />VOTRE COMPTE GestStudent A ETE MISE A JOUR AVEC SUCCES. <br /><br />Username : $username <br /><br />Mot de passe : $password1<br /><br />IITECH-BENIN POUR DES SOLUTIONS INNOVANTES.";
    $message    = str_replace('<br />', "\n", $message);// message trait&eacute; pour les sauts de ligne
    $admin_email = "iitech@iitech-benin.net";
    $subject = 'Mise à jour de votre mot de passe GestStudent';
    //send email
    $retour1 = mail($email, "$subject", $message, "From:" . $admin_email);
    $retour2 = mail($emailp, "$subject", $message, "From:" . $admin_email);
    
	/*$query = "select phone  from users_student where username = :username";
	$statement = $bdd->prepare($query);
	$statement->bindParam(':username', $username, PDO::PARAM_STR);
	$statement->execute();
	$tab=$statement->fetch(PDO::FETCH_ASSOC);
	$phone = '229';
	$phone  .= $tab['phone'];

    $sms = 'BONJOUR.  VOTRE COMPTE GestStudent A EST MISE A JOUR AVEC SUCCES. [Username = '.$username.'] [Mot de passe = '.$password1.']';
    $sms .= ' IITECH-BENIN POUR DES SOLUTIONS INNOVANTES.';

    sendSms($phone, $sms, 'GestStudent', $datetime = '');*/

	$password=htmlspecialchars($password1);
	$password=$chiffrer->encrypt($password);	
    $json_array = $userObject->updatePassword($username, $password, $bdd);
    echo json_encode($json_array);
}

?>