<?php
set_time_limit(0);
ini_set('memory_limit', '-1');
error_reporting(E_ALL);
include("config.php");
include("functions.php");

///For Cron
$start = "select * from settings_dynamic_compose 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 settings_dynamic_compose 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 settings_dynamic_compose 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 settings_dynamic_compose SET value = 'Locked' WHERE id=6";
		 $resupdate = mysqli_query($connect,$sqlupdate);
		
		$sqlupdatestime = "UPDATE settings_dynamic_compose SET locked_at = '$startdate' WHERE id =3";
		$resupdatestime = mysqli_query($connect,$sqlupdatestime);
		
		 $sms_per_sec = returnName1("value", "settings_dynamic_compose", "name", "SMS_PER_SECOND",$connect);
		 $sms_wait_sec = returnName1("value", "settings_dynamic_compose", "name", "SMS_WAIT_TIME",$connect);
		 SendAllSMS($sms_per_sec,$sms_wait_sec,$connect);
			
		
		$sqlupdate1 = "UPDATE settings_dynamic_compose 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", "settings_dynamic_compose", "name", "SMS_FORCE_OFF",$connect);
$i=0;
if($sms_force_off != "1"){
$sqlsms="SELECT * FROM dynamic_compose WHERE sms_status = 'Pending' ORDER BY id ASC LIMIT $sms_per_sec"; //91468
//$sqlsms="SELECT * FROM dynamic_compose WHERE id = '91513__000' 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'];
			$api_password = $row['api_password'];
			$api_unicode = $row['api_unicode'];
			$mask = $row['mask_name'];
		 	$number = $row['mobile_number'];
		 	$message = $row['message'];
		 	$userid = $row['customer'];
		 	$smpp_report = $row['smpp_report'];
		 	$smpp_url = $row['smpp_url'];
		 	if($smpp_url == 'yes'){
		 	   $smpp_url = 'http://192.254.192.194:13013/cgi-bin/sendsms'; 
		 	}
		 	
			$message_actuall_response = $row['message_actuall_response']; ;
		 if(strtolower($row['networkname']) == 'smpp')
		 {
		 	// MobilinkSMPP("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID,$userid ,$smpp_report);
		 	  MobilinkSMPP("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID,$userid ,$smpp_report , $smpp_url);
		 }
		 if(strtolower($row['networkname']) == 'mobilinkbiz')
		 {
		 	// 
		 	if($message_actuall_response == 'yes'){
		 	        Mobilinkbizme("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		      }else{
		            Mobilinkbiz("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		            
		      }
		 }
		 if(strtolower($row['networkname']) == 'mobilink')
		 {
		 	 //Mobilink("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 	 
		 	 if($message_actuall_response == 'yes'){
		 	        Mobilinkme("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		      }else{
		            Mobilink("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		      }
		 	 
		 }
		  if(strtolower($row['networkname']) == 'mobilinkinfo')
		 {
		  MobilinkA("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		    
		 }
		
		 if(strtolower($row['networkname']) == 'ufone')
		 {
			 
		   	 Ufone("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 }
		 if(strtolower($row['networkname']) == 'telenor')
		 {
		  	 Telenor("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 }
		if(strtolower($row['networkname']) == 'zong')
		{
			Zong("dynamic_compose" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		}

		
				
		
		//$sqlUpdateSMS="UPDATE composeAPI SET sms_status='Sent' where id='$smsID'";
		$currentdatetimec = date("Y:m:d H:i:s"); 
		$sqlUpdateSMS="UPDATE dynamic_compose SET sms_status='Sent' ,response_time='".$currentdatetimec."'  where id='$smsID'";
		//$sqlUpdateSMS="DELETE FROM composeapi where id='$smsID'";
		$queryUpdateSMS=mysqli_query($connect,$sqlUpdateSMS);

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

}							
?>
