<?php
/**
 * Checking login Data then redirect to index page
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 * @todo           Nothing
 */
ob_start();
session_start();
if(empty($_SERVER['HTTP_REFERER']) || !strpos($_SERVER['HTTP_REFERER'],'login.php')) {
	header("location: index.php");
	exit;
}
if(empty($_SESSION["loginCheck"]) || empty($_POST["loginCheck"]) || ($_SESSION["loginCheck"]!=$_POST["loginCheck"])) {
    header("location: login.php");
    exit;
}
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
include("includeInformation.php");
unset($_SESSION["userPassword"]);
unset($_SESSION["adminName"]);
unset($_SESSION["userId"]);
unset($_SESSION["power"]);
if(empty($_POST["user"]) || empty($_POST["pass"]))
{
    header("location:login.php?res=9");
    exit();
}
$userName = trim($_POST["user"]);
$password = trim($_POST["pass"]);
$passwordMd5 = md5($password);
if(empty($userName) || empty($password))
{
    header("location:login.php?res=9");
    exit();
}

$query = "SELECT id, userName, password FROM `user_login` WHERE userName='$userName' AND power='*'";
$result = mysqli_query($conn, $query);
$num = mysqli_num_rows($result);
if(empty($num))
{
    header("location:login.php?res=3");
    exit();
}
else
{
    $row = mysqli_fetch_array($result);
	if($row['password'] != $passwordMd5)
	{
		header("location:login.php?res=3");
		exit();
	}
}
$_SESSION["userPassword"] = $row["password"];
$_SESSION["adminName"] = $row["userName"];
$_SESSION["userId"] = $row["id"];
$_SESSION["power"] = '*';
$_SESSION["Language"] = 1;

if(!empty($_POST['remember']))
{
	setcookie("cookname", $_SESSION['adminName'], time()+60*60*24*100, "/");
	setcookie("cookpass", $_SESSION['userPassword'], time()+60*60*24*100, "/");
}
else
{
	setcookie("cookname", "", time()-60*60*24*100, "/");
	setcookie("cookpass", "", time()-60*60*24*100, "/");
}
$queryInsertMove = "INSERT INTO `move_archive` (userId,`type`,`date`) VALUES ";
$queryInsertMove .= "('".$_SESSION["userId"]."','1','".time()."')";
$resultInsertMove = mysqli_query($conn, $queryInsertMove);   
header("location: users.php");
exit();
ob_end_flush();
?>
