<?php
function clearArabicNum($msg)
{
    //arabic number
    $msg = strip_tags($msg);
    $msg = str_replace("&#1632;", "0", $msg);
    $msg = str_replace("&#1633;", "1", $msg);
    $msg = str_replace("&#1634;", "2", $msg);
    $msg = str_replace("&#1635;", "3", $msg);
    $msg = str_replace("&#1636;", "4", $msg);
    $msg = str_replace("&#1637;", "5", $msg);
    $msg = str_replace("&#1638;", "6", $msg);
    $msg = str_replace("&#1639;", "7", $msg);
    $msg = str_replace("&#1640;", "8", $msg);
    $msg = str_replace("&#1641;", "9", $msg);

    /**************************** Enter *****************************/
	$tempMsg = "";
	$msgLenght = strlen($msg);
	for($i=0; $i<$msgLenght; $i++)
	{
		if(ord($msg[$i]) != 13)
		$tempMsg.= $msg[$i];
	}
    /**************************** Enter *****************************/
    return $tempMsg;
}

function clearArray($delimiter, $string, $unique = 0, $array = 0, $numeric = 0)
{
    $newArray = array();
    if (!is_array($string))
    {
        $string = $string . $delimiter;
        $string = @explode($delimiter, $string);
    }
    $string = @array_diff($string, array(""));
    foreach ($string as $key => $vall)
    {
        if ($numeric == 1)
        {
            $vall = @trim($vall);
            if (@is_numeric($vall))
            {
                $newArray[] = $vall;
            }
        }
        else
        {
            $newArray[] = @trim($vall);
        }
    }

    if ($unique == 1)
    {
        $newArray = @array_values(@array_unique($newArray));
    }
	

    if ($array == 1)
    {
        return $newArray;
    }
    else
    {
        $string = @implode($delimiter, $newArray);
        return $string;
    }
}

function getNumINFilter($idSend)
{
    global $conn;
    $number=array();
    $qSelect = "SELECT number FROM `number_to_filter` WHERE idSend='$idSend'";
    $resultNumber = mysqli_query($conn, $qSelect);
    while ($rawResultNum = mysqli_fetch_array($resultNumber))
    {
        $number[] = $rawResultNum["number"];
    }
    return $number;
}

function filter_numberWithMobily($idSend, $number, $mobilyNumberDeny = "", $unique = 0)
{
    global $conn;
    $numNumber = $numErrorNumber = 0;
    if (!empty($unique))
    {
        $unique = 1;
    }

    $number = clearArray(',', $number, $unique, 1);
	
    $countNumber = count($number);
	if(!is_array($number))
	{
		$countNumber = 0;
		$numNumber = 0;
		$numErrorNumber = 0;
		$numbers = 0;
		$errorNumberFilter = 0;
		$return = array("count" => $countNumber, "countOfAcceptedNumber" => $numNumber, "countOfNotAcceptedNumber" => $numErrorNumber, "acceptedNumber" => $numbers, "notAcceptedNumber" => $errorNumberFilter);
		return $return;
		exit;
	}
	foreach ($number as $val)
    {
		$val = (double)$val;
		$val = doubleval($val);
		if(!empty($val))
		{
        	mysqli_query($conn, "INSERT INTO `number_to_filter` (idSend,number) VALUES ('$idSend', $val)");
		}
    }
    $number = getNumINFilter($idSend);
	if(!is_array($number))
	{
		$countNumber = 0;
		$numNumber = 0;
		$numErrorNumber = 0;
		$numbers = 0;
		$errorNumberFilter = 0;
		$return = array("count" => $countNumber, "countOfAcceptedNumber" => $numNumber, "countOfNotAcceptedNumber" => $numErrorNumber, "acceptedNumber" => $numbers, "notAcceptedNumber" => $errorNumberFilter);
		return $return;
		exit;
	}
    if (!empty($mobilyNumberDeny))
    {
        if(!is_array($mobilyNumberDeny))
            $mobilyNumberDeny=array_filter(explode("+",$mobilyNumberDeny));
        if(!empty($mobilyNumberDeny)){
            $numberDenyArray = clearArray('+', $mobilyNumberDeny, 1, 1, 1);
            foreach ($numberDenyArray as $numDeny)
            {
                mysqli_query($conn, "DELETE FROM `number_to_filter` WHERE idSend='$idSend' AND number like '" . $numDeny . "%' ");
            }
        }
    }
    $qDelete0 = "DELETE FROM `number_to_filter` WHERE idSend='$idSend' AND (length(number)<=10 OR length(number)>=14)";
    mysqli_query($conn, $qDelete0);

    $qDelete1 = "DELETE FROM `number_to_filter` WHERE idSend='$idSend' AND number like '966%' AND length(number)!=12";
    mysqli_query($conn, $qDelete1);

    $qDelete2 = "DELETE FROM `number_to_filter` WHERE idSend='$idSend' AND number like '964%' AND length(number)!=13";
    mysqli_query($conn, $qDelete2);

    $numbers = '';
    $numNumber = 0;
	$filterNumber=array();
    $fetchNumber = mysqli_query($conn, "SELECT number FROM `number_to_filter` WHERE idSend='$idSend' ");
    while ($row = mysqli_fetch_array($fetchNumber))
    {
        $filterNumber[] = $row['number'];
        $numbers .= $row['number'] . ',';
        $numNumber++;
    }
    mysqli_query($conn, "DELETE FROM `number_to_filter` WHERE idSend='$idSend'");
    $numNumber = count($filterNumber);
    if (!empty($filterNumber))
    {
        $errorNumberFilter = array_diff($number, $filterNumber);
        $numErrorNumber = count($errorNumberFilter);
        $errorNumberFilter = clearArray(",", $errorNumberFilter);
    }
    else
    {
    	$filterNumber= array("");
        $errorNumberFilter = array_diff($number, $filterNumber);
        $numErrorNumber = count($errorNumberFilter);
        $errorNumberFilter = clearArray(",", $errorNumberFilter);
    }
    $return = array("count" => $countNumber, "countOfAcceptedNumber" => $numNumber, "countOfNotAcceptedNumber" => $numErrorNumber, "acceptedNumber" => $numbers, "notAcceptedNumber" => $errorNumberFilter);
    //end function
    return $return;
}
?>