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

Source for file SMS.php

Documentation is available at SMS.php

  1. <?php
  2.  
  3. require_once 'PEAR.php';
  4.  
  5. /**
  6.  * Net_SMS Class
  7.  *
  8.  * Copyright 2003-2005 Marko Djukic <marko@oblo.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.php,v 1.9 2004/04/28 23:38:29 mdjukic Exp $
  14.  *
  15.  * @author  Marko Djukic <marko@oblo.com>
  16.  * @package Net_SMS
  17.  */
  18. class Net_SMS {
  19.  
  20.     /**
  21.      * A hash containing any parameters for the current gateway driver.
  22.      *
  23.      * @var array $_params 
  24.      */
  25.     var $_params = array();
  26.  
  27.     var $_auth = null;
  28.  
  29.     /**
  30.      * Constructor
  31.      *
  32.      * @param optional array $params  Any parameters needed for this gateway
  33.      *                                 driver.
  34.      */
  35.     function Net_SMS($params = null)
  36.     {
  37.         $this->_params $params;
  38.     }
  39.  
  40.     /**
  41.      * Returns a list of available gateway drivers.
  42.      *
  43.      * @access public
  44.      *
  45.      * @return array  An array of available drivers.
  46.      */
  47.     function getDrivers()
  48.     {
  49.         static $drivers = array();
  50.         if (!empty($drivers)) {
  51.             return $drivers;
  52.         }
  53.  
  54.         $drivers = array();
  55.  
  56.         if ($driver_dir = opendir(dirname(__FILE__'/SMS/')) {
  57.             while (false !== ($file = readdir($driver_dir))) {
  58.                 /* Hide dot files and non .php files. */
  59.                 if (substr($file01!= '.' && substr($file-4== '.php'{
  60.                     $driver = substr($file0-4);
  61.                     $driver_info = Net_SMS::getGatewayInfo($driver);
  62.                     $drivers[$driver$driver_info['name'];
  63.                 }
  64.             }
  65.             closedir($driver_dir);
  66.         }
  67.  
  68.         return $drivers;
  69.     }
  70.  
  71.     /**
  72.      * Returns information on a gateway, such as name and a brief description,
  73.      * from the driver subclass getInfo() function.
  74.      *
  75.      * @access public
  76.      *
  77.      * @return array  An array of extra information.
  78.      */
  79.     function getGatewayInfo($gateway)
  80.     {
  81.         static $info = array();
  82.         if (isset($info[$gateway])) {
  83.             return $info[$gateway];
  84.         }
  85.  
  86.         require_once 'Net/SMS/' $gateway '.php';
  87.         $class 'Net_SMS_' $gateway;
  88.         $info[$gatewaycall_user_func(array($class'getInfo'));
  89.  
  90.         return $info[$gateway];
  91.     }
  92.  
  93.     /**
  94.      * Returns parameters for a gateway from the driver subclass getParams()
  95.      * function.
  96.      *
  97.      * @access public
  98.      *
  99.      * @param string  The name of the gateway driver for which to return the
  100.      *                 parameters.
  101.      *
  102.      * @return array  An array of extra information.
  103.      */
  104.     function getGatewayParams($gateway)
  105.     {
  106.         static $params = array();
  107.         if (isset($params[$gateway])) {
  108.             return $params[$gateway];
  109.         }
  110.  
  111.         require_once 'Net/SMS/' $gateway '.php';
  112.         $class 'Net_SMS_' $gateway;
  113.         $params[$gatewaycall_user_func(array($class'getParams'));
  114.  
  115.         return $params[$gateway];
  116.     }
  117.  
  118.     /**
  119.      * Returns send parameters for a gateway from the driver subclass
  120.      * getDefaultSendParams()function. These are parameters which are
  121.      * available to the user during sending, such as setting a time for
  122.      * delivery, or type of SMS (normal text or flash), or source address,
  123.      * etc.
  124.      *
  125.      * @access public
  126.      *
  127.      * @param string  The name of the gateway driver for which to return the
  128.      *                 send parameters.
  129.      *
  130.      * @return array  An array of available send parameters.
  131.      */
  132.     function getDefaultSendParams($gateway)
  133.     {
  134.         static $params = array();
  135.         if (isset($params[$gateway])) {
  136.             return $params[$gateway];
  137.         }
  138.  
  139.         require_once 'Net/SMS/' $gateway '.php';
  140.         $class 'Net_SMS_' $gateway;
  141.         $params[$gatewaycall_user_func(array($class'getDefaultSendParams'));
  142.  
  143.         return $params[$gateway];
  144.     }
  145.  
  146.     /**
  147.      * Query the current Gateway object to find out if it supports the given
  148.      * capability.
  149.      *
  150.      * @access public
  151.      *
  152.      * @param string $capability  The capability to test for.
  153.      *
  154.      * @return mixed  Whether or not the capability is supported or any other
  155.      *                 value that the capability wishes to report.
  156.      */
  157.     function hasCapability($capability)
  158.     {
  159.         if (!empty($this->capabilities[$capability])) {
  160.             return $this->capabilities[$capability];
  161.         }
  162.         return false;
  163.     }
  164.  
  165.     /**
  166.      * Authenticates against the gateway if required.
  167.      *
  168.      * @access public
  169.      *
  170.      * @return mixed  True on success or PEAR Error on failure.
  171.      */
  172.     function authenticate()
  173.     {
  174.         /* Do authentication for this gateway if driver requires it. */
  175.         if ($this->hasCapability('auth')) {
  176.             $this->_auth = $this->_authenticate();
  177.             return $this->_auth;
  178.         }
  179.         return true;
  180.     }
  181.  
  182.     /**
  183.      * Sends a message to one or more recipients. Hands off the actual sending
  184.      * to the gateway driver.
  185.      *
  186.      * @access public
  187.      *
  188.      * @param array $message  The message to be sent, which is composed of:
  189.      *                         <pre>
  190.      *                           id   - A unique ID for the message;
  191.      *                           to   - An array of recipients;
  192.      *                           text - The text of the message;
  193.      *                         </pre>
  194.      *                          
  195.      *
  196.      * @return mixed  True on success or PEAR Error on failure.
  197.      */
  198.     function send(&$message)
  199.     {
  200.         /* Authenticate. */
  201.         if (is_a($this->authenticate()'PEAR_Error')) {
  202.             return $this->_auth;
  203.         }
  204.  
  205.         /* Make sure the recipients are in an array. */
  206.         if (!is_array($message['to'])) {
  207.             $message['to'= array($message['to']);
  208.         }
  209.  
  210.         /* Array to store each send. */
  211.         $sends = array();
  212.  
  213.         /* If gateway supports batch sending, preference is given to this
  214.          * method. */
  215.         if ($max_per_batch $this->hasCapability('batch')) {
  216.             /* Split up the recipients in the max recipients per batch as
  217.              * supported by gateway. */
  218.             $iMax count($message['to']);
  219.             $batches ceil($iMax $max_per_batch);
  220.  
  221.             /* Loop through the batches and compose messages to be sent. */
  222.             for ($b = 0; $b $batches$b++{
  223.                 $recipients array_slice($message['to']($b $max_per_batch)$max_per_batch);
  224.                 $response $this->_send($message$recipients);
  225.                 foreach ($recipients as $recipient{
  226.                     if ($response[$recipient][0== 1{
  227.                         /* Message was sent, store remote id. */
  228.                         $remote_id $response[$recipient][1];
  229.                         $error = null;
  230.                     else {
  231.                         /* Message failed, store error code. */
  232.                         $remote_id = null;
  233.                         $error $response[$recipient][1];
  234.                     }
  235.  
  236.                     /* Store the sends. */
  237.                     $sends[= array('message_id' => $message['id'],
  238.                                      'remote_id'  => $remote_id,
  239.                                      'recipient'  => $recipient,
  240.                                      'error'      => $error);
  241.                 }
  242.             }
  243.         else {
  244.             /* No batch sending available, just loop through all recipients
  245.              * and send a message for each one. */
  246.             foreach ($message['to'as $recipient{
  247.                 $response $this->_send($message$recipient);
  248.                 if ($response[0== 1{
  249.                     /* Message was sent, store remote id if any. */
  250.                     $remote_id (isset($response[1]$response[1: null);
  251.                     $error = null;
  252.                 else {
  253.                     /* Message failed, store error code. */
  254.                     $remote_id = null;
  255.                     $error $response[1];
  256.                 }
  257.  
  258.                 /* Store the sends. */
  259.                 $sends[= array('message_id' => $message['id'],
  260.                                  'remote_id'  => $remote_id,
  261.                                  'recipient'  => $recipient,
  262.                                  'error'      => $error);
  263.             }
  264.         }
  265.  
  266.         return $sends;
  267.     }
  268.  
  269.     /**
  270.      * If the current driver has a credit capability, queries the gateway for
  271.      * a credit balance and returns the value.
  272.      *
  273.      * @access public
  274.      *
  275.      * @return int  Value indicating available credit or null if not supported.
  276.      */
  277.     function getBalance()
  278.     {
  279.         /* Authenticate. */
  280.         if (is_a($this->authenticate()'PEAR_Error')) {
  281.             return $this->_auth;
  282.         }
  283.  
  284.         /* Check balance. */
  285.         if ($this->hasCapability('credit')) {
  286.             return $this->_getBalance();
  287.         else {
  288.             return null;
  289.         }
  290.     }
  291.  
  292.     /**
  293.      * Attempts to return a concrete Gateway instance based on $driver.
  294.      *
  295.      * @param string $driver  The type of concrete Gateway subclass to return.
  296.      *                         This is based on the gateway driver ($driver).
  297.      *                         The code is dynamically included.
  298.      * @param array $params   A hash containing any additional configuration or
  299.      *                         connection parameters a subclass might need.
  300.      *
  301.      * @return Net_SMS  The newly created concrete Gateway instance or false on
  302.      *                   an error.
  303.      */
  304.     function &factory($driver$params = array())
  305.     {
  306.         include_once 'Net/SMS/' $driver '.php';
  307.         $class 'Net_SMS_' $driver;
  308.         if (class_exists($class)) {
  309.             return $ret &new $class($params);
  310.         else {
  311.             return PEAR::raiseError(sprintf(_("Class definition of %s not found.")$driver));
  312.         }
  313.     }
  314.  
  315.     /**
  316.      * Attempts to return a reference to a concrete Net_SMS instance based on
  317.      * $driver. It wil only create a new instance if no Net_SMS instance with
  318.      * the same parameters currently exists.
  319.      *
  320.      * This method must be invoked as: $var = &Net_SMS::singleton()
  321.      *
  322.      * @param string $driver  The type of concrete Net_SMS subclass to return.
  323.      *                         The is based on the gateway driver ($driver).
  324.      *                         The code is dynamically included.
  325.      *
  326.      * @param array $params  A hash containing any additional configuration or
  327.      *                        connection parameters a subclass might need.
  328.      *
  329.      * @return mixed  The created concrete Net_SMS instance, or false on error.
  330.      */
  331.     function &singleton($driver$params = array())
  332.     {
  333.         static $instances;
  334.         if (!isset($instances)) {
  335.             $instances = array();
  336.         }
  337.  
  338.         $signature serialize(array($driver$params));
  339.         if (!isset($instances[$signature])) {
  340.             $instances[$signature&Net_SMS::factory($driver$params);
  341.         }
  342.  
  343.         return $instances[$signature];
  344.     }
  345.  
  346. }

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