<?php


$gettype = 'credit';
	
	$from ="985000145";
	$message ="تست ";
	$postalcode = "";
	$gender = "";
	$type = "";
	$time ="";
	$az = 0;
	$ta = 0;
	$uinqid ="";
	$fileUrl ="";
	$url = "37.130.202.188/services.jspd";
	$rcpt_nm = json_encode(array('9149049445'));
	//echo json_encode($rcpt_nm);
//	echo time();
	if(isset($_POST['gettype']) && $_POST['gettype'] !== "" ){
		$gettype  = sanitize($_POST['gettype']);
	}
	if(isset($_POST['from']) && $_POST['from'] !== ""){
			$from = sanitize($_POST['from']);
	}
	if(isset($_POST['message']) && $_POST['message'] !== "" ){
		
			$message = $_POST['message'];
			if($gettype == "ptop"){
				$msg = $message;
			}else{
				$msg = $message;
			}
	}
	if(isset($_POST['postalcode']) && $_POST['postalcode'] !== "" ){
		
			$postalcode = sanitize($_POST['postalcode']);
			$gender = sanitize($_POST['gender']);
			$type = sanitize($_POST['type']);
	}
	if(isset($_POST['time']) && $_POST['time'] !== ""){
			$time = sanitize($_POST['time']);
	}
	if(isset($_POST['uinqid']) && $_POST['uinqid'] !== ""){
			$uinqid = sanitize($_POST['uinqid']);
	}
	if(isset($_POST['az']) && $_POST['az'] !== ""){
			$az = sanitize($_POST['az']);
	}
	if(isset($_POST['ta']) && $_POST['ta'] !== ""){
			$ta = sanitize($_POST['ta']);
	}
	if(isset($_POST['fileUrl']) && $_POST['fileUrl'] !== ""){
			$fileUrl = sanitize($_POST['fileUrl']);
			$msg = $fileUrl;
	}
	if(isset($_POST['to']) && $_POST['to'] !== ""){
		
		
			$rcpt_nm = $_POST['to'];
			$numralar = json_decode($rcpt_nm);
	}
	
	// Array ( [uname] => saeed200990 [pass] => 123456 [op] => postalcodecount [type] => [gender] => [postalcode] => )

	

$credit = array('uname'=>$uname,'pass'=>$pass,'op'=>'credit');
$accesslist2 = array('uname'=>$uname,'pass'=>$pass,'op'=>'lines');
$news2 = array('uname'=>$uname,'pass'=>$pass,'op'=>'news');
$getpost = array('uname'=>$uname,'pass'=>$pass,'op'=>'postalcodecount','type'=>$type,'gender'=>$gender,'postalcode'=>$postalcode );
$sendpost = array('uname'=>$uname,'pass'=>$pass,'from'=>'50002040','op'=>'postalcodesend','message'=>$message,'type'=>$type,'gender'=>$gender,'from_row'=> $az,'count_send' => $ta,'postalcode'=>$postalcode );
$inboxlist = array('uname'=>$uname,'pass'=>$pass,'op'=>'inboxlist');
$deleteperiod = array('uname'=>$uname,'pass'=>$pass,'id'=>$uinqid,'op'=>'deleteperiod');
$delivery = array('uname'=>$uname,'pass'=>$pass,'op'=>'delivery','uinqid'=>$uinqid);
$ptop = array('uname'=>$uname,'pass'=>$pass,'from'=>$from,'message'=>$message,'to'=>$rcpt_nm,'op'=>'send');
$period = array('uname'=>$uname,'pass'=>$pass,'from'=>$from,'message'=>$message,'to'=>$rcpt_nm,'time' => $time,'op'=>'send');
$sms = array('uname'=>$uname,'pass'=>$pass,'from'=>$from,'message'=>$message,'to'=>$rcpt_nm,'op'=>'send');
$sendvoice = array('uname'=>$uname,'pass'=>$pass,'repeat'=>'1', 'to'=>$rcpt_nm,'fileUrl'=>$fileUrl,'op'=>'sendvoice' );
//print_r($getpost);
//print_r($_POST);
//exit();


if($_POST['gettype'] == "ptop" || $_POST['gettype'] == "sms" || $_POST['gettype'] == "sendvoice" || $_POST['gettype'] == "period"|| $_POST['gettype'] == "sendpost"){
	
	
	$numralar = json_decode($rcpt_nm);
	$nmcunt = count($numralar);
	$MS = json_decode($msg,true);
	$mscunt = 0;
	if($MS == null){
		$MS = $msg;
		$mscunt = smsstr($MS);
	}else{
		$mscunt = smsstr($MS[0]+8);
	}
	
	$restime = time()+900;
	
	
	if($_POST['gettype'] == "sendvoice"){
		 //$getpnum = getpnum($type,$gender,$postalcode);
		$mablagh  = 50*$nmcunt;
		$MS = $fileUrl;
		$from = "voice";
		//echo $getpnum."/".$mablagh."/".$mscunt."/".$price[$from];
	//exit();
	}elseif($_POST['gettype'] == "period"){
		 //$getpnum = getpnum($type,$gender,$postalcode);
		 $restime = strtotime($time)+900;
	}elseif($_POST['gettype'] == "sendpost"){
		
		 $getpnum = getpnum($type,$gender,$postalcode);
		 $nmcunt = $getpnum; 
		 $numralar = "postal code:".$postalcode;
		$mablagh  = $price[$from]* $getpnum*$mscunt;
		//print_r($sendpost);
		//echo $getpnum."/".$mablagh."/".$mscunt."/".$price[$from];
	//exit();
	}elseif($_POST['gettype'] == "ptop"){
		
		
		$mablagh  = $price[$from]* $nmcunt*$mscunt;
		//echo $getpnum."/".$mablagh."/".$mscunt."/".$price[$from];
	//exit();
	}else{
		
		$mablagh  = $price[$from]*$nmcunt*$mscunt;
	}
	
if($udata['credit'] > $mablagh){	
	
	$newmab = $udata['credit']-$mablagh;
	$handler = curl_init($url);             
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handler, CURLOPT_POSTFIELDS, $$gettype);                       
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
$response2 = curl_exec($handler);

$response2 = json_decode($response2);

$res_code = $response2[0];
$res_data = $response2[1];

echo json_encode(array('status'=>1,'msg'=>$res_data));
	
	$full = array('اعتبار'=>$udata['credit'],'طول پیام'=>$mscunt,'هزینه ارسال'=>$mablagh,'تعداد شماره ها'=>$nmcunt,'ارسال از'=>$from,'شماره ها'=>$numralar,'پیام'=>$MS);
	//INSERT INTO `ulog`(`id`, `uid`, `sendtype`, `deliverid`, `date`, `data`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6])
	$conn->query("INSERT INTO `ulog`(`id`, `uid`, `sendtype`, `deliverid`, `date`, `resdate`, `data`) VALUES ('','".$uid."','".$gettype."','".$res_data."','".time()."','".$restime."','".json_encode($full, JSON_UNESCAPED_UNICODE)."')");
	
	$conn->query("UPDATE users SET credit=$newmab WHERE id=".$uid);
	
	}else{
		
		
		echo json_encode(array('status'=>0,'msg'=>'اعتبار شما جهت ارسال کافی نیست'));
		
	}
	
}else{
	
	
	$handler = curl_init($url);             
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handler, CURLOPT_POSTFIELDS, $$gettype);                       
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
$response2 = curl_exec($handler);

$response2 = json_decode($response2);

$res_code = $response2[0];
$res_data = $response2[1];
//return;'{"status":1,"msg":"'.$res_data.'"}';
if(isset($_POST['gettype']) && $_POST['gettype'] == "credit" ){$res_data = number_format(floor($res_data)); }


if(isset($_POST['gettype']) && $_POST['gettype'] == "delivery" ){
//	print_r($_POST);
		if($uinqid !== ""){
			//$res_data = number_format(floor($res_data));
			//echo $res_data;
			$query = $conn->query("SELECT * FROM ulog WHERE deliverid=".$uinqid);
			$row = $query->fetch_assoc();
			$addt = json_decode($row['data'],true);
			$tdata = json_decode($row['tdata'],true);
			$row['data'] = $addt;
			$row['date'] = pdate("Y/m/d H:i:s",$row['date']);
			$row['reportTime'] = pdate("Y/m/d H:i:s",$row['resdate']);
			//print_r($row);
			$row['ersal'] = $addt['ارسال از'];
			$row['ersal_price'] = $addt['هزینه ارسال'];
			$row['message'] = $addt['پیام'];
			if($row['tasviye'] == 1){
				$row['comback_price'] = $tdata['مبلغ بازگشتی'];
			}else{
				$row['comback_price'] = "تسویه انجام نشده است";
			}
			
			unset($row['data']);
			unset($row['id']);
			unset($row['uid']);
			unset($row['tdata']);
			//unset $addt['شماره ها'];
			//unset $addt['اعتبار'];
			//unset $addt['شماره ها'];
			$stype = $row['sendtype'];
				if($stype !== 'ptop'){
					if($row['ersal'] !== '98sim'){
						$data = str_replace(':','":"',$res_data);
						$data = str_replace('[','{',$data);
						$data = str_replace(']','}',$data);
						//$counts = array_count_values($data);
						//echo $counts['discarded'];
						$data = json_decode($data,true);
						$kol = count($data);
						$discarded = array();
						$sended = array();
						foreach($data as $key=>$val){
							if($val == "discarded" || $val == "noresponse"){
								array_push($discarded,$key);
							}else{
								array_push($sended,$key);
							}
						}
						$data = array(
							//'kol_num'=>count($kol),
							'sended_num'=>count($sended),
							'discarded_num'=>count($discarded),
							'sended'=>$sended,
							'discarded'=>$discarded,
							'more'=>$row
						);
						echo json_encode($data);
									
					}else{
						echo json_encode(array('status'=>0,'msg'=>'ارسال از سیم کارت دلیوری ندارد','data'=>null));
					}
				}else{
					
					echo json_encode(array('status'=>0,'msg'=>'ارسال نفر به نفر  وارسال پستی دلیوری ندارد','data'=>null));
				}
			//echo $res_data;
		}else{
			
			
		}
	
	
	
	}elseif(isset($_POST['gettype']) && $_POST['gettype'] == "getpost" ){ echo'{"status":1,"msg":"'.$res_data.'"}'; }else{
		
		echo $res_data;
	}

	
}

function filterbug($param){
	
	return $param;
}

function getpnum($type,$gender,$postalcode){
	global $uname;
	global $pass;
	global $url;
	$getpost = array('uname'=>$uname,'pass'=>$pass,'op'=>'postalcodecount','type'=>$type,'gender'=>$gender,'postalcode'=>$postalcode );
	$handler = curl_init($url);             
	curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($handler, CURLOPT_POSTFIELDS, $getpost);                       
	curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
	$response2 = curl_exec($handler);
	$response2 = json_decode($response2);
	$res_code = $response2[0];
	$res_data = $response2[1];
	return $res_data;
	}



?>
