<?php
function getUnicode($msg)
{
	$unicode=0;
	$str = "ÐÏÌÍÎåÚÛÝÞËÕÖØßãäÊÇáÈíÓÔÙÒæÉìáÇÑÄÁÆÅáÅÃáÃÂáÂ";
	for($i=0;$i<=strlen($str);$i++)
	{
		$strResult= substr($str,$i,1);
		for($R=0;$R<=strlen($msg);$R++)
		{
			$msgResult= substr($msg,$R,1);
			if($strResult==$msgResult && $strResult)
			{
				$unicode=1;
			}
		}
	}
	return $unicode;
}
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();
	if(!$conn)
		return $number;
    $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 replaceValue($msgKey, $msg)
{
	$msgKey = str_replace(',','', $msgKey);
	$arrayMsgKey = explode('@',$msgKey);
	foreach($arrayMsgKey as $key => $value)
	{
		list($keyReplace,$valueReplace) = explode('#',$value);
		$keyReplace = trim($keyReplace);
		$valueReplace = trim($valueReplace);
		$msg = str_replace($keyReplace,$valueReplace,$msg);
	}
	return $msg;
}
function checkSender($userSenderSMS, $userId)
{
	global $conn;
	if(!$conn)
		return 0;
		
	$result = mysqli_query($conn, "SELECT sender,allowChangeSender FROM user_login WHERE id='".$userId."'");
	$num = mysqli_num_rows($result);
	$row = mysqli_fetch_assoc($result);
	if(!empty($num)){
		if(!empty($row["sender"])){
			if($userSenderSMS == $row["sender"]){
				return 1;
			}else{
				$result2 =mysqli_query($conn, "SELECT userSender FROM `cpanel`");
				$row2 = mysqli_fetch_assoc($result2);
				if($userSenderSMS == $row2["userSender"])
					return 1;
				else
					return 0;
			}
		}else{
			if(!empty($row["allowChangeSender"]))
				return 1;
			else
				return 0;
		}
	}
}
function getNumNumbers($numbers)
{
	$numNumber = 0;
    $arrayNumber = explode(",",$numbers);
    foreach($arrayNumber as $value)
	{
		if(!empty($value))
		{
			$value = doubleval($value);
			if(!empty($value))
				$numNumber++;
		}
    }
	return $numNumber;
}
function getMsgLen($msg)
{	
	$unicode = 0;
	$str = "ÏÌÍÎåÚÛÝÞËÕÖØßãäÊÇáÈíÓÔÙÒæÉìáÇÑÄÁÆÅáÅÃáÃÂáÂ¡º¿ÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÝÞßáãäåæìíÜðñòóõöøú";
	for($i=0;$i<=strlen($str);$i++)
	{
		$strResult= substr($str,$i,1);
		for($R=0;$R<=strlen($msg);$R++)
		{
			$msgResult= substr($msg,$R,1);
			if($strResult==$msgResult && $strResult)
			$unicode=1;
		}
	}
	
	$msgLenght = 1;
	$oneMsgLenth = 70;
	$twoMsgLenth = 67;
	if($unicode==0)
	{
		$oneMsgLenth = 160;
		$twoMsgLenth = 153;		
	}

	$numChar = strlen($msg);
	if($numChar > $oneMsgLenth)
		$msgLenght = ceil($numChar/$twoMsgLenth);

	return $msgLenght;
}
function chickUser($userName, $password)
{
	global $conn;
	if(!$conn)
		return 1;
	$result = mysqli_query($conn, "SELECT id FROM user_login WHERE userName='".$userName."'");
	$num = mysqli_num_rows($result);
	if($num == 0 || $num == "")
		return 1;
	else
	{
		$result2 = mysqli_query($conn, "SELECT id FROM user_login WHERE userName='".$userName."' AND password='".$password."'");
		$num2 = mysqli_num_rows($result2);
		if($num2 == 0 || $num2 == "")
			return 2;
	}
	return -1;
}
function isInteger($number)
{
	for($R = 0; $R <= strlen($number); $R++)
	{
		if(substr($number, $R, 1) >= "0" && substr($number, $R, 1) <= "9")
		{
		}
		else
			return 0;
	}
	return 1;
}
function filterNumbers($arrayNumber)
{
	$newNumbers = array();
	foreach($arrayNumber as $value)
	{
		if($value)
		{
			$value = str_replace("\r", "", $value);
			$value = str_replace("\n", "", $value);
			$value = str_replace(" ", "", $value);
			$value = trim($value);
			if(isInteger($value) == 1)
			{
				if(substr($value, 0, 2) == "00" && strlen($value) > 5)
					$newNumbers[] = substr($value, 2, strlen($value));
				elseif(strlen($value) > 5)
					$newNumbers[] = $value;
			}
		}
	}
	return $newNumbers;
}
function clearMsg($msg)
{
    $msg = str_replace("&#8233;", "", $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);
    $msgAscii = $msg;
    $msg = "";
    for($i = 0; $i < strlen($msgAscii); $i++)
    {
        if(ord(substr($msgAscii, $i, 1)) != 13)
            $msg .= substr($msgAscii, $i, 1);
    }
    $msgAscii = $msg;
    $msg = "";
    for($i = 0; $i < strlen($msgAscii); $i++)
    {
        if(ord(substr($msgAscii, $i, 1)) == 10)
            $msg .= chr(13);
        $msg .= substr($msgAscii, $i, 1);
    }
    return $msg;
}
function getPoints($numbers,$msg)
{
	$numNumber = 0;
    $arrayNumber = explode(",",$numbers);
    foreach($arrayNumber as $value)
	{
		if(!empty($value))
		{
			$value = doubleval($value);
			if(!empty($value))
				$numNumber++;
		}
    }
	$unicode = 0;
	$str = "ÏÌÍÎåÚÛÝÞËÕÖØßãäÊÇáÈíÓÔÙÒæÉìáÇÑÄÁÆÅáÅÃáÃÂáÂ¡º¿ÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÝÞßáãäåæìíÜðñòóõöøú";
	for($i=0;$i<=strlen($str);$i++)
	{
		$strResult = substr($str,$i,1);
		for($R=0;$R<=strlen($msg);$R++)
		{
			$msgResult = substr($msg,$R,1);
			if($strResult == $msgResult && $strResult)
				$unicode=1;
		}
	}
	$msgLenght = 1;
	$oneMsgLenth = 70;
	$twoMsgLenth = 67;
	if($unicode == 0)
	{
		$oneMsgLenth = 160;
		$twoMsgLenth = 153;		
	}
	$numChar = strlen($msg);
	if($numChar > $oneMsgLenth)
		$msgLenght = ceil($numChar/$twoMsgLenth);
	$points = $numNumber*$msgLenght;
	return $points;
}
function filter_numberWith($idSend, $number, $mobilyNumberDeny = "", $unique = 0) {
	global $conn;
	if(!$conn) {
		$countNumber = 0;
		$numNumber = 0;
		$numErrorNumber = 0;
		$numbers = 0;
		$errorNumberFilter = 0;
		$return = array("count" => $countNumber, "countOfAcceptedNumber" => $numNumber, "countOfNotAcceptedNumber" => $numErrorNumber, "acceptedNumber" => $numbers, "notAcceptedNumber" => $errorNumberFilter);
		return $return;
	}
	
    $numNumber = 0;
	$numErrorNumber = array();
    if(!empty($unique)) {
        $unique = 1;
    }

    $numbers = clearArray(',', $number, $unique, 1);
    $countNumber = count($numbers);
	if(!is_array($numbers)) {
		$countNumber = 0;
		$numNumber = 0;
		$numErrorNumber = 0;
		$numbers = 0;
		$errorNumberFilter = 0;
		$return = array("count" => $countNumber, "countOfAcceptedNumber" => $numNumber, "countOfNotAcceptedNumber" => $numErrorNumber, "acceptedNumber" => $numbers, "notAcceptedNumber" => $errorNumberFilter);
		return $return;
	}
	
    if (!empty($mobilyNumberDeny)) {
        $numberDenyArray = clearArray('+', $mobilyNumberDeny, 1, 1, 1);
		$numberDenyArray = array_filter(array_unique($numberDenyArray));
		
		if(count($numberDenyArray) != 0) {	
		
			foreach($numbers AS $numIndexMobily => $numValMobily) {
				if(!empty($numValMobily)) {
					
					if(substr($numValMobily, 0, 2)=="00")
						$numValMobily = substr($numValMobily, 2, strlen($numValMobily));
					if(substr($numValMobily, 0, 1)=="0")
						$numValMobily = substr($numValMobily, 1, strlen($numValMobily));
					if(strlen($numValMobily) == 9)
						$numValMobily = "966".$numValMobily;

					$numberInternationalBegin = substr($numValMobily, 0, 3);
					if(in_array($numberInternationalBegin, array('966')) && strlen($numValMobily) != 12) {
						$numErrorNumber[]= $numValMobily;
						unset($numbers[$numIndexMobily]);
						continue;
					}
						
					for($j=3;$j<=6;$j++) {
						if(in_array(substr($numValMobily, 0, $j), $numberDenyArray)) {
							$numErrorNumber[]= $numValMobily;
							unset($numbers[$numIndexMobily]);
							continue;
						}
					}
				}
			}
		}    
    }
    $numNumber = 0;
	$numNumber = count($numbers);
    if (!empty($numNumber)) {
		$numbers = implode(',',$numbers);
        $numErrorNumberCount = count($numErrorNumber);
        $errorNumberFilter = clearArray(",", $numErrorNumber);
    } else {
		$numbers = 0;
        $numErrorNumberCount = count($numErrorNumber);
        $errorNumberFilter = clearArray(",", $numErrorNumber);
    }
    $return = array("count" => $countNumber, "countOfAcceptedNumber" => $numNumber, "countOfNotAcceptedNumber" => $numErrorNumberCount, "acceptedNumber" => $numbers, "notAcceptedNumber" => $errorNumberFilter);
    //end function
    return $return;
}

?>