<?
require_once "include/mysql_config.php";
require_once('include/database.php');
require_once "include/function.php";

$configuration_query = db_query('select configuration_key as cfgKey, configuration_value as cfgValue from configuration');
while ($configuration = db_fetch_array($configuration_query)) {
	define($configuration['cfgKey'], $configuration['cfgValue']);
}

function SendSMSChinese($phone, $msg, $debug=false, $sender){
	$bulksms2u_user = SMS_USER;
	$bulksms2u_password = SMS_PWD;
	$bulksms2u_url = SMS_APIUNICODE;
	  

      $url = 'username='.$bulksms2u_user;
      $url.= '&password='.$bulksms2u_password;
      $url.= '&sender='.$sender;
      $url.= '&type=3';
      $url.= '&mobile='.urlencode($phone);
      $url.= '&message='.$msg;
//      $url.= '&message='.urlencode($msg);
      $urltouse =  $bulksms2u_url.$url;
      if ($debug) { echo "Request: <br>$urltouse<br><br>"; }

      //Open the URL to send the message
      $response = httpRequest($urltouse);
      if ($debug) {
           echo "Response: <br><pre>".
           str_replace(array("<",">"),array("&lt;","&gt;"),$response).
           "</pre><br>"; }

      return($response);
}

$i = 60;
while ($i > 0) {
    $i=$i-2;  

	unset($myrow);
	$sql="select smst_chinese,smst_chimsg,smst_msg,smsa_sender,smso_phone,smso_id from sms_outbox left join sms_alert on smso_alert=smsa_id left join sms_template on smsa_template=smst_id 
	where smso_status='Pending'  order by smso_id limit 1";
	$result=mysql_query($sql) or die(mysql_error());	
	$myrow=MySQL_fetch_array($result,MYSQL_ASSOC);
	
	if($myrow["smso_phone"]){
		$sql="update sms_outbox set smso_status='Process', smso_senddate=".CURRENT_TIMESTAMP." where  smso_id='".$myrow["smso_id"]."'";
		@mysql_query($sql) or die(mysql_error());
	
		if($myrow["smst_chinese"]=="Y")$res=SendSMSChinese($myrow["smso_phone"],$myrow["smst_chimsg"],false,$myrow["smsa_sender"]);						
		else $res=SendSMS($myrow["smso_phone"],$myrow["smst_msg"],false,$myrow["smsa_sender"]);						
	
		$sql="update sms_outbox set smso_status='$res' where smso_id='".$myrow["smso_id"]."'";
		@mysql_query($sql) or die(mysql_error());	
	}
	sleep(2);

}

?>