2 //INICIO IDENTIFICADOR DE VERSION No MODIFICAR
4 //FIN IDENTIFICADOR DE VERSION No MODIFICAR
5 /*************************************************
7 Snoopy - the PHP net client
8 Author: Monte Ohrt <monte@ispi.net>
9 Copyright (c): 1999-2000 ispi, all rights reserved
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 You may contact the author of Snoopy by e-mail at:
35 The latest version of Snoopy can be obtained from:
36 http://snoopy.sourceforge.net/
38 *************************************************/
42 /**** Public variables ****/
44 /* user definable vars */
46 var $host = "www.php.net"; // host name we are connecting to
47 var $port = 80; // port we are connecting to
48 var $proxy_host = ""; // proxy host to use
49 var $proxy_port = ""; // proxy port to use
50 var $proxy_user = ""; // proxy user to use
51 var $proxy_pass = ""; // proxy password to use
53 var $agent = "Snoopy v1.2"; // agent we masquerade as
54 var $referer = ""; // referer info to pass
55 var $cookies = array(); // array of cookies to pass
56 // $cookies["username"]="joe";
57 var $rawheaders = array(); // array of raw headers to send
58 // $rawheaders["Content-type"]="text/html";
60 var $maxredirs = 0; // http redirection depth maximum. 0 = disallow
61 var $lastredirectaddr = ""; // contains address of last redirected address
62 var $offsiteok = true; // allows redirection off-site
63 var $maxframes = 0; // frame content depth maximum. 0 = disallow
64 var $expandlinks = true; // expand links to fully qualified URLs.
65 // this only applies to fetchlinks()
67 var $passcookies = true; // pass set cookies back through redirects
68 // NOTE: this currently does not respect
69 // dates, domains or paths.
71 var $user = ""; // user for http authentication
72 var $pass = ""; // password for http authentication
75 var $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
77 var $results = ""; // where the content is put
79 var $error = ""; // error messages sent here
80 var $response_code = ""; // response code returned from server
81 var $headers = array(); // headers returned from server sent here
82 var $maxlength = 500000; // max return data length (body)
83 var $read_timeout = 0; // timeout on read operations, in seconds
84 // supported only since PHP 4 Beta 4
85 // set to 0 to disallow timeouts
86 var $timed_out = false; // if a read operation timed out
87 var $status = 0; // http request status
89 var $temp_dir = "/tmp"; // temporary directory that the webserver
90 // has permission to write to.
91 // under Windows, this should be C:\temp
93 var $curl_path = "/usr/local/bin/curl";
94 // Snoopy will use cURL for fetching
95 // SSL content if a full system path to
96 // the cURL binary is supplied here.
97 // set to false if you do not have
98 // cURL installed. See http://curl.haxx.se
99 // for details on installing cURL.
100 // Snoopy does *not* use the cURL
101 // library functions built into php,
102 // as these functions are not stable
103 // as of this Snoopy release.
105 /**** Private variables ****/
107 var $_maxlinelen = 4096; // max line length (headers)
109 var $_httpmethod = "GET"; // default http request method
110 var $_httpversion = "HTTP/1.0"; // default http request version
111 var $_submit_method = "POST"; // default submit method
112 var $_submit_type = "application/x-www-form-urlencoded"; // default submit type
113 var $_mime_boundary = ""; // MIME boundary for multipart/form-data submit type
114 var $_redirectaddr = false; // will be set if page fetched is a redirect
115 var $_redirectdepth = 0; // increments on an http redirect
116 var $_frameurls = array(); // frame src urls
117 var $_framedepth = 0; // increments on frame depth
119 var $_isproxy = false; // set if using a proxy server
120 var $_fp_timeout = 30; // timeout for socket connection
122 /*======================================================================*\
124 Purpose: Enables use of proxy server
125 Input: $state may be true or false
126 Output: $this->results current _isproxy value
127 \*======================================================================*/
129 function useproxy($state) {
130 return $_isproxy = $state;
133 /*======================================================================*\
135 Purpose: fetch the contents of a web page
136 (and possibly other protocols in the
137 future like ftp, nntp, gopher, etc.)
138 Input: $URI the location of the page to fetch
139 Output: $this->results the output text from the fetch
140 \*======================================================================*/
145 //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS);
146 $URI_PARTS = parse_url($URI);
147 if (!empty($URI_PARTS["user"]))
148 $this->user
= $URI_PARTS["user"];
149 if (!empty($URI_PARTS["pass"]))
150 $this->pass
= $URI_PARTS["pass"];
151 if (empty($URI_PARTS["query"]))
152 $URI_PARTS["query"] = '';
154 switch($URI_PARTS["scheme"])
157 $this->host
= $URI_PARTS["host"];
158 if(!empty($URI_PARTS["port"]))
159 $this->port
= $URI_PARTS["port"];
160 if($this->_connect($fp))
164 // using proxy, send entire URI
165 $this->_httprequest($URI,$fp,$URI,$this->_httpmethod
);
169 $path = $URI_PARTS["path"].($URI_PARTS["query"] ?
"?".$URI_PARTS["query"] : "");
170 // no proxy, send only the path
171 $this->_httprequest($path, $fp, $URI, $this->_httpmethod
);
174 $this->_disconnect($fp);
176 if($this->_redirectaddr
)
178 /* url was redirected, check if we've hit the max depth */
179 if($this->maxredirs
> $this->_redirectdepth
)
181 // only follow redirect if it's on this site, or offsiteok is true
182 if(preg_match("|^http://".preg_quote($this->host
)."|i",$this->_redirectaddr
) ||
$this->offsiteok
)
184 /* follow the redirect */
185 $this->_redirectdepth++
;
186 $this->lastredirectaddr
=$this->_redirectaddr
;
187 $this->fetch($this->_redirectaddr
);
192 if($this->_framedepth
< $this->maxframes
&& count($this->_frameurls
) > 0)
194 $frameurls = $this->_frameurls
;
195 $this->_frameurls
= array();
197 while(list(,$frameurl) = each($frameurls))
199 if($this->_framedepth
< $this->maxframes
)
201 $this->fetch($frameurl);
202 $this->_framedepth++
;
216 if(!$this->curl_path
)
218 if(function_exists("is_executable"))
219 if (!is_executable($this->curl_path
))
221 $this->host
= $URI_PARTS["host"];
222 if(!empty($URI_PARTS["port"]))
223 $this->port
= $URI_PARTS["port"];
226 // using proxy, send entire URI
227 $this->_httpsrequest($URI,$URI,$this->_httpmethod
);
231 $path = $URI_PARTS["path"].($URI_PARTS["query"] ?
"?".$URI_PARTS["query"] : "");
232 // no proxy, send only the path
233 $this->_httpsrequest($path, $URI, $this->_httpmethod
);
236 if($this->_redirectaddr
)
238 /* url was redirected, check if we've hit the max depth */
239 if($this->maxredirs
> $this->_redirectdepth
)
241 // only follow redirect if it's on this site, or offsiteok is true
242 if(preg_match("|^http://".preg_quote($this->host
)."|i",$this->_redirectaddr
) ||
$this->offsiteok
)
244 /* follow the redirect */
245 $this->_redirectdepth++
;
246 $this->lastredirectaddr
=$this->_redirectaddr
;
247 $this->fetch($this->_redirectaddr
);
252 if($this->_framedepth
< $this->maxframes
&& count($this->_frameurls
) > 0)
254 $frameurls = $this->_frameurls
;
255 $this->_frameurls
= array();
257 while(list(,$frameurl) = each($frameurls))
259 if($this->_framedepth
< $this->maxframes
)
261 $this->fetch($frameurl);
262 $this->_framedepth++
;
271 // not a valid protocol
272 $this->error
= 'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
279 /*======================================================================*\
281 Purpose: submit an http form
282 Input: $URI the location to post the data
283 $formvars the formvars to use.
284 format: $formvars["var"] = "val";
285 $formfiles an array of files to submit
286 format: $formfiles["var"] = "/dir/filename.ext";
287 Output: $this->results the text output from the post
288 \*======================================================================*/
290 function submit($URI, $formvars="", $formfiles="")
294 $postdata = $this->_prepare_post_body($formvars, $formfiles);
296 $URI_PARTS = parse_url($URI);
297 if (!empty($URI_PARTS["user"]))
298 $this->user
= $URI_PARTS["user"];
299 if (!empty($URI_PARTS["pass"]))
300 $this->pass
= $URI_PARTS["pass"];
301 if (empty($URI_PARTS["query"]))
302 $URI_PARTS["query"] = '';
304 switch($URI_PARTS["scheme"])
307 $this->host
= $URI_PARTS["host"];
308 if(!empty($URI_PARTS["port"]))
309 $this->port
= $URI_PARTS["port"];
310 if($this->_connect($fp))
314 // using proxy, send entire URI
315 $this->_httprequest($URI,$fp,$URI,$this->_submit_method
,$this->_submit_type
,$postdata);
319 $path = $URI_PARTS["path"].($URI_PARTS["query"] ?
"?".$URI_PARTS["query"] : "");
320 // no proxy, send only the path
321 $this->_httprequest($path, $fp, $URI, $this->_submit_method
, $this->_submit_type
, $postdata);
324 $this->_disconnect($fp);
326 if($this->_redirectaddr
)
328 /* url was redirected, check if we've hit the max depth */
329 if($this->maxredirs
> $this->_redirectdepth
)
331 if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr
))
332 $this->_redirectaddr
= $this->_expandlinks($this->_redirectaddr
,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
334 // only follow redirect if it's on this site, or offsiteok is true
335 if(preg_match("|^http://".preg_quote($this->host
)."|i",$this->_redirectaddr
) ||
$this->offsiteok
)
337 /* follow the redirect */
338 $this->_redirectdepth++
;
339 $this->lastredirectaddr
=$this->_redirectaddr
;
340 if( strpos( $this->_redirectaddr
, "?" ) > 0 )
341 $this->fetch($this->_redirectaddr
); // the redirect has changed the request method from post to get
343 $this->submit($this->_redirectaddr
,$formvars, $formfiles);
348 if($this->_framedepth
< $this->maxframes
&& count($this->_frameurls
) > 0)
350 $frameurls = $this->_frameurls
;
351 $this->_frameurls
= array();
353 while(list(,$frameurl) = each($frameurls))
355 if($this->_framedepth
< $this->maxframes
)
357 $this->fetch($frameurl);
358 $this->_framedepth++
;
373 if(!$this->curl_path
)
375 if(function_exists("is_executable"))
376 if (!is_executable($this->curl_path
))
378 $this->host
= $URI_PARTS["host"];
379 if(!empty($URI_PARTS["port"]))
380 $this->port
= $URI_PARTS["port"];
383 // using proxy, send entire URI
384 $this->_httpsrequest($URI, $URI, $this->_submit_method
, $this->_submit_type
, $postdata);
388 $path = $URI_PARTS["path"].($URI_PARTS["query"] ?
"?".$URI_PARTS["query"] : "");
389 // no proxy, send only the path
390 $this->_httpsrequest($path, $URI, $this->_submit_method
, $this->_submit_type
, $postdata);
393 if($this->_redirectaddr
)
395 /* url was redirected, check if we've hit the max depth */
396 if($this->maxredirs
> $this->_redirectdepth
)
398 if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr
))
399 $this->_redirectaddr
= $this->_expandlinks($this->_redirectaddr
,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
401 // only follow redirect if it's on this site, or offsiteok is true
402 if(preg_match("|^http://".preg_quote($this->host
)."|i",$this->_redirectaddr
) ||
$this->offsiteok
)
404 /* follow the redirect */
405 $this->_redirectdepth++
;
406 $this->lastredirectaddr
=$this->_redirectaddr
;
407 if( strpos( $this->_redirectaddr
, "?" ) > 0 )
408 $this->fetch($this->_redirectaddr
); // the redirect has changed the request method from post to get
410 $this->submit($this->_redirectaddr
,$formvars, $formfiles);
415 if($this->_framedepth
< $this->maxframes
&& count($this->_frameurls
) > 0)
417 $frameurls = $this->_frameurls
;
418 $this->_frameurls
= array();
420 while(list(,$frameurl) = each($frameurls))
422 if($this->_framedepth
< $this->maxframes
)
424 $this->fetch($frameurl);
425 $this->_framedepth++
;
435 // not a valid protocol
436 $this->error
= 'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
443 /*======================================================================*\
445 Purpose: fetch the links from a web page
446 Input: $URI where you are fetching from
447 Output: $this->results an array of the URLs
448 \*======================================================================*/
450 function fetchlinks($URI)
452 if ($this->fetch($URI))
455 if(is_array($this->results
))
457 for($x=0;$x<count($this->results
);$x++
)
458 $this->results
[$x] = $this->_striplinks($this->results
[$x]);
461 $this->results
= $this->_striplinks($this->results
);
463 if($this->expandlinks
)
464 $this->results
= $this->_expandlinks($this->results
, $URI);
471 /*======================================================================*\
473 Purpose: fetch the form elements from a web page
474 Input: $URI where you are fetching from
475 Output: $this->results the resulting html form
476 \*======================================================================*/
478 function fetchform($URI)
481 if ($this->fetch($URI))
484 if(is_array($this->results
))
486 for($x=0;$x<count($this->results
);$x++
)
487 $this->results
[$x] = $this->_stripform($this->results
[$x]);
490 $this->results
= $this->_stripform($this->results
);
499 /*======================================================================*\
501 Purpose: fetch the text from a web page, stripping the links
502 Input: $URI where you are fetching from
503 Output: $this->results the text from the web page
504 \*======================================================================*/
506 function fetchtext($URI)
508 if($this->fetch($URI))
510 if(is_array($this->results
))
512 for($x=0;$x<count($this->results
);$x++
)
513 $this->results
[$x] = $this->_striptext($this->results
[$x]);
516 $this->results
= $this->_striptext($this->results
);
523 /*======================================================================*\
524 Function: submitlinks
525 Purpose: grab links from a form submission
526 Input: $URI where you are submitting from
527 Output: $this->results an array of the links from the post
528 \*======================================================================*/
530 function submitlinks($URI, $formvars="", $formfiles="")
532 if($this->submit($URI,$formvars, $formfiles))
534 if(is_array($this->results
))
536 for($x=0;$x<count($this->results
);$x++
)
538 $this->results
[$x] = $this->_striplinks($this->results
[$x]);
539 if($this->expandlinks
)
540 $this->results
[$x] = $this->_expandlinks($this->results
[$x],$URI);
545 $this->results
= $this->_striplinks($this->results
);
546 if($this->expandlinks
)
547 $this->results
= $this->_expandlinks($this->results
,$URI);
555 /*======================================================================*\
557 Purpose: grab text from a form submission
558 Input: $URI where you are submitting from
559 Output: $this->results the text from the web page
560 \*======================================================================*/
562 function submittext($URI, $formvars = "", $formfiles = "")
564 if($this->submit($URI,$formvars, $formfiles))
566 if(is_array($this->results
))
568 for($x=0;$x<count($this->results
);$x++
)
570 $this->results
[$x] = $this->_striptext($this->results
[$x]);
571 if($this->expandlinks
)
572 $this->results
[$x] = $this->_expandlinks($this->results
[$x],$URI);
577 $this->results
= $this->_striptext($this->results
);
578 if($this->expandlinks
)
579 $this->results
= $this->_expandlinks($this->results
,$URI);
589 /*======================================================================*\
590 Function: set_submit_multipart
591 Purpose: Set the form submission content type to
593 \*======================================================================*/
594 function set_submit_multipart()
596 $this->_submit_type
= "multipart/form-data";
600 /*======================================================================*\
601 Function: set_submit_normal
602 Purpose: Set the form submission content type to
603 application/x-www-form-urlencoded
604 \*======================================================================*/
605 function set_submit_normal()
607 $this->_submit_type
= "application/x-www-form-urlencoded";
613 /*======================================================================*\
615 \*======================================================================*/
618 /*======================================================================*\
619 Function: _striplinks
620 Purpose: strip the hyperlinks from an html document
621 Input: $document document to strip.
622 Output: $match an array of the links
623 \*======================================================================*/
625 function _striplinks($document)
627 preg_match_all("'<\s*a\s.*?href\s*=\s* # find <a href=
628 ([\"\'])? # find single or double quote
629 (?(1) (.*?)\\1 | ([^\s\>]+)) # if quote found, match up to next matching
630 # quote, otherwise match up to next space
631 'isx",$document,$links);
634 // catenate the non-empty matches from the conditional subpattern
636 while(list($key,$val) = each($links[2]))
642 while(list($key,$val) = each($links[3]))
652 /*======================================================================*\
654 Purpose: strip the form elements from an html document
655 Input: $document document to strip.
656 Output: $match an array of the links
657 \*======================================================================*/
659 function _stripform($document)
661 preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements);
663 // catenate the matches
664 $match = implode("\r\n",$elements[0]);
672 /*======================================================================*\
674 Purpose: strip the text from an html document
675 Input: $document document to strip.
676 Output: $text the resulting text
677 \*======================================================================*/
679 function _striptext($document)
682 // I didn't use preg eval (//e) since that is only available in PHP 4.0.
683 // so, list your entities one by one here. I included some of the
686 $search = array("'<script[^>]*?>.*?</script>'si", // strip out javascript
687 "'<[\/\!]*?[^<>]*?>'si", // strip out html tags
688 "'([\r\n])[\s]+'", // strip out white space
689 "'&(quot|#34|#034|#x22);'i", // replace html entities
690 "'&(amp|#38|#038|#x26);'i", // added hexadecimal values
691 "'&(lt|#60|#060|#x3c);'i",
692 "'&(gt|#62|#062|#x3e);'i",
693 "'&(nbsp|#160|#xa0);'i",
700 "'&(#39|#039|#x27);'",
701 "'&(euro|#8364);'i", // europe
702 "'&a(uml|UML);'", // german
710 $replace = array( "",
735 $text = preg_replace($search,$replace,$document);
740 /*======================================================================*\
741 Function: _expandlinks
742 Purpose: expand each link into a fully qualified URL
743 Input: $links the links to qualify
744 $URI the full URI to get the base from
745 Output: $expandedLinks the expanded links
746 \*======================================================================*/
748 function _expandlinks($links,$URI)
751 preg_match("/^[^\?]+/",$URI,$match);
753 $match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]);
754 $match = preg_replace("|/$|","",$match);
756 $search = array( "|^http://".preg_quote($this->host
)."|i",
757 "|^(?!http://)(\/)?(?!mailto:)|i",
762 $replace = array( "",
768 $expandedLinks = preg_replace($search,$replace,$links);
770 return $expandedLinks;
773 /*======================================================================*\
774 Function: _httprequest
775 Purpose: go get the http data from the server
776 Input: $url the url to fetch
777 $fp the current open file pointer
779 $body body contents to send if any (POST)
781 \*======================================================================*/
783 function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="")
785 $cookie_headers = '';
786 if($this->passcookies
&& $this->_redirectaddr
)
789 $URI_PARTS = parse_url($URI);
792 $headers = $http_method." ".$url." ".$this->_httpversion
."\r\n";
793 if(!empty($this->agent
))
794 $headers .= "User-Agent: ".$this->agent
."\r\n";
795 if(!empty($this->host
) && !isset($this->rawheaders
['Host']))
796 $headers .= "Host: ".$this->host
."\r\n";
797 if(!empty($this->accept
))
798 $headers .= "Accept: ".$this->accept
."\r\n";
799 if(!empty($this->referer
))
800 $headers .= "Referer: ".$this->referer
."\r\n";
801 if(!empty($this->cookies
))
803 if(!is_array($this->cookies
))
804 $this->cookies
= (array)$this->cookies
;
806 reset($this->cookies
);
807 if ( count($this->cookies
) > 0 ) {
808 $cookie_headers .= 'Cookie: ';
809 foreach ( $this->cookies
as $cookieKey => $cookieVal ) {
810 $cookie_headers .= $cookieKey."=".urlencode($cookieVal)."; ";
812 $headers .= substr($cookie_headers,0,-2) . "\r\n";
815 if(!empty($this->rawheaders
))
817 if(!is_array($this->rawheaders
))
818 $this->rawheaders
= (array)$this->rawheaders
;
819 while(list($headerKey,$headerVal) = each($this->rawheaders
))
820 $headers .= $headerKey.": ".$headerVal."\r\n";
822 if(!empty($content_type)) {
823 $headers .= "Content-type: $content_type";
824 if ($content_type == "multipart/form-data")
825 $headers .= "; boundary=".$this->_mime_boundary
;
829 $headers .= "Content-length: ".strlen($body)."\r\n";
830 if(!empty($this->user
) ||
!empty($this->pass
))
831 $headers .= "Authorization: Basic ".base64_encode($this->user
.":".$this->pass
)."\r\n";
833 //add proxy auth headers
834 if(!empty($this->proxy_user
))
835 $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user
. ':' . $this->proxy_pass
)."\r\n";
840 // set the read timeout if needed
841 if ($this->read_timeout
> 0)
842 socket_set_timeout($fp, $this->read_timeout
);
843 $this->timed_out
= false;
845 fwrite($fp,$headers.$body,strlen($headers.$body));
847 $this->_redirectaddr
= false;
848 unset($this->headers
);
850 while($currentHeader = fgets($fp,$this->_maxlinelen
))
852 if ($this->read_timeout
> 0 && $this->_check_timeout($fp))
858 if($currentHeader == "\r\n")
861 // if a header begins with Location: or URI:, set the redirect
862 if(preg_match("/^(Location:|URI:)/i",$currentHeader))
864 // get URL portion of the redirect
865 preg_match("/^(Location:|URI:)[ ]+(.*)/",chop($currentHeader),$matches);
866 // look for :// in the Location header to see if hostname is included
867 if(!preg_match("|\:\/\/|",$matches[2]))
869 // no host in the path, so prepend
870 $this->_redirectaddr
= $URI_PARTS["scheme"]."://".$this->host
.":".$this->port
;
871 // eliminate double slash
872 if(!preg_match("|^/|",$matches[2]))
873 $this->_redirectaddr
.= "/".$matches[2];
875 $this->_redirectaddr
.= $matches[2];
878 $this->_redirectaddr
= $matches[2];
881 if(preg_match("|^HTTP/|",$currentHeader))
883 if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$currentHeader, $status))
885 $this->status
= $status[1];
887 $this->response_code
= $currentHeader;
890 $this->headers
[] = $currentHeader;
895 $_data = fread($fp, $this->maxlength
);
896 if (strlen($_data) == 0) {
902 if ($this->read_timeout
> 0 && $this->_check_timeout($fp))
908 // check if there is a a redirect meta tag
910 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
913 $this->_redirectaddr
= $this->_expandlinks($match[1],$URI);
916 // have we hit our frame depth and is there frame src to fetch?
917 if(($this->_framedepth
< $this->maxframes
) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
919 $this->results
[] = $results;
920 for($x=0; $x<count($match[1]); $x++
)
921 $this->_frameurls
[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host
);
923 // have we already fetched framed content?
924 elseif(is_array($this->results
))
925 $this->results
[] = $results;
928 $this->results
= $results;
933 /*======================================================================*\
934 Function: _httpsrequest
935 Purpose: go get the https data from the server using curl
936 Input: $url the url to fetch
938 $body body contents to send if any (POST)
940 \*======================================================================*/
942 function _httpsrequest($url,$URI,$http_method,$content_type="",$body="")
944 if($this->passcookies
&& $this->_redirectaddr
)
949 $URI_PARTS = parse_url($URI);
952 // GET ... header not needed for curl
953 //$headers[] = $http_method." ".$url." ".$this->_httpversion;
954 if(!empty($this->agent
))
955 $headers[] = "User-Agent: ".$this->agent
;
956 if(!empty($this->host
))
957 $headers[] = "Host: ".$this->host
;
958 if(!empty($this->accept
))
959 $headers[] = "Accept: ".$this->accept
;
960 if(!empty($this->referer
))
961 $headers[] = "Referer: ".$this->referer
;
962 if(!empty($this->cookies
))
964 if(!is_array($this->cookies
))
965 $this->cookies
= (array)$this->cookies
;
967 reset($this->cookies
);
968 if ( count($this->cookies
) > 0 ) {
969 $cookie_str = 'Cookie: ';
970 foreach ( $this->cookies
as $cookieKey => $cookieVal ) {
971 $cookie_str .= $cookieKey."=".urlencode($cookieVal)."; ";
973 $headers[] = substr($cookie_str,0,-2);
976 if(!empty($this->rawheaders
))
978 if(!is_array($this->rawheaders
))
979 $this->rawheaders
= (array)$this->rawheaders
;
980 while(list($headerKey,$headerVal) = each($this->rawheaders
))
981 $headers[] = $headerKey.": ".$headerVal;
983 if(!empty($content_type)) {
984 if ($content_type == "multipart/form-data")
985 $headers[] = "Content-type: $content_type; boundary=".$this->_mime_boundary
;
987 $headers[] = "Content-type: $content_type";
990 $headers[] = "Content-length: ".strlen($body);
991 if(!empty($this->user
) ||
!empty($this->pass
))
992 $headers[] = "Authorization: BASIC ".base64_encode($this->user
.":".$this->pass
);
994 for($curr_header = 0; $curr_header < count($headers); $curr_header++
)
995 $cmdline_params .= " -H \"".$headers[$curr_header]."\"";
998 $cmdline_params .= " -d \"$body\"";
1000 if($this->read_timeout
> 0)
1001 $cmdline_params .= " -m ".$this->read_timeout
;
1003 $headerfile = tempnam($temp_dir, "sno");
1005 $safer_URI = strtr( $URI, "\"", " " ); // strip quotes from the URI to avoid shell access
1006 exec($this->curl_path
." -D \"$headerfile\"".$cmdline_params." \"".$safer_URI."\"",$results,$return);
1010 $this->error
= "Error: cURL could not retrieve the document, error $return.";
1015 $results = implode("\r\n",$results);
1017 $result_headers = file("$headerfile");
1019 $this->_redirectaddr
= false;
1020 unset($this->headers
);
1022 for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++
)
1025 // if a header begins with Location: or URI:, set the redirect
1026 if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader]))
1028 // get URL portion of the redirect
1029 preg_match("/^(Location: |URI:)\s+(.*)/",chop($result_headers[$currentHeader]),$matches);
1030 // look for :// in the Location header to see if hostname is included
1031 if(!preg_match("|\:\/\/|",$matches[2]))
1033 // no host in the path, so prepend
1034 $this->_redirectaddr
= $URI_PARTS["scheme"]."://".$this->host
.":".$this->port
;
1035 // eliminate double slash
1036 if(!preg_match("|^/|",$matches[2]))
1037 $this->_redirectaddr
.= "/".$matches[2];
1039 $this->_redirectaddr
.= $matches[2];
1042 $this->_redirectaddr
= $matches[2];
1045 if(preg_match("|^HTTP/|",$result_headers[$currentHeader]))
1046 $this->response_code
= $result_headers[$currentHeader];
1048 $this->headers
[] = $result_headers[$currentHeader];
1051 // check if there is a a redirect meta tag
1053 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]+URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
1055 $this->_redirectaddr
= $this->_expandlinks($match[1],$URI);
1058 // have we hit our frame depth and is there frame src to fetch?
1059 if(($this->_framedepth
< $this->maxframes
) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
1061 $this->results
[] = $results;
1062 for($x=0; $x<count($match[1]); $x++
)
1063 $this->_frameurls
[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host
);
1065 // have we already fetched framed content?
1066 elseif(is_array($this->results
))
1067 $this->results
[] = $results;
1068 // no framed content
1070 $this->results
= $results;
1072 unlink("$headerfile");
1077 /*======================================================================*\
1078 Function: setcookies()
1079 Purpose: set cookies for a redirection
1080 \*======================================================================*/
1082 function setcookies()
1084 for($x=0; $x<count($this->headers
); $x++
)
1086 if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $this->headers
[$x],$match))
1087 $this->cookies
[$match[1]] = urldecode($match[2]);
1092 /*======================================================================*\
1093 Function: _check_timeout
1094 Purpose: checks whether timeout has occurred
1095 Input: $fp file pointer
1096 \*======================================================================*/
1098 function _check_timeout($fp)
1100 if ($this->read_timeout
> 0) {
1101 $fp_status = socket_get_status($fp);
1102 if ($fp_status["timed_out"]) {
1103 $this->timed_out
= true;
1110 /*======================================================================*\
1112 Purpose: make a socket connection
1113 Input: $fp file pointer
1114 \*======================================================================*/
1116 function _connect(&$fp)
1118 if(!empty($this->proxy_host
) && !empty($this->proxy_port
))
1120 $this->_isproxy
= true;
1122 $host = $this->proxy_host
;
1123 $port = $this->proxy_port
;
1127 $host = $this->host
;
1128 $port = $this->port
;
1141 // socket connection succeeded
1147 // socket connection failed
1148 $this->status
= $errno;
1152 $this->error
="socket creation failed (-3)";
1154 $this->error
="dns lookup failure (-4)";
1156 $this->error
="connection refused or timed out (-5)";
1158 $this->error
="connection failed (".$errno.")";
1163 /*======================================================================*\
1164 Function: _disconnect
1165 Purpose: disconnect a socket connection
1166 Input: $fp file pointer
1167 \*======================================================================*/
1169 function _disconnect($fp)
1171 return(fclose($fp));
1175 /*======================================================================*\
1176 Function: _prepare_post_body
1177 Purpose: Prepare post body according to encoding type
1178 Input: $formvars - form variables
1179 $formfiles - form upload files
1181 \*======================================================================*/
1183 function _prepare_post_body($formvars, $formfiles)
1185 settype($formvars, "array");
1186 settype($formfiles, "array");
1189 if (count($formvars) == 0 && count($formfiles) == 0)
1192 switch ($this->_submit_type
) {
1193 case "application/x-www-form-urlencoded":
1195 while(list($key,$val) = each($formvars)) {
1196 if (is_array($val) ||
is_object($val)) {
1197 while (list($cur_key, $cur_val) = each($val)) {
1198 $postdata .= urlencode($key)."[]=".urlencode($cur_val)."&";
1201 $postdata .= urlencode($key)."=".urlencode($val)."&";
1205 case "multipart/form-data":
1206 $this->_mime_boundary
= "Snoopy".md5(uniqid(microtime()));
1209 while(list($key,$val) = each($formvars)) {
1210 if (is_array($val) ||
is_object($val)) {
1211 while (list($cur_key, $cur_val) = each($val)) {
1212 $postdata .= "--".$this->_mime_boundary
."\r\n";
1213 $postdata .= "Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n";
1214 $postdata .= "$cur_val\r\n";
1217 $postdata .= "--".$this->_mime_boundary
."\r\n";
1218 $postdata .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
1219 $postdata .= "$val\r\n";
1224 while (list($field_name, $file_names) = each($formfiles)) {
1225 settype($file_names, "array");
1226 while (list(, $file_name) = each($file_names)) {
1227 if (!is_readable($file_name)) continue;
1229 $fp = fopen($file_name, "r");
1230 $file_content = fread($fp, filesize($file_name));
1232 $base_name = basename($file_name);
1234 $postdata .= "--".$this->_mime_boundary
."\r\n";
1235 $postdata .= "Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n";
1236 $postdata .= "$file_content\r\n";
1239 $postdata .= "--".$this->_mime_boundary
."--\r\n";