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-2004 Marko Djukic <marko@oblo.com>
  9.  *
  10.  * See the enclosed file COPYING for license information (LGPL). If you did not
  11.  * receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  12.  *
  13.  * $Horde: framework/Net_SMS/SMS.php,v 1.10 2004/05/04 21:54:11 mdjukic Exp $
  14.  *
  15.  * @author  Marko Djukic <marko@oblo.com>
  16.  * @version $Revision: 1.10 $
  17.  * @package Net_SMS
  18.  */
  19. class Net_SMS {
  20.  
  21.     /**
  22.      * A hash containing any parameters for the current gateway driver.
  23.      *
  24.      * @var array $_params 
  25.      */
  26.     var $_params = array();
  27.  
  28.     var $_auth = null;
  29.  
  30.     /**
  31.      * Constructor
  32.      *
  33.      * @param optional array $params  Any parameters needed for this gateway
  34.      *                                 driver.
  35.      */
  36.     function Net_SMS($params = null)
  37.     {
  38.         $this->_params $params;
  39.     }
  40.  
  41.     /**
  42.      * Returns a list of available gateway drivers.
  43.      *
  44.      * @access public
  45.      *
  46.      * @return array  An array of available drivers.
  47.      */
  48.     function getDrivers()
  49.     {
  50.         static $drivers = array();
  51.         if (!empty($drivers)) {
  52.             return $drivers;
  53.         }
  54.  
  55.         $drivers = array();
  56.  
  57.         if ($driver_dir = opendir(dirname(__FILE__'/SMS/')) {
  58.             while (false !== ($file = readdir($driver_dir))) {
  59.                 /* Hide dot files and non .php files. */
  60.                 if (substr($file01!= '.' && substr($file-4== '.php'{
  61.                     $driver = substr($file0-4);
  62.                     $driver_info = Net_SMS::getGatewayInfo($driver);
  63.                     $drivers[$driver$driver_info['name'];
  64.                 }
  65.             }
  66.             closedir($driver_dir);
  67.         }
  68.  
  69.         return $drivers;
  70.     }
  71.  
  72.     /**
  73.      * Returns information on a gateway, such as name and a brief description,
  74.      * from the driver subclass getInfo() function.
  75.      *
  76.      * @access public
  77.      *
  78.      * @return array  An array of extra information.
  79.      */
  80.     function getGatewayInfo($gateway)
  81.     {
  82.         static $info = array();
  83.         if (isset($info[$gateway])) {
  84.             return $info[$gateway];
  85.         }
  86.  
  87.         require_once 'Net/SMS/' $gateway '.php';
  88.         $class 'Net_SMS_' $gateway;
  89.         $info[$gatewaycall_user_func(array($class'getInfo'));
  90.  
  91.         return $info[$gateway];
  92.     }
  93.  
  94.     /**
  95.      * Returns parameters for a gateway from the driver subclass getParams()
  96.      * function.
  97.      *
  98.      * @access public
  99.      *
  100.      * @param string  The name of the gateway driver for which to return the
  101.      *                 parameters.
  102.      *
  103.      * @return array  An array of extra information.
  104.      */
  105.     function getGatewayParams($gateway)
  106.     {
  107.         static $params = array();
  108.         if (isset($params[$gateway])) {
  109.             return $params[$gateway];
  110.         }
  111.  
  112.         require_once 'Net/SMS/' $gateway '.php';
  113.         $class 'Net_SMS_' $gateway;
  114.         $params[$gatewaycall_user_func(array($class'getParams'));
  115.  
  116.         return $params[$gateway];
  117.     }
  118.  
  119.     /**
  120.      * Returns send parameters for a gateway from the driver subclass
  121.      * getDefaultSendParams()function. These are parameters which are available
  122.      * to the user during sending, such as setting a time for delivery, or
  123.      * type of SMS (normal text or flash), or source address, 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.      *                           id   - A unique ID for the message;
  190.      *                           to   - An array of recipients;
  191.      *                           text - The text of the message;
  192.      *                          
  193.      *
  194.      * @return mixed  True on success or PEAR Error on failure.
  195.      */
  196.     function send(&$message)
  197.     {
  198.         /* Authenticate. */
  199.         if (is_a($this->authenticate()'PEAR_Error')) {
  200.             return $this->_auth;
  201.         }
  202.  
  203.         /* Make sure the recipients are in an array. */
  204.         if (!is_array($message['to'])) {
  205.             $message['to'= array($message['to']);
  206.         }
  207.  
  208.         /* Array to store each send. */
  209.         $sends = array();
  210.  
  211.         /* If gateway supports batch sending, preference is given to this
  212.          * method. */
  213.         if ($max_per_batch $this->hasCapability('batch')) {
  214.             /* Split up the recipients in the max recipients per batch as
  215.              * supported by gateway. */
  216.             $iMax count($message['to']);
  217.             $batches ceil($iMax $max_per_batch);
  218.  
  219.             /* Loop through the batches and compose messages to be sent. */
  220.             for ($b = 0; $b $batches$b++{
  221.                 $recipients array_slice($message['to']($b $max_per_batch)$max_per_batch);
  222.                 $response $this->_send($message$recipients);
  223.                 foreach ($recipients as $recipient{
  224.                     if ($response[$recipient][0== 1{
  225.                         /* Message was sent, store remote id. */
  226.                         $remote_id $response[$recipient][1];
  227.                         $error = null;
  228.                     else {
  229.                         /* Message failed, store error code. */
  230.                         $remote_id = null;
  231.                         $error $response[$recipient][1];
  232.                     }
  233.  
  234.                     /* Store the sends. */
  235.                     $sends[= array('message_id' => $message['id'],
  236.                                      'remote_id'  => $remote_id,
  237.                                      'recipient'  => $recipient,
  238.                                      'error'      => $error);
  239.                 }
  240.             }
  241.         else {
  242.             /* No batch sending available, just loop through all recipients
  243.              * and send a message for each one. */
  244.             foreach ($message['to'as $recipient{
  245.                 $response $this->_send($message$recipient);
  246.                 if ($response[0== 1{
  247.                     /* Message was sent, store remote id if any. */
  248.                     $remote_id (isset($response[1]$response[1: null);
  249.                     $error = null;
  250.                 else {
  251.                     /* Message failed, store error code. */
  252.                     $remote_id = null;
  253.                     $error $response[1];
  254.                 }
  255.  
  256.                 /* Store the sends. */
  257.                 $sends[= array('message_id' => $message['id'],
  258.                                  'remote_id'  => $remote_id,
  259.                                  'recipient'  => $recipient,
  260.                                  'error'      => $error);
  261.             }
  262.         }
  263.  
  264.         return $sends;
  265.     }
  266.  
  267.     /**
  268.      * If the current driver has a credit capability, queries the gateway for
  269.      * a credit balance and returns the value.
  270.      *
  271.      * @access public
  272.      *
  273.      * @return int  Value indicating available credit or null if not supported.
  274.      */
  275.     function getBalance()
  276.     {
  277.         /* Authenticate. */
  278.         if (is_a($this->authenticate()'PEAR_Error')) {
  279.             return $this->_auth;
  280.         }
  281.  
  282.         /* Check balance. */
  283.         if ($this->hasCapability('credit')) {
  284.             return $this->_getBalance();
  285.         else {
  286.             return null;
  287.         }
  288.     }
  289.  
  290.     /**
  291.      * Attempts to return a concrete Gateway instance based on $driver.
  292.      *
  293.      * @param string $driver          The type of concrete Gateway subclass to
  294.      *                                 return. This is based on the gateway
  295.      *                                 driver ($driver). The code is dynamically
  296.      *                                 included.
  297.      * @param optional array $params  A hash containing any additional
  298.      *                                 configuration or connection parameters a
  299.      *                                 subclass might need.
  300.      *
  301.      * @return object Net_SMS  The newly created concrete Gateway instance or
  302.      *                          false on 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
  323.      *                                 subclass to return. The is based on the
  324.      *                                 gateway driver ($driver). The code is
  325.      *                                 dynamically included.
  326.      *
  327.      * @param optional array $params  A hash containing any additional
  328.      *                                 configuration or connection parameters a
  329.      *                                 subclass might need.
  330.      *
  331.      * @return mixed  The created concrete Net_SMS instance, or false on
  332.      *                 error.
  333.      */
  334.     function &singleton($driver$params = array())
  335.     {
  336.         static $instances;
  337.         if (!isset($instances)) {
  338.             $instances = array();
  339.         }
  340.  
  341.         $signature serialize(array($driver$params));
  342.         if (!isset($instances[$signature])) {
  343.             $instances[$signature&Net_SMS::factory($driver$params);
  344.         }
  345.  
  346.         return $instances[$signature];
  347.     }
  348.  
  349. }

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