Changeset 9303
- Timestamp:
- 10/23/2008 09:08:09 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/wp-includes/class-snoopy.php (modified) (74 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/class-snoopy.php
r8082 r9303 1 1 <?php 2 /** 3 * Snoopy - the PHP net client 4 * @author Monte Ohrt <monte@ispi.net> 5 * @copyright 1999-2000 ispi, all rights reserved 6 * @version 1.01 7 * @license GNU Lesser GPL 8 * @link http://snoopy.sourceforge.net/ 9 * @package Snoopy 10 */ 11 12 if ( !in_array('Snoopy', get_declared_classes() ) ) : 13 /** 14 * Snoopy - the PHP net client 15 * 16 * @author Monte Ohrt <monte@ispi.net> 17 * @copyright (c): 1999-2000 ispi, all rights reserved 18 * @version 1.01 19 * 2 3 /************************************************* 4 5 Snoopy - the PHP net client 6 Author: Monte Ohrt <monte@ispi.net> 7 Copyright (c): 1999-2008 New Digital Group, all rights reserved 8 Version: 1.2.4 9 20 10 * This library is free software; you can redistribute it and/or 21 11 * modify it under the terms of the GNU Lesser General Public … … 31 21 * License along with this library; if not, write to the Free Software 32 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 33 * 34 * You may contact the author of Snoopy by e-mail at: 35 * monte@ispi.net 36 * 37 * Or, write to: 38 * Monte Ohrt 39 * CTO, ispi 40 * 237 S. 70th suite 220 41 * Lincoln, NE 68510 42 * 43 * @link http://snoopy.sourceforge.net/ The latest version of Snoopy can be 44 * obtained 45 */ 23 24 You may contact the author of Snoopy by e-mail at: 25 monte@ohrt.com 26 27 The latest version of Snoopy can be obtained from: 28 http://snoopy.sourceforge.net/ 29 30 *************************************************/ 31 46 32 class Snoopy 47 33 { 48 34 /**** Public variables ****/ 49 35 50 36 /* user definable vars */ 51 37 … … 56 42 var $proxy_user = ""; // proxy user to use 57 43 var $proxy_pass = ""; // proxy password to use 58 59 var $agent = "Snoopy v1.2. 3"; // agent we masquerade as44 45 var $agent = "Snoopy v1.2.4"; // agent we masquerade as 60 46 var $referer = ""; // referer info to pass 61 47 var $cookies = array(); // array of cookies to pass … … 74 60 // NOTE: this currently does not respect 75 61 // dates, domains or paths. 76 62 77 63 var $user = ""; // user for http authentication 78 64 var $pass = ""; // password for http authentication 79 65 80 66 // http accept types 81 67 var $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"; 82 68 83 69 var $results = ""; // where the content is put 84 70 85 71 var $error = ""; // error messages sent here 86 72 var $response_code = ""; // response code returned from server 87 73 var $headers = array(); // headers returned from server sent here 88 var $maxlength = 8192; // max return data length (body)74 var $maxlength = 500000; // max return data length (body) 89 75 var $read_timeout = 0; // timeout on read operations, in seconds 90 76 // supported only since PHP 4 Beta 4 … … 108 94 // as these functions are not stable 109 95 // as of this Snoopy release. 110 111 /**** Private variables ****/ 112 96 97 /**** Private variables ****/ 98 113 99 var $_maxlinelen = 4096; // max line length (headers) 114 100 115 101 var $_httpmethod = "GET"; // default http request method 116 102 var $_httpversion = "HTTP/1.0"; // default http request version … … 122 108 var $_frameurls = array(); // frame src urls 123 109 var $_framedepth = 0; // increments on frame depth 124 110 125 111 var $_isproxy = false; // set if using a proxy server 126 112 var $_fp_timeout = 30; // timeout for socket connection … … 137 123 function fetch($URI) 138 124 { 139 125 140 126 //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS); 141 127 $URI_PARTS = parse_url($URI); … … 148 134 if (empty($URI_PARTS["path"])) 149 135 $URI_PARTS["path"] = ''; 150 136 151 137 switch(strtolower($URI_PARTS["scheme"])) 152 138 { … … 168 154 $this->_httprequest($path, $fp, $URI, $this->_httpmethod); 169 155 } 170 156 171 157 $this->_disconnect($fp); 172 158 … … 191 177 $frameurls = $this->_frameurls; 192 178 $this->_frameurls = array(); 193 179 194 180 while(list(,$frameurl) = each($frameurls)) 195 181 { … … 202 188 break; 203 189 } 204 } 190 } 205 191 } 206 192 else … … 208 194 return false; 209 195 } 210 return true; 196 return true; 211 197 break; 212 198 case "https": … … 262 248 break; 263 249 } 264 } 265 return true; 250 } 251 return true; 266 252 break; 267 253 default: … … 270 256 return false; 271 257 break; 272 } 258 } 273 259 return true; 274 260 } … … 288 274 { 289 275 unset($postdata); 290 276 291 277 $postdata = $this->_prepare_post_body($formvars, $formfiles); 292 278 293 279 $URI_PARTS = parse_url($URI); 294 280 if (!empty($URI_PARTS["user"])) … … 320 306 $this->_httprequest($path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata); 321 307 } 322 308 323 309 $this->_disconnect($fp); 324 310 … … 327 313 /* url was redirected, check if we've hit the max depth */ 328 314 if($this->maxredirs > $this->_redirectdepth) 329 { 315 { 330 316 if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr)) 331 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]); 332 317 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]); 318 333 319 // only follow redirect if it's on this site, or offsiteok is true 334 320 if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok) … … 349 335 $frameurls = $this->_frameurls; 350 336 $this->_frameurls = array(); 351 337 352 338 while(list(,$frameurl) = each($frameurls)) 353 { 339 { 354 340 if($this->_framedepth < $this->maxframes) 355 341 { … … 360 346 break; 361 347 } 362 } 363 348 } 349 364 350 } 365 351 else … … 367 353 return false; 368 354 } 369 return true; 355 return true; 370 356 break; 371 357 case "https": … … 394 380 /* url was redirected, check if we've hit the max depth */ 395 381 if($this->maxredirs > $this->_redirectdepth) 396 { 382 { 397 383 if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr)) 398 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]); 384 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]); 399 385 400 386 // only follow redirect if it's on this site, or offsiteok is true … … 418 404 419 405 while(list(,$frameurl) = each($frameurls)) 420 { 406 { 421 407 if($this->_framedepth < $this->maxframes) 422 408 { … … 427 413 break; 428 414 } 429 } 430 return true; 415 } 416 return true; 431 417 break; 432 418 433 419 default: 434 420 // not a valid protocol … … 436 422 return false; 437 423 break; 438 } 424 } 439 425 return true; 440 426 } … … 450 436 { 451 437 if ($this->fetch($URI)) 452 { 438 { 453 439 if($this->lastredirectaddr) 454 440 $URI = $this->lastredirectaddr; … … 478 464 function fetchform($URI) 479 465 { 480 466 481 467 if ($this->fetch($URI)) 482 { 468 { 483 469 484 470 if(is_array($this->results)) … … 489 475 else 490 476 $this->results = $this->_stripform($this->results); 491 477 492 478 return true; 493 479 } … … 495 481 return false; 496 482 } 497 498 483 484 499 485 /*======================================================================*\ 500 486 Function: fetchtext … … 507 493 { 508 494 if($this->fetch($URI)) 509 { 495 { 510 496 if(is_array($this->results)) 511 497 { … … 531 517 { 532 518 if($this->submit($URI,$formvars, $formfiles)) 533 { 519 { 534 520 if($this->lastredirectaddr) 535 521 $URI = $this->lastredirectaddr; … … 565 551 { 566 552 if($this->submit($URI,$formvars, $formfiles)) 567 { 553 { 568 554 if($this->lastredirectaddr) 569 555 $URI = $this->lastredirectaddr; … … 589 575 } 590 576 591 577 592 578 593 579 /*======================================================================*\ … … 601 587 } 602 588 603 589 604 590 /*======================================================================*\ 605 591 Function: set_submit_normal … … 612 598 } 613 599 614 615 600 601 616 602 617 603 /*======================================================================*\ 618 604 Private functions 619 605 \*======================================================================*/ 620 621 606 607 622 608 /*======================================================================*\ 623 609 Function: _striplinks … … 628 614 629 615 function _striplinks($document) 630 { 616 { 631 617 preg_match_all("'<\s*a\s.*?href\s*=\s* # find <a href= 632 618 ([\"\'])? # find single or double quote … … 634 620 # quote, otherwise match up to next space 635 621 'isx",$document,$links); 636 622 637 623 638 624 // catenate the non-empty matches from the conditional subpattern … … 642 628 if(!empty($val)) 643 629 $match[] = $val; 644 } 645 630 } 631 646 632 while(list($key,$val) = each($links[3])) 647 633 { 648 634 if(!empty($val)) 649 635 $match[] = $val; 650 } 651 636 } 637 652 638 // return the links 653 639 return $match; … … 662 648 663 649 function _stripform($document) 664 { 650 { 665 651 preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements); 666 652 667 653 // catenate the matches 668 654 $match = implode("\r\n",$elements[0]); 669 655 670 656 // return the links 671 657 return $match; 672 658 } 673 659 674 675 660 661 676 662 /*======================================================================*\ 677 663 Function: _striptext … … 683 669 function _striptext($document) 684 670 { 685 671 686 672 // I didn't use preg eval (//e) since that is only available in PHP 4.0. 687 673 // so, list your entities one by one here. I included some of the 688 674 // more common ones. 689 675 690 676 $search = array("'<script[^>]*?>.*?</script>'si", // strip out javascript 691 677 "'<[\/\!]*?[^<>]*?>'si", // strip out html tags … … 736 722 "ß", 737 723 ); 738 724 739 725 $text = preg_replace($search,$replace,$document); 740 726 741 727 return $text; 742 728 } … … 752 738 function _expandlinks($links,$URI) 753 739 { 754 740 755 741 preg_match("/^[^\?]+/",$URI,$match); 756 742 … … 760 746 $match_root = 761 747 $match_part["scheme"]."://".$match_part["host"]; 762 748 763 749 $search = array( "|^http://".preg_quote($this->host)."|i", 764 750 "|^(\/)|i", … … 767 753 "|/[^\/]+/\.\./|" 768 754 ); 769 755 770 756 $replace = array( "", 771 757 $match_root."/", … … 773 759 "/", 774 760 "/" 775 ); 776 761 ); 762 777 763 $expandedLinks = preg_replace($search,$replace,$links); 778 764 … … 787 773 $URI the full URI 788 774 $body body contents to send if any (POST) 789 Output: 790 \*======================================================================*/ 791 775 Output: 776 \*======================================================================*/ 777 792 778 function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="") 793 779 { … … 795 781 if($this->passcookies && $this->_redirectaddr) 796 782 $this->setcookies(); 797 783 798 784 $URI_PARTS = parse_url($URI); 799 785 if(empty($url)) 800 786 $url = "/"; 801 $headers = $http_method." ".$url." ".$this->_httpversion."\r\n"; 787 $headers = $http_method." ".$url." ".$this->_httpversion."\r\n"; 802 788 if(!empty($this->agent)) 803 789 $headers .= "User-Agent: ".$this->agent."\r\n"; 804 790 if(!empty($this->host) && !isset($this->rawheaders['Host'])) { 805 791 $headers .= "Host: ".$this->host; 806 if(!empty($this->port) && $this->port != 80)792 if(!empty($this->port)) 807 793 $headers .= ":".$this->port; 808 794 $headers .= "\r\n"; … … 813 799 $headers .= "Referer: ".$this->referer."\r\n"; 814 800 if(!empty($this->cookies)) 815 { 801 { 816 802 if(!is_array($this->cookies)) 817 803 $this->cookies = (array)$this->cookies; 818 804 819 805 reset($this->cookies); 820 806 if ( count($this->cookies) > 0 ) { … … 824 810 } 825 811 $headers .= substr($cookie_headers,0,-2) . "\r\n"; 826 } 812 } 827 813 } 828 814 if(!empty($this->rawheaders)) … … 839 825 $headers .= "\r\n"; 840 826 } 841 if(!empty($body)) 827 if(!empty($body)) 842 828 $headers .= "Content-length: ".strlen($body)."\r\n"; 843 if(!empty($this->user) || !empty($this->pass)) 829 if(!empty($this->user) || !empty($this->pass)) 844 830 $headers .= "Authorization: Basic ".base64_encode($this->user.":".$this->pass)."\r\n"; 845 831 846 832 //add proxy auth headers 847 if(!empty($this->proxy_user)) 833 if(!empty($this->proxy_user)) 848 834 $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass)."\r\n"; 849 835 850 836 851 837 $headers .= "\r\n"; 852 838 853 839 // set the read timeout if needed 854 840 if ($this->read_timeout > 0) 855 841 socket_set_timeout($fp, $this->read_timeout); 856 842 $this->timed_out = false; 857 843 858 844 fwrite($fp,$headers.$body,strlen($headers.$body)); 859 845 860 846 $this->_redirectaddr = false; 861 847 unset($this->headers); 862 848 863 849 while($currentHeader = fgets($fp,$this->_maxlinelen)) 864 850 { … … 868 854 return false; 869 855 } 870 856 871 857 if($currentHeader == "\r\n") 872 858 break; 873 859 874 860 // if a header begins with Location: or URI:, set the redirect 875 861 if(preg_match("/^(Location:|URI:)/i",$currentHeader)) … … 891 877 $this->_redirectaddr = $matches[2]; 892 878 } 893 879 894 880 if(preg_match("|^HTTP/|",$currentHeader)) 895 881 { … … 897 883 { 898 884 $this->status= $status[1]; 899 } 885 } 900 886 $this->response_code = $currentHeader; 901 887 } 902 888 903 889 $this->headers[] = $currentHeader; 904 890 } … … 918 904 return false; 919 905 } 920 906 921 907 // check if there is a a redirect meta tag 922 908 923 909 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match)) 924 910 925 911 { 926 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 912 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 927 913 } 928 914 … … 940 926 else 941 927 $this->results = $results; 942 928 943 929 return true; 944 930 } … … 950 936 $URI the full URI 951 937 $body body contents to send if any (POST) 952 Output: 953 \*======================================================================*/ 954 938 Output: 939 \*======================================================================*/ 940 955 941 function _httpsrequest($url,$URI,$http_method,$content_type="",$body="") 956 { 942 { 957 943 if($this->passcookies && $this->_redirectaddr) 958 944 $this->setcookies(); 959 945 960 $headers = array(); 961 946 $headers = array(); 947 962 948 $URI_PARTS = parse_url($URI); 963 949 if(empty($url)) 964 950 $url = "/"; 965 951 // GET ... header not needed for curl 966 //$headers[] = $http_method." ".$url." ".$this->_httpversion; 952 //$headers[] = $http_method." ".$url." ".$this->_httpversion; 967 953 if(!empty($this->agent)) 968 954 $headers[] = "User-Agent: ".$this->agent; … … 977 963 $headers[] = "Referer: ".$this->referer; 978 964 if(!empty($this->cookies)) 979 { 965 { 980 966 if(!is_array($this->cookies)) 981 967 $this->cookies = (array)$this->cookies; 982 968 983 969 reset($this->cookies); 984 970 if ( count($this->cookies) > 0 ) { … … 1003 989 $headers[] = "Content-type: $content_type"; 1004 990 } 1005 if(!empty($body)) 991 if(!empty($body)) 1006 992 $headers[] = "Content-length: ".strlen($body); 1007 if(!empty($this->user) || !empty($this->pass)) 993 if(!empty($this->user) || !empty($this->pass)) 1008 994 $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass); 1009 995 1010 996 for($curr_header = 0; $curr_header < count($headers); $curr_header++) { 1011 997 $safer_header = strtr( $headers[$curr_header], "\"", " " ); 1012 998 $cmdline_params .= " -H \"".$safer_header."\""; 1013 999 } 1014 1000 1015 1001 if(!empty($body)) 1016 1002 $cmdline_params .= " -d \"$body\""; 1017 1003 1018 1004 if($this->read_timeout > 0) 1019 1005 $cmdline_params .= " -m ".$this->read_timeout; 1020 1006 1021 1007 $headerfile = tempnam($temp_dir, "sno"); 1022 1008 1023 $safer_URI = strtr( $URI, "\"", " " ); // strip quotes from the URI to avoid shell access 1024 exec(escapeshellcmd($this->curl_path." -D \"$headerfile\"".$cmdline_params." \"".$safer_URI."\""),$results,$return); 1025 1009 exec($this->curl_path." -k -D \"$headerfile\"".$cmdline_params." \"".escapeshellcmd($URI)."\"",$results,$return); 1010 1026 1011 if($return) 1027 1012 { … … 1029 1014 return false; 1030 1015 } 1031 1032 1016 1017 1033 1018 $results = implode("\r\n",$results); 1034 1019 1035 1020 $result_headers = file("$headerfile"); 1036 1021 1037 1022 $this->_redirectaddr = false; 1038 1023 unset($this->headers); 1039 1024 1040 1025 for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++) 1041 1026 { 1042 1027 1043 1028 // if a header begins with Location: or URI:, set the redirect 1044 1029 if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader])) … … 1060 1045 $this->_redirectaddr = $matches[2]; 1061 1046 } 1062 1047 1063 1048 if(preg_match("|^HTTP/|",$result_headers[$currentHeader])) 1064 1049 $this->response_code = $result_headers[$currentHeader]; … … 1068 1053 1069 1054 // check if there is a a redirect meta tag 1070 1055 1071 1056 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match)) 1072 1057 { 1073 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 1058 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 1074 1059 } 1075 1060 … … 1089 1074 1090 1075 unlink("$headerfile"); 1091 1076 1092 1077 return true; 1093 1078 } … … 1097 1082 Purpose: set cookies for a redirection 1098 1083 \*======================================================================*/ 1099 1084 1100 1085 function setcookies() 1101 1086 { … … 1107 1092 } 1108 1093 1109 1094 1110 1095 /*======================================================================*\ 1111 1096 Function: _check_timeout … … 1131 1116 Input: $fp file pointer 1132 1117 \*======================================================================*/ 1133 1118 1134 1119 function _connect(&$fp) 1135 1120 { … … 1137 1122 { 1138 1123 $this->_isproxy = true; 1139 1124 1140 1125 $host = $this->proxy_host; 1141 1126 $port = $this->proxy_port; … … 1146 1131 $port = $this->port; 1147 1132 } 1148 1133 1149 1134 $this->status = 0; 1150 1135 1151 1136 if($fp = fsockopen( 1152 1137 $host, … … 1184 1169 Input: $fp file pointer 1185 1170 \*======================================================================*/ 1186 1171 1187 1172 function _disconnect($fp) 1188 1173 { … … 1190 1175 } 1191 1176 1192 1177 1193 1178 /*======================================================================*\ 1194 1179 Function: _prepare_post_body … … 1198 1183 Output: post body 1199 1184 \*======================================================================*/ 1200 1185 1201 1186 function _prepare_post_body($formvars, $formfiles) 1202 1187 { … … 1207 1192 if (count($formvars) == 0 && count($formfiles) == 0) 1208 1193 return; 1209 1194 1210 1195 switch ($this->_submit_type) { 1211 1196 case "application/x-www-form-urlencoded": … … 1223 1208 case "multipart/form-data": 1224 1209 $this->_mime_boundary = "Snoopy".md5(uniqid(microtime())); 1225 1210 1226 1211 reset($formvars); 1227 1212 while(list($key,$val) = each($formvars)) { … … 1238 1223 } 1239 1224 } 1240 1225 1241 1226 reset($formfiles); 1242 1227 while (list($field_name, $file_names) = each($formfiles)) { … … 1246 1231 1247 1232 $fp = fopen($file_name, "r"); 1248 while (!feof($fp)) { 1249 $file_content .= fread($fp, filesize($file_name)); 1250 } 1233 $file_content = fread($fp, filesize($file_name)); 1251 1234 fclose($fp); 1252 1235 $base_name = basename($file_name); … … 1264 1247 } 1265 1248 } 1266 endif;1267 1249 1268 1250 ?>
Note: See TracChangeset
for help on using the changeset viewer.