Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/crons/
Upload File :
Current File : /var/www/cesa.co.za/crons/sendsms.php

<?php

if (!isset($_SERVER['DOCUMENT_ROOT']) || strlen($_SERVER['DOCUMENT_ROOT']) <= 0) {
    $_SERVER['DOCUMENT_ROOT'] = str_replace('/crons', '', getcwd());
    // echo getcwd();
}

function SendSMS($to, $msg) {
	
	$sendurl="http://api.clickatell.com/http/sendmsg?user=CESA1SMS&password=abc123&api_id=3321299&concat=2&to=".$to."&text=".substr(urlencode($msg),0,320);


//	echo "SMS ".$msg." to ".$to;

	$ch = curl_init($sendurl);

	curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	
	$response = curl_exec($ch);
	var_dump($response);
	curl_close($ch);
}

function SendFromTables($SMSLog, $SMSSendLog) {
	global $db;
	// Get the message
	$sql = "SELECT * FROM ".$SMSLog." WHERE SMStart<='".date("Y-m-d H:i:s")."' AND SMSComplete=0";
	$rsMsg = mysqli_query($db, $sql);

	echo ".";
	flush();
	ob_flush();
	
	while ($rsM = mysqli_fetch_assoc($rsMsg)) {
		
		$bSendFinished = false;
	
		while (!$bSendFinished) {
			$sql = "SELECT SMSToID, SMSToNumber FROM ".$SMSSendLog." WHERE SMSID='".$rsM["SMSID"]."' and SMSSent=0 ORDER BY SMSToNumber LIMIT 0, 50";
			
			$rsRec = mysqli_query($db, $sql);
			
			if (mysqli_num_rows($rsRec) <= 0) {
				$bSendFinished = true;
				$sql="UPDATE ".$SMSLog." SET SMSComplete=1, SMSEnd='".date("Y-m-d H:i:s")."' WHERE SMSID='".$rsM["SMSID"]."'";
				mysqli_query($db, $sql);
			} else {
			
				$IDList = "";
				$ToList = "";
				$iMsgsSent=0;
				while ($rsR = mysqli_fetch_assoc($rsRec)) {
					$to = $rsR["SMSToNumber"];
					if (!empty($to)) {
						$to = str_replace(array("(",")"," ","-"),"",$to);
						$to=ltrim($to,"+27");
						$to=ltrim($to,"0");
						$to = "0".$to;
		
						$ToList .= $to.",";
						$iMsgsSent++;
					}
					$IDList .= $rsR["SMSToID"].",";
				}
				$ToList = rtrim($ToList, ",");
				$IDList = rtrim($IDList, ",");
				
				SendSMS($ToList, $rsM["SMSText"]);
				
				$bSomethingSent = true;
				
				$sql = "UPDATE ".$SMSSendLog." SET SMSSent=1 WHERE SMSToID in (".$IDList.")";
				mysqli_query($db, $sql);

				$sql = "UPDATE ".$SMSLog." SET SMSProgressCount=SMSProgressCount+".$iMsgsSent." WHERE SMSID='".$rsM["SMSID"]."'";
				mysqli_query($db, $sql);

				echo $ToList."<BR>";
				echo ".";
				flush();
				ob_flush();
			}
			mysqli_free_result($rsRec);

			//wait for a quarter second
			usleep(100000);
		}
	}
}

//if ($_GET["override"] != 1) die();

ini_set('max_execution_time', '0'); // 0 = no limit
ob_start();

include_once($_SERVER['DOCUMENT_ROOT'] . "/cfg/config.php");
$db = mysqli_connect(EW_CONN_HOST, EW_CONN_USER, EW_CONN_PASS, EW_CONN_DB);

$bCanSend = false;
$bSomethingSent = false;

$sql = "SELECT ConfigName, ConfigValue FROM Config WHERE ConfigName in ('StartSMSSend', 'EndSMSSend')";
$rsRec = mysqli_query($db, $sql);
$dtStartSend = "";
$dtEndSend = "";
while ($rsR = mysqli_fetch_assoc($rsRec)) {
	if ($rsR["ConfigName"]=="StartSMSSend") $dtStartSend = date("Y-m-d H:i:s", strtotime($rsR["ConfigValue"]));
	if ($rsR["ConfigName"]=="EndSMSSend") $dtEndSend = date("Y-m-d H:i:s", strtotime($rsR["ConfigValue"]));
}

if ((time() - strtotime($dtEndSend)) > 0) $bCanSend = true;

if ($bCanSend) {
	
	// Make sure another instance of this script doesn't start sending!!
	$sql = "UPDATE Config SET ConfigValue='".date("Y-m-d H:i:s")."' WHERE ConfigName='StartSMSSend'";
	mysqli_query($db, $sql);
	$sql = "UPDATE Config SET ConfigValue='2020-01-01' WHERE ConfigName='EndSMSSend'";
	mysqli_query($db, $sql);
	
	$sql="UPDATE MemberContacts JOIN SMSUnsubscribe ON (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(MemberContacts.`CellPhone`, '+27', ''), ' ', ''), '(', ''), ')', ''), '-', '')=SMSUnsubscribe.`MobileNumber`) SET MemberContacts.`SMSUnsubscribe`=1";
	mysqli_query($db, $sql);

	$sql="UPDATE OfficeContacts JOIN MemberContacts ON (ContactsID=ContactID) SET OfficeContacts.`SMSUnsubscribe`=1 WHERE MemberContacts.`SMSUnsubscribe`=1";
	mysqli_query($db, $sql);

	
	SendFromTables("SMSLog", "SMSSendLog");
	SendFromTables("SchoolSMSLog", "SchoolSMSSendLog");
	SendFromTables("EventSMSLog", "EventSMSSendLog");

	$sql = "UPDATE Config SET ConfigValue='".date("Y-m-d H:i:s")."' WHERE ConfigName='EndSMSSend'";
	mysqli_query($db, $sql);

} else {
	echo "Can't send yet.";
}

flush();
ob_flush();
				
?>