<?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");
include_once("functions/pointFunction.php");
include_once("functions/sendFunctions.php");
include_once("functions/insertMsg.php");

unset($_SESSION["userPassword"]);
unset($_SESSION["adminName"]);
unset($_SESSION["userId"]);
unset($_SESSION["power"]);
unset($_SESSION["userAccount"]);
unset($_SESSION["otp"]);
if(empty($_SESSION["otpCounter"]))
    $_SESSION["otpCounter"]=0;
function sendOTP($reSendMsg=false){
    global $conn , $loginLBL, $defSender;
    $res = mysqli_query($conn,"SELECT * FROM user_login WHERE `id`='$_SESSION[OTPUserId]'");
    $rowUser = mysqli_fetch_assoc($res);
    $code = rand(1000,9999);
    mysqli_query($conn, "UPDATE `user_login` SET `otp`='".$code."' WHERE `id`=".$rowUser["id"]);

    $msg = sprintf($loginLBL["codeKey"],$rowUser["userName"],$code);
    $resultGetPoint = 1;
    if($resultGetPoint)
    {
		$msgUniqueMd5 = md5(time());
		$resultFunction = insertDBNumber(1,$defSender,$rowUser["mobile"],$msg,6,$msgUniqueMd5,0,0,0);
        if(!empty($resultFunction))
        {
            $_SESSION["otpCounter"]++;
            $_SESSION["otp"]=true;
            header("location: otp.php".($reSendMsg?"?res=249":""));
            exit();
        }
        else
        {
            header("location: login.php?res=248");
            exit();
        }
    }
}
if(!empty($_POST["check"]) && empty($_SESSION["stopReSend"]))
{
    $_SESSION["stopReSend"] = true;
    $_SESSION["otp"] = true;
    if($_SESSION["otpCounter"]<=2)
        sendOTP(true);
    exit();
}
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);
$queryGetUser = mysqli_query($conn, "SELECT id, userName, password, power, activetAcount, userKey,`mobile`,`block` FROM `user_login` WHERE BINARY `userName` like CONVERT( _latin1 '$userName'USING utf8 ) AND `power`!='*' LIMIT 0 , 1");
$num = mysqli_num_rows($queryGetUser);
if(empty($num))
{
    header("location:login.php?res=3");
    exit();
}
else
{
    $row = mysqli_fetch_array($queryGetUser);
	if($row['block']=='1')
	{
		header("location:login.php?res=4");
		exit();
	}
	if($row['password'] != $passwordMd5)
	{
		header("location:login.php?res=3");
		exit();
	}
	else
	{
		if(empty($activeC)){	
			$_SESSION["userPassword"] = $row["password"];
			$_SESSION["adminName"] = $row["userName"];
			$_SESSION["userId"] = $row["id"];
			$_SESSION["power"] = $row["power"];
			$_SESSION["userAccount"] = $userAccount;
			$_SESSION["Language"] = 1;
			if(!empty($_POST['remember'])) {
				setcookie("cookname", $_POST["user"], time() + 60 * 60 * 24, "/");
				setcookie("cookpass", $_POST["pass"], time() + 60 * 60 * 24, "/");
			} else {
				setcookie("cookname", "", time() - 60 * 60 * 24, "/");
				setcookie("cookpass", "", time() - 60 * 60 * 24, "/");
			}
			$queryInsertMove = "INSERT INTO `move_archive` (userId,`type`,`date`) VALUES ";
			$queryInsertMove .= "('".$_SESSION["userId"]."','1','".time()."')";
			$resultInsertMove = mysqli_query($conn, $queryInsertMove); 
			header("location: sendMsg.php");
			exit();
		}else{
			if(!empty($_SESSION["OTPUserId"]) && $_SESSION["OTPUserId"]!= $row["id"]){
				unset($_SESSION["stopReSend"]);
				$_SESSION["otpCounter"] = 0;
			}
			if(empty($_SESSION["stopReSend"])){
				$_SESSION["OTPUserId"] = $row["id"];
				$_SESSION["OTPUser"] = $row["userName"];
				if($_SESSION["otpCounter"]<2)
					sendOTP();
				else
					header("location: login.php?res=248");
				exit;
			}else{
				header("location: login.php?res=248");
				exit;
			}
		}
	}
}
ob_end_flush();
?>
