<?php
set_time_limit(0);
include("config.php");
include("functionsTest.php");
ini_set('memory_limit','1024M');
ob_start();
ob_clean();
ini_set('max_execution_time', 0);

///For Cron
$start = "select * from settings 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 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 WHERE id=6";
	$queryres=mysqli_query($connect,$sqlres);
	$res_row = mysqli_fetch_array($queryres);
	//echo $res_row["value"];

	if($res_row["value"] == "Unlocked"){
		echo "Script start at ". date("H:i:s")."<br>";
		 //$sqlupdate = "UPDATE settings SET value = 'Locked' WHERE id=6";
		//$resupdate = mysqli_query($connect,$sqlupdate);
		
			$sms_per_sec = returnName1("value", "settings", "name", "SMS_PER_SECOND",$connect);
			$sms_wait_sec = returnName1("value", "settings", "name", "SMS_WAIT_TIME",$connect);
			SendAllSMS($sms_per_sec,$sms_wait_sec,$connect);
			
		$sqlupdate1 = "UPDATE settings 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", "name", "SMS_FORCE_OFF",$connect);
$i=0;
ob_start();
if($sms_force_off != "1"){
$sqlsms="SELECT * FROM composeAPINEW WHERE sms_status = 'Pending' ORDER BY id ASC LIMIT $sms_per_sec";

$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'];
	//	echo "<br>".$row['id']." => ".$row['mobile_number']." => ";
		 if(strtolower($row['networkname']) == 'mobilink')
		 {
		 	$mask = $row['mask_name'];
		 	$number = $row['mobile_number'];
		 	$message = $row['message'];
		 	 Mobilink($mask,$number,$message);
			 ob_flush();
             flush();
		 }
		 if(strtolower($row['networkname']) == 'ufone')
		 {
		 	$mask = $row['mask_name'];
		 	$number = $row['mobile_number'];
		 	$message = $row['message'];
		  	 Ufone($mask,$number,$message);
		 }
		 if(strtolower($row['networkname']) == 'telenor')
		 {
		 	$mask = $row['mask_name'];
		 	$number = $row['mobile_number'];
		 	$message = $row['message'];
		  	 Telenor($mask,$number,$message);
		 }
		if(strtolower($row['networkname']) == 'zong')
		{
			$mask = $row['mask_name'];
			$number = $row['mobile_number'];
			$message = $row['message'];
		 	Zong($mask,$number,$message);
		}

		 $smsID = $row["id"];
				
		
		$sqlUpdateSMS="UPDATE composeAPINEW SET sms_status='Sent' where id='$smsID'";
		//$sqlUpdateSMS="DELETE FROM composeapi where id='$smsID'";
		$queryUpdateSMS=mysqli_query($connect,$sqlUpdateSMS);
		ob_flush();
        flush();

	 }
		ob_flush();
        flush();
		usleep($sms_wait_sec);
		ob_end_flush();
		SendAllSMS($sms_per_sec,$sms_wait_sec,$connect);
		exit;
	}else{
		
		$sqlupdate1 = "UPDATE settings SET value = 'Unlocked' WHERE id =6";
		$resupdate1 = mysqli_query($connect,$sqlupdate1);
		echo "Script end at ". date("H:i:s")."<br>";
		ob_end_flush();
		exit;
	}
  }else{
	// exit if forecefully shutdown
	echo "Cron forecefully shutdown !!";
	exit;
  
  }	

}							
?>
