<?php
ob_start();
include("function/functionUnicode.php");
include("function/checkSendPorts.php");
if(curlTest() == 5)
	include("../functions/curlSMS.php");
elseif(fsockopenTest() == 6)
	include("../functions/fsockopen.php");
elseif(fopenTest() == 6)
	include("../functions/fopenSMS.php");
else
	exit;
include("config.php");
$resultSendMsgTemp = mysqli_query($conn, "SELECT * FROM `msg_send_temp` WHERE `excel`='1' AND counter<20 ORDER BY `id` ASC LIMIT 0,20");
$numSendMsgTemp = mysqli_num_rows($resultSendMsgTemp);
if(empty($numSendMsgTemp))
{
	mysqli_close($conn);
	ob_end_flush();
	exit();
}
while($rowSendMsgTemp = mysqli_fetch_assoc($resultSendMsgTemp))
{
	$numbers = $rowSendMsgTemp["numbers"];
	$msg = $rowSendMsgTemp["msg"];
	$userSender = $rowSendMsgTemp["userSender"];
	$MsgID = $rowSendMsgTemp["id"];
	$userId = $rowSendMsgTemp["userId"];
	$countNumbers = $rowSendMsgTemp["countNumbers"];
	$numMsgs = $rowSendMsgTemp["numMsgs"];
	$points = $rowSendMsgTemp["points"];
	$msgKey = $rowSendMsgTemp["msgKey"];
	$api = $rowSendMsgTemp["api"];
	$msgUnique = $rowSendMsgTemp["msgUnique"];
	$accountBeforSend = $rowSendMsgTemp["accountBeforSend"];
	$accountAfterSend = $rowSendMsgTemp["accountAfterSend"];
	$numbersNotAccepted = $rowSendMsgTemp["numbersNotAccepted"];
	$countNotAccepted = $rowSendMsgTemp["countNotAccepted"];
	$counterRep = $rowSendMsgTemp["counterRep"];
	
	
	$querySelect="SELECT `userName` FROM `user_login` WHERE `id`='$userId'";
	$resultSelect=mysqli_query($conn, $querySelect);
	$rowSelect=mysqli_fetch_assoc($resultSelect);
	$userName = $rowSelect["userName"];
	$result = sendSMS($userAccount, $passAccount, $numbers, $userSender, $msg, $MsgID);
	$result = json_decode($result ,true);
	if(!empty($result['Data']['result']) && $result['Data']['result']=="1")
	{
		$status = $result['status'];
		$responseStatus = $result['ResponseStatus'];
		$msgIdMobily = $result['Data']['msgId'];
		$resultMobily = $result['Data']['result'];
		$messageAr = $result['Data']['MessageAr'];
		$messageEn = $result['Data']['MessageEn'];
		$msgLengthMobily = $result['Data']['msgLength'];
		$countNumberMobily = $result['Data']['countNumber'];
		$pointMobily = $result['Data']['point'];
		$rejectedNumber = $result['Data']['rejectedNumber'];
		$rejectedNumber = str_replace("[", "", $rejectedNumber);
		$rejectedNumber = str_replace("]", "", $rejectedNumber);
		$rejectedNumberExplode = explode(',',$rejectedNumber);
		$rejectedNumberExplode = array_filter($rejectedNumberExplode);
		$rejectedNumberCount = count($rejectedNumberExplode);
		$rejectedNumber = implode(',',$rejectedNumberExplode);
		$rejectedNumber = $numbersNotAccepted.",".$rejectedNumber;
		$rejectedNumberCount = $rejectedNumberCount+$countNotAccepted;
		$date = time();
		mysqli_query($conn, "INSERT INTO history (numbers,msg,userSender,date,userName,idUser,countNumbers,numMsgs,points,msgKey,msgType,msgTempId,accountBeforSend,accountAfterSend,`api`,`status`,`responseStatus`,`msgIdMobily`,`resultMobily`,`messageAr`,`messageEn`,`msgLengthMobily`,`countNumberMobily`,`pointMobily`,`rejectedNumber`,`countNotAccepted`,`excel`,`accepted`,`count_accepted`) VALUES ('".mysqli_real_escape_string($conn, $numbers)."','".mysqli_real_escape_string($conn, $msg)."','".mysqli_real_escape_string($conn,$userSender)."','".$date."','".$userName."','".$userId."','".$countNumbers."','".$numMsgs."','".$points."','".$msgKey."','1','".$MsgID."','".$accountBeforSend."','".$accountAfterSend."','".$api."','".$status."','".$responseStatus."','".$msgIdMobily."','".$resultMobily."','".$messageAr."','".$messageEn."','".$msgLengthMobily."','".$countNumberMobily."','".$pointMobily."','".$rejectedNumber."','".$rejectedNumberCount."','1','".$numbers."','".$countNumbers."')");
		mysqli_query($conn, "DELETE FROM msg_send_temp WHERE id=$rowSendMsgTemp[id]");
	}
	elseif($result['Error']['ErrorCode'] == "15" || $result['Error']['ErrorCode'] == "16" || $result['Error']['ErrorCode'] == "17")
	{
		$status = $result['status'];
		$responseStatus = $result['ResponseStatus'];
		$messageAr = $result['Error']['MessageAr'];
		$messageEn = $result['Error']['MessageEn'];
		if($counterRep!=1)
		{
			$rejectedNumber2 = $numbersNotAccepted.",".$numbers;
			$countNotAcceptedAll = $countNotAccepted+$countNumbers;
			mysqli_query($conn, "UPDATE msg_send_temp SET counter='20',`numbersNotAccepted`='$rejectedNumber2',`countNotAccepted`=`countNotAccepted`+'$countNotAcceptedAll',`status`='".$status."',`responseStatus`='".$responseStatus."',`messageAr`='".$messageAr."',`messageEn`='".$messageEn."' WHERE id=$rowSendMsgTemp[id]");
		}
		else
			mysqli_query($conn, "UPDATE msg_send_temp SET counter='20',`status`='".$status."',`responseStatus`='".$responseStatus."',`messageAr`='".$messageAr."',`messageEn`='".$messageEn."' WHERE id=$rowSendMsgTemp[id]");
	}
	elseif($result['Error']['ErrorCode'] != "0" || $result['Error']['ErrorCode'] != "-1" || $result['Error']['ErrorCode'] != "-2" || $result['Error']['ErrorCode'] != "")
	{
		$status = $result['status'];
		$responseStatus = $result['ResponseStatus'];
		$messageAr = $result['Error']['MessageAr'];
		$messageEn = $result['Error']['MessageEn'];
		mysqli_query($conn, "UPDATE msg_send_temp SET counter=counter+1,`status`='".$status."',`responseStatus`='".$responseStatus."',`messageAr`='".$messageAr."',`messageEn`='".$messageEn."' WHERE id=$rowSendMsgTemp[id]");	
	}
}
mysqli_close($conn);
ob_end_flush();

?>