<?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 settings_composeAPI_noeffi 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_composeAPI_noeffi 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_composeAPI_noeffi 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_composeAPI_noeffi SET value = 'Locked' WHERE id=6";
		 $resupdate = mysqli_query($connect,$sqlupdate);
		 
		 // 28sep2024 sk 
		$sqlendtimesavesk_a = "UPDATE settings_composeAPI_noeffi SET locked_at = '$startdate' WHERE id =6";
		mysqli_query($connect,$sqlendtimesavesk_a);
		
		$sqlupdatestime = "UPDATE settings_composeAPI_noeffi SET locked_at = '$startdate' WHERE id =3";
		$resupdatestime = mysqli_query($connect,$sqlupdatestime);
		
		 $sms_per_sec = returnName1("value", "settings_composeAPI_noeffi", "name", "SMS_PER_SECOND",$connect);
		 $sms_wait_sec = returnName1("value", "settings_composeAPI_noeffi", "name", "SMS_WAIT_TIME",$connect);
		 SendAllSMS($sms_per_sec,$sms_wait_sec,$connect);
			
		
		$sqlupdate1 = "UPDATE settings_composeAPI_noeffi 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_composeAPI_noeffi", "name", "SMS_FORCE_OFF",$connect);
$i=0;
if($sms_force_off != "1"){
//$sqlsms="SELECT * FROM composeAPI_noeffi WHERE sms_status = 'Pending' ORDER BY id ASC LIMIT $sms_per_sec";
$sqlsms="SELECT * FROM composeAPI_noeffi WHERE sms_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'];
			$api_password = $row['api_password'];
			$api_unicode = $row['api_unicode'];
			$mask = $row['mask_name'];
		 	$number = $row['mobile_number'];
		 	$message = $row['message'];
				$userid = $row['customer'];
		 	//$skcustomer = $row['customer']; ;
		 	$message_actuall_response = $row['message_actuall_response']; ;
		 	$smsID_sk = $row["message_id"];
		 $smpp_report =  $row['smpp_report'];
		 	$smpp_url = "http://147.93.86.210:13013/cgi-bin/sendsms?";
		 
		 if(strtolower($row['networkname']) == 'anf')
		 {
		  //MobilinkA("composeAPI_noeffi" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		  
		  $starting_time = date("Y:m:d H:i:s");
		  $starting_timesql="UPDATE composeAPI_noeffi SET starting_time='".$starting_time."' where id='$smsID'";
		$queryUpdateSMS=mysqli_query($connect,$starting_timesql);
		
		  
		   MobilinkSMPPanf("api_quicksms" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID_sk,$userid ,$smpp_report , $smpp_url);

		    
		 }
		 
		  if(strtolower($row['networkname']) == 'mobilinkbiz') {
		     // if($skcustomer == 1561){
		     
		     $starting_time = date("Y:m:d H:i:s");
		  $starting_timesql="UPDATE composeAPI_noeffi SET starting_time='".$starting_time."' where id='$smsID'";
		$queryUpdateSMS=mysqli_query($connect,$starting_timesql);
		     
		     
		     if($message_actuall_response == 'yes'){
		 	        Mobilinkbizme("composeAPI_noeffi" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		      }else{
		            Mobilinkbiz_effi("composeAPI_noeffi" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		      }
		 }
		 if(strtolower($row['networkname']) == 'mobilink')
		 {
		      //if($skcustomer == 1561){
		      if($message_actuall_response == 'yes'){
		 	        Mobilinkme("composeAPI_noeffi" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		      }else{
		            Mobilink("composeAPI_noeffi" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		      }
		 }
		 if(strtolower($row['networkname']) == 'mobilinkinfo')
		 {
		  MobilinkA("composeAPI_noeffi" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);

		    
		 }
		 if(strtolower($row['networkname']) == 'ufone')
		 {
			 
		   	 Ufone("composeAPI_noeffi" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 }
		 if(strtolower($row['networkname']) == 'telenor')
		 {
		  	 Telenor("composeAPI_noeffi" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 }
		if(strtolower($row['networkname']) == 'zong')
		{
			Zong("composeAPI_noeffi" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		}
		//$sqlUpdateSMS="UPDATE composeAPI_noeffi SET sms_status='Sent' where id='$smsID'";
		$sqlUpdateSMSasd="UPDATE composeAPI_noeffi SET sms_status='Sent' where id='$smsID'";
		//echo 'BNBNBN'.$sqlUpdateSMSasd ;
		//$sqlUpdateSMS="DELETE FROM composeAPI_noeffi where id='$smsID'";
		$queryUpdateSMS=mysqli_query($connect,$sqlUpdateSMSasd);

	 }
		usleep($sms_wait_sec);
		SendAllSMS($sms_per_sec,$sms_wait_sec,$connect);
		exit;
	}else{
		
		$sqlupdate1 = "UPDATE settings_composeAPI_noeffi SET value = 'Unlocked' WHERE id =6";
		$resupdate1 = mysqli_query($connect,$sqlupdate1);
		
		$endtimesave = date("Y-m-d H:i:s");
		$sqlendtimesave = "UPDATE settings_composeAPI_noeffi SET locked_at = '$endtimesave' WHERE id =4";
		//echo $sqlendtimesave ; 
		$resendtimesave = mysqli_query($connect,$sqlendtimesave);
		// 28sep2024 sk 
		$sqlendtimesavesk = "UPDATE settings_composeAPI_noeffi SET locked_at = '$endtimesave' WHERE id =6";
		mysqli_query($connect,$sqlendtimesavesk);
		
		
		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;
  
  }	

}							
?>