3
/*************************************************
5
Snoopy - the PHP net client
6
Author: Monte Ohrt <monte@ispi.net>
7
Copyright (c): 1999-2000 ispi, all rights reserved
10
* This library is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public
12
* License as published by the Free Software Foundation; either
13
* version 2.1 of the License, or (at your option) any later version.
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Lesser General Public License for more details.
20
* You should have received a copy of the GNU Lesser General Public
21
* License along with this library; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
You may contact the author of Snoopy by e-mail at:
33
The latest version of Snoopy can be obtained from:
34
http://snoopy.sourceforge.com
36
*************************************************/
40
/**** Public variables ****/
42
/* user definable vars */
44
var $host = "www.php.net"; // host name we are connecting to
45
var $port = 80; // port we are connecting to
46
var $proxy_host = ""; // proxy host to use
47
var $proxy_port = ""; // proxy port to use
48
var $agent = "Snoopy v1.0"; // agent we masquerade as
49
var $referer = ""; // referer info to pass
50
var $cookies = array(); // array of cookies to pass
51
// $cookies["username"]="joe";
52
var $rawheaders = array(); // array of raw headers to send
53
// $rawheaders["Content-type"]="text/html";
55
var $maxredirs = 5; // http redirection depth maximum. 0 = disallow
56
var $lastredirectaddr = ""; // contains address of last redirected address
57
var $offsiteok = true; // allows redirection off-site
58
var $maxframes = 0; // frame content depth maximum. 0 = disallow
59
var $expandlinks = true; // expand links to fully qualified URLs.
60
// this only applies to fetchlinks()
62
var $passcookies = true; // pass set cookies back through redirects
63
// NOTE: this currently does not respect
64
// dates, domains or paths.
66
var $user = ""; // user for http authentication
67
var $pass = ""; // password for http authentication
70
var $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
72
var $results = ""; // where the content is put
74
var $error = ""; // error messages sent here
75
var $response_code = ""; // response code returned from server
76
var $headers = array(); // headers returned from server sent here
77
var $maxlength = 500000; // max return data length (body)
78
var $read_timeout = 0; // timeout on read operations, in seconds
79
// supported only since PHP 4 Beta 4
80
// set to 0 to disallow timeouts
81
var $timed_out = false; // if a read operation timed out
82
var $status = 0; // http request status
84
var $curl_path = "/usr/bin/curl";
85
// Snoopy will use cURL for fetching
86
// SSL content if a full system path to
87
// the cURL binary is supplied here.
88
// set to false if you do not have
89
// cURL installed. See http://curl.haxx.se
90
// for details on installing cURL.
91
// Snoopy does *not* use the cURL
92
// library functions built into php,
93
// as these functions are not stable
94
// as of this Snoopy release.
96
// send Accept-encoding: gzip?
99
/**** Private variables ****/
101
var $_maxlinelen = 4096; // max line length (headers)
103
var $_httpmethod = "GET"; // default http request method
104
var $_httpversion = "HTTP/1.0"; // default http request version
105
var $_submit_method = "POST"; // default submit method
106
var $_submit_type = "application/x-www-form-urlencoded"; // default submit type
107
var $_mime_boundary = ""; // MIME boundary for multipart/form-data submit type
108
var $_redirectaddr = false; // will be set if page fetched is a redirect
109
var $_redirectdepth = 0; // increments on an http redirect
110
var $_frameurls = array(); // frame src urls
111
var $_framedepth = 0; // increments on frame depth
113
var $_isproxy = false; // set if using a proxy server
114
var $_fp_timeout = 30; // timeout for socket connection
116
/*======================================================================*\
118
Purpose: fetch the contents of a web page
119
(and possibly other protocols in the
120
future like ftp, nntp, gopher, etc.)
121
Input: $URI the location of the page to fetch
122
Output: $this->results the output text from the fetch
123
\*======================================================================*/
128
//preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS);
129
$URI_PARTS = parse_url($URI);
130
if (!empty($URI_PARTS["user"]))
131
$this->user = $URI_PARTS["user"];
132
if (!empty($URI_PARTS["pass"]))
133
$this->pass = $URI_PARTS["pass"];
135
switch($URI_PARTS["scheme"])
138
$this->host = $URI_PARTS["host"];
139
if(!empty($URI_PARTS["port"]))
140
$this->port = $URI_PARTS["port"];
141
if($this->_connect($fp))
145
// using proxy, send entire URI
146
$this->_httprequest($URI,$fp,$URI,$this->_httpmethod);
150
$path = $URI_PARTS["path"].(isset($URI_PARTS["query"]) ? "?".$URI_PARTS["query"] : "");
151
// no proxy, send only the path
152
$this->_httprequest($path, $fp, $URI, $this->_httpmethod);
155
$this->_disconnect($fp);
157
if($this->_redirectaddr)
159
/* url was redirected, check if we've hit the max depth */
160
if($this->maxredirs > $this->_redirectdepth)
162
// only follow redirect if it's on this site, or offsiteok is true
163
if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
165
/* follow the redirect */
166
$this->_redirectdepth++;
167
$this->lastredirectaddr=$this->_redirectaddr;
168
$this->fetch($this->_redirectaddr);
173
if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
175
$frameurls = $this->_frameurls;
176
$this->_frameurls = array();
178
while(list(,$frameurl) = each($frameurls))
180
if($this->_framedepth < $this->maxframes)
182
$this->fetch($frameurl);
183
$this->_framedepth++;
197
if(!$this->curl_path || (!is_executable($this->curl_path))) {
198
$this->error = "Bad curl ($this->curl_path), can't fetch HTTPS \n";
201
$this->host = $URI_PARTS["host"];
202
if(!empty($URI_PARTS["port"]))
203
$this->port = $URI_PARTS["port"];
206
// using proxy, send entire URI
207
$this->_httpsrequest($URI,$URI,$this->_httpmethod);
211
$path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
212
// no proxy, send only the path
213
$this->_httpsrequest($path, $URI, $this->_httpmethod);
216
if($this->_redirectaddr)
218
/* url was redirected, check if we've hit the max depth */
219
if($this->maxredirs > $this->_redirectdepth)
221
// only follow redirect if it's on this site, or offsiteok is true
222
if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
224
/* follow the redirect */
225
$this->_redirectdepth++;
226
$this->lastredirectaddr=$this->_redirectaddr;
227
$this->fetch($this->_redirectaddr);
232
if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
234
$frameurls = $this->_frameurls;
235
$this->_frameurls = array();
237
while(list(,$frameurl) = each($frameurls))
239
if($this->_framedepth < $this->maxframes)
241
$this->fetch($frameurl);
242
$this->_framedepth++;
251
// not a valid protocol
252
$this->error = 'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
261
/*======================================================================*\
263
\*======================================================================*/
266
/*======================================================================*\
267
Function: _striplinks
268
Purpose: strip the hyperlinks from an html document
269
Input: $document document to strip.
270
Output: $match an array of the links
271
\*======================================================================*/
273
function _striplinks($document)
275
preg_match_all("'<\s*a\s+.*href\s*=\s* # find <a href=
276
([\"\'])? # find single or double quote
277
(?(1) (.*?)\\1 | ([^\s\>]+)) # if quote found, match up to next matching
278
# quote, otherwise match up to next space
279
'isx",$document,$links);
282
// catenate the non-empty matches from the conditional subpattern
284
while(list($key,$val) = each($links[2]))
290
while(list($key,$val) = each($links[3]))
300
/*======================================================================*\
302
Purpose: strip the form elements from an html document
303
Input: $document document to strip.
304
Output: $match an array of the links
305
\*======================================================================*/
307
function _stripform($document)
309
preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements);
311
// catenate the matches
312
$match = implode("\r\n",$elements[0]);
320
/*======================================================================*\
322
Purpose: strip the text from an html document
323
Input: $document document to strip.
324
Output: $text the resulting text
325
\*======================================================================*/
327
function _striptext($document)
330
// I didn't use preg eval (//e) since that is only available in PHP 4.0.
331
// so, list your entities one by one here. I included some of the
334
$search = array("'<script[^>]*?>.*?</script>'si", // strip out javascript
335
"'<[\/\!]*?[^<>]*?>'si", // strip out html tags
336
"'([\r\n])[\s]+'", // strip out white space
337
"'&(quote|#34);'i", // replace html entities
347
$replace = array( "",
360
$text = preg_replace($search,$replace,$document);
365
/*======================================================================*\
366
Function: _expandlinks
367
Purpose: expand each link into a fully qualified URL
368
Input: $links the links to qualify
369
$URI the full URI to get the base from
370
Output: $expandedLinks the expanded links
371
\*======================================================================*/
373
function _expandlinks($links,$URI)
376
preg_match("/^[^\?]+/",$URI,$match);
378
$match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]);
380
$search = array( "|^http://".preg_quote($this->host)."|i",
381
"|^(?!http://)(\/)?(?!mailto:)|i",
386
$replace = array( "",
392
$expandedLinks = preg_replace($search,$replace,$links);
394
return $expandedLinks;
397
/*======================================================================*\
398
Function: _httprequest
399
Purpose: go get the http data from the server
400
Input: $url the url to fetch
401
$fp the current open file pointer
403
$body body contents to send if any (POST)
405
\*======================================================================*/
407
function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="")
409
if($this->passcookies && $this->_redirectaddr)
412
$URI_PARTS = parse_url($URI);
415
$headers = $http_method." ".$url." ".$this->_httpversion."\r\n";
416
if(!empty($this->agent))
417
$headers .= "User-Agent: ".$this->agent."\r\n";
418
if(!empty($this->host) && !isset($this->rawheaders['Host']))
419
$headers .= "Host: ".$this->host."\r\n";
420
if(!empty($this->accept))
421
$headers .= "Accept: ".$this->accept."\r\n";
423
if($this->use_gzip) {
424
// make sure PHP was built with --with-zlib
425
// and we can handle gzipp'ed data
426
if ( function_exists(gzinflate) ) {
427
$headers .= "Accept-encoding: gzip\r\n";
431
"use_gzip is on, but PHP was built without zlib support.".
432
" Requesting file(s) without gzip encoding.",
437
if(!empty($this->referer))
438
$headers .= "Referer: ".$this->referer."\r\n";
439
if(!empty($this->cookies))
441
if(!is_array($this->cookies))
442
$this->cookies = (array)$this->cookies;
444
reset($this->cookies);
445
if ( count($this->cookies) > 0 ) {
446
$cookie_headers .= 'Cookie: ';
447
foreach ( $this->cookies as $cookieKey => $cookieVal ) {
448
$cookie_headers .= $cookieKey."=".urlencode($cookieVal)."; ";
450
$headers .= substr($cookie_headers,0,-2) . "\r\n";
453
if(!empty($this->rawheaders))
455
if(!is_array($this->rawheaders))
456
$this->rawheaders = (array)$this->rawheaders;
457
while(list($headerKey,$headerVal) = each($this->rawheaders))
458
$headers .= $headerKey.": ".$headerVal."\r\n";
460
if(!empty($content_type)) {
461
$headers .= "Content-type: $content_type";
462
if ($content_type == "multipart/form-data")
463
$headers .= "; boundary=".$this->_mime_boundary;
467
$headers .= "Content-length: ".strlen($body)."\r\n";
468
if(!empty($this->user) || !empty($this->pass))
469
$headers .= "Authorization: BASIC ".base64_encode($this->user.":".$this->pass)."\r\n";
473
// set the read timeout if needed
474
if ($this->read_timeout > 0)
475
socket_set_timeout($fp, $this->read_timeout);
476
$this->timed_out = false;
478
fwrite($fp,$headers.$body,strlen($headers.$body));
480
$this->_redirectaddr = false;
481
unset($this->headers);
483
// content was returned gzip encoded?
486
while($currentHeader = fgets($fp,$this->_maxlinelen))
488
if ($this->read_timeout > 0 && $this->_check_timeout($fp))
494
// if($currentHeader == "\r\n")
495
if(preg_match("/^\r?\n$/", $currentHeader) )
498
// if a header begins with Location: or URI:, set the redirect
499
if(preg_match("/^(Location:|URI:)/i",$currentHeader))
501
// get URL portion of the redirect
502
preg_match("/^(Location:|URI:)\s+(.*)/",chop($currentHeader),$matches);
503
// look for :// in the Location header to see if hostname is included
504
if(!preg_match("|\:\/\/|",$matches[2]))
506
// no host in the path, so prepend
507
$this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
508
// eliminate double slash
509
if(!preg_match("|^/|",$matches[2]))
510
$this->_redirectaddr .= "/".$matches[2];
512
$this->_redirectaddr .= $matches[2];
515
$this->_redirectaddr = $matches[2];
518
if(preg_match("|^HTTP/|",$currentHeader))
520
if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$currentHeader, $status))
522
$this->status= $status[1];
524
$this->response_code = $currentHeader;
527
if (preg_match("/Content-Encoding: gzip/", $currentHeader) ) {
531
$this->headers[] = $currentHeader;
534
# $results = fread($fp, $this->maxlength);
536
while ( $data = fread($fp, $this->maxlength) ) {
539
strlen($results) > $this->maxlength ) {
546
// per http://www.php.net/manual/en/function.gzencode.php
547
$results = substr($results, 10);
548
$results = gzinflate($results);
551
if ($this->read_timeout > 0 && $this->_check_timeout($fp))
557
// check if there is a a redirect meta tag
559
if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]+URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
561
$this->_redirectaddr = $this->_expandlinks($match[1],$URI);
564
// have we hit our frame depth and is there frame src to fetch?
565
if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
567
$this->results[] = $results;
568
for($x=0; $x<count($match[1]); $x++)
569
$this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host);
571
// have we already fetched framed content?
572
elseif(is_array($this->results))
573
$this->results[] = $results;
576
$this->results = $results;
581
/*======================================================================*\
582
Function: _httpsrequest
583
Purpose: go get the https data from the server using curl
584
Input: $url the url to fetch
586
$body body contents to send if any (POST)
588
\*======================================================================*/
590
function _httpsrequest($url,$URI,$http_method,$content_type="",$body="")
592
if($this->passcookies && $this->_redirectaddr)
597
$URI_PARTS = parse_url($URI);
600
// GET ... header not needed for curl
601
//$headers[] = $http_method." ".$url." ".$this->_httpversion;
602
if(!empty($this->agent))
603
$headers[] = "User-Agent: ".$this->agent;
604
if(!empty($this->host))
605
$headers[] = "Host: ".$this->host;
606
if(!empty($this->accept))
607
$headers[] = "Accept: ".$this->accept;
608
if(!empty($this->referer))
609
$headers[] = "Referer: ".$this->referer;
610
if(!empty($this->cookies))
612
if(!is_array($this->cookies))
613
$this->cookies = (array)$this->cookies;
615
reset($this->cookies);
616
if ( count($this->cookies) > 0 ) {
617
$cookie_str = 'Cookie: ';
618
foreach ( $this->cookies as $cookieKey => $cookieVal ) {
619
$cookie_str .= $cookieKey."=".urlencode($cookieVal)."; ";
621
$headers[] = substr($cookie_str,0,-2);
624
if(!empty($this->rawheaders))
626
if(!is_array($this->rawheaders))
627
$this->rawheaders = (array)$this->rawheaders;
628
while(list($headerKey,$headerVal) = each($this->rawheaders))
629
$headers[] = $headerKey.": ".$headerVal;
631
if(!empty($content_type)) {
632
if ($content_type == "multipart/form-data")
633
$headers[] = "Content-type: $content_type; boundary=".$this->_mime_boundary;
635
$headers[] = "Content-type: $content_type";
638
$headers[] = "Content-length: ".strlen($body);
639
if(!empty($this->user) || !empty($this->pass))
640
$headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass);
642
for($curr_header = 0; $curr_header < count($headers); $curr_header++)
643
$cmdline_params .= " -H \"".$headers[$curr_header]."\"";
646
$cmdline_params .= " -d \"$body\"";
648
if($this->read_timeout > 0)
649
$cmdline_params .= " -m ".$this->read_timeout;
651
$headerfile = uniqid(time());
653
# accept self-signed certs
654
$cmdline_params .= " -k";
655
exec($this->curl_path." -D \"/tmp/$headerfile\"".$cmdline_params." ".$URI,$results,$return);
659
$this->error = "Error: cURL could not retrieve the document, error $return.";
664
$results = implode("\r\n",$results);
666
$result_headers = file("/tmp/$headerfile");
668
$this->_redirectaddr = false;
669
unset($this->headers);
671
for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++)
674
// if a header begins with Location: or URI:, set the redirect
675
if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader]))
677
// get URL portion of the redirect
678
preg_match("/^(Location: |URI:)(.*)/",chop($result_headers[$currentHeader]),$matches);
679
// look for :// in the Location header to see if hostname is included
680
if(!preg_match("|\:\/\/|",$matches[2]))
682
// no host in the path, so prepend
683
$this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
684
// eliminate double slash
685
if(!preg_match("|^/|",$matches[2]))
686
$this->_redirectaddr .= "/".$matches[2];
688
$this->_redirectaddr .= $matches[2];
691
$this->_redirectaddr = $matches[2];
694
if(preg_match("|^HTTP/|",$result_headers[$currentHeader]))
696
$this->response_code = $result_headers[$currentHeader];
697
if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$this->response_code, $match))
699
$this->status= $match[1];
702
$this->headers[] = $result_headers[$currentHeader];
705
// check if there is a a redirect meta tag
707
if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]+URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
709
$this->_redirectaddr = $this->_expandlinks($match[1],$URI);
712
// have we hit our frame depth and is there frame src to fetch?
713
if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
715
$this->results[] = $results;
716
for($x=0; $x<count($match[1]); $x++)
717
$this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host);
719
// have we already fetched framed content?
720
elseif(is_array($this->results))
721
$this->results[] = $results;
724
$this->results = $results;
726
unlink("/tmp/$headerfile");
731
/*======================================================================*\
732
Function: setcookies()
733
Purpose: set cookies for a redirection
734
\*======================================================================*/
736
function setcookies()
738
for($x=0; $x<count($this->headers); $x++)
740
if(preg_match("/^set-cookie:[\s]+([^=]+)=([^;]+)/i", $this->headers[$x],$match))
741
$this->cookies[$match[1]] = $match[2];
746
/*======================================================================*\
747
Function: _check_timeout
748
Purpose: checks whether timeout has occurred
749
Input: $fp file pointer
750
\*======================================================================*/
752
function _check_timeout($fp)
754
if ($this->read_timeout > 0) {
755
$fp_status = socket_get_status($fp);
756
if ($fp_status["timed_out"]) {
757
$this->timed_out = true;
764
/*======================================================================*\
766
Purpose: make a socket connection
767
Input: $fp file pointer
768
\*======================================================================*/
770
function _connect(&$fp)
772
if(!empty($this->proxy_host) && !empty($this->proxy_port))
774
$this->_isproxy = true;
775
$host = $this->proxy_host;
776
$port = $this->proxy_port;
794
// socket connection succeeded
800
// socket connection failed
801
$this->status = $errno;
805
$this->error="socket creation failed (-3)";
807
$this->error="dns lookup failure (-4)";
809
$this->error="connection refused or timed out (-5)";
811
$this->error="connection failed (".$errno.")";
816
/*======================================================================*\
817
Function: _disconnect
818
Purpose: disconnect a socket connection
819
Input: $fp file pointer
820
\*======================================================================*/
822
function _disconnect($fp)
828
/*======================================================================*\
829
Function: _prepare_post_body
830
Purpose: Prepare post body according to encoding type
831
Input: $formvars - form variables
832
$formfiles - form upload files
834
\*======================================================================*/
836
function _prepare_post_body($formvars, $formfiles)
838
settype($formvars, "array");
839
settype($formfiles, "array");
841
if (count($formvars) == 0 && count($formfiles) == 0)
844
switch ($this->_submit_type) {
845
case "application/x-www-form-urlencoded":
847
while(list($key,$val) = each($formvars)) {
848
if (is_array($val) || is_object($val)) {
849
while (list($cur_key, $cur_val) = each($val)) {
850
$postdata .= urlencode($key)."[]=".urlencode($cur_val)."&";
853
$postdata .= urlencode($key)."=".urlencode($val)."&";
857
case "multipart/form-data":
858
$this->_mime_boundary = "Snoopy".md5(uniqid(microtime()));
861
while(list($key,$val) = each($formvars)) {
862
if (is_array($val) || is_object($val)) {
863
while (list($cur_key, $cur_val) = each($val)) {
864
$postdata .= "--".$this->_mime_boundary."\r\n";
865
$postdata .= "Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n";
866
$postdata .= "$cur_val\r\n";
869
$postdata .= "--".$this->_mime_boundary."\r\n";
870
$postdata .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
871
$postdata .= "$val\r\n";
876
while (list($field_name, $file_names) = each($formfiles)) {
877
settype($file_names, "array");
878
while (list(, $file_name) = each($file_names)) {
879
if (!is_readable($file_name)) continue;
881
$fp = fopen($file_name, "r");
882
$file_content = fread($fp, filesize($file_name));
884
$base_name = basename($file_name);
886
$postdata .= "--".$this->_mime_boundary."\r\n";
887
$postdata .= "Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n";
888
$postdata .= "$file_content\r\n";
891
$postdata .= "--".$this->_mime_boundary."--\r\n";