summaryrefslogtreecommitdiff
path: root/includes/browser.inc
blob: a6c4219d2eef5aa1b271af1517ba8af3faba0efc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
<?php
// $Id$

/**
 * @file
 * Browser API class.
 */

/**
 * @defgroup browser Browser
 * @{
 * Provides a powerful text based browser through a class based API.
 * The browser supports two HTTP backends natively: 1) PHP streams, and
 * 2) curl. The browser also supports arbitrary HTTP request types in addtion
 * to GET and POST, given that the backend supports them.
 *
 * The browser can be used to make a simple GET request to example.com as
 * shown below.
 * @code
 *   $browser = new Browser();
 *   $browser->get('http://example.com');
 * @endcode
 * The result of the GET request can be accessed in two ways: 1) the get()
 * method returns an array defining the result of the request, or 2) the
 * individual properties can be accessed from the browser instance via their
 * respective access methods. The following demonstrates the properties that
 * are avaialable and how to access them.
 * @code
 *   $browser->getUrl();
 *   $browser->getResponseHeaders();
 *   $browser->getContent();
 * @endcode
 *
 * When performing a POST request the following format is used.
 * @code
 *   $browser = new Browser();
 *   $post = array(
 *     'field_name1' => 'foo',
 *     'checkbox1' => TRUE,
 *     'multipleselect1[]' => array(
 *       'value1',
 *       'value2',
 *     ),
 *   );
 *   $browser->post('http://example.com/form', $post, 'Submit button text');
 * @endcode
 * To submit a multi-step form or to post to the current page the URL passed to
 * post() may be set to NULL. If there were two steps on the form shown in the
 * example above with the mutliple select field on the second page and a submit
 * button with the title "Next" on the first page the code be as follows.
 * @code
 *   $browser = new Browser();
 *   $post = array(
 *     'field_name1' => 'foo',
 *     'checkbox1' => TRUE,
 *   );
 *   $browser->post('http://example.com/form', $post, 'Next');
 *
 *   $post = array(
 *     'multipleselect1[]' => array(
 *       'value1',
 *       'value2',
 *     ),
 *   );
 *   $browser->post(NULL, $post, 'Final');
 * @endcode
 */

/**
 * Browser API class.
 *
 * All browser functionality is provided by this main class which manages the
 * various aspects of the browser.
 */
class Browser {

  /**
   * Flag indicating if curl is available.
   *
   * @var boolean
   */
  protected $curl;

  /**
   * The handle of the current curl connection.
   *
   * @var resource
   */
  protected $handle;

  /**
   * The current cookie file used by curl.
   *
   * Cookies are not reused so they can be stored in memory instead of a file.
   *
   * @var mixed
   */
  protected $cookieFile = NULL;

  /**
   * The request headers.
   *
   * @var array
   */
  protected $requestHeaders = array();

  /**
   * The URL of the current page.
   *
   * @var string
   */
  protected $url;

  /**
   * The response headers of the current page.
   *
   * @var Array
   */
  protected $headers = array();

  /**
   * The raw content of the current page.
   *
   * @var string
   */
  protected $content;

  /**
   * The BrowserPage class representing to the current page.
   *
   * @var BrowserPage
   */
  protected $page;

  /**
   * Initialize the browser.
   *
   * @param $force_stream
   *   Force the use of the PHP stream wrappers insead of CURL. This is used
   *   during testing to force the use of the stream wrapper so it can be
   *   tested.
   */
  public function __construct($force_stream = FALSE) {
    $this->curl = $force_stream ? FALSE : function_exists('curl_init');
    $this->setUserAgent('Drupal (+http://drupal.org/)');

    if ($this->curl) {
      $this->handle = curl_init();
      curl_setopt_array($this->handle, $this->curlOptions());
    }
    else {
      $this->handle = stream_context_create();
    }
  }

  /**
   * Check the the method is supported by the backend.
   *
   * @param $method
   *   The method string identifier.
   */
  public function isMethodSupported($method) {
    return $method == 'GET' || $method == 'POST';
  }

  /**
   * Get the request headers.
   *
   * The request headers are sent in every request made by the browser with a
   * few changes made the the individual request methods.
   *
   * @return
   *   Associative array of request headers.
   */
  public function getRequestHeaders() {
    return $this->requestHeaders;
  }

  /**
   * Set the request headers.
   *
   * @param $headers
   *   Associative array of request headers.
   */
  public function setRequestHeaders(array $headers) {
    $this->requestHeaders = $headers;
  }

  /**
   * Get the user-agent that the browser is identifying itself as.
   *
   * @return
   *   Browser user-agent.
   */
  public function getUserAgent() {
    return $this->requestHeaders['User-Agent'];
  }

  /**
   * Set the user-agent that the browser will identify itself as.
   *
   * @param $agent
   *   User-agent to to identify as.
   */
  public function setUserAgent($agent) {
    $this->requestHeaders['User-Agent'] = $agent;
  }

  /**
   * Get the URL of the current page.
   *
   * @return
   *   The URL of the current page.
   */
  public function getUrl() {
    return $this->url;
  }

  /**
   * Get the response headers of the current page.
   *
   * @return
   *   The response headers of the current page.
   */
  public function getResponseHeaders() {
    return $this->headers;
  }

  /**
   * Get the raw content of the current page.
   *
   * @return
   *   The raw content for the current page.
   */
  public function getContent() {
    return $this->content;
  }

  /**
   * Get the BrowserPage instance for the current page.
   *
   * If the raw content is new and the page has not yet been parsed then parse
   * the content and ensure that it is valid.
   *
   * @return
   *   BrowserPage instance for the current page.
   */
  public function getPage() {
    if (!isset($this->page)) {
      $this->page = new BrowserPage($this->url, $this->headers, $this->content);
    }
    return $this->page;
  }

  /**
   * Get the current state of the browser.
   *
   * @return
   *   An associative array containing state information, including: 1) url, 2)
   *   headers, 3) content.
   * @see getUrl()
   * @see getResponseHeaders()
   * @see getContent()
   */
  public function getState() {
    return array(
      'url' => $this->url,
      'headers' => $this->headers,
      'content' => $this->content,
    );
  }

  /**
   * Set the state of the browser.
   *
   * @param $url
   *   The URL of the current page.
   * @param $headers
   *   The response headers of the current page.
   * @param $content
   *   The raw content of the current page.
   */
  public function setState($url, $headers, $content) {
    $this->url = $url;
    $this->headers = $headers;
    $this->content = $content;

    // Clear the page variable since the content has change.
    unset($this->page);

    $this->refreshCheck();
  }

  /**
   * Perform a GET request.
   *
   * @param $url
   *   Absolute URL to request.
   * @return
   *   Associative array of state information, as returned by getState().
   * @see getState().
   */
  public function get($url) {
    if ($this->curl) {
      $this->curlExecute(array(
        CURLOPT_HTTPGET => TRUE,
        CURLOPT_URL => $url,
        CURLOPT_NOBODY => FALSE,
      ));
    }
    else {
      $this->streamExecute($url, array(
        'method' => 'GET',
        'header'  => array(
          'Content-Type' => 'application/x-www-form-urlencoded',
        ),
      ));
    }

    $this->refreshCheck();

    return $this->getState();
  }

  /**
   * Perform a POST request.
   *
   * @param $url
   *   Absolute URL to request, or NULL to submit the current page.
   * @param $fields
   *   Associative array of fields to submit as POST variables.
   * @param $submit
   *   Text contained in 'value' properly of submit button of which to press.
   * @return
   *   Associative array of state information, as returned by
   *   browser_state_get().
   * @see browser_state_get()
   */
  public function post($url, array $fields, $submit) {
    // If URL is set then request the page, otherwise use the current page.
    if ($url) {
      $this->get($url);
    }
    else {
      $url = $this->url;
    }

    if (($page = $this->getPage()) === FALSE) {
      return FALSE;
    }

    if (($form = $this->findForm($fields, $submit)) === FALSE) {
      return FALSE;
    }

    // If form specified action then use that for the post url.
    if ($form['action']) {
      $url = $page->getAbsoluteUrl($form['action']);
    }

    if ($this->curl) {
      $this->curlExecute(array(
        CURLOPT_POST => TRUE,
        CURLOPT_URL => $url,
        CURLOPT_POSTFIELDS => http_build_query($form['post'], NULL, '&'),
      ));
    }
    else {
      $this->streamExecute($url, array(
        'method'  => 'POST',
        'header'  => array(
          'Content-Type' => 'application/x-www-form-urlencoded',
        ),
        'content' => http_build_query($form['post'], NULL, '&'),
      ));
    }

    $this->refreshCheck();

    return $this->getState();
  }

  /**
   * Find the the form that patches the conditions.
   *
   * @param $fields
   *   Associative array of fields to submit as POST variables.
   * @param $submit
   *   Text contained in 'value' properly of submit button of which to press.
   * @return
   *   Form action and the complete post array containing default values if not
   *   overridden, or FALSE if no form matching the conditions was found.
   */
  protected function findForm(array $fields, $submit) {
    $page = $this->getPage();

    $forms = $page->getForms();
    foreach ($forms as $form) {
      if (($post = $this->processForm($form, $fields, $submit)) !== FALSE) {
        $action = (isset($form['action']) ? (string) $form['action'] : FALSE);
        return array(
          'action' => $action,
          'post' => $post,
        );
      }
    }
    return FALSE;
  }

  /**
   * Check the conditions against the specified form and process values.
   *
   * @param $form
   *   Form SimpleXMLElement object.
   * @param $fields
   *   Associative array of fields to submit as POST variables.
   * @param $submit
   *   Text contained in 'value' properly of submit button of which to press.
   * @return
   *   The complete post array containing default values if not overridden, or
   *   FALSE if no form matching the conditions was found.
   */
  protected function processForm($form, $fields, $submit) {
    $page = $this->getPage();

    $post = array();
    $submit_found = FALSE;
    $inputs = $page->getInputs($form);
    foreach ($inputs as $input) {
      $name = (string) $input['name'];
      $html_value = isset($input['value']) ? (string) $input['value'] : '';

      // Get type from input vs textarea and select.
      $type = isset($input['type']) ? (string) $input['type'] : $input->getName();

      if (isset($fields[$name])) {
        if ($type == 'file') {
          // Make sure the file path is the absolute path.
          $file = realpath($fields[$name]);
          if ($file && is_file($file)) {
            // Signify that the post field is a file in case backend needs to
            // perform additional processing.
            $post[$name] = '@' . $file;
          }
          // Known type, field processed.
          unset($fields[$name]);
        }
        elseif (($processed_value = $this->processField($input, $type, $fields[$name], $html_value)) !== NULL) {
          // Value may be ommitted (checkbox).
          if ($processed_value !== FALSE) {
            if (is_array($processed_value)) {
              $post += $processed_value;
            }
            else {
              $post[$name] = $processed_value;
            }
          }
          // Known type, field processed.
          unset($fields[$name]);
        }
      }

      // No post value for the field means that: no post field value specified,
      // the value does not match the field (checkbox, radio, select), or the
      // field is of an unknown type.
      if (!isset($post[$name])) {
        // No value specified so use default value (value in HTML).
        if (($default_value = $this->getDefaultFieldValue($input, $type, $html_value)) !== NULL) {
          $post[$name] = $default_value;
          unset($fields[$name]);
        }
      }

      // Check if the
      if (($type == 'submit' || $type == 'image') && $submit == $html_value) {
        $post[$name] = $html_value;
        $submit_found = TRUE;
      }
    }

    if ($submit_found) {
      return $post;
    }
    return FALSE;
  }

  /**
   * Get the value to be sent for the specified field.
   *
   * @param $input
   *   Input SimpleXMLElement object.
   * @param $type
   *   Input type: text, textarea, password, radio, checkbox, or select.
   * @param $new_value
   *   The new value to be assigned to the input.
   * @param $html_value
   *   The cleaned default value for the input from the HTML value.
   */
  protected function processField($input, $type, $new_value, $html_value) {
    switch ($type) {
      case 'text':
      case 'textarea':
      case 'password':
        return $new_value;
      case 'radio':
        if ($new_value == $html_value) {
          return $new_value;
        }
        return NULL;
      case 'checkbox':
        // If $new_value is set to FALSE then ommit checkbox value, otherwise
        // pass original value.
        if ($new_value === FALSE) {
          return FALSE;
        }
        return $html_value;
      case 'select':
        // Remove the ending [] from multi-select element name.
        $key = preg_replace('/\[\]$/', '', (string) $input['name']);

        $options = $page->getSelectOptions($input);
        $index = 0;
        $out = array();
        foreach ($options as $value => $text) {
          if (is_array($value)) {
            if (in_array($value, $new_value)) {
              $out[$key . '[' . $index++ . ']'] = $value;
            }
          }
          elseif ($new_value == $value) {
            return $new_value;
          }
        }
        return ($out ? $out : NULL);
      default:
        return NULL;
    }
  }

  /**
   * Get the cleaned default value for the input from the HTML value.
   *
   * @param $input
   *   Input SimpleXMLElement object.
   * @param $type
   *   Input type: text, textarea, password, radio, checkbox, or select.
   * @param $html_value
   *   The default value for the input, as specified in the HTML.
   */
  protected function getDefaultFieldValue($input, $type, $html_value) {
    switch ($type) {
      case 'textarea':
        return (string) $input;
      case 'select':
        // Remove the ending [] from multi-select element name.
        $key = preg_replace('/\[\]$/', '', (string) $input['name']);
        $single = empty($input['multiple']);

        $options = $page->getSelectOptionElements($input);
        $first = TRUE;
        $index = 0;
        $out = array();
        foreach ($options as $option) {
          // For single select, we load the first option, if there is a
          // selected option that will overwrite it later.
          if ($option['selected'] || ($first && $single)) {
            $first = FALSE;
            if ($single) {
              $out[$key] = (string) $option['value'];
            }
            else {
              $out[$key . '[' . $index++ . ']'] = (string) $option['value'];
            }
          }
          return ($single ? $out[$key] : $out);
        }
        break;
      case 'file':
        return NULL;
      case 'radio':
      case 'checkbox':
        if (!isset($input['checked'])) {
          return NULL;
        }
        // Deliberately no break.
      default:
        return $html_value;
    }
  }

  /**
   * Perform a request of arbitrary type.
   *
   * Please use get() and post() for GET and POST requests respectively.
   *
   * @param $method
   *   The method string identifier.
   * @param $url
   *   Absolute URL to request.
   * @param $additional
   *   Additional parameters related to the particular request method.
   * @return
   *   Associative array of state information, as returned by getState().
   * @see getState().
   */
  public function request($method, $url, array $additional) {
    if (!$this->isMethodSupported($method)) {
      return FALSE;
    }

    // TODO
  }

  /**
   * Perform the request using the PHP stream wrapper.
   *
   * @param $url
   *   The url to request.
   * @param $options
   *   The HTTP stream context options to be passed to
   *   stream_context_set_params().
   */
  protected function streamExecute($url, array $options) {
    // Global variable provided by PHP stream wapper.
    global $http_response_header;

    if (!isset($options['header'])) {
      $options['header'] = array();
    }

    // Merge default request headers with the passed headers and generate
    // header string to be sent in http request.
    $headers = $this->requestHeaders + $options['header'];
    $options['header'] = $this->headerString($headers);

    // Update the handler options.
    stream_context_set_params($this->handle, array(
      'options' => array(
        'http' => $options,
      )
    ));

    // Make the request.
    $this->content = file_get_contents($url, FALSE, $this->handle);
    $this->url = $url;
    $this->headers = $this->headerParseAll($http_response_header);
    unset($this->page);
  }


  /**
   * Perform curl_exec() with the specified option changes.
   *
   * @param $options
   *   Curl options to set, any options not set will maintain their previous
   *   value.
   */
  function curlExecute(array $options) {
    // Headers need to be reset since callback appends.
    $this->headers = array();

    // Ensure that request headers are up to date.
    curl_setopt($this->handle, CURLOPT_USERAGENT, $this->requestHeaders['User-Agent']);
    curl_setopt($this->handle, CURLOPT_HTTPHEADER, $this->requestHeaders);

    curl_setopt_array($this->handle, $options);
    $this->content = curl_exec($this->handle);
    $this->url = curl_getinfo($this->handle, CURLINFO_EFFECTIVE_URL);

    // $this->headers should be filled by $this->curlHeaderCallback().
    unset($this->page);
  }

  /**
   * Get the default curl options to be used with each request.
   *
   * @return
   *   Default curl options.
   */
  protected function curlOptions() {
    return array(
      CURLOPT_COOKIEJAR => $this->cookieFile,
      CURLOPT_FOLLOWLOCATION => TRUE,
      CURLOPT_HEADERFUNCTION => array($this, 'curlHeaderCallback'),
      CURLOPT_HTTPHEADER => $this->requestHeaders,
      CURLOPT_RETURNTRANSFER => TRUE,
      CURLOPT_SSL_VERIFYPEER => FALSE,
      CURLOPT_SSL_VERIFYHOST => FALSE,
      CURLOPT_URL => '/',
      CURLOPT_USERAGENT => $this->requestHeaders['User-Agent'],
    );
  }

  /**
   * Reads reponse headers and stores in $headers array.
   *
   * @param $curlHandler
   *   The curl handler.
   * @param $header
   *   An header.
   * @return
   *   The string length of the header. (required by curl)
   */
  protected function curlHeaderCallback($handler, $header) {
    // Ignore blank header lines.
    $clean_header = trim($header);
    if ($clean_header) {
      $this->headers += $this->headerParse($clean_header);
    }

    // Curl requires strlen() to be returned.
    return strlen($header);
  }

  /**
   * Generate a header string given he associative array of headers.
   *
   * @param $headers
   *   Associative array of headers.
   * @return
   *   Header string to be used with stream.
   */
  protected function headerString(array $headers) {
    $string = '';
    foreach ($headers as $key => $header) {
      $string .= "$key: $header\r\n";
    }
    return $string;
  }

  /**
   * Parse the response header array to create an associative array.
   *
   * @param $headers
   *   Array of headers.
   * @return
   *   An associative array of headers.
   */
  protected function headerParseAll(array $headers) {
    $out = array();
    foreach ($headers as $header) {
      $out += $this->headerParse($header);
    }
    return $out;
  }

  /**
   * Parse an individual header into name and value.
   *
   * @param $header
   *   A string header string.
   * @return
   *   Parsed header as array($name => $value), or array() if parse failed.
   */
  protected function headerParse($header) {
    $parts = explode(':', $header, 2);

    // Ensure header line is valid.
    if (count($parts) == 2) {
      $name = $this->headerName(trim($parts[0]));
      return array($name => trim($parts[1]));
    }
    return array();
  }

  /**
   * Ensure that header name is formatted with all lowercase letters.
   *
   * @param $name
   *   Header name to format.
   * @return
   *   Formatted header name.
   */
  protected function headerName($name) {
    return strtolower($name);
  }

  /**
   * Check for a refresh signifier.
   *
   * A refresh signifier can either be the 'Location' HTTP header or the meta
   * tag 'http-equiv="Refresh"'.
   */
  protected function refreshCheck() {
    // If not handled by backend wrapper then go ahead and handle.
    if (isset($this->headers['Location'])) {
      // Expect absolute URL.
      $this->get($this->headers['Location']);
    }

    if (($page = $this->getPage()) !== FALSE && ($tag = $page->getMetaTag('Refresh', 'http-equiv'))) {
      // Parse the content attribute of the meta tag for the format:
      // "[delay]: URL=[path_to_redirect_to]".
      if (preg_match('/\d+;\s*URL=(?P<url>.*)/i', $tag['content'], $match)) {
        $this->get($page->getAbsoluteUrl(decode_entities($match['url'])));
      }
    }
  }

  /**
   * Close the wrapper connection.
   */
  function __destruct() {
    if (isset($this->handle)) {
      if ($this->curl) {
        curl_close($this->handle);
      }
      unset($this->handle);
    }
  }
}


/**
 * Represents a page of content that has been fetched by the Browser. The class
 * provides a number of convenience methods that relate to page content.
 */
class BrowserPage {

  /**
   * The URL of the page.
   *
   * @var string
   */
  protected $url;

  /**
   * The response headers of the  page.
   *
   * @var Array
   */
  protected $headers;

  /**
   * The root element of the page.
   *
   * @var SimpleXMLElement
   */
  protected $root;

  /**
   * Initialize the BrowserPage with the page state information.
   *
   * @param $url
   *   The URL of the page.
   * @param $headers
   *   The response headers of the page.
   * @param $content
   *   The raw content of the page.
   */
  public function BrowserPage($url, $headers, $content) {
    $this->url = $url;
    $this->headers = $headers;
    $this->root = $this->load($content);
  }

  /**
   * Attempt to parse the raw content using DOM and import it into SimpleXML.
   *
   * @param $content
   *   The raw content of the page.
   * @return
   *   The root element of the page, or FALSE.
   */
  protected function load($content) {
    // Use DOM to load HTML soup, and hide warnings.
    $document = @DOMDocument::loadHTML($content);
    if ($document) {
      return simplexml_import_dom($document);
    }
    return FALSE;
  }

  /**
   * Check if the raw content is valid and could be parse.
   *
   * @return
   *   TRUE if content is valid, otherwise FALSE.
   */
  public function isValid() {
    return ($this->root !== FALSE);
  }

  /**
   * Perform an xpath search on the contents of the page.
   *
   * The search is relative to the root element, usually the HTML tag, of the
   * page. To perform a search using a different root element follow the
   * example below.
   * @code
   *   $parent = $page->xpath('.//parent');
   *   $parent[0]->xpath('//children');
   * @endcode
   *
   * @param $xpath
   *   The xpath string.
   * @return
   *   An array of SimpleXMLElement objects or FALSE in case of an error.
   * @link http://us.php.net/manual/function.simplexml-element-xpath.php
   */
  public function xpath($xpath) {
    if ($this->isValid()) {
      return $this->root->xpath($xpath);
    }
    return FALSE;
  }

  /**
   * Get all the meta tags.
   *
   * @return
   *   An array of SimpleXMLElement objects representing meta tags.
   */
  public function getMetaTags() {
    return $this->xpath('//meta');
  }

  /**
   * Get a specific meta tag.
   *
   * @param $key
   *   The meta tag key.
   * @param $type
   *   The type of meta tag, either: 'name' or 'http-equiv'.
   * @return
   *   A SimpleXMLElement object representing the meta tag, or FALSE if not
   *   found.
   */
  public function getMetaTag($key, $type = 'name') {
    if ($tags = $this->getMetaTags()) {
      foreach ($tags as $tag) {
        if ($tag[$type] == $key) {
          return $tag;
        }
      }
    }
    return FALSE;
  }

  /**
   * Get all the form elements.
   *
   * @return
   *   An array of SimpleXMLElement objects representing form elements.
   */
  public function getForms() {
    return $this->xpath('//form');
  }

  /**
   * Get all the input elements, or only those nested within a parent element.
   *
   * @param $parent
   *   SimpleXMLElement representing the parent to search within.
   * @return
   *   An array of SimpleXMLElement objects representing form elements.
   */
  public function getInputs($parent = NULL) {
    if ($parent) {
      return $parent->xpath('.//input|.//textarea|.//select');
    }
    return $this->xpath('.//input|.//textarea|.//select');
  }

  /**
   * Get all the options contained by a select, including nested options.
   *
   * @param $select
   *   SimpleXMLElement representing the select to extract option from.
   * @return
   *   Associative array where the keys represent each option value and the
   *   value is the text contained within the option tag. For example:
   * @code
   *   array(
   *     'option1' => 'Option 1',
   *     'option2' => 'Option 2',
   *   )
   * @endcode
   */
  public function getSelectOptions(SimpleXMLElement $select) {
    $elements = $this->getSelectOptionElements($select);

    $options = array();
    foreach ($elements as $element) {
      $options[(string) $element['value']] = $this->asText($element);
    }
    return $options;
  }

  /**
   * Get all selected options contained by a select, including nested options.
   *
   * @param $select
   *   SimpleXMLElement representing the select to extract option from.
   * @return
   *   Associative array of selected items in the format described by
   *   BrowserPage->getSelectOptions().
   * @see BrowserPage->getSelectOptions()
   */
  public function getSelectedOptions(SimpleXMLElement $select) {
    $elements = getSelectOptionElements($select);

    $options = array();
    foreach ($elements as $element) {
      if (isset($elements['selected'])) {
        $options[(string) $element['value']] = asText($element);
      }
    }
    return $options;
  }

  /**
   * Get all the options contained by a select, including nested options.
   *
   * @param $element
   *   SimpleXMLElement representing the select to extract option from.
   * @return
   *   An array of SimpleXMLElement objects representing option elements.
   */
  public function getSelectOptionElements(SimpleXMLElement $element) {
    $options = array();

    // Add all options items.
    foreach ($element->option as $option) {
      $options[] = $option;
    }

    // Search option group children.
    if (isset($element->optgroup)) {
      foreach ($element->optgroup as $group) {
        $options = array_merge($options, $this->getSelectOptionElements($group));
      }
    }
    return $options;
  }

  /**
   * Get the absolute URL for a given path, relative to the page.
   *
   * @param
   *   A path relative to the page or absolute.
   * @return
   *   An absolute path.
   */
  public function getAbsoluteUrl($path) {
    $parts = @parse_url($path);
    if (isset($parts['scheme'])) {
      return $path;
    }

    $base = $this->getBaseUrl();
    if ($path[0] == '/') {
      // Lead / then use host as base.
      $parts = parse_url($base);
      $base = $parts['scheme'] . '://' . $parts['host'];
    }
    return $base . $path;
  }

  /**
   * Get the base URL of the page.
   *
   * If a 'base' HTML element is defined then the URL it defines is used as the
   * base URL for the page, otherwise the page URL is used to determine the
   * base URL.
   *
   * @return
   *   The base URL of the page.
   */
  public function getBaseUrl() {
    // Check for base element.
    $elements = $this->xpath('.//base');
    if ($elements) {
      // More than one may be specified.
      foreach ($elements as $element) {
        if (isset($element['href'])) {
          $base = (string) $element['href'];
          break;
        }
      }
    }
    else {
      $base = $this->url;
      if ($pos = strpos($base, '?')) {
        // Remove query string.
        $base = substr($base, 0, $pos);
      }

      // Ignore everything after the last forward slash.
      $base = substr($base, 0, strrpos($base, '/'));
    }

    // Ensure that the last character is a forward slash.
    if ($base[strlen($base) - 1] != '/') {
      $base .= '/';
    }
    return $base;
  }

  /**
   * Extract the text contained by the element.
   *
   * Strips all XML/HTML tags, decodes HTML entities, and trims the result.
   *
   * @param $element
   *   SimpleXMLElement to extract text from.
   * @return
   *   Extracted text.
   */
  public function asText(SimpleXMLElement $element) {
    return trim(html_entity_decode(strip_tags($element->asXML())));
  }
}

/**
 * @} End of "defgroup browser".
 */