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

/library/SimplePie.php

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