You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

3094 lines
87 KiB

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