<?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");
include("../functions/insertMsg.php");
$link="sendFromExcel.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;
		}
	}
}

if(!empty($_SESSION["excelData"]) && !empty($_POST["numFiled"]) && !empty($_POST["msg"]))
{
	if(empty($_POST['msg']))
	{
		header("location: sendFromExcel.php?res=307");
		exit();
	}
	$msg = $_POST["msg"];
	$unique = "0";
	if(isset($_POST['deleteNum']) && $_POST['deleteNum'] == "on")
	{
		$unique = "1"; 	
	}
	
	$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>";
	}
	$defultStatus = array_fill(0, count($numbersToSend), -1);
	$arrayStatusNumbers = array_combine($numbersToSend, $defultStatus);
	$userSender = $_POST["userSender"];
	$arratDataExample = array();
	$numbersToSend = $return['acceptedNumber'];
	$MsgID =0;
	if(!empty($numbersToSend)) {
		insertDBNumber($_SESSION["userId"],$userSender,$numbersToSend,$msg,2,$_SESSION["msgUnique"],$return['notAcceptedNumber'],$errorNumberCount,1);
		header("location: queueList.php?sendErrorMsg=2");
		exit();
	} else {
		header("location: $link?sendErrorMsg=1");
		exit();
	}
		
}else
{
	header("Location: sendFromExcel.php"); 		
	exit();	
}

?>
