Changeset 9304
- Timestamp:
- 10/23/08 21:10:50 (3 months ago)
- Files:
-
- branches/2.6/wp-includes/class-snoopy.php (modified) (74 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/2.6/wp-includes/class-snoopy.php
r8082 r9304 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 … … 728 714 chr(39), 729 715 chr(128), 730 "ä", 731 "ö", 732 "ü", 733 "Ä", 734 "Ö", 735 "Ü", 736 "ß", 716 "� 717 "� "� "�, 718 "�, 719 "�, 720 "�, 737 721 ); 738 722 739 723 $text = preg_replace($search,$replace,$document); 740 724 741 725 return $text; 742 726 } … … 752 736 function _expandlinks($links,$URI) 753 737 { 754 738 755 739 preg_match("/^[^\?]+/",$URI,$match); 756 740 … … 760 744 $match_root = 761 745 $match_part["scheme"]."://".$match_part["host"]; 762 746 763 747 $search = array( "|^http://".preg_quote($this->host)."|i", 764 748 "|^(\/)|i", … … 767 751 "|/[^\/]+/\.\./|" 768 752 ); 769 753 770 754 $replace = array( "", 771 755 $match_root."/", … … 773 757 "/", 774 758 "/" 775 ); 776 759 ); 760 777 761 $expandedLinks = preg_replace($search,$replace,$links); 778 762 … … 787 771 $URI the full URI 788 772 $body body contents to send if any (POST) 789 Output: 790 \*======================================================================*/ 791 773 Output: 774 \*======================================================================*/ 775 792 776 function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="") 793 777 { … … 795 779 if($this->passcookies && $this->_redirectaddr) 796 780 $this->setcookies(); 797 781 798 782 $URI_PARTS = parse_url($URI); 799 783 if(empty($url)) 800 784 $url = "/"; 801 $headers = $http_method." ".$url." ".$this->_httpversion."\r\n"; 785 $headers = $http_method." ".$url." ".$this->_httpversion."\r\n"; 802 786 if(!empty($this->agent)) 803 787 $headers .= "User-Agent: ".$this->agent."\r\n"; 804 788 if(!empty($this->host) && !isset($this->rawheaders['Host'])) { 805 789 $headers .= "Host: ".$this->host; 806 if(!empty($this->port) && $this->port != 80)790 if(!empty($this->port)) 807 791 $headers .= ":".$this->port; 808 792 $headers .= "\r\n"; … … 813 797 $headers .= "Referer: ".$this->referer."\r\n"; 814 798 if(!empty($this->cookies)) 815 { 799 { 816 800 if(!is_array($this->cookies)) 817 801 $this->cookies = (array)$this->cookies; 818 802 819 803 reset($this->cookies); 820 804 if ( count($this->cookies) > 0 ) { … … 824 808 } 825 809 $headers .= substr($cookie_headers,0,-2) . "\r\n"; 826 } 810 } 827 811 } 828 812 if(!empty($this->rawheaders)) … … 839 823 $headers .= "\r\n"; 840 824 } 841 if(!empty($body)) 825 if(!empty($body)) 842 826 $headers .= "Content-length: ".strlen($body)."\r\n"; 843 if(!empty($this->user) || !empty($this->pass)) 827 if(!empty($this->user) || !empty($this->pass)) 844 828 $headers .= "Authorization: Basic ".base64_encode($this->user.":".$this->pass)."\r\n"; 845 829 846 830 //add proxy auth headers 847 if(!empty($this->proxy_user)) 831 if(!empty($this->proxy_user)) 848 832 $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass)."\r\n"; 849 833 850 834 851 835 $headers .= "\r\n"; 852 836 853 837 // set the read timeout if needed 854 838 if ($this->read_timeout > 0) 855 839 socket_set_timeout($fp, $this->read_timeout); 856 840 $this->timed_out = false; 857 841 858 842 fwrite($fp,$headers.$body,strlen($headers.$body)); 859 843 860 844 $this->_redirectaddr = false; 861 845 unset($this->headers); 862 846 863 847 while($currentHeader = fgets($fp,$this->_maxlinelen)) 864 848 { … … 868 852 return false; 869 853 } 870 854 871 855 if($currentHeader == "\r\n") 872 856 break; 873 857 874 858 // if a header begins with Location: or URI:, set the redirect 875 859 if(preg_match("/^(Location:|URI:)/i",$currentHeader)) … … 891 875 $this->_redirectaddr = $matches[2]; 892 876 } 893 877 894 878 if(preg_match("|^HTTP/|",$currentHeader)) 895 879 { … … 897 881 { 898 882 $this->status= $status[1]; 899 } 883 } 900 884 $this->response_code = $currentHeader; 901 885 } 902 886 903 887 $this->headers[] = $currentHeader; 904 888 } … … 918 902 return false; 919 903 } 920 904 921 905 // check if there is a a redirect meta tag 922 906 923 907 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match)) 924 908 925 909 { 926 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 910 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 927 911 } 928 912 … … 940 924 else 941 925 $this->results = $results; 942 926 943 927 return true; 944 928 } … … 950 934 $URI the full URI 951 935 $body body contents to send if any (POST) 952 Output: 953 \*======================================================================*/ 954 936 Output: 937 \*======================================================================*/ 938 955 939 function _httpsrequest($url,$URI,$http_method,$content_type="",$body="") 956 { 940 { 957 941 if($this->passcookies && $this->_redirectaddr) 958 942 $this->setcookies(); 959 943 960 $headers = array(); 961 944 $headers = array(); 945 962 946 $URI_PARTS = parse_url($URI); 963 947 if(empty($url)) 964 948 $url = "/"; 965 949 // GET ... header not needed for curl 966 //$headers[] = $http_method." ".$url." ".$this->_httpversion; 950 //$headers[] = $http_method." ".$url." ".$this->_httpversion; 967 951 if(!empty($this->agent)) 968 952 $headers[] = "User-Agent: ".$this->agent; … … 977 961 $headers[] = "Referer: ".$this->referer; 978 962 if(!empty($this->cookies)) 979 { 963 { 980 964 if(!is_array($this->cookies)) 981 965 $this->cookies = (array)$this->cookies; 982 966 983 967 reset($this->cookies); 984 968 if ( count($this->cookies) > 0 ) { … … 1003 987 $headers[] = "Content-type: $content_type"; 1004 988 } 1005 if(!empty($body)) 989 if(!empty($body)) 1006 990 $headers[] = "Content-length: ".strlen($body); 1007 if(!empty($this->user) || !empty($this->pass)) 991 if(!empty($this->user) || !empty($this->pass)) 1008 992 $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass); 1009 993 1010 994 for($curr_header = 0; $curr_header < count($headers); $curr_header++) { 1011 995 $safer_header = strtr( $headers[$curr_header], "\"", " " ); 1012 996 $cmdline_params .= " -H \"".$safer_header."\""; 1013 997 } 1014 998 1015 999 if(!empty($body)) 1016 1000 $cmdline_params .= " -d \"$body\""; 1017 1001 1018 1002 if($this->read_timeout > 0) 1019 1003 $cmdline_params .= " -m ".$this->read_timeout; 1020 1004 1021 1005 $headerfile = tempnam($temp_dir, "sno"); 1022 1006 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 1007 exec($this->curl_path." -k -D \"$headerfile\"".$cmdline_params." \"".escapeshellcmd($URI)."\"",$results,$return); 1008 1026 1009 if($return) 1027 1010 { … … 1029 1012 return false; 1030 1013 } 1031 1032 1014 1015 1033 1016 $results = implode("\r\n",$results); 1034 1017 1035 1018 $result_headers = file("$headerfile"); 1036 1019 1037 1020 $this->_redirectaddr = false; 1038 1021 unset($this->headers); 1039 1022 1040 1023 for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++) 1041 1024 { 1042 1025 1043 1026 // if a header begins with Location: or URI:, set the redirect 1044 1027 if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader])) … … 1060 1043 $this->_redirectaddr = $matches[2]; 1061 1044 } 1062 1045 1063 1046 if(preg_match("|^HTTP/|",$result_headers[$currentHeader])) 1064 1047 $this->response_code = $result_headers[$currentHeader]; … … 1068 1051 1069 1052 // check if there is a a redirect meta tag 1070 1053 1071 1054 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match)) 1072 1055 { 1073 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 1056 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 1074 1057 } 1075 1058 … … 1089 1072 1090 1073 unlink("$headerfile"); 1091 1074 1092 1075 return true; 1093 1076 } … … 1097 1080 Purpose: set cookies for a redirection 1098 1081 \*======================================================================*/ 1099 1082 1100 1083 function setcookies() 1101 1084 { … … 1107 1090 } 1108 1091 1109 1092 1110 1093 /*======================================================================*\ 1111 1094 Function: _check_timeout … … 1131 1114 Input: $fp file pointer 1132 1115 \*======================================================================*/ 1133 1116 1134 1117 function _connect(&$fp) 1135 1118 { … … 1137 1120 { 1138 1121 $this->_isproxy = true; 1139 1122 1140 1123 $host = $this->proxy_host; 1141 1124 $port = $this->proxy_port; … … 1146 1129 $port = $this->port; 1147 1130 } 1148 1131 1149 1132 $this->status = 0; 1150 1133 1151 1134 if($fp = fsockopen( 1152 1135 $host, … … 1184 1167 Input: $fp file pointer 1185 1168 \*======================================================================*/ 1186 1169 1187 1170 function _disconnect($fp) 1188 1171 { … … 1190 1173 } 1191 1174 1192 1175 1193 1176 /*======================================================================*\ 1194 1177 Function: _prepare_post_body … … 1198 1181 Output: post body 1199 1182 \*======================================================================*/ 1200 1183 1201 1184 function _prepare_post_body($formvars, $formfiles) 1202 1185 { … … 1207 1190 if (count($formvars) == 0 && count($formfiles) == 0) 1208 1191 return; 1209 1192 1210 1193 switch ($this->_submit_type) { 1211 1194 case "application/x-www-form-urlencoded": … … 1223 1206 case "multipart/form-data": 1224 1207 $this->_mime_boundary = "Snoopy".md5(uniqid(microtime())); 1225 1208 1226 1209 reset($formvars); 1227 1210 while(list($key,$val) = each($formvars)) { … … 1238 1221 } 1239 1222 } 1240 1223 1241 1224 reset($formfiles); 1242 1225 while (list($field_name, $file_names) = each($formfiles)) { … … 1246 1229 1247 1230 $fp = fopen($file_name, "r"); 1248 while (!feof($fp)) { 1249 $file_content .= fread($fp, filesize($file_name)); 1250 } 1231 $file_content = fread($fp, filesize($file_name)); 1251 1232 fclose($fp); 1252 1233 $base_name = basename($file_name); … … 1264 1245 } 1265 1246 } 1266 endif;1267 1247 1268 1248 ?>
