PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/wp-includes/class-simplepie.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 1796 lines | 845 code | 186 blank | 765 comment | 94 complexity | 429996d1d963cb7df9ef5864a3240d9a MD5 | raw 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-2012, Ryan Parman, Geoffrey 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.3.1
  67. * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  68. * @author Ryan Parman
  69. * @author Geoffrey 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.3.1');
  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 object Instance of SimplePie_File to use as a feed
  402. * @see SimplePie::set_file()
  403. * @access private
  404. */
  405. public $file;
  406. /**
  407. * @var string Raw feed data
  408. * @see SimplePie::set_raw_data()
  409. * @access private
  410. */
  411. public $raw_data;
  412. /**
  413. * @var int Timeout for fetching remote files
  414. * @see SimplePie::set_timeout()
  415. * @access private
  416. */
  417. public $timeout = 10;
  418. /**
  419. * @var bool Forces fsockopen() to be used for remote files instead
  420. * of cURL, even if a new enough version is installed
  421. * @see SimplePie::force_fsockopen()
  422. * @access private
  423. */
  424. public $force_fsockopen = false;
  425. /**
  426. * @var bool Force the given data/URL to be treated as a feed no matter what
  427. * it appears like
  428. * @see SimplePie::force_feed()
  429. * @access private
  430. */
  431. public $force_feed = false;
  432. /**
  433. * @var bool Enable/Disable Caching
  434. * @see SimplePie::enable_cache()
  435. * @access private
  436. */
  437. public $cache = true;
  438. /**
  439. * @var int Cache duration (in seconds)
  440. * @see SimplePie::set_cache_duration()
  441. * @access private
  442. */
  443. public $cache_duration = 3600;
  444. /**
  445. * @var int Auto-discovery cache duration (in seconds)
  446. * @see SimplePie::set_autodiscovery_cache_duration()
  447. * @access private
  448. */
  449. public $autodiscovery_cache_duration = 604800; // 7 Days.
  450. /**
  451. * @var string Cache location (relative to executing script)
  452. * @see SimplePie::set_cache_location()
  453. * @access private
  454. */
  455. public $cache_location = './cache';
  456. /**
  457. * @var string Function that creates the cache filename
  458. * @see SimplePie::set_cache_name_function()
  459. * @access private
  460. */
  461. public $cache_name_function = 'md5';
  462. /**
  463. * @var bool Reorder feed by date descending
  464. * @see SimplePie::enable_order_by_date()
  465. * @access private
  466. */
  467. public $order_by_date = true;
  468. /**
  469. * @var mixed Force input encoding to be set to the follow value
  470. * (false, or anything type-cast to false, disables this feature)
  471. * @see SimplePie::set_input_encoding()
  472. * @access private
  473. */
  474. public $input_encoding = false;
  475. /**
  476. * @var int Feed Autodiscovery Level
  477. * @see SimplePie::set_autodiscovery_level()
  478. * @access private
  479. */
  480. public $autodiscovery = SIMPLEPIE_LOCATOR_ALL;
  481. /**
  482. * Class registry object
  483. *
  484. * @var SimplePie_Registry
  485. */
  486. public $registry;
  487. /**
  488. * @var int Maximum number of feeds to check with autodiscovery
  489. * @see SimplePie::set_max_checked_feeds()
  490. * @access private
  491. */
  492. public $max_checked_feeds = 10;
  493. /**
  494. * @var array All the feeds found during the autodiscovery process
  495. * @see SimplePie::get_all_discovered_feeds()
  496. * @access private
  497. */
  498. public $all_discovered_feeds = array();
  499. /**
  500. * @var string Web-accessible path to the handler_image.php file.
  501. * @see SimplePie::set_image_handler()
  502. * @access private
  503. */
  504. public $image_handler = '';
  505. /**
  506. * @var array Stores the URLs when multiple feeds are being initialized.
  507. * @see SimplePie::set_feed_url()
  508. * @access private
  509. */
  510. public $multifeed_url = array();
  511. /**
  512. * @var array Stores SimplePie objects when multiple feeds initialized.
  513. * @access private
  514. */
  515. public $multifeed_objects = array();
  516. /**
  517. * @var array Stores the get_object_vars() array for use with multifeeds.
  518. * @see SimplePie::set_feed_url()
  519. * @access private
  520. */
  521. public $config_settings = null;
  522. /**
  523. * @var integer Stores the number of items to return per-feed with multifeeds.
  524. * @see SimplePie::set_item_limit()
  525. * @access private
  526. */
  527. public $item_limit = 0;
  528. /**
  529. * @var array Stores the default attributes to be stripped by strip_attributes().
  530. * @see SimplePie::strip_attributes()
  531. * @access private
  532. */
  533. public $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc');
  534. /**
  535. * @var array Stores the default tags to be stripped by strip_htmltags().
  536. * @see SimplePie::strip_htmltags()
  537. * @access private
  538. */
  539. public $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
  540. /**
  541. * The SimplePie class contains feed level data and options
  542. *
  543. * To use SimplePie, create the SimplePie object with no parameters. You can
  544. * then set configuration options using the provided methods. After setting
  545. * them, you must initialise the feed using $feed->init(). At that point the
  546. * object's methods and properties will be available to you.
  547. *
  548. * Previously, it was possible to pass in the feed URL along with cache
  549. * options directly into the constructor. This has been removed as of 1.3 as
  550. * it caused a lot of confusion.
  551. *
  552. * @since 1.0 Preview Release
  553. */
  554. public function __construct()
  555. {
  556. if (version_compare(PHP_VERSION, '5.2', '<'))
  557. {
  558. trigger_error('PHP 4.x, 5.0 and 5.1 are no longer supported. Please upgrade to PHP 5.2 or newer.');
  559. die();
  560. }
  561. // Other objects, instances created here so we can set options on them
  562. $this->sanitize = new SimplePie_Sanitize();
  563. $this->registry = new SimplePie_Registry();
  564. if (func_num_args() > 0)
  565. {
  566. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  567. trigger_error('Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_location() directly.', $level);
  568. $args = func_get_args();
  569. switch (count($args)) {
  570. case 3:
  571. $this->set_cache_duration($args[2]);
  572. case 2:
  573. $this->set_cache_location($args[1]);
  574. case 1:
  575. $this->set_feed_url($args[0]);
  576. $this->init();
  577. }
  578. }
  579. }
  580. /**
  581. * Used for converting object to a string
  582. */
  583. public function __toString()
  584. {
  585. return md5(serialize($this->data));
  586. }
  587. /**
  588. * Remove items that link back to this before destroying this object
  589. */
  590. public function __destruct()
  591. {
  592. if ((version_compare(PHP_VERSION, '5.3', '<') || !gc_enabled()) && !ini_get('zend.ze1_compatibility_mode'))
  593. {
  594. if (!empty($this->data['items']))
  595. {
  596. foreach ($this->data['items'] as $item)
  597. {
  598. $item->__destruct();
  599. }
  600. unset($item, $this->data['items']);
  601. }
  602. if (!empty($this->data['ordered_items']))
  603. {
  604. foreach ($this->data['ordered_items'] as $item)
  605. {
  606. $item->__destruct();
  607. }
  608. unset($item, $this->data['ordered_items']);
  609. }
  610. }
  611. }
  612. /**
  613. * Force the given data/URL to be treated as a feed
  614. *
  615. * This tells SimplePie to ignore the content-type provided by the server.
  616. * Be careful when using this option, as it will also disable autodiscovery.
  617. *
  618. * @since 1.1
  619. * @param bool $enable Force the given data/URL to be treated as a feed
  620. */
  621. public function force_feed($enable = false)
  622. {
  623. $this->force_feed = (bool) $enable;
  624. }
  625. /**
  626. * Set the URL of the feed you want to parse
  627. *
  628. * This allows you to enter the URL of the feed you want to parse, or the
  629. * website you want to try to use auto-discovery on. This takes priority
  630. * over any set raw data.
  631. *
  632. * You can set multiple feeds to mash together by passing an array instead
  633. * of a string for the $url. Remember that with each additional feed comes
  634. * additional processing and resources.
  635. *
  636. * @since 1.0 Preview Release
  637. * @see set_raw_data()
  638. * @param string|array $url This is the URL (or array of URLs) that you want to parse.
  639. */
  640. public function set_feed_url($url)
  641. {
  642. $this->multifeed_url = array();
  643. if (is_array($url))
  644. {
  645. foreach ($url as $value)
  646. {
  647. $this->multifeed_url[] = $this->registry->call('Misc', 'fix_protocol', array($value, 1));
  648. }
  649. }
  650. else
  651. {
  652. $this->feed_url = $this->registry->call('Misc', 'fix_protocol', array($url, 1));
  653. }
  654. }
  655. /**
  656. * Set an instance of {@see SimplePie_File} to use as a feed
  657. *
  658. * @param SimplePie_File &$file
  659. * @return bool True on success, false on failure
  660. */
  661. public function set_file(&$file)
  662. {
  663. if ($file instanceof SimplePie_File)
  664. {
  665. $this->feed_url = $file->url;
  666. $this->file =& $file;
  667. return true;
  668. }
  669. return false;
  670. }
  671. /**
  672. * Set the raw XML data to parse
  673. *
  674. * Allows you to use a string of RSS/Atom data instead of a remote feed.
  675. *
  676. * If you have a feed available as a string in PHP, you can tell SimplePie
  677. * to parse that data string instead of a remote feed. Any set feed URL
  678. * takes precedence.
  679. *
  680. * @since 1.0 Beta 3
  681. * @param string $data RSS or Atom data as a string.
  682. * @see set_feed_url()
  683. */
  684. public function set_raw_data($data)
  685. {
  686. $this->raw_data = $data;
  687. }
  688. /**
  689. * Set the the default timeout for fetching remote feeds
  690. *
  691. * This allows you to change the maximum time the feed's server to respond
  692. * and send the feed back.
  693. *
  694. * @since 1.0 Beta 3
  695. * @param int $timeout The maximum number of seconds to spend waiting to retrieve a feed.
  696. */
  697. public function set_timeout($timeout = 10)
  698. {
  699. $this->timeout = (int) $timeout;
  700. }
  701. /**
  702. * Force SimplePie to use fsockopen() instead of cURL
  703. *
  704. * @since 1.0 Beta 3
  705. * @param bool $enable Force fsockopen() to be used
  706. */
  707. public function force_fsockopen($enable = false)
  708. {
  709. $this->force_fsockopen = (bool) $enable;
  710. }
  711. /**
  712. * Enable/disable caching in SimplePie.
  713. *
  714. * This option allows you to disable caching all-together in SimplePie.
  715. * However, disabling the cache can lead to longer load times.
  716. *
  717. * @since 1.0 Preview Release
  718. * @param bool $enable Enable caching
  719. */
  720. public function enable_cache($enable = true)
  721. {
  722. $this->cache = (bool) $enable;
  723. }
  724. /**
  725. * Set the length of time (in seconds) that the contents of a feed will be
  726. * cached
  727. *
  728. * @param int $seconds The feed content cache duration
  729. */
  730. public function set_cache_duration($seconds = 3600)
  731. {
  732. $this->cache_duration = (int) $seconds;
  733. }
  734. /**
  735. * Set the length of time (in seconds) that the autodiscovered feed URL will
  736. * be cached
  737. *
  738. * @param int $seconds The autodiscovered feed URL cache duration.
  739. */
  740. public function set_autodiscovery_cache_duration($seconds = 604800)
  741. {
  742. $this->autodiscovery_cache_duration = (int) $seconds;
  743. }
  744. /**
  745. * Set the file system location where the cached files should be stored
  746. *
  747. * @param string $location The file system location.
  748. */
  749. public function set_cache_location($location = './cache')
  750. {
  751. $this->cache_location = (string) $location;
  752. }
  753. /**
  754. * Set whether feed items should be sorted into reverse chronological order
  755. *
  756. * @param bool $enable Sort as reverse chronological order.
  757. */
  758. public function enable_order_by_date($enable = true)
  759. {
  760. $this->order_by_date = (bool) $enable;
  761. }
  762. /**
  763. * Set the character encoding used to parse the feed
  764. *
  765. * This overrides the encoding reported by the feed, however it will fall
  766. * back to the normal encoding detection if the override fails
  767. *
  768. * @param string $encoding Character encoding
  769. */
  770. public function set_input_encoding($encoding = false)
  771. {
  772. if ($encoding)
  773. {
  774. $this->input_encoding = (string) $encoding;
  775. }
  776. else
  777. {
  778. $this->input_encoding = false;
  779. }
  780. }
  781. /**
  782. * Set how much feed autodiscovery to do
  783. *
  784. * @see SIMPLEPIE_LOCATOR_NONE
  785. * @see SIMPLEPIE_LOCATOR_AUTODISCOVERY
  786. * @see SIMPLEPIE_LOCATOR_LOCAL_EXTENSION
  787. * @see SIMPLEPIE_LOCATOR_LOCAL_BODY
  788. * @see SIMPLEPIE_LOCATOR_REMOTE_EXTENSION
  789. * @see SIMPLEPIE_LOCATOR_REMOTE_BODY
  790. * @see SIMPLEPIE_LOCATOR_ALL
  791. * @param int $level Feed Autodiscovery Level (level can be a combination of the above constants, see bitwise OR operator)
  792. */
  793. public function set_autodiscovery_level($level = SIMPLEPIE_LOCATOR_ALL)
  794. {
  795. $this->autodiscovery = (int) $level;
  796. }
  797. /**
  798. * Get the class registry
  799. *
  800. * Use this to override SimplePie's default classes
  801. * @see SimplePie_Registry
  802. * @return SimplePie_Registry
  803. */
  804. public function &get_registry()
  805. {
  806. return $this->registry;
  807. }
  808. /**#@+
  809. * Useful when you are overloading or extending SimplePie's default classes.
  810. *
  811. * @deprecated Use {@see get_registry()} instead
  812. * @link http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.extends PHP5 extends documentation
  813. * @param string $class Name of custom class
  814. * @return boolean True on success, false otherwise
  815. */
  816. /**
  817. * Set which class SimplePie uses for caching
  818. */
  819. public function set_cache_class($class = 'SimplePie_Cache')
  820. {
  821. return $this->registry->register('Cache', $class, true);
  822. }
  823. /**
  824. * Set which class SimplePie uses for auto-discovery
  825. */
  826. public function set_locator_class($class = 'SimplePie_Locator')
  827. {
  828. return $this->registry->register('Locator', $class, true);
  829. }
  830. /**
  831. * Set which class SimplePie uses for XML parsing
  832. */
  833. public function set_parser_class($class = 'SimplePie_Parser')
  834. {
  835. return $this->registry->register('Parser', $class, true);
  836. }
  837. /**
  838. * Set which class SimplePie uses for remote file fetching
  839. */
  840. public function set_file_class($class = 'SimplePie_File')
  841. {
  842. return $this->registry->register('File', $class, true);
  843. }
  844. /**
  845. * Set which class SimplePie uses for data sanitization
  846. */
  847. public function set_sanitize_class($class = 'SimplePie_Sanitize')
  848. {
  849. return $this->registry->register('Sanitize', $class, true);
  850. }
  851. /**
  852. * Set which class SimplePie uses for handling feed items
  853. */
  854. public function set_item_class($class = 'SimplePie_Item')
  855. {
  856. return $this->registry->register('Item', $class, true);
  857. }
  858. /**
  859. * Set which class SimplePie uses for handling author data
  860. */
  861. public function set_author_class($class = 'SimplePie_Author')
  862. {
  863. return $this->registry->register('Author', $class, true);
  864. }
  865. /**
  866. * Set which class SimplePie uses for handling category data
  867. */
  868. public function set_category_class($class = 'SimplePie_Category')
  869. {
  870. return $this->registry->register('Category', $class, true);
  871. }
  872. /**
  873. * Set which class SimplePie uses for feed enclosures
  874. */
  875. public function set_enclosure_class($class = 'SimplePie_Enclosure')
  876. {
  877. return $this->registry->register('Enclosure', $class, true);
  878. }
  879. /**
  880. * Set which class SimplePie uses for `<media:text>` captions
  881. */
  882. public function set_caption_class($class = 'SimplePie_Caption')
  883. {
  884. return $this->registry->register('Caption', $class, true);
  885. }
  886. /**
  887. * Set which class SimplePie uses for `<media:copyright>`
  888. */
  889. public function set_copyright_class($class = 'SimplePie_Copyright')
  890. {
  891. return $this->registry->register('Copyright', $class, true);
  892. }
  893. /**
  894. * Set which class SimplePie uses for `<media:credit>`
  895. */
  896. public function set_credit_class($class = 'SimplePie_Credit')
  897. {
  898. return $this->registry->register('Credit', $class, true);
  899. }
  900. /**
  901. * Set which class SimplePie uses for `<media:rating>`
  902. */
  903. public function set_rating_class($class = 'SimplePie_Rating')
  904. {
  905. return $this->registry->register('Rating', $class, true);
  906. }
  907. /**
  908. * Set which class SimplePie uses for `<media:restriction>`
  909. */
  910. public function set_restriction_class($class = 'SimplePie_Restriction')
  911. {
  912. return $this->registry->register('Restriction', $class, true);
  913. }
  914. /**
  915. * Set which class SimplePie uses for content-type sniffing
  916. */
  917. public function set_content_type_sniffer_class($class = 'SimplePie_Content_Type_Sniffer')
  918. {
  919. return $this->registry->register('Content_Type_Sniffer', $class, true);
  920. }
  921. /**
  922. * Set which class SimplePie uses item sources
  923. */
  924. public function set_source_class($class = 'SimplePie_Source')
  925. {
  926. return $this->registry->register('Source', $class, true);
  927. }
  928. /**#@-*/
  929. /**
  930. * Set the user agent string
  931. *
  932. * @param string $ua New user agent string.
  933. */
  934. public function set_useragent($ua = SIMPLEPIE_USERAGENT)
  935. {
  936. $this->useragent = (string) $ua;
  937. }
  938. /**
  939. * Set callback function to create cache filename with
  940. *
  941. * @param mixed $function Callback function
  942. */
  943. public function set_cache_name_function($function = 'md5')
  944. {
  945. if (is_callable($function))
  946. {
  947. $this->cache_name_function = $function;
  948. }
  949. }
  950. /**
  951. * Set options to make SP as fast as possible
  952. *
  953. * Forgoes a substantial amount of data sanitization in favor of speed. This
  954. * turns SimplePie into a dumb parser of feeds.
  955. *
  956. * @param bool $set Whether to set them or not
  957. */
  958. public function set_stupidly_fast($set = false)
  959. {
  960. if ($set)
  961. {
  962. $this->enable_order_by_date(false);
  963. $this->remove_div(false);
  964. $this->strip_comments(false);
  965. $this->strip_htmltags(false);
  966. $this->strip_attributes(false);
  967. $this->set_image_handler(false);
  968. }
  969. }
  970. /**
  971. * Set maximum number of feeds to check with autodiscovery
  972. *
  973. * @param int $max Maximum number of feeds to check
  974. */
  975. public function set_max_checked_feeds($max = 10)
  976. {
  977. $this->max_checked_feeds = (int) $max;
  978. }
  979. public function remove_div($enable = true)
  980. {
  981. $this->sanitize->remove_div($enable);
  982. }
  983. public function strip_htmltags($tags = '', $encode = null)
  984. {
  985. if ($tags === '')
  986. {
  987. $tags = $this->strip_htmltags;
  988. }
  989. $this->sanitize->strip_htmltags($tags);
  990. if ($encode !== null)
  991. {
  992. $this->sanitize->encode_instead_of_strip($tags);
  993. }
  994. }
  995. public function encode_instead_of_strip($enable = true)
  996. {
  997. $this->sanitize->encode_instead_of_strip($enable);
  998. }
  999. public function strip_attributes($attribs = '')
  1000. {
  1001. if ($attribs === '')
  1002. {
  1003. $attribs = $this->strip_attributes;
  1004. }
  1005. $this->sanitize->strip_attributes($attribs);
  1006. }
  1007. /**
  1008. * Set the output encoding
  1009. *
  1010. * Allows you to override SimplePie's output to match that of your webpage.
  1011. * This is useful for times when your webpages are not being served as
  1012. * UTF-8. This setting will be obeyed by {@see handle_content_type()}, and
  1013. * is similar to {@see set_input_encoding()}.
  1014. *
  1015. * It should be noted, however, that not all character encodings can support
  1016. * all characters. If your page is being served as ISO-8859-1 and you try
  1017. * to display a Japanese feed, you'll likely see garbled characters.
  1018. * Because of this, it is highly recommended to ensure that your webpages
  1019. * are served as UTF-8.
  1020. *
  1021. * The number of supported character encodings depends on whether your web
  1022. * host supports {@link http://php.net/mbstring mbstring},
  1023. * {@link http://php.net/iconv iconv}, or both. See
  1024. * {@link http://simplepie.org/wiki/faq/Supported_Character_Encodings} for
  1025. * more information.
  1026. *
  1027. * @param string $encoding
  1028. */
  1029. public function set_output_encoding($encoding = 'UTF-8')
  1030. {
  1031. $this->sanitize->set_output_encoding($encoding);
  1032. }
  1033. public function strip_comments($strip = false)
  1034. {
  1035. $this->sanitize->strip_comments($strip);
  1036. }
  1037. /**
  1038. * Set element/attribute key/value pairs of HTML attributes
  1039. * containing URLs that need to be resolved relative to the feed
  1040. *
  1041. * Defaults to |a|@href, |area|@href, |blockquote|@cite, |del|@cite,
  1042. * |form|@action, |img|@longdesc, |img|@src, |input|@src, |ins|@cite,
  1043. * |q|@cite
  1044. *
  1045. * @since 1.0
  1046. * @param array|null $element_attribute Element/attribute key/value pairs, null for default
  1047. */
  1048. public function set_url_replacements($element_attribute = null)
  1049. {
  1050. $this->sanitize->set_url_replacements($element_attribute);
  1051. }
  1052. /**
  1053. * Set the handler to enable the display of cached images.
  1054. *
  1055. * @param str $page Web-accessible path to the handler_image.php file.
  1056. * @param str $qs The query string that the value should be passed to.
  1057. */
  1058. public function set_image_handler($page = false, $qs = 'i')
  1059. {
  1060. if ($page !== false)
  1061. {
  1062. $this->sanitize->set_image_handler($page . '?' . $qs . '=');
  1063. }
  1064. else
  1065. {
  1066. $this->image_handler = '';
  1067. }
  1068. }
  1069. /**
  1070. * Set the limit for items returned per-feed with multifeeds
  1071. *
  1072. * @param integer $limit The maximum number of items to return.
  1073. */
  1074. public function set_item_limit($limit = 0)
  1075. {
  1076. $this->item_limit = (int) $limit;
  1077. }
  1078. /**
  1079. * Initialize the feed object
  1080. *
  1081. * This is what makes everything happen. Period. This is where all of the
  1082. * configuration options get processed, feeds are fetched, cached, and
  1083. * parsed, and all of that other good stuff.
  1084. *
  1085. * @return boolean True if successful, false otherwise
  1086. */
  1087. public function init()
  1088. {
  1089. // Check absolute bare minimum requirements.
  1090. if (!extension_loaded('xml') || !extension_loaded('pcre'))
  1091. {
  1092. return false;
  1093. }
  1094. // 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.
  1095. elseif (!extension_loaded('xmlreader'))
  1096. {
  1097. static $xml_is_sane = null;
  1098. if ($xml_is_sane === null)
  1099. {
  1100. $parser_check = xml_parser_create();
  1101. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  1102. xml_parser_free($parser_check);
  1103. $xml_is_sane = isset($values[0]['value']);
  1104. }
  1105. if (!$xml_is_sane)
  1106. {
  1107. return false;
  1108. }
  1109. }
  1110. if (method_exists($this->sanitize, 'set_registry'))
  1111. {
  1112. $this->sanitize->set_registry($this->registry);
  1113. }
  1114. // Pass whatever was set with config options over to the sanitizer.
  1115. // Pass the classes in for legacy support; new classes should use the registry instead
  1116. $this->sanitize->pass_cache_data($this->cache, $this->cache_location, $this->cache_name_function, $this->registry->get_class('Cache'));
  1117. $this->sanitize->pass_file_data($this->registry->get_class('File'), $this->timeout, $this->useragent, $this->force_fsockopen);
  1118. if (!empty($this->multifeed_url))
  1119. {
  1120. $i = 0;
  1121. $success = 0;
  1122. $this->multifeed_objects = array();
  1123. $this->error = array();
  1124. foreach ($this->multifeed_url as $url)
  1125. {
  1126. $this->multifeed_objects[$i] = clone $this;
  1127. $this->multifeed_objects[$i]->set_feed_url($url);
  1128. $single_success = $this->multifeed_objects[$i]->init();
  1129. $success |= $single_success;
  1130. if (!$single_success)
  1131. {
  1132. $this->error[$i] = $this->multifeed_objects[$i]->error();
  1133. }
  1134. $i++;
  1135. }
  1136. return (bool) $success;
  1137. }
  1138. elseif ($this->feed_url === null && $this->raw_data === null)
  1139. {
  1140. return false;
  1141. }
  1142. $this->error = null;
  1143. $this->data = array();
  1144. $this->multifeed_objects = array();
  1145. $cache = false;
  1146. if ($this->feed_url !== null)
  1147. {
  1148. $parsed_feed_url = $this->registry->call('Misc', 'parse_url', array($this->feed_url));
  1149. // Decide whether to enable caching
  1150. if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1151. {
  1152. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $this->feed_url), 'spc'));
  1153. }
  1154. // Fetch the data via SimplePie_File into $this->raw_data
  1155. if (($fetched = $this->fetch_data($cache)) === true)
  1156. {
  1157. return true;
  1158. }
  1159. elseif ($fetched === false) {
  1160. return false;
  1161. }
  1162. list($headers, $sniffed) = $fetched;
  1163. }
  1164. // Set up array of possible encodings
  1165. $encodings = array();
  1166. // First check to see if input has been overridden.
  1167. if ($this->input_encoding !== false)
  1168. {
  1169. $encodings[] = $this->input_encoding;
  1170. }
  1171. $application_types = array('application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity');
  1172. $text_types = array('text/xml', 'text/xml-external-parsed-entity');
  1173. // RFC 3023 (only applies to sniffed content)
  1174. if (isset($sniffed))
  1175. {
  1176. if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml')
  1177. {
  1178. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1179. {
  1180. $encodings[] = strtoupper($charset[1]);
  1181. }
  1182. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1183. $encodings[] = 'UTF-8';
  1184. }
  1185. elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml')
  1186. {
  1187. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1188. {
  1189. $encodings[] = $charset[1];
  1190. }
  1191. $encodings[] = 'US-ASCII';
  1192. }
  1193. // Text MIME-type default
  1194. elseif (substr($sniffed, 0, 5) === 'text/')
  1195. {
  1196. $encodings[] = 'US-ASCII';
  1197. }
  1198. }
  1199. // Fallback to XML 1.0 Appendix F.1/UTF-8/ISO-8859-1
  1200. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1201. $encodings[] = 'UTF-8';
  1202. $encodings[] = 'ISO-8859-1';
  1203. // There's no point in trying an encoding twice
  1204. $encodings = array_unique($encodings);
  1205. // Loop through each possible encoding, till we return something, or run out of possibilities
  1206. foreach ($encodings as $encoding)
  1207. {
  1208. // Change the encoding to UTF-8 (as we always use UTF-8 internally)
  1209. if ($utf8_data = $this->registry->call('Misc', 'change_encoding', array($this->raw_data, $encoding, 'UTF-8')))
  1210. {
  1211. // Create new parser
  1212. $parser = $this->registry->create('Parser');
  1213. // If it's parsed fine
  1214. if ($parser->parse($utf8_data, 'UTF-8'))
  1215. {
  1216. $this->data = $parser->get_data();
  1217. if (!($this->get_type() & ~SIMPLEPIE_TYPE_NONE))
  1218. {
  1219. $this->error = "A feed could not be found at $this->feed_url. This does not appear to be a valid RSS or Atom feed.";
  1220. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1221. return false;
  1222. }
  1223. if (isset($headers))
  1224. {
  1225. $this->data['headers'] = $headers;
  1226. }
  1227. $this->data['build'] = SIMPLEPIE_BUILD;
  1228. // Cache the file if caching is enabled
  1229. if ($cache && !$cache->save($this))
  1230. {
  1231. trigger_error("$this->cache_location is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1232. }
  1233. return true;
  1234. }
  1235. }
  1236. }
  1237. if (isset($parser))
  1238. {
  1239. // We have an error, just set SimplePie_Misc::error to it and quit
  1240. $this->error = sprintf('This XML document is invalid, 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());
  1241. }
  1242. else
  1243. {
  1244. $this->error = 'The data could not be converted to UTF-8. You MUST have either the iconv or mbstring extension installed. Upgrading to PHP 5.x (which includes iconv) is highly recommended.';
  1245. }
  1246. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1247. return false;
  1248. }
  1249. /**
  1250. * Fetch the data via SimplePie_File
  1251. *
  1252. * If the data is already cached, attempt to fetch it from there instead
  1253. * @param SimplePie_Cache|false $cache Cache handler, or false to not load from the cache
  1254. * @return array|true Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type
  1255. */
  1256. protected function fetch_data(&$cache)
  1257. {
  1258. // If it's enabled, use the cache
  1259. if ($cache)
  1260. {
  1261. // Load the Cache
  1262. $this->data = $cache->load();
  1263. if (!empty($this->data))
  1264. {
  1265. // If the cache is for an outdated build of SimplePie
  1266. if (!isset($this->data['build']) || $this->data['build'] !== SIMPLEPIE_BUILD)
  1267. {
  1268. $cache->unlink();
  1269. $this->data = array();
  1270. }
  1271. // If we've hit a collision just rerun it with caching disabled
  1272. elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url)
  1273. {
  1274. $cache = false;
  1275. $this->data = array();
  1276. }
  1277. // If we've got a non feed_url stored (if the page isn't actually a feed, or is a redirect) use that URL.
  1278. elseif (isset($this->data['feed_url']))
  1279. {
  1280. // If the autodiscovery cache is still valid use it.
  1281. if ($cache->mtime() + $this->autodiscovery_cache_duration > time())
  1282. {
  1283. // Do not need to do feed autodiscovery yet.
  1284. if ($this->data['feed_url'] !== $this->data['url'])
  1285. {
  1286. $this->set_feed_url($this->data['feed_url']);
  1287. return $this->init();
  1288. }
  1289. $cache->unlink();
  1290. $this->data = array();
  1291. }
  1292. }
  1293. // Check if the cache has been updated
  1294. elseif ($cache->mtime() + $this->cache_duration < time())
  1295. {
  1296. // If we have last-modified and/or etag set
  1297. if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))
  1298. {
  1299. $headers = array(
  1300. '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',
  1301. );
  1302. if (isset($this->data['headers']['last-modified']))
  1303. {
  1304. $headers['if-modified-since'] = $this->data['headers']['last-modified'];
  1305. }
  1306. if (isset($this->data['headers']['etag']))
  1307. {
  1308. $headers['if-none-match'] = $this->data['headers']['etag'];
  1309. }
  1310. $file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen));
  1311. if ($file->success)
  1312. {
  1313. if ($file->status_code === 304)
  1314. {
  1315. $cache->touch();
  1316. return true;
  1317. }
  1318. }
  1319. else
  1320. {
  1321. unset($file);
  1322. }
  1323. }
  1324. }
  1325. // If the cache is still valid, just return true
  1326. else
  1327. {
  1328. $this->raw_data = false;
  1329. return true;
  1330. }
  1331. }
  1332. // If the cache is empty, delete it
  1333. else
  1334. {
  1335. $cache->unlink();
  1336. $this->data = array();
  1337. }
  1338. }
  1339. // 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.
  1340. if (!isset($file))
  1341. {
  1342. if ($this->file instanceof SimplePie_File && $this->file->url === $this->feed_url)
  1343. {
  1344. $file =& $this->file;
  1345. }
  1346. else
  1347. {
  1348. $headers = array(
  1349. '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',
  1350. );
  1351. $file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen));
  1352. }
  1353. }
  1354. // If the file connection has an error, set SimplePie::error to that and quit
  1355. if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1356. {
  1357. $this->error = $file->error;
  1358. return !empty($this->data);
  1359. }
  1360. if (!$this->force_feed)
  1361. {
  1362. // Check if the supplied URL is a feed, if it isn't, look for it.
  1363. $locate = $this->registry->create('Locator', array(&$file, $this->timeout, $this->useragent, $this->max_checked_feeds));
  1364. if (!$locate->is_feed($file))
  1365. {
  1366. // We need to unset this so that if SimplePie::set_file() has been called that object is untouched
  1367. unset($file);
  1368. try
  1369. {
  1370. if (!($file = $locate->find($this->autodiscovery, $this->all_discovered_feeds)))
  1371. {
  1372. $this->error = "A feed could not be found at $this->feed_url. A feed with an invalid mime type may fall victim to this error, or " . SIMPLEPIE_NAME . " was unable to auto-discover it.. Use force_feed() if you are certain this URL is a real feed.";
  1373. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1374. return false;
  1375. }
  1376. }
  1377. catch (SimplePie_Exception $e)
  1378. {
  1379. // This is usually because DOMDocument doesn't exist
  1380. $this->error = $e->getMessage();
  1381. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, $e->getFile(), $e->getLine()));
  1382. return false;
  1383. }
  1384. if ($cache)
  1385. {
  1386. $this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
  1387. if (!$cache->save($this))
  1388. {
  1389. trigger_error("$this->cache_location is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1390. }
  1391. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $file->url), 'spc'));
  1392. }
  1393. $this->feed_url = $file->url;
  1394. }
  1395. $locate = null;
  1396. }
  1397. $this->raw_data = $file->body;
  1398. $headers = $file->headers;
  1399. $sniffer = $this->registry->create('Content_Type_Sniffer', array(&$file));
  1400. $sniffed = $sniffer->get_type();
  1401. return array($headers, $sniffed);
  1402. }
  1403. /**
  1404. * Get the error message for the occurred error.
  1405. *
  1406. * @return string|array Error message, or array of messages for multifeeds
  1407. */
  1408. public function error()
  1409. {
  1410. return $this->error;
  1411. }
  1412. /**
  1413. * Get the raw XML
  1414. *
  1415. * This is the same as the old `$feed->enable_xml_dump(true)`, but returns
  1416. * the data instead of printing it.
  1417. *
  1418. * @return string|boolean Raw XML data, false if the cache is used
  1419. */
  1420. public function get_raw_data()
  1421. {
  1422. return $this->raw_data;
  1423. }
  1424. /**
  1425. * Get the character encoding used for output
  1426. *
  1427. * @since Preview Release
  1428. * @return string
  1429. */
  1430. public function get_encoding()
  1431. {
  1432. return $this->sanitize->output_encoding;
  1433. }
  1434. /**
  1435. * Send the content-type header with correct encoding
  1436. *
  1437. * This method ensures that the SimplePie-enabled page is being served with
  1438. * the correct {@link http://www.iana.org/assignments/media-types/ mime-type}
  1439. * and character encoding HTTP headers (character encoding determined by the
  1440. * {@see set_output_encoding} config option).
  1441. *
  1442. * This won't work properly if any content or whitespace has already been
  1443. * sent to the browser, because it relies on PHP's
  1444. * {@link http://php.net/header header()} function, and these are the
  1445. * circumstances under which the function works.
  1446. *
  1447. * Because it's setting these settings for the entire page (as is the nature
  1448. * of HTTP headers), this should only be used once per page (again, at the
  1449. * top).
  1450. *
  1451. * @param string $mime MIME type to serve the page as
  1452. */
  1453. public function handle_content_type($mime = 'text/html')
  1454. {
  1455. if (!headers_sent())
  1456. {
  1457. $header = "Content-type: $mime;";
  1458. if ($this->get_encoding())
  1459. {
  1460. $header .= ' charset=' . $this->get_encoding();
  1461. }
  1462. else
  1463. {
  1464. $header .= ' charset=UTF-8';
  1465. }
  1466. header($header);
  1467. }
  1468. }
  1469. /**
  1470. * Get the type of the feed
  1471. *
  1472. * This returns a SIMPLEPIE_TYPE_* constant, which can be tested against
  1473. * using {@link http://php.net/language.operators.bitwise bitwise operators}
  1474. *
  1475. * @since 0.8 (usage changed to using constants in 1.0)
  1476. * @see SIMPLEPIE_TYPE_NONE Unknown.
  1477. * @see SIMPLEPIE_TYPE_RSS_090 RSS 0.90.
  1478. * @see SIMPLEPIE_TYPE_RSS_091_NETSCAPE RSS 0.91 (Netscape).
  1479. * @see SIMPLEPIE_TYPE_RSS_091_USERLAND RSS 0.91 (Userland).
  1480. * @see SIMPLEPIE_TYPE_RSS_091 RSS 0.91.
  1481. * @see SIMPLEPIE_TYPE_RSS_092 RSS 0.92.
  1482. * @see SIMPLEPIE_TYPE_RSS_093 RSS 0.93.
  1483. * @see SIMPLEPIE_TYPE_RSS_094 RSS 0.94.
  1484. * @see SIMPLEPIE_TYPE_RSS_10 RSS 1.0.
  1485. * @see SIMPLEPIE_TYPE_RSS_20 RSS 2.0.x.
  1486. * @see SIMPLEPIE_TYPE_RSS_RDF RDF-based RSS.
  1487. * @see SIMPLEPIE_TYPE_RSS_SYNDICATION Non-RDF-based RSS (truly intended as syndication format).
  1488. * @see SIMPLEPIE_TYPE_RSS_ALL Any version of RSS.
  1489. * @see SIMPLEPIE_TYPE_ATOM_03 Atom 0.3.
  1490. * @see SIMPLEPIE_TYPE_ATOM_10 Atom 1.0.
  1491. * @see SIMPLEPIE_TYPE_ATOM_ALL Any version of Atom.
  1492. * @see SIMPLEPIE_TYPE_ALL Any known/supported feed type.
  1493. * @return int SIMPLEPIE_TYPE_* constant
  1494. */
  1495. public function get_type()
  1496. {
  1497. if (!isset($this->data['type']))
  1498. {
  1499. $this->data['type'] = SIMPLEPIE_TYPE_ALL;
  1500. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed']))
  1501. {
  1502. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_10;
  1503. }
  1504. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed']))
  1505. {
  1506. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_03;
  1507. }
  1508. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF']))
  1509. {
  1510. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['channel'])
  1511. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['image'])
  1512. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item'])
  1513. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['textinput']))
  1514. {
  1515. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_10;
  1516. }
  1517. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['channel'])
  1518. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['image'])
  1519. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item'])
  1520. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['textinput']))
  1521. {
  1522. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_090;
  1523. }
  1524. }
  1525. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss']))
  1526. {
  1527. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_ALL;
  1528. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1529. {
  1530. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1531. {
  1532. case '0.91':
  1533. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091;
  1534. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1535. {
  1536. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1537. {
  1538. case '0':
  1539. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_NETSCAPE;
  1540. break;
  1541. case '24':
  1542. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_USERLAND;
  1543. break;
  1544. }
  1545. }
  1546. break;
  1547. case '0.92':
  1548. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_092;
  1549. break;
  1550. case '0.93':
  1551. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_093;
  1552. break;
  1553. case '0.94':
  1554. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_094;
  1555. break;
  1556. case '2.0':
  1557. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_20;
  1558. break;
  1559. }
  1560. }
  1561. }
  1562. else
  1563. {
  1564. $this->data['type'] = SIMPLEPIE_TYPE_NONE;
  1565. }
  1566. }
  1567. return $this->data['type'];
  1568. }
  1569. /**
  1570. * Get the URL for the feed
  1571. *
  1572. * May or may not be different from the URL passed to {@see set_feed_url()},
  1573. * depending on whether auto-discovery was used.
  1574. *
  1575. * @since Preview Release (previously called `get_feed_url()` since SimplePie 0.8.)
  1576. * @todo If we have a perm redirect we should return the new URL
  1577. * @todo When we make the above change, let's support <itunes:new-feed-url> as well
  1578. * @todo Also, |atom:link|@rel=self
  1579. * @return string|null
  1580. */
  1581. public function subscribe_url()
  1582. {
  1583. if ($this->feed_url !== null)
  1584. {
  1585. return $this->sanitize($this->feed_url, SIMPLEPIE_CONSTRUCT_IRI);
  1586. }
  1587. else
  1588. {
  1589. return null;
  1590. }
  1591. }
  1592. /**
  1593. * Get data for an feed-level element
  1594. *
  1595. * This method allows you to get access to ANY element/attribute that is a
  1596. * sub-element of the opening feed tag.
  1597. *
  1598. * The return value is an indexed array of elements matching the given
  1599. * namespace and tag name. Each element h