[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/SimplePie/ -> File.php (source)

   1  <?php
   2  /**
   3   * SimplePie
   4   *
   5   * A PHP-Based RSS and Atom Feed Framework.
   6   * Takes the hard work out of managing a complete RSS/Atom solution.
   7   *
   8   * Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
   9   * All rights reserved.
  10   *
  11   * Redistribution and use in source and binary forms, with or without modification, are
  12   * permitted provided that the following conditions are met:
  13   *
  14   *     * Redistributions of source code must retain the above copyright notice, this list of
  15   *       conditions and the following disclaimer.
  16   *
  17   *     * Redistributions in binary form must reproduce the above copyright notice, this list
  18   *       of conditions and the following disclaimer in the documentation and/or other materials
  19   *       provided with the distribution.
  20   *
  21   *     * Neither the name of the SimplePie Team nor the names of its contributors may be used
  22   *       to endorse or promote products derived from this software without specific prior
  23   *       written permission.
  24   *
  25   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26   * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28   * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33   * POSSIBILITY OF SUCH DAMAGE.
  34   *
  35   * @package SimplePie
  36   * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
  37   * @author Ryan Parman
  38   * @author Sam Sneddon
  39   * @author Ryan McCue
  40   * @link http://simplepie.org/ SimplePie
  41   * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42   */
  43  
  44  /**
  45   * Used for fetching remote files and reading local files
  46   *
  47   * Supports HTTP 1.0 via cURL or fsockopen, with spotty HTTP 1.1 support
  48   *
  49   * This class can be overloaded with {@see SimplePie::set_file_class()}
  50   *
  51   * @package SimplePie
  52   * @subpackage HTTP
  53   * @todo Move to properly supporting RFC2616 (HTTP/1.1)
  54   */
  55  class SimplePie_File
  56  {
  57      var $url;
  58      var $useragent;
  59      var $success = true;
  60      var $headers = array();
  61      var $body;
  62      var $status_code;
  63      var $redirects = 0;
  64      var $error;
  65      var $method = SIMPLEPIE_FILE_SOURCE_NONE;
  66      var $permanent_url;
  67  
  68  	public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false, $curl_options = array())
  69      {
  70          if (class_exists('idna_convert'))
  71          {
  72              $idn = new idna_convert();
  73              $parsed = SimplePie_Misc::parse_url($url);
  74              $url = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], NULL);
  75          }
  76          $this->url = $url;
  77          $this->permanent_url = $url;
  78          $this->useragent = $useragent;
  79          if (preg_match('/^http(s)?:\/\//i', $url))
  80          {
  81              if ($useragent === null)
  82              {
  83                  $useragent = ini_get('user_agent');
  84                  $this->useragent = $useragent;
  85              }
  86              if (!is_array($headers))
  87              {
  88                  $headers = array();
  89              }
  90              if (!$force_fsockopen && function_exists('curl_exec'))
  91              {
  92                  $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
  93                  $fp = curl_init();
  94                  $headers2 = array();
  95                  foreach ($headers as $key => $value)
  96                  {
  97                      $headers2[] = "$key: $value";
  98                  }
  99                  if (version_compare(SimplePie_Misc::get_curl_version(), '7.10.5', '>='))
 100                  {
 101                      curl_setopt($fp, CURLOPT_ENCODING, '');
 102                  }
 103                  curl_setopt($fp, CURLOPT_URL, $url);
 104                  curl_setopt($fp, CURLOPT_HEADER, 1);
 105                  curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
 106                  curl_setopt($fp, CURLOPT_FAILONERROR, 1);
 107                  curl_setopt($fp, CURLOPT_TIMEOUT, $timeout);
 108                  curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout);
 109                  curl_setopt($fp, CURLOPT_REFERER, SimplePie_Misc::url_remove_credentials($url));
 110                  curl_setopt($fp, CURLOPT_USERAGENT, $useragent);
 111                  curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);
 112                  foreach ($curl_options as $curl_param => $curl_value) {
 113                      curl_setopt($fp, $curl_param, $curl_value);
 114                  }
 115  
 116                  $this->headers = curl_exec($fp);
 117                  if (curl_errno($fp) === 23 || curl_errno($fp) === 61)
 118                  {
 119                      curl_setopt($fp, CURLOPT_ENCODING, 'none');
 120                      $this->headers = curl_exec($fp);
 121                  }
 122                  $this->status_code = curl_getinfo($fp, CURLINFO_HTTP_CODE);
 123                  if (curl_errno($fp))
 124                  {
 125                      $this->error = 'cURL error ' . curl_errno($fp) . ': ' . curl_error($fp);
 126                      $this->success = false;
 127                  }
 128                  else
 129                  {
 130                      // Use the updated url provided by curl_getinfo after any redirects.
 131                      if ($info = curl_getinfo($fp)) {
 132                          $this->url = $info['url'];
 133                      }
 134                      curl_close($fp);
 135                      $this->headers = SimplePie_HTTP_Parser::prepareHeaders($this->headers, $info['redirect_count'] + 1);
 136                      $parser = new SimplePie_HTTP_Parser($this->headers);
 137                      if ($parser->parse())
 138                      {
 139                          $this->headers = $parser->headers;
 140                          $this->body = trim($parser->body);
 141                          $this->status_code = $parser->status_code;
 142                          if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects)
 143                          {
 144                              $this->redirects++;
 145                              $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
 146                              $previousStatusCode = $this->status_code;
 147                              $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen, $curl_options);
 148                              $this->permanent_url = ($previousStatusCode == 301) ? $location : $url;
 149                              return;
 150                          }
 151                      }
 152                  }
 153              }
 154              else
 155              {
 156                  $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_FSOCKOPEN;
 157                  $url_parts = parse_url($url);
 158                  $socket_host = $url_parts['host'];
 159                  if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https')
 160                  {
 161                      $socket_host = "ssl://$url_parts[host]";
 162                      $url_parts['port'] = 443;
 163                  }
 164                  if (!isset($url_parts['port']))
 165                  {
 166                      $url_parts['port'] = 80;
 167                  }
 168                  $fp = @fsockopen($socket_host, $url_parts['port'], $errno, $errstr, $timeout);
 169                  if (!$fp)
 170                  {
 171                      $this->error = 'fsockopen error: ' . $errstr;
 172                      $this->success = false;
 173                  }
 174                  else
 175                  {
 176                      stream_set_timeout($fp, $timeout);
 177                      if (isset($url_parts['path']))
 178                      {
 179                          if (isset($url_parts['query']))
 180                          {
 181                              $get = "$url_parts[path]?$url_parts[query]";
 182                          }
 183                          else
 184                          {
 185                              $get = $url_parts['path'];
 186                          }
 187                      }
 188                      else
 189                      {
 190                          $get = '/';
 191                      }
 192                      $out = "GET $get HTTP/1.1\r\n";
 193                      $out .= "Host: $url_parts[host]\r\n";
 194                      $out .= "User-Agent: $useragent\r\n";
 195                      if (extension_loaded('zlib'))
 196                      {
 197                          $out .= "Accept-Encoding: x-gzip,gzip,deflate\r\n";
 198                      }
 199  
 200                      if (isset($url_parts['user']) && isset($url_parts['pass']))
 201                      {
 202                          $out .= "Authorization: Basic " . base64_encode("$url_parts[user]:$url_parts[pass]") . "\r\n";
 203                      }
 204                      foreach ($headers as $key => $value)
 205                      {
 206                          $out .= "$key: $value\r\n";
 207                      }
 208                      $out .= "Connection: Close\r\n\r\n";
 209                      fwrite($fp, $out);
 210  
 211                      $info = stream_get_meta_data($fp);
 212  
 213                      $this->headers = '';
 214                      while (!$info['eof'] && !$info['timed_out'])
 215                      {
 216                          $this->headers .= fread($fp, 1160);
 217                          $info = stream_get_meta_data($fp);
 218                      }
 219                      if (!$info['timed_out'])
 220                      {
 221                          $parser = new SimplePie_HTTP_Parser($this->headers);
 222                          if ($parser->parse())
 223                          {
 224                              $this->headers = $parser->headers;
 225                              $this->body = $parser->body;
 226                              $this->status_code = $parser->status_code;
 227                              if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects)
 228                              {
 229                                  $this->redirects++;
 230                                  $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
 231                                  $previousStatusCode = $this->status_code;
 232                                  $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen, $curl_options);
 233                                  $this->permanent_url = ($previousStatusCode == 301) ? $location : $url;
 234                                  return;
 235                              }
 236                              if (isset($this->headers['content-encoding']))
 237                              {
 238                                  // Hey, we act dumb elsewhere, so let's do that here too
 239                                  switch (strtolower(trim($this->headers['content-encoding'], "\x09\x0A\x0D\x20")))
 240                                  {
 241                                      case 'gzip':
 242                                      case 'x-gzip':
 243                                          $decoder = new SimplePie_gzdecode($this->body);
 244                                          if (!$decoder->parse())
 245                                          {
 246                                              $this->error = 'Unable to decode HTTP "gzip" stream';
 247                                              $this->success = false;
 248                                          }
 249                                          else
 250                                          {
 251                                              $this->body = trim($decoder->data);
 252                                          }
 253                                          break;
 254  
 255                                      case 'deflate':
 256                                          if (($decompressed = gzinflate($this->body)) !== false)
 257                                          {
 258                                              $this->body = $decompressed;
 259                                          }
 260                                          else if (($decompressed = gzuncompress($this->body)) !== false)
 261                                          {
 262                                              $this->body = $decompressed;
 263                                          }
 264                                          else if (function_exists('gzdecode') && ($decompressed = gzdecode($this->body)) !== false)
 265                                          {
 266                                              $this->body = $decompressed;
 267                                          }
 268                                          else
 269                                          {
 270                                              $this->error = 'Unable to decode HTTP "deflate" stream';
 271                                              $this->success = false;
 272                                          }
 273                                          break;
 274  
 275                                      default:
 276                                          $this->error = 'Unknown content coding';
 277                                          $this->success = false;
 278                                  }
 279                              }
 280                          }
 281                      }
 282                      else
 283                      {
 284                          $this->error = 'fsocket timed out';
 285                          $this->success = false;
 286                      }
 287                      fclose($fp);
 288                  }
 289              }
 290          }
 291          else
 292          {
 293              $this->method = SIMPLEPIE_FILE_SOURCE_LOCAL | SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS;
 294              if (empty($url) || !($this->body = trim(file_get_contents($url))))
 295              {
 296                  $this->error = 'file_get_contents could not read the file';
 297                  $this->success = false;
 298              }
 299          }
 300      }
 301  }


Generated: Fri Dec 13 01:00:02 2024 Cross-referenced by PHPXref 0.7.1