65 var
$accept =
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
123 $URI_PARTS = parse_url($URI);
124 if (!empty($URI_PARTS[
"user"]))
125 $this->user = $URI_PARTS[
"user"];
126 if (!empty($URI_PARTS[
"pass"]))
127 $this->pass = $URI_PARTS[
"pass"];
128 if (empty($URI_PARTS[
"query"]))
129 $URI_PARTS[
"query"] =
'';
130 if (empty($URI_PARTS[
"path"]))
131 $URI_PARTS[
"path"] =
'';
135 switch (strtolower($URI_PARTS[
"scheme"])) {
137 if (!extension_loaded(
'openssl')) {
138 trigger_error(
"openssl extension required for HTTPS", E_USER_ERROR);
143 $this->scheme = strtolower($URI_PARTS[
"scheme"]);
144 $this->host = $URI_PARTS[
"host"];
145 if (!empty($URI_PARTS[
"port"]))
146 $this->port = $URI_PARTS[
"port"];
148 if ($this->_isproxy) {
150 $this->
_httprequest($URI, $fp, $URI, $this->_httpmethod);
152 $path = $URI_PARTS[
"path"] . ($URI_PARTS[
"query"] ?
"?" . $URI_PARTS[
"query"] :
"");
159 if ($this->_redirectaddr) {
161 if ($this->maxredirs > $this->_redirectdepth) {
163 if (preg_match(
"|^https?://" . preg_quote($this->host) .
"|i", $this->_redirectaddr) || $this->offsiteok) {
165 $this->_redirectdepth++;
167 $this->
fetch($this->_redirectaddr);
172 if ($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0) {
174 $this->_frameurls = array();
176 while (list(, $frameurl) = each($frameurls)) {
177 if ($this->_framedepth < $this->maxframes) {
178 $this->
fetch($frameurl);
179 $this->_framedepth++;
191 $this->error =
'Invalid protocol "' . $URI_PARTS[
"scheme"] .
'"\n';
209 function submit($URI, $formvars =
"", $formfiles =
"")
215 $URI_PARTS = parse_url($URI);
216 if (!empty($URI_PARTS[
"user"]))
217 $this->user = $URI_PARTS[
"user"];
218 if (!empty($URI_PARTS[
"pass"]))
219 $this->pass = $URI_PARTS[
"pass"];
220 if (empty($URI_PARTS[
"query"]))
221 $URI_PARTS[
"query"] =
'';
222 if (empty($URI_PARTS[
"path"]))
223 $URI_PARTS[
"path"] =
'';
225 switch (strtolower($URI_PARTS[
"scheme"])) {
227 if (!extension_loaded(
'openssl')) {
228 trigger_error(
"openssl extension required for HTTPS", E_USER_ERROR);
233 $this->scheme = strtolower($URI_PARTS[
"scheme"]);
234 $this->host = $URI_PARTS[
"host"];
235 if (!empty($URI_PARTS[
"port"]))
236 $this->port = $URI_PARTS[
"port"];
238 if ($this->_isproxy) {
240 $this->
_httprequest($URI, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
242 $path = $URI_PARTS[
"path"] . ($URI_PARTS[
"query"] ?
"?" . $URI_PARTS[
"query"] :
"");
244 $this->
_httprequest(
$path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
249 if ($this->_redirectaddr) {
251 if ($this->maxredirs > $this->_redirectdepth) {
252 if (!preg_match(
"|^" . $URI_PARTS[
"scheme"] .
"://|", $this->_redirectaddr))
253 $this->_redirectaddr = $this->
_expandlinks($this->_redirectaddr, $URI_PARTS[
"scheme"] .
"://" . $URI_PARTS[
"host"]);
256 if (preg_match(
"|^https?://" . preg_quote($this->host) .
"|i", $this->_redirectaddr) || $this->offsiteok) {
258 $this->_redirectdepth++;
260 if (strpos($this->_redirectaddr,
"?") > 0)
261 $this->
fetch($this->_redirectaddr);
263 $this->
submit($this->_redirectaddr, $formvars, $formfiles);
268 if ($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0) {
270 $this->_frameurls = array();
272 while (list(, $frameurl) = each($frameurls)) {
273 if ($this->_framedepth < $this->maxframes) {
274 $this->
fetch($frameurl);
275 $this->_framedepth++;
288 $this->error =
'Invalid protocol "' . $URI_PARTS[
"scheme"] .
'"\n';
304 if ($this->
fetch($URI) !==
false) {
305 if ($this->lastredirectaddr)
307 if (is_array($this->results)) {
308 for ($x = 0; $x < count($this->results); $x++)
309 $this->results[$x] = $this->
_striplinks($this->results[$x]);
311 $this->results = $this->
_striplinks($this->results);
313 if ($this->expandlinks)
314 $this->results = $this->
_expandlinks($this->results, $URI);
330 if ($this->
fetch($URI) !==
false) {
332 if (is_array($this->results)) {
333 for ($x = 0; $x < count($this->results); $x++)
334 $this->results[$x] = $this->
_stripform($this->results[$x]);
336 $this->results = $this->
_stripform($this->results);
353 if ($this->
fetch($URI) !==
false) {
354 if (is_array($this->results)) {
355 for ($x = 0; $x < count($this->results); $x++)
356 $this->results[$x] = $this->
_striptext($this->results[$x]);
358 $this->results = $this->
_striptext($this->results);
373 if ($this->
submit($URI, $formvars, $formfiles) !==
false) {
374 if ($this->lastredirectaddr)
376 if (is_array($this->results)) {
377 for ($x = 0; $x < count($this->results); $x++) {
378 $this->results[$x] = $this->
_striplinks($this->results[$x]);
379 if ($this->expandlinks)
380 $this->results[$x] = $this->
_expandlinks($this->results[$x], $URI);
383 $this->results = $this->
_striplinks($this->results);
384 if ($this->expandlinks)
385 $this->results = $this->
_expandlinks($this->results, $URI);
401 if ($this->
submit($URI, $formvars, $formfiles) !==
false) {
402 if ($this->lastredirectaddr)
404 if (is_array($this->results)) {
405 for ($x = 0; $x < count($this->results); $x++) {
406 $this->results[$x] = $this->
_striptext($this->results[$x]);
407 if ($this->expandlinks)
408 $this->results[$x] = $this->
_expandlinks($this->results[$x], $URI);
411 $this->results = $this->
_striptext($this->results);
412 if ($this->expandlinks)
413 $this->results = $this->
_expandlinks($this->results, $URI);
428 $this->_submit_type =
"multipart/form-data";
440 $this->_submit_type =
"application/x-www-form-urlencoded";
461 preg_match_all(
"'<\s*a\s.*?href\s*=\s* # find <a href=
462 ([\"\'])? # find single or double quote
463 (?(1) (.*?)\\1 | ([^\s>]+)) # if quote found, match up to next matching
464 # quote, otherwise match up to next space
465 'isx", $document, $links);
470 while (list($key, $val) = each($links[2])) {
475 while (list($key, $val) = each($links[3])) {
493 preg_match_all(
"'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi", $document, $elements);
496 $match = implode(
"\r\n", $elements[0]);
517 $search = array(
"'<script[^>]*?>.*?</script>'si",
518 "'<[\/\!]*?[^<>]*?>'si",
520 "'&(quot|#34|#034|#x22);'i",
521 "'&(amp|#38|#038|#x26);'i",
522 "'&(lt|#60|#060|#x3c);'i",
523 "'&(gt|#62|#062|#x3e);'i",
524 "'&(nbsp|#160|#xa0);'i",
531 "'&(#39|#039|#x27);'",
582 preg_match(
"/^[^\?]+/", $URI, $match);
584 $match = preg_replace(
"|/[^\/\.]+\.[^\/\.]+$|",
"", $match[0]);
585 $match = preg_replace(
"|/$|",
"", $match);
586 $match_part = parse_url($match);
588 $match_part[
"scheme"] .
"://" . $match_part[
"host"];
590 $search = array(
"|^http://" . preg_quote($this->host) .
"|i",
592 "|^(?!http://)(?!mailto:)|i",
604 $expandedLinks = preg_replace(
$search, $replace, $links);
606 return $expandedLinks;
621 $cookie_headers =
'';
622 if ($this->passcookies && $this->_redirectaddr)
625 $URI_PARTS = parse_url($URI);
628 $headers = $http_method .
" " .
$url .
" " . $this->_httpversion .
"\r\n";
629 if (!empty($this->host) && !isset($this->rawheaders[
'Host'])) {
631 if (!empty($this->port) && $this->port !=
'80')
635 if (!empty($this->agent))
636 $headers .=
"User-Agent: " . $this->agent .
"\r\n";
637 if (!empty($this->accept))
638 $headers .=
"Accept: " . $this->accept .
"\r\n";
639 if ($this->use_gzip) {
642 if (function_exists(
'gzinflate')) {
643 $headers .=
"Accept-encoding: gzip\r\n";
646 "use_gzip is on, but PHP was built without zlib support." .
647 " Requesting file(s) without gzip encoding.",
651 if (!empty($this->referer))
652 $headers .=
"Referer: " . $this->referer .
"\r\n";
653 if (!empty($this->cookies)) {
654 if (!is_array($this->cookies))
655 $this->cookies = (array)$this->cookies;
657 reset($this->cookies);
658 if (count($this->cookies) > 0) {
659 $cookie_headers .=
'Cookie: ';
660 foreach ($this->cookies as $cookieKey => $cookieVal) {
661 $cookie_headers .= $cookieKey .
"=" . urlencode($cookieVal) .
"; ";
663 $headers .= substr($cookie_headers, 0, -2) .
"\r\n";
666 if (!empty($this->rawheaders)) {
667 if (!is_array($this->rawheaders))
668 $this->rawheaders = (array)$this->rawheaders;
669 while (list($headerKey, $headerVal) = each($this->rawheaders))
670 $headers .= $headerKey .
": " . $headerVal .
"\r\n";
672 if (!empty($content_type)) {
673 $headers .=
"Content-type: $content_type";
674 if ($content_type ==
"multipart/form-data")
679 $headers .=
"Content-length: " . strlen($body) .
"\r\n";
680 if (!empty($this->user) || !empty($this->pass))
681 $headers .=
"Authorization: Basic " . base64_encode($this->user .
":" . $this->pass) .
"\r\n";
684 if (!empty($this->proxy_user))
685 $headers .=
'Proxy-Authorization: ' .
'Basic ' . base64_encode($this->proxy_user .
':' . $this->proxy_pass) .
"\r\n";
691 if ($this->read_timeout > 0)
692 socket_set_timeout($fp, $this->read_timeout);
693 $this->timed_out =
false;
695 fwrite($fp, $headers . $body, strlen($headers . $body));
697 $this->_redirectaddr =
false;
698 unset($this->headers);
703 while ($currentHeader = fgets($fp, $this->_maxlinelen)) {
705 $this->status = -100;
709 if ($currentHeader ==
"\r\n")
713 if (preg_match(
"/^(Location:|URI:)/i", $currentHeader)) {
715 preg_match(
"/^(Location:|URI:)[ ]+(.*)/i", chop($currentHeader), $matches);
717 if (!preg_match(
"|\:\/\/|", $matches[2])) {
719 $this->_redirectaddr = $URI_PARTS[
"scheme"] .
"://" . $this->host .
":" .
$this->port;
721 if (!preg_match(
"|^/|", $matches[2]))
722 $this->_redirectaddr .=
"/" . $matches[2];
724 $this->_redirectaddr .= $matches[2];
726 $this->_redirectaddr = $matches[2];
729 if (preg_match(
"|^HTTP/|", $currentHeader)) {
730 if (preg_match(
"|^HTTP/[^\s]*\s(.*?)\s|", $currentHeader, $status)) {
731 $this->status = $status[1];
733 $this->response_code = $currentHeader;
736 if (preg_match(
"/Content-Encoding: gzip/", $currentHeader)) {
740 $this->headers[] = $currentHeader;
745 $_data = fread($fp, $this->maxlength);
746 if (strlen($_data) == 0) {
755 $results = substr($results, 10);
756 $results = gzinflate($results);
760 $this->status = -100;
766 if (preg_match(
"'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i", $results, $match)) {
767 $this->_redirectaddr = $this->
_expandlinks($match[1], $URI);
771 if (($this->_framedepth < $this->maxframes) && preg_match_all(
"'<frame\s+.*src[\s]*=[\'\"]?([^\'\">]+)'i", $results, $match)) {
773 for ($x = 0; $x < count($match[1]); $x++)
774 $this->_frameurls[] = $this->
_expandlinks($match[1][$x], $URI_PARTS[
"scheme"] .
"://" . $this->host);
776 elseif (is_array($this->results))
792 for ($x = 0; $x < count($this->headers); $x++) {
793 if (preg_match(
'/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $this->headers[$x], $match))
794 $this->cookies[$match[1]] = urldecode($match[2]);
808 if ($this->read_timeout > 0) {
809 $fp_status = socket_get_status($fp);
810 if ($fp_status[
"timed_out"]) {
811 $this->timed_out =
true;
826 if (!empty($this->proxy_host) && !empty($this->proxy_port)) {
827 $this->_isproxy =
true;
832 if ($this->scheme ==
'https') {
833 trigger_error(
"HTTPS connections over proxy are currently not supported", E_USER_ERROR);
843 $context_opts = array();
845 if ($this->scheme ==
'https') {
848 if (isset($this->cafile) || isset($this->capath)) {
849 $context_opts[
'ssl'] = array(
850 'verify_peer' =>
true,
851 'CN_match' => $this->host,
852 'disable_compression' =>
true,
855 if (isset($this->cafile))
857 if (isset($this->capath))
861 $host =
'ssl://' .
$host;
864 $context = stream_context_create($context_opts);
866 if (version_compare(PHP_VERSION,
'5.0.0',
'>')) {
867 if($this->scheme ==
'http')
868 $host =
"tcp://" .
$host;
869 $fp = stream_socket_client(
874 STREAM_CLIENT_CONNECT,
891 $this->status = $errno;
894 $this->error =
"socket creation failed (-3)";
896 $this->error =
"dns lookup failure (-4)";
898 $this->error =
"connection refused or timed out (-5)";
900 $this->error =
"connection failed (" . $errno .
")";
914 return (fclose($fp));
928 settype($formvars,
"array");
929 settype($formfiles,
"array");
932 if (count($formvars) == 0 && count($formfiles) == 0)
935 switch ($this->_submit_type) {
936 case "application/x-www-form-urlencoded":
938 while (list($key, $val) = each($formvars)) {
939 if (is_array($val) || is_object($val)) {
940 while (list($cur_key, $cur_val) = each($val)) {
941 $postdata .= urlencode($key) .
"[]=" . urlencode($cur_val) .
"&";
944 $postdata .= urlencode($key) .
"=" . urlencode($val) .
"&";
948 case "multipart/form-data":
949 $this->_mime_boundary =
"Snoopy" . md5(uniqid(microtime()));
952 while (list($key, $val) = each($formvars)) {
953 if (is_array($val) || is_object($val)) {
954 while (list($cur_key, $cur_val) = each($val)) {
955 $postdata .=
"--" . $this->_mime_boundary .
"\r\n";
956 $postdata .=
"Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n";
957 $postdata .=
"$cur_val\r\n";
960 $postdata .=
"--" . $this->_mime_boundary .
"\r\n";
961 $postdata .=
"Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
962 $postdata .=
"$val\r\n";
967 while (list($field_name, $file_names) = each($formfiles)) {
968 settype($file_names,
"array");
969 while (list(, $file_name) = each($file_names)) {
970 if (!is_readable($file_name))
continue;
972 $fp = fopen($file_name,
"r");
973 $file_content = fread($fp, filesize($file_name));
975 $base_name = basename($file_name);
977 $postdata .=
"--" . $this->_mime_boundary .
"\r\n";
978 $postdata .=
"Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n";
979 $postdata .=
"$file_content\r\n";
982 $postdata .=
"--" . $this->_mime_boundary .
"--\r\n";
_httprequest($url, $fp, $URI, $http_method, $content_type="", $body="")
_expandlinks($links, $URI)
submitlinks($URI, $formvars="", $formfiles="")
submittext($URI, $formvars="", $formfiles="")
submit($URI, $formvars="", $formfiles="")
_prepare_post_body($formvars, $formfiles)