<?php
    require "../commonincludefiles.php";
	set_time_limit(0);
	
	$trans_id = Escape($_REQUEST["trans_id"]);
	
	$msgpifix = Escape($_REQUEST["msgpifix"]);
	$msgbody = Escape($_REQUEST["msgbody"]);
	$msgsufix = Escape($_REQUEST["msgsufix"]);
	//$trans_id = 1;
	
	$condition = " AND sms_status = 'pending' ";
	if($trans_id != ""){
		$condition .= " AND trans_id = ".$trans_id;
	}
	
	$objTransactionDetailMgmt = new TransactionDetailMgmt();
 	$result = $objTransactionDetailMgmt->getAllRecords(0,0,$condition,"trandetail_id","ASC");
	
	if(dbRows($result) > 0){
		$starttime = microtime(true);
		$file      = fsockopen ("sms4indian.com", 80, $errno, $errstr, 10);
		$stoptime  = microtime(true);
		$status    = 0;
		if(!$file) $status = -1;  // Site is down
		else{
			fclose($file);
			$clientsmsid = 0;
			
			
			$SQL_ID = "SELECT config_value FROM siteconfigmaster WHERE config_key = 'id' ";
			$SQL_PWD = "SELECT config_value FROM siteconfigmaster WHERE config_key = 'pwd' ";
			$SQL_API = "SELECT config_value FROM siteconfigmaster WHERE config_key = 'sendapi' ";
			
			$RESULT_ID = dbQuery($SQL_ID);
			$RECORD_ID = dbArray($RESULT_ID);
			
			$RESULT_PWD = dbQuery($SQL_PWD);
			$RECORD_PWD = dbArray($RESULT_PWD);
			
			$RESULT_API = dbQuery($SQL_API);
			$RECORD_API = dbArray($RESULT_API);
			
			$ID = $RECORD_ID['config_value'];
			$Pwd = $RECORD_PWD['config_value'];
			$remoteApp = $RECORD_API['config_value'];
			
			//$ID = "suratdeo";
			//$Pwd = "suratdeo1234";
		
			//$Text=$_REQUEST["msgpifix"]." ".$_REQUEST['msgbody']." ".$_REQUEST['msgsufix'];
			
			$Text=$msgpifix." ".$msgbody." ".$msgsufix;
			//$remoteApp = "http://vibgyor.aruhat.com/sendsms.jsp";
			
			while($record = dbArray($result)){
				
				$Mobno = $record['sms_mobileno'];
				$messageid=0;
				
				$requestString = "user=$ID&password=$Pwd&mobiles=$Mobno&sms=$Text&group=-1&clientsmsid=$clientsmsid";
				
	
				//Init the curl session 
				$ch = curl_init(); 
				
				// set the post-to url (do not include the ?query+string here!) 
				curl_setopt ($ch, CURLOPT_URL, $remoteApp);
				
				// Header control 
				curl_setopt ($ch, CURLOPT_HEADER, 0);
				
				//Tell it to make a POST, not a GET 
				curl_setopt ($ch, CURLOPT_POST, 1);
				
				// Put the query string here starting without "?"
				curl_setopt ($ch, CURLOPT_POSTFIELDS, $requestString);
				
				// This allows the output to be set into a variable
				curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
				
				// execute the curl session and return the output to a variable $response        
				$response = curl_exec($ch);
				
				// Close the curl session 
				curl_close ($ch); 
				
				$xml = simplexml_load_string($response);
	
				$arr2 = array();
					
				$arr2 = xml2phpArray($xml);          
																	
				$messageid = $arr2["messageid"];
			//	$messageid = 1234;
								
				$objTransactionDetailMgmt->trans_id			= $trans_id;
				$objTransactionDetailMgmt->trandetail_id	= $record['trandetail_id'];
				$objTransactionDetailMgmt->sms_apimessageid	= $messageid;
				$objTransactionDetailMgmt->sms_credit	   	= 1;
				if($messageid==-1 && $messageid!=0)
				{
					$objTransactionDetailMgmt->sms_status	= "fail";
				}
				else
				{
					$objTransactionDetailMgmt->sms_status	= "send";
				}
				$objTransactionDetailMgmt->edit();
				
			}
			echo '1';	
		}
	}
?>