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

Source for file textmagic_http.php

Documentation is available at textmagic_http.php

  1. <?php
  2. require_once 'HTTP/Request2.php';
  3.  
  4. /**
  5.  * Net_SMS_textmagic_http Class implements the HTTP API for accessing the
  6.  * TextMagic (api.textmagic.com) SMS gateway.
  7.  *
  8.  * Copyright 2009 Fedyashev Nikita <nikita@realitydrivendeveloper.com>
  9.  *
  10.  * See the enclosed file COPYING for license information (LGPL). If you did
  11.  * not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  12.  *
  13.  * $Horde: framework/Net_SMS/SMS/textmagic_http.php,v 1.20 2006/01/01 21:10:07 jan Exp $
  14.  *
  15.  * @package Net_SMS
  16.  * @author  Fedyashev Nikita <nikita@realitydrivendeveloper.com>
  17.  *
  18.  */
  19. {
  20.     var $_base_url 'https://www.textmagic.com/app/api?';
  21.  
  22.     protected $request;
  23.  
  24.     protected $username;
  25.     protected $password;
  26.  
  27.     public function __construct($paramsHTTP_Request2 $request)
  28.     {
  29.         parent::__construct($params);
  30.         $this->setRequest($request);
  31.         $this->setUsername(isset($params['user']$params['user': null);
  32.         $this->setPassword(isset($params['password']$params['password': null);
  33.     }
  34.  
  35.     public function setRequest(HTTP_Request2 $request{
  36.         $this->request = $request;
  37.     }
  38.  
  39.     /**
  40.      * An array of capabilities, so that the driver can report which operations
  41.      * it supports and which it doesn't. Possible values are:<pre>
  42.      *   send           - Send SMS, scheduled sending;
  43.      *   account        - Check account balance;
  44.      *   messageStatus  - Check messages's cost and delivery status;
  45.      *   receive        - Receive incoming messages;
  46.      *   deleteReply    - Delete specified incoming messages;
  47.      *   checkNumber    - Check phone number validity and destination price;
  48.      * </pre>
  49.      *
  50.      * @var array 
  51.      */
  52.     var $capabilities = array('auth'           => false,
  53.                               'batch'          => 100,
  54.                               'multi'          => true,
  55.                               'receive'        => true,
  56.                               'credit'         => true,
  57.                               'addressbook'    => false,
  58.                               'lists'          => false,
  59.  
  60.                               'message_status' => true,
  61.                               'delete_reply'   => true,
  62.                               'check_number'   => true);
  63.  
  64.     /**
  65.      * This function does the actual sending of the message.
  66.      *
  67.      * @param array  &$message The array containing the message and its send
  68.      *                          parameters.
  69.      * @param string $to       The destination string.
  70.      *
  71.      * @return array  An array with the success status and additional
  72.      *                 information.
  73.      */
  74.     function _send(&$message$to)
  75.     {
  76.  
  77.         $unicode    $this->_getUnicodeParam($message);
  78.         $max_length $this->_getMaxLengthParam($message);
  79.  
  80.         $to implode(','$to);
  81.  
  82.         $url sprintf('cmd=send&phone=%s&text=%s&unicode=%s&max_length=%s',
  83.                        urlencode($to),
  84.                        urlencode($message['text']),
  85.                        $unicode,
  86.                        $max_length);
  87.  
  88.         $response $this->_callURL($url);
  89.  
  90.         if (is_a($response'PEAR_Error')) {
  91.               return PEAR::raiseError(sprintf(_("Send failed.")));
  92.         }
  93.  
  94.         $result = array();
  95.  
  96.         if (!array_key_exists('error_code'$response)) {
  97.  
  98.             if (count(explode(','$to)) == 1{
  99.  
  100.                 $message_ids array_keys($response['message_id']);
  101.  
  102.                 $result[$to= array(
  103.                     0 => 1,
  104.                     1 => $message_ids[0]
  105.                 );
  106.             else {
  107.                 foreach ($response['message_id'as $id => $recipient{
  108.                     $result[$recipient= array(1$id);
  109.                 }
  110.             }
  111.         else {
  112.  
  113.             if (count(explode(','$to)) == 1{
  114.  
  115.                 $result[$to= array(
  116.                     0 => 0,
  117.                     1 => $response['error_message']
  118.                 );
  119.             else {
  120.                 foreach (explode(','$toas $recipient{
  121.                     $result[$recipient= array(0$response['error_message']);
  122.                 }
  123.             }
  124.         }
  125.  
  126.         return $result;
  127.     }
  128.  
  129.     function _getMaxLengthParam($message{
  130.         $default_params $this->getDefaultSendParams();
  131.  
  132.         if (isset($message['send_params']['max_length'])) {
  133.             $max_length $message['send_params']['max_length'];
  134.         else {
  135.             $max_length $default_params['max_length']['default_value'];
  136.         }
  137.  
  138.         return $max_length;
  139.  
  140.     }
  141.  
  142.     function _getUnicodeParam($message{
  143.         $default_params $this->getDefaultSendParams();
  144.  
  145.         if (isset($message['send_params']['unicode'])) {
  146.             $unicode $message['send_params']['unicode'];
  147.         else {
  148.             $unicode $default_params['unicode']['default_value'];
  149.         }
  150.  
  151.         return $unicode;
  152.     }
  153.  
  154.     /**
  155.      * This function check message delivery status.
  156.      *
  157.      * @param array $ids The array containing message IDs.
  158.      *
  159.      * @return array  An array with the success status and additional
  160.      *                 information.
  161.      */
  162.     function messageStatus($ids)
  163.     {
  164.  
  165.         if (!is_array($ids)) {
  166.             $ids = array($ids);
  167.         }
  168.  
  169.         $ids implode(','$ids);
  170.  
  171.         $url sprintf('cmd=message_status&ids=%s',
  172.                        urlencode($ids));
  173.  
  174.         $response $this->_callURL($url);
  175.  
  176.         if (is_a($response'PEAR_Error')) {
  177.               return PEAR::raiseError(sprintf(_("Send failed.")));
  178.         }
  179.  
  180.         $result = array();
  181.  
  182.         if (!array_key_exists('error_code'$response)) {
  183.  
  184.             if (count(explode(','$ids)) == 1{
  185.  
  186.                 $result[$ids= array(
  187.                     0 => 1,
  188.                     1 => $response[$ids]
  189.                 );
  190.             else {
  191.                 foreach ($response as $id => $message{
  192.                     $result[$id= array(1$message);
  193.                 }
  194.             }
  195.         else {
  196.  
  197.             if (count(explode(','$ids)) == 1{
  198.  
  199.                 $result[$to= array(
  200.                     0 => 0,
  201.                     1 => $response['error_message']
  202.                 );
  203.             else {
  204.                 foreach (explode(','$idsas $id{
  205.                     $result[$id= array(0$response['error_message']);
  206.                 }
  207.             }
  208.         }
  209.  
  210.         return $result;
  211.     }
  212.  
  213.     /**
  214.      * This function retrieves incoming messages.
  215.      *
  216.      * @param array $last_retrieved_id The array containing message IDs.
  217.      *
  218.      * @return array  An array with the success status and additional
  219.      *                 information.
  220.      */
  221.     function receive($last_retrieved_id)
  222.     {
  223.         if (!is_int($last_retrieved_id)) {
  224.             $last_retrieved_id = int($last_retrieved_id);
  225.         }
  226.  
  227.         $url sprintf('cmd=receive&last=%s'$last_retrieved_id);
  228.  
  229.         $response $this->_callURL($url);
  230.  
  231.         if (is_a($response'PEAR_Error')) {
  232.               return PEAR::raiseError(sprintf(_("Send failed.")));
  233.         }
  234.  
  235.         $result = array();
  236.  
  237.         if (!array_key_exists('error_code'$response)) {
  238.             $result[0= 1;
  239.  
  240.             $result[1$response;
  241.         else {
  242.             $result[0= 0;
  243.  
  244.             $result[1$response['error_message'];
  245.         }
  246.  
  247.         return $result;
  248.     }
  249.  
  250.  
  251.     /**
  252.      * This function allows you to delete Incoming message
  253.      *
  254.      * @param array $ids  The array containing message IDs.
  255.      *
  256.      * @return array An array with the success status and additional
  257.      *                 information.
  258.      */
  259.     function deleteReply($ids)
  260.     {
  261.  
  262.         if (!is_array($ids)) {
  263.             $ids = array($ids);
  264.         }
  265.  
  266.         $ids implode(','$ids);
  267.  
  268.         /* Set up the http sending url. */
  269.         $url sprintf('cmd=delete_reply&ids=%s',
  270.                        urlencode($ids));
  271.  
  272.         $response $this->_callURL($url);
  273.  
  274.         if (is_a($response'PEAR_Error')) {
  275.               return PEAR::raiseError(sprintf(_("Send failed.")));
  276.         }
  277.  
  278.         $result = array();
  279.  
  280.         if (!array_key_exists('error_code'$response)) {
  281.  
  282.             if (count(explode(','$ids)) == 1{
  283.  
  284.                 $result[$ids= array(
  285.                     0 => 1,
  286.                     1 => true
  287.                 );
  288.             else {
  289.                 foreach ($response['deleted'as $id{
  290.                     $result[$id= array(1true);
  291.                 }
  292.             }
  293.         else {
  294.  
  295.             if (count(explode(','$ids)) == 1{
  296.  
  297.                 $result[$to= array(
  298.                     0 => 0,
  299.                     1 => $response['error_message']
  300.                 );
  301.             else {
  302.                 foreach (explode(','$idsas $id{
  303.                     $result[$id= array(0$response['error_message']);
  304.                 }
  305.             }
  306.         }
  307.  
  308.         return $result;
  309.     }
  310.  
  311.      /**
  312.      * This function allows you to validate phone number's format,
  313.      * check its country and message price to the destination .
  314.      *
  315.      * @param array $numbers Phone numbers array to be checked.
  316.      *
  317.      * @return array  An array with the success status and additional
  318.      *                 information.
  319.      */
  320.     function checkNumber($numbers)
  321.     {
  322.  
  323.         if (!is_array($numbers)) {
  324.             $numbers = array($numbers);
  325.         }
  326.  
  327.         $numbers implode(','$numbers);
  328.  
  329.         $url sprintf('cmd=check_number&phone=%s',
  330.                        urlencode($numbers));
  331.  
  332.         $response $this->_callURL($url);
  333.  
  334.         if (is_a($response'PEAR_Error')) {
  335.               return PEAR::raiseError(sprintf(_("Send failed.")));
  336.         }
  337.  
  338.         if (is_a($response'PEAR_Error')) {
  339.               return PEAR::raiseError(sprintf(_("Send failed.")));
  340.         }
  341.  
  342.         $result = array();
  343.  
  344.         if (!array_key_exists('error_code'$response)) {
  345.  
  346.             if (count(explode(','$numbers)) == 1{
  347.  
  348.                 $result[$numbers= array(
  349.                     0 => 1,
  350.                     1 => array(
  351.                         "price" => $response[$numbers]['price'],
  352.                         "country" => $response[$numbers]['country']
  353.                     )
  354.                 );
  355.             else {
  356.                 foreach (explode(','$numbersas $number{
  357.                     $result[$number= array(1array(
  358.                         "price" => $response[$number]['price'],
  359.                         "country" => $response[$number]['country']
  360.                     ));
  361.                 }
  362.             }
  363.         else {
  364.  
  365.             if (count(explode(','$numbers)) == 1{
  366.  
  367.                 $result[explode(','$numbers)= array(
  368.                     0 => 0,
  369.                     1 => $response['error_message']
  370.                 );
  371.             else {
  372.                 foreach (explode(','$numbersas $number{
  373.                     $result[$number= array(0$response['error_message']);
  374.                 }
  375.             }
  376.         }
  377.  
  378.         return $result;
  379.  
  380.     }
  381.  
  382.     /**
  383.      * Returns the current credit balance on the gateway.
  384.      *
  385.      * @access private
  386.      *
  387.      * @return integer  The credit balance available on the gateway.
  388.      */
  389.     function _getBalance()
  390.     {
  391.         $url 'cmd=account';
  392.  
  393.         $response $this->_callURL($url);
  394.  
  395.         if (is_a($response'PEAR_Error')) {
  396.             return PEAR::raiseError(sprintf(_("Send failed. %s")$response['error_message']));
  397.         }
  398.  
  399.         if (!array_key_exists('error_code'$response)) {
  400.             return $response['balance'];
  401.         else {
  402.             return $this->getError($response['error_message']_("Could not check balance. %s"));
  403.         }
  404.     }
  405.  
  406.     /**
  407.      * Identifies this gateway driver and returns a brief description.
  408.      *
  409.      * @return array  Array of driver info.
  410.      */
  411.     function getInfo()
  412.     {
  413.         $info['name'_("TextMagic via HTTP");
  414.         $info['desc'_("This driver allows sending of messages through the TextMagic (http://api.textmagic.com) gateway, using the HTTP API");
  415.  
  416.         return $info;
  417.     }
  418.  
  419.     /**
  420.      * Returns the required parameters for this gateway driver.
  421.      *
  422.      * @return array  Array of required parameters.
  423.      */
  424.     function getParams()
  425.     {
  426.         $params               = array();
  427.         $params['username']   = array('label' => _("Username")'type' => 'text');
  428.         $params['password']   = array('label' => _("Password")'type' => 'text');
  429.         $params['unicode']    = array('label' => _("Unicode message flag")'type' => 'int');
  430.         $params['max_length'= array('label' => _("Maximum messages to be sent at once")'type' => 'int');
  431.  
  432.         return $params;
  433.     }
  434.  
  435.     /**
  436.      * Returns the parameters that can be set as default for sending messages
  437.      * using this gateway driver and displayed when sending messages.
  438.      *
  439.      * @return array  Array of parameters that can be set as default.
  440.      * @todo  Set up batch fields/params, would be nice to have ringtone/logo
  441.      *         support too, queue choice, unicode choice.
  442.      */
  443.     function getDefaultSendParams()
  444.     {
  445.         $params = array();
  446.  
  447.         $params['max_length'= array(
  448.             'label' => _("Max messages quantity"),
  449.             'type' => 'int',
  450.             'default_value' => 3);
  451.  
  452.         $params['unicode'= array(
  453.             'label' => _("Unicode message flag"),
  454.             'type' => 'int',
  455.             'default_value' => 1);
  456.  
  457.         return $params;
  458.     }
  459.  
  460.     /**
  461.      * Returns the parameters for sending messages using this gateway driver,
  462.      * displayed when sending messages. These are filtered out using the
  463.      * default values set for the gateway.
  464.      *
  465.      * @return array  Array of required parameters.
  466.      */
  467.     function getSendParams($params)
  468.     {
  469.         if (empty($params['max_length'])) {
  470.             $params['max_length'= array(
  471.                 'label' => _("Max messages quantity"),
  472.                 'type' => 'int');
  473.         }
  474.  
  475.         if (empty($params['unicode'])) {
  476.             $params['unicode'= array(
  477.                 'label' => _("Unicode message flag"),
  478.                 'type' => 'int');
  479.         }
  480.  
  481.         return $params;
  482.     }
  483.  
  484.     /**
  485.      * Returns a string representation of an error code.
  486.      *
  487.      * @param string  $error_text An existing error text to use to raise a
  488.      *                             PEAR Error.
  489.      * @param integer $error The error code to look up.
  490.      *
  491.      * @return mixed  A textual message corresponding to the error code or a
  492.      *                 PEAR Error if passed an existing error text.
  493.      *
  494.      * @todo  Check which of these are actually required and trim down the
  495.      *         list.
  496.      */
  497.     function getError($error_text ''$error)
  498.     {
  499.         /* An array of error codes returned by the gateway. */
  500.         $errors = array(2  => _("Low balance"),
  501.                         5  => _("Invalid username/password combination"),
  502.                         6  => _("Message was not sent"),
  503.                         7  => _("Too long message length"),
  504.                         8  => _("IP address is not allowed"),
  505.                         9  => _("Wrong phone number format"),
  506.                         10 => _("Wrong parameter value"),
  507.                         11 => _("Daily API requests limit exceeded"),
  508.                         12 => _("Too many items per request"),
  509.                         13 => _("Your account has been deactivated"),
  510.                         14 => _("Unknwon message ID"),
  511.                         15 => _("Unicode characters detected on unicode=0 option"));
  512.  
  513.         if (!empty($error_text)) {
  514.             return $error_text;
  515.         else {
  516.             return PEAR::raiseError($errors[$error]$error);
  517.         }
  518.     }
  519.  
  520.     /**
  521.      * Do the http call using a url passed to the function.
  522.      *
  523.      * @param string $url The url to call.
  524.      *
  525.      * @return mixed  The response on success or PEAR Error on failure.
  526.      */
  527.     function _callURL($url)
  528.     {
  529.         $this->request->setURL($this->_base_url $url);
  530.  
  531.         /* Add the authentication values to POST. */
  532.         $this->request->addPostParameter('username'$this->getUsername());
  533.         $this->request->addPostParameter('password'$this->getPassword());
  534.  
  535.  
  536.         $response $this->request->send();
  537.         if ($response->getStatus(!= 200{
  538.             throw new Net_URL_Exception(sprintf(_("Could not open %s.")$url));
  539.         }
  540.  
  541.         return json_decode($response->getBody()true);
  542.     }
  543.  
  544.     public function getUsername({
  545.         return $this->username;
  546.     }
  547.  
  548.     public function getPassword({
  549.         return $this->password;
  550.     }
  551.  
  552.     public function setUsername($username{
  553.         $this->username = $username;
  554.     }
  555.  
  556.     public function setPassword($password{
  557.         $this->password = $password;
  558.     }
  559. }

Documentation generated on Mon, 11 Mar 2019 15:57:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.