PageRenderTime 75ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simplepie/library/SimplePie.php

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