MODX Revolution PHP Cross Reference Content Management Systems

Source: /core/model/modx/xmlrss/extlib/snoopy.class.php - 902 lines - 27448 bytes - Summary - Text - Print

   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  
  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        =    false;                // set if using a proxy server
 114      var $_fp_timeout    =    30;                    // timeout for socket connection
 115  
 116  /*======================================================================*\
 117      Function:    fetch
 118      Purpose:    fetch the contents of a web page
 119                  (and possibly other protocols in the
 120                  future like ftp, nntp, gopher, etc.)
 121      Input:        $URI    the location of the page to fetch
 122      Output:        $this->results    the output text from the fetch
 123  \*======================================================================*/
 124  
 125  	function fetch($URI)
 126      {
 127      
 128          //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS);
 129          $URI_PARTS = parse_url($URI);
 130          if (!empty($URI_PARTS["user"]))
 131              $this->user = $URI_PARTS["user"];
 132          if (!empty($URI_PARTS["pass"]))
 133              $this->pass = $URI_PARTS["pass"];
 134  
 135          $fp = '';
 136          switch($URI_PARTS["scheme"])
 137          {
 138              case "http":
 139                  $this->host = $URI_PARTS["host"];
 140                  if(!empty($URI_PARTS["port"]))
 141                      $this->port = $URI_PARTS["port"];
 142                  if($this->_connect($fp))
 143                  {
 144                      if($this->_isproxy)
 145                      {
 146                          // using proxy, send entire URI
 147                          $this->_httprequest($URI,$fp,$URI,$this->_httpmethod);
 148                      }
 149                      else
 150                      {
 151                          $path = $URI_PARTS["path"].(isset($URI_PARTS["query"]) ? "?".$URI_PARTS["query"] : "");
 152                          // no proxy, send only the path
 153                          $this->_httprequest($path, $fp, $URI, $this->_httpmethod);
 154                      }
 155                      
 156                      $this->_disconnect($fp);
 157  
 158                      if($this->_redirectaddr)
 159                      {
 160                          /* url was redirected, check if we've hit the max depth */
 161                          if($this->maxredirs > $this->_redirectdepth)
 162                          {
 163                              // only follow redirect if it's on this site, or offsiteok is true
 164                              if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
 165                              {
 166                                  /* follow the redirect */
 167                                  $this->_redirectdepth++;
 168                                  $this->lastredirectaddr=$this->_redirectaddr;
 169                                  $this->fetch($this->_redirectaddr);
 170                              }
 171                          }
 172                      }
 173  
 174                      if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
 175                      {
 176                          $frameurls = $this->_frameurls;
 177                          $this->_frameurls = array();
 178                          
 179                          while(list(,$frameurl) = each($frameurls))
 180                          {
 181                              if($this->_framedepth < $this->maxframes)
 182                              {
 183                                  $this->fetch($frameurl);
 184                                  $this->_framedepth++;
 185                              }
 186                              else
 187                                  break;
 188                          }
 189                      }                    
 190                  }
 191                  else
 192                  {
 193                      return false;
 194                  }
 195                  return true;                    
 196                  break;
 197              case "https":
 198                  if(!$this->curl_path || (!is_executable($this->curl_path))) {
 199                      $this->error = "Bad curl ($this->curl_path), can't fetch HTTPS \n";
 200                      return false;
 201                  }
 202                  $this->host = $URI_PARTS["host"];
 203                  if(!empty($URI_PARTS["port"]))
 204                      $this->port = $URI_PARTS["port"];
 205                  if($this->_isproxy)
 206                  {
 207                      // using proxy, send entire URI
 208                      $this->_httpsrequest($URI,$URI,$this->_httpmethod);
 209                  }
 210                  else
 211                  {
 212                      $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
 213                      // no proxy, send only the path
 214                      $this->_httpsrequest($path, $URI, $this->_httpmethod);
 215                  }
 216  
 217                  if($this->_redirectaddr)
 218                  {
 219                      /* url was redirected, check if we've hit the max depth */
 220                      if($this->maxredirs > $this->_redirectdepth)
 221                      {
 222                          // only follow redirect if it's on this site, or offsiteok is true
 223                          if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
 224                          {
 225                              /* follow the redirect */
 226                              $this->_redirectdepth++;
 227                              $this->lastredirectaddr=$this->_redirectaddr;
 228                              $this->fetch($this->_redirectaddr);
 229                          }
 230                      }
 231                  }
 232  
 233                  if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
 234                  {
 235                      $frameurls = $this->_frameurls;
 236                      $this->_frameurls = array();
 237  
 238                      while(list(,$frameurl) = each($frameurls))
 239                      {
 240                          if($this->_framedepth < $this->maxframes)
 241                          {
 242                              $this->fetch($frameurl);
 243                              $this->_framedepth++;
 244                          }
 245                          else
 246                              break;
 247                      }
 248                  }                    
 249                  return true;                    
 250                  break;
 251              default:
 252                  // not a valid protocol
 253                  $this->error    =    'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
 254                  return false;
 255                  break;
 256          }        
 257          return true;
 258      }
 259  
 260  
 261  
 262  /*======================================================================*\
 263      Private functions
 264  \*======================================================================*/
 265      
 266      
 267  /*======================================================================*\
 268      Function:    _striplinks
 269      Purpose:    strip the hyperlinks from an html document
 270      Input:        $document    document to strip.
 271      Output:        $match        an array of the links
 272  \*======================================================================*/
 273  
 274  	function _striplinks($document)
 275      {    
 276          preg_match_all("'<\s*a\s+.*href\s*=\s*            # find <a href=
 277                          ([\"\'])?                    # find single or double quote
 278                          (?(1) (.*?)\\1 | ([^\s\>]+))        # if quote found, match up to next matching
 279                                                      # quote, otherwise match up to next space
 280                          'isx",$document,$links);
 281                          
 282  
 283          // catenate the non-empty matches from the conditional subpattern
 284  
 285          while(list($key,$val) = each($links[2]))
 286          {
 287              if(!empty($val))
 288                  $match[] = $val;
 289          }                
 290          
 291          while(list($key,$val) = each($links[3]))
 292          {
 293              if(!empty($val))
 294                  $match[] = $val;
 295          }        
 296          
 297          // return the links
 298          return $match;
 299      }
 300  
 301  /*======================================================================*\
 302      Function:    _stripform
 303      Purpose:    strip the form elements from an html document
 304      Input:        $document    document to strip.
 305      Output:        $match        an array of the links
 306  \*======================================================================*/
 307  
 308  	function _stripform($document)
 309      {    
 310          preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements);
 311          
 312          // catenate the matches
 313          $match = implode("\r\n",$elements[0]);
 314                  
 315          // return the links
 316          return $match;
 317      }
 318  
 319      
 320      
 321  /*======================================================================*\
 322      Function:    _striptext
 323      Purpose:    strip the text from an html document
 324      Input:        $document    document to strip.
 325      Output:        $text        the resulting text
 326  \*======================================================================*/
 327  
 328  	function _striptext($document)
 329      {
 330          
 331          // I didn't use preg eval (//e) since that is only available in PHP 4.0.
 332          // so, list your entities one by one here. I included some of the
 333          // more common ones.
 334                                  
 335          $search = array("'<script[^>]*?>.*?</script>'si",    // strip out javascript
 336                          "'<[\/\!]*?[^<>]*?>'si",            // strip out html tags
 337                          "'([\r\n])[\s]+'",                    // strip out white space
 338                          "'&(quote|#34);'i",                    // replace html entities
 339                          "'&(amp|#38);'i",
 340                          "'&(lt|#60);'i",
 341                          "'&(gt|#62);'i",
 342                          "'&(nbsp|#160);'i",
 343                          "'&(iexcl|#161);'i",
 344                          "'&(cent|#162);'i",
 345                          "'&(pound|#163);'i",
 346                          "'&(copy|#169);'i"
 347                          );                
 348          $replace = array(    "",
 349                              "",
 350                              "\\1",
 351                              "\"",
 352                              "&",
 353                              "<",
 354                              ">",
 355                              " ",
 356                              chr(161),
 357                              chr(162),
 358                              chr(163),
 359                              chr(169));
 360                      
 361          $text = preg_replace($search,$replace,$document);
 362                                  
 363          return $text;
 364      }
 365  
 366  /*======================================================================*\
 367      Function:    _expandlinks
 368      Purpose:    expand each link into a fully qualified URL
 369      Input:        $links            the links to qualify
 370                  $URI            the full URI to get the base from
 371      Output:        $expandedLinks    the expanded links
 372  \*======================================================================*/
 373  
 374  	function _expandlinks($links,$URI)
 375      {
 376          
 377          preg_match("/^[^\?]+/",$URI,$match);
 378  
 379          $match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]);
 380                  
 381          $search = array(     "|^http://".preg_quote($this->host)."|i",
 382                              "|^(?!http://)(\/)?(?!mailto:)|i",
 383                              "|/\./|",
 384                              "|/[^\/]+/\.\./|"
 385                          );
 386                          
 387          $replace = array(    "",
 388                              $match."/",
 389                              "/",
 390                              "/"
 391                          );            
 392                  
 393          $expandedLinks = preg_replace($search,$replace,$links);
 394  
 395          return $expandedLinks;
 396      }
 397  
 398  /*======================================================================*\
 399      Function:    _httprequest
 400      Purpose:    go get the http data from the server
 401      Input:        $url        the url to fetch
 402                  $fp            the current open file pointer
 403                  $URI        the full URI
 404                  $body        body contents to send if any (POST)
 405      Output:        
 406  \*======================================================================*/
 407      
 408  	function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="")
 409      {
 410          $cookie_headers = '';
 411          if($this->passcookies && $this->_redirectaddr)
 412              $this->setcookies();
 413              
 414          $URI_PARTS = parse_url($URI);
 415          if(empty($url))
 416              $url = "/";
 417          $headers = $http_method." ".$url." ".$this->_httpversion."\r\n";        
 418          if(!empty($this->agent))
 419              $headers .= "User-Agent: ".$this->agent."\r\n";
 420          if(!empty($this->host) && !isset($this->rawheaders['Host']))
 421              $headers .= "Host: ".$this->host."\r\n";
 422          if(!empty($this->accept))
 423              $headers .= "Accept: ".$this->accept."\r\n";
 424          
 425          if($this->use_gzip) {
 426              // make sure PHP was built with --with-zlib
 427              // and we can handle gzipp'ed data
 428              if ( function_exists(gzinflate) ) {
 429                 $headers .= "Accept-encoding: gzip\r\n";
 430              }
 431              else {
 432                 trigger_error(
 433                     "use_gzip is on, but PHP was built without zlib support.".
 434                  "  Requesting file(s) without gzip encoding.", 
 435                  E_USER_NOTICE);
 436              }
 437          }
 438          
 439          if(!empty($this->referer))
 440              $headers .= "Referer: ".$this->referer."\r\n";
 441          if(!empty($this->cookies))
 442          {            
 443              if(!is_array($this->cookies))
 444                  $this->cookies = (array)$this->cookies;
 445      
 446              reset($this->cookies);
 447              if ( count($this->cookies) > 0 ) {
 448                  $cookie_headers .= 'Cookie: ';
 449                  foreach ( $this->cookies as $cookieKey => $cookieVal ) {
 450                  $cookie_headers .= $cookieKey."=".urlencode($cookieVal)."; ";
 451                  }
 452                  $headers .= substr($cookie_headers,0,-2) . "\r\n";
 453              } 
 454          }
 455          if(!empty($this->rawheaders))
 456          {
 457              if(!is_array($this->rawheaders))
 458                  $this->rawheaders = (array)$this->rawheaders;
 459              while(list($headerKey,$headerVal) = each($this->rawheaders))
 460                  $headers .= $headerKey.": ".$headerVal."\r\n";
 461          }
 462          if(!empty($content_type)) {
 463              $headers .= "Content-type: $content_type";
 464              if ($content_type == "multipart/form-data")
 465                  $headers .= "; boundary=".$this->_mime_boundary;
 466              $headers .= "\r\n";
 467          }
 468          if(!empty($body))    
 469              $headers .= "Content-length: ".strlen($body)."\r\n";
 470          if(!empty($this->user) || !empty($this->pass))    
 471              $headers .= "Authorization: BASIC ".base64_encode($this->user.":".$this->pass)."\r\n";
 472  
 473          $headers .= "\r\n";
 474          
 475          // set the read timeout if needed
 476          if ($this->read_timeout > 0)
 477              socket_set_timeout($fp, $this->read_timeout,0);
 478          $this->timed_out = false;
 479          
 480          fwrite($fp,$headers.$body,strlen($headers.$body));
 481          
 482          $this->_redirectaddr = false;
 483          unset($this->headers);
 484          
 485          // content was returned gzip encoded?
 486          $is_gzipped = false;
 487                          
 488          while($currentHeader = fgets($fp,$this->_maxlinelen))
 489          {
 490              if ($this->read_timeout > 0 && $this->_check_timeout($fp))
 491              {
 492                  $this->status=-100;
 493                  return false;
 494              }
 495                  
 496          //    if($currentHeader == "\r\n")
 497              if(preg_match("/^\r?\n$/", $currentHeader) )
 498                    break;
 499                          
 500              // if a header begins with Location: or URI:, set the redirect
 501              if(preg_match("/^(Location:|URI:)/i",$currentHeader))
 502              {
 503                  // get URL portion of the redirect
 504                  preg_match("/^(Location:|URI:)\s+(.*)/",chop($currentHeader),$matches);
 505                  // look for :// in the Location header to see if hostname is included
 506                  if(!preg_match("|\:\/\/|",$matches[2]))
 507                  {
 508                      // no host in the path, so prepend
 509                      $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
 510                      // eliminate double slash
 511                      if(!preg_match("|^/|",$matches[2]))
 512                              $this->_redirectaddr .= "/".$matches[2];
 513                      else
 514                              $this->_redirectaddr .= $matches[2];
 515                  }
 516                  else
 517                      $this->_redirectaddr = $matches[2];
 518              }
 519          
 520              if(preg_match("|^HTTP/|",$currentHeader))
 521              {
 522                  if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$currentHeader, $status))
 523                  {
 524                      $this->status= $status[1];
 525                  }                
 526                  $this->response_code = $currentHeader;
 527              }
 528              
 529              if (preg_match("/Content-Encoding: gzip/", $currentHeader) ) {
 530                  $is_gzipped = true;
 531              }
 532              
 533              $this->headers[] = $currentHeader;
 534          }
 535  
 536          # $results = fread($fp, $this->maxlength);
 537          $results = "";
 538          while ( $data = fread($fp, $this->maxlength) ) {
 539              $results .= $data;
 540              if (
 541                  strlen($results) > $this->maxlength ) {
 542                  break;
 543              }
 544          }
 545          
 546          // gunzip
 547          if ( $is_gzipped ) {
 548              // per http://www.php.net/manual/en/function.gzencode.php
 549              $results = substr($results, 10);
 550              $results = gzinflate($results);
 551          }
 552          
 553          if ($this->read_timeout > 0 && $this->_check_timeout($fp))
 554          {
 555              $this->status=-100;
 556              return false;
 557          }
 558          
 559          // check if there is a a redirect meta tag
 560          
 561          if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]+URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
 562          {
 563              $this->_redirectaddr = $this->_expandlinks($match[1],$URI);    
 564          }
 565  
 566          // have we hit our frame depth and is there frame src to fetch?
 567          if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
 568          {
 569              $this->results[] = $results;
 570              for($x=0; $x<count($match[1]); $x++)
 571                  $this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host);
 572          }
 573          // have we already fetched framed content?
 574          elseif(is_array($this->results))
 575              $this->results[] = $results;
 576          // no framed content
 577          else
 578              $this->results = $results;
 579          
 580          return true;
 581      }
 582  
 583  /*======================================================================*\
 584      Function:    _httpsrequest
 585      Purpose:    go get the https data from the server using curl
 586      Input:        $url        the url to fetch
 587                  $URI        the full URI
 588                  $body        body contents to send if any (POST)
 589      Output:        
 590  \*======================================================================*/
 591      
 592  	function _httpsrequest($url,$URI,$http_method,$content_type="",$body="")
 593      {
 594          if($this->passcookies && $this->_redirectaddr)
 595              $this->setcookies();
 596  
 597          $headers = array();        
 598                      
 599          $URI_PARTS = parse_url($URI);
 600          if(empty($url))
 601              $url = "/";
 602          // GET ... header not needed for curl
 603          //$headers[] = $http_method." ".$url." ".$this->_httpversion;        
 604          if(!empty($this->agent))
 605              $headers[] = "User-Agent: ".$this->agent;
 606          if(!empty($this->host))
 607              $headers[] = "Host: ".$this->host;
 608          if(!empty($this->accept))
 609              $headers[] = "Accept: ".$this->accept;
 610          if(!empty($this->referer))
 611              $headers[] = "Referer: ".$this->referer;
 612          if(!empty($this->cookies))
 613          {            
 614              if(!is_array($this->cookies))
 615                  $this->cookies = (array)$this->cookies;
 616      
 617              reset($this->cookies);
 618              if ( count($this->cookies) > 0 ) {
 619                  $cookie_str = 'Cookie: ';
 620                  foreach ( $this->cookies as $cookieKey => $cookieVal ) {
 621                  $cookie_str .= $cookieKey."=".urlencode($cookieVal)."; ";
 622                  }
 623                  $headers[] = substr($cookie_str,0,-2);
 624              }
 625          }
 626          if(!empty($this->rawheaders))
 627          {
 628              if(!is_array($this->rawheaders))
 629                  $this->rawheaders = (array)$this->rawheaders;
 630              while(list($headerKey,$headerVal) = each($this->rawheaders))
 631                  $headers[] = $headerKey.": ".$headerVal;
 632          }
 633          if(!empty($content_type)) {
 634              if ($content_type == "multipart/form-data")
 635                  $headers[] = "Content-type: $content_type; boundary=".$this->_mime_boundary;
 636              else
 637                  $headers[] = "Content-type: $content_type";
 638          }
 639          if(!empty($body))    
 640              $headers[] = "Content-length: ".strlen($body);
 641          if(!empty($this->user) || !empty($this->pass))    
 642              $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass);
 643              
 644          for($curr_header = 0; $curr_header < count($headers); $curr_header++) {
 645              $cmdline_params .= " -H \"".$headers[$curr_header]."\"";
 646          }
 647                                             
 648          if(!empty($body))
 649              $cmdline_params .= " -d \"$body\"";
 650          
 651          if($this->read_timeout > 0)
 652              $cmdline_params .= " -m ".$this->read_timeout;
 653          
 654          $headerfile = uniqid(time());
 655          
 656          # accept self-signed certs
 657          $cmdline_params .= " -k"; 
 658          exec($this->curl_path." -D \"/tmp/$headerfile\"".escapeshellcmd($cmdline_params)." ".escapeshellcmd($URI),$results,$return);
 659          
 660          if($return)
 661          {
 662              $this->error = "Error: cURL could not retrieve the document, error $return.";
 663              return false;
 664          }
 665              
 666              
 667          $results = implode("\r\n",$results);
 668          
 669          $result_headers = file("/tmp/$headerfile");
 670                          
 671          $this->_redirectaddr = false;
 672          unset($this->headers);
 673                          
 674          for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++)
 675          {
 676              
 677              // if a header begins with Location: or URI:, set the redirect
 678              if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader]))
 679              {
 680                  // get URL portion of the redirect
 681                  preg_match("/^(Location: |URI:)(.*)/",chop($result_headers[$currentHeader]),$matches);
 682                  // look for :// in the Location header to see if hostname is included
 683                  if(!preg_match("|\:\/\/|",$matches[2]))
 684                  {
 685                      // no host in the path, so prepend
 686                      $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
 687                      // eliminate double slash
 688                      if(!preg_match("|^/|",$matches[2]))
 689                              $this->_redirectaddr .= "/".$matches[2];
 690                      else
 691                              $this->_redirectaddr .= $matches[2];
 692                  }
 693                  else
 694                      $this->_redirectaddr = $matches[2];
 695              }
 696          
 697              if(preg_match("|^HTTP/|",$result_headers[$currentHeader]))
 698              {
 699                  $this->response_code = $result_headers[$currentHeader];
 700                  if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$this->response_code, $match))
 701                  {
 702                  $this->status= $match[1];
 703                          }
 704              }
 705              $this->headers[] = $result_headers[$currentHeader];
 706          }
 707  
 708          // check if there is a a redirect meta tag
 709          
 710          if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]+URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
 711          {
 712              $this->_redirectaddr = $this->_expandlinks($match[1],$URI);    
 713          }
 714  
 715          // have we hit our frame depth and is there frame src to fetch?
 716          if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
 717          {
 718              $this->results[] = $results;
 719              for($x=0; $x<count($match[1]); $x++)
 720                  $this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host);
 721          }
 722          // have we already fetched framed content?
 723          elseif(is_array($this->results))
 724              $this->results[] = $results;
 725          // no framed content
 726          else
 727              $this->results = $results;
 728  
 729          unlink("/tmp/$headerfile");
 730          
 731          return true;
 732      }
 733  
 734  /*======================================================================*\
 735      Function:    setcookies()
 736      Purpose:    set cookies for a redirection
 737  \*======================================================================*/
 738      
 739  	function setcookies()
 740      {
 741          for($x=0; $x<count($this->headers); $x++)
 742          {
 743          if(preg_match("/^set-cookie:[\s]+([^=]+)=([^;]+)/i", $this->headers[$x],$match))
 744              $this->cookies[$match[1]] = $match[2];
 745          }
 746      }
 747  
 748      
 749  /*======================================================================*\
 750      Function:    _check_timeout
 751      Purpose:    checks whether timeout has occurred
 752      Input:        $fp    file pointer
 753  \*======================================================================*/
 754  
 755  	function _check_timeout($fp)
 756      {
 757          if ($this->read_timeout > 0) {
 758              $fp_status = socket_get_status($fp);
 759              if ($fp_status["timed_out"]) {
 760                  $this->timed_out = true;
 761                  return true;
 762              }
 763          }
 764          return false;
 765      }
 766  
 767  /*======================================================================*\
 768      Function:    _connect
 769      Purpose:    make a socket connection
 770      Input:        $fp    file pointer
 771  \*======================================================================*/
 772      
 773  	function _connect(&$fp)
 774      {
 775          if(!empty($this->proxy_host) && !empty($this->proxy_port))
 776              {
 777                  $this->_isproxy = true;
 778                  $host = $this->proxy_host;
 779                  $port = $this->proxy_port;
 780              }
 781          else
 782          {
 783              $host = $this->host;
 784              $port = $this->port;
 785          }
 786      
 787          $this->status = 0;
 788          
 789          if($fp = fsockopen(
 790                      $host,
 791                      $port,
 792                      $errno,
 793                      $errstr,
 794                      $this->_fp_timeout
 795                      ))
 796          {
 797              // socket connection succeeded
 798  
 799              return true;
 800          }
 801          else
 802          {
 803              // socket connection failed
 804              $this->status = $errno;
 805              switch($errno)
 806              {
 807                  case -3:
 808                      $this->error="socket creation failed (-3)";
 809                  case -4:
 810                      $this->error="dns lookup failure (-4)";
 811                  case -5:
 812                      $this->error="connection refused or timed out (-5)";
 813                  default:
 814                      $this->error="connection failed (".$errno.")";
 815              }
 816              return false;
 817          }
 818      }
 819  /*======================================================================*\
 820      Function:    _disconnect
 821      Purpose:    disconnect a socket connection
 822      Input:        $fp    file pointer
 823  \*======================================================================*/
 824      
 825  	function _disconnect($fp)
 826      {
 827          return(fclose($fp));
 828      }
 829  
 830      
 831  /*======================================================================*\
 832      Function:    _prepare_post_body
 833      Purpose:    Prepare post body according to encoding type
 834      Input:        $formvars  - form variables
 835                  $formfiles - form upload files
 836      Output:        post body
 837  \*======================================================================*/
 838      
 839  	function _prepare_post_body($formvars, $formfiles)
 840      {
 841          settype($formvars, "array");
 842          settype($formfiles, "array");
 843  
 844          if (count($formvars) == 0 && count($formfiles) == 0)
 845              return;
 846          
 847          switch ($this->_submit_type) {
 848              case "application/x-www-form-urlencoded":
 849                  reset($formvars);
 850                  while(list($key,$val) = each($formvars)) {
 851                      if (is_array($val) || is_object($val)) {
 852                          while (list($cur_key, $cur_val) = each($val)) {
 853                              $postdata .= urlencode($key)."[]=".urlencode($cur_val)."&";
 854                          }
 855                      } else
 856                          $postdata .= urlencode($key)."=".urlencode($val)."&";
 857                  }
 858                  break;
 859  
 860              case "multipart/form-data":
 861                  $this->_mime_boundary = "Snoopy".md5(uniqid(microtime()));
 862                  
 863                  reset($formvars);
 864                  while(list($key,$val) = each($formvars)) {
 865                      if (is_array($val) || is_object($val)) {
 866                          while (list($cur_key, $cur_val) = each($val)) {
 867                              $postdata .= "--".$this->_mime_boundary."\r\n";
 868                              $postdata .= "Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n";
 869                              $postdata .= "$cur_val\r\n";
 870                          }
 871                      } else {
 872                          $postdata .= "--".$this->_mime_boundary."\r\n";
 873                          $postdata .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
 874                          $postdata .= "$val\r\n";
 875                      }
 876                  }
 877                  
 878                  reset($formfiles);
 879                  while (list($field_name, $file_names) = each($formfiles)) {
 880                      settype($file_names, "array");
 881                      while (list(, $file_name) = each($file_names)) {
 882                          if (!is_readable($file_name)) continue;
 883  
 884                          $fp = fopen($file_name, "r");
 885                          $file_content = fread($fp, filesize($file_name));
 886                          fclose($fp);
 887                          $base_name = basename($file_name);
 888  
 889                          $postdata .= "--".$this->_mime_boundary."\r\n";
 890                          $postdata .= "Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n";
 891                          $postdata .= "$file_content\r\n";
 892                      }
 893                  }
 894                  $postdata .= "--".$this->_mime_boundary."--\r\n";
 895                  break;
 896          }
 897  
 898          return $postdata;
 899      }
 900  }
 901  
 902  ?>

title

Description

title

Description

title

Description

title

title

Body