<?php
/**
 * Processing Forget password form data:
 * check data then generate new password and send it through SMPT Email
 *
 * 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();
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
include("validateLogin.php");
include("includeInformation.php");
include_once("../functions/pointFunction.php");
include_once("../functions/sendFunctions.php");

function filterNumberNew($number){
	global $conn;
	$number = preg_replace("/[^0-9]/", "", $number);
	if(substr($number, 0, 3)=="966" && strlen($number)!=12){
		return false;
	}else{
		if(substr($number,0,3) == '966' || substr($number,0,4) == '+966' || substr($number,0,2) == '05' || substr($number,0,1) == '5') {
			if(in_array(substr($number,0,6), array('+96650','+96651','+96653','+96654','+96655','+96656','+96658','+96659','+96657'))) {
				$number = ltrim($number, "");
			}elseif(in_array(substr($number,0,3), array('050','051','053','054','055','056','058','059','057'))) {
				$number = preg_replace('/0/', '966', $number, 1);
			} elseif(in_array(substr($number,0,2), array('50','51','53','54','55','56','58','59','57'))){
				$number = '966'.$number;
			}
			return $number;
		} else {
			return false;
		}
	}
}

$numberDetail="";
// Send Variables //
$Language = $_SESSION["language"];
$dir = "rtl";
if($Language == 2)
	$dir = "ltr";
$unique = "0";
if(isset($_POST['deleteNum']) && $_POST['deleteNum'] == "on")
{
	$unique = "1"; 	
}
$sendTime = '';

$msg = $_POST["msg"];
$msg = iconv("utf-8", "windows-1256", $msg);

$category = $_POST["category"];
$numbers = "";
$userSender = trim($_POST['userSender']);
$numNumber = 0;
$idSend = $_SESSION['userId']."_".microtime()."_".rand(1,999999)."_".rand(1,1000)."_".rand(1,2000)."_".rand(1,3000);

$queryAccount = "SELECT * FROM $userLogin WHERE id=1";
$resultAccount = mysqli_query($conn, $queryAccount);
$rowAccount = mysqli_fetch_array($resultAccount);

$dataToSend = array();
$numbersToSend = array();	 
$excelDataNum = $_SESSION["excelData"];
$numberCeil  = $_POST["ceil"];
$numFiled = $_POST["numFiled"];

$return = array('count'=>0, 'countOfAcceptedNumber'=>0, 'countOfNotAcceptedNumber'=>0, 'acceptedNumber'=>array(), 'notAcceptedNumber'=>array());
if(!empty($excelDataNum)) {
	foreach($excelDataNum AS $dataArray) {
		if(empty($dataArray) || empty($dataArray[$numberCeil]) || !isset($dataArray[$numberCeil]))
			continue;
		$return['count']+=1;
		$thisNumber = $dataArray[$numberCeil];
		$filteredNumber = filterNumberNew($thisNumber);
		if($filteredNumber !== false) {
			if($unique) {
				if(array_search($filteredNumber, $return['acceptedNumber']) !== false)
					continue;
			}
			$return['countOfAcceptedNumber']+=1;
			$return['acceptedNumber'][] = $filteredNumber;
			$dataToSend[$filteredNumber][] = $dataArray;
		} else {
			$return['countOfNotAcceptedNumber']+=1;
			$return['notAcceptedNumber'][] = $thisNumber;
		}
	}
	$return['acceptedNumber'] = implode(',', $return['acceptedNumber']);
	$return['notAcceptedNumber'] = implode(',', $return['notAcceptedNumber']);
}
$numNumber = 0;
$allPoints = 0;
/* Error Numbers*/
$errorNumber = "";
$errorSendNumber = "";
$errorNumberCount = $return["countOfNotAcceptedNumber"];
if(!empty($errorNumberCount)) {
	$counterNotNumber = 1;
	$notAcceptedNumber = explode(',', $return["notAcceptedNumber"]);
	foreach($notAcceptedNumber AS $notNumber)
		$errorNumber.= ($counterNotNumber++).": ".$notNumber."<br>";
}

$arrayDataExample = array();
if(!empty($return["countOfAcceptedNumber"])) {
	foreach($dataToSend AS $thisNumber => $dataOfNumber) {
		$value = $dataOfNumber[0];
		if(count($value) >= 1) {
			for($j=0; $j<$numFiled; $j++) {
				if($j==0){
					if(!empty($value[$j]))
						$thisMsg = str_replace("<<ceil".$j.">>",$value[$j],$msg);
					else
						continue;
				}else{
					if(!empty($value[$j]))
						$thisMsg = str_replace("<<ceil".$j.">>",$value[$j],$thisMsg);	
					else
						continue;
				}
			}
			$thisMsg = clearArabicNum($thisMsg);
			$tempMsg = "";
			$msgLenght = strlen($thisMsg);
			for($x=0; $x<$msgLenght; $x++) {
				if(ord($thisMsg[$x]) != 13)
					$tempMsg.= $thisMsg[$x];
			}
			$thisMsg = $tempMsg;
			$msgPoint = getMsgLen($thisMsg);
			$arrayDataExample[] = $thisNumber."&nbsp;&nbsp;&nbsp;&nbsp;".$thisMsg;
			$thisPoints = getPoints($thisNumber, $thisMsg);
			$allPoints+= $thisPoints;
			$numNumber++;
		} else {
			$errorNumber .= (++$errorNumberCount).": ".$thisNumber."<br>";
			$errorNumberCount++;
		}
		if(array_key_exists('1', $dataOfNumber) && is_array($dataOfNumber[1])) {
			unset($dataOfNumber[0]);
			foreach($dataOfNumber AS $repeatedNumberData) {
				$value = $repeatedNumberData;
				if(count($value) >= 1) {
					for($j=0; $j<$numFiled; $j++) {
						if($j==0){
							if(!empty($value[$j]))
								$thisMsg = str_replace("<<ceil".$j.">>",$value[$j],$msg);
							else
								continue;
						}else{
							if(!empty($value[$j]))
								$thisMsg = str_replace("<<ceil".$j.">>",$value[$j],$thisMsg);	
							else
								continue;
						}
					}
					$thisMsg = clearArabicNum($thisMsg);
					$tempMsg = "";
					$msgLenght = strlen($thisMsg);
					for($x=0; $x<$msgLenght; $x++) {
						if(ord($thisMsg[$x]) != 13)
							$tempMsg.= $thisMsg[$x];
					}
					$thisMsg = $tempMsg;
					$msgPoint = getMsgLen($thisMsg);
					$arrayDataExample[] = $thisNumber."&nbsp;&nbsp;&nbsp;&nbsp;".$thisMsg;
					$thisPoints = getPoints($thisNumber, $thisMsg);
					$allPoints+= $thisPoints;
					$numNumber++;
				} else {
					$errorNumber .= (++$errorNumberCount).": ".$thisNumber."<br>";
					$errorNumberCount++;
				}				
			}
		}
	}
}
$points = $allPoints;

$errMsg = "";
if(!empty($errMsg))
{
    $numberDetail.= "<table width='300px' cellpadding=0 cellspacing=0  style=direction:" . $dir . ">";
    $numberDetail.= "      <tr>    ";
    $numberDetail.= "           <td align='center' nowrap><B>" . $errMsg . "</td>";
    $numberDetail.= "     </tr>  ";
    $numberDetail.= "</table>";
    $numberDetail.= "<input type='hidden' value='0' name='balance'>";
}
else
{
	$numberDetail.= "<input type='hidden' value='".$rowAccount["account"]."' name='balance'>";
    $numberDetail.= "<table border='0' cellpadding='3' cellspacing='0' style='direction:".$dir.";width: 100%;'>";
    if(!empty($sendTime)){
        $numberDetail.= "      <tr>  ";
        $numberDetail.= "        	<td nowrap class='myModalLabel'>".$sendMsgLBL["sendTime"].": ".$sendTime."</td>";
        $numberDetail.= "     </tr>    ";
    }
    $numberDetail.= "      <tr>  ";
    $numberDetail.= "        	<td nowrap class='myModalLabel'>".$sendMsgLBL["sender"].": ".$userSender."</td>";
    $numberDetail.= "     </tr>    ";
	$numberDetail .="      <tr> ";
	$numberDetail .="           <td nowrap><b>".$sendFromExcelLBL["addedNumbers"].":</b></td>  ";
	$numberDetail .="     </tr>  ";
	$numberDetail .="      <tr>";
	$numberDetail .="           <td  nowrap>".$sendFromExcelLBL["youSendTo"]." ".($numNumber+$errorNumberCount)." ".$sendFromExcelLBL["number"]."</td>";
	$numberDetail .="     </tr>   ";
	$numberDetail .="      <tr>  ";
	$numberDetail .="           <td nowrap><b>".$sendFromExcelLBL["acceptedNumbers2"].":</b></td> ";
	$numberDetail .="     </tr>    ";
	if(!empty($return["countOfAcceptedNumber"]))
		$sendFromExcelLBL["acceptedAfterCheck"] = "";
	$numberDetail .="      <tr>  ";
	$numberDetail .="           <td nowrap>".$sendFromExcelLBL["acceptedAfterCheck"]." ".($numNumber)." ".$sendFromExcelLBL["numbersWillbeSent"]."</td>";
	$numberDetail .="     </tr> ";
	if(!empty($return["countOfAcceptedNumber"]))
	{
		$numberDetail .="      <tr>  ";
		$numberDetail .="           <td nowrap><b>".$sendFromExcelLBL["viewSomeSendNumbers"].":</b></td> ";
		$numberDetail .="     </tr>    ";
		$i = 0;
		while($i < 5)
		{
			if(isset($arrayDataExample[$i]))
			{
				$numberDetail.="      <tr>  ";
				$numberDetail.="           <td  nowrap>".$arrayDataExample[$i]."</td>";
				$numberDetail.="     </tr> ";
			}
			$i++;
		}
	}
	if($errorNumberCount)
	{
		$numberDetail .="      <tr>";
		$numberDetail .="           <td nowrap><B> ".$sendFromExcelLBL["countErrorNumbers"]." :</td> ";
		$numberDetail .="     </tr> ";
		$numberDetail .="      <tr> ";
		$numberDetail .="           <td  nowrap>".$sendFromExcelLBL["rejectedAfterCheck"]." ".($errorNumberCount).$sendFromExcelLBL["uploadFile"].($errorNumber)." </td>";
		$numberDetail .="     </tr>";
	}
	if(!empty($errorNumberNotAcceptedSendNumbers))
	{
		$numberDetail .="      <tr>";
		$numberDetail .="           <td nowrap><B> ".$sendFromExcelLBL["countErrorNumbers"]." :</td> ";
		$numberDetail .="     </tr> ";
		$numberDetail .="      <tr> ";
		$numberDetail .="           <td  nowrap>".$sendFromExcelLBL["rejectedAfterCheck"]." ".($notAcceptedSendNumbercount).$sendFromExcelLBL["uploadFile"].($errorSendNumber)." </td>";
		$numberDetail .="     </tr>";
	}
	if(!empty($numNumber))
	{
		$numberDetail.= "      <tr >";
		$numberDetail.= "           <td align='center' nowrap colspan=2><b> <span style='font-size:11pt'>".$sendFromExcelLBL["usedBalance"]." ( " . $points . " ) ".$sendFromExcelLBL["pointsFromAccount"]."</td>";
		$numberDetail.= "      </tr>   ";
	}
   if(!empty($return["countOfAcceptedNumber"]))
	{
		$numberDetail.= " <tr class='trBG'>";
		$numberDetail.= " <td nowrap valign=top colspan=2  align=left style='font-size:13px'><hr />";
		$numberDetail.= " <input  type=button value='".$sendFromExcelLBL["contSend"]."' class='btn btn-success waves-effect waves-light' name=send onClick='document.formMsg.submit(); this.disabled=true;'>&nbsp;</td> ";
		$numberDetail.= " </tr>";
	}
    $numberDetail.= "</table>";
}
echo $numberDetail;
?>
