Changeset 3517 for trunk/wp-includes/class-snoopy.php
- Timestamp:
- 02/12/2006 07:53:23 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/class-snoopy.php
r3235 r3517 40 40 { 41 41 /**** Public variables ****/ 42 42 43 43 /* user definable vars */ 44 44 … … 49 49 var $proxy_user = ""; // proxy user to use 50 50 var $proxy_pass = ""; // proxy password to use 51 51 52 52 var $agent = "Snoopy v1.2.3"; // agent we masquerade as 53 53 var $referer = ""; // referer info to pass … … 67 67 // NOTE: this currently does not respect 68 68 // dates, domains or paths. 69 69 70 70 var $user = ""; // user for http authentication 71 71 var $pass = ""; // password for http authentication 72 72 73 73 // http accept types 74 74 var $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"; 75 75 76 76 var $results = ""; // where the content is put 77 77 78 78 var $error = ""; // error messages sent here 79 79 var $response_code = ""; // response code returned from server … … 101 101 // as these functions are not stable 102 102 // as of this Snoopy release. 103 104 /**** Private variables ****/ 105 103 104 /**** Private variables ****/ 105 106 106 var $_maxlinelen = 4096; // max line length (headers) 107 107 108 108 var $_httpmethod = "GET"; // default http request method 109 109 var $_httpversion = "HTTP/1.0"; // default http request version … … 115 115 var $_frameurls = array(); // frame src urls 116 116 var $_framedepth = 0; // increments on frame depth 117 117 118 118 var $_isproxy = false; // set if using a proxy server 119 119 var $_fp_timeout = 30; // timeout for socket connection … … 130 130 function fetch($URI) 131 131 { 132 132 133 133 //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS); 134 134 $URI_PARTS = parse_url($URI); … … 141 141 if (empty($URI_PARTS["path"])) 142 142 $URI_PARTS["path"] = ''; 143 143 144 144 switch(strtolower($URI_PARTS["scheme"])) 145 145 { … … 161 161 $this->_httprequest($path, $fp, $URI, $this->_httpmethod); 162 162 } 163 163 164 164 $this->_disconnect($fp); 165 165 … … 184 184 $frameurls = $this->_frameurls; 185 185 $this->_frameurls = array(); 186 186 187 187 while(list(,$frameurl) = each($frameurls)) 188 188 { … … 195 195 break; 196 196 } 197 } 197 } 198 198 } 199 199 else … … 201 201 return false; 202 202 } 203 return true; 203 return true; 204 204 break; 205 205 case "https": … … 255 255 break; 256 256 } 257 } 258 return true; 257 } 258 return true; 259 259 break; 260 260 default: … … 263 263 return false; 264 264 break; 265 } 265 } 266 266 return true; 267 267 } … … 281 281 { 282 282 unset($postdata); 283 283 284 284 $postdata = $this->_prepare_post_body($formvars, $formfiles); 285 285 286 286 $URI_PARTS = parse_url($URI); 287 287 if (!empty($URI_PARTS["user"])) … … 313 313 $this->_httprequest($path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata); 314 314 } 315 315 316 316 $this->_disconnect($fp); 317 317 … … 320 320 /* url was redirected, check if we've hit the max depth */ 321 321 if($this->maxredirs > $this->_redirectdepth) 322 { 322 { 323 323 if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr)) 324 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]); 325 324 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]); 325 326 326 // only follow redirect if it's on this site, or offsiteok is true 327 327 if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok) … … 342 342 $frameurls = $this->_frameurls; 343 343 $this->_frameurls = array(); 344 344 345 345 while(list(,$frameurl) = each($frameurls)) 346 { 346 { 347 347 if($this->_framedepth < $this->maxframes) 348 348 { … … 353 353 break; 354 354 } 355 } 356 355 } 356 357 357 } 358 358 else … … 360 360 return false; 361 361 } 362 return true; 362 return true; 363 363 break; 364 364 case "https": … … 387 387 /* url was redirected, check if we've hit the max depth */ 388 388 if($this->maxredirs > $this->_redirectdepth) 389 { 389 { 390 390 if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr)) 391 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]); 391 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]); 392 392 393 393 // only follow redirect if it's on this site, or offsiteok is true … … 411 411 412 412 while(list(,$frameurl) = each($frameurls)) 413 { 413 { 414 414 if($this->_framedepth < $this->maxframes) 415 415 { … … 420 420 break; 421 421 } 422 } 423 return true; 422 } 423 return true; 424 424 break; 425 425 426 426 default: 427 427 // not a valid protocol … … 429 429 return false; 430 430 break; 431 } 431 } 432 432 return true; 433 433 } … … 443 443 { 444 444 if ($this->fetch($URI)) 445 { 445 { 446 446 if($this->lastredirectaddr) 447 447 $URI = $this->lastredirectaddr; … … 471 471 function fetchform($URI) 472 472 { 473 473 474 474 if ($this->fetch($URI)) 475 { 475 { 476 476 477 477 if(is_array($this->results)) … … 482 482 else 483 483 $this->results = $this->_stripform($this->results); 484 484 485 485 return true; 486 486 } … … 488 488 return false; 489 489 } 490 491 490 491 492 492 /*======================================================================*\ 493 493 Function: fetchtext … … 500 500 { 501 501 if($this->fetch($URI)) 502 { 502 { 503 503 if(is_array($this->results)) 504 504 { … … 524 524 { 525 525 if($this->submit($URI,$formvars, $formfiles)) 526 { 526 { 527 527 if($this->lastredirectaddr) 528 528 $URI = $this->lastredirectaddr; … … 558 558 { 559 559 if($this->submit($URI,$formvars, $formfiles)) 560 { 560 { 561 561 if($this->lastredirectaddr) 562 562 $URI = $this->lastredirectaddr; … … 582 582 } 583 583 584 584 585 585 586 586 /*======================================================================*\ … … 594 594 } 595 595 596 596 597 597 /*======================================================================*\ 598 598 Function: set_submit_normal … … 605 605 } 606 606 607 608 607 608 609 609 610 610 /*======================================================================*\ 611 611 Private functions 612 612 \*======================================================================*/ 613 614 613 614 615 615 /*======================================================================*\ 616 616 Function: _striplinks … … 621 621 622 622 function _striplinks($document) 623 { 623 { 624 624 preg_match_all("'<\s*a\s.*?href\s*=\s* # find <a href= 625 625 ([\"\'])? # find single or double quote … … 627 627 # quote, otherwise match up to next space 628 628 'isx",$document,$links); 629 629 630 630 631 631 // catenate the non-empty matches from the conditional subpattern … … 635 635 if(!empty($val)) 636 636 $match[] = $val; 637 } 638 637 } 638 639 639 while(list($key,$val) = each($links[3])) 640 640 { 641 641 if(!empty($val)) 642 642 $match[] = $val; 643 } 644 643 } 644 645 645 // return the links 646 646 return $match; … … 655 655 656 656 function _stripform($document) 657 { 657 { 658 658 preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements); 659 659 660 660 // catenate the matches 661 661 $match = implode("\r\n",$elements[0]); 662 662 663 663 // return the links 664 664 return $match; 665 665 } 666 666 667 668 667 668 669 669 /*======================================================================*\ 670 670 Function: _striptext … … 676 676 function _striptext($document) 677 677 { 678 678 679 679 // I didn't use preg eval (//e) since that is only available in PHP 4.0. 680 680 // so, list your entities one by one here. I included some of the 681 681 // more common ones. 682 682 683 683 $search = array("'<script[^>]*?>.*?</script>'si", // strip out javascript 684 684 "'<[\/\!]*?[^<>]*?>'si", // strip out html tags … … 729 729 "ß", 730 730 ); 731 731 732 732 $text = preg_replace($search,$replace,$document); 733 733 734 734 return $text; 735 735 } … … 745 745 function _expandlinks($links,$URI) 746 746 { 747 747 748 748 preg_match("/^[^\?]+/",$URI,$match); 749 749 … … 753 753 $match_root = 754 754 $match_part["scheme"]."://".$match_part["host"]; 755 755 756 756 $search = array( "|^http://".preg_quote($this->host)."|i", 757 757 "|^(\/)|i", … … 760 760 "|/[^\/]+/\.\./|" 761 761 ); 762 762 763 763 $replace = array( "", 764 764 $match_root."/", … … 766 766 "/", 767 767 "/" 768 ); 769 768 ); 769 770 770 $expandedLinks = preg_replace($search,$replace,$links); 771 771 … … 780 780 $URI the full URI 781 781 $body body contents to send if any (POST) 782 Output: 783 \*======================================================================*/ 784 782 Output: 783 \*======================================================================*/ 784 785 785 function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="") 786 786 { … … 788 788 if($this->passcookies && $this->_redirectaddr) 789 789 $this->setcookies(); 790 790 791 791 $URI_PARTS = parse_url($URI); 792 792 if(empty($url)) 793 793 $url = "/"; 794 $headers = $http_method." ".$url." ".$this->_httpversion."\r\n"; 794 $headers = $http_method." ".$url." ".$this->_httpversion."\r\n"; 795 795 if(!empty($this->agent)) 796 796 $headers .= "User-Agent: ".$this->agent."\r\n"; … … 806 806 $headers .= "Referer: ".$this->referer."\r\n"; 807 807 if(!empty($this->cookies)) 808 { 808 { 809 809 if(!is_array($this->cookies)) 810 810 $this->cookies = (array)$this->cookies; 811 811 812 812 reset($this->cookies); 813 813 if ( count($this->cookies) > 0 ) { … … 832 832 $headers .= "\r\n"; 833 833 } 834 if(!empty($body)) 834 if(!empty($body)) 835 835 $headers .= "Content-length: ".strlen($body)."\r\n"; 836 if(!empty($this->user) || !empty($this->pass)) 836 if(!empty($this->user) || !empty($this->pass)) 837 837 $headers .= "Authorization: Basic ".base64_encode($this->user.":".$this->pass)."\r\n"; 838 838 839 839 //add proxy auth headers 840 if(!empty($this->proxy_user)) 840 if(!empty($this->proxy_user)) 841 841 $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass)."\r\n"; 842 842 843 843 844 844 $headers .= "\r\n"; 845 845 846 846 // set the read timeout if needed 847 847 if ($this->read_timeout > 0) 848 848 socket_set_timeout($fp, $this->read_timeout); 849 849 $this->timed_out = false; 850 850 851 851 fwrite($fp,$headers.$body,strlen($headers.$body)); 852 852 853 853 $this->_redirectaddr = false; 854 854 unset($this->headers); 855 855 856 856 while($currentHeader = fgets($fp,$this->_maxlinelen)) 857 857 { … … 861 861 return false; 862 862 } 863 863 864 864 if($currentHeader == "\r\n") 865 865 break; 866 866 867 867 // if a header begins with Location: or URI:, set the redirect 868 868 if(preg_match("/^(Location:|URI:)/i",$currentHeader)) … … 884 884 $this->_redirectaddr = $matches[2]; 885 885 } 886 886 887 887 if(preg_match("|^HTTP/|",$currentHeader)) 888 888 { … … 890 890 { 891 891 $this->status= $status[1]; 892 } 892 } 893 893 $this->response_code = $currentHeader; 894 894 } 895 895 896 896 $this->headers[] = $currentHeader; 897 897 } … … 911 911 return false; 912 912 } 913 913 914 914 // check if there is a a redirect meta tag 915 915 916 916 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match)) 917 917 918 918 { 919 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 919 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 920 920 } 921 921 … … 933 933 else 934 934 $this->results = $results; 935 935 936 936 return true; 937 937 } … … 943 943 $URI the full URI 944 944 $body body contents to send if any (POST) 945 Output: 946 \*======================================================================*/ 947 945 Output: 946 \*======================================================================*/ 947 948 948 function _httpsrequest($url,$URI,$http_method,$content_type="",$body="") 949 949 { … … 951 951 $this->setcookies(); 952 952 953 $headers = array(); 954 953 $headers = array(); 954 955 955 $URI_PARTS = parse_url($URI); 956 956 if(empty($url)) 957 957 $url = "/"; 958 958 // GET ... header not needed for curl 959 //$headers[] = $http_method." ".$url." ".$this->_httpversion; 959 //$headers[] = $http_method." ".$url." ".$this->_httpversion; 960 960 if(!empty($this->agent)) 961 961 $headers[] = "User-Agent: ".$this->agent; … … 970 970 $headers[] = "Referer: ".$this->referer; 971 971 if(!empty($this->cookies)) 972 { 972 { 973 973 if(!is_array($this->cookies)) 974 974 $this->cookies = (array)$this->cookies; 975 975 976 976 reset($this->cookies); 977 977 if ( count($this->cookies) > 0 ) { … … 996 996 $headers[] = "Content-type: $content_type"; 997 997 } 998 if(!empty($body)) 998 if(!empty($body)) 999 999 $headers[] = "Content-length: ".strlen($body); 1000 if(!empty($this->user) || !empty($this->pass)) 1000 if(!empty($this->user) || !empty($this->pass)) 1001 1001 $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass); 1002 1002 1003 1003 for($curr_header = 0; $curr_header < count($headers); $curr_header++) { 1004 1004 $safer_header = strtr( $headers[$curr_header], "\"", " " ); 1005 1005 $cmdline_params .= " -H \"".$safer_header."\""; 1006 1006 } 1007 1007 1008 1008 if(!empty($body)) 1009 1009 $cmdline_params .= " -d \"$body\""; 1010 1010 1011 1011 if($this->read_timeout > 0) 1012 1012 $cmdline_params .= " -m ".$this->read_timeout; 1013 1013 1014 1014 $headerfile = tempnam($temp_dir, "sno"); 1015 1015 1016 1016 $safer_URI = strtr( $URI, "\"", " " ); // strip quotes from the URI to avoid shell access 1017 1017 exec(escapeshellcmd($this->curl_path." -D \"$headerfile\"".$cmdline_params." \"".$safer_URI."\""),$results,$return); 1018 1018 1019 1019 if($return) 1020 1020 { … … 1022 1022 return false; 1023 1023 } 1024 1025 1024 1025 1026 1026 $results = implode("\r\n",$results); 1027 1027 1028 1028 $result_headers = file("$headerfile"); 1029 1029 1030 1030 $this->_redirectaddr = false; 1031 1031 unset($this->headers); 1032 1032 1033 1033 for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++) 1034 1034 { 1035 1035 1036 1036 // if a header begins with Location: or URI:, set the redirect 1037 1037 if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader])) … … 1053 1053 $this->_redirectaddr = $matches[2]; 1054 1054 } 1055 1055 1056 1056 if(preg_match("|^HTTP/|",$result_headers[$currentHeader])) 1057 1057 $this->response_code = $result_headers[$currentHeader]; … … 1061 1061 1062 1062 // check if there is a a redirect meta tag 1063 1063 1064 1064 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match)) 1065 1065 { 1066 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 1066 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 1067 1067 } 1068 1068 … … 1082 1082 1083 1083 unlink("$headerfile"); 1084 1084 1085 1085 return true; 1086 1086 } … … 1090 1090 Purpose: set cookies for a redirection 1091 1091 \*======================================================================*/ 1092 1092 1093 1093 function setcookies() 1094 1094 { … … 1100 1100 } 1101 1101 1102 1102 1103 1103 /*======================================================================*\ 1104 1104 Function: _check_timeout … … 1124 1124 Input: $fp file pointer 1125 1125 \*======================================================================*/ 1126 1126 1127 1127 function _connect(&$fp) 1128 1128 { … … 1130 1130 { 1131 1131 $this->_isproxy = true; 1132 1132 1133 1133 $host = $this->proxy_host; 1134 1134 $port = $this->proxy_port; … … 1139 1139 $port = $this->port; 1140 1140 } 1141 1141 1142 1142 $this->status = 0; 1143 1143 1144 1144 if($fp = fsockopen( 1145 1145 $host, … … 1177 1177 Input: $fp file pointer 1178 1178 \*======================================================================*/ 1179 1179 1180 1180 function _disconnect($fp) 1181 1181 { … … 1183 1183 } 1184 1184 1185 1185 1186 1186 /*======================================================================*\ 1187 1187 Function: _prepare_post_body … … 1191 1191 Output: post body 1192 1192 \*======================================================================*/ 1193 1193 1194 1194 function _prepare_post_body($formvars, $formfiles) 1195 1195 { … … 1200 1200 if (count($formvars) == 0 && count($formfiles) == 0) 1201 1201 return; 1202 1202 1203 1203 switch ($this->_submit_type) { 1204 1204 case "application/x-www-form-urlencoded": … … 1216 1216 case "multipart/form-data": 1217 1217 $this->_mime_boundary = "Snoopy".md5(uniqid(microtime())); 1218 1218 1219 1219 reset($formvars); 1220 1220 while(list($key,$val) = each($formvars)) { … … 1231 1231 } 1232 1232 } 1233 1233 1234 1234 reset($formfiles); 1235 1235 while (list($field_name, $file_names) = each($formfiles)) {
Note: See TracChangeset
for help on using the changeset viewer.