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