1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
|
<?php
/**
* Net_SMS_sms2email_http Class implements the HTTP API for accessing
* the sms2email (www.sms2email.com) SMS gateway.
*
* Copyright 2003-2005 Marko Djukic <marko@oblo.com>
*
* See the enclosed file COPYING for license information (LGPL). If you did not
* receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* $Horde: framework/Net_SMS/SMS/sms2email_http.php,v 1.23.10.1 2005/01/03 12:19:09 jan Exp $
*
* @author Marko Djukic <marko@oblo.com>
* @version $Revision: 1.23.10.1 $
* @package Net_SMS
*/
class Net_SMS_sms2email_http extends Net_SMS {
var $_base_url = 'horde.sms2email.com/horde/';
/**
* An array of capabilities, so that the driver can report which operations
* it supports and which it doesn't. Possible values are:
* auth - The gateway requires authentication before sending;
* batch - Batch sending is supported;
* multi - Sending of messages to multiple recipients is supported;
* receive - Whether this driver is capable of receiving SMS;
* credit - Is use of the gateway based on credits;
* addressbook - Are gateway addressbooks supported;
* lists - Gateway support for distribution lists.
*
* @var array $capabilities
*/
var $capabilities = array('auth' => false,
'batch' => 100,
'multi' => true,
'receive' => false,
'credit' => true,
'addressbook' => true,
'lists' => true);
/**
* This function does the actual sending of the message.
*
* @access private
*
* @param array $message The array containing the message and its send
* parameters.
* @param string $to The destination string.
*
* @return mixed True on success or PEAR Error on failure.
*/
function _send(&$message, $to)
{
/* Set up the sending url. */
$url = sprintf('postmsg.php?username=%s&password=%s&message=%s',
urlencode($this->_params['user']),
urlencode($this->_params['password']),
urlencode($message['text']));
/* Check if source from is set. */
if (!empty($message['send_params']['from'])) {
$url .= '&orig=' . urlencode($message['send_params']['from']);
}
/* Check if message type is flash. */
if ($message['send_params']['msg_type'] == 'SMS_FLASH') {
$url .= '&flash=1';
}
/* Check if delivery report url has been set. */
if (!empty($this->_params['delivery_report'])) {
$url .= '&dlurl=' . urlencode($this->_params['delivery_report']) .
'reportcode=%code&destinationnumber=%dest';
}
/* Loop through recipients and do some minimal validity checking. */
if (is_array($to)) {
foreach ($to as $key => $val) {
if (preg_match('/^.*?<?\+?(\d{7,})(>|$)/', $val, $matches)) {
$to[$key] = $matches[1];
} else {
/* FIXME: Silently drop bad recipients. This should be
* logged and/or reported. */
unset($to[$key]);
}
}
$to = implode(',', $to);
} else {
if (preg_match('/^.*?<?\+?(\d{7,})(>|$)/', $to, $matches)) {
$to = $matches[1];
} else {
return PEAR::raiseError(sprintf(_("Invalid recipient: '%s'"), $to));
}
}
/* Append the recipients of this message and call the url. */
$url .= '&to_num=' . $to;
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Parse the response, check for new lines in case of multiple
* recipients. */
$lines = explode("\n", $response);
$response = array();
if (count($lines) > 1) {
/* Multiple recipients. */
foreach ($lines as $line) {
$parts = explode('To:', $line);
$recipient = trim($parts[1]);
if ($lines[0] == 'AQSMS-OK') {
$response[$recipient] = array(1, null);
} else {
$response[$recipient] = array(0, $lines[0]);
}
}
} else {
/* Single recipient. */
if ($lines[0] == 'AQSMS-OK') {
$response[$to] = array(1, null);
} else {
$response[$to] = array(0, $lines[0]);
}
}
return $response;
}
/**
* Returns the current credit balance on the gateway.
*
* @access private
*
* @return int The credit balance available on the gateway.
*/
function _getBalance()
{
/* Set up the url and call it. */
$url = sprintf('postmsg.php?username=%s&password=%s&cmd=credit',
urlencode($this->_params['user']),
urlencode($this->_params['password']));
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Try splitting up the response. */
$lines = explode('=', $response);
if ($lines[0] == 'AQSMS-CREDIT') {
return $lines[1];
} else {
return $this->getError($lines[0], _("Could not check balance. %s"));
}
}
/**
* Adds a contact to the gateway's addressbook.
*
* @access public
*
* @param string $name The name for this contact
* @param int $number The contact's phone number.
*
* @return mixed The remote contact ID on success or PEAR Error on
* failure.
*/
function addContact($name, $number)
{
/* Set up the url and call it. */
$url = sprintf('postcontacts.php?username=%s&password=%s&cmd=ADDCONTACT&name=%s&number=%s',
urlencode($this->_params['user']),
urlencode($this->_params['password']),
urlencode($name),
$number);
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Check if there was an error response. */
if (substr($response, 0, 17) != 'AQSMS-CONTACTIDOK') {
return $this->getError($response, _("Could not add contact. %s"));
}
/* Split up the response. */
$lines = explode(',=', $response);
return $lines[1];
}
/**
* Updates a contact in the gateway's addressbook.
*
* @access public
*
* @param int $id The contact's ID on the gateway.
* @param string $name The name for this contact
* @param int $number The contact's phone number.
*
* @return mixed True on success or PEAR Error on failure.
*/
function updateContact($id, $name, $number)
{
/* Set up the url and call it. */
$url = sprintf('postcontacts.php?username=%s&password=%s&cmd=UPDATECONTACT&id=%s&name=%s&number=%s',
urlencode($this->_params['user']),
urlencode($this->_params['password']),
$id,
urlencode($name),
$number);
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Parse the response. */
if ($response == 'AQSMS-OK') {
return true;
} else {
return $this->getError($response, _("Could not update contact. %s"));
}
}
/**
* Deletes a contact in the gateway's addressbook.
*
* @access public
*
* @param int $id The contact's ID on the gateway.
*
* @return mixed True on success or PEAR Error on failure.
*/
function deleteContact($id)
{
/* Set up the url and call it. */
$url = sprintf('postcontacts.php?username=%s&password=%s&cmd=DELETECONTACT&id=%s',
urlencode($this->_params['user']),
urlencode($this->_params['password']),
$id);
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Parse the response. */
if ($response == 'AQSMS-OK') {
return true;
} else {
return $this->getError($response, _("Could not delete contact. %s"));
}
}
/**
* Fetches the entire address book from the gateway.
*
* @access public
*
* @return mixed Array of contacts on success or PEAR Error on failure.
* Format of the returned contacts is for example:
* array(<uniqueid> => array('name' => <name>,
* 'number' => <number>),
* <uniqueid> => array('name' => <name>,
* 'number' => <number>));
*/
function getAddressBook()
{
/* Set up the url and call it. */
$url = sprintf('postcontacts.php?username=%s&password=%s&cmd=GETADDRESSBOOK',
urlencode($this->_params['user']),
urlencode($this->_params['password']));
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Check if there was an error response. */
if (substr($response, 0, 19) != 'AQSMS-ADDRESSBOOKOK') {
return $this->getError($response, _("Could not retrieve address book. %s"));
}
/* Parse the response and construct the array. */
list($response, $contacts_str) = explode(',', $response, 2);
/* Check that the full address book list has been received. */
$length = substr($response, 19);
if (strlen($contacts_str) != $length) {
return PEAR::raiseError(_("Could not fetch complete address book."));
}
$contacts_lines = explode("\n", $contacts_str);
$contacts = array();
/* Loop through lines and pick out the fields, make sure that the ""
* are not included in the values, so get the line less 1 char on each
* end and split for ",". */
foreach ($contacts_lines as $line) {
list($id, $name, $number) = explode('","', substr($line, 1, -1));
$contacts[$id] = array('name' => $name, 'number' => $number);
}
return $contacts;
}
/**
* Creates a new distribution list on the gateway.
*
* @access public
*
* @param string $name An arbitrary name for the new list.
* @param array $numbers A simple array of numbers to add to the list.
*
* @return mixed Gateway ID for the created list on success or PEAR Error
* on failure.
*/
function listCreate($name, $numbers)
{
/* Set up the url and call it. */
$url = sprintf('postdistribution.php?username=%s&password=%s&cmd=ADDDISTLIST&name=%s&numlist=%s',
urlencode($this->_params['user']),
urlencode($this->_params['password']),
urlencode($name),
implode(',', $numbers));
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Check if there was an error response. */
if (substr($response, 0, 16) != 'AQSMS-DISTITEMID') {
return $this->getError($response, _("Could not create distribution list. %s"));
}
/* Parse the response and get the distribution list ID. */
list($response, $id) = explode('=', $response);
/* TODO: do we need to check the length of the id string? */
return $id;
}
/**
* Deletes a distribution list from the gateway.
*
* @access public
*
* @param string $id The gateway ID for the list to delete.
*
* @return mixed True on success or PEAR Error on failure.
*/
function listDelete($id)
{
/* Set up the url and call it. */
$url = sprintf('postdistribution.php?username=%s&password=%s&cmd=DELETEDISTLIST&distid=%s',
urlencode($this->_params['user']),
urlencode($this->_params['password']),
$id);
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Check response. */
if ($response == 'AQSMS-OK') {
return true;
} else {
return $this->getError($response, _("Could not delete distribution list. %s"));
}
}
/**
* Updates a distribution list on the gateway.
*
* @access public
*
* @param string $id The gateway ID for the list to update.
* @param string $name The arbitrary name of the list. If
* different from the original name that
* the list was created under, the list
* will be renamed.
* @param optional string $numbers The new list of numbers in the list.
* If left empty, the result will be the
* same as calling the listRename()
* function.
*
* @return mixed True on success or PEAR Error on failure.
*/
function listUpdate($id, $name, $numbers = array())
{
/* Set up the url and call it. */
$url = sprintf('postdistribution.php?username=%s&password=%s&cmd=UPDATELISTNAME&distid=%s&name=%s',
urlencode($this->_params['user']),
urlencode($this->_params['password']),
$id,
urlencode($name));
/* Check if the list numbers need updating. */
if (!empty($numbers)) {
$url .= '&numbers=' . implode(',', $numbers);
}
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Check response. */
if ($response == 'AQSMS-OK') {
return true;
} else {
return $this->getError($response, _("Could not update distribution list. %s"));
}
}
/**
* Renames a distribution list on the gateway. Does nothing other than
* calling the listUpdate() function with just the $id and $name variables.
*
* @access public
*
* @param string $id The gateway ID for the list to update.
* @param string $name The new arbitrary name for the list.
*
* @return mixed True on success or PEAR Error on failure.
*/
function listRename($id, $name)
{
return $this->listUpdate($id, $name);
}
/**
* Fetches a listing of available distribution lists on the server.
*
* @access public
*
* @return mixed An array of lists on success or PEAR Error on failure.
* Format of the returned lists is for example:
* array(<uniqueid> => array('name' => <name>),
* <uniqueid> => array('name' => <name>));
*/
function getLists()
{
/* Set up the url and call it. */
$url = sprintf('postdistribution.php?username=%s&password=%s&cmd=GETCOMPACTLIST',
urlencode($this->_params['user']),
urlencode($this->_params['password']));
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Check if there was an error response. */
if (substr($response, 0, 22) != 'AQSMS-DISTRIBUTIONLIST') {
return $this->getError($response, _("Could not retrieve distribution lists. %s"));
}
/* Parse the response and construct the array. */
list($response, $lists_str) = explode(',', $response, 2);
/* Check that the full list of distribution lists has been received. */
$length = substr($response, 22);
if (strlen($lists_str) != $length) {
return PEAR::raiseError(_("Could not fetch the complete list of distribution lists."));
}
$lists_lines = explode("\n", $lists_str);
$lists = array();
/* Loop through lines and pick out the fields, make sure that the ""
* are not included in the values, so get the line less 1 char on each
* end and split for ",". */
foreach ($lists_lines as $line) {
list($id, $name, $count) = explode('","', substr($line, 1, -1));
$lists[$id] = array('name' => $name,
'count' => $count);
}
return $lists;
}
/**
* Fetches a specific distribution list from the gateway.
*
* @access public
*
* @param string The ID of the distribution list to fetch.
*
* @return mixed An array of numbers in the list on success or PEAR Error
* on failure.
*/
function getList($id)
{
/* Set up the url and call it. */
$url = sprintf('postdistribution.php?username=%s&password=%s&cmd=GETNUMBERSWITHID&distid=%s',
urlencode($this->_params['user']),
urlencode($this->_params['password']),
$id);
$response = $this->_callURL($url);
if (is_a($response, 'PEAR_Error')) {
return $response;
}
/* Check if there was an error response. */
if (substr($response, 0, 22) != 'AQSMS-DISTRIBUTIONLIST') {
return $this->getError($response, _("Could not retrieve distribution list. %s"));
}
/* Parse the response and construct the array. */
list($response, $list_str) = explode(',', $response, 2);
/* Check that the full list of distribution lists has been received. */
$length = substr($response, 22);
if (strlen($list_str) != $length) {
return PEAR::raiseError(_("Could not fetch complete distribution list."));
}
$list_str = trim($list_str);
list($count, $numbers) = explode('","', $list_str);
/* TODO: Anything useful that can be done with the count of numbers at
* the start? */
$count = substr($count, 1);
/* Explode the list of numbers into an array and return. */
$numbers = substr($numbers, 0, -1);
return explode(',', $numbers);
}
/**
* Identifies this gateway driver and returns a brief description.
*
* @access public
*
* @return array Array of driver info.
*/
function getInfo()
{
$info['name'] = _("sms2email via HTTP");
$info['desc'] = _("This driver allows sending of messages through the sms2email (http://sms2email.com) gateway, using the HTTP API");
return $info;
}
/**
* Returns the required parameters for this gateway driver. The settable
* parameters for this gateway are:
* - user - The username for authentication on the gateway;
* - password - The password for authentication on the gateway;
* - ssl - Whether or not to use SSL for communication with
* the gateway.
* - delivery_report - A URL for a script which would accept delivery
* report from the gateway.
*
* @access public
*
* @return array Array of required parameters.
*/
function getParams()
{
$params = array();
$params['user'] = array('label' => _("Username"), 'type' => 'text');
$params['password'] = array('label' => _("Password"), 'type' => 'text');
$params['ssl'] = array('label' => _("Use SSL"),
'type' => 'boolean',
'required' => false);
$params['delivery_report'] = array('label' => _("URL for your script delivery status report"),
'type' => 'text',
'required' => false);
return $params;
}
/**
* Returns the parameters that can be set as default for sending messages
* using this gateway driver and displayed when sending messages.
*
* @access public
*
* @return array Array of parameters that can be set as default.
*/
function getDefaultSendParams()
{
$params = array();
$params['from'] = array(
'label' => _("Source address"),
'type' => 'text');
$params['deliv_time'] = array(
'label' => _("Delivery time"),
'type' => 'enum',
'params' => array(array('now' => _("immediate"), 'user' => _("user select"))));
$types = array('SMS_TEXT' => 'SMS_TEXT', 'SMS_FLASH' => 'SMS_FLASH');
$params['msg_type'] = array(
'label' => _("Message type"),
'type' => 'multienum',
'params' => array($types));
return $params;
}
/**
* Returns the parameters for sending messages using this gateway driver,
* displayed when sending messages. These are filtered out using the
* default values set up when creating the gateway.
*
* @access public
*
* @return array Array of required parameters.
* @todo Would be nice to use a time/date setup rather than minutes from
* now for the delivery time. Upload field for ringtones/logos?
*/
function getSendParams($params)
{
if (empty($params['from'])) {
$params['from'] = array(
'label' => _("Source address"),
'type' => 'text');
}
if ($params['deliv_time'] == 'user') {
$params['deliv_time'] = array(
'label' => _("Delivery time"),
'type' => 'int',
'desc' => _("Value in minutes from now."));
}
if (count($params['msg_type']) > 1) {
$params['msg_type'] = array(
'label' => _("Message type"),
'type' => 'enum',
'params' => array($params['msg_type']));
} else {
$params['msg_type'] = $params['msg_type'][0];
}
return $params;
}
/**
* Returns a string representation of an error code.
*
* @access public
*
* @param int $error The error code to look up.
* @param optional string $text An existing error text to use to raise a
* PEAR Error.
*
* @return mixed A textual message corrisponding to the error code or a
* PEAR Error if passed an existing error text.
*
* @todo Check which of these are actually required and trim down the list.
*/
function getError($error, $error_text = '')
{
$error = trim($error);
/* An array of error codes returned by the gateway. */
$errors = array(
'AQSMS-NOAUTHDETAILS' => _("No username and/or password sent."),
'AQSMS-AUTHERROR' => _("Incorrect username and/or password."),
'AQSMS-NOMSG' => _("No message supplied."),
'AQSMS-NODEST' => _("No destination supplied."),
'AQSMS-NOCREDIT' => _("Insufficient credit."),
'AQSMS-NONAMESUPPLIED' => _("No name specified."),
'AQSMS-NONUMBERSUPPLIED' => _("No number specified."),
'AQSMS-ADDRESSBOOKERROR' => _("There was an error performing the specified address book function. Please try again later."),
'AQSMS-CONTACTIDERROR' => _("The contact ID number was not specified, left blank or was not found in the database."),
'AQSMS-CONTACTUPDATEERROR' => _("There was an error updating the contact details. Please try again later."),
'AQSMS-DISTIDERROR' => _("The distribution list ID was either not specified, left blank or not found in the database."),
'AQSMS-NODISTLISTSUPPLIED' => _("The distribution list was not specified."),
'AQSMS-INSUFFICIENTCREDITS' => _("Insufficient credit to send to the distribution list."),
'AQSMS-NONUMBERLISTSUPPLIED' => _("Numbers not specified for updating in distribution list."),
'AQSMS-DISTLISTUPDATEERROR' => _("There was an error updating the distribution list. Please try again later."));
if (empty($error_text)) {
return $errors[$error];
} else {
return PEAR::raiseError(sprintf($error_text, $errors[$error]));
}
}
/**
* Do the http call using a url passed to the function.
*
* @access private
*
* @param string $url The url to call.
*
* @return mixed The response on success or PEAR Error on failure.
*/
function _callURL($url)
{
$options['method'] = 'POST';
$options['timeout'] = 5;
$options['allowRedirects'] = true;
$url = ($this->_params['ssl'] ? 'https://' : 'http://') . $this->_base_url . $url;
if (!@include_once 'HTTP/Request.php') {
return PEAR::raiseError(_("Missing PEAR package HTTP_Request."));
}
$http = &new HTTP_Request($url, $options);
@$http->sendRequest();
if ($http->getResponseCode() != 200) {
return PEAR::raiseError(sprintf(_("Could not open %s."), $url));
}
return $http->getResponseBody();
}
}
|