<?php
set_time_limit(0);
ini_set('memory_limit', '-1');
include("config.php");
include("functions.php");
//include("function_sk");
error_reporting(E_ALL);
//error_reporting(E_ALL & ~E_NOTICE);
///For Cron
$start = "select * from settings_api_quick 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_api_quick 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_api_quick WHERE id=6";
	echo $sqlres ; 
	$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");
		$sqlupdatestime = "UPDATE settings_api_quick SET locked_at = '$startdate' WHERE id =3";
		$resupdatestime = mysqli_query($connect,$sqlupdatestime);
		
		 $sqlupdate = "UPDATE settings_api_quick SET value = 'Locked' WHERE id=6";
		 $resupdate = mysqli_query($connect,$sqlupdate);
		 
		  // 1oct2024 sk 
		$sqlendtimesavesk_a = "UPDATE settings_api_quick SET locked_at = '$startdate' WHERE id =6";
		mysqli_query($connect,$sqlendtimesavesk_a);
		
		 $sms_per_sec = returnName1("value", "settings_api_quick", "name", "SMS_PER_SECOND",$connect);
		 $sms_wait_sec = returnName1("value", "settings_api_quick", "name", "SMS_WAIT_TIME",$connect);
		 SendAllSMS($sms_per_sec,$sms_wait_sec,$connect);
			
		$sqlupdate1 = "UPDATE settings_api_quick 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_api_quick", "name", "SMS_FORCE_OFF",$connect);
$i=0;
//echo 'fdsfdsfdsf'.$sms_force_off ; die ;

if($sms_force_off != "1"){
//$sqlsms="SELECT * FROM composeAPI WHERE sms_status = 'Pending' ORDER BY id ASC LIMIT $sms_per_sec";
$sqlsms="SELECT * FROM api_quicksms WHERE sms_status = 'Pending' ORDER BY id ASC LIMIT $sms_per_sec";
//$sqlsms="SELECT * FROM api_quicksms WHERE id = '575' ORDER BY id ASC LIMIT $sms_per_sec";
//echo 'dfghdjbfdgfdj'.$sqlsms." sk <br>"; //die ; 

$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['networkname'] . " === ".$row['id']." => ".$row['mobile_number'];
		 $smsID = $row["id"];
		  $smsID_sk = $row["message_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'];
		 	$skcustomer = $row['customer']; ;
		 	$message_actuall_response = $row['message_actuall_response']; ;



		 	
		 if(strtolower($row['networkname']) == 'smpp')
		 {
		 	 MobilinkSMPP("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID_sk,$userid ,$smpp_report , $smpp_url);
		 }
		 
		 	
		 if(strtolower($row['networkname']) == 'mobilink')
		 {
		 	 //Mobilink("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 	// if($skcustomer == 1561){
		 	if($message_actuall_response == 'yes'){
		 	        Mobilinkme("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		      }else{
		            Mobilink("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		      }
		 }
		 if(strtolower($row['networkname']) == 'mobilinkbiz')
		 {
		     
		      //if($skcustomer == 1561){
		      if($message_actuall_response == 'yes'){
		 	        Mobilinkbizme("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		      }else{
		           
		            Mobilinkbiz("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID );
		      }
		     
		 	 
		 }
		 
		 
		 if(strtolower($row['networkname']) == 'mobilinkinfo')
		 {
		  MobilinkA("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		    
		 }
		 
		 
		 if(strtolower($row['networkname']) == 'ufone')
		 {
			 
		   	 Ufone("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 }
		 if(strtolower($row['networkname']) == 'telenor')
		 {
		  	 Telenor("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 }
		if(strtolower($row['networkname']) == 'zong')
		{
			Zong("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		}

		
				
		
		//$sqlUpdateSMS="UPDATE composeAPI SET sms_status='Sent' where id='$smsID'";
		$sqlUpdateSMS="UPDATE api_quicksms SET sms_status='Sent' 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_api_quick SET value = 'Unlocked' WHERE id =6";
		$resupdate1 = mysqli_query($connect,$sqlupdate1);
		$ENDdate = date("Y-m-d H:i:s");
		$sqlupdateetime = "UPDATE settings_api_quick SET locked_at = '$ENDdate' WHERE id =4";
		$resupdateetime = mysqli_query($connect,$sqlupdateetime);
			// 01oct2024 sk 
		$sqlendtimesavesk = "UPDATE settings_api_quick SET locked_at = '$ENDdate' WHERE id =6";
		mysqli_query($connect,$sqlendtimesavesk);
		echo "<br><br><b>Script end at ". date("H:i:s")."</b><br>";
		exit;
	}
  }else{
	// exit if forecefully shutdown
	echo "Cron forecefully shutdown !!";
	exit;
  
  }	

}							
?>
