#native_company# #native_desc#
#native_cta#

2-way SMS/EMS/Ringtone/Logo script

By Marcel Manz
on June 22, 2003

Version: 1.0

Type: Full Script

Category: Networking

License: GNU General Public License

Description: This script allows you to send SMS (also long SMS), EMS, RTTTL-Ringtones, Logos (GIF, JPG, PNG) with automatic image resizing, EMS Pictures with copy protection and autosave feature, Nokia Operator Logos, Nokia Multipart messages and so on. Pictures and Ringtones can be HEX-included or specified to be downloaded from a HTTP-source. You can setup the 2-way service within minutes on TrueSenses.com. Requires NO additional libraries programs or databases.

<html>
<head>
<title>TrueSenses Response</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php

/*
************************************************************************************
* 2-way mobile messaging script, (c) 2003 TrueSenses.com - http://www.truesenses.com
*
* -> This script allows you to operate a 2-way SMS service and send ringtones, logos
* -> and much more directly from your site.
*
* -> This script does not need any other execution rights like executing sub-
* -> programs and should therefore run on almost every server supporting PHP.
*
* -> For outgoing SMS you will need to have a TrueSenses.com account.
* -> For incoming SMS you will need to activate the keyword service for as low as
* -> CHF 5.- (Swiss Francs) per keyword and month including unlimited incoming SMS.
*
* You will need to configure $truesenses_account,$truesenses_password and
* $truesenses_origin
*
* This script is pre-configured to act on incoming SMS messages sent by the
* TrueSenses.com SMS-Gateway. It will compare the incoming keyword and send back
* the requested content.
*
* If you ONLY want to use this script to send messages (without 2-way service),
* simply modify the MAIN program code to discard the incoming parameters provided
* by the gateway: $NUMBER (origin number, international format) and $MESSAGE
*
* The following message-modes are supported:
*
* - SMS
* - EMS
* - EMS Images
* - NOKIA Ringtones
* - NOKIA Logos
* - NOKIA Multipart messages (Picture Message with text)
*
*
* In order to install the 2-way SMS script follow these steps:
*
* 
* 1. Open an account on TrueSenses.com
* 2. Configure this script with your Account/Password and upload it to your server
* 3. Buy some outgoing SMS credits
* 4. Go to Configuration / Keyword services and add a keyword
* 4.1 Click on the keyword and activate PUSH to forward incoming SMS to this script
*     example: http://www.[yoursitename].com/truesenses_2way.php
*     method: select POST method - dont forget to click on "Activated"
* 4.2 Activate the keyword by clicking "Activate keyword"
* 5. Your service is now up and running
*
* Send the following SMS to +41-79 381 60 10 or the number given by TrueSenses.com
*
* yourkeyword SMS
* -> You will receive a SMS back to your phone.
*
* yourkeyword SMSFLASH
* -> You will receive a flashing class 0 SMS back to your phone.
*
* yourkeyword NOKIARINGTONE
* -> You will receive a ringtone for your NOKIA phone, downloaded from HTTP-Source.
*
* yourkeyword NOKIARINGTONEHEX
* -> You will receive a ringtone for your NOKIA phone, included as hex-encoded.
*
* yourkeyword NOKIALOGO
* -> You will receive an operator logo for your NOKIA phone, downloaded from HTTP-
* -> source. IMPORTANT: configure the mnc and mmc settings first according to the
* -> destination provider. Otherwise the logo will not show up.
*
* yourkeyword NOKIAMULTIPART
* -> You will receive a picture message with a text for your NOKIA phone, downloaded
* -> from a HTTP-source.
*
* yourkeyword EMSLOGO
* -> You will receive a picture message for your NON-Nokia phone like SonyEricsson
* -> and all other vendors who support the EMS-standard on their phones. The image
* -> is by default copy protected and cannot be sent to another phone. You can de-
* -> activate this by setting protected = "NO".
*
************************************************************************************
*/




$truesenses_account = "YourAccountName"; // TrueSenses.com Account-Name
$truesenses_password = "YourPassword"; // Account-Password
$truesenses_origin = "TrueSenses"; // Message-Origin, 11 alpha chars or numeric number
$debug = 0; // debug-output 0 = off, 1 = on


//
// open a http-channel, transmit data and return received buffer
function sendToHost($host,$method,$path,$data,$useragent,$poststr)
    {
	
	global $debug;
	
	if (empty($method)) { // default GET
		$method = 'GET';
	}
	$method = strtoupper($method);
	$fp = fsockopen($host, 80, $errno, $errstr, 30);
	if (!$fp) {
    	if ($debug == 1) {
			echo("Could not open connection<br>$errstr ($errno)<br>n");
		}
	} else {
	if ($method == 'GET')
		$path .= '?' . $data;
	fputs($fp, "$method $path HTTP/1.0rn");
	fputs($fp, "Host: $hostrn");
	fputs($fp, "Content-Type: application/x-www-form-urlencodedrn");
	if ($method == 'POST') {
		fputs($fp, "Content-Length: " . strlen($data) . "rn");
	} else {
		if (strlen($poststr)>0) { // if something to POST on GET method
			fputs($fp, "Content-Length: " . strlen($poststr) . "rn");
		}
	}
	if (strlen($useragent)>0) {
		fputs($fp, "User-Agent: $useragentrn");
	} else {
		fputs($fp, "User-Agent: TrueSensesPHPrn");
	}
	fputs($fp, "Connection: closern");
	fputs($fp, "rn");
	if ($method == 'POST') {
		fputs($fp, $data);
	} else { // GET method
		if (strlen($poststr)>0) { // if something to POST on GET method
			fputs($fp, $poststr);
		}
	}
	
	$buf = "";
	$line = "";
	$chr = "0";
	$contentsize = "0";
	// echo("reading responsen");
	while ((!feof($fp)) && (strlen($buf)<=100000) && ($chr < 500)) { // read max 100000 bytes or 500 lines
   		$chr = $chr + 1;		
		$line = fgets($fp, 10000); // read one line
		$buf .= $line; // append to buffer
	}
	
	// sometimes contentsize was not received ...had to replace with part below
	/*
	$contpos = strpos(strtoupper($buf), "CONTENT-LENGTH:");
	if ($contpos > 0) { // determine content size
		if ($debug == 1) {
			echo("decoding content-length<br>n");
		}
		$contentsize = substr($buf, ($contpos + 16), 20);
		while (strpos($contentsize, "n") > 0) {
			$contentsize = substr($contentsize, 0, strpos($contentsize, "n") -1);
		}
		while (strpos($contentsize, "r") > 0) {
			$contentsize = substr($contentsize, 0, strpos($contentsize, "r") -1);
		}
		if ($debug == 1) {
			echo("contentsize::$contentsize::<br>n");
		}	
	} else {
		if ($debug == 1) {
			echo("NO contentsize found in ::$buf::<br>n");
		}
		return "ERROR";
	}
	*/
	
	// replaced above part with this one
	$contpos = strpos(strtoupper($buf), "CONTENT-TYPE");
	if ($contpos > 0) {
		// echo("FOUND contenttype in ::$buf::<br>n");
		$buf = substr($buf, $contpos, strlen($buf) - $contpos);
		// echo("new buffer::$buf::<br>n");
		// $buf = substr($buf, strpos($buf, "n") + 1, strlen($buf) - strpos($buf, "n"));
		$buf = substr($buf, 27, strlen($buf) - (27 + 1)); // assumes Content-Type: text/html (27 chars)	
	
	} else {
		if ($debug == 1) {
			echo("NO contenttype found in ::$buf::<br>n");
		}
		return "ERROR";
	}
	
	// delete everything else than the content from the buffer
	// $buf = substr($buf, (strlen($buf) - $contentsize), $contentsize - 1); 
	
	// echo("buffer::$buf::<br>n");
	// echo("after responsen");
	
	fclose($fp);
	return $buf; // return the buffer
	}
} // sendToHost


//
// compose the XML-message ans transfer it to sendToHost
function sendXML($destination, $origin, $middle)
{
			global $truesenses_account;
			global $truesenses_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";
	
			$sendresp = sendToHost('www.truesenses.com','get','/cgi-bin/smsgateway.cgi', "FUNCTION=EXTERN_XMLUPLOAD&ACCOUNT=$truesenses_account&PASSWORD=$truesenses_password", "", "$poststring");

			return $sendresp; // return response

} // sendXML




// MAIN program
	
	// Look if incoming message contains a space (keyword detected)
	//
	// Incoming message will contain the full message (no keyword required) if you
	// are hosting a dedicated access number on TrueSenses
	//
	$pos = strpos ($MESSAGE, " "); 

	if ($pos === false) { // no space in message detected, keyword is message
		$KEYWORD = "$MESSAGE";
		$MSGONLY = "$MESSAGE";
	} else { // space detected, extract keyword
		$KEYWORD = substr($MESSAGE, 0, $pos);
		$MSGONLY = substr($MESSAGE, ($pos + 1), (strlen($MESSAGE) - ($pos)));
	}

	    if ($debug == 1) {
			echo("Keyword::$KEYWORD:: Msgonly::$MSGONLY:: Fullmessage::$MESSAGE::<br>n");
		}

		if (strcmp(strtoupper($MSGONLY), "SMS") == 0) {

			// sends a normal SMS
			
			$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("Your keyword was $KEYWORD and your message was $MSGONLY"); // hex-encode the content
			$xmlmsg .= "</CONTENT>n";
			$xmlmsg .= "</SMS>n";
			
			$rcbuf = sendXML($NUMBER, $truesenses_origin, $xmlmsg);
	
			if ($debug == 1) {
				echo("RESPONSE::$rcbuf::n");
			}
		} // SMS reply
		
		if (strcmp(strtoupper($MSGONLY), "SMSFLASH") == 0) {

			// sends a flasing SMS
			
			$xmlmsg = "<SMS encoded="YES">n";
			$xmlmsg .= "<flashing />n"; // set msgclass 0 flag
			// add the following line if you want to send long-SMS (spawning over multiple SMS)
			// $xmlmsg .= "<long />n";
			$xmlmsg .= "<CONTENT>";
			$xmlmsg .= urlencode("Your keyword was $KEYWORD and your message was $MSGONLY"); // hex-encode the content
			$xmlmsg .= "</CONTENT>n";
			$xmlmsg .= "</SMS>n";
			
			$rcbuf = sendXML($NUMBER, $truesenses_origin, $xmlmsg);
	
			if ($debug == 1) {
				echo("RESPONSE::$rcbuf::n");
			}
		} // SMS reply
		
		if (strcmp(strtoupper($MSGONLY), "NOKIARINGTONE") == 0) {
		
			// sends a nokiaringtone - input must be in RTTTL format
			//
			
			// assemble XML message
			
			$http_location = "http://www.simmcomm.ch/ringtone.txt"; // location of the ringtone
			
			$xmlmsg = "<NOKIARINGTONE>n";
			$xmlmsg .= "<RINGTONE source="HTTP" type="RTTTL">$http_location</RINGTONE>n";
			$xmlmsg .= "</NOKIARINGTONE>n";
			
			$rcbuf = sendXML($NUMBER, $truesenses_origin, $xmlmsg);
			
			if ($debug == 1) {
				echo("RESPONSE::$rcbuf::n");
			}
		} // NOKIARINGTONE
		
		if (strcmp(strtoupper($MSGONLY), "NOKIARINGTONEHEX") == 0) {
		
			// sends a nokiaringtone as hex include - input must be in RTTTL format
			//
			
			// assemble XML message
			
			$http_location = urlencode("KnightRider:d=4,o=5,b=125:16e,16p,16f,16e,16e,16p,16e,16e,16f,16e,16e,16e,16d#,16e,16e,16e,16e,16p,16f,16e,16e,16p,16f,16e,16f,16e,16e,16e,16d#,16e,16e,16e,16d,16p,16e,16d,16d,16p,16e,16d,16e,16d,16d,16d,16c,16d,16d,16d,16d,16p,16e,16d,16d,16p,16e,16d,16e,16d,16d,16d,16c,16d,16d,16d"); // free demo ringtone from the internet
			
			$xmlmsg = "<NOKIARINGTONE>n";
			$xmlmsg .= "<RINGTONE source="BINARY" type="RTTTL">$http_location</RINGTONE>n";
			$xmlmsg .= "</NOKIARINGTONE>n";
			
			$rcbuf = sendXML($NUMBER, $truesenses_origin, $xmlmsg);
			
			if ($debug == 1) {
				echo("RESPONSE::$rcbuf::n");
			}
		} // NOKIARINGTONEHEX
		
		if (strcmp(strtoupper($MSGONLY), "NOKIALOGO") == 0) {
		
			// sends a nokialogo - input must be in GIF, JPG or PNG
			//
			// parameters:
			// mcc = MobileCountryCode for destination (example 228 - switzerland)
			// mnc = MobileNetworkCode for destination (example 01 - swisscom, 02 sunrise, 03 orange)
		
			// assemble XML message
			
			$http_type = "GIF";
			$http_location = "http://www.simmcomm.ch/nokialogo.gif"; // location of the logo
			
			$destination_mcc = "228"; // mandatory: MobileCountryCode for destination
			$destination_mnc = "01"; // mandatory: MobileNetworkCode for destination
			
			
			$xmlmsg = "<NOKIALOGO mcc="$destination_mcc" mnc="$destination_mnc">n";
			$xmlmsg .= "<IMAGE source="HTTP" type="$http_type">$http_location</IMAGE>n";
			$xmlmsg .= "</NOKIALOGO>n";
			
			$rcbuf = sendXML($NUMBER, $truesenses_origin, $xmlmsg);
			
			if ($debug == 1) {
				echo("RESPONSE::$rcbuf::n");
			}
		} // NOKIALOGO
		
		if (strcmp(strtoupper($MSGONLY), "NOKIAMULTIPART") == 0) {
		
			// sends a nokiamultipart message (picturemessage)  - input must be in GIF, JPG or PNG
			//
			// options:
			// - specify <CONTENT> tag if you want to send some text with your image
		
			// assemble XML message
			
			$http_type = "GIF";
			$http_location = "http://www.simmcomm.ch/nokialogo.gif"; // location of the logo
			
			
			$xmlmsg = "<NOKIAMULTIPART encoded="YES">n";
			$xmlmsg .= "<IMAGE source="HTTP" type="$http_type">$http_location</IMAGE>n";
			$xmlmsg .= "<CONTENT>";
			$xmlmsg .= urlencode("Your logo"); // hex-encode the content
			$xmlmsg .="</CONTENT>n";
			$xmlmsg .= "</NOKIAMULTIPART>n";
			
			$rcbuf = sendXML($NUMBER, $truesenses_origin, $xmlmsg);
			
			if ($debug == 1) {
				echo("RESPONSE::$rcbuf::n");
			}
		} // NOKIAMULTIPART
		
		if (strcmp(strtoupper($MSGONLY), "EMSLOGO") == 0) {
		
			// sends a logo to NON-nokia, EMS-capable phones like SonyEricsson
			//
			// parameters:
			// autosave = YES/NO (automatically ask user to save image)
			// protected = YES/NO (disallow re-distribution of image)
			// width = x (width of image, do not provide parameter for autosizing)
			// height = y (height of image, do not provide parameter for autosizing)
		
			// assemble XML message
			
			$http_type = "GIF";
			$http_location = "http://www.simmcomm.ch/nokialogo.gif"; // location of the logo
			
			$xmlmsg = "<EMS>n";
			// no newline after <content> tag as it would convert to text-enter %0A in ems and would consume space
			$xmlmsg .= "<CONTENT><IMAGE source="HTTP" type="$http_type" autosave="YES" protected="NO">$http_location</IMAGE></CONTENT>n"; 
			$xmlmsg .= "</EMS>n";
			
			$rcbuf = sendXML($NUMBER, $truesenses_origin, $xmlmsg);
			
			if ($debug == 1) {
				echo("RESPONSE::$rcbuf::n");
			}
		} // EMSLOGO
	
	// send confirmation to TrueSenses.com that we have received the incoming sms

	echo("OK<br>n");
?>
</body>
</html>