Source for file SMSSend_Site.php

Documentation is available at SMSSend_Site.php


1 <?php
2 // $Id$
3
4 // username/email to login with
5 define('SWS_PARAM_USER', 'login_user');
6 // password to login with
7 define('SWS_PARAM_PASS', 'login_pass');
8 // country code of number includes '+'
9 define('SWS_PARAM_CCODE', 'ccode');
10 // country code of number excludes '+'
11 define('SWS_PARAM_CCODE_NP', 'ccode_np');
12 // number to send to
13 define('SWS_PARAM_NUMBER', 'number');
14 // number to send to (excluding '0', if present)
15 define('SWS_PARAM_NUMBER_NZ', 'number_nz');
16 // message to send
17 define('SWS_PARAM_MESSAGE', 'message');
18 // require SMS_Site
19 require_once(SWS_CLASS_DIR.'SMS_Site.php');
20 // require HTTP_Request_Common from HTTP Navigator classes
21 require_once(HTTPNAV_ROOT.'HTTP_Request_Common.php');
22 // require Debug from HTTP Navigator classes
23 require_once(HTTPNAV_ROOT.'Debug.php');
24 /**
25 * SMSSend Site
26 *
27 * This class provides limited support for parsing SMSSend scripts.
28 *
29 * SMS Web Sender requires certain standard parameters be available for all
30 * SMS Sites, therefore, along with the SMSSend script, you will also need to
31 * supply a config file. The config file should map the SMSSend parameters and
32 * error codes to the standard SMS Web Sender ones.
33 *
34 * Please consider this class experimental, I haven't tested it enough so it
35 * might produce odd results. I also haven't looked at the SMSSend source code
36 * properly, so don't expect results identical to SMSSend.
37 * I've been relying on the SMSSend scripting docs:
38 * <http://zekiller.skytech.org/fichiers/smssend/doc_scr_en.html>
39 *
40 * These features are still not supported by this class:
41 *
42 * - NoAdd <Name>
43 * - RTGetForm-<num>-<ind>
44 * - RTSubURL-<num>-<sub>
45 * - RTFollowFrame-<num>-<name>
46 * - RTRegex-<num>-<gpos>-<pos>-<regex>
47 *
48 * @author Keyvan Minoukadeh <keyvan@k1m.com>
49 * @version 2.0
50 */
51 class SMSSend_Site extends SMS_Site
52 {
53 /**
54 * Phase Results
55 *
56 * Array holding instances if HTTP_Response
57 * @var array
58 */
59 var $phase_results;
60
61 /**
62 * Phase cookies
63 *
64 * Array holding cookies created by the SetCookie SMSSend command
65 * @var array
66 */
67 var $phase_cookies;
68
69 /**
70 * Param mapping
71 *
72 * Associative array mapping SMSSend params to the standard SWS ones.
73 * @var array
74 */
75 var $param_mapping;
76
77 /**
78 * Param options
79 *
80 * @var array
81 */
82 var $param_options;
83
84 /**
85 * Param values
86 *
87 * @var array
88 */
89 var $param_values;
90
91 /**
92 * Error mapping
93 *
94 * Associative array mapping SMSSend script error codes to the standard SWS
95 * ones.
96 * @var array
97 */
98 var $error_mapping;
99
100 /**
101 * HTTP Request message
102 *
103 * @var object HTTP_Request
104 */
105 var $http_request;
106
107 /**
108 * Number format
109 *
110 * @var string
111 */
112 var $number_format;
113
114 /**
115 * Search
116 *
117 * @var array
118 */
119 var $search;
120
121 /**
122 * Script
123 *
124 * @var array
125 */
126 var $script;
127
128 function SMSSend_Site()
129 {
130 parent::SMS_Site();
131 $this->phase_results = array();
132 $this->phase_cookies = array();
133 }
134
135 /**
136 * Script exists
137 *
138 * @param string $site_name
139 * @return bool
140 * @static
141 */
142 function script_exists($site_name)
143 {
144 $site_name = strtolower($site_name);
145 return (is_readable(SWS_SMSSEND_SITE_DIR.'config/'.$site_name.'.php') &&
146 is_readable(SWS_SMSSEND_SITE_DIR.$site_name.'.sms'));
147 }
148
149 /**
150 * Send SMS
151 *
152 * @return bool true on success, false on error
153 */
154 function send()
155 {
156 return $this->parse_script();
157 }
158
159 /**
160 * Get number (overrides method in SMS_Site)
161 *
162 * Take into account <var>$number_format</var> specified in the script config
163 * file.
164 */
165 function get_number()
166 {
167 $replace = array(
168 '[ccode]' => $this->get_ccode(),
169 '[ccode_np]' => $this->get_ccode_np(),
170 '[number]' => parent::get_number(),
171 '[number_nz]' => $this->get_number_nz()
172 );
173 return strtr($this->number_format, $replace);
174 }
175
176 /**
177 * Return phase result (HTTP_Response object) from phase <var>$num</var>
178 *
179 * @param int $num default: last result
180 * @return object HTTP_Response object
181 */
182 function &get_phase_result($num=null)
183 {
184 if (!isset($num)) $num = count($this->phase_results);
185 // smssend phases start at 1, our phase array starts at 0
186 $num = $num-1;
187 if (isset($this->phase_results[$num])) {
188 return $this->phase_results[$num];
189 } else {
190 return false;
191 }
192 }
193
194 function get_param_value($param)
195 {
196 $param = strtolower($param);
197 if (isset($this->param_values[$param])) {
198 return $this->param_values[$param];
199 } else {
200 return false;
201 }
202 }
203
204 function param_value_exists($param)
205 {
206 $param = strtolower($param);
207 return (isset($this->param_values[$param]));
208 }
209
210 function param_exists($param)
211 {
212 $param = strtolower($param);
213 return (isset($this->param_mapping[$param]));
214 }
215
216 function get_param($param)
217 {
218 $param = strtolower($param);
219 $method = 'get_'.$this->param_mapping[$param];
220 if (method_exists($this, $method)) {
221 return $this->$method();
222 } else {
223 return false;
224 }
225 }
226
227 function get_param_option($param, $option)
228 {
229 $param = strtolower($param);
230 if (isset($this->param_options[$param][$option])) {
231 return $this->param_options[$param][$option];
232 } else {
233 return false;
234 }
235 }
236
237 /**
238 * Parse SMSSend script
239 *
240 * Opens an SMSSend script and the SMS Web Sender config file for the script
241 * and begins to process it.
242 * @return bool
243 */
244 function parse_script()
245 {
246 // include config file
247 if (!SMSSend_Site::script_exists($this->get_site_name())) {
248 Debug::debug('SMSSend script/config file for "'.$this->get_site_name().'" not readable', __FILE__, __LINE__);
249 $this->set_error('SMSSend script/config file does not exist or is not readable');
250 return false;
251 }
252 $file = SWS_SMSSEND_SITE_DIR.'config/'.$this->get_site_name().'.php';
253 include($file);
254 // set country codes
255 if (isset($country_codes)) $this->country_codes = $country_codes;
256 // set param mapping
257 if (isset($param_mapping)) $this->param_mapping = array_change_key_case($param_mapping, CASE_LOWER);
258 // set error mapping
259 if (isset($error_mapping)) $this->error_mapping = $error_mapping;
260 // set param values
261 if (isset($param_values)) $this->param_values = array_change_key_case($param_values, CASE_LOWER);
262 // set number format
263 if (isset($number_format)) $this->number_format = $number_format;
264
265 // get script to parse
266 $file = SWS_SMSSEND_SITE_DIR.$this->get_site_name().'.sms';
267 $this->script = array_map('trim', file($file));
268 // remove empty lines or comment lines
269 foreach ($this->script as $idx => $line) {
270 if (($line == '') || (substr($line, 0, 1) == '#')) {
271 unset($this->script[$idx]);
272 }
273 }
274
275 // parse params
276 while ($line = array_shift($this->script)) {
277 // extract first char
278 $char = substr($line, 0, 1);
279 // process if param
280 if ($char == '%') {
281 $this->process_param($line);
282 // process if alias
283 } elseif ($char == '$') {
284 // skip for now
285 // $this->process_alias($line);
286 // process if setCookie line
287 } elseif (substr($line, 0, 9) == 'SetCookie') {
288 $this->command_setcookie(substr($line, 10));
289 // process get block
290 } elseif (substr($line, 0, 6) == 'GetURL') {
291 // if process of block not successful, we exit method
292 if (!$this->process_block('GET', substr($line, 7))) {
293 return false;
294 }
295 // process post block
296 } elseif (substr($line, 0, 7) == 'PostURL') {
297 // if process of block not successful, we exit method
298 if (!$this->process_block('POST', substr($line, 8))) {
299 return false;
300 }
301 }
302 }
303 return true;
304 }
305
306 /**
307 * Process a GetURL or PostURL block
308 *
309 * @param string $method (either 'POST' or 'GET')
310 * @param string $url
311 * @return bool
312 */
313 function process_block($method, $url)
314 {
315 $this->reset_block_attributes();
316 $url = $this->replace_params($url);
317 $this->http_request =& call_user_func(array('HTTP_Request_Common', $method), trim($url));
318 $this->http_request->set_protocol('HTTP/1.0');
319 while ($line = array_shift($this->script)) {
320 $command_arg = preg_split('/\s+/', $line, 2);
321 // check for 'GO' command, if found, execute command and exit block
322 if ($command_arg[0] == 'GO') {
323 return $this->command_go();
324 }
325 $method = 'command_'.strtolower($command_arg[0]);
326 // check if method exists for command
327 if (method_exists($this, $method)) {
328 if (isset($command_arg[1])) {
329 $this->$method($command_arg[1]);
330 } else {
331 $this->$method();
332 }
333 }
334 }
335 // error, should not have reached here
336 $this->set_error('Invalid SMSSend script');
337 return false;
338 }
339
340 /**
341 * SMSSend command: Params
342 *
343 * @param string $arg querystring to append to URL
344 */
345 function command_params($arg)
346 {
347 $arg = $this->replace_params($arg);
348 $url = $this->http_request->get_url_string();
349 $url .= '?'.$arg;
350 Debug::debug("Setting URL: $url");
351 $this->http_request->set_url($url);
352 }
353
354 /**
355 * SMSSend command: PostData
356 *
357 * @param string $arg body of POST request
358 */
359 function command_postdata($arg)
360 {
361 $arg = $this->replace_params($arg);
362 Debug::debug("Setting body: $arg");
363 $this->http_request->set_body($arg);
364 }
365
366 /**
367 * SMSSend command: Referer
368 *
369 * @param string $arg referer URL
370 */
371 function command_referer($arg)
372 {
373 $arg = $this->replace_params($arg);
374 $this->http_request->referer($arg);
375 }
376
377 /**
378 * SMSSend command: Search
379 *
380 * <pre>Search <text></pre>
381 * Look for text in the HTTP response. Must be followed be ErrorMsg or
382 * PrintMsg. Only ONE Search per bloc (for more searches, use ElseSearch
383 * after this one).
384 * @param string $arg search text
385 */
386 function command_search($arg)
387 {
388 $idx = count($this->search);
389 $this->search[$idx]['find'] = $arg;
390 // get command for matched search
391 $nextline = array_shift($this->script);
392 $command_arg = preg_split('/\s+/', $nextline, 2);
393 if ($command_arg[0] == 'ErrorMsg') {
394 // seperate error number and message
395 $num_message = preg_split('/\s+/', $command_arg[1], 2);
396 $num = (int)$num_message[0];
397 // check if error number mapped to standard SWS errors, if not
398 // mark error as unknown.
399 if (isset($this->error_mapping[$num])) {
400 $error_num = $this->error_mapping[$num];
401 } else {
402 $error_num = SWS_ERR_UNKNOWN;
403 }
404 $this->search[$idx]['error_num'] = $error_num;
405 $this->search[$idx]['error_msg'] = $num_message[1];
406 } elseif ($command_arg[0] == 'PrintMsg') {
407 $this->search[$idx]['print_msg'] = $command_arg[1];
408 }
409 }
410
411 /**
412 * SMSSend command: ElseSearch (alias to to Search in our case)
413 *
414 * @param string $arg search text
415 */
416 function command_elsesearch($arg)
417 {
418 $this->command_search($arg);
419 }
420
421 /**
422 * SMSSend command: Else (what to do if previous search terms didn't match)
423 */
424 function command_else()
425 {
426 // passing null here tells search we're not looking for anything else
427 $this->command_search(null);
428 }
429
430 /**
431 * SMSSend command: Sleep
432 *
433 * @param string $arg seconds to sleep
434 */
435 function command_sleep($arg)
436 {
437 sleep((int)$arg);
438 }
439
440 /**
441 * SMSSend command: SetCookie <Phase> <Domain>-<Path>-<Name>=<Value>
442 *
443 * @param string $arg
444 */
445 function command_setcookie($arg)
446 {
447 $phase_cookie = explode(' ', $arg, 2);
448 $phase = (int)$phase_cookie[0];
449 $domain_path_nameval = explode('-', $phase_cookie[1], 3);
450 if (count($domain_path_nameval) != 3) return false;
451 $domain = $domain_path_nameval[0];
452 $path = $domain_path_nameval[1];
453 $name_val = explode('=', $domain_path_nameval[2], 2);
454 if (count($name_val) != 2) return false;
455 $name = $name_val[0];
456 $value = $name_val[1];
457 $this->phase_cookies[] = array(
458 'phase' => $phase,
459 'domain' => $domain,
460 'path' => $path,
461 'name' => $name,
462 'value' => $value
463 );
464 }
465
466 /**
467 * SMSSend command: GO
468 *
469 * Submits HTTP Request and returns boolean (true on success, false on fail)
470 * @return bool true if success, false if search match results in error
471 */
472 function command_go()
473 {
474 $cur_phase = count($this->phase_results) + 1;
475 // check for cookies
476 if (count($this->phase_cookies)) {
477 $cookie_jar =& $this->http_client->get_cookie_jar();
478 foreach ($this->phase_cookies as $key => $cookie) {
479 if ($cookie['phase'] == $cur_phase) {
480 Debug::debug("Setting cookie: $cookie[name]=$cookie[value] for domain: $cookie[domain]", __FILE__);
481 $cookie_jar->set_cookie($cookie['domain'], $cookie['path'], $cookie['name'], $cookie['value']);
482 unset($this->phase_cookies[$key]);
483 }
484 }
485 }
486
487 // get phase results
488 $responses = array();
489 $r =& $this->http_client->request($this->http_request);
490 do {
491 $responses[] =& $r;
492 } while ($r =& $r->get_previous());
493 // store phase results (smssend counts auto redirects as individual phases)
494 for ($x=(count($responses)-1); $x >= 0; $x--) {
495 $this->phase_results[] =& $responses[$x];
496 }
497
498 // do searches
499 $response =& $this->get_phase_result();
500 $body =& $response->get_body();
501 while ($search = array_shift($this->search)) {
502 // Process Search/ElseSearch/Else command
503 if (!isset($search['find']) || strpos($body, $search['find']) !== false) {
504 // search matched or Else command encountered
505 if (isset($search['error_msg'])) {
506 Debug::debug("Error: {$search['error_msg']}");
507 $this->set_error($search['error_msg'], $search['error_num']);
508 return false;
509 } elseif (isset($search['print_msg'])) {
510 Debug::debug("Search matched: {$search['print_msg']}");
511 return true;
512 } else {
513 Debug::debug('No ErrorMsg or PrintMsg found after search');
514 $this->set_error('No ErrorMsg or PrintMsg found after search');
515 return false;
516 }
517 }
518 }
519 // assume success
520 return true;
521 }
522
523 /**
524 * Reset block attributes
525 *
526 * Called at the start of each block
527 */
528 function reset_block_attributes()
529 {
530 $this->search = array();
531 }
532
533 /**
534 * Replace params
535 *
536 * @param string $arg
537 * @return string
538 */
539 function replace_params($arg)
540 {
541 return preg_replace_callback('/\\\%([^%]+)%/', array(&$this, '_replace_params_callback'), $arg);
542 }
543
544 function process_param($line)
545 {
546 // get rid of first character (%) and everything after colon ':'
547 // (including the colon itself)
548 $line = substr($line, 1);
549 if ($colon_pos = strpos($line, ':')) {
550 $line = substr($line, 0, $colon_pos);
551 }
552 // split by whitespace
553 $parts = preg_split('/\s+/', $line);
554 // check for options (convert or size=###)
555 if (count($parts) > 1) {
556 // store options
557 $param = strtolower(array_shift($parts));
558 while ($option = strtolower(array_shift($parts))) {
559 if ($option == 'convert') {
560 $this->param_options[$param]['convert'] = true;
561 } elseif (substr($option, 0, 5) == 'size=') {
562 $this->param_options[$param]['size'] = (int)substr($option, 5);
563 }
564 }
565 }
566 }
567
568 function process_alias($line)
569 {
570 // don't yet understand how these are used in smssend, should I be
571 // storing these?
572 }
573
574 /**
575 * SMSSend command: RTURL-<num>
576 *
577 * Returns URL string used in phase <var>$num</var> request
578 * @return string
579 */
580 function dynamic_rturl($num)
581 {
582 if ($response =& $this->get_phase_result((int)$num)) {
583 $request =& $response->get_request();
584 return $request->get_url_string();
585 }
586 return false;
587 }
588
589 /**
590 * SMSSend command: RTParams-<num>
591 *
592 * Returns params string used in phase <var>$num</var> request
593 * @return string
594 */
595 function dynamic_rtparams($num)
596 {
597 if ($response =& $this->get_phase_result((int)$num)) {
598 $request =& $response->get_request();
599 $url =& $request->get_url();
600 return $url->get_query();
601 }
602 return false;
603 }
604
605 /**
606 * SMSSend command: RTHost-<num>
607 *
608 * Returns host used in phase <var>$num</var> request
609 * @return string
610 */
611 function dynamic_rthost($num)
612 {
613 if ($response =& $this->get_phase_result((int)$num)) {
614 $request =& $response->get_request();
615 $url =& $request->get_url();
616 return $url->get_host();
617 }
618 return false;
619 }
620
621 /**
622 * SMSSend command: RTFollowLink-<num>-<name>
623 *
624 * Returns link URL from HTML with name passed in <var>$arg</var>
625 * @return string
626 */
627 function dynamic_rtfollowlink($arg)
628 {
629 $num_linkname = explode('-', $arg, 2);
630 if ($response =& $this->get_phase_result((int)$num_linkname[0])) {
631 $linkname = $num_linkname[1];
632 $body =& $response->get_body();
633 return $this->html_functions->get_link_url($body, $linkname);
634 }
635 return false;
636 }
637
638 /**
639 * SMSSend command: RTGetString-<num>-<textBefore>
640 *
641 * Returns string found just after textBefore (after removing spaces and
642 * string delimiters) searched in the result from phase num.
643 * @return string
644 */
645 function dynamic_rtgetstring($arg)
646 {
647 $num_textbefore = explode('-', $arg, 2);
648 if ($response =& $this->get_phase_result((int)$num_textbefore[0])) {
649 $textbefore = $num_textbefore[1];
650 $body =& $response->get_body();
651 return $this->html_functions->get_string_after($body, $textbefore);
652 }
653 return false;
654 }
655
656 /**
657 * SMSSend command: RTGetInput-<num>-<name>
658 *
659 * Returns value of attribute VALUE from INPUT tag whose NAME attribute value
660 * matches that provided in <name>. Searched in the result from phase <num>
661 * @return string
662 */
663 function dynamic_rtgetinput($arg)
664 {
665 $num_name = explode('-', $arg, 2);
666 if (count($num_name) != 2) return false;
667 if ($response =& $this->get_phase_result((int)$num_name[0])) {
668 $body =& $response->get_body();
669 return $this->html_functions->get_input_value_by_name($body, $num_name[1]);
670 }
671 return false;
672 }
673
674 /**
675 * SMSSend command: RTGetInput2-<num>-<idx>-<flags>
676 *
677 * Returns the fields (defined by flags) of the INPUT tag of index idx
678 * (the first input being 1) searched in the result from phase num
679 * (the first phase being 1, and automatic redirect being counted).
680 * Flags : {a:NAME=VALUE}{n:NAME}{v:VALUE}{t:TYPE}.
681 * @return string
682 */
683 function dynamic_rtgetinput2($arg)
684 {
685 $num_idx_flags = explode('-', $arg, 3);
686 if (count($num_idx_flags) != 3) return false;
687 if ($response =& $this->get_phase_result((int)$num_idx_flags[0])) {
688 $body =& $response->get_body();
689 $idx = (int)$num_idx_flags[1];
690 $flags = $num_idx_flags[2];
691 switch ($flags) {
692 case 'a':
693 return $this->html_functions->get_input_name_value($body, $idx);
694 break;
695 case 'n':
696 return $this->html_functions->get_input_name_by_index($body, $idx);
697 break;
698 case 'v':
699 return $this->html_functions->get_input_value_by_index($body, $idx);
700 break;
701 case 't':
702 return $this->html_functions->get_input_type_by_index($body, $idx);
703 break;
704 }
705 }
706 return false;
707 }
708
709 /**
710 * SMSSend command: RTGetInput3-<num>-<ind>-<idx>
711 *
712 * Returns the NAME and VALUE fields of the INPUT tag of index idx
713 * (the first input being 1) of the form ind (the first form being 0)
714 * searched in the result from phase num (the first phase being 1, and
715 * automatic redirect being counted).
716 * @return string
717 */
718 function dynamic_rtgetinput3($arg)
719 {
720 $num_ind_idx = explode('-', $arg, 3);
721 if (count($num_ind_idx) != 3) return false;
722 if ($response =& $this->get_phase_result((int)$num_ind_idx[0])) {
723 $body =& $response->get_body();
724 $ind = (int)$num_ind_idx[1];
725 $idx = (int)$num_ind_idx[2];
726 return $this->html_functions->get_input_name_value($body, $idx, $ind+1);
727 }
728 return false;
729 }
730
731 /**
732 * SMSSend command: RTFormAction-<num>-<ind>
733 *
734 * Returns the ACTION field of the form ind (the first form being 0)
735 * searched in the result from phase num (the first phase being 1, and
736 * automatic redirect being counted).
737 * @return string
738 */
739 function dynamic_rtformaction($arg)
740 {
741 $num_ind = explode('-', $arg, 2);
742 if (count($num_ind) != 2) return false;
743 if ($response =& $this->get_phase_result((int)$num_ind[0])) {
744 $ind = (int)$num_ind[1];
745 $body =& $response->get_body();
746 return $this->html_functions->get_form_action_by_index($body, $ind+1);
747 }
748 return false;
749 }
750
751 // private methods
752
753 function _replace_params_callback($match)
754 {
755 $param = $match[1];
756
757 // replace with value
758 if ($this->param_value_exists($param)) {
759 return $this->get_param_value($param);
760 }
761
762 // use param mappings
763 if ($this->param_exists($param)) {
764 $value = $this->get_param($param);
765 if ($size = $this->get_param_option($param, 'size')) {
766 $value = substr($value, 0, (int)$size);
767 }
768 if ($this->get_param_option($param, 'convert')) {
769 $value = urlencode($value);
770 }
771 return $value;
772 }
773
774 // check for dynamic replacement
775 if (substr($param, 0, 2) == 'RT') {
776 $param_arg = explode('-', $param, 2);
777 if (count($param_arg) > 1) {
778 $method = 'dynamic_'.strtolower($param_arg[0]);
779 if (method_exists($this, $method)) {
780 return $this->$method($param_arg[1]);
781 }
782 }
783 }
784 return '';
785 }
786 }
787 ?>

Documention generated on Sat, 19 Jul 2003 03:43:25 +0100 by phpDocumentor 1.2.0