Net_SMS
[ class tree: Net_SMS ] [ index: Net_SMS ] [ all elements ]

Source for file clickatell_http.php

Documentation is available at clickatell_http.php

  1. <?php
  2. /**
  3.  * Net_SMS_clickatell_http Class implements the HTTP API for accessing
  4.  * the Clickatell (www.clickatell.com) SMS gateway.
  5.  *
  6.  * Copyright 2003-2004 Marko Djukic <marko@oblo.com>
  7.  *
  8.  * See the enclosed file COPYING for license information (LGPL). If you did not
  9.  * receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  10.  *
  11.  * $Horde: framework/Net_SMS/SMS/clickatell_http.php,v 1.13 2004/04/28 23:38:29 mdjukic Exp $
  12.  *
  13.  * @author Marko Djukic <marko@oblo.com>
  14.  * @version $Revision: 1.13 $
  15.  * @package Net_SMS
  16.  */
  17. class Net_SMS_clickatell_http extends Net_SMS {
  18.  
  19.     var $_session_id = null;
  20.     var $_base_url 'http://api.clickatell.com/http/';
  21.  
  22.     /**
  23.      * An array of capabilities, so that the driver can report which operations
  24.      * it supports and which it doesn't. Possible values are:
  25.      *   auth        - The gateway require authentication before sending;
  26.      *   batch       - Batch sending is supported;
  27.      *   multi       - Sending of messages to multiple recipients is supported;
  28.      *   receive     - Whether this driver is capable of receiving SMS;
  29.      *   credit      - Is use of the gateway based on credits;
  30.      *   addressbook - Are gateway addressbooks supported;
  31.      *   lists       - Gateway support for distribution lists.
  32.      *
  33.      * @var array $capabilities 
  34.      */
  35.     var $capabilities = array('auth'        => true,
  36.                               'batch'       => 100,
  37.                               'multi'       => true,
  38.                               'receive'     => false,
  39.                               'credit'      => true,
  40.                               'addressbook' => false,
  41.                               'lists'       => false);
  42.  
  43.     /**
  44.      * Authenticate at the gateway and set a session id if successful. Caching
  45.      * is used to minimise the http calls for subsequent messages.
  46.      *
  47.      * @access private
  48.      *
  49.      * @return mixed  True on success or PEAR Error on failure.
  50.      */
  51.     function _authenticate()
  52.     {
  53.         global $conf;
  54.  
  55.         /* We have already authenticated so return true. */
  56.         if (!empty($this->_session_id)) {
  57.             return true;
  58.         }
  59.  
  60.         /* Set up the http authentication url. */
  61.         $url sprintf('auth?user=%s&password=%s&api_id=%s',
  62.                        urlencode($this->_params['user']),
  63.                        urlencode($this->_params['password']),
  64.                        $this->_params['api_id']);
  65.  
  66.         /* Do the HTTP authentication and get the response. */
  67.         $response Net_SMS_clickatell_http::_callURL($url);
  68.         if (is_a($response'PEAR_Error')) {
  69.             return PEAR::raiseError(sprintf(_("Authentication failed. %s")$response->getMessage()));
  70.         }
  71.  
  72.         /* Split up the response. */
  73.         $response explode(':'$response);
  74.         if ($response[0== 'OK'{
  75.             $this->_session_id trim($response[1]);
  76.             return true;
  77.         else {
  78.             return $this->getError($response[1]_("Authentication failed. %s"));
  79.         }
  80.     }
  81.  
  82.     /**
  83.      * This function does the actual sending of the message.
  84.      *
  85.      * @access private
  86.      *
  87.      * @param array  $message  The array containing the message and its send
  88.      *                          parameters.
  89.      * @param string $to       The destination string.
  90.      *
  91.      * @return mixed  True on success or PEAR Error on failure.
  92.      */
  93.     function _send(&$message$to)
  94.     {
  95.         /* Set up the http sending url. */
  96.         $url sprintf('sendmsg?session_id=%s&text=%s',
  97.                        $this->_session_id,
  98.                        urlencode($message['text']));
  99.  
  100.         $req_feat = 0;
  101.         if (!empty($message['send_params']['from'])) {
  102.             /* If source from is set, require it for transit gateways and append
  103.                to url. */
  104.             $req_feat =+ 16;
  105.             $url .= '&from=' urlencode($message['send_params']['from']);
  106.         }
  107.         if ($message['send_params']['msg_type'== 'SMS_FLASH'{
  108.             /* If message type is flash, require it for transit gateways. */
  109.             $req_feat =+ 512;
  110.             $url .= '&msg_type=' $message['send_params']['msg_type'];
  111.         }
  112.         if (!empty($req_feat)) {
  113.             /* If features have been required, add to url. */
  114.             $url .= '&req_feat=' $req_feat;
  115.         }
  116.  
  117.         /* Append the recipients of this message and call the url. */
  118.         foreach ($to as $key => $val{
  119.             if (preg_match('/^.*?<?\+?(\d{7,})(>|$)/'$val$matches)) {
  120.                 $to[$key$matches[1];
  121.             else {
  122.                 /* FIXME: Silently drop bad recipients. This should be logged
  123.                  * and/or reported. */
  124.                 unset($to[$key]);
  125.             }
  126.         }
  127.         $to implode(','$to);
  128.         $url .= '&to=' $to;
  129.         $response trim($this->_callURL($url));
  130.  
  131.         /* Ugly parsing of the response, but that's how it comes back. */
  132.         $lines explode("\n"$response);
  133.         $response = array();
  134.  
  135.         if (count($lines> 1{
  136.             foreach ($lines as $line{
  137.                 $parts explode('To:'$line);
  138.                 $recipient trim($parts[1]);
  139.                 $outcome explode(':'$parts[0]);
  140.                 $response[$recipient= array(($outcome[0== 'ID' ? 1 : 0)$outcome[1]);
  141.             }
  142.         else {
  143.             /* Single recipient. */
  144.             $outcome explode(':'$lines[0]);
  145.             $response[$to= array(($outcome[0== 'ID' ? 1 : 0)$outcome[1]);
  146.         }
  147.  
  148.         return $response;
  149.     }
  150.  
  151.     /**
  152.      * Returns the current credit balance on the gateway.
  153.      *
  154.      * @access private
  155.      *
  156.      * @return int  The credit balance available on the gateway.
  157.      */
  158.     function _getBalance()
  159.     {
  160.         /* Set up the url and call it. */
  161.         $url sprintf('getbalance?session_id=%s',
  162.                        $this->_session_id);
  163.         $response trim($this->_callURL($url));
  164.  
  165.         /* Try splitting up the response. */
  166.         $lines explode('='$response);
  167.  
  168.         /* Split up the response. */
  169.         $response explode(':'$response);
  170.         if ($response[0== 'Credit'{
  171.             return trim($response[1]);
  172.         else {
  173.             return $this->getError($response[1]_("Could not check balance. %s"));
  174.         }
  175.     }
  176.  
  177.     /**
  178.      * Identifies this gateway driver and returns a brief description.
  179.      *
  180.      * @access public
  181.      *
  182.      * @return array  Array of driver info.
  183.      */
  184.     function getInfo()
  185.     {
  186.         $info['name'_("Clickatell via HTTP");
  187.         $info['desc'_("This driver allows sending of messages through the Clickatell (http://clickatell.com) gateway, using the HTTP API");
  188.  
  189.         return $info;
  190.     }
  191.  
  192.     /**
  193.      * Returns the required parameters for this gateway driver.
  194.      *
  195.      * @access public
  196.      *
  197.      * @return array  Array of required parameters.
  198.      */
  199.     function getParams()
  200.     {
  201.         $params = array();
  202.         $params['user']     = array('label' => _("Username")'type' => 'text');
  203.         $params['password'= array('label' => _("Password")'type' => 'text');
  204.         $params['api_id']   = array('label' => _("API ID")'type' => 'text');
  205.  
  206.         return $params;
  207.     }
  208.  
  209.     /**
  210.      * Returns the parameters that can be set as default for sending messages
  211.      * using this gateway driver and displayed when sending messages.
  212.      *
  213.      * @access public
  214.      *
  215.      * @return array  Array of parameters that can be set as default.
  216.      * @todo  Set up batch fields/params, would be nice to have ringtone/logo
  217.      *         support too, queue choice, unicode choice.
  218.      */
  219.     function getDefaultSendParams()
  220.     {
  221.         $params = array();
  222.         $params['from'= array(
  223.             'label' => _("Source address"),
  224.             'type' => 'text');
  225.  
  226.         $params['deliv_time'= array(
  227.             'label' => _("Delivery time"),
  228.             'type' => 'enum',
  229.             'params' => array(array('now' => _("immediate")'user' => _("user select"))));
  230.  
  231.         $types = array('SMS_TEXT' => 'SMS_TEXT''SMS_FLASH' => 'SMS_FLASH');
  232.         $params['msg_type'= array(
  233.             'label' => _("Message type"),
  234.             'type' => 'multienum',
  235.             'params' => array($types));
  236.  
  237.         return $params;
  238.     }
  239.  
  240.     /**
  241.      * Returns the parameters for sending messages using this gateway driver,
  242.      * displayed when sending messages. These are filtered out using the
  243.      * default values set for the gateway.
  244.      *
  245.      * @access public
  246.      *
  247.      * @return array  Array of required parameters.
  248.      * @todo  Would be nice to use a time/date setup rather than minutes from
  249.      *         now for the delivery time. Upload field for ringtones/logos?
  250.      */
  251.     function getSendParams($params)
  252.     {
  253.         if (empty($params['from'])) {
  254.             $params['from'= array(
  255.                 'label' => _("Source address"),
  256.                 'type' => 'text');
  257.         }
  258.  
  259.         if ($params['deliv_time'== 'user'{
  260.             $params['deliv_time'= array(
  261.                 'label' => _("Delivery time"),
  262.                 'type' => 'int',
  263.                 'desc' => _("Value in minutes from now."));
  264.         }
  265.  
  266.         if (count($params['msg_type']> 1{
  267.             $params['msg_type'= array(
  268.                 'label' => _("Message type"),
  269.                 'type' => 'enum',
  270.                 'params' => array($params['msg_type']));
  271.         else {
  272.             $params['msg_type'$params['msg_type'][0];
  273.         }
  274.  
  275.         return $params;
  276.     }
  277.  
  278.     /**
  279.      * Returns a string representation of an error code.
  280.      *
  281.      * @access public
  282.      *
  283.      * @param int $error             The error code to look up.
  284.      * @param optional string $text  An existing error text to use to raise a
  285.      *                                PEAR Error.
  286.      *
  287.      * @return mixed  A textual message corrisponding to the error code or a
  288.      *                 PEAR Error if passed an existing error text.
  289.      *
  290.      * @todo  Check which of these are actually required and trim down the list.
  291.      */
  292.     function getError($error$error_text '')
  293.     {
  294.         /* Make sure we get only the number at the start of an error. */
  295.         list($errorexplode(','$error);
  296.         $error trim($error);
  297.  
  298.         /* An array of error codes returned by the gateway. */
  299.         $errors = array('001' => _("Authentication failed"),
  300.                         '002' => _("Unknown username or password."),
  301.                         '003' => _("Session ID expired."),
  302.                         '004' => _("Account frozen."),
  303.                         '005' => _("Missing session ID."),
  304.                         '007' => _("IP lockdown violation."),
  305.                         '101' => _("Invalid or missing parameters."),
  306.                         '102' => _("Invalid UDH. (User Data Header)."),
  307.                         '103' => _("Unknown apimsgid (API Message ID)."),
  308.                         '104' => _("Unknown climsgid (Client Message ID)."),
  309.                         '105' => _("Invalid destination address."),
  310.                         '106' => _("Invalid source address."),
  311.                         '107' => _("Empty message."),
  312.                         '108' => _("Invalid or missing api_id."),
  313.                         '109' => _("Missing message ID."),
  314.                         '110' => _("Error with email message."),
  315.                         '111' => _("Invalid protocol."),
  316.                         '112' => _("Invalid msg_type."),
  317.                         '113' => _("Max message parts exceeded."),
  318.                         '114' => _("Cannot route message to specified number."),
  319.                         '115' => _("Message expired."),
  320.                         '116' => _("Invalid unicode data."),
  321.                         '201' => _("Invalid batch ID."),
  322.                         '202' => _("No batch template."),
  323.                         '301' => _("No credit left."),
  324.                         '302' => _("Max allowed credit."));
  325.  
  326.         if (empty($error_text)) {
  327.             return $errors[$error];
  328.         else {
  329.             return PEAR::raiseError(sprintf($error_text$errors[$error]));
  330.         }
  331.     }
  332.  
  333.     /**
  334.      * Do the http call using a url passed to the function.
  335.      *
  336.      * @access private
  337.      *
  338.      * @param string $url  The url to call.
  339.      *
  340.      * @return mixed  The response on success or PEAR Error on failure.
  341.      */
  342.     function _callURL($url)
  343.     {
  344.         $options['method''POST';
  345.         $options['timeout'= 5;
  346.         $options['allowRedirects'= true;
  347.  
  348.         if (!@include_once 'HTTP/Request.php'{
  349.             return PEAR::raiseError(_("Missing PEAR package HTTP_Request."));
  350.         }
  351.         $http &new HTTP_Request($this->_base_url $url$options);
  352.         @$http->sendRequest();
  353.         if ($http->getResponseCode(!= 200{
  354.             return PEAR::raiseError(sprintf(_("Could not open %s.")$url));
  355.         }
  356.  
  357.         return $http->getResponseBody();
  358.     }
  359.  
  360. }

Documentation generated on Mon, 11 Mar 2019 10:17:14 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.