<?php

/*
************************************************************************************
* XML Transmission script, (c) 2003 TrueSenses.com - http://www.truesenses.com
*
* -> This script allows you to test your bulk SMS account. It requires
* -> CURL to be installed.
*
************************************************************************************
*/


$truesenses_account = "YourAccount"; // TrueSenses.com Account-Name
$truesenses_password = "YourPassword"; // Account-Password
$truesenses_origin = "SenderID"; // either numeric or alphanumeric (max 11 characters on alphanumeric)


// open a http-channel, transmit data and return received buffer
function sendToHost($host,$method,$poststr)
    {
	
	if (empty($method)) { // default GET
		$method = 'GET';
	}
	
	$method = strtoupper($method);
	
	if (strcmp($method, "GET") == 0) {
	
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $host);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, "TrueSenses.com Gateway/1.01");
		curl_setopt($ch, CURLOPT_HEADER, 1); // include the header in the output
		curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s 
		
		// check for something to POST on GET
		if (strlen($poststr) > 0) {
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $poststr);
		}
		
		$store = curl_exec ($ch);
	
	} else {

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $host);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $poststr);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, "TrueSenses.com Gateway/1.01");
		curl_setopt($ch, CURLOPT_HEADER, 1); // include the header in the output
		curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s 
		$store = curl_exec ($ch);
		
	}
	
	if (curl_errno($ch)) {
		$result = sprintf("TrueSenses-Status: ERROR\r\n%s\n%s\r\n", curl_errno($ch), curl_error($ch));
    } else {
    	curl_close($ch);

		$result = sprintf("TrueSenses-Status: OK\r\n%s\r\n", $store);
    }
	
	return $result; // return the buffer
	
} // sendToHost


//
// compose the XML-message ans transfer it to sendToHost
function sendXML($destination, $origin, $middle, $account, $password)
{
			
			$poststring = "<?xml version=\"1.0\"?>\n";
			$poststring .= "<Message>\n";
			
			$poststring .= "<Destination>\n";
			$poststring .= "<GSM number=\"$destination\" />\n";
			$poststring .= "</Destination>\n";
			
			$poststring .= "<ORIGINATOR value=\"$origin\" />\n";
			
			$poststring .= $middle; // insert middle part here
			
			$poststring .= "</Message>\n";

			$url = sprintf("http://www.truesenses.com/cgi-bin/smsgateway.cgi?FUNCTION=EXTERN_XMLUPLOAD&ACCOUNT=%s&PASSWORD=%s", $account, $password);
	
			$sendresp = sendToHost("$url", 'get', "$poststring");

			return $sendresp; // return response

} // sendXML



function result_status($xml)
{

// $xml input examples
//
// <Result destination="+41791234567" number="0041791234567" mode="SMS" type="TEST" creditspersms="1" creditstotal="4">000000000000,000000000000,000000000000,000000000000</Result>
// <Result destination="+4179" number="004179" mode="SMS" type="ERROR" creditspersms="1" creditstotal="0">Invalid destination number</Result>	
	
	
	$ownerpos = strpos($xml, "<Result");
	$nextpos = strpos($xml, "</Result>");
	$tres = substr($xml, $ownerpos + 8, $nextpos - $ownerpos - 7); // extract content between <Result> </Result>
	
	// process it
	
	$returnstring = "";
	
	if (strpos($tres, "type=\"ERROR\"") > 0) {
		// error appeared
		
		$tres1 = substr($tres, strpos($tres, ">") + 1 , 250);
		$returnstring = sprintf("ERROR: %s", substr($tres1, 0, strpos($tres1, "<")));
		
	} else {
		// sent OK
		
		$tres1 = substr($tres, strpos($tres, ">") + 1 , 250);
		$returnstring = substr($tres1, 0, strpos($tres1, "<")); 
	}
	
	return $returnstring;
	
} // result_status






// MAIN program
	
	

		// sms text
		$message = sprintf("This is the SMS text which will be sent. If the <long /> parameter is provided, it can be longer than 160 characters.");
		
		// recipient
		$rcpt = sprintf("+4179123456");

		$xmlmsg = "<SMS encoded=\"YES\">\n";
		// add the following line if you want to send long-SMS (spawning over multiple SMS)
		$xmlmsg .= "<long />\n";
		$xmlmsg .= "<CONTENT>";
		$xmlmsg .= urlencode($message); // hex-encode the content
		$xmlmsg .= "</CONTENT>\n";
		$xmlmsg .= "</SMS>\n";
		
		
		$rcbuf = sendXML($rcpt, $truesenses_origin, $xmlmsg, $truesenses_account, $truesenses_password);


		$out = sprintf("Message-Status: %s\n\n", result_status($rcbuf));
		echo $out;


?>