Documentation is available at SMS_Web_Sender.php
1 <?php
2 // $Id$
3
4 // main SMS Web Sender classes
5 if (!defined('SWS_CLASS_DIR')) {
6 define('SWS_CLASS_DIR', dirname(__FILE__).'/');
7 }
8 // SMS Web Sender site classes
9 if (!defined('SWS_SITE_DIR')) {
10 define('SWS_SITE_DIR', realpath(SWS_CLASS_DIR.'../sites').'/');
11 }
12 // SMSSend scripts
13 if (!defined('SWS_SMSSEND_SITE_DIR')) {
14 define('SWS_SMSSEND_SITE_DIR', realpath(SWS_CLASS_DIR.'../smssend_sites').'/');
15 }
16 // include User_Agent from HTTP Navigator classes
17 require_once(HTTPNAV_ROOT.'User_Agent.php');
18 // include SMS_Site class
19 require_once(SWS_CLASS_DIR.'SMS_Site.php');
20 // Error constants
21 define('SWS_ERR_LOGIN', 1);
22 define('SWS_ERR_QUOTA', 2);
23 define('SWS_ERR_SEND', 3);
24 define('SWS_ERR_UNKNOWN', 4);
25 /**
26 * SMS Web Sender
27 *
28 * Instance of this class should be used to handle SMS requests.
29 *
30 * @author Keyvan Minoukadeh <keyvan@k1m.com>
31 * @version 2.0
32 */
33 class SMS_Web_Sender
34 {
35 /**
36 * HTTP client (instance of User_Agent from HTTP Navigator)
37 * @var object User_Agent
38 */
39 var $http_client;
40
41 /**
42 * SMS Sites
43 * @var array
44 */
45 var $sms_sites;
46
47 /**
48 * Current site
49 * @var object
50 */
51 var $current_site;
52
53 /**
54 * Allow SMSSend scripts
55 * @var bool
56 */
57 var $allow_smssend;
58
59 function SMS_Web_Sender()
60 {
61 $this->set_allow_smssend(true);
62 $this->sms_sites = array();
63 // for array shuffle
64 srand((float)microtime()*1000000);
65 // set up HTTP client
66 $options = array(
67 'agent' => 'Mozilla/6.0 (compatible; MSIE 5.01; Windows NT)',
68 'protocols_allowed' => array('HTTP', 'HTTPS'),
69 'cookie_jar' => true,
70 'http_version' => '1.0',
71 'lax_redirect' => true,
72 'max_size' => 35*1024,
73 'keep_alive' => 0,
74 'timeout' => 8,
75 'timeout_rw' => 10,
76 'gzip_support' => true,
77 'scheme_implementor' => array('HTTP'=>'Protocol_HTTP', 'HTTPS'=>'Protocol_CURL')
78 );
79 $ua =& new User_Agent($options);
80 $this->set_http_client($ua);
81 }
82
83 /**
84 * Set allow SMSSend
85 *
86 * If enabled add_site() and get_site_names() will also look in the
87 * smssend_sites folder to find sites.
88 *
89 * @param bool $allow pass true to enable, false to disable
90 */
91 function set_allow_smssend($allow)
92 {
93 $this->allow_smssend = $allow;
94 }
95
96 /**
97 * Get allow SMSSend
98 *
99 * @return bool
100 */
101 function get_allow_smssend()
102 {
103 return $this->allow_smssend;
104 }
105
106 /**
107 * Set HTTP client
108 * @param object $ua User_Agent object
109 */
110 function set_http_client(&$ua)
111 {
112 $this->http_client =& $ua;
113 }
114
115 /**
116 * Get HTTP client
117 * @return object User_Agent object
118 */
119 function &get_http_client()
120 {
121 return $this->http_client;
122 }
123
124 /**
125 * Get site names
126 *
127 * Returns indexed array holding site names available for SMS_Web_Sender to
128 * use.
129 * @return array
130 */
131 function get_site_names()
132 {
133 $sites = array();
134 // Custom ones
135 $dir = dir(SWS_SITE_DIR);
136 while (false !== ($file = $dir->read())) {
137 if (($file == '.') || ($file == '..')) continue;
138 $file = basename($file, '.php');
139 $prefix = 'SMS_Site_';
140 if (substr($file, 0, count($prefix)) == $prefix) {
141 $file = substr($file, count($prefix));
142 $sites[] = $file;
143 }
144 }
145 $dir->close();
146 // SMSSend ones
147 if ($this->allow_smssend) {
148 require_once(SWS_CLASS_DIR.'SMSSend_Site.php');
149 $dir = dir(SWS_SMSSEND_SITE_DIR);
150 while (false !== ($file = $dir->read())) {
151 if (($file == '.') || ($file == '..') || !strpos($file, '.')) continue;
152 $site = basename($file, '.sms');
153 if (SMSSend_Site::script_exists($site)) {
154 $sites[] = $site;
155 }
156 }
157 $dir->close();
158 }
159 sort($sites);
160 return array_unique($sites);
161 }
162
163 /**
164 * Send SMS
165 *
166 * This method should be called only after some sites have been added to the
167 * SMS_Web_Sender object.
168 *
169 * Example:
170 * <code>
171 * // create instance of SMS_Web_Sender
172 * $sws =& new SMS_Web_Sender();
173 * // add a site, in this example 'asite' must be a valid site our
174 * // SMS_Web_Sender object can locate, see documentation for more on this.
175 * $sws->add_site('asite', 'username', 'pass');
176 * // Send message
177 * if ($sws->send('+44', '07977777777', 'Hello, this is my message')) {
178 * echo 'Success, your message has been sent';
179 * } else {
180 * echo 'Sorry, send failed, reason: ', $sws->get_error();
181 * }
182 * </code>
183 *
184 * @param string $ccode country code
185 * @param string $number number to send (including leading '0' but excluding
186 * country code)
187 * @param string $message message
188 * @return bool returns true on successful send, false on error
189 */
190 function send($ccode, $number, $message)
191 {
192 // no sites
193 if (!count($this->sms_sites)) return false;
194
195 // shuffle
196 shuffle($this->sms_sites);
197
198 // copy site keys
199 $site_keys = array_keys($this->sms_sites);
200
201 do {
202 // return false if no more sites in array
203 if (!count($site_keys)) return false;
204 // get current index
205 $index = current($site_keys);
206 // pick first one
207 $site =& $this->sms_sites[$index];
208 unset($site_keys[key($site_keys)]);
209 // move onto the next site if country not supported
210 if (!$site->supports_country($ccode)) {
211 Debug::debug('Site '.$site->get_site_name().' does not support country code "'.$ccode.'", skipping');
212 // try next site
213 continue;
214 }
215 // set as current site
216 $this->current_site =& $site;
217 // set HTTP client
218 $site->set_http_client($this->http_client);
219 // set country code and number
220 $site->set_ccode($ccode);
221 $site->set_number($number);
222 // set message
223 $site->set_message($message);
224 // attempt send
225 Debug::debug('Trying site: '.$site->get_site_name());
226 } while (!$site->send());
227 // success
228 return true;
229 }
230
231 /**
232 * Site class exists
233 *
234 * @param string $site_name
235 * @return bool returns true if <var>$site_name</var> is a valid class, false
236 * otherwise.
237 */
238 function site_class_exists($site_name)
239 {
240 $site_name = strtolower($site_name);
241 return is_readable(SWS_SITE_DIR.'SMS_Site_'.$site_name.'.php');
242 }
243
244 /**
245 * Add sites
246 *
247 * <var>$sites</var> should be an indexed array holding associative arrays,
248 * Example:
249 * <code>
250 * $sites = array();
251 * $sites[] = array('name'=>'sitename', 'user'=>'login_user', 'pass'=>'login_pass');
252 * $sites[] = array('name'=>'site2', 'user'=>'user2', 'pass'=>'pass2');
253 * $sws->add_sites($sites);
254 * </code>
255 * This is equivalent to:
256 * <code>
257 * $sws->add_site('sitename', 'login_user', 'login_pass');
258 * $sws->add_site('site2', 'user2', 'pass2');
259 * </code>
260 * The last method is preferred as add_site() returns true/false to indicate
261 * if site has been added successfully.
262 *
263 * @param array $sites indexed array holding associative arrays
264 * @see add_site()
265 */
266 function add_sites($sites)
267 {
268 foreach ($sites as $site) {
269 $site_name = $site['name'];
270 $user = (isset($site['user']) ? $site['user'] : null);
271 $pass = (isset($site['pass']) ? $site['pass'] : null);
272 $this->add_site($site_name, $user, $pass);
273 }
274 }
275
276 /**
277 * Add site
278 *
279 * Example:
280 * <code>
281 * $sws->add_site('sitename', 'login_user', 'login_pass');
282 * $sws->add_site('site2', 'user2', 'pass2');
283 * </code>
284 *
285 * @param string $site_name
286 * @param string $user
287 * @param string $pass
288 * @return bool returns true if <var>$site_name</var> was successfully added,
289 * false otherwise.
290 */
291 function add_site($site_name, $user=null, $pass=null)
292 {
293 $site_name = strtolower($site_name);
294 // check for custom site class
295 if ($this->site_class_exists($site_name)) {
296 $class = "SMS_Site_$site_name";
297 require_once(SWS_SITE_DIR.$class.'.php');
298 $site =& new $class();
299 // check for smssend script (if allowed)
300 } elseif ($this->allow_smssend) {
301 $class = 'SMSSend_Site';
302 require_once(SWS_CLASS_DIR.$class.'.php');
303 if (SMSSend_Site::script_exists($site_name)) {
304 $site =& new $class();
305 }
306 }
307 if (!isset($site)) return false;
308 // set site name
309 $site->set_site_name($site_name);
310 // set login details
311 $site->set_login($user, $pass);
312 // add site to list and return
313 return $this->add_site_object($site);
314 }
315
316 /**
317 * Add site object
318 *
319 * This method should be used if the site you want to add is not available
320 * in one of the relevant site folders, or if the site object should be
321 * configured in a special way before being used.
322 *
323 * Note:
324 * - <var>$site</var> must be a subclass of SMS_Site, otherwise it won't be
325 * added.
326 * - Make sure you call the follwing methods before adding <var>$site</var>
327 * to SMS_Web_Sender: set_site_name() to set the sitename, set_login() to
328 * set login details (if required by <var>$site</var>).
329 *
330 * @param object $site must be a subclass of SMS_Site
331 * @return true returns true if <var>$site</var> has been added successfully,
332 * false otherwise.
333 * @see add_site()
334 */
335 function add_site_object(&$site)
336 {
337 if (is_object($site) && is_subclass_of($site, 'sms_site')) {
338 $this->sms_sites[] =& $site;
339 return true;
340 }
341 return false;
342 }
343
344 /**
345 * Get error
346 *
347 * This method should be called if send() fails. The last site to have
348 * attempted the send should hold a suitable error message which will be
349 * returned by this method.
350 *
351 * @return string
352 */
353 function get_error()
354 {
355 return $this->current_site->get_error();
356 }
357
358 /**
359 * Get error number
360 *
361 * This method should be called if send() fails. The last site to have
362 * attempted the send should hold a suitable error number which will be
363 * returned by this method.
364 * To check the returned error number, you can use the SMS Web Sender error
365 * constants. Example:
366 * <code>
367 * $error = $sws->get_error_number();
368 * if ($error == SWS_ERR_QUOTA) {
369 * // not enough quota to send
370 * }
371 * </code>
372 * Usually the error message produced by get_error() will be fine to display
373 * to users, but to determine the reason in code you can use this method and
374 * compare against the error constants.
375 *
376 * Note: this method will always return one of the SWS_ERR_* constants, if
377 * the error number is unrecognised SWS_ERR_UNKNOWN will be returned. So only
378 * call this when you know there's been an error.
379 *
380 * @return int
381 */
382 function get_error_number()
383 {
384 $errno = $this->current_site->get_error_number();
385 switch ($errno) {
386 case SWS_ERR_LOGIN:
387 case SWS_ERR_QUOTA:
388 case SWS_ERR_SEND:
389 return $errno;
390 break;
391 default:
392 return SWS_ERR_UNKNOWN;
393 }
394 }
395 }
396 ?>
Documention generated on Sat, 19 Jul 2003 03:43:29 +0100 by phpDocumentor 1.2.0