PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-simplepie.php

https://bitbucket.org/opehei/wordpress-trunk
PHP | 3038 lines | 1728 code | 228 blank | 1082 comment | 210 complexity | 43cdf6264b3441475638c5b7dc615c2b MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0

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

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

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