wp/wp-includes/class-simplepie.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    66  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    66  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    67  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    67  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    68  * POSSIBILITY OF SUCH DAMAGE.
    68  * POSSIBILITY OF SUCH DAMAGE.
    69  *
    69  *
    70  * @package SimplePie
    70  * @package SimplePie
    71  * @version 1.5.6
    71  * @version 1.5.8
    72  * @copyright 2004-2017 Ryan Parman, Sam Sneddon, Ryan McCue
    72  * @copyright 2004-2017 Ryan Parman, Sam Sneddon, Ryan McCue
    73  * @author Ryan Parman
    73  * @author Ryan Parman
    74  * @author Sam Sneddon
    74  * @author Sam Sneddon
    75  * @author Ryan McCue
    75  * @author Ryan McCue
    76  * @link http://simplepie.org/ SimplePie
    76  * @link http://simplepie.org/ SimplePie
    83 define('SIMPLEPIE_NAME', 'SimplePie');
    83 define('SIMPLEPIE_NAME', 'SimplePie');
    84 
    84 
    85 /**
    85 /**
    86  * SimplePie Version
    86  * SimplePie Version
    87  */
    87  */
    88 define('SIMPLEPIE_VERSION', '1.5.6');
    88 define('SIMPLEPIE_VERSION', '1.5.8');
    89 
    89 
    90 /**
    90 /**
    91  * SimplePie Build
    91  * SimplePie Build
    92  * @todo Hardcode for release (there's no need to have to call SimplePie_Misc::get_build() only every load of simplepie.inc)
    92  * @todo Hardcode for release (there's no need to have to call SimplePie_Misc::get_build() only every load of simplepie.inc)
    93  */
    93  */
   456 	/**
   456 	/**
   457 	 * @var mixed Error string
   457 	 * @var mixed Error string
   458 	 * @access private
   458 	 * @access private
   459 	 */
   459 	 */
   460 	public $error;
   460 	public $error;
       
   461 
       
   462 	/**
       
   463 	 * @var int HTTP status code
       
   464 	 * @see SimplePie::status_code()
       
   465 	 * @access private
       
   466 	 */
       
   467 	public $status_code;
   461 
   468 
   462 	/**
   469 	/**
   463 	 * @var object Instance of SimplePie_Sanitize (or other class)
   470 	 * @var object Instance of SimplePie_Sanitize (or other class)
   464 	 * @see SimplePie::set_sanitize_class()
   471 	 * @see SimplePie::set_sanitize_class()
   465 	 * @access private
   472 	 * @access private
   942 	{
   949 	{
   943 		$this->cache_location = (string) $location;
   950 		$this->cache_location = (string) $location;
   944 	}
   951 	}
   945 
   952 
   946 	/**
   953 	/**
       
   954 	 * Return the filename (i.e. hash, without path and without extension) of the file to cache a given URL.
       
   955 	 * @param string $url The URL of the feed to be cached.
       
   956 	 * @return string A filename (i.e. hash, without path and without extension).
       
   957 	 */
       
   958 	public function get_cache_filename($url)
       
   959 	{
       
   960 		// Append custom parameters to the URL to avoid cache pollution in case of multiple calls with different parameters.
       
   961 		$url .= $this->force_feed ? '#force_feed' : '';
       
   962 		$options = array();
       
   963 		if ($this->timeout != 10)
       
   964 		{
       
   965 			$options[CURLOPT_TIMEOUT] = $this->timeout;
       
   966 		}
       
   967 		if ($this->useragent !== SIMPLEPIE_USERAGENT)
       
   968 		{
       
   969 			$options[CURLOPT_USERAGENT] = $this->useragent;
       
   970 		}
       
   971 		if (!empty($this->curl_options))
       
   972 		{
       
   973 			foreach ($this->curl_options as $k => $v)
       
   974 			{
       
   975 				$options[$k] = $v;
       
   976 			}
       
   977 		}
       
   978 		if (!empty($options))
       
   979 		{
       
   980 			ksort($options);
       
   981 			$url .= '#' . urlencode(var_export($options, true));
       
   982 		}
       
   983 		return call_user_func($this->cache_name_function, $url);
       
   984 	}
       
   985 
       
   986 	/**
   947 	 * Set whether feed items should be sorted into reverse chronological order
   987 	 * Set whether feed items should be sorted into reverse chronological order
   948 	 *
   988 	 *
   949 	 * @param bool $enable Sort as reverse chronological order.
   989 	 * @param bool $enable Sort as reverse chronological order.
   950 	 */
   990 	 */
   951 	public function enable_order_by_date($enable = true)
   991 	public function enable_order_by_date($enable = true)
  1179 			$this->strip_comments(false);
  1219 			$this->strip_comments(false);
  1180 			$this->strip_htmltags(false);
  1220 			$this->strip_htmltags(false);
  1181 			$this->strip_attributes(false);
  1221 			$this->strip_attributes(false);
  1182 			$this->add_attributes(false);
  1222 			$this->add_attributes(false);
  1183 			$this->set_image_handler(false);
  1223 			$this->set_image_handler(false);
       
  1224 			$this->set_https_domains(array());
  1184 		}
  1225 		}
  1185 	}
  1226 	}
  1186 
  1227 
  1187 	/**
  1228 	/**
  1188 	 * Set maximum number of feeds to check with autodiscovery
  1229 	 * Set maximum number of feeds to check with autodiscovery
  1282 	{
  1323 	{
  1283 		$this->sanitize->set_url_replacements($element_attribute);
  1324 		$this->sanitize->set_url_replacements($element_attribute);
  1284 	}
  1325 	}
  1285 
  1326 
  1286 	/**
  1327 	/**
       
  1328 	 * Set the list of domains for which to force HTTPS.
       
  1329 	 * @see SimplePie_Sanitize::set_https_domains()
       
  1330 	 * @param array List of HTTPS domains. Example array('biz', 'example.com', 'example.org', 'www.example.net').
       
  1331 	 */
       
  1332 	public function set_https_domains($domains = array())
       
  1333 	{
       
  1334 		if (is_array($domains))
       
  1335 		{
       
  1336 			$this->sanitize->set_https_domains($domains);
       
  1337 		}
       
  1338 	}
       
  1339 
       
  1340 	/**
  1287 	 * Set the handler to enable the display of cached images.
  1341 	 * Set the handler to enable the display of cached images.
  1288 	 *
  1342 	 *
  1289 	 * @param string $page Web-accessible path to the handler_image.php file.
  1343 	 * @param string $page Web-accessible path to the handler_image.php file.
  1290 	 * @param string $qs The query string that the value should be passed to.
  1344 	 * @param string $qs The query string that the value should be passed to.
  1291 	 */
  1345 	 */
  1406 			$parsed_feed_url = $this->registry->call('Misc', 'parse_url', array($this->feed_url));
  1460 			$parsed_feed_url = $this->registry->call('Misc', 'parse_url', array($this->feed_url));
  1407 
  1461 
  1408 			// Decide whether to enable caching
  1462 			// Decide whether to enable caching
  1409 			if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1463 			if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1410 			{
  1464 			{
  1411 				$url = $this->feed_url . ($this->force_feed ? '#force_feed' : '');
  1465 				$filename = $this->get_cache_filename($this->feed_url);
  1412 				$cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $url), 'spc'));
  1466 				$cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, $filename, 'spc'));
  1413 			}
  1467 			}
  1414 
  1468 
  1415 			// Fetch the data via SimplePie_File into $this->raw_data
  1469 			// Fetch the data via SimplePie_File into $this->raw_data
  1416 			if (($fetched = $this->fetch_data($cache)) === true)
  1470 			if (($fetched = $this->fetch_data($cache)) === true)
  1417 			{
  1471 			{
  1547 
  1601 
  1548 	/**
  1602 	/**
  1549 	 * Fetch the data via SimplePie_File
  1603 	 * Fetch the data via SimplePie_File
  1550 	 *
  1604 	 *
  1551 	 * If the data is already cached, attempt to fetch it from there instead
  1605 	 * If the data is already cached, attempt to fetch it from there instead
  1552 	 * @param SimplePie_Cache|false $cache Cache handler, or false to not load from the cache
  1606 	 * @param SimplePie_Cache_Base|false $cache Cache handler, or false to not load from the cache
  1553 	 * @return array|true Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type
  1607 	 * @return array|true Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type
  1554 	 */
  1608 	 */
  1555 	protected function fetch_data(&$cache)
  1609 	protected function fetch_data(&$cache)
  1556 	{
  1610 	{
  1557 		// If it's enabled, use the cache
  1611 		// If it's enabled, use the cache
  1610 						{
  1664 						{
  1611 							$headers['if-none-match'] = $this->data['headers']['etag'];
  1665 							$headers['if-none-match'] = $this->data['headers']['etag'];
  1612 						}
  1666 						}
  1613 
  1667 
  1614 						$file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1668 						$file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
       
  1669 						$this->status_code = $file->status_code;
  1615 
  1670 
  1616 						if ($file->success)
  1671 						if ($file->success)
  1617 						{
  1672 						{
  1618 							if ($file->status_code === 304)
  1673 							if ($file->status_code === 304)
  1619 							{
  1674 							{
  1664 					'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  1719 					'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  1665 				);
  1720 				);
  1666 				$file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1721 				$file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1667 			}
  1722 			}
  1668 		}
  1723 		}
       
  1724 		$this->status_code = $file->status_code;
       
  1725 
  1669 		// If the file connection has an error, set SimplePie::error to that and quit
  1726 		// If the file connection has an error, set SimplePie::error to that and quit
  1670 		if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1727 		if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1671 		{
  1728 		{
  1672 			$this->error = $file->error;
  1729 			$this->error = $file->error;
  1673 			return !empty($this->data);
  1730 			return !empty($this->data);
  1761 
  1818 
  1762 		return array($headers, $sniffed);
  1819 		return array($headers, $sniffed);
  1763 	}
  1820 	}
  1764 
  1821 
  1765 	/**
  1822 	/**
  1766 	 * Get the error message for the occured error
  1823 	 * Get the error message for the occurred error
  1767 	 *
  1824 	 *
  1768 	 * @return string|array Error message, or array of messages for multifeeds
  1825 	 * @return string|array Error message, or array of messages for multifeeds
  1769 	 */
  1826 	 */
  1770 	public function error()
  1827 	public function error()
  1771 	{
  1828 	{
  1772 		return $this->error;
  1829 		return $this->error;
       
  1830 	}
       
  1831 
       
  1832 	/**
       
  1833 	 * Get the last HTTP status code
       
  1834 	 *
       
  1835 	 * @return int Status code
       
  1836 	 */
       
  1837 	public function status_code()
       
  1838 	{
       
  1839 		return $this->status_code;
  1773 	}
  1840 	}
  1774 
  1841 
  1775 	/**
  1842 	/**
  1776 	 * Get the raw XML
  1843 	 * Get the raw XML
  1777 	 *
  1844 	 *
  2613 				}
  2680 				}
  2614 				$this->data['links'][$key] = array_unique($this->data['links'][$key]);
  2681 				$this->data['links'][$key] = array_unique($this->data['links'][$key]);
  2615 			}
  2682 			}
  2616 		}
  2683 		}
  2617 
  2684 
  2618 		if (isset($this->data['headers']['link']) &&
  2685 		if (isset($this->data['headers']['link']))
  2619 		    preg_match('/<([^>]+)>; rel='.preg_quote($rel).'/',
  2686 		{
  2620 		               $this->data['headers']['link'], $match))
  2687 			$link_headers = $this->data['headers']['link'];
  2621 		{
  2688 			if (is_string($link_headers)) {
  2622 			return array($match[1]);
  2689 				$link_headers = array($link_headers);
  2623 		}
  2690 			}
  2624 		else if (isset($this->data['links'][$rel]))
  2691 			$matches = preg_filter('/<([^>]+)>; rel='.preg_quote($rel).'/', '$1', $link_headers);
       
  2692 			if (!empty($matches)) {
       
  2693 				return $matches;
       
  2694 			}
       
  2695 		}
       
  2696 
       
  2697 		if (isset($this->data['links'][$rel]))
  2625 		{
  2698 		{
  2626 			return $this->data['links'][$rel];
  2699 			return $this->data['links'][$rel];
  2627 		}
  2700 		}
  2628 
  2701 
  2629 		return null;
  2702 		return null;