PageRenderTime 68ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/class-simplepie.php

http://github.com/markjaquith/WordPress
PHP | 3281 lines | 1827 code | 275 blank | 1179 comment | 228 complexity | bc1330cf64b696785e74f37669ddf011 MD5 | raw file
Possible License(s): 0BSD

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. if ( ! class_exists( 'SimplePie', false ) ) :
  3. // Load classes we will need.
  4. require ABSPATH . WPINC . '/SimplePie/Misc.php';
  5. require ABSPATH . WPINC . '/SimplePie/Cache.php';
  6. require ABSPATH . WPINC . '/SimplePie/File.php';
  7. require ABSPATH . WPINC . '/SimplePie/Sanitize.php';
  8. require ABSPATH . WPINC . '/SimplePie/Registry.php';
  9. require ABSPATH . WPINC . '/SimplePie/IRI.php';
  10. require ABSPATH . WPINC . '/SimplePie/Locator.php';
  11. require ABSPATH . WPINC . '/SimplePie/Content/Type/Sniffer.php';
  12. require ABSPATH . WPINC . '/SimplePie/XML/Declaration/Parser.php';
  13. require ABSPATH . WPINC . '/SimplePie/Parser.php';
  14. require ABSPATH . WPINC . '/SimplePie/Item.php';
  15. require ABSPATH . WPINC . '/SimplePie/Parse/Date.php';
  16. require ABSPATH . WPINC . '/SimplePie/Author.php';
  17. /**
  18. * WordPress autoloader for SimplePie.
  19. *
  20. * @since 3.5.0
  21. */
  22. function wp_simplepie_autoload( $class ) {
  23. if ( 0 !== strpos( $class, 'SimplePie_' ) )
  24. return;
  25. $file = ABSPATH . WPINC . '/' . str_replace( '_', '/', $class ) . '.php';
  26. include $file;
  27. }
  28. /**
  29. * We autoload classes we may not need.
  30. */
  31. spl_autoload_register( 'wp_simplepie_autoload' );
  32. /**
  33. * SimplePie
  34. *
  35. * A PHP-Based RSS and Atom Feed Framework.
  36. * Takes the hard work out of managing a complete RSS/Atom solution.
  37. *
  38. * Copyright (c) 2004-2017, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
  39. * All rights reserved.
  40. *
  41. * Redistribution and use in source and binary forms, with or without modification, are
  42. * permitted provided that the following conditions are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright notice, this list of
  45. * conditions and the following disclaimer.
  46. *
  47. * * Redistributions in binary form must reproduce the above copyright notice, this list
  48. * of conditions and the following disclaimer in the documentation and/or other materials
  49. * provided with the distribution.
  50. *
  51. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  52. * to endorse or promote products derived from this software without specific prior
  53. * written permission.
  54. *
  55. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  56. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  57. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  58. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  59. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  60. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  61. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  62. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  63. * POSSIBILITY OF SUCH DAMAGE.
  64. *
  65. * @package SimplePie
  66. * @version 1.5.5
  67. * @copyright 2004-2017 Ryan Parman, Sam Sneddon, Ryan McCue
  68. * @author Ryan Parman
  69. * @author Sam Sneddon
  70. * @author Ryan McCue
  71. * @link http://simplepie.org/ SimplePie
  72. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  73. */
  74. /**
  75. * SimplePie Name
  76. */
  77. define('SIMPLEPIE_NAME', 'SimplePie');
  78. /**
  79. * SimplePie Version
  80. */
  81. define('SIMPLEPIE_VERSION', '1.5.5');
  82. /**
  83. * SimplePie Build
  84. * @todo Hardcode for release (there's no need to have to call SimplePie_Misc::get_build() only every load of simplepie.inc)
  85. */
  86. define('SIMPLEPIE_BUILD', gmdate('YmdHis', SimplePie_Misc::get_build()));
  87. /**
  88. * SimplePie Website URL
  89. */
  90. define('SIMPLEPIE_URL', 'http://simplepie.org');
  91. /**
  92. * SimplePie Useragent
  93. * @see SimplePie::set_useragent()
  94. */
  95. define('SIMPLEPIE_USERAGENT', SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION . ' (Feed Parser; ' . SIMPLEPIE_URL . '; Allow like Gecko) Build/' . SIMPLEPIE_BUILD);
  96. /**
  97. * SimplePie Linkback
  98. */
  99. define('SIMPLEPIE_LINKBACK', '<a href="' . SIMPLEPIE_URL . '" title="' . SIMPLEPIE_NAME . ' ' . SIMPLEPIE_VERSION . '">' . SIMPLEPIE_NAME . '</a>');
  100. /**
  101. * No Autodiscovery
  102. * @see SimplePie::set_autodiscovery_level()
  103. */
  104. define('SIMPLEPIE_LOCATOR_NONE', 0);
  105. /**
  106. * Feed Link Element Autodiscovery
  107. * @see SimplePie::set_autodiscovery_level()
  108. */
  109. define('SIMPLEPIE_LOCATOR_AUTODISCOVERY', 1);
  110. /**
  111. * Local Feed Extension Autodiscovery
  112. * @see SimplePie::set_autodiscovery_level()
  113. */
  114. define('SIMPLEPIE_LOCATOR_LOCAL_EXTENSION', 2);
  115. /**
  116. * Local Feed Body Autodiscovery
  117. * @see SimplePie::set_autodiscovery_level()
  118. */
  119. define('SIMPLEPIE_LOCATOR_LOCAL_BODY', 4);
  120. /**
  121. * Remote Feed Extension Autodiscovery
  122. * @see SimplePie::set_autodiscovery_level()
  123. */
  124. define('SIMPLEPIE_LOCATOR_REMOTE_EXTENSION', 8);
  125. /**
  126. * Remote Feed Body Autodiscovery
  127. * @see SimplePie::set_autodiscovery_level()
  128. */
  129. define('SIMPLEPIE_LOCATOR_REMOTE_BODY', 16);
  130. /**
  131. * All Feed Autodiscovery
  132. * @see SimplePie::set_autodiscovery_level()
  133. */
  134. define('SIMPLEPIE_LOCATOR_ALL', 31);
  135. /**
  136. * No known feed type
  137. */
  138. define('SIMPLEPIE_TYPE_NONE', 0);
  139. /**
  140. * RSS 0.90
  141. */
  142. define('SIMPLEPIE_TYPE_RSS_090', 1);
  143. /**
  144. * RSS 0.91 (Netscape)
  145. */
  146. define('SIMPLEPIE_TYPE_RSS_091_NETSCAPE', 2);
  147. /**
  148. * RSS 0.91 (Userland)
  149. */
  150. define('SIMPLEPIE_TYPE_RSS_091_USERLAND', 4);
  151. /**
  152. * RSS 0.91 (both Netscape and Userland)
  153. */
  154. define('SIMPLEPIE_TYPE_RSS_091', 6);
  155. /**
  156. * RSS 0.92
  157. */
  158. define('SIMPLEPIE_TYPE_RSS_092', 8);
  159. /**
  160. * RSS 0.93
  161. */
  162. define('SIMPLEPIE_TYPE_RSS_093', 16);
  163. /**
  164. * RSS 0.94
  165. */
  166. define('SIMPLEPIE_TYPE_RSS_094', 32);
  167. /**
  168. * RSS 1.0
  169. */
  170. define('SIMPLEPIE_TYPE_RSS_10', 64);
  171. /**
  172. * RSS 2.0
  173. */
  174. define('SIMPLEPIE_TYPE_RSS_20', 128);
  175. /**
  176. * RDF-based RSS
  177. */
  178. define('SIMPLEPIE_TYPE_RSS_RDF', 65);
  179. /**
  180. * Non-RDF-based RSS (truly intended as syndication format)
  181. */
  182. define('SIMPLEPIE_TYPE_RSS_SYNDICATION', 190);
  183. /**
  184. * All RSS
  185. */
  186. define('SIMPLEPIE_TYPE_RSS_ALL', 255);
  187. /**
  188. * Atom 0.3
  189. */
  190. define('SIMPLEPIE_TYPE_ATOM_03', 256);
  191. /**
  192. * Atom 1.0
  193. */
  194. define('SIMPLEPIE_TYPE_ATOM_10', 512);
  195. /**
  196. * All Atom
  197. */
  198. define('SIMPLEPIE_TYPE_ATOM_ALL', 768);
  199. /**
  200. * All feed types
  201. */
  202. define('SIMPLEPIE_TYPE_ALL', 1023);
  203. /**
  204. * No construct
  205. */
  206. define('SIMPLEPIE_CONSTRUCT_NONE', 0);
  207. /**
  208. * Text construct
  209. */
  210. define('SIMPLEPIE_CONSTRUCT_TEXT', 1);
  211. /**
  212. * HTML construct
  213. */
  214. define('SIMPLEPIE_CONSTRUCT_HTML', 2);
  215. /**
  216. * XHTML construct
  217. */
  218. define('SIMPLEPIE_CONSTRUCT_XHTML', 4);
  219. /**
  220. * base64-encoded construct
  221. */
  222. define('SIMPLEPIE_CONSTRUCT_BASE64', 8);
  223. /**
  224. * IRI construct
  225. */
  226. define('SIMPLEPIE_CONSTRUCT_IRI', 16);
  227. /**
  228. * A construct that might be HTML
  229. */
  230. define('SIMPLEPIE_CONSTRUCT_MAYBE_HTML', 32);
  231. /**
  232. * All constructs
  233. */
  234. define('SIMPLEPIE_CONSTRUCT_ALL', 63);
  235. /**
  236. * Don't change case
  237. */
  238. define('SIMPLEPIE_SAME_CASE', 1);
  239. /**
  240. * Change to lowercase
  241. */
  242. define('SIMPLEPIE_LOWERCASE', 2);
  243. /**
  244. * Change to uppercase
  245. */
  246. define('SIMPLEPIE_UPPERCASE', 4);
  247. /**
  248. * PCRE for HTML attributes
  249. */
  250. define('SIMPLEPIE_PCRE_HTML_ATTRIBUTE', '((?:[\x09\x0A\x0B\x0C\x0D\x20]+[^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"(?:[^"]*)"|\'(?:[^\']*)\'|(?:[^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?)*)[\x09\x0A\x0B\x0C\x0D\x20]*');
  251. /**
  252. * PCRE for XML attributes
  253. */
  254. define('SIMPLEPIE_PCRE_XML_ATTRIBUTE', '((?:\s+(?:(?:[^\s:]+:)?[^\s:]+)\s*=\s*(?:"(?:[^"]*)"|\'(?:[^\']*)\'))*)\s*');
  255. /**
  256. * XML Namespace
  257. */
  258. define('SIMPLEPIE_NAMESPACE_XML', 'http://www.w3.org/XML/1998/namespace');
  259. /**
  260. * Atom 1.0 Namespace
  261. */
  262. define('SIMPLEPIE_NAMESPACE_ATOM_10', 'http://www.w3.org/2005/Atom');
  263. /**
  264. * Atom 0.3 Namespace
  265. */
  266. define('SIMPLEPIE_NAMESPACE_ATOM_03', 'http://purl.org/atom/ns#');
  267. /**
  268. * RDF Namespace
  269. */
  270. define('SIMPLEPIE_NAMESPACE_RDF', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  271. /**
  272. * RSS 0.90 Namespace
  273. */
  274. define('SIMPLEPIE_NAMESPACE_RSS_090', 'http://my.netscape.com/rdf/simple/0.9/');
  275. /**
  276. * RSS 1.0 Namespace
  277. */
  278. define('SIMPLEPIE_NAMESPACE_RSS_10', 'http://purl.org/rss/1.0/');
  279. /**
  280. * RSS 1.0 Content Module Namespace
  281. */
  282. define('SIMPLEPIE_NAMESPACE_RSS_10_MODULES_CONTENT', 'http://purl.org/rss/1.0/modules/content/');
  283. /**
  284. * RSS 2.0 Namespace
  285. * (Stupid, I know, but I'm certain it will confuse people less with support.)
  286. */
  287. define('SIMPLEPIE_NAMESPACE_RSS_20', '');
  288. /**
  289. * DC 1.0 Namespace
  290. */
  291. define('SIMPLEPIE_NAMESPACE_DC_10', 'http://purl.org/dc/elements/1.0/');
  292. /**
  293. * DC 1.1 Namespace
  294. */
  295. define('SIMPLEPIE_NAMESPACE_DC_11', 'http://purl.org/dc/elements/1.1/');
  296. /**
  297. * W3C Basic Geo (WGS84 lat/long) Vocabulary Namespace
  298. */
  299. define('SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO', 'http://www.w3.org/2003/01/geo/wgs84_pos#');
  300. /**
  301. * GeoRSS Namespace
  302. */
  303. define('SIMPLEPIE_NAMESPACE_GEORSS', 'http://www.georss.org/georss');
  304. /**
  305. * Media RSS Namespace
  306. */
  307. define('SIMPLEPIE_NAMESPACE_MEDIARSS', 'http://search.yahoo.com/mrss/');
  308. /**
  309. * Wrong Media RSS Namespace. Caused by a long-standing typo in the spec.
  310. */
  311. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG', 'http://search.yahoo.com/mrss');
  312. /**
  313. * Wrong Media RSS Namespace #2. New namespace introduced in Media RSS 1.5.
  314. */
  315. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2', 'http://video.search.yahoo.com/mrss');
  316. /**
  317. * Wrong Media RSS Namespace #3. A possible typo of the Media RSS 1.5 namespace.
  318. */
  319. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3', 'http://video.search.yahoo.com/mrss/');
  320. /**
  321. * Wrong Media RSS Namespace #4. New spec location after the RSS Advisory Board takes it over, but not a valid namespace.
  322. */
  323. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4', 'http://www.rssboard.org/media-rss');
  324. /**
  325. * Wrong Media RSS Namespace #5. A possible typo of the RSS Advisory Board URL.
  326. */
  327. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5', 'http://www.rssboard.org/media-rss/');
  328. /**
  329. * iTunes RSS Namespace
  330. */
  331. define('SIMPLEPIE_NAMESPACE_ITUNES', 'http://www.itunes.com/dtds/podcast-1.0.dtd');
  332. /**
  333. * XHTML Namespace
  334. */
  335. define('SIMPLEPIE_NAMESPACE_XHTML', 'http://www.w3.org/1999/xhtml');
  336. /**
  337. * IANA Link Relations Registry
  338. */
  339. define('SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY', 'http://www.iana.org/assignments/relation/');
  340. /**
  341. * No file source
  342. */
  343. define('SIMPLEPIE_FILE_SOURCE_NONE', 0);
  344. /**
  345. * Remote file source
  346. */
  347. define('SIMPLEPIE_FILE_SOURCE_REMOTE', 1);
  348. /**
  349. * Local file source
  350. */
  351. define('SIMPLEPIE_FILE_SOURCE_LOCAL', 2);
  352. /**
  353. * fsockopen() file source
  354. */
  355. define('SIMPLEPIE_FILE_SOURCE_FSOCKOPEN', 4);
  356. /**
  357. * cURL file source
  358. */
  359. define('SIMPLEPIE_FILE_SOURCE_CURL', 8);
  360. /**
  361. * file_get_contents() file source
  362. */
  363. define('SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS', 16);
  364. /**
  365. * SimplePie
  366. *
  367. * @package SimplePie
  368. * @subpackage API
  369. */
  370. class SimplePie
  371. {
  372. /**
  373. * @var array Raw data
  374. * @access private
  375. */
  376. public $data = array();
  377. /**
  378. * @var mixed Error string
  379. * @access private
  380. */
  381. public $error;
  382. /**
  383. * @var object Instance of SimplePie_Sanitize (or other class)
  384. * @see SimplePie::set_sanitize_class()
  385. * @access private
  386. */
  387. public $sanitize;
  388. /**
  389. * @var string SimplePie Useragent
  390. * @see SimplePie::set_useragent()
  391. * @access private
  392. */
  393. public $useragent = SIMPLEPIE_USERAGENT;
  394. /**
  395. * @var string Feed URL
  396. * @see SimplePie::set_feed_url()
  397. * @access private
  398. */
  399. public $feed_url;
  400. /**
  401. * @var string Original feed URL, or new feed URL iff HTTP 301 Moved Permanently
  402. * @see SimplePie::subscribe_url()
  403. * @access private
  404. */
  405. public $permanent_url = null;
  406. /**
  407. * @var object Instance of SimplePie_File to use as a feed
  408. * @see SimplePie::set_file()
  409. * @access private
  410. */
  411. public $file;
  412. /**
  413. * @var string Raw feed data
  414. * @see SimplePie::set_raw_data()
  415. * @access private
  416. */
  417. public $raw_data;
  418. /**
  419. * @var int Timeout for fetching remote files
  420. * @see SimplePie::set_timeout()
  421. * @access private
  422. */
  423. public $timeout = 10;
  424. /**
  425. * @var array Custom curl options
  426. * @see SimplePie::set_curl_options()
  427. * @access private
  428. */
  429. public $curl_options = array();
  430. /**
  431. * @var bool Forces fsockopen() to be used for remote files instead
  432. * of cURL, even if a new enough version is installed
  433. * @see SimplePie::force_fsockopen()
  434. * @access private
  435. */
  436. public $force_fsockopen = false;
  437. /**
  438. * @var bool Force the given data/URL to be treated as a feed no matter what
  439. * it appears like
  440. * @see SimplePie::force_feed()
  441. * @access private
  442. */
  443. public $force_feed = false;
  444. /**
  445. * @var bool Enable/Disable Caching
  446. * @see SimplePie::enable_cache()
  447. * @access private
  448. */
  449. public $cache = true;
  450. /**
  451. * @var bool Force SimplePie to fallback to expired cache, if enabled,
  452. * when feed is unavailable.
  453. * @see SimplePie::force_cache_fallback()
  454. * @access private
  455. */
  456. public $force_cache_fallback = false;
  457. /**
  458. * @var int Cache duration (in seconds)
  459. * @see SimplePie::set_cache_duration()
  460. * @access private
  461. */
  462. public $cache_duration = 3600;
  463. /**
  464. * @var int Auto-discovery cache duration (in seconds)
  465. * @see SimplePie::set_autodiscovery_cache_duration()
  466. * @access private
  467. */
  468. public $autodiscovery_cache_duration = 604800; // 7 Days.
  469. /**
  470. * @var string Cache location (relative to executing script)
  471. * @see SimplePie::set_cache_location()
  472. * @access private
  473. */
  474. public $cache_location = './cache';
  475. /**
  476. * @var string Function that creates the cache filename
  477. * @see SimplePie::set_cache_name_function()
  478. * @access private
  479. */
  480. public $cache_name_function = 'md5';
  481. /**
  482. * @var bool Reorder feed by date descending
  483. * @see SimplePie::enable_order_by_date()
  484. * @access private
  485. */
  486. public $order_by_date = true;
  487. /**
  488. * @var mixed Force input encoding to be set to the follow value
  489. * (false, or anything type-cast to false, disables this feature)
  490. * @see SimplePie::set_input_encoding()
  491. * @access private
  492. */
  493. public $input_encoding = false;
  494. /**
  495. * @var int Feed Autodiscovery Level
  496. * @see SimplePie::set_autodiscovery_level()
  497. * @access private
  498. */
  499. public $autodiscovery = SIMPLEPIE_LOCATOR_ALL;
  500. /**
  501. * Class registry object
  502. *
  503. * @var SimplePie_Registry
  504. */
  505. public $registry;
  506. /**
  507. * @var int Maximum number of feeds to check with autodiscovery
  508. * @see SimplePie::set_max_checked_feeds()
  509. * @access private
  510. */
  511. public $max_checked_feeds = 10;
  512. /**
  513. * @var array All the feeds found during the autodiscovery process
  514. * @see SimplePie::get_all_discovered_feeds()
  515. * @access private
  516. */
  517. public $all_discovered_feeds = array();
  518. /**
  519. * @var string Web-accessible path to the handler_image.php file.
  520. * @see SimplePie::set_image_handler()
  521. * @access private
  522. */
  523. public $image_handler = '';
  524. /**
  525. * @var array Stores the URLs when multiple feeds are being initialized.
  526. * @see SimplePie::set_feed_url()
  527. * @access private
  528. */
  529. public $multifeed_url = array();
  530. /**
  531. * @var array Stores SimplePie objects when multiple feeds initialized.
  532. * @access private
  533. */
  534. public $multifeed_objects = array();
  535. /**
  536. * @var array Stores the get_object_vars() array for use with multifeeds.
  537. * @see SimplePie::set_feed_url()
  538. * @access private
  539. */
  540. public $config_settings = null;
  541. /**
  542. * @var integer Stores the number of items to return per-feed with multifeeds.
  543. * @see SimplePie::set_item_limit()
  544. * @access private
  545. */
  546. public $item_limit = 0;
  547. /**
  548. * @var bool Stores if last-modified and/or etag headers were sent with the
  549. * request when checking a feed.
  550. */
  551. public $check_modified = false;
  552. /**
  553. * @var array Stores the default attributes to be stripped by strip_attributes().
  554. * @see SimplePie::strip_attributes()
  555. * @access private
  556. */
  557. public $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc');
  558. /**
  559. * @var array Stores the default attributes to add to different tags by add_attributes().
  560. * @see SimplePie::add_attributes()
  561. * @access private
  562. */
  563. public $add_attributes = array('audio' => array('preload' => 'none'), 'iframe' => array('sandbox' => 'allow-scripts allow-same-origin'), 'video' => array('preload' => 'none'));
  564. /**
  565. * @var array Stores the default tags to be stripped by strip_htmltags().
  566. * @see SimplePie::strip_htmltags()
  567. * @access private
  568. */
  569. public $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
  570. /**
  571. * @var bool Should we throw exceptions, or use the old-style error property?
  572. * @access private
  573. */
  574. public $enable_exceptions = false;
  575. /**
  576. * The SimplePie class contains feed level data and options
  577. *
  578. * To use SimplePie, create the SimplePie object with no parameters. You can
  579. * then set configuration options using the provided methods. After setting
  580. * them, you must initialise the feed using $feed->init(). At that point the
  581. * object's methods and properties will be available to you.
  582. *
  583. * Previously, it was possible to pass in the feed URL along with cache
  584. * options directly into the constructor. This has been removed as of 1.3 as
  585. * it caused a lot of confusion.
  586. *
  587. * @since 1.0 Preview Release
  588. */
  589. public function __construct()
  590. {
  591. if (version_compare(PHP_VERSION, '5.6', '<'))
  592. {
  593. trigger_error('Please upgrade to PHP 5.6 or newer.');
  594. die();
  595. }
  596. // Other objects, instances created here so we can set options on them
  597. $this->sanitize = new SimplePie_Sanitize();
  598. $this->registry = new SimplePie_Registry();
  599. if (func_num_args() > 0)
  600. {
  601. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  602. trigger_error('Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_duration() directly.', $level);
  603. $args = func_get_args();
  604. switch (count($args)) {
  605. case 3:
  606. $this->set_cache_duration($args[2]);
  607. case 2:
  608. $this->set_cache_location($args[1]);
  609. case 1:
  610. $this->set_feed_url($args[0]);
  611. $this->init();
  612. }
  613. }
  614. }
  615. /**
  616. * Used for converting object to a string
  617. */
  618. public function __toString()
  619. {
  620. return md5(serialize($this->data));
  621. }
  622. /**
  623. * Remove items that link back to this before destroying this object
  624. */
  625. public function __destruct()
  626. {
  627. if (!gc_enabled())
  628. {
  629. if (!empty($this->data['items']))
  630. {
  631. foreach ($this->data['items'] as $item)
  632. {
  633. $item->__destruct();
  634. }
  635. unset($item, $this->data['items']);
  636. }
  637. if (!empty($this->data['ordered_items']))
  638. {
  639. foreach ($this->data['ordered_items'] as $item)
  640. {
  641. $item->__destruct();
  642. }
  643. unset($item, $this->data['ordered_items']);
  644. }
  645. }
  646. }
  647. /**
  648. * Force the given data/URL to be treated as a feed
  649. *
  650. * This tells SimplePie to ignore the content-type provided by the server.
  651. * Be careful when using this option, as it will also disable autodiscovery.
  652. *
  653. * @since 1.1
  654. * @param bool $enable Force the given data/URL to be treated as a feed
  655. */
  656. public function force_feed($enable = false)
  657. {
  658. $this->force_feed = (bool) $enable;
  659. }
  660. /**
  661. * Set the URL of the feed you want to parse
  662. *
  663. * This allows you to enter the URL of the feed you want to parse, or the
  664. * website you want to try to use auto-discovery on. This takes priority
  665. * over any set raw data.
  666. *
  667. * You can set multiple feeds to mash together by passing an array instead
  668. * of a string for the $url. Remember that with each additional feed comes
  669. * additional processing and resources.
  670. *
  671. * @since 1.0 Preview Release
  672. * @see set_raw_data()
  673. * @param string|array $url This is the URL (or array of URLs) that you want to parse.
  674. */
  675. public function set_feed_url($url)
  676. {
  677. $this->multifeed_url = array();
  678. if (is_array($url))
  679. {
  680. foreach ($url as $value)
  681. {
  682. $this->multifeed_url[] = $this->registry->call('Misc', 'fix_protocol', array($value, 1));
  683. }
  684. }
  685. else
  686. {
  687. $this->feed_url = $this->registry->call('Misc', 'fix_protocol', array($url, 1));
  688. $this->permanent_url = $this->feed_url;
  689. }
  690. }
  691. /**
  692. * Set an instance of {@see SimplePie_File} to use as a feed
  693. *
  694. * @param SimplePie_File &$file
  695. * @return bool True on success, false on failure
  696. */
  697. public function set_file(&$file)
  698. {
  699. if ($file instanceof SimplePie_File)
  700. {
  701. $this->feed_url = $file->url;
  702. $this->permanent_url = $this->feed_url;
  703. $this->file =& $file;
  704. return true;
  705. }
  706. return false;
  707. }
  708. /**
  709. * Set the raw XML data to parse
  710. *
  711. * Allows you to use a string of RSS/Atom data instead of a remote feed.
  712. *
  713. * If you have a feed available as a string in PHP, you can tell SimplePie
  714. * to parse that data string instead of a remote feed. Any set feed URL
  715. * takes precedence.
  716. *
  717. * @since 1.0 Beta 3
  718. * @param string $data RSS or Atom data as a string.
  719. * @see set_feed_url()
  720. */
  721. public function set_raw_data($data)
  722. {
  723. $this->raw_data = $data;
  724. }
  725. /**
  726. * Set the default timeout for fetching remote feeds
  727. *
  728. * This allows you to change the maximum time the feed's server to respond
  729. * and send the feed back.
  730. *
  731. * @since 1.0 Beta 3
  732. * @param int $timeout The maximum number of seconds to spend waiting to retrieve a feed.
  733. */
  734. public function set_timeout($timeout = 10)
  735. {
  736. $this->timeout = (int) $timeout;
  737. }
  738. /**
  739. * Set custom curl options
  740. *
  741. * This allows you to change default curl options
  742. *
  743. * @since 1.0 Beta 3
  744. * @param array $curl_options Curl options to add to default settings
  745. */
  746. public function set_curl_options(array $curl_options = array())
  747. {
  748. $this->curl_options = $curl_options;
  749. }
  750. /**
  751. * Force SimplePie to use fsockopen() instead of cURL
  752. *
  753. * @since 1.0 Beta 3
  754. * @param bool $enable Force fsockopen() to be used
  755. */
  756. public function force_fsockopen($enable = false)
  757. {
  758. $this->force_fsockopen = (bool) $enable;
  759. }
  760. /**
  761. * Enable/disable caching in SimplePie.
  762. *
  763. * This option allows you to disable caching all-together in SimplePie.
  764. * However, disabling the cache can lead to longer load times.
  765. *
  766. * @since 1.0 Preview Release
  767. * @param bool $enable Enable caching
  768. */
  769. public function enable_cache($enable = true)
  770. {
  771. $this->cache = (bool) $enable;
  772. }
  773. /**
  774. * SimplePie to continue to fall back to expired cache, if enabled, when
  775. * feed is unavailable.
  776. *
  777. * This tells SimplePie to ignore any file errors and fall back to cache
  778. * instead. This only works if caching is enabled and cached content
  779. * still exists.
  780. * @param bool $enable Force use of cache on fail.
  781. */
  782. public function force_cache_fallback($enable = false)
  783. {
  784. $this->force_cache_fallback= (bool) $enable;
  785. }
  786. /**
  787. * Set the length of time (in seconds) that the contents of a feed will be
  788. * cached
  789. *
  790. * @param int $seconds The feed content cache duration
  791. */
  792. public function set_cache_duration($seconds = 3600)
  793. {
  794. $this->cache_duration = (int) $seconds;
  795. }
  796. /**
  797. * Set the length of time (in seconds) that the autodiscovered feed URL will
  798. * be cached
  799. *
  800. * @param int $seconds The autodiscovered feed URL cache duration.
  801. */
  802. public function set_autodiscovery_cache_duration($seconds = 604800)
  803. {
  804. $this->autodiscovery_cache_duration = (int) $seconds;
  805. }
  806. /**
  807. * Set the file system location where the cached files should be stored
  808. *
  809. * @param string $location The file system location.
  810. */
  811. public function set_cache_location($location = './cache')
  812. {
  813. $this->cache_location = (string) $location;
  814. }
  815. /**
  816. * Set whether feed items should be sorted into reverse chronological order
  817. *
  818. * @param bool $enable Sort as reverse chronological order.
  819. */
  820. public function enable_order_by_date($enable = true)
  821. {
  822. $this->order_by_date = (bool) $enable;
  823. }
  824. /**
  825. * Set the character encoding used to parse the feed
  826. *
  827. * This overrides the encoding reported by the feed, however it will fall
  828. * back to the normal encoding detection if the override fails
  829. *
  830. * @param string $encoding Character encoding
  831. */
  832. public function set_input_encoding($encoding = false)
  833. {
  834. if ($encoding)
  835. {
  836. $this->input_encoding = (string) $encoding;
  837. }
  838. else
  839. {
  840. $this->input_encoding = false;
  841. }
  842. }
  843. /**
  844. * Set how much feed autodiscovery to do
  845. *
  846. * @see SIMPLEPIE_LOCATOR_NONE
  847. * @see SIMPLEPIE_LOCATOR_AUTODISCOVERY
  848. * @see SIMPLEPIE_LOCATOR_LOCAL_EXTENSION
  849. * @see SIMPLEPIE_LOCATOR_LOCAL_BODY
  850. * @see SIMPLEPIE_LOCATOR_REMOTE_EXTENSION
  851. * @see SIMPLEPIE_LOCATOR_REMOTE_BODY
  852. * @see SIMPLEPIE_LOCATOR_ALL
  853. * @param int $level Feed Autodiscovery Level (level can be a combination of the above constants, see bitwise OR operator)
  854. */
  855. public function set_autodiscovery_level($level = SIMPLEPIE_LOCATOR_ALL)
  856. {
  857. $this->autodiscovery = (int) $level;
  858. }
  859. /**
  860. * Get the class registry
  861. *
  862. * Use this to override SimplePie's default classes
  863. * @see SimplePie_Registry
  864. * @return SimplePie_Registry
  865. */
  866. public function &get_registry()
  867. {
  868. return $this->registry;
  869. }
  870. /**#@+
  871. * Useful when you are overloading or extending SimplePie's default classes.
  872. *
  873. * @deprecated Use {@see get_registry()} instead
  874. * @link http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.extends PHP5 extends documentation
  875. * @param string $class Name of custom class
  876. * @return boolean True on success, false otherwise
  877. */
  878. /**
  879. * Set which class SimplePie uses for caching
  880. */
  881. public function set_cache_class($class = 'SimplePie_Cache')
  882. {
  883. return $this->registry->register('Cache', $class, true);
  884. }
  885. /**
  886. * Set which class SimplePie uses for auto-discovery
  887. */
  888. public function set_locator_class($class = 'SimplePie_Locator')
  889. {
  890. return $this->registry->register('Locator', $class, true);
  891. }
  892. /**
  893. * Set which class SimplePie uses for XML parsing
  894. */
  895. public function set_parser_class($class = 'SimplePie_Parser')
  896. {
  897. return $this->registry->register('Parser', $class, true);
  898. }
  899. /**
  900. * Set which class SimplePie uses for remote file fetching
  901. */
  902. public function set_file_class($class = 'SimplePie_File')
  903. {
  904. return $this->registry->register('File', $class, true);
  905. }
  906. /**
  907. * Set which class SimplePie uses for data sanitization
  908. */
  909. public function set_sanitize_class($class = 'SimplePie_Sanitize')
  910. {
  911. return $this->registry->register('Sanitize', $class, true);
  912. }
  913. /**
  914. * Set which class SimplePie uses for handling feed items
  915. */
  916. public function set_item_class($class = 'SimplePie_Item')
  917. {
  918. return $this->registry->register('Item', $class, true);
  919. }
  920. /**
  921. * Set which class SimplePie uses for handling author data
  922. */
  923. public function set_author_class($class = 'SimplePie_Author')
  924. {
  925. return $this->registry->register('Author', $class, true);
  926. }
  927. /**
  928. * Set which class SimplePie uses for handling category data
  929. */
  930. public function set_category_class($class = 'SimplePie_Category')
  931. {
  932. return $this->registry->register('Category', $class, true);
  933. }
  934. /**
  935. * Set which class SimplePie uses for feed enclosures
  936. */
  937. public function set_enclosure_class($class = 'SimplePie_Enclosure')
  938. {
  939. return $this->registry->register('Enclosure', $class, true);
  940. }
  941. /**
  942. * Set which class SimplePie uses for `<media:text>` captions
  943. */
  944. public function set_caption_class($class = 'SimplePie_Caption')
  945. {
  946. return $this->registry->register('Caption', $class, true);
  947. }
  948. /**
  949. * Set which class SimplePie uses for `<media:copyright>`
  950. */
  951. public function set_copyright_class($class = 'SimplePie_Copyright')
  952. {
  953. return $this->registry->register('Copyright', $class, true);
  954. }
  955. /**
  956. * Set which class SimplePie uses for `<media:credit>`
  957. */
  958. public function set_credit_class($class = 'SimplePie_Credit')
  959. {
  960. return $this->registry->register('Credit', $class, true);
  961. }
  962. /**
  963. * Set which class SimplePie uses for `<media:rating>`
  964. */
  965. public function set_rating_class($class = 'SimplePie_Rating')
  966. {
  967. return $this->registry->register('Rating', $class, true);
  968. }
  969. /**
  970. * Set which class SimplePie uses for `<media:restriction>`
  971. */
  972. public function set_restriction_class($class = 'SimplePie_Restriction')
  973. {
  974. return $this->registry->register('Restriction', $class, true);
  975. }
  976. /**
  977. * Set which class SimplePie uses for content-type sniffing
  978. */
  979. public function set_content_type_sniffer_class($class = 'SimplePie_Content_Type_Sniffer')
  980. {
  981. return $this->registry->register('Content_Type_Sniffer', $class, true);
  982. }
  983. /**
  984. * Set which class SimplePie uses item sources
  985. */
  986. public function set_source_class($class = 'SimplePie_Source')
  987. {
  988. return $this->registry->register('Source', $class, true);
  989. }
  990. /**#@-*/
  991. /**
  992. * Set the user agent string
  993. *
  994. * @param string $ua New user agent string.
  995. */
  996. public function set_useragent($ua = SIMPLEPIE_USERAGENT)
  997. {
  998. $this->useragent = (string) $ua;
  999. }
  1000. /**
  1001. * Set callback function to create cache filename with
  1002. *
  1003. * @param mixed $function Callback function
  1004. */
  1005. public function set_cache_name_function($function = 'md5')
  1006. {
  1007. if (is_callable($function))
  1008. {
  1009. $this->cache_name_function = $function;
  1010. }
  1011. }
  1012. /**
  1013. * Set options to make SP as fast as possible
  1014. *
  1015. * Forgoes a substantial amount of data sanitization in favor of speed. This
  1016. * turns SimplePie into a dumb parser of feeds.
  1017. *
  1018. * @param bool $set Whether to set them or not
  1019. */
  1020. public function set_stupidly_fast($set = false)
  1021. {
  1022. if ($set)
  1023. {
  1024. $this->enable_order_by_date(false);
  1025. $this->remove_div(false);
  1026. $this->strip_comments(false);
  1027. $this->strip_htmltags(false);
  1028. $this->strip_attributes(false);
  1029. $this->add_attributes(false);
  1030. $this->set_image_handler(false);
  1031. }
  1032. }
  1033. /**
  1034. * Set maximum number of feeds to check with autodiscovery
  1035. *
  1036. * @param int $max Maximum number of feeds to check
  1037. */
  1038. public function set_max_checked_feeds($max = 10)
  1039. {
  1040. $this->max_checked_feeds = (int) $max;
  1041. }
  1042. public function remove_div($enable = true)
  1043. {
  1044. $this->sanitize->remove_div($enable);
  1045. }
  1046. public function strip_htmltags($tags = '', $encode = null)
  1047. {
  1048. if ($tags === '')
  1049. {
  1050. $tags = $this->strip_htmltags;
  1051. }
  1052. $this->sanitize->strip_htmltags($tags);
  1053. if ($encode !== null)
  1054. {
  1055. $this->sanitize->encode_instead_of_strip($tags);
  1056. }
  1057. }
  1058. public function encode_instead_of_strip($enable = true)
  1059. {
  1060. $this->sanitize->encode_instead_of_strip($enable);
  1061. }
  1062. public function strip_attributes($attribs = '')
  1063. {
  1064. if ($attribs === '')
  1065. {
  1066. $attribs = $this->strip_attributes;
  1067. }
  1068. $this->sanitize->strip_attributes($attribs);
  1069. }
  1070. public function add_attributes($attribs = '')
  1071. {
  1072. if ($attribs === '')
  1073. {
  1074. $attribs = $this->add_attributes;
  1075. }
  1076. $this->sanitize->add_attributes($attribs);
  1077. }
  1078. /**
  1079. * Set the output encoding
  1080. *
  1081. * Allows you to override SimplePie's output to match that of your webpage.
  1082. * This is useful for times when your webpages are not being served as
  1083. * UTF-8. This setting will be obeyed by {@see handle_content_type()}, and
  1084. * is similar to {@see set_input_encoding()}.
  1085. *
  1086. * It should be noted, however, that not all character encodings can support
  1087. * all characters. If your page is being served as ISO-8859-1 and you try
  1088. * to display a Japanese feed, you'll likely see garbled characters.
  1089. * Because of this, it is highly recommended to ensure that your webpages
  1090. * are served as UTF-8.
  1091. *
  1092. * The number of supported character encodings depends on whether your web
  1093. * host supports {@link http://php.net/mbstring mbstring},
  1094. * {@link http://php.net/iconv iconv}, or both. See
  1095. * {@link http://simplepie.org/wiki/faq/Supported_Character_Encodings} for
  1096. * more information.
  1097. *
  1098. * @param string $encoding
  1099. */
  1100. public function set_output_encoding($encoding = 'UTF-8')
  1101. {
  1102. $this->sanitize->set_output_encoding($encoding);
  1103. }
  1104. public function strip_comments($strip = false)
  1105. {
  1106. $this->sanitize->strip_comments($strip);
  1107. }
  1108. /**
  1109. * Set element/attribute key/value pairs of HTML attributes
  1110. * containing URLs that need to be resolved relative to the feed
  1111. *
  1112. * Defaults to |a|@href, |area|@href, |blockquote|@cite, |del|@cite,
  1113. * |form|@action, |img|@longdesc, |img|@src, |input|@src, |ins|@cite,
  1114. * |q|@cite
  1115. *
  1116. * @since 1.0
  1117. * @param array|null $element_attribute Element/attribute key/value pairs, null for default
  1118. */
  1119. public function set_url_replacements($element_attribute = null)
  1120. {
  1121. $this->sanitize->set_url_replacements($element_attribute);
  1122. }
  1123. /**
  1124. * Set the handler to enable the display of cached images.
  1125. *
  1126. * @param string $page Web-accessible path to the handler_image.php file.
  1127. * @param string $qs The query string that the value should be passed to.
  1128. */
  1129. public function set_image_handler($page = false, $qs = 'i')
  1130. {
  1131. if ($page !== false)
  1132. {
  1133. $this->sanitize->set_image_handler($page . '?' . $qs . '=');
  1134. }
  1135. else
  1136. {
  1137. $this->image_handler = '';
  1138. }
  1139. }
  1140. /**
  1141. * Set the limit for items returned per-feed with multifeeds
  1142. *
  1143. * @param integer $limit The maximum number of items to return.
  1144. */
  1145. public function set_item_limit($limit = 0)
  1146. {
  1147. $this->item_limit = (int) $limit;
  1148. }
  1149. /**
  1150. * Enable throwing exceptions
  1151. *
  1152. * @param boolean $enable Should we throw exceptions, or use the old-style error property?
  1153. */
  1154. public function enable_exceptions($enable = true)
  1155. {
  1156. $this->enable_exceptions = $enable;
  1157. }
  1158. /**
  1159. * Initialize the feed object
  1160. *
  1161. * This is what makes everything happen. Period. This is where all of the
  1162. * configuration options get processed, feeds are fetched, cached, and
  1163. * parsed, and all of that other good stuff.
  1164. *
  1165. * @return boolean True if successful, false otherwise
  1166. */
  1167. public function init()
  1168. {
  1169. // Check absolute bare minimum requirements.
  1170. if (!extension_loaded('xml') || !extension_loaded('pcre'))
  1171. {
  1172. $this->error = 'XML or PCRE extensions not loaded!';
  1173. return false;
  1174. }
  1175. // Then check the xml extension is sane (i.e., libxml 2.7.x issue on PHP < 5.2.9 and libxml 2.7.0 to 2.7.2 on any version) if we don't have xmlreader.
  1176. elseif (!extension_loaded('xmlreader'))
  1177. {
  1178. static $xml_is_sane = null;
  1179. if ($xml_is_sane === null)
  1180. {
  1181. $parser_check = xml_parser_create();
  1182. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  1183. xml_parser_free($parser_check);
  1184. $xml_is_sane = isset($values[0]['value']);
  1185. }
  1186. if (!$xml_is_sane)
  1187. {
  1188. return false;
  1189. }
  1190. }
  1191. // The default sanitize class gets set in the constructor, check if it has
  1192. // changed.
  1193. if ($this->registry->get_class('Sanitize') !== 'SimplePie_Sanitize') {
  1194. $this->sanitize = $this->registry->create('Sanitize');
  1195. }
  1196. if (method_exists($this->sanitize, 'set_registry'))
  1197. {
  1198. $this->sanitize->set_registry($this->registry);
  1199. }
  1200. // Pass whatever was set with config options over to the sanitizer.
  1201. // Pass the classes in for legacy support; new classes should use the registry instead
  1202. $this->sanitize->pass_cache_data($this->cache, $this->cache_location, $this->cache_name_function, $this->registry->get_class('Cache'));
  1203. $this->sanitize->pass_file_data($this->registry->get_class('File'), $this->timeout, $this->useragent, $this->force_fsockopen, $this->curl_options);
  1204. if (!empty($this->multifeed_url))
  1205. {
  1206. $i = 0;
  1207. $success = 0;
  1208. $this->multifeed_objects = array();
  1209. $this->error = array();
  1210. foreach ($this->multifeed_url as $url)
  1211. {
  1212. $this->multifeed_objects[$i] = clone $this;
  1213. $this->multifeed_objects[$i]->set_feed_url($url);
  1214. $single_success = $this->multifeed_objects[$i]->init();
  1215. $success |= $single_success;
  1216. if (!$single_success)
  1217. {
  1218. $this->error[$i] = $this->multifeed_objects[$i]->error();
  1219. }
  1220. $i++;
  1221. }
  1222. return (bool) $success;
  1223. }
  1224. elseif ($this->feed_url === null && $this->raw_data === null)
  1225. {
  1226. return false;
  1227. }
  1228. $this->error = null;
  1229. $this->data = array();
  1230. $this->check_modified = false;
  1231. $this->multifeed_objects = array();
  1232. $cache = false;
  1233. if ($this->feed_url !== null)
  1234. {
  1235. $parsed_feed_url = $this->registry->call('Misc', 'parse_url', array($this->feed_url));
  1236. // Decide whether to enable caching
  1237. if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1238. {
  1239. $url = $this->feed_url . ($this->force_feed ? '#force_feed' : '');
  1240. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $url), 'spc'));
  1241. }
  1242. // Fetch the data via SimplePie_File into $this->raw_data
  1243. if (($fetched = $this->fetch_data($cache)) === true)
  1244. {
  1245. return true;
  1246. }
  1247. elseif ($fetched === false) {
  1248. return false;
  1249. }
  1250. list($headers, $sniffed) = $fetched;
  1251. }
  1252. // Empty response check
  1253. if(empty($this->raw_data)){
  1254. $this->error = "A feed could not be found at `$this->feed_url`. Empty body.";
  1255. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1256. return false;
  1257. }
  1258. // Set up array of possible encodings
  1259. $encodings = array();
  1260. // First check to see if input has been overridden.
  1261. if ($this->input_encoding !== false)
  1262. {
  1263. $encodings[] = strtoupper($this->input_encoding);
  1264. }
  1265. $application_types = array('application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity');
  1266. $text_types = array('text/xml', 'text/xml-external-parsed-entity');
  1267. // RFC 3023 (only applies to sniffed content)
  1268. if (isset($sniffed))
  1269. {
  1270. if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml')
  1271. {
  1272. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1273. {
  1274. $encodings[] = strtoupper($charset[1]);
  1275. }
  1276. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1277. $encodings[] = 'UTF-8';
  1278. }
  1279. elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml')
  1280. {
  1281. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1282. {
  1283. $encodings[] = strtoupper($charset[1]);
  1284. }
  1285. $encodings[] = 'US-ASCII';
  1286. }
  1287. // Text MIME-type default
  1288. elseif (substr($sniffed, 0, 5) === 'text/')
  1289. {
  1290. $encodings[] = 'UTF-8';
  1291. }
  1292. }
  1293. // Fallback to XML 1.0 Appendix F.1/UTF-8/ISO-8859-1
  1294. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1295. $encodings[] = 'UTF-8';
  1296. $encodings[] = 'ISO-8859-1';
  1297. // There's no point in trying an encoding twice
  1298. $encodings = array_unique($encodings);
  1299. // Loop through each possible encoding, till we return something, or run out of possibilities
  1300. foreach ($encodings as $encoding)
  1301. {
  1302. // Change the encoding to UTF-8 (as we always use UTF-8 internally)
  1303. if ($utf8_data = $this->registry->call('Misc', 'change_encoding', array($this->raw_data, $encoding, 'UTF-8')))
  1304. {
  1305. // Create new parser
  1306. $parser = $this->registry->create('Parser');
  1307. // If it's parsed fine
  1308. if ($parser->parse($utf8_data, 'UTF-8', $this->permanent_url))
  1309. {
  1310. $this->data = $parser->get_data();
  1311. if (!($this->get_type() & ~SIMPLEPIE_TYPE_NONE))
  1312. {
  1313. $this->error = "A feed could not be found at `$this->feed_url`. This does not appear to be a valid RSS or Atom feed.";
  1314. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1315. return false;
  1316. }
  1317. if (isset($headers))
  1318. {
  1319. $this->data['headers'] = $headers;
  1320. }
  1321. $this->data['build'] = SIMPLEPIE_BUILD;
  1322. // Cache the file if caching is enabled
  1323. if ($cache && !$cache->save($this))
  1324. {
  1325. trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1326. }
  1327. return true;
  1328. }
  1329. }
  1330. }
  1331. if (isset($parser))
  1332. {
  1333. // We have an error, just set SimplePie_Misc::error to it and quit
  1334. $this->error = $this->feed_url;
  1335. $this->error .= sprintf(' is invalid XML, likely due to invalid characters. XML error: %s at line %d, column %d', $parser->get_error_string(), $parser->get_current_line(), $parser->get_current_column());
  1336. }
  1337. else
  1338. {
  1339. $this->error = 'The data could not be converted to UTF-8.';
  1340. if (!extension_loaded('mbstring') && !extension_loaded('iconv') && !class_exists('\UConverter')) {
  1341. $this->error .= ' You MUST have either the iconv, mbstring or intl (PHP 5.5+) extension installed and enabled.';
  1342. } else {
  1343. $missingExtensions = array();
  1344. if (!extension_loaded('iconv')) {
  1345. $missingExtensions[] = 'iconv';
  1346. }
  1347. if (!extension_loaded('mbstring')) {
  1348. $missingExtensions[] = 'mbstring';
  1349. }
  1350. if (!class_exists('\UConverter')) {
  1351. $missingExtensions[] = 'intl (PHP 5.5+)';
  1352. }
  1353. $this->error .= ' Try installing/enabling the ' . implode(' or ', $missingExtensions) . ' extension.';
  1354. }
  1355. }
  1356. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1357. return false;
  1358. }
  1359. /**
  1360. * Fetch the data via SimplePie_File
  1361. *
  1362. * If the data is already cached, attempt to fetch it from there instead
  1363. * @param SimplePie_Cache|false $cache Cache handler, or false to not load from the cache
  1364. * @return array|true Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type
  1365. */
  1366. protected function fetch_data(&$cache)
  1367. {
  1368. // If it's enabled, use the cache
  1369. if ($cache)
  1370. {
  1371. // Load the Cache
  1372. $this->data = $cache->load();
  1373. if (!empty($this->data))
  1374. {
  1375. // If the cache is for an outdated build of SimplePie
  1376. if (!isset($this->data['build']) || $this->data['build'] !== SIMPLEPIE_BUILD)
  1377. {
  1378. $cache->unlink();
  1379. $this->data = array();
  1380. }
  1381. // If we've hit a collision just rerun it with caching disabled
  1382. elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url)
  1383. {
  1384. $cache = false;
  1385. $this->data = array();
  1386. }
  1387. // If we've got a non feed_url stored (if the page isn't actually a feed, or is a redirect) use that URL.
  1388. elseif (isset($this->data['feed_url']))
  1389. {
  1390. // If the autodiscovery cache is still valid use it.
  1391. if ($cache->mtime() + $this->autodiscovery_cache_duration > time())
  1392. {
  1393. // Do not need to do feed autodiscovery yet.
  1394. if ($this->data['feed_url'] !== $this->data['url'])
  1395. {
  1396. $this->set_feed_url($this->data['feed_url']);
  1397. return $this->init();
  1398. }
  1399. $cache->unlink();
  1400. $this->data = array();
  1401. }
  1402. }
  1403. // Check if the cache has been updated
  1404. elseif ($cache->mtime() + $this->cache_duration < time())
  1405. {
  1406. // Want to know if we tried to send last-modified and/or etag headers
  1407. // when requesting this file. (Note that it's up to the file to
  1408. // support this, but we don't always send the headers either.)
  1409. $this->check_modified = true;
  1410. if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))
  1411. {
  1412. $headers = array(
  1413. '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',
  1414. );
  1415. if (isset($this->data['headers']['last-modified']))
  1416. {
  1417. $headers['if-modified-since'] = $this->data['headers']['last-modified'];
  1418. }
  1419. if (isset($this->data['headers']['etag']))
  1420. {
  1421. $headers['if-none-match'] = $this->data['headers']['etag'];
  1422. }
  1423. $file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1424. if ($file->success)
  1425. {
  1426. if ($file->status_code === 304)
  1427. {
  1428. // Set raw_data to false here too, to signify that the cache
  1429. // is still valid.
  1430. $this->raw_data = false;
  1431. $cache->touch();
  1432. return true;
  1433. }
  1434. }
  1435. else
  1436. {
  1437. $this->check_modified = false;
  1438. if($this->force_cache_fallback)
  1439. {
  1440. $cache->touch();
  1441. return true;
  1442. }
  1443. unset($file);
  1444. }
  1445. }
  1446. }
  1447. // If the cache is still valid, just return true
  1448. else
  1449. {
  1450. $this->raw_data = false;
  1451. return true;
  1452. }
  1453. }
  1454. // If the cache is empty, delete it
  1455. else
  1456. {
  1457. $cache->unlink();
  1458. $this->data = array();
  1459. }
  1460. }
  1461. // If we don't already have the file (it'll only exist if we've opened it to check if the cache has been modified), open it.
  1462. if (!isset($file))
  1463. {
  1464. if ($this->file instanceof SimplePie_File && $this->file->url === $this->feed_url)
  1465. {
  1466. $file =& $this->file;
  1467. }
  1468. else
  1469. {
  1470. $headers = array(
  1471. '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',
  1472. );
  1473. $file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1474. }
  1475. }
  1476. // If the file connection has an error, set SimplePie::error to that and quit
  1477. if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1478. {
  1479. $this->error = $file->error;
  1480. return !empty($this->data);
  1481. }
  1482. if (!$this->force_feed)
  1483. {
  1484. // Check if the supplied URL is a feed, if it isn't, look for it.
  1485. $locate = $this->registry->create('Locator', array(&$file, $this->timeout, $this->useragent, $this->max_checked_feeds, $this->force_fsockopen, $this->curl_options));
  1486. if (!$locate->is_feed($file))
  1487. {
  1488. $copyStatusCode = $file->status_code;
  1489. $copyContentType = $file->headers['content-type'];
  1490. try
  1491. {
  1492. $microformats = false;
  1493. if (class_exists('DOMXpath') && function_exists('Mf2\parse')) {
  1494. $doc = new DOMDocument();
  1495. @$doc->loadHTML($file->body);
  1496. $xpath = new DOMXpath($doc);
  1497. // Check for both h-feed and h-entry, as both a feed with no entries
  1498. // and a list of entries without an h-feed wrapper are both valid.
  1499. $query = '//*[contains(concat(" ", @class, " "), " h-feed ") or '.
  1500. 'contains(concat(" ", @class, " "), " h-entry ")]';
  1501. $result = $xpath->query($query);
  1502. $microformats = $result->length !== 0;
  1503. }
  1504. // Now also do feed discovery, but if microformats were found don't
  1505. // overwrite the current value of file.
  1506. $discovered = $locate->find($this->autodiscovery,
  1507. $this->all_discovered_feeds);
  1508. if ($microformats)
  1509. {
  1510. if ($hub = $locate->get_rel_link('hub'))
  1511. {
  1512. $self = $locate->get_rel_link('self');
  1513. $this->store_links($file, $hub, $self);
  1514. }
  1515. // Push the current file onto all_discovered feeds so the user can
  1516. // be shown this as one of the options.
  1517. if (isset($this->all_discovered_feeds)) {
  1518. $this->all_discovered_feeds[] = $file;
  1519. }
  1520. }
  1521. else
  1522. {
  1523. if ($discovered)
  1524. {
  1525. $file = $discovered;
  1526. }
  1527. else
  1528. {
  1529. // We need to unset this so that if SimplePie::set_file() has
  1530. // been called that object is untouched
  1531. unset($file);
  1532. $this->error = "A feed could not be found at `$this->feed_url`; the status code is `$copyStatusCode` and content-type is `$copyContentType`";
  1533. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1534. return false;
  1535. }
  1536. }
  1537. }
  1538. catch (SimplePie_Exception $e)
  1539. {
  1540. // We need to unset this so that if SimplePie::set_file() has been called that object is untouched
  1541. unset($file);
  1542. // This is usually because DOMDocument doesn't exist
  1543. $this->error = $e->getMessage();
  1544. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, $e->getFile(), $e->getLine()));
  1545. return false;
  1546. }
  1547. if ($cache)
  1548. {
  1549. $this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
  1550. if (!$cache->save($this))
  1551. {
  1552. trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1553. }
  1554. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $file->url), 'spc'));
  1555. }
  1556. }
  1557. $this->feed_url = $file->url;
  1558. $locate = null;
  1559. }
  1560. $this->raw_data = $file->body;
  1561. $this->permanent_url = $file->permanent_url;
  1562. $headers = $file->headers;
  1563. $sniffer = $this->registry->create('Content_Type_Sniffer', array(&$file));
  1564. $sniffed = $sniffer->get_type();
  1565. return array($headers, $sniffed);
  1566. }
  1567. /**
  1568. * Get the error message for the occured error
  1569. *
  1570. * @return string|array Error message, or array of messages for multifeeds
  1571. */
  1572. public function error()
  1573. {
  1574. return $this->error;
  1575. }
  1576. /**
  1577. * Get the raw XML
  1578. *
  1579. * This is the same as the old `$feed->enable_xml_dump(true)`, but returns
  1580. * the data instead of printing it.
  1581. *
  1582. * @return string|boolean Raw XML data, false if the cache is used
  1583. */
  1584. public function get_raw_data()
  1585. {
  1586. return $this->raw_data;
  1587. }
  1588. /**
  1589. * Get the character encoding used for output
  1590. *
  1591. * @since Preview Release
  1592. * @return string
  1593. */
  1594. public function get_encoding()
  1595. {
  1596. return $this->sanitize->output_encoding;
  1597. }
  1598. /**
  1599. * Send the content-type header with correct encoding
  1600. *
  1601. * This method ensures that the SimplePie-enabled page is being served with
  1602. * the correct {@link http://www.iana.org/assignments/media-types/ mime-type}
  1603. * and character encoding HTTP headers (character encoding determined by the
  1604. * {@see set_output_encoding} config option).
  1605. *
  1606. * This won't work properly if any content or whitespace has already been
  1607. * sent to the browser, because it relies on PHP's
  1608. * {@link http://php.net/header header()} function, and these are the
  1609. * circumstances under which the function works.
  1610. *
  1611. * Because it's setting these settings for the entire page (as is the nature
  1612. * of HTTP headers), this should only be us…

Large files files are truncated, but you can click here to view the full file