<?php
ob_start();
header("Content-Type: application/json");
$status = 1;
if(empty($_REQUEST['username']) || !isset($_REQUEST['username']))
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 2,'MessageAr' => "اسم المستخدم غير صحيح او غير متوفر",'MessageEn' => "Username is incorrect or unavailable");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
if(empty($_REQUEST['password']) || !isset($_REQUEST['password']))
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 3,'MessageAr' => "كلمة المرورغير صحيحة",'MessageEn' => "Password is incorrect");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
if(empty($_REQUEST['msg']) || !isset($_REQUEST['msg']))
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 5,'MessageAr' => "نص الرسالة غير صحيح او غير متوفر",'MessageEn' => "Message text is incorrect or unavailable");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
if(empty($_REQUEST['numbers']) || !isset($_REQUEST['numbers']))
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 8,'MessageAr' => "الارقام المرسل اليها غير متوفرة",'MessageEn' => "username you sent to is unavailable ");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
include("config.php");
$_REQUEST['password'] = md5($_REQUEST['password']);
$result = chickUser($_REQUEST['username'], $_REQUEST['password']);
if($result == 1)
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 2,'MessageAr' => "اسم المستخدم غير صحيح او غير متوفر",'MessageEn' => "Username is incorrect or unavailable");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
if($result == 2)
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 3,'MessageAr' => "كلمة المرورغير صحيحة",'MessageEn' => "Password is incorrect");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
balanceSMS($userAccount, $passAccount);
$resultAccount = mysqli_query($conn, "SELECT id,account,allowChangeSender,sender FROM user_login WHERE userName='".$_REQUEST['username']."' AND password='".$_REQUEST['password']."'");
$rowAccount = mysqli_fetch_assoc($resultAccount);
$userId = $rowAccount['id'];
$accounts = $rowAccount['account'];
$sender = trim($_REQUEST['sender']);
if(checkSender($sender, $userId) == 0)
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 4,'MessageAr' => "اسم المرسل غبر متاح حاليا",'MessageEn' => "Sender name is unavailable or unauthorized");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
$msg = iconv("UTF-8","windows-1256//IGNORE",$_REQUEST['msg']);
$msg = clearMsg($msg);
if(empty($msg))
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 5,'MessageAr' => "نص الرسالة غير صحيح او غير متوفر",'MessageEn' => "Message text is incorrect or unavailable");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}	
if(!empty($_REQUEST['numbers']))
{
	$unique = 1;
	$numbers = $_REQUEST['numbers'];
	$idSend = $userId . "_" . time();
	$numberDeny = prefixDeny();
	$numbersNotAccepted = "";
	$countOfNotExpted = 0;
	$return = filter_numberWith($idSend, $numbers, $numberDeny, $unique);
	if(!empty($return["notAcceptedNumber"]))
	{
		$numbersNotAccepted = $return["notAcceptedNumber"];
		$countOfNotExpted = $return["countOfNotAcceptedNumber"];
	}
	$numbers = $return["acceptedNumber"];
	$arrayNumbers = explode(",",$numbers);
}
if(empty($numbers))
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 8,'MessageAr' => "الارقام المرسل اليها غير متوفرة",'MessageEn' => "username you sent to is unavailable ");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
$number966 = $numberOhers = '';
foreach($arrayNumbers as $value)
{
	if(!empty($value))
	{
		if(in_array(substr($value,0,5), array('96650','96657','96653','96654','96655','96656','96658','96659','96651')))
			$number966 .= $value.',';
		else
			$numberOhers .= $value.',';
	}
}
$points = $points2 = 0;
if(!empty($number966))
	$points = getPoints($number966, $msg);
//if(!empty($numberOhers))
	//$points2 = getPoints($numberOhers, $msg);

if($accounts <= 0)
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 6,'MessageAr' => "الرصيد = 0",'MessageEn' => "Your current balance is 0");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
if($points > $accounts)
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 7,'MessageAr' => "رصيدك غير كافي لإرسال كود التفعيل",'MessageEn' => "Your current balance is insufficient");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
exit;
}
$resultFunction = "";
$msgMd5 = $md5STR = $userId.$msg.$number966.$sender.date('H') . date('m') . date('d') . date('Y');
$msgUniqe = md5($msgMd5);
$resultFunction = "";
if(!empty($number966))
	$resultFunction = insertDBNumber($userId,$sender,$number966,$msg,7,$msgUniqe,$numbersNotAccepted,$countOfNotExpted,0,1);
else{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 8,'MessageAr' => "الارقام المرسل اليها غير متوفرة",'MessageEn' => "username you sent to is unavailable ");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
if(!empty($resultFunction)){	
    header("Content-Type: application/json");
	$ResponseStatus = 'success';
	$Error = NULL;
	$Data = array('result' => 1,'MessageAr' => "تم حفظ الرسالة وجاري ارسالها",'MessageEn' => "The message has been saved and will be sent","msgId"=>$resultFunction);
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
} 
else 
{
    header("Content-Type: application/json");
	$ResponseStatus = 'fail';
	$Data = NULL;
	$Error = array('ErrorCode' => 9,'MessageAr' => "لم تتم عملية ارسال الرسالة بالشكل الصحيح هناك مشكلة في قواعد البيانات",'MessageEn' => "message was not sent properly. There is a problem with the database");
	$jsonRespond = array('status' => $status,'ResponseStatus'=> $ResponseStatus,'Data' => $Data,'Error' => $Error);	
	$valJasonReturn = json_encode($jsonRespond);
	echo $valJasonReturn;
	exit;
}
ob_end_flush();
?>