[ Index ] |
|
Code source de vtiger CRM 5.0.2 |
1 <?php 2 3 /************************************************* 4 5 Snoopy - the PHP net client 6 Author: Monte Ohrt <monte@ispi.net> 7 Copyright (c): 1999-2000 ispi, all rights reserved 8 Version: 1.0 9 10 * This library is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU Lesser General Public 12 * License as published by the Free Software Foundation; either 13 * version 2.1 of the License, or (at your option) any later version. 14 * 15 * This library is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * Lesser General Public License for more details. 19 * 20 * You should have received a copy of the GNU Lesser General Public 21 * License along with this library; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 24 You may contact the author of Snoopy by e-mail at: 25 monte@ispi.net 26 27 Or, write to: 28 Monte Ohrt 29 CTO, ispi 30 237 S. 70th suite 220 31 Lincoln, NE 68510 32 33 The latest version of Snoopy can be obtained from: 34 http://snoopy.sourceforge.com 35 36 *************************************************/ 37 require_once ("include/database/PearDatabase.php"); 38 class Snoopy 39 { 40 /**** Public variables ****/ 41 42 /* user definable vars */ 43 44 var $host = "www.php.net"; // host name we are connecting to 45 var $port = 80; // port we are connecting to 46 var $proxy_host = ""; // proxy host to use 47 var $proxy_port = ""; // proxy port to use 48 var $agent = "Snoopy v1.0"; // agent we masquerade as 49 var $referer = ""; // referer info to pass 50 var $cookies = array(); // array of cookies to pass 51 // $cookies["username"]="joe"; 52 var $rawheaders = array(); // array of raw headers to send 53 // $rawheaders["Content-type"]="text/html"; 54 55 var $maxredirs = 5; // http redirection depth maximum. 0 = disallow 56 var $lastredirectaddr = ""; // contains address of last redirected address 57 var $offsiteok = true; // allows redirection off-site 58 var $maxframes = 0; // frame content depth maximum. 0 = disallow 59 var $expandlinks = true; // expand links to fully qualified URLs. 60 // this only applies to fetchlinks() 61 // or submitlinks() 62 var $passcookies = true; // pass set cookies back through redirects 63 // NOTE: this currently does not respect 64 // dates, domains or paths. 65 66 var $user = ""; // user for http authentication 67 var $pass = ""; // password for http authentication 68 69 // http accept types 70 var $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"; 71 72 var $results = ""; // where the content is put 73 74 var $error = ""; // error messages sent here 75 var $response_code = ""; // response code returned from server 76 var $headers = array(); // headers returned from server sent here 77 var $maxlength = 500000; // max return data length (body) 78 var $read_timeout = 0; // timeout on read operations, in seconds 79 // supported only since PHP 4 Beta 4 80 // set to 0 to disallow timeouts 81 var $timed_out = false; // if a read operation timed out 82 var $status = 0; // http request status 83 84 var $curl_path = "/usr/bin/curl"; 85 // Snoopy will use cURL for fetching 86 // SSL content if a full system path to 87 // the cURL binary is supplied here. 88 // set to false if you do not have 89 // cURL installed. See http://curl.haxx.se 90 // for details on installing cURL. 91 // Snoopy does *not* use the cURL 92 // library functions built into php, 93 // as these functions are not stable 94 // as of this Snoopy release. 95 96 // send Accept-encoding: gzip? 97 var $use_gzip = true; 98 99 /**** Private variables ****/ 100 101 var $_maxlinelen = 4096; // max line length (headers) 102 103 var $_httpmethod = "GET"; // default http request method 104 var $_httpversion = "HTTP/1.0"; // default http request version 105 var $_submit_method = "POST"; // default submit method 106 var $_submit_type = "application/x-www-form-urlencoded"; // default submit type 107 var $_mime_boundary = ""; // MIME boundary for multipart/form-data submit type 108 var $_redirectaddr = false; // will be set if page fetched is a redirect 109 var $_redirectdepth = 0; // increments on an http redirect 110 var $_frameurls = array(); // frame src urls 111 var $_framedepth = 0; // increments on frame depth 112 113 var $_isproxy = true; // set if using a proxy server 114 var $_fp_timeout = 60; // timeout for socket connection 115 function Snoopy() 116 { 117 global $adb; 118 $query = "select * from vtiger_systems where server_type='proxy'"; 119 $result = $adb->query($query); 120 $noofrows = $adb->num_rows($result); 121 if($noofrows != 0) 122 { 123 $this->proxy_host=$adb->query_result($result,0,"server"); 124 $this->proxy_port=$adb->query_result($result,0,"server_port"); 125 $this->user=$adb->query_result($result,0,"server_username"); 126 $this->pass=$adb->query_result($result,0,"server_password"); 127 } 128 } 129 130 /*======================================================================*\ 131 Function: fetch 132 Purpose: fetch the contents of a web page 133 (and possibly other protocols in the 134 future like ftp, nntp, gopher, etc.) 135 Input: $URI the location of the page to fetch 136 Output: $this->results the output text from the fetch 137 \*======================================================================*/ 138 139 function fetch($URI) 140 { 141 142 //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS); 143 $URI_PARTS = parse_url($URI); 144 if (!empty($URI_PARTS["user"])) 145 $this->user = $URI_PARTS["user"]; 146 if (!empty($URI_PARTS["pass"])) 147 $this->pass = $URI_PARTS["pass"]; 148 149 switch($URI_PARTS["scheme"]) 150 { 151 case "http": 152 $this->host = $URI_PARTS["host"]; 153 if(!empty($URI_PARTS["port"])) 154 $this->port = $URI_PARTS["port"]; 155 if($this->_connect($fp)) 156 { 157 if($this->_isproxy) 158 { 159 // using proxy, send entire URI 160 $this->_httprequest($URI,$fp,$URI,$this->_httpmethod); 161 } 162 else 163 { 164 $path = $URI_PARTS["path"].(isset($URI_PARTS["query"]) ? "?".$URI_PARTS["query"] : ""); 165 // no proxy, send only the path 166 $this->_httprequest($path, $fp, $URI, $this->_httpmethod); 167 } 168 169 $this->_disconnect($fp); 170 171 if($this->_redirectaddr) 172 { 173 /* url was redirected, check if we've hit the max depth */ 174 if($this->maxredirs > $this->_redirectdepth) 175 { 176 // only follow redirect if it's on this site, or offsiteok is true 177 if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok) 178 { 179 /* follow the redirect */ 180 $this->_redirectdepth++; 181 $this->lastredirectaddr=$this->_redirectaddr; 182 $this->fetch($this->_redirectaddr); 183 } 184 } 185 } 186 187 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0) 188 { 189 $frameurls = $this->_frameurls; 190 $this->_frameurls = array(); 191 192 while(list(,$frameurl) = each($frameurls)) 193 { 194 if($this->_framedepth < $this->maxframes) 195 { 196 $this->fetch($frameurl); 197 $this->_framedepth++; 198 } 199 else 200 break; 201 } 202 } 203 } 204 else 205 { 206 return false; 207 } 208 return true; 209 break; 210 case "https": 211 if(!$this->curl_path || (!is_executable($this->curl_path))) { 212 $this->error = "Bad curl ($this->curl_path), can't fetch HTTPS \n"; 213 return false; 214 } 215 $this->host = $URI_PARTS["host"]; 216 if(!empty($URI_PARTS["port"])) 217 $this->port = $URI_PARTS["port"]; 218 if($this->_isproxy) 219 { 220 // using proxy, send entire URI 221 $this->_httpsrequest($URI,$URI,$this->_httpmethod); 222 } 223 else 224 { 225 $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : ""); 226 // no proxy, send only the path 227 $this->_httpsrequest($path, $URI, $this->_httpmethod); 228 } 229 230 if($this->_redirectaddr) 231 { 232 /* url was redirected, check if we've hit the max depth */ 233 if($this->maxredirs > $this->_redirectdepth) 234 { 235 // only follow redirect if it's on this site, or offsiteok is true 236 if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok) 237 { 238 /* follow the redirect */ 239 $this->_redirectdepth++; 240 $this->lastredirectaddr=$this->_redirectaddr; 241 $this->fetch($this->_redirectaddr); 242 } 243 } 244 } 245 246 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0) 247 { 248 $frameurls = $this->_frameurls; 249 $this->_frameurls = array(); 250 251 while(list(,$frameurl) = each($frameurls)) 252 { 253 if($this->_framedepth < $this->maxframes) 254 { 255 $this->fetch($frameurl); 256 $this->_framedepth++; 257 } 258 else 259 break; 260 } 261 } 262 return true; 263 break; 264 default: 265 // not a valid protocol 266 $this->error = 'Invalid protocol "'.$URI_PARTS["scheme"].'"\n'; 267 return false; 268 break; 269 } 270 return true; 271 } 272 273 274 275 /*======================================================================*\ 276 Private functions 277 \*======================================================================*/ 278 279 280 /*======================================================================*\ 281 Function: _striplinks 282 Purpose: strip the hyperlinks from an html document 283 Input: $document document to strip. 284 Output: $match an array of the links 285 \*======================================================================*/ 286 287 function _striplinks($document) 288 { 289 preg_match_all("'<\s*a\s+.*href\s*=\s* # find <a href= 290 ([\"\'])? # find single or double quote 291 (?(1) (.*?)\\1 | ([^\s\>]+)) # if quote found, match up to next matching 292 # quote, otherwise match up to next space 293 'isx",$document,$links); 294 295 296 // catenate the non-empty matches from the conditional subpattern 297 298 while(list($key,$val) = each($links[2])) 299 { 300 if(!empty($val)) 301 $match[] = $val; 302 } 303 304 while(list($key,$val) = each($links[3])) 305 { 306 if(!empty($val)) 307 $match[] = $val; 308 } 309 310 // return the links 311 return $match; 312 } 313 314 /*======================================================================*\ 315 Function: _stripform 316 Purpose: strip the form elements from an html document 317 Input: $document document to strip. 318 Output: $match an array of the links 319 \*======================================================================*/ 320 321 function _stripform($document) 322 { 323 preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements); 324 325 // catenate the matches 326 $match = implode("\r\n",$elements[0]); 327 328 // return the links 329 return $match; 330 } 331 332 333 334 /*======================================================================*\ 335 Function: _striptext 336 Purpose: strip the text from an html document 337 Input: $document document to strip. 338 Output: $text the resulting text 339 \*======================================================================*/ 340 341 function _striptext($document) 342 { 343 344 // I didn't use preg eval (//e) since that is only available in PHP 4.0. 345 // so, list your entities one by one here. I included some of the 346 // more common ones. 347 348 $search = array("'<script[^>]*?>.*?</script>'si", // strip out javascript 349 "'<[\/\!]*?[^<>]*?>'si", // strip out html tags 350 "'([\r\n])[\s]+'", // strip out white space 351 "'&(quote|#34);'i", // replace html entities 352 "'&(amp|#38);'i", 353 "'&(lt|#60);'i", 354 "'&(gt|#62);'i", 355 "'&(nbsp|#160);'i", 356 "'&(iexcl|#161);'i", 357 "'&(cent|#162);'i", 358 "'&(pound|#163);'i", 359 "'&(copy|#169);'i" 360 ); 361 $replace = array( "", 362 "", 363 "\\1", 364 "\"", 365 "&", 366 "<", 367 ">", 368 " ", 369 chr(161), 370 chr(162), 371 chr(163), 372 chr(169)); 373 374 $text = preg_replace($search,$replace,$document); 375 376 return $text; 377 } 378 379 /*======================================================================*\ 380 Function: _expandlinks 381 Purpose: expand each link into a fully qualified URL 382 Input: $links the links to qualify 383 $URI the full URI to get the base from 384 Output: $expandedLinks the expanded links 385 \*======================================================================*/ 386 387 function _expandlinks($links,$URI) 388 { 389 390 preg_match("/^[^\?]+/",$URI,$match); 391 392 $match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]); 393 394 $search = array( "|^http://".preg_quote($this->host)."|i", 395 "|^(?!http://)(\/)?(?!mailto:)|i", 396 "|/\./|", 397 "|/[^\/]+/\.\./|" 398 ); 399 400 $replace = array( "", 401 $match."/", 402 "/", 403 "/" 404 ); 405 406 $expandedLinks = preg_replace($search,$replace,$links); 407 408 return $expandedLinks; 409 } 410 411 /*======================================================================*\ 412 Function: _httprequest 413 Purpose: go get the http data from the server 414 Input: $url the url to fetch 415 $fp the current open file pointer 416 $URI the full URI 417 $body body contents to send if any (POST) 418 Output: 419 \*======================================================================*/ 420 421 function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="") 422 { 423 if($this->passcookies && $this->_redirectaddr) 424 $this->setcookies(); 425 426 $URI_PARTS = parse_url($URI); 427 if(empty($url)) 428 $url = "/"; 429 $headers = $http_method." ".$url." ".$this->_httpversion."\r\n"; 430 if(!empty($this->agent)) 431 $headers .= "User-Agent: ".$this->agent."\r\n"; 432 if(!empty($this->host) && !isset($this->rawheaders['Host'])) 433 $headers .= "Host: ".$this->host."\r\n"; 434 if(!empty($this->accept)) 435 $headers .= "Accept: ".$this->accept."\r\n"; 436 437 if($this->use_gzip) { 438 // make sure PHP was built with --with-zlib 439 // and we can handle gzipp'ed data 440 if ( function_exists(gzinflate) ) { 441 $headers .= "Accept-encoding: gzip\r\n"; 442 } 443 else { 444 //commented to supress the warning 445 /*trigger_error( 446 "use_gzip is on, but PHP was built without zlib support.". 447 " Requesting file(s) without gzip encoding.", 448 E_USER_NOTICE);*/ 449 } 450 } 451 452 if(!empty($this->referer)) 453 $headers .= "Referer: ".$this->referer."\r\n"; 454 if(!empty($this->cookies)) 455 { 456 if(!is_array($this->cookies)) 457 $this->cookies = (array)$this->cookies; 458 459 reset($this->cookies); 460 if ( count($this->cookies) > 0 ) { 461 $cookie_headers .= 'Cookie: '; 462 foreach ( $this->cookies as $cookieKey => $cookieVal ) { 463 $cookie_headers .= $cookieKey."=".urlencode($cookieVal)."; "; 464 } 465 $headers .= substr($cookie_headers,0,-2) . "\r\n"; 466 } 467 } 468 if(!empty($this->rawheaders)) 469 { 470 if(!is_array($this->rawheaders)) 471 $this->rawheaders = (array)$this->rawheaders; 472 while(list($headerKey,$headerVal) = each($this->rawheaders)) 473 $headers .= $headerKey.": ".$headerVal."\r\n"; 474 } 475 if(!empty($content_type)) { 476 $headers .= "Content-type: $content_type"; 477 if ($content_type == "multipart/form-data") 478 $headers .= "; boundary=".$this->_mime_boundary; 479 $headers .= "\r\n"; 480 } 481 if(!empty($body)) 482 $headers .= "Content-length: ".strlen($body)."\r\n"; 483 if(!empty($this->user) || !empty($this->pass)) 484 $headers .= "Proxy-Authorization: BASIC ".base64_encode($this->user.":".$this->pass)."\r\n"; 485 486 $headers .= "\r\n"; 487 488 // set the read timeout if needed 489 if ($this->read_timeout > 0) 490 socket_set_timeout($fp, $this->read_timeout); 491 $this->timed_out = false; 492 493 fwrite($fp,$headers.$body,strlen($headers.$body)); 494 495 $this->_redirectaddr = false; 496 unset($this->headers); 497 498 // content was returned gzip encoded? 499 $is_gzipped = false; 500 501 while($currentHeader = fgets($fp,$this->_maxlinelen)) 502 { 503 if ($this->read_timeout > 0 && $this->_check_timeout($fp)) 504 { 505 $this->status=-100; 506 return false; 507 } 508 509 // if($currentHeader == "\r\n") 510 if(preg_match("/^\r?\n$/", $currentHeader) ) 511 break; 512 513 // if a header begins with Location: or URI:, set the redirect 514 if(preg_match("/^(Location:|URI:)/i",$currentHeader)) 515 { 516 // get URL portion of the redirect 517 preg_match("/^(Location:|URI:)\s+(.*)/",chop($currentHeader),$matches); 518 // look for :// in the Location header to see if hostname is included 519 if(!preg_match("|\:\/\/|",$matches[2])) 520 { 521 // no host in the path, so prepend 522 $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port; 523 // eliminate double slash 524 if(!preg_match("|^/|",$matches[2])) 525 $this->_redirectaddr .= "/".$matches[2]; 526 else 527 $this->_redirectaddr .= $matches[2]; 528 } 529 else 530 $this->_redirectaddr = $matches[2]; 531 } 532 533 if(preg_match("|^HTTP/|",$currentHeader)) 534 { 535 if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$currentHeader, $status)) 536 { 537 $this->status= $status[1]; 538 } 539 $this->response_code = $currentHeader; 540 } 541 542 if (preg_match("/Content-Encoding: gzip/", $currentHeader) ) { 543 $is_gzipped = true; 544 } 545 546 $this->headers[] = $currentHeader; 547 } 548 549 # $results = fread($fp, $this->maxlength); 550 $results = ""; 551 while ( $data = fread($fp, $this->maxlength) ) { 552 $results .= $data; 553 if ( 554 strlen($results) > $this->maxlength ) { 555 break; 556 } 557 } 558 559 // gunzip 560 if ( $is_gzipped ) { 561 // per http://www.php.net/manual/en/function.gzencode.php 562 $results = substr($results, 10); 563 $results = gzinflate($results); 564 } 565 566 if ($this->read_timeout > 0 && $this->_check_timeout($fp)) 567 { 568 $this->status=-100; 569 return false; 570 } 571 572 // check if there is a a redirect meta tag 573 574 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]+URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match)) 575 { 576 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 577 } 578 579 // have we hit our frame depth and is there frame src to fetch? 580 if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match)) 581 { 582 $this->results[] = $results; 583 for($x=0; $x<count($match[1]); $x++) 584 $this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host); 585 } 586 // have we already fetched framed content? 587 elseif(is_array($this->results)) 588 $this->results[] = $results; 589 // no framed content 590 else 591 $this->results = $results; 592 593 return true; 594 } 595 596 /*======================================================================*\ 597 Function: _httpsrequest 598 Purpose: go get the https data from the server using curl 599 Input: $url the url to fetch 600 $URI the full URI 601 $body body contents to send if any (POST) 602 Output: 603 \*======================================================================*/ 604 605 function _httpsrequest($url,$URI,$http_method,$content_type="",$body="") 606 { 607 if($this->passcookies && $this->_redirectaddr) 608 $this->setcookies(); 609 610 $headers = array(); 611 612 $URI_PARTS = parse_url($URI); 613 if(empty($url)) 614 $url = "/"; 615 // GET ... header not needed for curl 616 //$headers[] = $http_method." ".$url." ".$this->_httpversion; 617 if(!empty($this->agent)) 618 $headers[] = "User-Agent: ".$this->agent; 619 if(!empty($this->host)) 620 $headers[] = "Host: ".$this->host; 621 if(!empty($this->accept)) 622 $headers[] = "Accept: ".$this->accept; 623 if(!empty($this->referer)) 624 $headers[] = "Referer: ".$this->referer; 625 if(!empty($this->cookies)) 626 { 627 if(!is_array($this->cookies)) 628 $this->cookies = (array)$this->cookies; 629 630 reset($this->cookies); 631 if ( count($this->cookies) > 0 ) { 632 $cookie_str = 'Cookie: '; 633 foreach ( $this->cookies as $cookieKey => $cookieVal ) { 634 $cookie_str .= $cookieKey."=".urlencode($cookieVal)."; "; 635 } 636 $headers[] = substr($cookie_str,0,-2); 637 } 638 } 639 if(!empty($this->rawheaders)) 640 { 641 if(!is_array($this->rawheaders)) 642 $this->rawheaders = (array)$this->rawheaders; 643 while(list($headerKey,$headerVal) = each($this->rawheaders)) 644 $headers[] = $headerKey.": ".$headerVal; 645 } 646 if(!empty($content_type)) { 647 if ($content_type == "multipart/form-data") 648 $headers[] = "Content-type: $content_type; boundary=".$this->_mime_boundary; 649 else 650 $headers[] = "Content-type: $content_type"; 651 } 652 if(!empty($body)) 653 $headers[] = "Content-length: ".strlen($body); 654 if(!empty($this->user) || !empty($this->pass)) 655 $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass); 656 657 for($curr_header = 0; $curr_header < count($headers); $curr_header++) { 658 $cmdline_params .= " -H \"".$headers[$curr_header]."\""; 659 } 660 661 if(!empty($body)) 662 $cmdline_params .= " -d \"$body\""; 663 664 if($this->read_timeout > 0) 665 $cmdline_params .= " -m ".$this->read_timeout; 666 667 $headerfile = uniqid(time()); 668 669 # accept self-signed certs 670 $cmdline_params .= " -k"; 671 exec($this->curl_path." -D \"/tmp/$headerfile\"".escapeshellcmd($cmdline_params)." ".escapeshellcmd($URI),$results,$return); 672 673 if($return) 674 { 675 $this->error = "Error: cURL could not retrieve the document, error $return."; 676 return false; 677 } 678 679 680 $results = implode("\r\n",$results); 681 682 $result_headers = file("/tmp/$headerfile"); 683 684 $this->_redirectaddr = false; 685 unset($this->headers); 686 687 for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++) 688 { 689 690 // if a header begins with Location: or URI:, set the redirect 691 if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader])) 692 { 693 // get URL portion of the redirect 694 preg_match("/^(Location: |URI:)(.*)/",chop($result_headers[$currentHeader]),$matches); 695 // look for :// in the Location header to see if hostname is included 696 if(!preg_match("|\:\/\/|",$matches[2])) 697 { 698 // no host in the path, so prepend 699 $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port; 700 // eliminate double slash 701 if(!preg_match("|^/|",$matches[2])) 702 $this->_redirectaddr .= "/".$matches[2]; 703 else 704 $this->_redirectaddr .= $matches[2]; 705 } 706 else 707 $this->_redirectaddr = $matches[2]; 708 } 709 710 if(preg_match("|^HTTP/|",$result_headers[$currentHeader])) 711 { 712 $this->response_code = $result_headers[$currentHeader]; 713 if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$this->response_code, $match)) 714 { 715 $this->status= $match[1]; 716 } 717 } 718 $this->headers[] = $result_headers[$currentHeader]; 719 } 720 721 // check if there is a a redirect meta tag 722 723 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]+URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match)) 724 { 725 $this->_redirectaddr = $this->_expandlinks($match[1],$URI); 726 } 727 728 // have we hit our frame depth and is there frame src to fetch? 729 if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match)) 730 { 731 $this->results[] = $results; 732 for($x=0; $x<count($match[1]); $x++) 733 $this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host); 734 } 735 // have we already fetched framed content? 736 elseif(is_array($this->results)) 737 $this->results[] = $results; 738 // no framed content 739 else 740 $this->results = $results; 741 742 unlink("/tmp/$headerfile"); 743 744 return true; 745 } 746 747 /*======================================================================*\ 748 Function: setcookies() 749 Purpose: set cookies for a redirection 750 \*======================================================================*/ 751 752 function setcookies() 753 { 754 for($x=0; $x<count($this->headers); $x++) 755 { 756 if(preg_match("/^set-cookie:[\s]+([^=]+)=([^;]+)/i", $this->headers[$x],$match)) 757 $this->cookies[$match[1]] = $match[2]; 758 } 759 } 760 761 762 /*======================================================================*\ 763 Function: _check_timeout 764 Purpose: checks whether timeout has occurred 765 Input: $fp file pointer 766 \*======================================================================*/ 767 768 function _check_timeout($fp) 769 { 770 if ($this->read_timeout > 0) { 771 $fp_status = socket_get_status($fp); 772 if ($fp_status["timed_out"]) { 773 $this->timed_out = true; 774 return true; 775 } 776 } 777 return false; 778 } 779 780 /*======================================================================*\ 781 Function: _connect 782 Purpose: make a socket connection 783 Input: $fp file pointer 784 \*======================================================================*/ 785 786 function _connect(&$fp) 787 { 788 if(!empty($this->proxy_host) && !empty($this->proxy_port)) 789 { 790 $this->_isproxy = true; 791 $host = $this->proxy_host; 792 $port = $this->proxy_port; 793 } 794 else 795 { 796 $host = $this->host; 797 $port = $this->port; 798 } 799 800 $this->status = 0; 801 802 if($fp = fsockopen( 803 $host, 804 $port, 805 $errno, 806 $errstr, 807 $this->_fp_timeout 808 )) 809 { 810 // socket connection succeeded 811 812 return true; 813 } 814 else 815 { 816 // socket connection failed 817 $this->status = $errno; 818 switch($errno) 819 { 820 case -3: 821 $this->error="socket creation failed (-3)"; 822 case -4: 823 $this->error="dns lookup failure (-4)"; 824 case -5: 825 $this->error="connection refused or timed out (-5)"; 826 default: 827 $this->error="connection failed (".$errno.")"; 828 } 829 return false; 830 } 831 } 832 /*======================================================================*\ 833 Function: _disconnect 834 Purpose: disconnect a socket connection 835 Input: $fp file pointer 836 \*======================================================================*/ 837 838 function _disconnect($fp) 839 { 840 return(fclose($fp)); 841 } 842 843 844 /*======================================================================*\ 845 Function: _prepare_post_body 846 Purpose: Prepare post body according to encoding type 847 Input: $formvars - form variables 848 $formfiles - form upload files 849 Output: post body 850 \*======================================================================*/ 851 852 function _prepare_post_body($formvars, $formfiles) 853 { 854 settype($formvars, "array"); 855 settype($formfiles, "array"); 856 857 if (count($formvars) == 0 && count($formfiles) == 0) 858 return; 859 860 switch ($this->_submit_type) { 861 case "application/x-www-form-urlencoded": 862 reset($formvars); 863 while(list($key,$val) = each($formvars)) { 864 if (is_array($val) || is_object($val)) { 865 while (list($cur_key, $cur_val) = each($val)) { 866 $postdata .= urlencode($key)."[]=".urlencode($cur_val)."&"; 867 } 868 } else 869 $postdata .= urlencode($key)."=".urlencode($val)."&"; 870 } 871 break; 872 873 case "multipart/form-data": 874 $this->_mime_boundary = "Snoopy".md5(uniqid(microtime())); 875 876 reset($formvars); 877 while(list($key,$val) = each($formvars)) { 878 if (is_array($val) || is_object($val)) { 879 while (list($cur_key, $cur_val) = each($val)) { 880 $postdata .= "--".$this->_mime_boundary."\r\n"; 881 $postdata .= "Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n"; 882 $postdata .= "$cur_val\r\n"; 883 } 884 } else { 885 $postdata .= "--".$this->_mime_boundary."\r\n"; 886 $postdata .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n"; 887 $postdata .= "$val\r\n"; 888 } 889 } 890 891 reset($formfiles); 892 while (list($field_name, $file_names) = each($formfiles)) { 893 settype($file_names, "array"); 894 while (list(, $file_name) = each($file_names)) { 895 if (!is_readable($file_name)) continue; 896 897 $fp = fopen($file_name, "r"); 898 $file_content = fread($fp, filesize($file_name)); 899 fclose($fp); 900 $base_name = basename($file_name); 901 902 $postdata .= "--".$this->_mime_boundary."\r\n"; 903 $postdata .= "Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n"; 904 $postdata .= "$file_content\r\n"; 905 } 906 } 907 $postdata .= "--".$this->_mime_boundary."--\r\n"; 908 break; 909 } 910 911 return $postdata; 912 } 913 } 914 915 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 10:22:19 2007 | par Balluche grâce à PHPXref 0.7 |