| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* CLICKATELL SMS API
|
| 4 |
*
|
| 5 |
* This class is meant to send SMS messages via the Clickatell gateway
|
| 6 |
* and provides support to authenticate to this service and also query
|
| 7 |
* for the current account balance. This class use the fopen or CURL module
|
| 8 |
* to communicate with the gateway via HTTP/S.
|
| 9 |
*
|
| 10 |
* For more information about CLICKATELL service visit http://www.clickatell.com
|
| 11 |
*
|
| 12 |
* version 1.3d
|
| 13 |
* package gateway_clickatell_http
|
| 14 |
* @author Aleksandar Markovic <mikikg@gmail.com>, David Hamilton
|
| 15 |
* @copyright Copyright © 2004, 2005 Aleksandar Markovic
|
| 16 |
* @link http://sourceforge.net/projects/sms-api/ SMS-API Sourceforge project page
|
| 17 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
| 18 |
*
|
| 19 |
*/
|
| 20 |
|
| 21 |
/** returns gateway name
|
| 22 |
* @return the human-readable name of the provider plugin (service provider and protocol), for use in selection user interface
|
| 23 |
*/
|
| 24 |
function smsgateway_gateway_clickatell_http_name() {
|
| 25 |
return t('Clickatell HTTP/S');
|
| 26 |
}
|
| 27 |
|
| 28 |
/** returns gateway description text
|
| 29 |
* @return The human-readable decription of the module, containing more information about where to go to get information about
|
| 30 |
* the service provider/protocol. May contain HTML formatting.
|
| 31 |
*/
|
| 32 |
function smsgateway_gateway_clickatell_http_description() {
|
| 33 |
return t('Clickatell HTTP/S Gateway. For details and account information visit <a href=\'http://www.clickatell.com/\'>http://www.clickatell.com/</a>');
|
| 34 |
}
|
| 35 |
|
| 36 |
/** sends a single message to the gateway provider.
|
| 37 |
* @param smssettings array of settings containing the account information to allow message transmission. Fields are
|
| 38 |
'serverurl' The URL used to connect to the server.
|
| 39 |
'account_id' The identifier of the account to use
|
| 40 |
'accountname' The account or username used to login
|
| 41 |
'password' The password to use to login
|
| 42 |
'from' The telephone number to be used for the sender
|
| 43 |
'usessl' 1 to indicate the use of an SSL connection
|
| 44 |
@param message array of message specific variables
|
| 45 |
'destination_number' the destination telephone number to send the message to
|
| 46 |
'message_body' the text body of the message
|
| 47 |
@return an array of response variables.
|
| 48 |
'response' the vendor specific text response as sent by the provider gateway server
|
| 49 |
'connectstring' a debug string indicating the string used to connect to the server (if supported)
|
| 50 |
'initialbalance' the balance at the start of the send (if supported)
|
| 51 |
*/
|
| 52 |
function smsgateway_gateway_clickatell_http_sendsingle( $smssettings, $message) {
|
| 53 |
$destination_number = trim($message['destination_number']);
|
| 54 |
$message_body = trim($message['message_body']);
|
| 55 |
$result = array();
|
| 56 |
$smsapi = new clickatell($smssettings['account_id'], $smssettings['accountname'], $smssettings['password'], $smssettings['usessl']);
|
| 57 |
$result['connectstring'] = $smsapi->build_connection_string();
|
| 58 |
$result['initialbalance'] = $smsapi->getbalance();
|
| 59 |
$result['response'] = $smsapi->send($destination_number, $smssettings['from'], $message_body);
|
| 60 |
return $result;
|
| 61 |
}
|
| 62 |
|
| 63 |
/** sends a batch of messages to the gateway provider.
|
| 64 |
* @param smssettings array of settings containing the account information to allow message transmission. Fields are
|
| 65 |
'serverurl' The URL used to connect to the server.
|
| 66 |
'account_id' The identifier of the account to use
|
| 67 |
'accountname' The account or username used to login
|
| 68 |
'password' The password to use to login
|
| 69 |
'from' The telephone number to be used for the sender
|
| 70 |
'usessl' 1 to indicate the use of an SSL connection
|
| 71 |
@param sms_messages an array of messages keyed by a unique ID for the message. This is used to track the responses for the
|
| 72 |
individual messages. Each individual message is itself an array of message specific variables:
|
| 73 |
'destination_number' the destination telephone number to send the message to
|
| 74 |
'message_body' the text body of the message
|
| 75 |
@return an array of response variables.
|
| 76 |
'connectstring' a debug string indicating the string used to connect to the server (if supported)
|
| 77 |
'initialbalance' the balance at the start of the send (if supported)
|
| 78 |
Id-specific - the Unique ID passed in with each message is used to track the vendor specific text response as
|
| 79 |
sent by the provider gateway server
|
| 80 |
*/
|
| 81 |
function smsgateway_gateway_clickatell_http_sendbatch($smssettings, $sms_messages) {
|
| 82 |
$smsapi = new clickatell($smssettings['account_id'], $smssettings['accountname'], $smssettings['password'], $smssettings['usessl']);
|
| 83 |
$result['connectstring'] = $smsapi->build_connection_string();
|
| 84 |
$result['initialbalance'] = $smsapi->getbalance();
|
| 85 |
foreach( $sms_messages as $msgid => $message) {
|
| 86 |
$destination_number = trim($message['destination_number']);
|
| 87 |
$message_body = trim($message['message_body']);
|
| 88 |
if ($destination_number == null || strlen($destination_number) == 0) {
|
| 89 |
$result[ $msgid] = 'No telephone number specified';
|
| 90 |
}
|
| 91 |
else {
|
| 92 |
$result[ $msgid] = $smsapi->send( $destination_number, $smssettings['from'], $message_body);
|
| 93 |
}
|
| 94 |
}
|
| 95 |
return $result;
|
| 96 |
}
|
| 97 |
|
| 98 |
/** parses a received message. Ensures that all received parameters are HTML displayable.
|
| 99 |
* @return the values from the message parsed into an array of variables keyed as follows:
|
| 100 |
'account_id' the ID of the account that the message was received on
|
| 101 |
'sender_number' the telephone number of the message sender
|
| 102 |
'destination_number' the telephone number that the message was sent to
|
| 103 |
'timestamp' the timestamp placed on the message
|
| 104 |
'charset' optional character set information for the message body
|
| 105 |
'header' any additional header information
|
| 106 |
'body' the body of the message - the message text
|
| 107 |
*/
|
| 108 |
function smsgateway_gateway_clickatell_http_parsereceived() {
|
| 109 |
$result = array();
|
| 110 |
$result['account_id'] = check_plain($_GET['api_id']);
|
| 111 |
$result['sender_number'] = check_plain($_GET['from']);
|
| 112 |
$result['destination_number'] = check_plain($_GET['to']);
|
| 113 |
$result['timestamp'] = check_plain($_GET['timestamp']);
|
| 114 |
$result['charset'] = check_plain($_GET['charset']);
|
| 115 |
$result['header'] = check_plain($_GET['udh']);
|
| 116 |
$result['body'] = check_plain($_GET['text']);
|
| 117 |
|
| 118 |
return $result;
|
| 119 |
}
|
| 120 |
|
| 121 |
/**
|
| 122 |
* Main SMS-API class
|
| 123 |
*
|
| 124 |
* Example:
|
| 125 |
* <code>
|
| 126 |
* <?php
|
| 127 |
* require_once ("sms_api.php");
|
| 128 |
* $mysms = new sms();
|
| 129 |
* echo $mysms->session;
|
| 130 |
* echo $mysms->getbalance();
|
| 131 |
* $mysms->send ("38160123", "netsector", "TEST MESSAGE");
|
| 132 |
* ?>
|
| 133 |
* </code>
|
| 134 |
* @package clickatell_api
|
| 135 |
*/
|
| 136 |
|
| 137 |
class clickatell {
|
| 138 |
|
| 139 |
/**
|
| 140 |
* Clickatell API-ID
|
| 141 |
* @link http://sourceforge.net/forum/forum.php?thread_id=1005106&forum_id=344522 How to get CLICKATELL API ID?
|
| 142 |
* @var integer
|
| 143 |
*/
|
| 144 |
var $api_id = "test";
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Clickatell username
|
| 148 |
* @var mixed
|
| 149 |
*/
|
| 150 |
var $user = "test";
|
| 151 |
|
| 152 |
/**
|
| 153 |
* Clickatell password
|
| 154 |
* @var mixed
|
| 155 |
*/
|
| 156 |
var $password = "test";
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Use SSL (HTTPS) protocol
|
| 160 |
* @var bool
|
| 161 |
*/
|
| 162 |
var $use_ssl = false;
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Define SMS balance limit below class will not work
|
| 166 |
* @var integer
|
| 167 |
*/
|
| 168 |
var $balace_limit = 0;
|
| 169 |
|
| 170 |
/**
|
| 171 |
* Gateway command sending method (curl,fopen)
|
| 172 |
* @var mixed
|
| 173 |
*/
|
| 174 |
var $sending_method = "fopen";
|
| 175 |
|
| 176 |
/**
|
| 177 |
* Optional CURL Proxy
|
| 178 |
* @var bool
|
| 179 |
*/
|
| 180 |
var $curl_use_proxy = false;
|
| 181 |
|
| 182 |
/**
|
| 183 |
* Proxy URL and PORT
|
| 184 |
* @var mixed
|
| 185 |
*/
|
| 186 |
var $curl_proxy = "http://127.0.0.1:8080";
|
| 187 |
|
| 188 |
/**
|
| 189 |
* Proxy username and password
|
| 190 |
* @var mixed
|
| 191 |
*/
|
| 192 |
var $curl_proxyuserpwd = "login:secretpass";
|
| 193 |
|
| 194 |
/**
|
| 195 |
* Callback
|
| 196 |
* 0 - Off
|
| 197 |
* 1 - Returns only intermediate statuses
|
| 198 |
* 2 - Returns only final statuses
|
| 199 |
* 3 - Returns both intermediate and final statuses
|
| 200 |
* @var integer
|
| 201 |
*/
|
| 202 |
var $callback = 0;
|
| 203 |
|
| 204 |
/**
|
| 205 |
* Session variable
|
| 206 |
* @var mixed
|
| 207 |
*/
|
| 208 |
var $session;
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Class constructor
|
| 212 |
* Create SMS object and authenticate SMS gateway
|
| 213 |
* @param new_api_id API ID value
|
| 214 |
* @param new_user user/account name
|
| 215 |
* @param new_password password to use
|
| 216 |
* @param new_use_ssl whether to use SSL
|
| 217 |
* @return object New SMS object.
|
| 218 |
* @access public
|
| 219 |
*/
|
| 220 |
function clickatell ($new_api_id, $new_user, $new_password, $new_use_ssl = false) {
|
| 221 |
$this->api_id = $new_api_id;
|
| 222 |
$this->user = $new_user;
|
| 223 |
$this->password = $new_password;
|
| 224 |
$this->use_ssl = $new_use_ssl;
|
| 225 |
if ($this->use_ssl) {
|
| 226 |
$this->base = "http://api.clickatell.com/http";
|
| 227 |
$this->base_s = "https://api.clickatell.com/http";
|
| 228 |
}
|
| 229 |
else {
|
| 230 |
$this->base = "http://api.clickatell.com/http";
|
| 231 |
$this->base_s = $this->base;
|
| 232 |
}
|
| 233 |
|
| 234 |
$this->_auth();
|
| 235 |
}
|
| 236 |
|
| 237 |
/**
|
| 238 |
* Return gateway connection string
|
| 239 |
* @return The gateway connection string
|
| 240 |
* @access public
|
| 241 |
*/
|
| 242 |
function build_connection_string() {
|
| 243 |
return sprintf ("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s, $this->api_id, $this->user, $this->password);
|
| 244 |
}
|
| 245 |
|
| 246 |
|
| 247 |
/**
|
| 248 |
* Set sending vars
|
| 249 |
|
| 250 |
* @return mixed "OK" or exception describing error
|
| 251 |
* @access private
|
| 252 |
*/
|
| 253 |
function _auth() {
|
| 254 |
$this->session = $this->_parse_auth ($this->_execgw( $this->build_connection_string()));
|
| 255 |
}
|
| 256 |
|
| 257 |
/**
|
| 258 |
* Query SMS credis balance
|
| 259 |
* @return integer number of SMS credits
|
| 260 |
* @access public
|
| 261 |
*/
|
| 262 |
function getbalance() {
|
| 263 |
$comm = sprintf ("%s/getbalance?session_id=%s", $this->base, $this->session);
|
| 264 |
return $this->_parse_getbalance ($this->_execgw($comm));
|
| 265 |
}
|
| 266 |
|
| 267 |
/**
|
| 268 |
* Send SMS message
|
| 269 |
* @param to mixed The destination address.
|
| 270 |
* @param from mixed The source/sender address
|
| 271 |
* @param text mixed The text content of the message
|
| 272 |
* @return mixed "OK" or exception describing error
|
| 273 |
* @access public
|
| 274 |
*/
|
| 275 |
function send($to=null, $from=null, $text=null) {
|
| 276 |
|
| 277 |
/* Check SMS credits balance */
|
| 278 |
if ($this->getbalance() < $this->balace_limit) {
|
| 279 |
return ("Failed: Insufficient Credit with gateway.");
|
| 280 |
};
|
| 281 |
|
| 282 |
/* Check SMS $text length */
|
| 283 |
if (drupal_strlen ($text) > 465) {
|
| 284 |
return ("Failed: The message is to long, current length=".drupal_strlen ($text)." chars.");
|
| 285 |
}
|
| 286 |
|
| 287 |
/* Does message need to be concatenate */
|
| 288 |
if (drupal_strlen ($text) > 160) {
|
| 289 |
$concat = "&concat=3";
|
| 290 |
}
|
| 291 |
else {
|
| 292 |
$concat = "";
|
| 293 |
}
|
| 294 |
|
| 295 |
/* Check $to and $from is not empty */
|
| 296 |
if (empty ($to)) {
|
| 297 |
return ("Failed: No destination address specified.");
|
| 298 |
}
|
| 299 |
if (empty ($from)) {
|
| 300 |
return ("Failed: No from address specified.");
|
| 301 |
}
|
| 302 |
|
| 303 |
/* Reformat $to number */
|
| 304 |
$cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
|
| 305 |
$to = str_replace($cleanup_chr, "", $to);
|
| 306 |
|
| 307 |
/* Send SMS now */
|
| 308 |
$comm = sprintf ("%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&callback=%s%s",
|
| 309 |
$this->base,
|
| 310 |
$this->session,
|
| 311 |
rawurlencode($to),
|
| 312 |
rawurlencode($from),
|
| 313 |
rawurlencode($text),
|
| 314 |
$this->callback,
|
| 315 |
$concat
|
| 316 |
);
|
| 317 |
return $this->_parse_send ($this->_execgw($comm));
|
| 318 |
}
|
| 319 |
|
| 320 |
/**
|
| 321 |
* Execute gateway commands
|
| 322 |
* @access private
|
| 323 |
*/
|
| 324 |
function _execgw($command) {
|
| 325 |
if ($this->sending_method == "curl")
|
| 326 |
return $this->_curl($command);
|
| 327 |
if ($this->sending_method == "fopen")
|
| 328 |
return $this->_fopen($command);
|
| 329 |
return ("Failed: Unsupported sending method selected");
|
| 330 |
}
|
| 331 |
|
| 332 |
/**
|
| 333 |
* CURL sending method
|
| 334 |
* @access private
|
| 335 |
*/
|
| 336 |
function _curl($command) {
|
| 337 |
$this->_chk_curl();
|
| 338 |
$ch = curl_init ($command);
|
| 339 |
curl_setopt ($ch, CURLOPT_HEADER, 0);
|
| 340 |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1);
|
| 341 |
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);
|
| 342 |
if ($this->curl_use_proxy) {
|
| 343 |
curl_setopt ($ch, CURLOPT_PROXY, $this->curl_proxy);
|
| 344 |
curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $this->curl_proxyuserpwd);
|
| 345 |
}
|
| 346 |
$result=curl_exec ($ch);
|
| 347 |
curl_close ($ch);
|
| 348 |
return $result;
|
| 349 |
}
|
| 350 |
|
| 351 |
/**
|
| 352 |
* fopen sending method
|
| 353 |
* @access private
|
| 354 |
*/
|
| 355 |
function _fopen($command) {
|
| 356 |
$result = '';
|
| 357 |
$handler = @fopen ($command, 'r');
|
| 358 |
if ($handler) {
|
| 359 |
while ($line = @fgets($handler,1024)) {
|
| 360 |
$result .= $line;
|
| 361 |
}
|
| 362 |
fclose ($handler);
|
| 363 |
return $result;
|
| 364 |
}
|
| 365 |
else {
|
| 366 |
return ("Failed: Unable to open remote connection. Check that PHP version greater than 4.3.0 and OpenSSL support installed.");
|
| 367 |
}
|
| 368 |
}
|
| 369 |
|
| 370 |
/**
|
| 371 |
* Parse authentication command response text
|
| 372 |
* @access private
|
| 373 |
*/
|
| 374 |
function _parse_auth ($result) {
|
| 375 |
$session = drupal_substr($result, 4);
|
| 376 |
$code = drupal_substr($result, 0, 2);
|
| 377 |
if ($code!="OK") {
|
| 378 |
return ("Failed: Unable to get gateway authorization! Code: ".$result);
|
| 379 |
}
|
| 380 |
return $session;
|
| 381 |
}
|
| 382 |
|
| 383 |
/**
|
| 384 |
* Parse send command response text
|
| 385 |
* @access private
|
| 386 |
*/
|
| 387 |
function _parse_send ($result) {
|
| 388 |
$code = drupal_substr($result, 0, 2);
|
| 389 |
if ($code!="ID") {
|
| 390 |
return ("Failed: SMS Gateway returned error: Code: ".$result);
|
| 391 |
}
|
| 392 |
else {
|
| 393 |
return "OK: ".$result;
|
| 394 |
}
|
| 395 |
return $result;
|
| 396 |
}
|
| 397 |
|
| 398 |
/**
|
| 399 |
* Parse getbalance command response text
|
| 400 |
* @access private
|
| 401 |
*/
|
| 402 |
function _parse_getbalance ($result) {
|
| 403 |
$result = drupal_substr($result, 8);
|
| 404 |
return (int)$result;
|
| 405 |
}
|
| 406 |
|
| 407 |
/**
|
| 408 |
* Check for CURL PHP module
|
| 409 |
* @access private
|
| 410 |
*/
|
| 411 |
function _chk_curl() {
|
| 412 |
if (!extension_loaded('curl')) {
|
| 413 |
return ("Failed: PHP CURL module not installed.");
|
| 414 |
}
|
| 415 |
}
|
| 416 |
}
|
| 417 |
|
| 418 |
|
| 419 |
?>
|