73 var
$accept =
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
133 $URI_PARTS = parse_url($URI);
134 if (!empty($URI_PARTS[
"user"]))
135 $this->user = $URI_PARTS[
"user"];
136 if (!empty($URI_PARTS[
"pass"]))
137 $this->pass = $URI_PARTS[
"pass"];
138 if (empty($URI_PARTS[
"query"]))
139 $URI_PARTS[
"query"] =
'';
140 if (empty($URI_PARTS[
"path"]))
141 $URI_PARTS[
"path"] =
'';
143 switch(strtolower($URI_PARTS[
"scheme"]))
146 $this->host = $URI_PARTS[
"host"];
147 if(!empty($URI_PARTS[
"port"]))
148 $this->port = $URI_PARTS[
"port"];
158 $path = $URI_PARTS[
"path"].($URI_PARTS[
"query"] ?
"?".$URI_PARTS[
"query"] :
"");
165 if($this->_redirectaddr)
168 if($this->maxredirs > $this->_redirectdepth)
171 if(preg_match(
"|^http://".preg_quote($this->host).
"|i",$this->_redirectaddr) || $this->offsiteok)
174 $this->_redirectdepth++;
176 $this->
fetch($this->_redirectaddr);
181 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
184 $this->_frameurls = array();
186 while(list(,$frameurl) = each($frameurls))
188 if($this->_framedepth < $this->maxframes)
190 $this->
fetch($frameurl);
191 $this->_framedepth++;
205 if(!$this->curl_path)
207 if(function_exists(
"is_executable"))
208 if (!is_executable($this->curl_path))
210 $this->host = $URI_PARTS[
"host"];
211 if(!empty($URI_PARTS[
"port"]))
212 $this->port = $URI_PARTS[
"port"];
220 $path = $URI_PARTS[
"path"].($URI_PARTS[
"query"] ?
"?".$URI_PARTS[
"query"] :
"");
225 if($this->_redirectaddr)
228 if($this->maxredirs > $this->_redirectdepth)
231 if(preg_match(
"|^http://".preg_quote($this->host).
"|i",$this->_redirectaddr) || $this->offsiteok)
234 $this->_redirectdepth++;
236 $this->
fetch($this->_redirectaddr);
241 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
244 $this->_frameurls = array();
246 while(list(,$frameurl) = each($frameurls))
248 if($this->_framedepth < $this->maxframes)
250 $this->
fetch($frameurl);
251 $this->_framedepth++;
261 $this->error =
'Invalid protocol "'.$URI_PARTS[
"scheme"].
'"\n';
279 function submit($URI, $formvars=
"", $formfiles=
"")
285 $URI_PARTS = parse_url($URI);
286 if (!empty($URI_PARTS[
"user"]))
287 $this->user = $URI_PARTS[
"user"];
288 if (!empty($URI_PARTS[
"pass"]))
289 $this->pass = $URI_PARTS[
"pass"];
290 if (empty($URI_PARTS[
"query"]))
291 $URI_PARTS[
"query"] =
'';
292 if (empty($URI_PARTS[
"path"]))
293 $URI_PARTS[
"path"] =
'';
295 switch(strtolower($URI_PARTS[
"scheme"]))
298 $this->host = $URI_PARTS[
"host"];
299 if(!empty($URI_PARTS[
"port"]))
300 $this->port = $URI_PARTS[
"port"];
306 $this->
_httprequest($URI,$fp,$URI,$this->_submit_method,$this->_submit_type,$postdata);
310 $path = $URI_PARTS[
"path"].($URI_PARTS[
"query"] ?
"?".$URI_PARTS[
"query"] :
"");
312 $this->
_httprequest(
$path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
317 if($this->_redirectaddr)
320 if($this->maxredirs > $this->_redirectdepth)
322 if(!preg_match(
"|^".$URI_PARTS[
"scheme"].
"://|", $this->_redirectaddr))
323 $this->_redirectaddr = $this->
_expandlinks($this->_redirectaddr,$URI_PARTS[
"scheme"].
"://".$URI_PARTS[
"host"]);
326 if(preg_match(
"|^http://".preg_quote($this->host).
"|i",$this->_redirectaddr) || $this->offsiteok)
329 $this->_redirectdepth++;
331 if( strpos( $this->_redirectaddr,
"?" ) > 0 )
332 $this->
fetch($this->_redirectaddr);
334 $this->
submit($this->_redirectaddr,$formvars, $formfiles);
339 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
342 $this->_frameurls = array();
344 while(list(,$frameurl) = each($frameurls))
346 if($this->_framedepth < $this->maxframes)
348 $this->
fetch($frameurl);
349 $this->_framedepth++;
364 if(!$this->curl_path)
366 if(function_exists(
"is_executable"))
367 if (!is_executable($this->curl_path))
369 $this->host = $URI_PARTS[
"host"];
370 if(!empty($URI_PARTS[
"port"]))
371 $this->port = $URI_PARTS[
"port"];
375 $this->
_httpsrequest($URI, $URI, $this->_submit_method, $this->_submit_type, $postdata);
379 $path = $URI_PARTS[
"path"].($URI_PARTS[
"query"] ?
"?".$URI_PARTS[
"query"] :
"");
381 $this->
_httpsrequest(
$path, $URI, $this->_submit_method, $this->_submit_type, $postdata);
384 if($this->_redirectaddr)
387 if($this->maxredirs > $this->_redirectdepth)
389 if(!preg_match(
"|^".$URI_PARTS[
"scheme"].
"://|", $this->_redirectaddr))
390 $this->_redirectaddr = $this->
_expandlinks($this->_redirectaddr,$URI_PARTS[
"scheme"].
"://".$URI_PARTS[
"host"]);
393 if(preg_match(
"|^http://".preg_quote($this->host).
"|i",$this->_redirectaddr) || $this->offsiteok)
396 $this->_redirectdepth++;
398 if( strpos( $this->_redirectaddr,
"?" ) > 0 )
399 $this->
fetch($this->_redirectaddr);
401 $this->
submit($this->_redirectaddr,$formvars, $formfiles);
406 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
409 $this->_frameurls = array();
411 while(list(,$frameurl) = each($frameurls))
413 if($this->_framedepth < $this->maxframes)
415 $this->
fetch($frameurl);
416 $this->_framedepth++;
427 $this->error =
'Invalid protocol "'.$URI_PARTS[
"scheme"].
'"\n';
443 if ($this->
fetch($URI))
445 if($this->lastredirectaddr)
447 if(is_array($this->results))
449 for($x=0;$x<count($this->results);$x++)
450 $this->results[$x] = $this->
_striplinks($this->results[$x]);
453 $this->results = $this->
_striplinks($this->results);
455 if($this->expandlinks)
456 $this->results = $this->
_expandlinks($this->results, $URI);
473 if ($this->
fetch($URI))
476 if(is_array($this->results))
478 for($x=0;$x<count($this->results);$x++)
479 $this->results[$x] = $this->
_stripform($this->results[$x]);
482 $this->results = $this->
_stripform($this->results);
500 if($this->
fetch($URI))
502 if(is_array($this->results))
504 for($x=0;$x<count($this->results);$x++)
505 $this->results[$x] = $this->
_striptext($this->results[$x]);
508 $this->results = $this->
_striptext($this->results);
524 if($this->
submit($URI,$formvars, $formfiles))
526 if($this->lastredirectaddr)
528 if(is_array($this->results))
530 for($x=0;$x<count($this->results);$x++)
532 $this->results[$x] = $this->
_striplinks($this->results[$x]);
533 if($this->expandlinks)
534 $this->results[$x] = $this->
_expandlinks($this->results[$x],$URI);
539 $this->results = $this->
_striplinks($this->results);
540 if($this->expandlinks)
541 $this->results = $this->
_expandlinks($this->results,$URI);
558 if($this->
submit($URI,$formvars, $formfiles))
560 if($this->lastredirectaddr)
562 if(is_array($this->results))
564 for($x=0;$x<count($this->results);$x++)
566 $this->results[$x] = $this->
_striptext($this->results[$x]);
567 if($this->expandlinks)
568 $this->results[$x] = $this->
_expandlinks($this->results[$x],$URI);
573 $this->results = $this->
_striptext($this->results);
574 if($this->expandlinks)
575 $this->results = $this->
_expandlinks($this->results,$URI);
592 $this->_submit_type =
"multipart/form-data";
603 $this->_submit_type =
"application/x-www-form-urlencoded";
616 $this->_submit_type =
"text/xml";
635 preg_match_all(
"'<\s*a\s.*?href\s*=\s* # find <a href=
636 ([\"\'])? # find single or double quote
637 (?(1) (.*?)\\1 | ([^\s>]+)) # if quote found, match up to next matching
638 # quote, otherwise match up to next space
639 'isx",$document,$links);
644 while(list($key,$val) = each($links[2]))
650 while(list($key,$val) = each($links[3]))
669 preg_match_all(
"'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements);
672 $match = implode(
"\r\n",$elements[0]);
694 $search = array(
"'<script[^>]*?>.*?</script>'si",
695 "'<[\/\!]*?[^<>]*?>'si",
697 "'&(quot|#34|#034|#x22);'i",
698 "'&(amp|#38|#038|#x26);'i",
699 "'&(lt|#60|#060|#x3c);'i",
700 "'&(gt|#62|#062|#x3e);'i",
701 "'&(nbsp|#160|#xa0);'i",
708 "'&(#39|#039|#x27);'",
718 $replace = array(
"",
743 $text = preg_replace($search,$replace,$document);
759 preg_match(
"/^[^\?]+/",$URI,$match);
761 $match = preg_replace(
"|/[^\/\.]+\.[^\/\.]+$|",
"",$match[0]);
762 $match = preg_replace(
"|/$|",
"",$match);
763 $match_part = parse_url($match);
765 $match_part[
"scheme"].
"://".$match_part[
"host"];
767 $search = array(
"|^http://".preg_quote($this->host).
"|i",
769 "|^(?!http://)(?!mailto:)|i",
774 $replace = array(
"",
781 $expandedLinks = preg_replace($search,$replace,$links);
783 return $expandedLinks;
798 $cookie_headers =
'';
799 if($this->passcookies && $this->_redirectaddr)
802 $URI_PARTS = parse_url($URI);
805 $headers = $http_method.
" ".
$url.
" ".$this->_httpversion.
"\r\n";
806 if(!empty($this->agent))
807 $headers .=
"User-Agent: ".$this->agent.
"\r\n";
808 if(!empty($this->host) && !isset($this->rawheaders[
'Host'])) {
810 if(!empty($this->port))
814 if(!empty($this->accept))
815 $headers .=
"Accept: ".$this->accept.
"\r\n";
816 if(!empty($this->referer))
817 $headers .=
"Referer: ".$this->referer.
"\r\n";
818 if(!empty($this->cookies))
820 if(!is_array($this->cookies))
821 $this->cookies = (array)$this->cookies;
823 reset($this->cookies);
824 if ( count($this->cookies) > 0 ) {
825 $cookie_headers .=
'Cookie: ';
826 foreach ( $this->cookies as $cookieKey => $cookieVal ) {
827 $cookie_headers .= $cookieKey.
"=".urlencode($cookieVal).
"; ";
829 $headers .= substr($cookie_headers,0,-2) .
"\r\n";
832 if(!empty($this->rawheaders))
834 if(!is_array($this->rawheaders))
835 $this->rawheaders = (array)$this->rawheaders;
836 while(list($headerKey,$headerVal) = each($this->rawheaders))
837 $headers .= $headerKey.
": ".$headerVal.
"\r\n";
839 if(!empty($content_type)) {
840 $headers .=
"Content-type: $content_type";
841 if ($content_type ==
"multipart/form-data")
842 $headers .=
"; boundary=".$this->_mime_boundary;
846 $headers .=
"Content-length: ".strlen($body).
"\r\n";
847 if(!empty($this->user) || !empty($this->pass))
848 $headers .=
"Authorization: Basic ".base64_encode($this->user.
":".$this->pass).
"\r\n";
851 if(!empty($this->proxy_user))
852 $headers .=
'Proxy-Authorization: ' .
'Basic ' . base64_encode($this->proxy_user .
':' . $this->proxy_pass).
"\r\n";
858 if ($this->read_timeout > 0)
859 socket_set_timeout($fp, $this->read_timeout);
860 $this->timed_out =
false;
864 $this->_redirectaddr =
false;
865 unset($this->headers);
867 while($currentHeader = fgets($fp,$this->_maxlinelen))
875 if($currentHeader ==
"\r\n")
879 if(preg_match(
"/^(Location:|URI:)/i",$currentHeader))
882 preg_match(
"/^(Location:|URI:)[ ]+(.*)/i",chop($currentHeader),$matches);
884 if(!preg_match(
"|\:\/\/|",$matches[2]))
887 $this->_redirectaddr = $URI_PARTS[
"scheme"].
"://".$this->host.
":".
$this->port;
889 if(!preg_match(
"|^/|",$matches[2]))
890 $this->_redirectaddr .=
"/".$matches[2];
892 $this->_redirectaddr .= $matches[2];
895 $this->_redirectaddr = $matches[2];
898 if(preg_match(
"|^HTTP/|",$currentHeader))
900 if(preg_match(
"|^HTTP/[^\s]*\s(.*?)\s|",$currentHeader,
$status))
904 $this->response_code = $currentHeader;
907 $this->headers[] = $currentHeader;
912 $_data = fread($fp, $this->maxlength);
913 if (strlen($_data) == 0) {
927 if(preg_match(
"'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",
$results,$match))
930 $this->_redirectaddr = $this->
_expandlinks($match[1],$URI);
934 if(($this->_framedepth < $this->maxframes) && preg_match_all(
"'<frame\s+.*src[\s]*=[\'\"]?([^\'\">]+)'i",
$results,$match))
937 for($x=0; $x<count($match[1]); $x++)
938 $this->_frameurls[] = $this->
_expandlinks($match[1][$x],$URI_PARTS[
"scheme"].
"://".$this->host);
941 elseif(is_array($this->results))
961 if($this->passcookies && $this->_redirectaddr)
966 $URI_PARTS = parse_url($URI);
971 if(!empty($this->agent))
972 $headers[] =
"User-Agent: ".$this->agent;
973 if(!empty($this->host))
974 if(!empty($this->port))
978 if(!empty($this->accept))
979 $headers[] =
"Accept: ".$this->accept;
980 if(!empty($this->referer))
981 $headers[] =
"Referer: ".$this->referer;
982 if(!empty($this->cookies))
984 if(!is_array($this->cookies))
985 $this->cookies = (array)$this->cookies;
987 reset($this->cookies);
988 if ( count($this->cookies) > 0 ) {
989 $cookie_str =
'Cookie: ';
990 foreach ( $this->cookies as $cookieKey => $cookieVal ) {
991 $cookie_str .= $cookieKey.
"=".urlencode($cookieVal).
"; ";
993 $headers[] = substr($cookie_str,0,-2);
996 if(!empty($this->rawheaders))
998 if(!is_array($this->rawheaders))
999 $this->rawheaders = (array)$this->rawheaders;
1000 while(list($headerKey,$headerVal) = each($this->rawheaders))
1001 $headers[] = $headerKey.
": ".$headerVal;
1003 if(!empty($content_type)) {
1004 if ($content_type ==
"multipart/form-data")
1005 $headers[] =
"Content-type: $content_type; boundary=".$this->_mime_boundary;
1007 $headers[] =
"Content-type: $content_type";
1010 $headers[] =
"Content-length: ".strlen($body);
1011 if(!empty($this->user) || !empty($this->pass))
1012 $headers[] =
"Authorization: BASIC ".base64_encode($this->user.
":".$this->pass);
1014 for($curr_header = 0; $curr_header < count(
$headers); $curr_header++) {
1015 $safer_header = strtr(
$headers[$curr_header],
"\"",
" " );
1016 $cmdline_params .=
" -H \"".$safer_header.
"\"";
1020 $cmdline_params .=
" -d \"$body\"";
1022 if($this->read_timeout > 0)
1023 $cmdline_params .=
" -m ".$this->read_timeout;
1025 $headerfile = tempnam(
$temp_dir,
"sno");
1027 exec($this->curl_path.
" -k -D \"$headerfile\"".$cmdline_params.
" \"".escapeshellcmd($URI).
"\"",
$results,$return);
1031 $this->error =
"Error: cURL could not retrieve the document, error $return.";
1038 $result_headers = file(
"$headerfile");
1040 $this->_redirectaddr =
false;
1041 unset($this->headers);
1043 for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++)
1047 if(preg_match(
"/^(Location: |URI: )/i",$result_headers[$currentHeader]))
1050 preg_match(
"/^(Location: |URI:)\s+(.*)/",chop($result_headers[$currentHeader]),$matches);
1052 if(!preg_match(
"|\:\/\/|",$matches[2]))
1055 $this->_redirectaddr = $URI_PARTS[
"scheme"].
"://".$this->host.
":".
$this->port;
1057 if(!preg_match(
"|^/|",$matches[2]))
1058 $this->_redirectaddr .=
"/".$matches[2];
1060 $this->_redirectaddr .= $matches[2];
1063 $this->_redirectaddr = $matches[2];
1066 if(preg_match(
"|^HTTP/|",$result_headers[$currentHeader]))
1067 $this->response_code = $result_headers[$currentHeader];
1069 $this->headers[] = $result_headers[$currentHeader];
1074 if(preg_match(
"'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",
$results,$match))
1076 $this->_redirectaddr = $this->
_expandlinks($match[1],$URI);
1080 if(($this->_framedepth < $this->maxframes) && preg_match_all(
"'<frame\s+.*src[\s]*=[\'\"]?([^\'\">]+)'i",
$results,$match))
1083 for($x=0; $x<count($match[1]); $x++)
1084 $this->_frameurls[] = $this->
_expandlinks($match[1][$x],$URI_PARTS[
"scheme"].
"://".$this->host);
1087 elseif(is_array($this->results))
1093 unlink(
"$headerfile");
1105 for($x=0; $x<count($this->headers); $x++)
1107 if(preg_match(
'/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $this->headers[$x],$match))
1108 $this->cookies[$match[1]] = urldecode($match[2]);
1121 if ($this->read_timeout > 0) {
1122 $fp_status = socket_get_status($fp);
1123 if ($fp_status[
"timed_out"]) {
1124 $this->timed_out =
true;
1139 if(!empty($this->proxy_host) && !empty($this->proxy_port))
1141 $this->_isproxy =
true;
1169 $this->status = $errno;
1173 $this->error=
"socket creation failed (-3)";
1175 $this->error=
"dns lookup failure (-4)";
1177 $this->error=
"connection refused or timed out (-5)";
1179 $this->error=
"connection failed (".$errno.
")";
1192 return(fclose($fp));
1206 settype($formvars,
"array");
1207 settype($formfiles,
"array");
1210 if (count($formvars) == 0 && count($formfiles) == 0)
1213 switch ($this->_submit_type) {
1214 case "application/x-www-form-urlencoded":
1216 while(list($key,$val) = each($formvars)) {
1217 if (is_array($val) || is_object($val)) {
1218 while (list($cur_key, $cur_val) = each($val)) {
1219 $postdata .= urlencode($key).
"[]=".urlencode($cur_val).
"&";
1222 $postdata .= urlencode($key).
"=".urlencode($val).
"&";
1226 case "multipart/form-data":
1227 $this->_mime_boundary =
"Snoopy".md5(uniqid(microtime()));
1230 while(list($key,$val) = each($formvars)) {
1231 if (is_array($val) || is_object($val)) {
1232 while (list($cur_key, $cur_val) = each($val)) {
1233 $postdata .=
"--".$this->_mime_boundary.
"\r\n";
1234 $postdata .=
"Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n";
1235 $postdata .=
"$cur_val\r\n";
1238 $postdata .=
"--".$this->_mime_boundary.
"\r\n";
1239 $postdata .=
"Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
1240 $postdata .=
"$val\r\n";
1245 while (list($field_name, $file_names) = each($formfiles)) {
1246 settype($file_names,
"array");
1247 while (list(, $file_name) = each($file_names)) {
1248 if (!is_readable($file_name))
continue;
1250 $fp = fopen($file_name,
"r");
1251 $file_content = fread($fp, filesize($file_name));
1253 $base_name = basename($file_name);
1255 $postdata .=
"--".$this->_mime_boundary.
"\r\n";
1256 $postdata .=
"Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n";
1257 $postdata .=
"$file_content\r\n";
1260 $postdata .=
"--".$this->_mime_boundary.
"--\r\n";
1266 $postdata = $formvars[0];