PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/class-simplepie.php

http://github.com/markjaquith/WordPress
PHP | 3281 lines | 1827 code | 275 blank | 1179 comment | 228 complexity | bc1330cf64b696785e74f37669ddf011 MD5 | raw file
Possible License(s): 0BSD
  1. <?php
  2. if ( ! class_exists( 'SimplePie', false ) ) :
  3. // Load classes we will need.
  4. require ABSPATH . WPINC . '/SimplePie/Misc.php';
  5. require ABSPATH . WPINC . '/SimplePie/Cache.php';
  6. require ABSPATH . WPINC . '/SimplePie/File.php';
  7. require ABSPATH . WPINC . '/SimplePie/Sanitize.php';
  8. require ABSPATH . WPINC . '/SimplePie/Registry.php';
  9. require ABSPATH . WPINC . '/SimplePie/IRI.php';
  10. require ABSPATH . WPINC . '/SimplePie/Locator.php';
  11. require ABSPATH . WPINC . '/SimplePie/Content/Type/Sniffer.php';
  12. require ABSPATH . WPINC . '/SimplePie/XML/Declaration/Parser.php';
  13. require ABSPATH . WPINC . '/SimplePie/Parser.php';
  14. require ABSPATH . WPINC . '/SimplePie/Item.php';
  15. require ABSPATH . WPINC . '/SimplePie/Parse/Date.php';
  16. require ABSPATH . WPINC . '/SimplePie/Author.php';
  17. /**
  18. * WordPress autoloader for SimplePie.
  19. *
  20. * @since 3.5.0
  21. */
  22. function wp_simplepie_autoload( $class ) {
  23. if ( 0 !== strpos( $class, 'SimplePie_' ) )
  24. return;
  25. $file = ABSPATH . WPINC . '/' . str_replace( '_', '/', $class ) . '.php';
  26. include $file;
  27. }
  28. /**
  29. * We autoload classes we may not need.
  30. */
  31. spl_autoload_register( 'wp_simplepie_autoload' );
  32. /**
  33. * SimplePie
  34. *
  35. * A PHP-Based RSS and Atom Feed Framework.
  36. * Takes the hard work out of managing a complete RSS/Atom solution.
  37. *
  38. * Copyright (c) 2004-2017, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
  39. * All rights reserved.
  40. *
  41. * Redistribution and use in source and binary forms, with or without modification, are
  42. * permitted provided that the following conditions are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright notice, this list of
  45. * conditions and the following disclaimer.
  46. *
  47. * * Redistributions in binary form must reproduce the above copyright notice, this list
  48. * of conditions and the following disclaimer in the documentation and/or other materials
  49. * provided with the distribution.
  50. *
  51. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  52. * to endorse or promote products derived from this software without specific prior
  53. * written permission.
  54. *
  55. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  56. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  57. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  58. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  59. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  60. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  61. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  62. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  63. * POSSIBILITY OF SUCH DAMAGE.
  64. *
  65. * @package SimplePie
  66. * @version 1.5.5
  67. * @copyright 2004-2017 Ryan Parman, Sam Sneddon, Ryan McCue
  68. * @author Ryan Parman
  69. * @author Sam Sneddon
  70. * @author Ryan McCue
  71. * @link http://simplepie.org/ SimplePie
  72. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  73. */
  74. /**
  75. * SimplePie Name
  76. */
  77. define('SIMPLEPIE_NAME', 'SimplePie');
  78. /**
  79. * SimplePie Version
  80. */
  81. define('SIMPLEPIE_VERSION', '1.5.5');
  82. /**
  83. * SimplePie Build
  84. * @todo Hardcode for release (there's no need to have to call SimplePie_Misc::get_build() only every load of simplepie.inc)
  85. */
  86. define('SIMPLEPIE_BUILD', gmdate('YmdHis', SimplePie_Misc::get_build()));
  87. /**
  88. * SimplePie Website URL
  89. */
  90. define('SIMPLEPIE_URL', 'http://simplepie.org');
  91. /**
  92. * SimplePie Useragent
  93. * @see SimplePie::set_useragent()
  94. */
  95. define('SIMPLEPIE_USERAGENT', SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION . ' (Feed Parser; ' . SIMPLEPIE_URL . '; Allow like Gecko) Build/' . SIMPLEPIE_BUILD);
  96. /**
  97. * SimplePie Linkback
  98. */
  99. define('SIMPLEPIE_LINKBACK', '<a href="' . SIMPLEPIE_URL . '" title="' . SIMPLEPIE_NAME . ' ' . SIMPLEPIE_VERSION . '">' . SIMPLEPIE_NAME . '</a>');
  100. /**
  101. * No Autodiscovery
  102. * @see SimplePie::set_autodiscovery_level()
  103. */
  104. define('SIMPLEPIE_LOCATOR_NONE', 0);
  105. /**
  106. * Feed Link Element Autodiscovery
  107. * @see SimplePie::set_autodiscovery_level()
  108. */
  109. define('SIMPLEPIE_LOCATOR_AUTODISCOVERY', 1);
  110. /**
  111. * Local Feed Extension Autodiscovery
  112. * @see SimplePie::set_autodiscovery_level()
  113. */
  114. define('SIMPLEPIE_LOCATOR_LOCAL_EXTENSION', 2);
  115. /**
  116. * Local Feed Body Autodiscovery
  117. * @see SimplePie::set_autodiscovery_level()
  118. */
  119. define('SIMPLEPIE_LOCATOR_LOCAL_BODY', 4);
  120. /**
  121. * Remote Feed Extension Autodiscovery
  122. * @see SimplePie::set_autodiscovery_level()
  123. */
  124. define('SIMPLEPIE_LOCATOR_REMOTE_EXTENSION', 8);
  125. /**
  126. * Remote Feed Body Autodiscovery
  127. * @see SimplePie::set_autodiscovery_level()
  128. */
  129. define('SIMPLEPIE_LOCATOR_REMOTE_BODY', 16);
  130. /**
  131. * All Feed Autodiscovery
  132. * @see SimplePie::set_autodiscovery_level()
  133. */
  134. define('SIMPLEPIE_LOCATOR_ALL', 31);
  135. /**
  136. * No known feed type
  137. */
  138. define('SIMPLEPIE_TYPE_NONE', 0);
  139. /**
  140. * RSS 0.90
  141. */
  142. define('SIMPLEPIE_TYPE_RSS_090', 1);
  143. /**
  144. * RSS 0.91 (Netscape)
  145. */
  146. define('SIMPLEPIE_TYPE_RSS_091_NETSCAPE', 2);
  147. /**
  148. * RSS 0.91 (Userland)
  149. */
  150. define('SIMPLEPIE_TYPE_RSS_091_USERLAND', 4);
  151. /**
  152. * RSS 0.91 (both Netscape and Userland)
  153. */
  154. define('SIMPLEPIE_TYPE_RSS_091', 6);
  155. /**
  156. * RSS 0.92
  157. */
  158. define('SIMPLEPIE_TYPE_RSS_092', 8);
  159. /**
  160. * RSS 0.93
  161. */
  162. define('SIMPLEPIE_TYPE_RSS_093', 16);
  163. /**
  164. * RSS 0.94
  165. */
  166. define('SIMPLEPIE_TYPE_RSS_094', 32);
  167. /**
  168. * RSS 1.0
  169. */
  170. define('SIMPLEPIE_TYPE_RSS_10', 64);
  171. /**
  172. * RSS 2.0
  173. */
  174. define('SIMPLEPIE_TYPE_RSS_20', 128);
  175. /**
  176. * RDF-based RSS
  177. */
  178. define('SIMPLEPIE_TYPE_RSS_RDF', 65);
  179. /**
  180. * Non-RDF-based RSS (truly intended as syndication format)
  181. */
  182. define('SIMPLEPIE_TYPE_RSS_SYNDICATION', 190);
  183. /**
  184. * All RSS
  185. */
  186. define('SIMPLEPIE_TYPE_RSS_ALL', 255);
  187. /**
  188. * Atom 0.3
  189. */
  190. define('SIMPLEPIE_TYPE_ATOM_03', 256);
  191. /**
  192. * Atom 1.0
  193. */
  194. define('SIMPLEPIE_TYPE_ATOM_10', 512);
  195. /**
  196. * All Atom
  197. */
  198. define('SIMPLEPIE_TYPE_ATOM_ALL', 768);
  199. /**
  200. * All feed types
  201. */
  202. define('SIMPLEPIE_TYPE_ALL', 1023);
  203. /**
  204. * No construct
  205. */
  206. define('SIMPLEPIE_CONSTRUCT_NONE', 0);
  207. /**
  208. * Text construct
  209. */
  210. define('SIMPLEPIE_CONSTRUCT_TEXT', 1);
  211. /**
  212. * HTML construct
  213. */
  214. define('SIMPLEPIE_CONSTRUCT_HTML', 2);
  215. /**
  216. * XHTML construct
  217. */
  218. define('SIMPLEPIE_CONSTRUCT_XHTML', 4);
  219. /**
  220. * base64-encoded construct
  221. */
  222. define('SIMPLEPIE_CONSTRUCT_BASE64', 8);
  223. /**
  224. * IRI construct
  225. */
  226. define('SIMPLEPIE_CONSTRUCT_IRI', 16);
  227. /**
  228. * A construct that might be HTML
  229. */
  230. define('SIMPLEPIE_CONSTRUCT_MAYBE_HTML', 32);
  231. /**
  232. * All constructs
  233. */
  234. define('SIMPLEPIE_CONSTRUCT_ALL', 63);
  235. /**
  236. * Don't change case
  237. */
  238. define('SIMPLEPIE_SAME_CASE', 1);
  239. /**
  240. * Change to lowercase
  241. */
  242. define('SIMPLEPIE_LOWERCASE', 2);
  243. /**
  244. * Change to uppercase
  245. */
  246. define('SIMPLEPIE_UPPERCASE', 4);
  247. /**
  248. * PCRE for HTML attributes
  249. */
  250. define('SIMPLEPIE_PCRE_HTML_ATTRIBUTE', '((?:[\x09\x0A\x0B\x0C\x0D\x20]+[^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"(?:[^"]*)"|\'(?:[^\']*)\'|(?:[^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?)*)[\x09\x0A\x0B\x0C\x0D\x20]*');
  251. /**
  252. * PCRE for XML attributes
  253. */
  254. define('SIMPLEPIE_PCRE_XML_ATTRIBUTE', '((?:\s+(?:(?:[^\s:]+:)?[^\s:]+)\s*=\s*(?:"(?:[^"]*)"|\'(?:[^\']*)\'))*)\s*');
  255. /**
  256. * XML Namespace
  257. */
  258. define('SIMPLEPIE_NAMESPACE_XML', 'http://www.w3.org/XML/1998/namespace');
  259. /**
  260. * Atom 1.0 Namespace
  261. */
  262. define('SIMPLEPIE_NAMESPACE_ATOM_10', 'http://www.w3.org/2005/Atom');
  263. /**
  264. * Atom 0.3 Namespace
  265. */
  266. define('SIMPLEPIE_NAMESPACE_ATOM_03', 'http://purl.org/atom/ns#');
  267. /**
  268. * RDF Namespace
  269. */
  270. define('SIMPLEPIE_NAMESPACE_RDF', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  271. /**
  272. * RSS 0.90 Namespace
  273. */
  274. define('SIMPLEPIE_NAMESPACE_RSS_090', 'http://my.netscape.com/rdf/simple/0.9/');
  275. /**
  276. * RSS 1.0 Namespace
  277. */
  278. define('SIMPLEPIE_NAMESPACE_RSS_10', 'http://purl.org/rss/1.0/');
  279. /**
  280. * RSS 1.0 Content Module Namespace
  281. */
  282. define('SIMPLEPIE_NAMESPACE_RSS_10_MODULES_CONTENT', 'http://purl.org/rss/1.0/modules/content/');
  283. /**
  284. * RSS 2.0 Namespace
  285. * (Stupid, I know, but I'm certain it will confuse people less with support.)
  286. */
  287. define('SIMPLEPIE_NAMESPACE_RSS_20', '');
  288. /**
  289. * DC 1.0 Namespace
  290. */
  291. define('SIMPLEPIE_NAMESPACE_DC_10', 'http://purl.org/dc/elements/1.0/');
  292. /**
  293. * DC 1.1 Namespace
  294. */
  295. define('SIMPLEPIE_NAMESPACE_DC_11', 'http://purl.org/dc/elements/1.1/');
  296. /**
  297. * W3C Basic Geo (WGS84 lat/long) Vocabulary Namespace
  298. */
  299. define('SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO', 'http://www.w3.org/2003/01/geo/wgs84_pos#');
  300. /**
  301. * GeoRSS Namespace
  302. */
  303. define('SIMPLEPIE_NAMESPACE_GEORSS', 'http://www.georss.org/georss');
  304. /**
  305. * Media RSS Namespace
  306. */
  307. define('SIMPLEPIE_NAMESPACE_MEDIARSS', 'http://search.yahoo.com/mrss/');
  308. /**
  309. * Wrong Media RSS Namespace. Caused by a long-standing typo in the spec.
  310. */
  311. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG', 'http://search.yahoo.com/mrss');
  312. /**
  313. * Wrong Media RSS Namespace #2. New namespace introduced in Media RSS 1.5.
  314. */
  315. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2', 'http://video.search.yahoo.com/mrss');
  316. /**
  317. * Wrong Media RSS Namespace #3. A possible typo of the Media RSS 1.5 namespace.
  318. */
  319. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3', 'http://video.search.yahoo.com/mrss/');
  320. /**
  321. * Wrong Media RSS Namespace #4. New spec location after the RSS Advisory Board takes it over, but not a valid namespace.
  322. */
  323. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4', 'http://www.rssboard.org/media-rss');
  324. /**
  325. * Wrong Media RSS Namespace #5. A possible typo of the RSS Advisory Board URL.
  326. */
  327. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5', 'http://www.rssboard.org/media-rss/');
  328. /**
  329. * iTunes RSS Namespace
  330. */
  331. define('SIMPLEPIE_NAMESPACE_ITUNES', 'http://www.itunes.com/dtds/podcast-1.0.dtd');
  332. /**
  333. * XHTML Namespace
  334. */
  335. define('SIMPLEPIE_NAMESPACE_XHTML', 'http://www.w3.org/1999/xhtml');
  336. /**
  337. * IANA Link Relations Registry
  338. */
  339. define('SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY', 'http://www.iana.org/assignments/relation/');
  340. /**
  341. * No file source
  342. */
  343. define('SIMPLEPIE_FILE_SOURCE_NONE', 0);
  344. /**
  345. * Remote file source
  346. */
  347. define('SIMPLEPIE_FILE_SOURCE_REMOTE', 1);
  348. /**
  349. * Local file source
  350. */
  351. define('SIMPLEPIE_FILE_SOURCE_LOCAL', 2);
  352. /**
  353. * fsockopen() file source
  354. */
  355. define('SIMPLEPIE_FILE_SOURCE_FSOCKOPEN', 4);
  356. /**
  357. * cURL file source
  358. */
  359. define('SIMPLEPIE_FILE_SOURCE_CURL', 8);
  360. /**
  361. * file_get_contents() file source
  362. */
  363. define('SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS', 16);
  364. /**
  365. * SimplePie
  366. *
  367. * @package SimplePie
  368. * @subpackage API
  369. */
  370. class SimplePie
  371. {
  372. /**
  373. * @var array Raw data
  374. * @access private
  375. */
  376. public $data = array();
  377. /**
  378. * @var mixed Error string
  379. * @access private
  380. */
  381. public $error;
  382. /**
  383. * @var object Instance of SimplePie_Sanitize (or other class)
  384. * @see SimplePie::set_sanitize_class()
  385. * @access private
  386. */
  387. public $sanitize;
  388. /**
  389. * @var string SimplePie Useragent
  390. * @see SimplePie::set_useragent()
  391. * @access private
  392. */
  393. public $useragent = SIMPLEPIE_USERAGENT;
  394. /**
  395. * @var string Feed URL
  396. * @see SimplePie::set_feed_url()
  397. * @access private
  398. */
  399. public $feed_url;
  400. /**
  401. * @var string Original feed URL, or new feed URL iff HTTP 301 Moved Permanently
  402. * @see SimplePie::subscribe_url()
  403. * @access private
  404. */
  405. public $permanent_url = null;
  406. /**
  407. * @var object Instance of SimplePie_File to use as a feed
  408. * @see SimplePie::set_file()
  409. * @access private
  410. */
  411. public $file;
  412. /**
  413. * @var string Raw feed data
  414. * @see SimplePie::set_raw_data()
  415. * @access private
  416. */
  417. public $raw_data;
  418. /**
  419. * @var int Timeout for fetching remote files
  420. * @see SimplePie::set_timeout()
  421. * @access private
  422. */
  423. public $timeout = 10;
  424. /**
  425. * @var array Custom curl options
  426. * @see SimplePie::set_curl_options()
  427. * @access private
  428. */
  429. public $curl_options = array();
  430. /**
  431. * @var bool Forces fsockopen() to be used for remote files instead
  432. * of cURL, even if a new enough version is installed
  433. * @see SimplePie::force_fsockopen()
  434. * @access private
  435. */
  436. public $force_fsockopen = false;
  437. /**
  438. * @var bool Force the given data/URL to be treated as a feed no matter what
  439. * it appears like
  440. * @see SimplePie::force_feed()
  441. * @access private
  442. */
  443. public $force_feed = false;
  444. /**
  445. * @var bool Enable/Disable Caching
  446. * @see SimplePie::enable_cache()
  447. * @access private
  448. */
  449. public $cache = true;
  450. /**
  451. * @var bool Force SimplePie to fallback to expired cache, if enabled,
  452. * when feed is unavailable.
  453. * @see SimplePie::force_cache_fallback()
  454. * @access private
  455. */
  456. public $force_cache_fallback = false;
  457. /**
  458. * @var int Cache duration (in seconds)
  459. * @see SimplePie::set_cache_duration()
  460. * @access private
  461. */
  462. public $cache_duration = 3600;
  463. /**
  464. * @var int Auto-discovery cache duration (in seconds)
  465. * @see SimplePie::set_autodiscovery_cache_duration()
  466. * @access private
  467. */
  468. public $autodiscovery_cache_duration = 604800; // 7 Days.
  469. /**
  470. * @var string Cache location (relative to executing script)
  471. * @see SimplePie::set_cache_location()
  472. * @access private
  473. */
  474. public $cache_location = './cache';
  475. /**
  476. * @var string Function that creates the cache filename
  477. * @see SimplePie::set_cache_name_function()
  478. * @access private
  479. */
  480. public $cache_name_function = 'md5';
  481. /**
  482. * @var bool Reorder feed by date descending
  483. * @see SimplePie::enable_order_by_date()
  484. * @access private
  485. */
  486. public $order_by_date = true;
  487. /**
  488. * @var mixed Force input encoding to be set to the follow value
  489. * (false, or anything type-cast to false, disables this feature)
  490. * @see SimplePie::set_input_encoding()
  491. * @access private
  492. */
  493. public $input_encoding = false;
  494. /**
  495. * @var int Feed Autodiscovery Level
  496. * @see SimplePie::set_autodiscovery_level()
  497. * @access private
  498. */
  499. public $autodiscovery = SIMPLEPIE_LOCATOR_ALL;
  500. /**
  501. * Class registry object
  502. *
  503. * @var SimplePie_Registry
  504. */
  505. public $registry;
  506. /**
  507. * @var int Maximum number of feeds to check with autodiscovery
  508. * @see SimplePie::set_max_checked_feeds()
  509. * @access private
  510. */
  511. public $max_checked_feeds = 10;
  512. /**
  513. * @var array All the feeds found during the autodiscovery process
  514. * @see SimplePie::get_all_discovered_feeds()
  515. * @access private
  516. */
  517. public $all_discovered_feeds = array();
  518. /**
  519. * @var string Web-accessible path to the handler_image.php file.
  520. * @see SimplePie::set_image_handler()
  521. * @access private
  522. */
  523. public $image_handler = '';
  524. /**
  525. * @var array Stores the URLs when multiple feeds are being initialized.
  526. * @see SimplePie::set_feed_url()
  527. * @access private
  528. */
  529. public $multifeed_url = array();
  530. /**
  531. * @var array Stores SimplePie objects when multiple feeds initialized.
  532. * @access private
  533. */
  534. public $multifeed_objects = array();
  535. /**
  536. * @var array Stores the get_object_vars() array for use with multifeeds.
  537. * @see SimplePie::set_feed_url()
  538. * @access private
  539. */
  540. public $config_settings = null;
  541. /**
  542. * @var integer Stores the number of items to return per-feed with multifeeds.
  543. * @see SimplePie::set_item_limit()
  544. * @access private
  545. */
  546. public $item_limit = 0;
  547. /**
  548. * @var bool Stores if last-modified and/or etag headers were sent with the
  549. * request when checking a feed.
  550. */
  551. public $check_modified = false;
  552. /**
  553. * @var array Stores the default attributes to be stripped by strip_attributes().
  554. * @see SimplePie::strip_attributes()
  555. * @access private
  556. */
  557. public $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc');
  558. /**
  559. * @var array Stores the default attributes to add to different tags by add_attributes().
  560. * @see SimplePie::add_attributes()
  561. * @access private
  562. */
  563. public $add_attributes = array('audio' => array('preload' => 'none'), 'iframe' => array('sandbox' => 'allow-scripts allow-same-origin'), 'video' => array('preload' => 'none'));
  564. /**
  565. * @var array Stores the default tags to be stripped by strip_htmltags().
  566. * @see SimplePie::strip_htmltags()
  567. * @access private
  568. */
  569. public $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
  570. /**
  571. * @var bool Should we throw exceptions, or use the old-style error property?
  572. * @access private
  573. */
  574. public $enable_exceptions = false;
  575. /**
  576. * The SimplePie class contains feed level data and options
  577. *
  578. * To use SimplePie, create the SimplePie object with no parameters. You can
  579. * then set configuration options using the provided methods. After setting
  580. * them, you must initialise the feed using $feed->init(). At that point the
  581. * object's methods and properties will be available to you.
  582. *
  583. * Previously, it was possible to pass in the feed URL along with cache
  584. * options directly into the constructor. This has been removed as of 1.3 as
  585. * it caused a lot of confusion.
  586. *
  587. * @since 1.0 Preview Release
  588. */
  589. public function __construct()
  590. {
  591. if (version_compare(PHP_VERSION, '5.6', '<'))
  592. {
  593. trigger_error('Please upgrade to PHP 5.6 or newer.');
  594. die();
  595. }
  596. // Other objects, instances created here so we can set options on them
  597. $this->sanitize = new SimplePie_Sanitize();
  598. $this->registry = new SimplePie_Registry();
  599. if (func_num_args() > 0)
  600. {
  601. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  602. trigger_error('Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_duration() directly.', $level);
  603. $args = func_get_args();
  604. switch (count($args)) {
  605. case 3:
  606. $this->set_cache_duration($args[2]);
  607. case 2:
  608. $this->set_cache_location($args[1]);
  609. case 1:
  610. $this->set_feed_url($args[0]);
  611. $this->init();
  612. }
  613. }
  614. }
  615. /**
  616. * Used for converting object to a string
  617. */
  618. public function __toString()
  619. {
  620. return md5(serialize($this->data));
  621. }
  622. /**
  623. * Remove items that link back to this before destroying this object
  624. */
  625. public function __destruct()
  626. {
  627. if (!gc_enabled())
  628. {
  629. if (!empty($this->data['items']))
  630. {
  631. foreach ($this->data['items'] as $item)
  632. {
  633. $item->__destruct();
  634. }
  635. unset($item, $this->data['items']);
  636. }
  637. if (!empty($this->data['ordered_items']))
  638. {
  639. foreach ($this->data['ordered_items'] as $item)
  640. {
  641. $item->__destruct();
  642. }
  643. unset($item, $this->data['ordered_items']);
  644. }
  645. }
  646. }
  647. /**
  648. * Force the given data/URL to be treated as a feed
  649. *
  650. * This tells SimplePie to ignore the content-type provided by the server.
  651. * Be careful when using this option, as it will also disable autodiscovery.
  652. *
  653. * @since 1.1
  654. * @param bool $enable Force the given data/URL to be treated as a feed
  655. */
  656. public function force_feed($enable = false)
  657. {
  658. $this->force_feed = (bool) $enable;
  659. }
  660. /**
  661. * Set the URL of the feed you want to parse
  662. *
  663. * This allows you to enter the URL of the feed you want to parse, or the
  664. * website you want to try to use auto-discovery on. This takes priority
  665. * over any set raw data.
  666. *
  667. * You can set multiple feeds to mash together by passing an array instead
  668. * of a string for the $url. Remember that with each additional feed comes
  669. * additional processing and resources.
  670. *
  671. * @since 1.0 Preview Release
  672. * @see set_raw_data()
  673. * @param string|array $url This is the URL (or array of URLs) that you want to parse.
  674. */
  675. public function set_feed_url($url)
  676. {
  677. $this->multifeed_url = array();
  678. if (is_array($url))
  679. {
  680. foreach ($url as $value)
  681. {
  682. $this->multifeed_url[] = $this->registry->call('Misc', 'fix_protocol', array($value, 1));
  683. }
  684. }
  685. else
  686. {
  687. $this->feed_url = $this->registry->call('Misc', 'fix_protocol', array($url, 1));
  688. $this->permanent_url = $this->feed_url;
  689. }
  690. }
  691. /**
  692. * Set an instance of {@see SimplePie_File} to use as a feed
  693. *
  694. * @param SimplePie_File &$file
  695. * @return bool True on success, false on failure
  696. */
  697. public function set_file(&$file)
  698. {
  699. if ($file instanceof SimplePie_File)
  700. {
  701. $this->feed_url = $file->url;
  702. $this->permanent_url = $this->feed_url;
  703. $this->file =& $file;
  704. return true;
  705. }
  706. return false;
  707. }
  708. /**
  709. * Set the raw XML data to parse
  710. *
  711. * Allows you to use a string of RSS/Atom data instead of a remote feed.
  712. *
  713. * If you have a feed available as a string in PHP, you can tell SimplePie
  714. * to parse that data string instead of a remote feed. Any set feed URL
  715. * takes precedence.
  716. *
  717. * @since 1.0 Beta 3
  718. * @param string $data RSS or Atom data as a string.
  719. * @see set_feed_url()
  720. */
  721. public function set_raw_data($data)
  722. {
  723. $this->raw_data = $data;
  724. }
  725. /**
  726. * Set the default timeout for fetching remote feeds
  727. *
  728. * This allows you to change the maximum time the feed's server to respond
  729. * and send the feed back.
  730. *
  731. * @since 1.0 Beta 3
  732. * @param int $timeout The maximum number of seconds to spend waiting to retrieve a feed.
  733. */
  734. public function set_timeout($timeout = 10)
  735. {
  736. $this->timeout = (int) $timeout;
  737. }
  738. /**
  739. * Set custom curl options
  740. *
  741. * This allows you to change default curl options
  742. *
  743. * @since 1.0 Beta 3
  744. * @param array $curl_options Curl options to add to default settings
  745. */
  746. public function set_curl_options(array $curl_options = array())
  747. {
  748. $this->curl_options = $curl_options;
  749. }
  750. /**
  751. * Force SimplePie to use fsockopen() instead of cURL
  752. *
  753. * @since 1.0 Beta 3
  754. * @param bool $enable Force fsockopen() to be used
  755. */
  756. public function force_fsockopen($enable = false)
  757. {
  758. $this->force_fsockopen = (bool) $enable;
  759. }
  760. /**
  761. * Enable/disable caching in SimplePie.
  762. *
  763. * This option allows you to disable caching all-together in SimplePie.
  764. * However, disabling the cache can lead to longer load times.
  765. *
  766. * @since 1.0 Preview Release
  767. * @param bool $enable Enable caching
  768. */
  769. public function enable_cache($enable = true)
  770. {
  771. $this->cache = (bool) $enable;
  772. }
  773. /**
  774. * SimplePie to continue to fall back to expired cache, if enabled, when
  775. * feed is unavailable.
  776. *
  777. * This tells SimplePie to ignore any file errors and fall back to cache
  778. * instead. This only works if caching is enabled and cached content
  779. * still exists.
  780. * @param bool $enable Force use of cache on fail.
  781. */
  782. public function force_cache_fallback($enable = false)
  783. {
  784. $this->force_cache_fallback= (bool) $enable;
  785. }
  786. /**
  787. * Set the length of time (in seconds) that the contents of a feed will be
  788. * cached
  789. *
  790. * @param int $seconds The feed content cache duration
  791. */
  792. public function set_cache_duration($seconds = 3600)
  793. {
  794. $this->cache_duration = (int) $seconds;
  795. }
  796. /**
  797. * Set the length of time (in seconds) that the autodiscovered feed URL will
  798. * be cached
  799. *
  800. * @param int $seconds The autodiscovered feed URL cache duration.
  801. */
  802. public function set_autodiscovery_cache_duration($seconds = 604800)
  803. {
  804. $this->autodiscovery_cache_duration = (int) $seconds;
  805. }
  806. /**
  807. * Set the file system location where the cached files should be stored
  808. *
  809. * @param string $location The file system location.
  810. */
  811. public function set_cache_location($location = './cache')
  812. {
  813. $this->cache_location = (string) $location;
  814. }
  815. /**
  816. * Set whether feed items should be sorted into reverse chronological order
  817. *
  818. * @param bool $enable Sort as reverse chronological order.
  819. */
  820. public function enable_order_by_date($enable = true)
  821. {
  822. $this->order_by_date = (bool) $enable;
  823. }
  824. /**
  825. * Set the character encoding used to parse the feed
  826. *
  827. * This overrides the encoding reported by the feed, however it will fall
  828. * back to the normal encoding detection if the override fails
  829. *
  830. * @param string $encoding Character encoding
  831. */
  832. public function set_input_encoding($encoding = false)
  833. {
  834. if ($encoding)
  835. {
  836. $this->input_encoding = (string) $encoding;
  837. }
  838. else
  839. {
  840. $this->input_encoding = false;
  841. }
  842. }
  843. /**
  844. * Set how much feed autodiscovery to do
  845. *
  846. * @see SIMPLEPIE_LOCATOR_NONE
  847. * @see SIMPLEPIE_LOCATOR_AUTODISCOVERY
  848. * @see SIMPLEPIE_LOCATOR_LOCAL_EXTENSION
  849. * @see SIMPLEPIE_LOCATOR_LOCAL_BODY
  850. * @see SIMPLEPIE_LOCATOR_REMOTE_EXTENSION
  851. * @see SIMPLEPIE_LOCATOR_REMOTE_BODY
  852. * @see SIMPLEPIE_LOCATOR_ALL
  853. * @param int $level Feed Autodiscovery Level (level can be a combination of the above constants, see bitwise OR operator)
  854. */
  855. public function set_autodiscovery_level($level = SIMPLEPIE_LOCATOR_ALL)
  856. {
  857. $this->autodiscovery = (int) $level;
  858. }
  859. /**
  860. * Get the class registry
  861. *
  862. * Use this to override SimplePie's default classes
  863. * @see SimplePie_Registry
  864. * @return SimplePie_Registry
  865. */
  866. public function &get_registry()
  867. {
  868. return $this->registry;
  869. }
  870. /**#@+
  871. * Useful when you are overloading or extending SimplePie's default classes.
  872. *
  873. * @deprecated Use {@see get_registry()} instead
  874. * @link http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.extends PHP5 extends documentation
  875. * @param string $class Name of custom class
  876. * @return boolean True on success, false otherwise
  877. */
  878. /**
  879. * Set which class SimplePie uses for caching
  880. */
  881. public function set_cache_class($class = 'SimplePie_Cache')
  882. {
  883. return $this->registry->register('Cache', $class, true);
  884. }
  885. /**
  886. * Set which class SimplePie uses for auto-discovery
  887. */
  888. public function set_locator_class($class = 'SimplePie_Locator')
  889. {
  890. return $this->registry->register('Locator', $class, true);
  891. }
  892. /**
  893. * Set which class SimplePie uses for XML parsing
  894. */
  895. public function set_parser_class($class = 'SimplePie_Parser')
  896. {
  897. return $this->registry->register('Parser', $class, true);
  898. }
  899. /**
  900. * Set which class SimplePie uses for remote file fetching
  901. */
  902. public function set_file_class($class = 'SimplePie_File')
  903. {
  904. return $this->registry->register('File', $class, true);
  905. }
  906. /**
  907. * Set which class SimplePie uses for data sanitization
  908. */
  909. public function set_sanitize_class($class = 'SimplePie_Sanitize')
  910. {
  911. return $this->registry->register('Sanitize', $class, true);
  912. }
  913. /**
  914. * Set which class SimplePie uses for handling feed items
  915. */
  916. public function set_item_class($class = 'SimplePie_Item')
  917. {
  918. return $this->registry->register('Item', $class, true);
  919. }
  920. /**
  921. * Set which class SimplePie uses for handling author data
  922. */
  923. public function set_author_class($class = 'SimplePie_Author')
  924. {
  925. return $this->registry->register('Author', $class, true);
  926. }
  927. /**
  928. * Set which class SimplePie uses for handling category data
  929. */
  930. public function set_category_class($class = 'SimplePie_Category')
  931. {
  932. return $this->registry->register('Category', $class, true);
  933. }
  934. /**
  935. * Set which class SimplePie uses for feed enclosures
  936. */
  937. public function set_enclosure_class($class = 'SimplePie_Enclosure')
  938. {
  939. return $this->registry->register('Enclosure', $class, true);
  940. }
  941. /**
  942. * Set which class SimplePie uses for `<media:text>` captions
  943. */
  944. public function set_caption_class($class = 'SimplePie_Caption')
  945. {
  946. return $this->registry->register('Caption', $class, true);
  947. }
  948. /**
  949. * Set which class SimplePie uses for `<media:copyright>`
  950. */
  951. public function set_copyright_class($class = 'SimplePie_Copyright')
  952. {
  953. return $this->registry->register('Copyright', $class, true);
  954. }
  955. /**
  956. * Set which class SimplePie uses for `<media:credit>`
  957. */
  958. public function set_credit_class($class = 'SimplePie_Credit')
  959. {
  960. return $this->registry->register('Credit', $class, true);
  961. }
  962. /**
  963. * Set which class SimplePie uses for `<media:rating>`
  964. */
  965. public function set_rating_class($class = 'SimplePie_Rating')
  966. {
  967. return $this->registry->register('Rating', $class, true);
  968. }
  969. /**
  970. * Set which class SimplePie uses for `<media:restriction>`
  971. */
  972. public function set_restriction_class($class = 'SimplePie_Restriction')
  973. {
  974. return $this->registry->register('Restriction', $class, true);
  975. }
  976. /**
  977. * Set which class SimplePie uses for content-type sniffing
  978. */
  979. public function set_content_type_sniffer_class($class = 'SimplePie_Content_Type_Sniffer')
  980. {
  981. return $this->registry->register('Content_Type_Sniffer', $class, true);
  982. }
  983. /**
  984. * Set which class SimplePie uses item sources
  985. */
  986. public function set_source_class($class = 'SimplePie_Source')
  987. {
  988. return $this->registry->register('Source', $class, true);
  989. }
  990. /**#@-*/
  991. /**
  992. * Set the user agent string
  993. *
  994. * @param string $ua New user agent string.
  995. */
  996. public function set_useragent($ua = SIMPLEPIE_USERAGENT)
  997. {
  998. $this->useragent = (string) $ua;
  999. }
  1000. /**
  1001. * Set callback function to create cache filename with
  1002. *
  1003. * @param mixed $function Callback function
  1004. */
  1005. public function set_cache_name_function($function = 'md5')
  1006. {
  1007. if (is_callable($function))
  1008. {
  1009. $this->cache_name_function = $function;
  1010. }
  1011. }
  1012. /**
  1013. * Set options to make SP as fast as possible
  1014. *
  1015. * Forgoes a substantial amount of data sanitization in favor of speed. This
  1016. * turns SimplePie into a dumb parser of feeds.
  1017. *
  1018. * @param bool $set Whether to set them or not
  1019. */
  1020. public function set_stupidly_fast($set = false)
  1021. {
  1022. if ($set)
  1023. {
  1024. $this->enable_order_by_date(false);
  1025. $this->remove_div(false);
  1026. $this->strip_comments(false);
  1027. $this->strip_htmltags(false);
  1028. $this->strip_attributes(false);
  1029. $this->add_attributes(false);
  1030. $this->set_image_handler(false);
  1031. }
  1032. }
  1033. /**
  1034. * Set maximum number of feeds to check with autodiscovery
  1035. *
  1036. * @param int $max Maximum number of feeds to check
  1037. */
  1038. public function set_max_checked_feeds($max = 10)
  1039. {
  1040. $this->max_checked_feeds = (int) $max;
  1041. }
  1042. public function remove_div($enable = true)
  1043. {
  1044. $this->sanitize->remove_div($enable);
  1045. }
  1046. public function strip_htmltags($tags = '', $encode = null)
  1047. {
  1048. if ($tags === '')
  1049. {
  1050. $tags = $this->strip_htmltags;
  1051. }
  1052. $this->sanitize->strip_htmltags($tags);
  1053. if ($encode !== null)
  1054. {
  1055. $this->sanitize->encode_instead_of_strip($tags);
  1056. }
  1057. }
  1058. public function encode_instead_of_strip($enable = true)
  1059. {
  1060. $this->sanitize->encode_instead_of_strip($enable);
  1061. }
  1062. public function strip_attributes($attribs = '')
  1063. {
  1064. if ($attribs === '')
  1065. {
  1066. $attribs = $this->strip_attributes;
  1067. }
  1068. $this->sanitize->strip_attributes($attribs);
  1069. }
  1070. public function add_attributes($attribs = '')
  1071. {
  1072. if ($attribs === '')
  1073. {
  1074. $attribs = $this->add_attributes;
  1075. }
  1076. $this->sanitize->add_attributes($attribs);
  1077. }
  1078. /**
  1079. * Set the output encoding
  1080. *
  1081. * Allows you to override SimplePie's output to match that of your webpage.
  1082. * This is useful for times when your webpages are not being served as
  1083. * UTF-8. This setting will be obeyed by {@see handle_content_type()}, and
  1084. * is similar to {@see set_input_encoding()}.
  1085. *
  1086. * It should be noted, however, that not all character encodings can support
  1087. * all characters. If your page is being served as ISO-8859-1 and you try
  1088. * to display a Japanese feed, you'll likely see garbled characters.
  1089. * Because of this, it is highly recommended to ensure that your webpages
  1090. * are served as UTF-8.
  1091. *
  1092. * The number of supported character encodings depends on whether your web
  1093. * host supports {@link http://php.net/mbstring mbstring},
  1094. * {@link http://php.net/iconv iconv}, or both. See
  1095. * {@link http://simplepie.org/wiki/faq/Supported_Character_Encodings} for
  1096. * more information.
  1097. *
  1098. * @param string $encoding
  1099. */
  1100. public function set_output_encoding($encoding = 'UTF-8')
  1101. {
  1102. $this->sanitize->set_output_encoding($encoding);
  1103. }
  1104. public function strip_comments($strip = false)
  1105. {
  1106. $this->sanitize->strip_comments($strip);
  1107. }
  1108. /**
  1109. * Set element/attribute key/value pairs of HTML attributes
  1110. * containing URLs that need to be resolved relative to the feed
  1111. *
  1112. * Defaults to |a|@href, |area|@href, |blockquote|@cite, |del|@cite,
  1113. * |form|@action, |img|@longdesc, |img|@src, |input|@src, |ins|@cite,
  1114. * |q|@cite
  1115. *
  1116. * @since 1.0
  1117. * @param array|null $element_attribute Element/attribute key/value pairs, null for default
  1118. */
  1119. public function set_url_replacements($element_attribute = null)
  1120. {
  1121. $this->sanitize->set_url_replacements($element_attribute);
  1122. }
  1123. /**
  1124. * Set the handler to enable the display of cached images.
  1125. *
  1126. * @param string $page Web-accessible path to the handler_image.php file.
  1127. * @param string $qs The query string that the value should be passed to.
  1128. */
  1129. public function set_image_handler($page = false, $qs = 'i')
  1130. {
  1131. if ($page !== false)
  1132. {
  1133. $this->sanitize->set_image_handler($page . '?' . $qs . '=');
  1134. }
  1135. else
  1136. {
  1137. $this->image_handler = '';
  1138. }
  1139. }
  1140. /**
  1141. * Set the limit for items returned per-feed with multifeeds
  1142. *
  1143. * @param integer $limit The maximum number of items to return.
  1144. */
  1145. public function set_item_limit($limit = 0)
  1146. {
  1147. $this->item_limit = (int) $limit;
  1148. }
  1149. /**
  1150. * Enable throwing exceptions
  1151. *
  1152. * @param boolean $enable Should we throw exceptions, or use the old-style error property?
  1153. */
  1154. public function enable_exceptions($enable = true)
  1155. {
  1156. $this->enable_exceptions = $enable;
  1157. }
  1158. /**
  1159. * Initialize the feed object
  1160. *
  1161. * This is what makes everything happen. Period. This is where all of the
  1162. * configuration options get processed, feeds are fetched, cached, and
  1163. * parsed, and all of that other good stuff.
  1164. *
  1165. * @return boolean True if successful, false otherwise
  1166. */
  1167. public function init()
  1168. {
  1169. // Check absolute bare minimum requirements.
  1170. if (!extension_loaded('xml') || !extension_loaded('pcre'))
  1171. {
  1172. $this->error = 'XML or PCRE extensions not loaded!';
  1173. return false;
  1174. }
  1175. // Then check the xml extension is sane (i.e., libxml 2.7.x issue on PHP < 5.2.9 and libxml 2.7.0 to 2.7.2 on any version) if we don't have xmlreader.
  1176. elseif (!extension_loaded('xmlreader'))
  1177. {
  1178. static $xml_is_sane = null;
  1179. if ($xml_is_sane === null)
  1180. {
  1181. $parser_check = xml_parser_create();
  1182. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  1183. xml_parser_free($parser_check);
  1184. $xml_is_sane = isset($values[0]['value']);
  1185. }
  1186. if (!$xml_is_sane)
  1187. {
  1188. return false;
  1189. }
  1190. }
  1191. // The default sanitize class gets set in the constructor, check if it has
  1192. // changed.
  1193. if ($this->registry->get_class('Sanitize') !== 'SimplePie_Sanitize') {
  1194. $this->sanitize = $this->registry->create('Sanitize');
  1195. }
  1196. if (method_exists($this->sanitize, 'set_registry'))
  1197. {
  1198. $this->sanitize->set_registry($this->registry);
  1199. }
  1200. // Pass whatever was set with config options over to the sanitizer.
  1201. // Pass the classes in for legacy support; new classes should use the registry instead
  1202. $this->sanitize->pass_cache_data($this->cache, $this->cache_location, $this->cache_name_function, $this->registry->get_class('Cache'));
  1203. $this->sanitize->pass_file_data($this->registry->get_class('File'), $this->timeout, $this->useragent, $this->force_fsockopen, $this->curl_options);
  1204. if (!empty($this->multifeed_url))
  1205. {
  1206. $i = 0;
  1207. $success = 0;
  1208. $this->multifeed_objects = array();
  1209. $this->error = array();
  1210. foreach ($this->multifeed_url as $url)
  1211. {
  1212. $this->multifeed_objects[$i] = clone $this;
  1213. $this->multifeed_objects[$i]->set_feed_url($url);
  1214. $single_success = $this->multifeed_objects[$i]->init();
  1215. $success |= $single_success;
  1216. if (!$single_success)
  1217. {
  1218. $this->error[$i] = $this->multifeed_objects[$i]->error();
  1219. }
  1220. $i++;
  1221. }
  1222. return (bool) $success;
  1223. }
  1224. elseif ($this->feed_url === null && $this->raw_data === null)
  1225. {
  1226. return false;
  1227. }
  1228. $this->error = null;
  1229. $this->data = array();
  1230. $this->check_modified = false;
  1231. $this->multifeed_objects = array();
  1232. $cache = false;
  1233. if ($this->feed_url !== null)
  1234. {
  1235. $parsed_feed_url = $this->registry->call('Misc', 'parse_url', array($this->feed_url));
  1236. // Decide whether to enable caching
  1237. if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1238. {
  1239. $url = $this->feed_url . ($this->force_feed ? '#force_feed' : '');
  1240. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $url), 'spc'));
  1241. }
  1242. // Fetch the data via SimplePie_File into $this->raw_data
  1243. if (($fetched = $this->fetch_data($cache)) === true)
  1244. {
  1245. return true;
  1246. }
  1247. elseif ($fetched === false) {
  1248. return false;
  1249. }
  1250. list($headers, $sniffed) = $fetched;
  1251. }
  1252. // Empty response check
  1253. if(empty($this->raw_data)){
  1254. $this->error = "A feed could not be found at `$this->feed_url`. Empty body.";
  1255. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1256. return false;
  1257. }
  1258. // Set up array of possible encodings
  1259. $encodings = array();
  1260. // First check to see if input has been overridden.
  1261. if ($this->input_encoding !== false)
  1262. {
  1263. $encodings[] = strtoupper($this->input_encoding);
  1264. }
  1265. $application_types = array('application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity');
  1266. $text_types = array('text/xml', 'text/xml-external-parsed-entity');
  1267. // RFC 3023 (only applies to sniffed content)
  1268. if (isset($sniffed))
  1269. {
  1270. if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml')
  1271. {
  1272. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1273. {
  1274. $encodings[] = strtoupper($charset[1]);
  1275. }
  1276. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1277. $encodings[] = 'UTF-8';
  1278. }
  1279. elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml')
  1280. {
  1281. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1282. {
  1283. $encodings[] = strtoupper($charset[1]);
  1284. }
  1285. $encodings[] = 'US-ASCII';
  1286. }
  1287. // Text MIME-type default
  1288. elseif (substr($sniffed, 0, 5) === 'text/')
  1289. {
  1290. $encodings[] = 'UTF-8';
  1291. }
  1292. }
  1293. // Fallback to XML 1.0 Appendix F.1/UTF-8/ISO-8859-1
  1294. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1295. $encodings[] = 'UTF-8';
  1296. $encodings[] = 'ISO-8859-1';
  1297. // There's no point in trying an encoding twice
  1298. $encodings = array_unique($encodings);
  1299. // Loop through each possible encoding, till we return something, or run out of possibilities
  1300. foreach ($encodings as $encoding)
  1301. {
  1302. // Change the encoding to UTF-8 (as we always use UTF-8 internally)
  1303. if ($utf8_data = $this->registry->call('Misc', 'change_encoding', array($this->raw_data, $encoding, 'UTF-8')))
  1304. {
  1305. // Create new parser
  1306. $parser = $this->registry->create('Parser');
  1307. // If it's parsed fine
  1308. if ($parser->parse($utf8_data, 'UTF-8', $this->permanent_url))
  1309. {
  1310. $this->data = $parser->get_data();
  1311. if (!($this->get_type() & ~SIMPLEPIE_TYPE_NONE))
  1312. {
  1313. $this->error = "A feed could not be found at `$this->feed_url`. This does not appear to be a valid RSS or Atom feed.";
  1314. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1315. return false;
  1316. }
  1317. if (isset($headers))
  1318. {
  1319. $this->data['headers'] = $headers;
  1320. }
  1321. $this->data['build'] = SIMPLEPIE_BUILD;
  1322. // Cache the file if caching is enabled
  1323. if ($cache && !$cache->save($this))
  1324. {
  1325. trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1326. }
  1327. return true;
  1328. }
  1329. }
  1330. }
  1331. if (isset($parser))
  1332. {
  1333. // We have an error, just set SimplePie_Misc::error to it and quit
  1334. $this->error = $this->feed_url;
  1335. $this->error .= sprintf(' is invalid XML, likely due to invalid characters. XML error: %s at line %d, column %d', $parser->get_error_string(), $parser->get_current_line(), $parser->get_current_column());
  1336. }
  1337. else
  1338. {
  1339. $this->error = 'The data could not be converted to UTF-8.';
  1340. if (!extension_loaded('mbstring') && !extension_loaded('iconv') && !class_exists('\UConverter')) {
  1341. $this->error .= ' You MUST have either the iconv, mbstring or intl (PHP 5.5+) extension installed and enabled.';
  1342. } else {
  1343. $missingExtensions = array();
  1344. if (!extension_loaded('iconv')) {
  1345. $missingExtensions[] = 'iconv';
  1346. }
  1347. if (!extension_loaded('mbstring')) {
  1348. $missingExtensions[] = 'mbstring';
  1349. }
  1350. if (!class_exists('\UConverter')) {
  1351. $missingExtensions[] = 'intl (PHP 5.5+)';
  1352. }
  1353. $this->error .= ' Try installing/enabling the ' . implode(' or ', $missingExtensions) . ' extension.';
  1354. }
  1355. }
  1356. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1357. return false;
  1358. }
  1359. /**
  1360. * Fetch the data via SimplePie_File
  1361. *
  1362. * If the data is already cached, attempt to fetch it from there instead
  1363. * @param SimplePie_Cache|false $cache Cache handler, or false to not load from the cache
  1364. * @return array|true Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type
  1365. */
  1366. protected function fetch_data(&$cache)
  1367. {
  1368. // If it's enabled, use the cache
  1369. if ($cache)
  1370. {
  1371. // Load the Cache
  1372. $this->data = $cache->load();
  1373. if (!empty($this->data))
  1374. {
  1375. // If the cache is for an outdated build of SimplePie
  1376. if (!isset($this->data['build']) || $this->data['build'] !== SIMPLEPIE_BUILD)
  1377. {
  1378. $cache->unlink();
  1379. $this->data = array();
  1380. }
  1381. // If we've hit a collision just rerun it with caching disabled
  1382. elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url)
  1383. {
  1384. $cache = false;
  1385. $this->data = array();
  1386. }
  1387. // If we've got a non feed_url stored (if the page isn't actually a feed, or is a redirect) use that URL.
  1388. elseif (isset($this->data['feed_url']))
  1389. {
  1390. // If the autodiscovery cache is still valid use it.
  1391. if ($cache->mtime() + $this->autodiscovery_cache_duration > time())
  1392. {
  1393. // Do not need to do feed autodiscovery yet.
  1394. if ($this->data['feed_url'] !== $this->data['url'])
  1395. {
  1396. $this->set_feed_url($this->data['feed_url']);
  1397. return $this->init();
  1398. }
  1399. $cache->unlink();
  1400. $this->data = array();
  1401. }
  1402. }
  1403. // Check if the cache has been updated
  1404. elseif ($cache->mtime() + $this->cache_duration < time())
  1405. {
  1406. // Want to know if we tried to send last-modified and/or etag headers
  1407. // when requesting this file. (Note that it's up to the file to
  1408. // support this, but we don't always send the headers either.)
  1409. $this->check_modified = true;
  1410. if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))
  1411. {
  1412. $headers = array(
  1413. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  1414. );
  1415. if (isset($this->data['headers']['last-modified']))
  1416. {
  1417. $headers['if-modified-since'] = $this->data['headers']['last-modified'];
  1418. }
  1419. if (isset($this->data['headers']['etag']))
  1420. {
  1421. $headers['if-none-match'] = $this->data['headers']['etag'];
  1422. }
  1423. $file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1424. if ($file->success)
  1425. {
  1426. if ($file->status_code === 304)
  1427. {
  1428. // Set raw_data to false here too, to signify that the cache
  1429. // is still valid.
  1430. $this->raw_data = false;
  1431. $cache->touch();
  1432. return true;
  1433. }
  1434. }
  1435. else
  1436. {
  1437. $this->check_modified = false;
  1438. if($this->force_cache_fallback)
  1439. {
  1440. $cache->touch();
  1441. return true;
  1442. }
  1443. unset($file);
  1444. }
  1445. }
  1446. }
  1447. // If the cache is still valid, just return true
  1448. else
  1449. {
  1450. $this->raw_data = false;
  1451. return true;
  1452. }
  1453. }
  1454. // If the cache is empty, delete it
  1455. else
  1456. {
  1457. $cache->unlink();
  1458. $this->data = array();
  1459. }
  1460. }
  1461. // If we don't already have the file (it'll only exist if we've opened it to check if the cache has been modified), open it.
  1462. if (!isset($file))
  1463. {
  1464. if ($this->file instanceof SimplePie_File && $this->file->url === $this->feed_url)
  1465. {
  1466. $file =& $this->file;
  1467. }
  1468. else
  1469. {
  1470. $headers = array(
  1471. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  1472. );
  1473. $file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1474. }
  1475. }
  1476. // If the file connection has an error, set SimplePie::error to that and quit
  1477. if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1478. {
  1479. $this->error = $file->error;
  1480. return !empty($this->data);
  1481. }
  1482. if (!$this->force_feed)
  1483. {
  1484. // Check if the supplied URL is a feed, if it isn't, look for it.
  1485. $locate = $this->registry->create('Locator', array(&$file, $this->timeout, $this->useragent, $this->max_checked_feeds, $this->force_fsockopen, $this->curl_options));
  1486. if (!$locate->is_feed($file))
  1487. {
  1488. $copyStatusCode = $file->status_code;
  1489. $copyContentType = $file->headers['content-type'];
  1490. try
  1491. {
  1492. $microformats = false;
  1493. if (class_exists('DOMXpath') && function_exists('Mf2\parse')) {
  1494. $doc = new DOMDocument();
  1495. @$doc->loadHTML($file->body);
  1496. $xpath = new DOMXpath($doc);
  1497. // Check for both h-feed and h-entry, as both a feed with no entries
  1498. // and a list of entries without an h-feed wrapper are both valid.
  1499. $query = '//*[contains(concat(" ", @class, " "), " h-feed ") or '.
  1500. 'contains(concat(" ", @class, " "), " h-entry ")]';
  1501. $result = $xpath->query($query);
  1502. $microformats = $result->length !== 0;
  1503. }
  1504. // Now also do feed discovery, but if microformats were found don't
  1505. // overwrite the current value of file.
  1506. $discovered = $locate->find($this->autodiscovery,
  1507. $this->all_discovered_feeds);
  1508. if ($microformats)
  1509. {
  1510. if ($hub = $locate->get_rel_link('hub'))
  1511. {
  1512. $self = $locate->get_rel_link('self');
  1513. $this->store_links($file, $hub, $self);
  1514. }
  1515. // Push the current file onto all_discovered feeds so the user can
  1516. // be shown this as one of the options.
  1517. if (isset($this->all_discovered_feeds)) {
  1518. $this->all_discovered_feeds[] = $file;
  1519. }
  1520. }
  1521. else
  1522. {
  1523. if ($discovered)
  1524. {
  1525. $file = $discovered;
  1526. }
  1527. else
  1528. {
  1529. // We need to unset this so that if SimplePie::set_file() has
  1530. // been called that object is untouched
  1531. unset($file);
  1532. $this->error = "A feed could not be found at `$this->feed_url`; the status code is `$copyStatusCode` and content-type is `$copyContentType`";
  1533. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1534. return false;
  1535. }
  1536. }
  1537. }
  1538. catch (SimplePie_Exception $e)
  1539. {
  1540. // We need to unset this so that if SimplePie::set_file() has been called that object is untouched
  1541. unset($file);
  1542. // This is usually because DOMDocument doesn't exist
  1543. $this->error = $e->getMessage();
  1544. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, $e->getFile(), $e->getLine()));
  1545. return false;
  1546. }
  1547. if ($cache)
  1548. {
  1549. $this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
  1550. if (!$cache->save($this))
  1551. {
  1552. trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1553. }
  1554. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $file->url), 'spc'));
  1555. }
  1556. }
  1557. $this->feed_url = $file->url;
  1558. $locate = null;
  1559. }
  1560. $this->raw_data = $file->body;
  1561. $this->permanent_url = $file->permanent_url;
  1562. $headers = $file->headers;
  1563. $sniffer = $this->registry->create('Content_Type_Sniffer', array(&$file));
  1564. $sniffed = $sniffer->get_type();
  1565. return array($headers, $sniffed);
  1566. }
  1567. /**
  1568. * Get the error message for the occured error
  1569. *
  1570. * @return string|array Error message, or array of messages for multifeeds
  1571. */
  1572. public function error()
  1573. {
  1574. return $this->error;
  1575. }
  1576. /**
  1577. * Get the raw XML
  1578. *
  1579. * This is the same as the old `$feed->enable_xml_dump(true)`, but returns
  1580. * the data instead of printing it.
  1581. *
  1582. * @return string|boolean Raw XML data, false if the cache is used
  1583. */
  1584. public function get_raw_data()
  1585. {
  1586. return $this->raw_data;
  1587. }
  1588. /**
  1589. * Get the character encoding used for output
  1590. *
  1591. * @since Preview Release
  1592. * @return string
  1593. */
  1594. public function get_encoding()
  1595. {
  1596. return $this->sanitize->output_encoding;
  1597. }
  1598. /**
  1599. * Send the content-type header with correct encoding
  1600. *
  1601. * This method ensures that the SimplePie-enabled page is being served with
  1602. * the correct {@link http://www.iana.org/assignments/media-types/ mime-type}
  1603. * and character encoding HTTP headers (character encoding determined by the
  1604. * {@see set_output_encoding} config option).
  1605. *
  1606. * This won't work properly if any content or whitespace has already been
  1607. * sent to the browser, because it relies on PHP's
  1608. * {@link http://php.net/header header()} function, and these are the
  1609. * circumstances under which the function works.
  1610. *
  1611. * Because it's setting these settings for the entire page (as is the nature
  1612. * of HTTP headers), this should only be used once per page (again, at the
  1613. * top).
  1614. *
  1615. * @param string $mime MIME type to serve the page as
  1616. */
  1617. public function handle_content_type($mime = 'text/html')
  1618. {
  1619. if (!headers_sent())
  1620. {
  1621. $header = "Content-type: $mime;";
  1622. if ($this->get_encoding())
  1623. {
  1624. $header .= ' charset=' . $this->get_encoding();
  1625. }
  1626. else
  1627. {
  1628. $header .= ' charset=UTF-8';
  1629. }
  1630. header($header);
  1631. }
  1632. }
  1633. /**
  1634. * Get the type of the feed
  1635. *
  1636. * This returns a SIMPLEPIE_TYPE_* constant, which can be tested against
  1637. * using {@link http://php.net/language.operators.bitwise bitwise operators}
  1638. *
  1639. * @since 0.8 (usage changed to using constants in 1.0)
  1640. * @see SIMPLEPIE_TYPE_NONE Unknown.
  1641. * @see SIMPLEPIE_TYPE_RSS_090 RSS 0.90.
  1642. * @see SIMPLEPIE_TYPE_RSS_091_NETSCAPE RSS 0.91 (Netscape).
  1643. * @see SIMPLEPIE_TYPE_RSS_091_USERLAND RSS 0.91 (Userland).
  1644. * @see SIMPLEPIE_TYPE_RSS_091 RSS 0.91.
  1645. * @see SIMPLEPIE_TYPE_RSS_092 RSS 0.92.
  1646. * @see SIMPLEPIE_TYPE_RSS_093 RSS 0.93.
  1647. * @see SIMPLEPIE_TYPE_RSS_094 RSS 0.94.
  1648. * @see SIMPLEPIE_TYPE_RSS_10 RSS 1.0.
  1649. * @see SIMPLEPIE_TYPE_RSS_20 RSS 2.0.x.
  1650. * @see SIMPLEPIE_TYPE_RSS_RDF RDF-based RSS.
  1651. * @see SIMPLEPIE_TYPE_RSS_SYNDICATION Non-RDF-based RSS (truly intended as syndication format).
  1652. * @see SIMPLEPIE_TYPE_RSS_ALL Any version of RSS.
  1653. * @see SIMPLEPIE_TYPE_ATOM_03 Atom 0.3.
  1654. * @see SIMPLEPIE_TYPE_ATOM_10 Atom 1.0.
  1655. * @see SIMPLEPIE_TYPE_ATOM_ALL Any version of Atom.
  1656. * @see SIMPLEPIE_TYPE_ALL Any known/supported feed type.
  1657. * @return int SIMPLEPIE_TYPE_* constant
  1658. */
  1659. public function get_type()
  1660. {
  1661. if (!isset($this->data['type']))
  1662. {
  1663. $this->data['type'] = SIMPLEPIE_TYPE_ALL;
  1664. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed']))
  1665. {
  1666. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_10;
  1667. }
  1668. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed']))
  1669. {
  1670. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_03;
  1671. }
  1672. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF']))
  1673. {
  1674. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['channel'])
  1675. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['image'])
  1676. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item'])
  1677. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['textinput']))
  1678. {
  1679. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_10;
  1680. }
  1681. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['channel'])
  1682. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['image'])
  1683. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item'])
  1684. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['textinput']))
  1685. {
  1686. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_090;
  1687. }
  1688. }
  1689. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss']))
  1690. {
  1691. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_ALL;
  1692. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1693. {
  1694. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1695. {
  1696. case '0.91':
  1697. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091;
  1698. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1699. {
  1700. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1701. {
  1702. case '0':
  1703. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_NETSCAPE;
  1704. break;
  1705. case '24':
  1706. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_USERLAND;
  1707. break;
  1708. }
  1709. }
  1710. break;
  1711. case '0.92':
  1712. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_092;
  1713. break;
  1714. case '0.93':
  1715. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_093;
  1716. break;
  1717. case '0.94':
  1718. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_094;
  1719. break;
  1720. case '2.0':
  1721. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_20;
  1722. break;
  1723. }
  1724. }
  1725. }
  1726. else
  1727. {
  1728. $this->data['type'] = SIMPLEPIE_TYPE_NONE;
  1729. }
  1730. }
  1731. return $this->data['type'];
  1732. }
  1733. /**
  1734. * Get the URL for the feed
  1735. *
  1736. * When the 'permanent' mode is enabled, returns the original feed URL,
  1737. * except in the case of an `HTTP 301 Moved Permanently` status response,
  1738. * in which case the location of the first redirection is returned.
  1739. *
  1740. * When the 'permanent' mode is disabled (default),
  1741. * may or may not be different from the URL passed to {@see set_feed_url()},
  1742. * depending on whether auto-discovery was used, and whether there were
  1743. * any redirects along the way.
  1744. *
  1745. * @since Preview Release (previously called `get_feed_url()` since SimplePie 0.8.)
  1746. * @todo Support <itunes:new-feed-url>
  1747. * @todo Also, |atom:link|@rel=self
  1748. * @param bool $permanent Permanent mode to return only the original URL or the first redirection
  1749. * iff it is a 301 redirection
  1750. * @return string|null
  1751. */
  1752. public function subscribe_url($permanent = false)
  1753. {
  1754. if ($permanent)
  1755. {
  1756. if ($this->permanent_url !== null)
  1757. {
  1758. // sanitize encodes ampersands which are required when used in a url.
  1759. return str_replace('&amp;', '&',
  1760. $this->sanitize($this->permanent_url,
  1761. SIMPLEPIE_CONSTRUCT_IRI));
  1762. }
  1763. }
  1764. else
  1765. {
  1766. if ($this->feed_url !== null)
  1767. {
  1768. return str_replace('&amp;', '&',
  1769. $this->sanitize($this->feed_url,
  1770. SIMPLEPIE_CONSTRUCT_IRI));
  1771. }
  1772. }
  1773. return null;
  1774. }
  1775. /**
  1776. * Get data for an feed-level element
  1777. *
  1778. * This method allows you to get access to ANY element/attribute that is a
  1779. * sub-element of the opening feed tag.
  1780. *
  1781. * The return value is an indexed array of elements matching the given
  1782. * namespace and tag name. Each element has `attribs`, `data` and `child`
  1783. * subkeys. For `attribs` and `child`, these contain namespace subkeys.
  1784. * `attribs` then has one level of associative name => value data (where
  1785. * `value` is a string) after the namespace. `child` has tag-indexed keys
  1786. * after the namespace, each member of which is an indexed array matching
  1787. * this same format.
  1788. *
  1789. * For example:
  1790. * <pre>
  1791. * // This is probably a bad example because we already support
  1792. * // <media:content> natively, but it shows you how to parse through
  1793. * // the nodes.
  1794. * $group = $item->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'group');
  1795. * $content = $group[0]['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content'];
  1796. * $file = $content[0]['attribs']['']['url'];
  1797. * echo $file;
  1798. * </pre>
  1799. *
  1800. * @since 1.0
  1801. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  1802. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  1803. * @param string $tag Tag name
  1804. * @return array
  1805. */
  1806. public function get_feed_tags($namespace, $tag)
  1807. {
  1808. $type = $this->get_type();
  1809. if ($type & SIMPLEPIE_TYPE_ATOM_10)
  1810. {
  1811. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag]))
  1812. {
  1813. return $this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag];
  1814. }
  1815. }
  1816. if ($type & SIMPLEPIE_TYPE_ATOM_03)
  1817. {
  1818. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag]))
  1819. {
  1820. return $this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag];
  1821. }
  1822. }
  1823. if ($type & SIMPLEPIE_TYPE_RSS_RDF)
  1824. {
  1825. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag]))
  1826. {
  1827. return $this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag];
  1828. }
  1829. }
  1830. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1831. {
  1832. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][$namespace][$tag]))
  1833. {
  1834. return $this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][$namespace][$tag];
  1835. }
  1836. }
  1837. return null;
  1838. }
  1839. /**
  1840. * Get data for an channel-level element
  1841. *
  1842. * This method allows you to get access to ANY element/attribute in the
  1843. * channel/header section of the feed.
  1844. *
  1845. * See {@see SimplePie::get_feed_tags()} for a description of the return value
  1846. *
  1847. * @since 1.0
  1848. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  1849. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  1850. * @param string $tag Tag name
  1851. * @return array
  1852. */
  1853. public function get_channel_tags($namespace, $tag)
  1854. {
  1855. $type = $this->get_type();
  1856. if ($type & SIMPLEPIE_TYPE_ATOM_ALL)
  1857. {
  1858. if ($return = $this->get_feed_tags($namespace, $tag))
  1859. {
  1860. return $return;
  1861. }
  1862. }
  1863. if ($type & SIMPLEPIE_TYPE_RSS_10)
  1864. {
  1865. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'channel'))
  1866. {
  1867. if (isset($channel[0]['child'][$namespace][$tag]))
  1868. {
  1869. return $channel[0]['child'][$namespace][$tag];
  1870. }
  1871. }
  1872. }
  1873. if ($type & SIMPLEPIE_TYPE_RSS_090)
  1874. {
  1875. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'channel'))
  1876. {
  1877. if (isset($channel[0]['child'][$namespace][$tag]))
  1878. {
  1879. return $channel[0]['child'][$namespace][$tag];
  1880. }
  1881. }
  1882. }
  1883. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1884. {
  1885. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'channel'))
  1886. {
  1887. if (isset($channel[0]['child'][$namespace][$tag]))
  1888. {
  1889. return $channel[0]['child'][$namespace][$tag];
  1890. }
  1891. }
  1892. }
  1893. return null;
  1894. }
  1895. /**
  1896. * Get data for an channel-level element
  1897. *
  1898. * This method allows you to get access to ANY element/attribute in the
  1899. * image/logo section of the feed.
  1900. *
  1901. * See {@see SimplePie::get_feed_tags()} for a description of the return value
  1902. *
  1903. * @since 1.0
  1904. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  1905. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  1906. * @param string $tag Tag name
  1907. * @return array
  1908. */
  1909. public function get_image_tags($namespace, $tag)
  1910. {
  1911. $type = $this->get_type();
  1912. if ($type & SIMPLEPIE_TYPE_RSS_10)
  1913. {
  1914. if ($image = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'image'))
  1915. {
  1916. if (isset($image[0]['child'][$namespace][$tag]))
  1917. {
  1918. return $image[0]['child'][$namespace][$tag];
  1919. }
  1920. }
  1921. }
  1922. if ($type & SIMPLEPIE_TYPE_RSS_090)
  1923. {
  1924. if ($image = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'image'))
  1925. {
  1926. if (isset($image[0]['child'][$namespace][$tag]))
  1927. {
  1928. return $image[0]['child'][$namespace][$tag];
  1929. }
  1930. }
  1931. }
  1932. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1933. {
  1934. if ($image = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'image'))
  1935. {
  1936. if (isset($image[0]['child'][$namespace][$tag]))
  1937. {
  1938. return $image[0]['child'][$namespace][$tag];
  1939. }
  1940. }
  1941. }
  1942. return null;
  1943. }
  1944. /**
  1945. * Get the base URL value from the feed
  1946. *
  1947. * Uses `<xml:base>` if available, otherwise uses the first link in the
  1948. * feed, or failing that, the URL of the feed itself.
  1949. *
  1950. * @see get_link
  1951. * @see subscribe_url
  1952. *
  1953. * @param array $element
  1954. * @return string
  1955. */
  1956. public function get_base($element = array())
  1957. {
  1958. if (!($this->get_type() & SIMPLEPIE_TYPE_RSS_SYNDICATION) && !empty($element['xml_base_explicit']) && isset($element['xml_base']))
  1959. {
  1960. return $element['xml_base'];
  1961. }
  1962. elseif ($this->get_link() !== null)
  1963. {
  1964. return $this->get_link();
  1965. }
  1966. return $this->subscribe_url();
  1967. }
  1968. /**
  1969. * Sanitize feed data
  1970. *
  1971. * @access private
  1972. * @see SimplePie_Sanitize::sanitize()
  1973. * @param string $data Data to sanitize
  1974. * @param int $type One of the SIMPLEPIE_CONSTRUCT_* constants
  1975. * @param string $base Base URL to resolve URLs against
  1976. * @return string Sanitized data
  1977. */
  1978. public function sanitize($data, $type, $base = '')
  1979. {
  1980. try
  1981. {
  1982. return $this->sanitize->sanitize($data, $type, $base);
  1983. }
  1984. catch (SimplePie_Exception $e)
  1985. {
  1986. if (!$this->enable_exceptions)
  1987. {
  1988. $this->error = $e->getMessage();
  1989. $this->registry->call('Misc', 'error', array($this->error, E_USER_WARNING, $e->getFile(), $e->getLine()));
  1990. return '';
  1991. }
  1992. throw $e;
  1993. }
  1994. }
  1995. /**
  1996. * Get the title of the feed
  1997. *
  1998. * Uses `<atom:title>`, `<title>` or `<dc:title>`
  1999. *
  2000. * @since 1.0 (previously called `get_feed_title` since 0.8)
  2001. * @return string|null
  2002. */
  2003. public function get_title()
  2004. {
  2005. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'title'))
  2006. {
  2007. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2008. }
  2009. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'title'))
  2010. {
  2011. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2012. }
  2013. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'title'))
  2014. {
  2015. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2016. }
  2017. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'title'))
  2018. {
  2019. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2020. }
  2021. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'title'))
  2022. {
  2023. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2024. }
  2025. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'title'))
  2026. {
  2027. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2028. }
  2029. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'title'))
  2030. {
  2031. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2032. }
  2033. return null;
  2034. }
  2035. /**
  2036. * Get a category for the feed
  2037. *
  2038. * @since Unknown
  2039. * @param int $key The category that you want to return. Remember that arrays begin with 0, not 1
  2040. * @return SimplePie_Category|null
  2041. */
  2042. public function get_category($key = 0)
  2043. {
  2044. $categories = $this->get_categories();
  2045. if (isset($categories[$key]))
  2046. {
  2047. return $categories[$key];
  2048. }
  2049. return null;
  2050. }
  2051. /**
  2052. * Get all categories for the feed
  2053. *
  2054. * Uses `<atom:category>`, `<category>` or `<dc:subject>`
  2055. *
  2056. * @since Unknown
  2057. * @return array|null List of {@see SimplePie_Category} objects
  2058. */
  2059. public function get_categories()
  2060. {
  2061. $categories = array();
  2062. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'category') as $category)
  2063. {
  2064. $term = null;
  2065. $scheme = null;
  2066. $label = null;
  2067. if (isset($category['attribs']['']['term']))
  2068. {
  2069. $term = $this->sanitize($category['attribs']['']['term'], SIMPLEPIE_CONSTRUCT_TEXT);
  2070. }
  2071. if (isset($category['attribs']['']['scheme']))
  2072. {
  2073. $scheme = $this->sanitize($category['attribs']['']['scheme'], SIMPLEPIE_CONSTRUCT_TEXT);
  2074. }
  2075. if (isset($category['attribs']['']['label']))
  2076. {
  2077. $label = $this->sanitize($category['attribs']['']['label'], SIMPLEPIE_CONSTRUCT_TEXT);
  2078. }
  2079. $categories[] = $this->registry->create('Category', array($term, $scheme, $label));
  2080. }
  2081. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'category') as $category)
  2082. {
  2083. // This is really the label, but keep this as the term also for BC.
  2084. // Label will also work on retrieving because that falls back to term.
  2085. $term = $this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2086. if (isset($category['attribs']['']['domain']))
  2087. {
  2088. $scheme = $this->sanitize($category['attribs']['']['domain'], SIMPLEPIE_CONSTRUCT_TEXT);
  2089. }
  2090. else
  2091. {
  2092. $scheme = null;
  2093. }
  2094. $categories[] = $this->registry->create('Category', array($term, $scheme, null));
  2095. }
  2096. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'subject') as $category)
  2097. {
  2098. $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  2099. }
  2100. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'subject') as $category)
  2101. {
  2102. $categories[] = $this->registry->create('Category', array($this->sanitize($category['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  2103. }
  2104. if (!empty($categories))
  2105. {
  2106. return array_unique($categories);
  2107. }
  2108. return null;
  2109. }
  2110. /**
  2111. * Get an author for the feed
  2112. *
  2113. * @since 1.1
  2114. * @param int $key The author that you want to return. Remember that arrays begin with 0, not 1
  2115. * @return SimplePie_Author|null
  2116. */
  2117. public function get_author($key = 0)
  2118. {
  2119. $authors = $this->get_authors();
  2120. if (isset($authors[$key]))
  2121. {
  2122. return $authors[$key];
  2123. }
  2124. return null;
  2125. }
  2126. /**
  2127. * Get all authors for the feed
  2128. *
  2129. * Uses `<atom:author>`, `<author>`, `<dc:creator>` or `<itunes:author>`
  2130. *
  2131. * @since 1.1
  2132. * @return array|null List of {@see SimplePie_Author} objects
  2133. */
  2134. public function get_authors()
  2135. {
  2136. $authors = array();
  2137. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author') as $author)
  2138. {
  2139. $name = null;
  2140. $uri = null;
  2141. $email = null;
  2142. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']))
  2143. {
  2144. $name = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2145. }
  2146. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']))
  2147. {
  2148. $uri = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]));
  2149. }
  2150. if (isset($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data']))
  2151. {
  2152. $email = $this->sanitize($author['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2153. }
  2154. if ($name !== null || $email !== null || $uri !== null)
  2155. {
  2156. $authors[] = $this->registry->create('Author', array($name, $uri, $email));
  2157. }
  2158. }
  2159. if ($author = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'author'))
  2160. {
  2161. $name = null;
  2162. $url = null;
  2163. $email = null;
  2164. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data']))
  2165. {
  2166. $name = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2167. }
  2168. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data']))
  2169. {
  2170. $url = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]));
  2171. }
  2172. if (isset($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data']))
  2173. {
  2174. $email = $this->sanitize($author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2175. }
  2176. if ($name !== null || $email !== null || $url !== null)
  2177. {
  2178. $authors[] = $this->registry->create('Author', array($name, $url, $email));
  2179. }
  2180. }
  2181. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'creator') as $author)
  2182. {
  2183. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  2184. }
  2185. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'creator') as $author)
  2186. {
  2187. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  2188. }
  2189. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'author') as $author)
  2190. {
  2191. $authors[] = $this->registry->create('Author', array($this->sanitize($author['data'], SIMPLEPIE_CONSTRUCT_TEXT), null, null));
  2192. }
  2193. if (!empty($authors))
  2194. {
  2195. return array_unique($authors);
  2196. }
  2197. return null;
  2198. }
  2199. /**
  2200. * Get a contributor for the feed
  2201. *
  2202. * @since 1.1
  2203. * @param int $key The contrbutor that you want to return. Remember that arrays begin with 0, not 1
  2204. * @return SimplePie_Author|null
  2205. */
  2206. public function get_contributor($key = 0)
  2207. {
  2208. $contributors = $this->get_contributors();
  2209. if (isset($contributors[$key]))
  2210. {
  2211. return $contributors[$key];
  2212. }
  2213. return null;
  2214. }
  2215. /**
  2216. * Get all contributors for the feed
  2217. *
  2218. * Uses `<atom:contributor>`
  2219. *
  2220. * @since 1.1
  2221. * @return array|null List of {@see SimplePie_Author} objects
  2222. */
  2223. public function get_contributors()
  2224. {
  2225. $contributors = array();
  2226. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'contributor') as $contributor)
  2227. {
  2228. $name = null;
  2229. $uri = null;
  2230. $email = null;
  2231. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']))
  2232. {
  2233. $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2234. }
  2235. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']))
  2236. {
  2237. $uri = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]));
  2238. }
  2239. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data']))
  2240. {
  2241. $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2242. }
  2243. if ($name !== null || $email !== null || $uri !== null)
  2244. {
  2245. $contributors[] = $this->registry->create('Author', array($name, $uri, $email));
  2246. }
  2247. }
  2248. foreach ((array) $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'contributor') as $contributor)
  2249. {
  2250. $name = null;
  2251. $url = null;
  2252. $email = null;
  2253. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data']))
  2254. {
  2255. $name = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['name'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2256. }
  2257. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data']))
  2258. {
  2259. $url = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['url'][0]));
  2260. }
  2261. if (isset($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data']))
  2262. {
  2263. $email = $this->sanitize($contributor['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['email'][0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2264. }
  2265. if ($name !== null || $email !== null || $url !== null)
  2266. {
  2267. $contributors[] = $this->registry->create('Author', array($name, $url, $email));
  2268. }
  2269. }
  2270. if (!empty($contributors))
  2271. {
  2272. return array_unique($contributors);
  2273. }
  2274. return null;
  2275. }
  2276. /**
  2277. * Get a single link for the feed
  2278. *
  2279. * @since 1.0 (previously called `get_feed_link` since Preview Release, `get_feed_permalink()` since 0.8)
  2280. * @param int $key The link that you want to return. Remember that arrays begin with 0, not 1
  2281. * @param string $rel The relationship of the link to return
  2282. * @return string|null Link URL
  2283. */
  2284. public function get_link($key = 0, $rel = 'alternate')
  2285. {
  2286. $links = $this->get_links($rel);
  2287. if (isset($links[$key]))
  2288. {
  2289. return $links[$key];
  2290. }
  2291. return null;
  2292. }
  2293. /**
  2294. * Get the permalink for the item
  2295. *
  2296. * Returns the first link available with a relationship of "alternate".
  2297. * Identical to {@see get_link()} with key 0
  2298. *
  2299. * @see get_link
  2300. * @since 1.0 (previously called `get_feed_link` since Preview Release, `get_feed_permalink()` since 0.8)
  2301. * @internal Added for parity between the parent-level and the item/entry-level.
  2302. * @return string|null Link URL
  2303. */
  2304. public function get_permalink()
  2305. {
  2306. return $this->get_link(0);
  2307. }
  2308. /**
  2309. * Get all links for the feed
  2310. *
  2311. * Uses `<atom:link>` or `<link>`
  2312. *
  2313. * @since Beta 2
  2314. * @param string $rel The relationship of links to return
  2315. * @return array|null Links found for the feed (strings)
  2316. */
  2317. public function get_links($rel = 'alternate')
  2318. {
  2319. if (!isset($this->data['links']))
  2320. {
  2321. $this->data['links'] = array();
  2322. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'link'))
  2323. {
  2324. foreach ($links as $link)
  2325. {
  2326. if (isset($link['attribs']['']['href']))
  2327. {
  2328. $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
  2329. $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  2330. }
  2331. }
  2332. }
  2333. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'link'))
  2334. {
  2335. foreach ($links as $link)
  2336. {
  2337. if (isset($link['attribs']['']['href']))
  2338. {
  2339. $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate';
  2340. $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($link));
  2341. }
  2342. }
  2343. }
  2344. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'link'))
  2345. {
  2346. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  2347. }
  2348. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'link'))
  2349. {
  2350. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  2351. }
  2352. if ($links = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'link'))
  2353. {
  2354. $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($links[0]));
  2355. }
  2356. $keys = array_keys($this->data['links']);
  2357. foreach ($keys as $key)
  2358. {
  2359. if ($this->registry->call('Misc', 'is_isegment_nz_nc', array($key)))
  2360. {
  2361. if (isset($this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]))
  2362. {
  2363. $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key]);
  2364. $this->data['links'][$key] =& $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key];
  2365. }
  2366. else
  2367. {
  2368. $this->data['links'][SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY . $key] =& $this->data['links'][$key];
  2369. }
  2370. }
  2371. elseif (substr($key, 0, 41) === SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY)
  2372. {
  2373. $this->data['links'][substr($key, 41)] =& $this->data['links'][$key];
  2374. }
  2375. $this->data['links'][$key] = array_unique($this->data['links'][$key]);
  2376. }
  2377. }
  2378. if (isset($this->data['headers']['link']) &&
  2379. preg_match('/<([^>]+)>; rel='.preg_quote($rel).'/',
  2380. $this->data['headers']['link'], $match))
  2381. {
  2382. return array($match[1]);
  2383. }
  2384. else if (isset($this->data['links'][$rel]))
  2385. {
  2386. return $this->data['links'][$rel];
  2387. }
  2388. return null;
  2389. }
  2390. public function get_all_discovered_feeds()
  2391. {
  2392. return $this->all_discovered_feeds;
  2393. }
  2394. /**
  2395. * Get the content for the item
  2396. *
  2397. * Uses `<atom:subtitle>`, `<atom:tagline>`, `<description>`,
  2398. * `<dc:description>`, `<itunes:summary>` or `<itunes:subtitle>`
  2399. *
  2400. * @since 1.0 (previously called `get_feed_description()` since 0.8)
  2401. * @return string|null
  2402. */
  2403. public function get_description()
  2404. {
  2405. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'subtitle'))
  2406. {
  2407. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2408. }
  2409. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'tagline'))
  2410. {
  2411. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2412. }
  2413. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'description'))
  2414. {
  2415. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2416. }
  2417. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'description'))
  2418. {
  2419. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  2420. }
  2421. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'description'))
  2422. {
  2423. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  2424. }
  2425. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'description'))
  2426. {
  2427. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2428. }
  2429. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'description'))
  2430. {
  2431. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2432. }
  2433. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'summary'))
  2434. {
  2435. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  2436. }
  2437. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'subtitle'))
  2438. {
  2439. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_HTML, $this->get_base($return[0]));
  2440. }
  2441. return null;
  2442. }
  2443. /**
  2444. * Get the copyright info for the feed
  2445. *
  2446. * Uses `<atom:rights>`, `<atom:copyright>` or `<dc:rights>`
  2447. *
  2448. * @since 1.0 (previously called `get_feed_copyright()` since 0.8)
  2449. * @return string|null
  2450. */
  2451. public function get_copyright()
  2452. {
  2453. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'rights'))
  2454. {
  2455. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2456. }
  2457. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'copyright'))
  2458. {
  2459. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  2460. }
  2461. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'copyright'))
  2462. {
  2463. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2464. }
  2465. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'rights'))
  2466. {
  2467. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2468. }
  2469. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'rights'))
  2470. {
  2471. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2472. }
  2473. return null;
  2474. }
  2475. /**
  2476. * Get the language for the feed
  2477. *
  2478. * Uses `<language>`, `<dc:language>`, or @xml_lang
  2479. *
  2480. * @since 1.0 (previously called `get_feed_language()` since 0.8)
  2481. * @return string|null
  2482. */
  2483. public function get_language()
  2484. {
  2485. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'language'))
  2486. {
  2487. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2488. }
  2489. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'language'))
  2490. {
  2491. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2492. }
  2493. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'language'))
  2494. {
  2495. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2496. }
  2497. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['xml_lang']))
  2498. {
  2499. return $this->sanitize($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  2500. }
  2501. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['xml_lang']))
  2502. {
  2503. return $this->sanitize($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  2504. }
  2505. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['xml_lang']))
  2506. {
  2507. return $this->sanitize($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['xml_lang'], SIMPLEPIE_CONSTRUCT_TEXT);
  2508. }
  2509. elseif (isset($this->data['headers']['content-language']))
  2510. {
  2511. return $this->sanitize($this->data['headers']['content-language'], SIMPLEPIE_CONSTRUCT_TEXT);
  2512. }
  2513. return null;
  2514. }
  2515. /**
  2516. * Get the latitude coordinates for the item
  2517. *
  2518. * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications
  2519. *
  2520. * Uses `<geo:lat>` or `<georss:point>`
  2521. *
  2522. * @since 1.0
  2523. * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo
  2524. * @link http://www.georss.org/ GeoRSS
  2525. * @return string|null
  2526. */
  2527. public function get_latitude()
  2528. {
  2529. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lat'))
  2530. {
  2531. return (float) $return[0]['data'];
  2532. }
  2533. elseif (($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
  2534. {
  2535. return (float) $match[1];
  2536. }
  2537. return null;
  2538. }
  2539. /**
  2540. * Get the longitude coordinates for the feed
  2541. *
  2542. * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications
  2543. *
  2544. * Uses `<geo:long>`, `<geo:lon>` or `<georss:point>`
  2545. *
  2546. * @since 1.0
  2547. * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo
  2548. * @link http://www.georss.org/ GeoRSS
  2549. * @return string|null
  2550. */
  2551. public function get_longitude()
  2552. {
  2553. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'long'))
  2554. {
  2555. return (float) $return[0]['data'];
  2556. }
  2557. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO, 'lon'))
  2558. {
  2559. return (float) $return[0]['data'];
  2560. }
  2561. elseif (($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match))
  2562. {
  2563. return (float) $match[2];
  2564. }
  2565. return null;
  2566. }
  2567. /**
  2568. * Get the feed logo's title
  2569. *
  2570. * RSS 0.9.0, 1.0 and 2.0 feeds are allowed to have a "feed logo" title.
  2571. *
  2572. * Uses `<image><title>` or `<image><dc:title>`
  2573. *
  2574. * @return string|null
  2575. */
  2576. public function get_image_title()
  2577. {
  2578. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'title'))
  2579. {
  2580. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2581. }
  2582. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'title'))
  2583. {
  2584. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2585. }
  2586. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'title'))
  2587. {
  2588. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2589. }
  2590. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_DC_11, 'title'))
  2591. {
  2592. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2593. }
  2594. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_DC_10, 'title'))
  2595. {
  2596. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  2597. }
  2598. return null;
  2599. }
  2600. /**
  2601. * Get the feed logo's URL
  2602. *
  2603. * RSS 0.9.0, 2.0, Atom 1.0, and feeds with iTunes RSS tags are allowed to
  2604. * have a "feed logo" URL. This points directly to the image itself.
  2605. *
  2606. * Uses `<itunes:image>`, `<atom:logo>`, `<atom:icon>`,
  2607. * `<image><title>` or `<image><dc:title>`
  2608. *
  2609. * @return string|null
  2610. */
  2611. public function get_image_url()
  2612. {
  2613. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES, 'image'))
  2614. {
  2615. return $this->sanitize($return[0]['attribs']['']['href'], SIMPLEPIE_CONSTRUCT_IRI);
  2616. }
  2617. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'logo'))
  2618. {
  2619. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2620. }
  2621. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'icon'))
  2622. {
  2623. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2624. }
  2625. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'url'))
  2626. {
  2627. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2628. }
  2629. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'url'))
  2630. {
  2631. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2632. }
  2633. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'url'))
  2634. {
  2635. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2636. }
  2637. return null;
  2638. }
  2639. /**
  2640. * Get the feed logo's link
  2641. *
  2642. * RSS 0.9.0, 1.0 and 2.0 feeds are allowed to have a "feed logo" link. This
  2643. * points to a human-readable page that the image should link to.
  2644. *
  2645. * Uses `<itunes:image>`, `<atom:logo>`, `<atom:icon>`,
  2646. * `<image><title>` or `<image><dc:title>`
  2647. *
  2648. * @return string|null
  2649. */
  2650. public function get_image_link()
  2651. {
  2652. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'link'))
  2653. {
  2654. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2655. }
  2656. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'link'))
  2657. {
  2658. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2659. }
  2660. elseif ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'link'))
  2661. {
  2662. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_IRI, $this->get_base($return[0]));
  2663. }
  2664. return null;
  2665. }
  2666. /**
  2667. * Get the feed logo's link
  2668. *
  2669. * RSS 2.0 feeds are allowed to have a "feed logo" width.
  2670. *
  2671. * Uses `<image><width>` or defaults to 88.0 if no width is specified and
  2672. * the feed is an RSS 2.0 feed.
  2673. *
  2674. * @return int|float|null
  2675. */
  2676. public function get_image_width()
  2677. {
  2678. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'width'))
  2679. {
  2680. return round($return[0]['data']);
  2681. }
  2682. elseif ($this->get_type() & SIMPLEPIE_TYPE_RSS_SYNDICATION && $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'url'))
  2683. {
  2684. return 88.0;
  2685. }
  2686. return null;
  2687. }
  2688. /**
  2689. * Get the feed logo's height
  2690. *
  2691. * RSS 2.0 feeds are allowed to have a "feed logo" height.
  2692. *
  2693. * Uses `<image><height>` or defaults to 31.0 if no height is specified and
  2694. * the feed is an RSS 2.0 feed.
  2695. *
  2696. * @return int|float|null
  2697. */
  2698. public function get_image_height()
  2699. {
  2700. if ($return = $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'height'))
  2701. {
  2702. return round($return[0]['data']);
  2703. }
  2704. elseif ($this->get_type() & SIMPLEPIE_TYPE_RSS_SYNDICATION && $this->get_image_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'url'))
  2705. {
  2706. return 31.0;
  2707. }
  2708. return null;
  2709. }
  2710. /**
  2711. * Get the number of items in the feed
  2712. *
  2713. * This is well-suited for {@link http://php.net/for for()} loops with
  2714. * {@see get_item()}
  2715. *
  2716. * @param int $max Maximum value to return. 0 for no limit
  2717. * @return int Number of items in the feed
  2718. */
  2719. public function get_item_quantity($max = 0)
  2720. {
  2721. $max = (int) $max;
  2722. $qty = count($this->get_items());
  2723. if ($max === 0)
  2724. {
  2725. return $qty;
  2726. }
  2727. return ($qty > $max) ? $max : $qty;
  2728. }
  2729. /**
  2730. * Get a single item from the feed
  2731. *
  2732. * This is better suited for {@link http://php.net/for for()} loops, whereas
  2733. * {@see get_items()} is better suited for
  2734. * {@link http://php.net/foreach foreach()} loops.
  2735. *
  2736. * @see get_item_quantity()
  2737. * @since Beta 2
  2738. * @param int $key The item that you want to return. Remember that arrays begin with 0, not 1
  2739. * @return SimplePie_Item|null
  2740. */
  2741. public function get_item($key = 0)
  2742. {
  2743. $items = $this->get_items();
  2744. if (isset($items[$key]))
  2745. {
  2746. return $items[$key];
  2747. }
  2748. return null;
  2749. }
  2750. /**
  2751. * Get all items from the feed
  2752. *
  2753. * This is better suited for {@link http://php.net/for for()} loops, whereas
  2754. * {@see get_items()} is better suited for
  2755. * {@link http://php.net/foreach foreach()} loops.
  2756. *
  2757. * @see get_item_quantity
  2758. * @since Beta 2
  2759. * @param int $start Index to start at
  2760. * @param int $end Number of items to return. 0 for all items after `$start`
  2761. * @return SimplePie_Item[]|null List of {@see SimplePie_Item} objects
  2762. */
  2763. public function get_items($start = 0, $end = 0)
  2764. {
  2765. if (!isset($this->data['items']))
  2766. {
  2767. if (!empty($this->multifeed_objects))
  2768. {
  2769. $this->data['items'] = SimplePie::merge_items($this->multifeed_objects, $start, $end, $this->item_limit);
  2770. if (empty($this->data['items']))
  2771. {
  2772. return array();
  2773. }
  2774. return $this->data['items'];
  2775. }
  2776. $this->data['items'] = array();
  2777. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'entry'))
  2778. {
  2779. $keys = array_keys($items);
  2780. foreach ($keys as $key)
  2781. {
  2782. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2783. }
  2784. }
  2785. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'entry'))
  2786. {
  2787. $keys = array_keys($items);
  2788. foreach ($keys as $key)
  2789. {
  2790. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2791. }
  2792. }
  2793. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'item'))
  2794. {
  2795. $keys = array_keys($items);
  2796. foreach ($keys as $key)
  2797. {
  2798. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2799. }
  2800. }
  2801. if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'item'))
  2802. {
  2803. $keys = array_keys($items);
  2804. foreach ($keys as $key)
  2805. {
  2806. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2807. }
  2808. }
  2809. if ($items = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'item'))
  2810. {
  2811. $keys = array_keys($items);
  2812. foreach ($keys as $key)
  2813. {
  2814. $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
  2815. }
  2816. }
  2817. }
  2818. if (empty($this->data['items']))
  2819. {
  2820. return array();
  2821. }
  2822. if ($this->order_by_date)
  2823. {
  2824. if (!isset($this->data['ordered_items']))
  2825. {
  2826. $this->data['ordered_items'] = $this->data['items'];
  2827. usort($this->data['ordered_items'], array(get_class($this), 'sort_items'));
  2828. }
  2829. $items = $this->data['ordered_items'];
  2830. }
  2831. else
  2832. {
  2833. $items = $this->data['items'];
  2834. }
  2835. // Slice the data as desired
  2836. if ($end === 0)
  2837. {
  2838. return array_slice($items, $start);
  2839. }
  2840. return array_slice($items, $start, $end);
  2841. }
  2842. /**
  2843. * Set the favicon handler
  2844. *
  2845. * @deprecated Use your own favicon handling instead
  2846. */
  2847. public function set_favicon_handler($page = false, $qs = 'i')
  2848. {
  2849. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2850. trigger_error('Favicon handling has been removed, please use your own handling', $level);
  2851. return false;
  2852. }
  2853. /**
  2854. * Get the favicon for the current feed
  2855. *
  2856. * @deprecated Use your own favicon handling instead
  2857. */
  2858. public function get_favicon()
  2859. {
  2860. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2861. trigger_error('Favicon handling has been removed, please use your own handling', $level);
  2862. if (($url = $this->get_link()) !== null)
  2863. {
  2864. return 'https://www.google.com/s2/favicons?domain=' . urlencode($url);
  2865. }
  2866. return false;
  2867. }
  2868. /**
  2869. * Magic method handler
  2870. *
  2871. * @param string $method Method name
  2872. * @param array $args Arguments to the method
  2873. * @return mixed
  2874. */
  2875. public function __call($method, $args)
  2876. {
  2877. if (strpos($method, 'subscribe_') === 0)
  2878. {
  2879. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2880. trigger_error('subscribe_*() has been deprecated, implement the callback yourself', $level);
  2881. return '';
  2882. }
  2883. if ($method === 'enable_xml_dump')
  2884. {
  2885. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  2886. trigger_error('enable_xml_dump() has been deprecated, use get_raw_data() instead', $level);
  2887. return false;
  2888. }
  2889. $class = get_class($this);
  2890. $trace = debug_backtrace();
  2891. $file = $trace[0]['file'];
  2892. $line = $trace[0]['line'];
  2893. trigger_error("Call to undefined method $class::$method() in $file on line $line", E_USER_ERROR);
  2894. }
  2895. /**
  2896. * Sorting callback for items
  2897. *
  2898. * @access private
  2899. * @param SimplePie $a
  2900. * @param SimplePie $b
  2901. * @return boolean
  2902. */
  2903. public static function sort_items($a, $b)
  2904. {
  2905. $a_date = $a->get_date('U');
  2906. $b_date = $b->get_date('U');
  2907. if ($a_date && $b_date) {
  2908. return $a_date > $b_date ? -1 : 1;
  2909. }
  2910. // Sort items without dates to the top.
  2911. if ($a_date) {
  2912. return 1;
  2913. }
  2914. if ($b_date) {
  2915. return -1;
  2916. }
  2917. return 0;
  2918. }
  2919. /**
  2920. * Merge items from several feeds into one
  2921. *
  2922. * If you're merging multiple feeds together, they need to all have dates
  2923. * for the items or else SimplePie will refuse to sort them.
  2924. *
  2925. * @link http://simplepie.org/wiki/tutorial/sort_multiple_feeds_by_time_and_date#if_feeds_require_separate_per-feed_settings
  2926. * @param array $urls List of SimplePie feed objects to merge
  2927. * @param int $start Starting item
  2928. * @param int $end Number of items to return
  2929. * @param int $limit Maximum number of items per feed
  2930. * @return array
  2931. */
  2932. public static function merge_items($urls, $start = 0, $end = 0, $limit = 0)
  2933. {
  2934. if (is_array($urls) && sizeof($urls) > 0)
  2935. {
  2936. $items = array();
  2937. foreach ($urls as $arg)
  2938. {
  2939. if ($arg instanceof SimplePie)
  2940. {
  2941. $items = array_merge($items, $arg->get_items(0, $limit));
  2942. }
  2943. else
  2944. {
  2945. trigger_error('Arguments must be SimplePie objects', E_USER_WARNING);
  2946. }
  2947. }
  2948. usort($items, array(get_class($urls[0]), 'sort_items'));
  2949. if ($end === 0)
  2950. {
  2951. return array_slice($items, $start);
  2952. }
  2953. return array_slice($items, $start, $end);
  2954. }
  2955. trigger_error('Cannot merge zero SimplePie objects', E_USER_WARNING);
  2956. return array();
  2957. }
  2958. /**
  2959. * Store PubSubHubbub links as headers
  2960. *
  2961. * There is no way to find PuSH links in the body of a microformats feed,
  2962. * so they are added to the headers when found, to be used later by get_links.
  2963. * @param SimplePie_File $file
  2964. * @param string $hub
  2965. * @param string $self
  2966. */
  2967. private function store_links(&$file, $hub, $self) {
  2968. if (isset($file->headers['link']['hub']) ||
  2969. (isset($file->headers['link']) &&
  2970. preg_match('/rel=hub/', $file->headers['link'])))
  2971. {
  2972. return;
  2973. }
  2974. if ($hub)
  2975. {
  2976. if (isset($file->headers['link']))
  2977. {
  2978. if ($file->headers['link'] !== '')
  2979. {
  2980. $file->headers['link'] = ', ';
  2981. }
  2982. }
  2983. else
  2984. {
  2985. $file->headers['link'] = '';
  2986. }
  2987. $file->headers['link'] .= '<'.$hub.'>; rel=hub';
  2988. if ($self)
  2989. {
  2990. $file->headers['link'] .= ', <'.$self.'>; rel=self';
  2991. }
  2992. }
  2993. }
  2994. }
  2995. endif;