<?php
set_time_limit(0);
ini_set('memory_limit', '-1');
include("config.php");
//include("functions.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
///For Cron
$start = "select * from setting_make_call where name='Cron_start'";
$start_que_start = mysqli_query($connect,$start);
$startData = mysqli_fetch_array($start_que_start);
$startArray = explode(":",$startData['value']);
//print_r($startArray);
$stopQuery = "select * from setting_make_call where name='Cron_stop'";
$start_que_stop = mysqli_query($connect,$stopQuery);
$stopData = mysqli_fetch_array($start_que_stop);
$stopArray = explode(":",$stopData['value']);
$cronStop = date("H:i",strtotime($stopData['value']));
$cronStart = date("H:i",strtotime($startData['value']));

if(date("H:i") <= $cronStop and date("H:i") >= $cronStart){


	$sqlres="SELECT * FROM setting_make_call WHERE id=6";
	$queryres=mysqli_query($connect,$sqlres);
	$res_row = mysqli_fetch_array($queryres);
	//echo $res_row["value"];

	if($res_row["value"] == "Unlocked"){
		echo "<b>Script start at ". date("H:i:s")."</b><br>";
		 $startdate = date("Y-m-d H:i:s");
		
		 $sqlupdate = "UPDATE setting_make_call SET value = 'Locked' WHERE id=6";
		 $resupdate = mysqli_query($connect,$sqlupdate);
		
		$sqlupdatestime = "UPDATE setting_make_call SET locked_at = '$startdate' WHERE id =3";
		$resupdatestime = mysqli_query($connect,$sqlupdatestime);
		
		 $sms_per_sec = returnName1("value", "setting_make_call", "name", "SMS_PER_SECOND",$connect);
		 $sms_wait_sec = returnName1("value", "setting_make_call", "name", "SMS_WAIT_TIME",$connect);
		 SendAllSMS($sms_per_sec,$sms_wait_sec,$connect);
			
		
		$sqlupdate1 = "UPDATE setting_make_call SET value = 'Unlocked' WHERE id =6";
		$resupdate1 = mysqli_query($connect,$sqlupdate1);
		
		
	}
		
}else{
	echo "Cron Stops !!";
	exit;
}

function SendAllSMS($sms_per_sec,$sms_wait_sec,$connect){
$sms_force_off = "0";
$sms_force_off = returnName1("value", "setting_make_call", "name", "SMS_FORCE_OFF",$connect);
$i=0;
if($sms_force_off != "1"){
//$sqlsms="SELECT * FROM make_call_compose WHERE status = 'Pending' ORDER BY id ASC LIMIT $sms_per_sec";
$sqlsms="SELECT * FROM make_call_compose WHERE status = 'Pending' ORDER BY id ASC LIMIT $sms_per_sec";
echo $sqlsms."<br>";

$querysms=mysqli_query($connect,$sqlsms);
if(mysqli_num_rows($querysms) > 0 ){
	while($row=mysqli_fetch_array($querysms))
	{
		//echo '<pre>';print_r($row['mask_name']);
		$i++;
		//echo $i."=>".$row['mobile_number']."<br>";
		
		//echo "<br>".$row['id']." => ".$row['mobile_number'];
		 $smsID = $row["id"];
		 $api_username = $row['api_username'];  //03236486751-14
			$api_password = $row['api_password']; // Jazz@123
			$CLI = $row['CLI']; // 03236486751
			$mask = $row['code_mask'];
		 	$number = $row['phonenumber'];
		 	$message = $row['audio_message'];
		 
		 
		 $cSession = curl_init(); 
curl_setopt($cSession,CURLOPT_URL,"https://enterprise.jazzcmt.com/voice_api.html?Username=".$api_username."&Password=".$api_password."&CLI=".$CLI."&To=".$number."&Template=".$message."");
// curl_setopt($cSession,CURLOPT_URL,"https://enterprise.jazzcmt.com/voice_api.html?Username=03236486751-14&Password=Jazz@123&CLI=03236486751&To=923324830192&Template=1583"); 
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false); 
$result=curl_exec($cSession);
curl_close($cSession);
echo $result;// die ; 
		 
		 
		//$sqlUpdateSMS="UPDATE make_call_compose SET status='Sent' where id='$smsID'";
		$sqlUpdateSMSasd="UPDATE make_call_compose SET status='Sent' where id='$smsID'";
	//	echo 'BNBNBN'.$sqlUpdateSMSasd ;
		//$sqlUpdateSMS="DELETE FROM composeapi where id='$smsID'";
		$queryUpdateSMS=mysqli_query($connect,$sqlUpdateSMSasd);

	 }
		usleep($sms_wait_sec);
		SendAllSMS($sms_per_sec,$sms_wait_sec,$connect);
		exit;
	}else{
		
		$sqlupdate1 = "UPDATE setting_make_call SET value = 'Unlocked' WHERE id =6";
		$resupdate1 = mysqli_query($connect,$sqlupdate1);
		
		$endtimesave = date("Y-m-d H:i:s");
		$sqlendtimesave = "UPDATE setting_make_call SET locked_at = '$endtimesave' WHERE id =4";
		//echo $sqlendtimesave ; 
		$resendtimesave = mysqli_query($connect,$sqlendtimesave);
		echo "<br><br><b>Script end at sksk ". date("H:i:s")."</b><br>";
		exit;
	}
  }else{
	// exit if forecefully shutdown
	echo "Cron forecefully shutdown !!";
	exit;
  
  }	

}							
?>