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

//testsk("123");
//die ;


$start = "select * from settings_composesmppbulkMobilink 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_composesmppbulkMobilink 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_composesmppbulkMobilink 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_composesmppbulkMobilink SET value = 'Locked' WHERE id=6";
		 $resupdate = mysqli_query($connect,$sqlupdate);
		
		$sqlupdatestime = "UPDATE settings_composesmppbulkMobilink SET locked_at = '$startdate' WHERE id =3";
		$resupdatestime = mysqli_query($connect,$sqlupdatestime);
		
		 $sms_per_sec = returnName1("value", "settings_composesmppbulkMobilink", "name", "SMS_PER_SECOND",$connect);
		 $sms_wait_sec = returnName1("value", "settings_composesmppbulkMobilink", "name", "SMS_WAIT_TIME",$connect);
		 SendAllSMS($sms_per_sec,$sms_wait_sec,$connect);
			
		
		$sqlupdate1 = "UPDATE settings_composesmppbulkMobilink 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_composesmppbulkMobilink", "name", "SMS_FORCE_OFF",$connect);
$i=0;
if($sms_force_off != "1"){
//$sqlsms="SELECT * FROM composesmppbulkMobilink WHERE sms_status = 'Pending' ORDER BY id ASC LIMIT $sms_per_sec";
$sqlsms="SELECT * FROM composesmppbulkMobilink 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'];
		 	
		 if(strtolower($row['networkname']) == 'mobilink')
		 {
		 	 MobilinkSMPP("composesmppbulkMobilink" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 }
		 if(strtolower($row['networkname']) == 'ufone')
		 {
			 
		   	 Ufone("composesmppbulkMobilink" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 }
		 if(strtolower($row['networkname']) == 'telenor')
		 {
		  	 Telenor("composesmppbulkMobilink" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		 }
		if(strtolower($row['networkname']) == 'zong')
		{
			Zong("composesmppbulkMobilink" , "telecom_response" , $api_username , $api_password , $api_unicode  ,$mask,$number,$message,$smsID);
		}

		
				
		
		//$sqlUpdateSMS="UPDATE composesmppbulkMobilink SET sms_status='Sent' where id='$smsID'";
		$sqlUpdateSMS="UPDATE composesmppbulkMobilink SET sms_status='Sent' where id='$smsID'";
		//$sqlUpdateSMS="DELETE FROM composesmppbulkMobilink 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_composesmppbulkMobilink SET value = 'Unlocked' WHERE id =6";
		$resupdate1 = mysqli_query($connect,$sqlupdate1);
		
		$endtimesave = date("Y-m-d H:i:s");
		$sqlendtimesave = "UPDATE settings_composesmppbulkMobilink 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;
  
  }	

}							
?>