<?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");
$numberDetail="";
// Send Variables //
$Language = $_SESSION["Language"];
$dir = "rtl";
if($Language == 2)
	$dir = "ltr";
$unique = "0";
if($_POST['deleteNum'] == "on")
{
	$unique = "1"; 	
}
$sendTime = '-1';
$msg = clearArabicNum($_POST["msg"]);
$msg = iconv("utf-8", "windows-1256", $msg);
$errMsg = "";

$msgLenProg = strlen($msg);
$msgLenMobily =checkMsgLength($msg);
if($msgLenProg != $msgLenMobily)
{
	$numberDetail.= "<table width='300px' cellpadding=0 cellspacing=0  style=direction:" . $dir . ">";
    $numberDetail.= "      <tr>    ";
    $numberDetail.= "           <td align='center' nowrap valign=middle><font color=red><B>" . $sendMsgLBL["noBalance"]. "</B></font></td>";
    $numberDetail.= "     </tr>  ";
    $numberDetail.= "</table>";
	echo $numberDetail;
	exit();
}

$category = $_POST["category"];
$userSender = trim($_POST['userSender']);
$numNumber = 0;
$idSend = $_SESSION['userId'] . "_" . time();
$queryCategory = "SELECT * FROM $userCategory WHERE userId=$_SESSION[userId]";
$resultCategory = mysqli_query($conn, $queryCategory);
$numCategory = mysqli_num_rows($resultCategory);
$queryAccount = "SELECT * FROM $userLogin WHERE id=$_SESSION[userId]";
$resultAccount = mysqli_query($conn, $queryAccount);
$rowAccount = mysqli_fetch_array($resultAccount);
$tempNumbers = "";
$tempNumbersNew = array();
if (!empty($category))
{
    $category = clearArray(",", $category);
    $queryNum = "SELECT id, number FROM user_number WHERE userId=$_SESSION[userId] AND categoryId IN ($category) ORDER BY id DESC";
    $resultNum = mysqli_query($conn, $queryNum);
    $numNum = mysqli_num_rows($resultNum);
    while($rowNum = mysqli_fetch_array($resultNum))
    	$tempNumbers.= $rowNum["number"].",";
}

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

$numbers = clearArray(",", $numbers);

$allNumbers ="";
$numbersArray = explode(",", $numbers);
foreach ($numbersArray as $value)
{
	if(!empty($value))
	{
		if(substr($value,0,3) == '966')
		{
			if(in_array(substr($value,0,5), array('96650','96657','96653','96654','96655','96656','96658','96659','96651')))// && strlen($value) == "12"
			{
				$allNumbers .= $value.',';
			}
			else
			{
				continue;
			}
		}
		else
		{
			$allNumbers .= $value.',';
		}
	}
}	
$numbers=$allNumbers;
$numberRepeted="";
$allrepeatCount=0;

if($_POST['deleteNum'] == "on")
{
	$array= explode( ',', $numbers );
	$arrayR = array_count_values($array);
	if(count($arrayR) != 0)
	{
		foreach($arrayR AS $num => $repeatCount)
		{
			if($repeatCount == 1)
				unset($arrayR[$num]);
		}
		
		$viewCounter = 5;
		if(count($arrayR) > $viewCounter)
			$viewCounter = count($arrayR);
			
		foreach($arrayR AS $num => $repeatCount)
		{
			$allrepeatCount += $repeatCount-1;
			$numberRepeted.= $num."<br>";
			--$viewCounter;
			if(empty($viewCounter) || $viewCounter=="0")
				break;
		}
	}
}

$newNumbers = "";
$errorNumber = array();
$numberDeny = prefixDeny();
$numbersNotAccepted = "";

$return = filter_numberWithMobily($idSend, $numbers, $numberDeny, $unique);
$numbers = $return["acceptedNumber"];

$points = getPoints($numbers, $msg);
$msgLenght = getMsgLen($msg);
if ($rowAccount["account"] <= 0)
  $errMsg = $sendMsgLBL["balance0"];
if ($points > $rowAccount["account"])
   $errMsg = $sendMsgLBL["noBalance"];
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='2' cellspacing='0' style='direction:".$dir.";width:100%'>";
	$numberDetail.= "      <tr >";
	$numberDetail.= "           <td align='center' nowrap colspan=2><b> <span style='font-size:11pt'>".$sendMsgLBL["usedBalance"]." ( " . $points . " ) ".$sendMsgLBL["pointsFromAccount"]."</td>";
	$numberDetail.= "      </tr>   ";
	$numberDetail.= "      <tr>   ";
	$numberDetail.= "      <tr>  ";
	$numberDetail.= "        	<td nowrap class='myModalLabel'>".$sendMsgLBL["sender"]." : </td>";
	$numberDetail.= "      	 	<td nowrap>".$userSender."</td>";
	$numberDetail.= "     </tr>    ";
	$numberDetail.= "      <tr> ";
	$numberDetail.= "          	<td  nowrap class='myModalLabel' valign='top'>".$sendMsgLBL["message"]." :</td>";
	$numberDetail.= "      		<td  nowrap><div style='border:1px solid #000; padding:2px; width:210px; height:60px; overflow:auto'>".nl2br($msg)."</div></td>";
	$numberDetail.= "      </tr>";
	$numberDetail.= "      <tr> ";
	$numberDetail.= "			<td>&nbsp;</td>";
	$numberDetail.= "      		<td nowrap><span class='myModalLabel'>".$sendMsgLBL["charCount"]." : </span>" . $numChar = strlen($msg) . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	<span class='myModalLabel'>".$sendMsgLBL["msgLength"]." : $msgLenght</span></td>";
	$numberDetail.= "      </tr>";								
	$numberDetail.= "      <tr>  ";
	$numberDetail.= "           <td nowrap class='myModalLabel'>".$sendMsgLBL["acceptedNumbers"]." : </td> ";
	$numberDetail.= "           <td nowrap><b>".$return["countOfAcceptedNumber"]."</b><input type=hidden value='".$numbers."' name='numbers'><input type=hidden value='".$numbersNotAccepted."' name='numbersNotAccepted'></td> ";
	$numberDetail.= "      </tr>    ";
	if($numberRepeted!="")
	{
		$numberDetail.= "      <tr>  ";
		$numberDetail.= "           <td nowrap class='myModalLabel'>".$sendMsgLBL["repeatedNumbers"].": </td> ";
		$numberDetail.= "           <td nowrap><b>".$allrepeatCount." </b></td> ";
		$numberDetail.= "      </tr>    ";
		
		$numberDetail.= "      <tr>  ";
		$numberDetail.= "           <td nowrap class='myModalLabel'>".$sendMsgLBL["example"].": </td> ";
		$numberDetail.= "           <td nowrap><div style='max-height: 150px;overflow: auto'><b>".$numberRepeted."</b></div></td>";
		$numberDetail.= "      </tr>    ";
	}
	$numberDetail.= "      <tr>  ";
	$numberDetail.= "           <td nowrap class='myModalLabel'>".$sendMsgLBL["rejectedNumbersCount"].": </td>";
	$numberDetail.= "           <td nowrap><b>".$return["countOfNotAcceptedNumber"] ."</b></td>";
	$numberDetail.= "     </tr>    ";
	if(empty($return["countOfAcceptedNumber"]))
	{
		$numberDetail.= "  <tr>  ";
		$numberDetail.= "           <td nowrap class='myModalLabel' valign='top' colspan='2' style='color:red; text-align:center'>".$sendMsgLBL["noAcceptedNumbers"]."</td>";
		$numberDetail.= "  </tr>    ";
	}
	$numberDetail.= "<input type=hidden value='".$category."' name='catSelected'>";
	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='".$sendMsgLBL["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;
ob_end_flush();

?>
