<?php
/**
 * Processing change password form data
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    17-08-2020
 * @edited_by
 * @last_edit_date
 * @todo           Nothing
 */
ob_start();
session_start();
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
include("validateLogin.php");
include("includeInformation.php");

if(empty($_POST["newPass"]) || empty($_POST["oldPass"]) || empty($_POST["newPass2"])) {
    header("Location: changePassword.php?res=2");
    exit;
}
$_POST["oldPass"] = md5($_POST["oldPass"]);
$_POST["newPass"] = mysqli_real_escape_string($conn, $_POST["newPass"]);
$md5Password = md5($_POST["newPass"]);
$queryChangePass = "select id, userName, password FROM `user_login` WHERE `userName`='".$_SESSION["adminName"]."'";
$resultChangePass = mysqli_query($conn, $queryChangePass);
$rowChangePass = mysqli_fetch_row($resultChangePass);
if($rowChangePass[2] == $_POST["oldPass"]) {
	
	$query = "UPDATE `user_login` SET";
	$query .=" password='".$md5Password."'";
	$query .=" WHERE `id`='".$_SESSION["userId"]."'";
	$result = mysqli_query($conn, $query);
	if($result) {
		$queryInsertMove = "INSERT INTO `move_archive` (userId,`type`,`date`) VALUES ";
		$queryInsertMove .= "('".$rowChangePass[0]."','3','".time()."')";
		$resultInsertMove = mysqli_query($conn, $queryInsertMove);
		 
		$_SESSION["userPassword"] = $_POST["newPass"];
		if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass']))
			setcookie("cookpass", $_POST["newPass"], time()+60*60*24*100, "/"); 
		
		header("location:changePassword.php?res=35");
		exit;
	}
}
header("Location: changePassword.php?res=2");
exit;
ob_end_flush();
?>
