<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
include "pdate.php";
date_default_timezone_set("Asia/Tehran");
$servername = "localhost";
$username = "systemsms_api";
$password = "Qy^gCKlQGMeF";
$dbname = "systemsms_api";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
mysqli_set_charset($conn,"utf8");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$setup = $conn->query("SELECT * from setup WHERE id=1 ")->fetch_assoc();
//payapi
$payapi = $setup['api'];

//sms data
$uname = $setup['smsu'];
$pass = $setup['smsp'];

$fsd = $setup['findersood'];
$asd = $setup['agentsood'];
$ssd = $setup['smssood'];
$vlink = $setup['versionLink'];
$vnum = $setup['versionNum'];
$smsbase = 11;

	
function sendSms($to,$data){
		global $uname;
		global $pass;
		$url = "37.130.202.188/services.jspd";
		
		$rcpt_nm = array($to);
		$param = array
					(
						'uname'=>$uname,
						'pass'=>$pass,
						'from'=>'98100009',
						'message'=>$data,
						'to'=>json_encode($rcpt_nm),
						'op'=>'send'
					);
					
		$handler = curl_init($url);             
		curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($handler, CURLOPT_POSTFIELDS, $param);                       
		curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
		$response2 = curl_exec($handler);
		
		$response2 = json_decode($response2);
		$res_code = $response2[0];
		$res_data = $response2[1];
		
		
		//echo $res_data;

		
		


}
function calsood($id,$money){
		global $conn;
		global $ssd;
		 $Query = $conn->query("SELECT * from package WHERE code=$id ");
		 $data = $Query->fetch_assoc();
		 $sp = json_decode($data['smsprice'],true);
		 
		 
		 $sood = floor(($money)*($sp['afzoode']/100));
		 
		 
		 
		 $moaref = floor($sood*($ssd/100));
		 return $moaref;
	}
function getuser($id,$fild){
		global $conn;
		 $Query = $conn->query("SELECT * from users WHERE id=$id ");
		if($Query->num_rows > 0){
			$data = $Query->fetch_assoc();
			return $data[$fild];
		} else{
			return 0;
		}
	}
	function addbalance($id,$mony,$lastm){
		global $conn;
		 $newmab = $lastm+($mony*10);
		$conn->query("UPDATE `users` SET `credit`=$newmab WHERE id=".$id);
		echo $conn->error;
	}
function fulluser($id){
		global $conn;
		 $Query = $conn->query("SELECT * from users WHERE id=$id ");
		 if($Query->num_rows > 0){
			 $data = $Query->fetch_assoc();
			 $data['is'] = 1;
		 }else{
			 $data = array('is'=>0);
		 }
		 
		 return $data;
	}
	

function tofa($num)
{
    $persian_num_array = ['0'=>'۰','1'=>'۱','2'=>'۲','3'=>'۳','4'=>'۴','5'=>'۵','6'=>'۶','7'=>'۷','8'=>'۸','9'=>'۹'];

    $num = (float) $num;
    return strtr(number_format($num), $persian_num_array);
}
function hid($mob) {
	return  substr($mob, -4) . '***' . substr($mob,  0, 4);
}
function tofa2($num)
{
    $persian_num_array = ['0'=>'۰','1'=>'۱','2'=>'۲','3'=>'۳','4'=>'۴','5'=>'۵','6'=>'۶','7'=>'۷','8'=>'۸','9'=>'۹'];

    //$num = (string) $num;
    return strtr($num, $persian_num_array);
}

function faTOen($string) {
    return strtr($string, array('۰'=>'0', '۱'=>'1', '۲'=>'2', '۳'=>'3', '۴'=>'4', '۵'=>'5', '۶'=>'6', '۷'=>'7', '۸'=>'8', '۹'=>'9', '٠'=>'0', '١'=>'1', '٢'=>'2', '٣'=>'3', '٤'=>'4', '٥'=>'5', '٦'=>'6', '٧'=>'7', '٨'=>'8', '٩'=>'9'));
}
function verify($api, $transId) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'https://pay.ir/payment/verify');
	curl_setopt($ch, CURLOPT_POSTFIELDS, "api=$api&transId=$transId");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$res = curl_exec($ch);
	curl_close($ch);
	return $res;
}
 
function pushsend($title,$con,$id=null) {
	$TOKEN = "95b4c12a6d9cbd6dd1a0c8e2e1c62e027c4cb4b7";
	$data = array(
	  "app_ids" => ["com.sms_system"],
	  "data" => array(
		"title" => $title,
		"content" => $con,
		"ticker" => $title,
		"led_color" => "-5635841",
		"sound_url" => "http://systemsms.ir/ping.mp3",
		 "wake_screen" => true,
		 "action"=> array(
			"url"=> "",
			"action_type"=> "A"
		 ),
		 
	  )
	);
	if($id !== null){
	$data['filters'] = array("pushe_id"=>$id);
	}
	//print_r($data);
	$data_string = json_encode($data);
	$ch = curl_init("http://api.pushe.co/v2/messaging/notifications");
	curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
	  "Content-Type: application/json",
	  "Accept: application/json",
	  "Authorization: Token " . $TOKEN,
	));
	//echo 0;
	curl_setopt($ch, CURLOPT_POST, 1);
	
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$res = curl_exec($ch);
	curl_close($ch);
	return $res;
//$output = curl_exec($ch)
// print_r(json_decode($output,true));

	

}

function send($api, $amount, $redirect, $factorNumber=null) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'https://pay.ir/payment/send');
	curl_setopt($ch, CURLOPT_POSTFIELDS,"api=$api&amount=$amount&redirect=$redirect&factorNumber=$factorNumber");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$res = curl_exec($ch);
	curl_close($ch);
	return $res;
}

function ansmood($id){
	
	if($id == 0){
		return "برسی نشده!";
	}elseif($id == 1){
		return "در حال برسی";
	}elseif($id == 2){
		return "پاسخ داده شد";
	}elseif($id == 3){
		return "بسته شد";
	}
	
}
function rooz($exp){
	$d = floor(($exp-time())/86400);
	if($d < 0){
		$d = 0;
	}
	return $d;
}
function package($id){
		global $conn;
		$sql = "SELECT * from package WHERE code=$id ";
		$Query = $conn->query($sql);
		$data = $Query->fetch_assoc();
		//echo "Error: " . $sql . "<br>" . $conn->error;
		 return $data['smsprice'];
	}
	function packim($id){
		//echo "SELECT * from package WHERE code=$id ";
		global $conn;
		$sql = "SELECT * from package WHERE code=$id ";
		$Query = $conn->query($sql);
		$data = $Query->fetch_assoc();
		//echo "Error: " . $sql . "<br>" . $conn->error;
		echo $conn->error;
		 return $data;
	}
	function packid($id){
		global $conn;
		$sql = "SELECT * from package WHERE id=$id ";
		$Query = $conn->query($sql);
		$data = $Query->fetch_assoc();
		//echo "Error: " . $sql . "<br>" . $conn->error;
		 return $data;
	}
	function smsstr($id){
		//return ceil($id/70);
		//return strlen($id);
		return ceil(count(mb_str_split($id))/70);
		
	}
	function mb_str_split( $string ) { 
    # Split at all position not after the start: ^ 
    # and not before the end: $ 
    return preg_split('/(?<!^)(?!$)/u', $string ); 
} 
	
function sanitize($var)
    {
        $var = strip_tags($var);
        $var = htmlentities($var, ENT_COMPAT, 'UTF-8');
        $var = stripslashes($var);
        $var = htmlspecialchars($var);
       // $var=str_replace("/","",$var);
        //$var=str_replace("\\","",$var);
		//$var=str_replace("and","",$var);
		$var=str_replace("1=0","",$var);
		$var=str_replace("1=1","",$var);
		//$var=str_replace(" ","-",$var);
		//$var=str_replace("'","",$var);
       // $var=str_replace("%","",$var);
        $var=str_replace("$","",$var);
        $var=str_replace("#","",$var);
        $var=str_replace("^","",$var);
        $var=str_replace("~","",$var);
        //$var=str_replace("etc","",$var);
        //$var=str_replace("passwd","",$var);
        $var=str_replace("<script>","",$var);
        $var=str_replace("<div>","",$var);
        $var=str_replace("while","w h i l e",$var);
        $var=str_replace("for","F o r",$var);
        return $var;
    }

?>