[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-snoopy.php (source)

   1  <?php
   2  
   3  /**
   4   * Deprecated. Use WP_HTTP (http.php) instead.
   5   */
   6  _deprecated_file( basename( __FILE__ ), '3.0.0', WPINC . '/http.php' );
   7  
   8  if ( ! class_exists( 'Snoopy', false ) ) :
   9  /*************************************************
  10  
  11  Snoopy - the PHP net client
  12  Author: Monte Ohrt <monte@ispi.net>
  13  Copyright (c): 1999-2008 New Digital Group, all rights reserved
  14  Version: 1.2.4
  15  
  16   * This library is free software; you can redistribute it and/or
  17   * modify it under the terms of the GNU Lesser General Public
  18   * License as published by the Free Software Foundation; either
  19   * version 2.1 of the License, or (at your option) any later version.
  20   *
  21   * This library is distributed in the hope that it will be useful,
  22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  24   * Lesser General Public License for more details.
  25   *
  26   * You should have received a copy of the GNU Lesser General Public
  27   * License along with this library; if not, write to the Free Software
  28   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  29  
  30  You may contact the author of Snoopy by e-mail at:
  31  monte@ohrt.com
  32  
  33  The latest version of Snoopy can be obtained from:
  34  http://snoopy.sourceforge.net/
  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 $proxy_user        =    "";                    // proxy user to use
  49      var $proxy_pass        =    "";                    // proxy password to use
  50  
  51      var $agent            =    "Snoopy v1.2.4";    // agent we masquerade as
  52      var    $referer        =    "";                    // referer info to pass
  53      var $cookies        =    array();            // array of cookies to pass
  54                                                  // $cookies["username"]="joe";
  55      var    $rawheaders        =    array();            // array of raw headers to send
  56                                                  // $rawheaders["Content-Type"]="text/html";
  57  
  58      var $maxredirs        =    5;                    // http redirection depth maximum. 0 = disallow
  59      var $lastredirectaddr    =    "";                // contains address of last redirected address
  60      var    $offsiteok        =    true;                // allows redirection off-site
  61      var $maxframes        =    0;                    // frame content depth maximum. 0 = disallow
  62      var $expandlinks    =    true;                // expand links to fully qualified URLs.
  63                                                  // this only applies to fetchlinks()
  64                                                  // submitlinks(), and submittext()
  65      var $passcookies    =    true;                // pass set cookies back through redirects
  66                                                  // NOTE: this currently does not respect
  67                                                  // dates, domains or paths.
  68  
  69      var    $user            =    "";                    // user for http authentication
  70      var    $pass            =    "";                    // password for http authentication
  71  
  72      // http accept types
  73      var $accept            =    "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
  74  
  75      var $results        =    "";                    // where the content is put
  76  
  77      var $error            =    "";                    // error messages sent here
  78      var    $response_code    =    "";                    // response code returned from server
  79      var    $headers        =    array();            // headers returned from server sent here
  80      var    $maxlength        =    500000;                // max return data length (body)
  81      var $read_timeout    =    0;                    // timeout on read operations, in seconds
  82                                                  // supported only since PHP 4 Beta 4
  83                                                  // set to 0 to disallow timeouts
  84      var $timed_out        =    false;                // if a read operation timed out
  85      var    $status            =    0;                    // http request status
  86  
  87      var $temp_dir        =    "/tmp";                // temporary directory that the webserver
  88                                                  // has permission to write to.
  89                                                  // under Windows, this should be C:\temp
  90  
  91      var    $curl_path        =    "/usr/local/bin/curl";
  92                                                  // Snoopy will use cURL for fetching
  93                                                  // SSL content if a full system path to
  94                                                  // the cURL binary is supplied here.
  95                                                  // set to false if you do not have
  96                                                  // cURL installed. See http://curl.haxx.se
  97                                                  // for details on installing cURL.
  98                                                  // Snoopy does *not* use the cURL
  99                                                  // library functions built into php,
 100                                                  // as these functions are not stable
 101                                                  // as of this Snoopy release.
 102  
 103      /**** Private variables ****/
 104  
 105      var    $_maxlinelen    =    4096;                // max line length (headers)
 106  
 107      var $_httpmethod    =    "GET";                // default http request method
 108      var $_httpversion    =    "HTTP/1.0";            // default http request version
 109      var $_submit_method    =    "POST";                // default submit method
 110      var $_submit_type    =    "application/x-www-form-urlencoded";    // default submit type
 111      var $_mime_boundary    =   "";                    // MIME boundary for multipart/form-data submit type
 112      var $_redirectaddr    =    false;                // will be set if page fetched is a redirect
 113      var $_redirectdepth    =    0;                    // increments on an http redirect
 114      var $_frameurls        =     array();            // frame src urls
 115      var $_framedepth    =    0;                    // increments on frame depth
 116  
 117      var $_isproxy        =    false;                // set if using a proxy server
 118      var $_fp_timeout    =    30;                    // timeout for socket connection
 119  
 120  /*======================================================================*\
 121      Function:    fetch
 122      Purpose:    fetch the contents of a web page
 123                  (and possibly other protocols in the
 124                  future like ftp, nntp, gopher, etc.)
 125      Input:        $URI    the location of the page to fetch
 126      Output:        $this->results    the output text from the fetch
 127  \*======================================================================*/
 128  
 129  	function fetch($URI)
 130      {
 131  
 132          //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS);
 133          $URI_PARTS = parse_url($URI);
 134          if (!empty($URI_PARTS["user"]))
 135              $this->user = $URI_PARTS["user"];
 136          if (!empty($URI_PARTS["pass"]))
 137              $this->pass = $URI_PARTS["pass"];
 138          if (empty($URI_PARTS["query"]))
 139              $URI_PARTS["query"] = '';
 140          if (empty($URI_PARTS["path"]))
 141              $URI_PARTS["path"] = '';
 142  
 143          switch(strtolower($URI_PARTS["scheme"]))
 144          {
 145              case "http":
 146                  $this->host = $URI_PARTS["host"];
 147                  if(!empty($URI_PARTS["port"]))
 148                      $this->port = $URI_PARTS["port"];
 149                  if($this->_connect($fp))
 150                  {
 151                      if($this->_isproxy)
 152                      {
 153                          // using proxy, send entire URI
 154                          $this->_httprequest($URI,$fp,$URI,$this->_httpmethod);
 155                      }
 156                      else
 157                      {
 158                          $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
 159                          // no proxy, send only the path
 160                          $this->_httprequest($path, $fp, $URI, $this->_httpmethod);
 161                      }
 162  
 163                      $this->_disconnect($fp);
 164  
 165                      if($this->_redirectaddr)
 166                      {
 167                          /* url was redirected, check if we've hit the max depth */
 168                          if($this->maxredirs > $this->_redirectdepth)
 169                          {
 170                              // only follow redirect if it's on this site, or offsiteok is true
 171                              if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
 172                              {
 173                                  /* follow the redirect */
 174                                  $this->_redirectdepth++;
 175                                  $this->lastredirectaddr=$this->_redirectaddr;
 176                                  $this->fetch($this->_redirectaddr);
 177                              }
 178                          }
 179                      }
 180  
 181                      if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
 182                      {
 183                          $frameurls = $this->_frameurls;
 184                          $this->_frameurls = array();
 185  
 186                          foreach ( $frameurls as $frameurl )
 187                          {
 188                              if($this->_framedepth < $this->maxframes)
 189                              {
 190                                  $this->fetch($frameurl);
 191                                  $this->_framedepth++;
 192                              }
 193                              else
 194                                  break;
 195                          }
 196                      }
 197                  }
 198                  else
 199                  {
 200                      return false;
 201                  }
 202                  return true;
 203                  break;
 204              case "https":
 205                  if(!$this->curl_path)
 206                      return false;
 207                  if(function_exists("is_executable"))
 208                      if (!is_executable($this->curl_path))
 209                          return false;
 210                  $this->host = $URI_PARTS["host"];
 211                  if(!empty($URI_PARTS["port"]))
 212                      $this->port = $URI_PARTS["port"];
 213                  if($this->_isproxy)
 214                  {
 215                      // using proxy, send entire URI
 216                      $this->_httpsrequest($URI,$URI,$this->_httpmethod);
 217                  }
 218                  else
 219                  {
 220                      $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
 221                      // no proxy, send only the path
 222                      $this->_httpsrequest($path, $URI, $this->_httpmethod);
 223                  }
 224  
 225                  if($this->_redirectaddr)
 226                  {
 227                      /* url was redirected, check if we've hit the max depth */
 228                      if($this->maxredirs > $this->_redirectdepth)
 229                      {
 230                          // only follow redirect if it's on this site, or offsiteok is true
 231                          if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
 232                          {
 233                              /* follow the redirect */
 234                              $this->_redirectdepth++;
 235                              $this->lastredirectaddr=$this->_redirectaddr;
 236                              $this->fetch($this->_redirectaddr);
 237                          }
 238                      }
 239                  }
 240  
 241                  if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
 242                  {
 243                      $frameurls = $this->_frameurls;
 244                      $this->_frameurls = array();
 245  
 246                      foreach ( $frameurls as $frameurl )
 247                      {
 248                          if($this->_framedepth < $this->maxframes)
 249                          {
 250                              $this->fetch($frameurl);
 251                              $this->_framedepth++;
 252                          }
 253                          else
 254                              break;
 255                      }
 256                  }
 257                  return true;
 258                  break;
 259              default:
 260                  // not a valid protocol
 261                  $this->error    =    'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
 262                  return false;
 263                  break;
 264          }
 265          return true;
 266      }
 267  
 268  /*======================================================================*\
 269      Function:    submit
 270      Purpose:    submit an http form
 271      Input:        $URI    the location to post the data
 272                  $formvars    the formvars to use.
 273                      format: $formvars["var"] = "val";
 274                  $formfiles  an array of files to submit
 275                      format: $formfiles["var"] = "/dir/filename.ext";
 276      Output:        $this->results    the text output from the post
 277  \*======================================================================*/
 278  
 279  	function submit($URI, $formvars="", $formfiles="")
 280      {
 281          $postdata = $this->_prepare_post_body($formvars, $formfiles);
 282  
 283          $URI_PARTS = parse_url($URI);
 284          if (!empty($URI_PARTS["user"]))
 285              $this->user = $URI_PARTS["user"];
 286          if (!empty($URI_PARTS["pass"]))
 287              $this->pass = $URI_PARTS["pass"];
 288          if (empty($URI_PARTS["query"]))
 289              $URI_PARTS["query"] = '';
 290          if (empty($URI_PARTS["path"]))
 291              $URI_PARTS["path"] = '';
 292  
 293          switch(strtolower($URI_PARTS["scheme"]))
 294          {
 295              case "http":
 296                  $this->host = $URI_PARTS["host"];
 297                  if(!empty($URI_PARTS["port"]))
 298                      $this->port = $URI_PARTS["port"];
 299                  if($this->_connect($fp))
 300                  {
 301                      if($this->_isproxy)
 302                      {
 303                          // using proxy, send entire URI
 304                          $this->_httprequest($URI,$fp,$URI,$this->_submit_method,$this->_submit_type,$postdata);
 305                      }
 306                      else
 307                      {
 308                          $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
 309                          // no proxy, send only the path
 310                          $this->_httprequest($path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
 311                      }
 312  
 313                      $this->_disconnect($fp);
 314  
 315                      if($this->_redirectaddr)
 316                      {
 317                          /* url was redirected, check if we've hit the max depth */
 318                          if($this->maxredirs > $this->_redirectdepth)
 319                          {
 320                              if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
 321                                  $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
 322  
 323                              // only follow redirect if it's on this site, or offsiteok is true
 324                              if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
 325                              {
 326                                  /* follow the redirect */
 327                                  $this->_redirectdepth++;
 328                                  $this->lastredirectaddr=$this->_redirectaddr;
 329                                  if( strpos( $this->_redirectaddr, "?" ) > 0 )
 330                                      $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get
 331                                  else
 332                                      $this->submit($this->_redirectaddr,$formvars, $formfiles);
 333                              }
 334                          }
 335                      }
 336  
 337                      if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
 338                      {
 339                          $frameurls = $this->_frameurls;
 340                          $this->_frameurls = array();
 341  
 342                          foreach ( $frameurls as $frameurl )
 343                          {
 344                              if($this->_framedepth < $this->maxframes)
 345                              {
 346                                  $this->fetch($frameurl);
 347                                  $this->_framedepth++;
 348                              }
 349                              else
 350                                  break;
 351                          }
 352                      }
 353  
 354                  }
 355                  else
 356                  {
 357                      return false;
 358                  }
 359                  return true;
 360                  break;
 361              case "https":
 362                  if(!$this->curl_path)
 363                      return false;
 364                  if(function_exists("is_executable"))
 365                      if (!is_executable($this->curl_path))
 366                          return false;
 367                  $this->host = $URI_PARTS["host"];
 368                  if(!empty($URI_PARTS["port"]))
 369                      $this->port = $URI_PARTS["port"];
 370                  if($this->_isproxy)
 371                  {
 372                      // using proxy, send entire URI
 373                      $this->_httpsrequest($URI, $URI, $this->_submit_method, $this->_submit_type, $postdata);
 374                  }
 375                  else
 376                  {
 377                      $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
 378                      // no proxy, send only the path
 379                      $this->_httpsrequest($path, $URI, $this->_submit_method, $this->_submit_type, $postdata);
 380                  }
 381  
 382                  if($this->_redirectaddr)
 383                  {
 384                      /* url was redirected, check if we've hit the max depth */
 385                      if($this->maxredirs > $this->_redirectdepth)
 386                      {
 387                          if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
 388                              $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
 389  
 390                          // only follow redirect if it's on this site, or offsiteok is true
 391                          if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
 392                          {
 393                              /* follow the redirect */
 394                              $this->_redirectdepth++;
 395                              $this->lastredirectaddr=$this->_redirectaddr;
 396                              if( strpos( $this->_redirectaddr, "?" ) > 0 )
 397                                  $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get
 398                              else
 399                                  $this->submit($this->_redirectaddr,$formvars, $formfiles);
 400                          }
 401                      }
 402                  }
 403  
 404                  if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
 405                  {
 406                      $frameurls = $this->_frameurls;
 407                      $this->_frameurls = array();
 408  
 409                      foreach ( $frameurls as $frameurl )
 410                      {
 411                          if($this->_framedepth < $this->maxframes)
 412                          {
 413                              $this->fetch($frameurl);
 414                              $this->_framedepth++;
 415                          }
 416                          else
 417                              break;
 418                      }
 419                  }
 420                  return true;
 421                  break;
 422  
 423              default:
 424                  // not a valid protocol
 425                  $this->error    =    'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
 426                  return false;
 427                  break;
 428          }
 429          return true;
 430      }
 431  
 432  /*======================================================================*\
 433      Function:    fetchlinks
 434      Purpose:    fetch the links from a web page
 435      Input:        $URI    where you are fetching from
 436      Output:        $this->results    an array of the URLs
 437  \*======================================================================*/
 438  
 439  	function fetchlinks($URI)
 440      {
 441          if ($this->fetch($URI))
 442          {
 443              if($this->lastredirectaddr)
 444                  $URI = $this->lastredirectaddr;
 445              if(is_array($this->results))
 446              {
 447                  for($x=0;$x<count($this->results);$x++)
 448                      $this->results[$x] = $this->_striplinks($this->results[$x]);
 449              }
 450              else
 451                  $this->results = $this->_striplinks($this->results);
 452  
 453              if($this->expandlinks)
 454                  $this->results = $this->_expandlinks($this->results, $URI);
 455              return true;
 456          }
 457          else
 458              return false;
 459      }
 460  
 461  /*======================================================================*\
 462      Function:    fetchform
 463      Purpose:    fetch the form elements from a web page
 464      Input:        $URI    where you are fetching from
 465      Output:        $this->results    the resulting html form
 466  \*======================================================================*/
 467  
 468  	function fetchform($URI)
 469      {
 470  
 471          if ($this->fetch($URI))
 472          {
 473  
 474              if(is_array($this->results))
 475              {
 476                  for($x=0;$x<count($this->results);$x++)
 477                      $this->results[$x] = $this->_stripform($this->results[$x]);
 478              }
 479              else
 480                  $this->results = $this->_stripform($this->results);
 481  
 482              return true;
 483          }
 484          else
 485              return false;
 486      }
 487  
 488  
 489  /*======================================================================*\
 490      Function:    fetchtext
 491      Purpose:    fetch the text from a web page, stripping the links
 492      Input:        $URI    where you are fetching from
 493      Output:        $this->results    the text from the web page
 494  \*======================================================================*/
 495  
 496  	function fetchtext($URI)
 497      {
 498          if($this->fetch($URI))
 499          {
 500              if(is_array($this->results))
 501              {
 502                  for($x=0;$x<count($this->results);$x++)
 503                      $this->results[$x] = $this->_striptext($this->results[$x]);
 504              }
 505              else
 506                  $this->results = $this->_striptext($this->results);
 507              return true;
 508          }
 509          else
 510              return false;
 511      }
 512  
 513  /*======================================================================*\
 514      Function:    submitlinks
 515      Purpose:    grab links from a form submission
 516      Input:        $URI    where you are submitting from
 517      Output:        $this->results    an array of the links from the post
 518  \*======================================================================*/
 519  
 520  	function submitlinks($URI, $formvars="", $formfiles="")
 521      {
 522          if($this->submit($URI,$formvars, $formfiles))
 523          {
 524              if($this->lastredirectaddr)
 525                  $URI = $this->lastredirectaddr;
 526              if(is_array($this->results))
 527              {
 528                  for($x=0;$x<count($this->results);$x++)
 529                  {
 530                      $this->results[$x] = $this->_striplinks($this->results[$x]);
 531                      if($this->expandlinks)
 532                          $this->results[$x] = $this->_expandlinks($this->results[$x],$URI);
 533                  }
 534              }
 535              else
 536              {
 537                  $this->results = $this->_striplinks($this->results);
 538                  if($this->expandlinks)
 539                      $this->results = $this->_expandlinks($this->results,$URI);
 540              }
 541              return true;
 542          }
 543          else
 544              return false;
 545      }
 546  
 547  /*======================================================================*\
 548      Function:    submittext
 549      Purpose:    grab text from a form submission
 550      Input:        $URI    where you are submitting from
 551      Output:        $this->results    the text from the web page
 552  \*======================================================================*/
 553  
 554  	function submittext($URI, $formvars = "", $formfiles = "")
 555      {
 556          if($this->submit($URI,$formvars, $formfiles))
 557          {
 558              if($this->lastredirectaddr)
 559                  $URI = $this->lastredirectaddr;
 560              if(is_array($this->results))
 561              {
 562                  for($x=0;$x<count($this->results);$x++)
 563                  {
 564                      $this->results[$x] = $this->_striptext($this->results[$x]);
 565                      if($this->expandlinks)
 566                          $this->results[$x] = $this->_expandlinks($this->results[$x],$URI);
 567                  }
 568              }
 569              else
 570              {
 571                  $this->results = $this->_striptext($this->results);
 572                  if($this->expandlinks)
 573                      $this->results = $this->_expandlinks($this->results,$URI);
 574              }
 575              return true;
 576          }
 577          else
 578              return false;
 579      }
 580  
 581  
 582  
 583  /*======================================================================*\
 584      Function:    set_submit_multipart
 585      Purpose:    Set the form submission content type to
 586                  multipart/form-data
 587  \*======================================================================*/
 588  	function set_submit_multipart()
 589      {
 590          $this->_submit_type = "multipart/form-data";
 591      }
 592  
 593  
 594  /*======================================================================*\
 595      Function:    set_submit_normal
 596      Purpose:    Set the form submission content type to
 597                  application/x-www-form-urlencoded
 598  \*======================================================================*/
 599  	function set_submit_normal()
 600      {
 601          $this->_submit_type = "application/x-www-form-urlencoded";
 602      }
 603  
 604  
 605  
 606  
 607  /*======================================================================*\
 608      Private functions
 609  \*======================================================================*/
 610  
 611  
 612  /*======================================================================*\
 613      Function:    _striplinks
 614      Purpose:    strip the hyperlinks from an html document
 615      Input:        $document    document to strip.
 616      Output:        $match        an array of the links
 617  \*======================================================================*/
 618  
 619  	function _striplinks($document)
 620      {
 621          preg_match_all("'<\s*a\s.*?href\s*=\s*            # find <a href=
 622                          ([\"\'])?                    # find single or double quote
 623                          (?(1) (.*?)\\1 | ([^\s\>]+))        # if quote found, match up to next matching
 624                                                      # quote, otherwise match up to next space
 625                          'isx",$document,$links);
 626  
 627  
 628          // catenate the non-empty matches from the conditional subpattern
 629  
 630          foreach ( $links[2] as $key => $val )
 631          {
 632              if(!empty($val))
 633                  $match[] = $val;
 634          }
 635  
 636          foreach ( $links[3] as $key => $val )
 637          {
 638              if(!empty($val))
 639                  $match[] = $val;
 640          }
 641  
 642          // return the links
 643          return $match;
 644      }
 645  
 646  /*======================================================================*\
 647      Function:    _stripform
 648      Purpose:    strip the form elements from an html document
 649      Input:        $document    document to strip.
 650      Output:        $match        an array of the links
 651  \*======================================================================*/
 652  
 653  	function _stripform($document)
 654      {
 655          preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements);
 656  
 657          // catenate the matches
 658          $match = implode("\r\n",$elements[0]);
 659  
 660          // return the links
 661          return $match;
 662      }
 663  
 664  
 665  
 666  /*======================================================================*\
 667      Function:    _striptext
 668      Purpose:    strip the text from an html document
 669      Input:        $document    document to strip.
 670      Output:        $text        the resulting text
 671  \*======================================================================*/
 672  
 673  	function _striptext($document)
 674      {
 675  
 676          // I didn't use preg eval (//e) since that is only available in PHP 4.0.
 677          // so, list your entities one by one here. I included some of the
 678          // more common ones.
 679  
 680          $search = array("'<script[^>]*?>.*?</script>'si",    // strip out javascript
 681                          "'<[\/\!]*?[^<>]*?>'si",            // strip out html tags
 682                          "'([\r\n])[\s]+'",                    // strip out white space
 683                          "'&(quot|#34|#034|#x22);'i",        // replace html entities
 684                          "'&(amp|#38|#038|#x26);'i",            // added hexadecimal values
 685                          "'&(lt|#60|#060|#x3c);'i",
 686                          "'&(gt|#62|#062|#x3e);'i",
 687                          "'&(nbsp|#160|#xa0);'i",
 688                          "'&(iexcl|#161);'i",
 689                          "'&(cent|#162);'i",
 690                          "'&(pound|#163);'i",
 691                          "'&(copy|#169);'i",
 692                          "'&(reg|#174);'i",
 693                          "'&(deg|#176);'i",
 694                          "'&(#39|#039|#x27);'",
 695                          "'&(euro|#8364);'i",                // europe
 696                          "'&a(uml|UML);'",                    // german
 697                          "'&o(uml|UML);'",
 698                          "'&u(uml|UML);'",
 699                          "'&A(uml|UML);'",
 700                          "'&O(uml|UML);'",
 701                          "'&U(uml|UML);'",
 702                          "'&szlig;'i",
 703                          );
 704          $replace = array(    "",
 705                              "",
 706                              "\\1",
 707                              "\"",
 708                              "&",
 709                              "<",
 710                              ">",
 711                              " ",
 712                              chr(161),
 713                              chr(162),
 714                              chr(163),
 715                              chr(169),
 716                              chr(174),
 717                              chr(176),
 718                              chr(39),
 719                              chr(128),
 720                              chr(0xE4), // ANSI &auml;
 721                              chr(0xF6), // ANSI &ouml;
 722                              chr(0xFC), // ANSI &uuml;
 723                              chr(0xC4), // ANSI &Auml;
 724                              chr(0xD6), // ANSI &Ouml;
 725                              chr(0xDC), // ANSI &Uuml;
 726                              chr(0xDF), // ANSI &szlig;
 727                          );
 728  
 729          $text = preg_replace($search,$replace,$document);
 730  
 731          return $text;
 732      }
 733  
 734  /*======================================================================*\
 735      Function:    _expandlinks
 736      Purpose:    expand each link into a fully qualified URL
 737      Input:        $links            the links to qualify
 738                  $URI            the full URI to get the base from
 739      Output:        $expandedLinks    the expanded links
 740  \*======================================================================*/
 741  
 742  	function _expandlinks($links,$URI)
 743      {
 744  
 745          preg_match("/^[^\?]+/",$URI,$match);
 746  
 747          $match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]);
 748          $match = preg_replace("|/$|","",$match);
 749          $match_part = parse_url($match);
 750          $match_root =
 751          $match_part["scheme"]."://".$match_part["host"];
 752  
 753          $search = array(     "|^http://".preg_quote($this->host)."|i",
 754                              "|^(\/)|i",
 755                              "|^(?!http://)(?!mailto:)|i",
 756                              "|/\./|",
 757                              "|/[^\/]+/\.\./|"
 758                          );
 759  
 760          $replace = array(    "",
 761                              $match_root."/",
 762                              $match."/",
 763                              "/",
 764                              "/"
 765                          );
 766  
 767          $expandedLinks = preg_replace($search,$replace,$links);
 768  
 769          return $expandedLinks;
 770      }
 771  
 772  /*======================================================================*\
 773      Function:    _httprequest
 774      Purpose:    go get the http data from the server
 775      Input:        $url        the url to fetch
 776                  $fp            the current open file pointer
 777                  $URI        the full URI
 778                  $body        body contents to send if any (POST)
 779      Output:
 780  \*======================================================================*/
 781  
 782  	function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="")
 783      {
 784          $cookie_headers = '';
 785          if($this->passcookies && $this->_redirectaddr)
 786              $this->setcookies();
 787  
 788          $URI_PARTS = parse_url($URI);
 789          if(empty($url))
 790              $url = "/";
 791          $headers = $http_method." ".$url." ".$this->_httpversion."\r\n";
 792          if(!empty($this->agent))
 793              $headers .= "User-Agent: ".$this->agent."\r\n";
 794          if(!empty($this->host) && !isset($this->rawheaders['Host'])) {
 795              $headers .= "Host: ".$this->host;
 796              if(!empty($this->port) && $this->port != 80)
 797                  $headers .= ":".$this->port;
 798              $headers .= "\r\n";
 799          }
 800          if(!empty($this->accept))
 801              $headers .= "Accept: ".$this->accept."\r\n";
 802          if(!empty($this->referer))
 803              $headers .= "Referer: ".$this->referer."\r\n";
 804          if(!empty($this->cookies))
 805          {
 806              if(!is_array($this->cookies))
 807                  $this->cookies = (array)$this->cookies;
 808  
 809              reset($this->cookies);
 810              if ( count($this->cookies) > 0 ) {
 811                  $cookie_headers .= 'Cookie: ';
 812                  foreach ( $this->cookies as $cookieKey => $cookieVal ) {
 813                  $cookie_headers .= $cookieKey."=".urlencode($cookieVal)."; ";
 814                  }
 815                  $headers .= substr($cookie_headers,0,-2) . "\r\n";
 816              }
 817          }
 818          if(!empty($this->rawheaders))
 819          {
 820              if(!is_array($this->rawheaders))
 821                  $this->rawheaders = (array)$this->rawheaders;
 822              foreach ( $this->rawheaders as $headerKey => $headerVal )
 823                  $headers .= $headerKey.": ".$headerVal."\r\n";
 824          }
 825          if(!empty($content_type)) {
 826              $headers .= "Content-Type: $content_type";
 827              if ($content_type == "multipart/form-data")
 828                  $headers .= "; boundary=".$this->_mime_boundary;
 829              $headers .= "\r\n";
 830          }
 831          if(!empty($body))
 832              $headers .= "Content-Length: ".strlen($body)."\r\n";
 833          if(!empty($this->user) || !empty($this->pass))
 834              $headers .= "Authorization: Basic ".base64_encode($this->user.":".$this->pass)."\r\n";
 835  
 836          //add proxy auth headers
 837          if(!empty($this->proxy_user))
 838              $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass)."\r\n";
 839  
 840  
 841          $headers .= "\r\n";
 842  
 843          // set the read timeout if needed
 844          if ($this->read_timeout > 0)
 845              stream_set_timeout($fp, $this->read_timeout);
 846          $this->timed_out = false;
 847  
 848          fwrite($fp,$headers.$body,strlen($headers.$body));
 849  
 850          $this->_redirectaddr = false;
 851          unset($this->headers);
 852  
 853          while($currentHeader = fgets($fp,$this->_maxlinelen))
 854          {
 855              if ($this->read_timeout > 0 && $this->_check_timeout($fp))
 856              {
 857                  $this->status=-100;
 858                  return false;
 859              }
 860  
 861              if($currentHeader == "\r\n")
 862                  break;
 863  
 864              // if a header begins with Location: or URI:, set the redirect
 865              if(preg_match("/^(Location:|URI:)/i",$currentHeader))
 866              {
 867                  // get URL portion of the redirect
 868                  preg_match("/^(Location:|URI:)[ ]+(.*)/i",chop($currentHeader),$matches);
 869                  // look for :// in the Location header to see if hostname is included
 870                  if(!preg_match("|\:\/\/|",$matches[2]))
 871                  {
 872                      // no host in the path, so prepend
 873                      $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
 874                      // eliminate double slash
 875                      if(!preg_match("|^/|",$matches[2]))
 876                              $this->_redirectaddr .= "/".$matches[2];
 877                      else
 878                              $this->_redirectaddr .= $matches[2];
 879                  }
 880                  else
 881                      $this->_redirectaddr = $matches[2];
 882              }
 883  
 884              if(preg_match("|^HTTP/|",$currentHeader))
 885              {
 886                  if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$currentHeader, $status))
 887                  {
 888                      $this->status= $status[1];
 889                  }
 890                  $this->response_code = $currentHeader;
 891              }
 892  
 893              $this->headers[] = $currentHeader;
 894          }
 895  
 896          $results = '';
 897          do {
 898              $_data = fread($fp, $this->maxlength);
 899              if (strlen($_data) == 0) {
 900                  break;
 901              }
 902              $results .= $_data;
 903          } while(true);
 904  
 905          if ($this->read_timeout > 0 && $this->_check_timeout($fp))
 906          {
 907              $this->status=-100;
 908              return false;
 909          }
 910  
 911          // check if there is a redirect meta tag
 912  
 913          if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
 914  
 915          {
 916              $this->_redirectaddr = $this->_expandlinks($match[1],$URI);
 917          }
 918  
 919          // have we hit our frame depth and is there frame src to fetch?
 920          if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
 921          {
 922              $this->results[] = $results;
 923              for($x=0; $x<count($match[1]); $x++)
 924                  $this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host);
 925          }
 926          // have we already fetched framed content?
 927          elseif(is_array($this->results))
 928              $this->results[] = $results;
 929          // no framed content
 930          else
 931              $this->results = $results;
 932  
 933          return true;
 934      }
 935  
 936  /*======================================================================*\
 937      Function:    _httpsrequest
 938      Purpose:    go get the https data from the server using curl
 939      Input:        $url        the url to fetch
 940                  $URI        the full URI
 941                  $body        body contents to send if any (POST)
 942      Output:
 943  \*======================================================================*/
 944  
 945  	function _httpsrequest($url,$URI,$http_method,$content_type="",$body="")
 946      {
 947          if($this->passcookies && $this->_redirectaddr)
 948              $this->setcookies();
 949  
 950          $headers = array();
 951  
 952          $URI_PARTS = parse_url($URI);
 953          if(empty($url))
 954              $url = "/";
 955          // GET ... header not needed for curl
 956          //$headers[] = $http_method." ".$url." ".$this->_httpversion;
 957          if(!empty($this->agent))
 958              $headers[] = "User-Agent: ".$this->agent;
 959          if(!empty($this->host))
 960              if(!empty($this->port))
 961                  $headers[] = "Host: ".$this->host.":".$this->port;
 962              else
 963                  $headers[] = "Host: ".$this->host;
 964          if(!empty($this->accept))
 965              $headers[] = "Accept: ".$this->accept;
 966          if(!empty($this->referer))
 967              $headers[] = "Referer: ".$this->referer;
 968          if(!empty($this->cookies))
 969          {
 970              if(!is_array($this->cookies))
 971                  $this->cookies = (array)$this->cookies;
 972  
 973              reset($this->cookies);
 974              if ( count($this->cookies) > 0 ) {
 975                  $cookie_str = 'Cookie: ';
 976                  foreach ( $this->cookies as $cookieKey => $cookieVal ) {
 977                  $cookie_str .= $cookieKey."=".urlencode($cookieVal)."; ";
 978                  }
 979                  $headers[] = substr($cookie_str,0,-2);
 980              }
 981          }
 982          if(!empty($this->rawheaders))
 983          {
 984              if(!is_array($this->rawheaders))
 985                  $this->rawheaders = (array)$this->rawheaders;
 986              foreach ( $this->rawheaders as $headerKey => $headerVal )
 987                  $headers[] = $headerKey.": ".$headerVal;
 988          }
 989          if(!empty($content_type)) {
 990              if ($content_type == "multipart/form-data")
 991                  $headers[] = "Content-Type: $content_type; boundary=".$this->_mime_boundary;
 992              else
 993                  $headers[] = "Content-Type: $content_type";
 994          }
 995          if(!empty($body))
 996              $headers[] = "Content-Length: ".strlen($body);
 997          if(!empty($this->user) || !empty($this->pass))
 998              $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass);
 999  
1000          $headerfile = tempnam( $this->temp_dir, "sno" );
1001          $cmdline_params = '-k -D ' . escapeshellarg( $headerfile );
1002  
1003          foreach ( $headers as $header ) {
1004              $cmdline_params .= ' -H ' . escapeshellarg( $header );
1005          }
1006  
1007          if ( ! empty( $body ) ) {
1008              $cmdline_params .= ' -d ' . escapeshellarg( $body );
1009          }
1010  
1011          if ( $this->read_timeout > 0 ) {
1012              $cmdline_params .= ' -m ' . escapeshellarg( $this->read_timeout );
1013          }
1014  
1015  
1016          exec( $this->curl_path . ' ' . $cmdline_params . ' ' . escapeshellarg( $URI ), $results, $return );
1017  
1018          if($return)
1019          {
1020              $this->error = "Error: cURL could not retrieve the document, error $return.";
1021              return false;
1022          }
1023  
1024  
1025          $results = implode("\r\n",$results);
1026  
1027          $result_headers = file("$headerfile");
1028  
1029          $this->_redirectaddr = false;
1030          unset($this->headers);
1031  
1032          for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++)
1033          {
1034  
1035              // if a header begins with Location: or URI:, set the redirect
1036              if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader]))
1037              {
1038                  // get URL portion of the redirect
1039                  preg_match("/^(Location: |URI:)\s+(.*)/",chop($result_headers[$currentHeader]),$matches);
1040                  // look for :// in the Location header to see if hostname is included
1041                  if(!preg_match("|\:\/\/|",$matches[2]))
1042                  {
1043                      // no host in the path, so prepend
1044                      $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
1045                      // eliminate double slash
1046                      if(!preg_match("|^/|",$matches[2]))
1047                              $this->_redirectaddr .= "/".$matches[2];
1048                      else
1049                              $this->_redirectaddr .= $matches[2];
1050                  }
1051                  else
1052                      $this->_redirectaddr = $matches[2];
1053              }
1054  
1055              if(preg_match("|^HTTP/|",$result_headers[$currentHeader]))
1056                  $this->response_code = $result_headers[$currentHeader];
1057  
1058              $this->headers[] = $result_headers[$currentHeader];
1059          }
1060  
1061          // check if there is a redirect meta tag
1062  
1063          if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
1064          {
1065              $this->_redirectaddr = $this->_expandlinks($match[1],$URI);
1066          }
1067  
1068          // have we hit our frame depth and is there frame src to fetch?
1069          if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
1070          {
1071              $this->results[] = $results;
1072              for($x=0; $x<count($match[1]); $x++)
1073                  $this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host);
1074          }
1075          // have we already fetched framed content?
1076          elseif(is_array($this->results))
1077              $this->results[] = $results;
1078          // no framed content
1079          else
1080              $this->results = $results;
1081  
1082          unlink("$headerfile");
1083  
1084          return true;
1085      }
1086  
1087  /*======================================================================*\
1088      Function:    setcookies()
1089      Purpose:    set cookies for a redirection
1090  \*======================================================================*/
1091  
1092  	function setcookies()
1093      {
1094          for($x=0; $x<count($this->headers); $x++)
1095          {
1096          if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $this->headers[$x],$match))
1097              $this->cookies[$match[1]] = urldecode($match[2]);
1098          }
1099      }
1100  
1101  
1102  /*======================================================================*\
1103      Function:    _check_timeout
1104      Purpose:    checks whether timeout has occurred
1105      Input:        $fp    file pointer
1106  \*======================================================================*/
1107  
1108  	function _check_timeout($fp)
1109      {
1110          if ($this->read_timeout > 0) {
1111              $fp_status = socket_get_status($fp);
1112              if ($fp_status["timed_out"]) {
1113                  $this->timed_out = true;
1114                  return true;
1115              }
1116          }
1117          return false;
1118      }
1119  
1120  /*======================================================================*\
1121      Function:    _connect
1122      Purpose:    make a socket connection
1123      Input:        $fp    file pointer
1124  \*======================================================================*/
1125  
1126  	function _connect(&$fp)
1127      {
1128          if(!empty($this->proxy_host) && !empty($this->proxy_port))
1129              {
1130                  $this->_isproxy = true;
1131  
1132                  $host = $this->proxy_host;
1133                  $port = $this->proxy_port;
1134              }
1135          else
1136          {
1137              $host = $this->host;
1138              $port = $this->port;
1139          }
1140  
1141          $this->status = 0;
1142  
1143          if($fp = fsockopen(
1144                      $host,
1145                      $port,
1146                      $errno,
1147                      $errstr,
1148                      $this->_fp_timeout
1149                      ))
1150          {
1151              // socket connection succeeded
1152  
1153              return true;
1154          }
1155          else
1156          {
1157              // socket connection failed
1158              $this->status = $errno;
1159              switch($errno)
1160              {
1161                  case -3:
1162                      $this->error="socket creation failed (-3)";
1163                  case -4:
1164                      $this->error="dns lookup failure (-4)";
1165                  case -5:
1166                      $this->error="connection refused or timed out (-5)";
1167                  default:
1168                      $this->error="connection failed (".$errno.")";
1169              }
1170              return false;
1171          }
1172      }
1173  /*======================================================================*\
1174      Function:    _disconnect
1175      Purpose:    disconnect a socket connection
1176      Input:        $fp    file pointer
1177  \*======================================================================*/
1178  
1179  	function _disconnect($fp)
1180      {
1181          return(fclose($fp));
1182      }
1183  
1184  
1185  /*======================================================================*\
1186      Function:    _prepare_post_body
1187      Purpose:    Prepare post body according to encoding type
1188      Input:        $formvars  - form variables
1189                  $formfiles - form upload files
1190      Output:        post body
1191  \*======================================================================*/
1192  
1193  	function _prepare_post_body($formvars, $formfiles)
1194      {
1195          settype($formvars, "array");
1196          settype($formfiles, "array");
1197          $postdata = '';
1198  
1199          if (count($formvars) == 0 && count($formfiles) == 0)
1200              return;
1201  
1202          switch ($this->_submit_type) {
1203              case "application/x-www-form-urlencoded":
1204                  reset($formvars);
1205                  foreach ( $formvars as $key => $val ) {
1206                      if (is_array($val) || is_object($val)) {
1207                          foreach ( $val as $cur_key => $cur_val ) {
1208                              $postdata .= urlencode($key)."[]=".urlencode($cur_val)."&";
1209                          }
1210                      } else
1211                          $postdata .= urlencode($key)."=".urlencode($val)."&";
1212                  }
1213                  break;
1214  
1215              case "multipart/form-data":
1216                  $this->_mime_boundary = "Snoopy".md5(uniqid(microtime()));
1217  
1218                  reset($formvars);
1219                  foreach ( $formvars as $key => $val ) {
1220                      if (is_array($val) || is_object($val)) {
1221                          foreach ( $val as $cur_key => $cur_val ) {
1222                              $postdata .= "--".$this->_mime_boundary."\r\n";
1223                              $postdata .= "Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n";
1224                              $postdata .= "$cur_val\r\n";
1225                          }
1226                      } else {
1227                          $postdata .= "--".$this->_mime_boundary."\r\n";
1228                          $postdata .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
1229                          $postdata .= "$val\r\n";
1230                      }
1231                  }
1232  
1233                  reset($formfiles);
1234                  foreach ( $formfiles as $field_name => $file_names ) {
1235                      settype($file_names, "array");
1236                      foreach ( $file_names as $file_name ) {
1237                          if (!is_readable($file_name)) continue;
1238  
1239                          $fp = fopen($file_name, "r");
1240                          $file_content = fread($fp, filesize($file_name));
1241                          fclose($fp);
1242                          $base_name = basename($file_name);
1243  
1244                          $postdata .= "--".$this->_mime_boundary."\r\n";
1245                          $postdata .= "Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n";
1246                          $postdata .= "$file_content\r\n";
1247                      }
1248                  }
1249                  $postdata .= "--".$this->_mime_boundary."--\r\n";
1250                  break;
1251          }
1252  
1253          return $postdata;
1254      }
1255  }
1256  endif;
1257  ?>


Generated : Sun Aug 30 08:20:25 2026 Cross-referenced by PHPXref