PageRenderTime 76ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-simplepie.php

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