Jun 22, 2010

Dhiraagu webSMS Conduit

I posted a simple bash script a long time ago to try and ease the process of sending webSMS's. While the age when everybody around here was messing around with the service is long gone, I have personally found the service to be quite useful (it's free after all!), and still occasionally mess around with it. The script works through and through and has served it's purpose well over the ages. The problem however, was that it still required the exchange of a (relatively) large amount of data between the client (me) and the server(Dhiraagu). While this is inconsequential on a regular broadband connection, it is a problem on my rather expensive mobile data connection.

Thus, out of this particular need, and after a brief spurt of late night coding (4:00 am - 4:15am?) the following PHP script was born. It is to live on my hosted server, and there act as a "Conduit" between myself and Dhiraagu. As you can see, it is, more or less, a "port" of the original Bash script to PHP...and I hope the PHP gods won't rain thunder and rocks on my head for the horrible job I did of it.

An unexpected upshot of this however, is that it allows me to use it over SSL which I suppose is always a good thing.

 

Anyways...here be the thing.

Enjoy.

 

 

<?php
/*....
PHP script to act as a go-between for Dhiraagu websms
to minimize data-exchange. Ideally for use over a
mobile network.

just upload the php file to some place that supports
php_curl and make a standard GET request to the file
with the follwing parameters:

user,pass,num,msg,<cookie>

where cookie is optional, and is the session string of
an existing session.

The Script returns a JSON encoded status message.
*/

$old_error_handler = set_error_handler("myErrorHandler");
$SessionData=array(
'status' => 'OK',
'count' => NULL,
'cookie' => NULL,
'retries' => 0,
);

function terminate_now() {
global $SessionData;
echo json_encode($SessionData)."\n";
exit(1);
}

function myErrorHandler($errno, $errstr, $errfile, $errline)
{

global $SessionData;
if (!(error_reporting() & $errno)) {
return;
}

switch ($errno) {
case E_USER_ERROR:
$SessionData['status']="ERR: ".$errstr;
terminate_now();
break;

case E_USER_WARNING:
$SessionData['status']="WARN: ".$errstr;
break;

default:
$SessionData['status']="ERRUNKWN: ".$errstr;
terminate_now();
break;
}

return true;
}

function make_curl_request($url,$post_params,$useCookie) {

global $SessionData;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_AUTOREFERER,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_params);
curl_setopt($ch,CURLOPT_COOKIESESSION,1);
if($useCookie) {
curl_setopt($ch,CURLOPT_COOKIE,'Dhi='.$SessionData['cookie']);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);
curl_close($ch);

return $output;

}

function login()
{
global $SessionData;
$username=$_GET['user'];
$password=$_GET['pass'];

$return=make_curl_request('http://websms.dhimobile.com.mv/cgi-bin/websms/index.pl',
'username='.urlencode($username).'&password='.urlencode($password),FALSE);

if(preg_match('/^Set-Cookie: Dhi=(.*?);/m', $return, $cookie_out)) {
$SessionData['cookie']=$cookie_out[1];

//CHECK DAILY QUOTA
if(preg_match('/send (.*?) more/',$return,$msg_count)) {
if(($SessionData['count']=$msg_count[1])<1){ trigger_error('You have Reached your Daily Quota',E_USER_ERROR); } else { sendsms(); } } return true; } else { trigger_error('Invalid Username andor Password',E_USER_ERROR); return false; } } function sendsms() { global $SessionData; $number=$_GET['num']; $message=$_GET['msg']; if(strlen($message) > 140) {
trigger_error('Message Truncated',E_USER_WARNING);
$message=substr($message,0,140);
}

$return=make_curl_request('http://websms.dhimobile.com.mv/cgi-bin/websms/send_message.pl',
'mobilenumber='.urlencode($number).'&message='.urlencode($message),TRUE);

//VALIDATE THE RETURN STUFF
if(preg_match('/send (.*?) more/',$return,$msg_count)) {
$SessionData['count']=$msg_count[1];
} else {
if(($SessionData['retries']++)>3){
trigger_error('maximum number of retries exceeded',E_USER_ERROR);
}
login();
}

}

if (isset($_GET['user']) && isset($_GET['pass']) && isset($_GET['msg']) && isset($_GET['num'])) {
if(preg_match("/^7[4-9][0-9]{5}$/",$_GET['num'],$number)){
$_GET['num']=$number[0];
} else {
trigger_error('Invalid Number Format',E_USER_ERROR);
}
} else {
trigger_error('Insufficiant Arguments',E_USER_ERROR);
}

if(isset($_GET['cookie'])){
$SessionData['cookie']=$_GET['cookie'];
sendsms();
} else {
login();
}

terminate_now();
?>

0 Comments:

Post a Comment