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