PageRenderTime 87ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 2ms

/inc/app/news/lib/simplepie.php

https://github.com/lux/sitellite
PHP | 17595 lines | 13661 code | 795 blank | 3139 comment | 1080 complexity | eeb028a5a389ce541236cf806af6b6b3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  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
  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');
  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', '20120707113013');
  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. trigger_error('Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_location() directly.');
  537. }
  538. }
  539. /**
  540. * Used for converting object to a string
  541. */
  542. public function __toString()
  543. {
  544. return md5(serialize($this->data));
  545. }
  546. /**
  547. * Remove items that link back to this before destroying this object
  548. */
  549. public function __destruct()
  550. {
  551. if ((version_compare(PHP_VERSION, '5.3', '<') || !gc_enabled()) && !ini_get('zend.ze1_compatibility_mode'))
  552. {
  553. if (!empty($this->data['items']))
  554. {
  555. foreach ($this->data['items'] as $item)
  556. {
  557. $item->__destruct();
  558. }
  559. unset($item, $this->data['items']);
  560. }
  561. if (!empty($this->data['ordered_items']))
  562. {
  563. foreach ($this->data['ordered_items'] as $item)
  564. {
  565. $item->__destruct();
  566. }
  567. unset($item, $this->data['ordered_items']);
  568. }
  569. }
  570. }
  571. /**
  572. * Force the given data/URL to be treated as a feed
  573. *
  574. * This tells SimplePie to ignore the content-type provided by the server.
  575. * Be careful when using this option, as it will also disable autodiscovery.
  576. *
  577. * @since 1.1
  578. * @param bool $enable Force the given data/URL to be treated as a feed
  579. */
  580. public function force_feed($enable = false)
  581. {
  582. $this->force_feed = (bool) $enable;
  583. }
  584. /**
  585. * Set the URL of the feed you want to parse
  586. *
  587. * This allows you to enter the URL of the feed you want to parse, or the
  588. * website you want to try to use auto-discovery on. This takes priority
  589. * over any set raw data.
  590. *
  591. * You can set multiple feeds to mash together by passing an array instead
  592. * of a string for the $url. Remember that with each additional feed comes
  593. * additional processing and resources.
  594. *
  595. * @since 1.0 Preview Release
  596. * @see set_raw_data()
  597. * @param string|array $url This is the URL (or array of URLs) that you want to parse.
  598. */
  599. public function set_feed_url($url)
  600. {
  601. $this->multifeed_url = array();
  602. if (is_array($url))
  603. {
  604. foreach ($url as $value)
  605. {
  606. $this->multifeed_url[] = $this->registry->call('Misc', 'fix_protocol', array($value, 1));
  607. }
  608. }
  609. else
  610. {
  611. $this->feed_url = $this->registry->call('Misc', 'fix_protocol', array($url, 1));
  612. }
  613. }
  614. /**
  615. * Set an instance of {@see SimplePie_File} to use as a feed
  616. *
  617. * @param SimplePie_File &$file
  618. * @return bool True on success, false on failure
  619. */
  620. public function set_file(&$file)
  621. {
  622. if ($file instanceof SimplePie_File)
  623. {
  624. $this->feed_url = $file->url;
  625. $this->file =& $file;
  626. return true;
  627. }
  628. return false;
  629. }
  630. /**
  631. * Set the raw XML data to parse
  632. *
  633. * Allows you to use a string of RSS/Atom data instead of a remote feed.
  634. *
  635. * If you have a feed available as a string in PHP, you can tell SimplePie
  636. * to parse that data string instead of a remote feed. Any set feed URL
  637. * takes precedence.
  638. *
  639. * @since 1.0 Beta 3
  640. * @param string $data RSS or Atom data as a string.
  641. * @see set_feed_url()
  642. */
  643. public function set_raw_data($data)
  644. {
  645. $this->raw_data = $data;
  646. }
  647. /**
  648. * Set the the default timeout for fetching remote feeds
  649. *
  650. * This allows you to change the maximum time the feed's server to respond
  651. * and send the feed back.
  652. *
  653. * @since 1.0 Beta 3
  654. * @param int $timeout The maximum number of seconds to spend waiting to retrieve a feed.
  655. */
  656. public function set_timeout($timeout = 10)
  657. {
  658. $this->timeout = (int) $timeout;
  659. }
  660. /**
  661. * Force SimplePie to use fsockopen() instead of cURL
  662. *
  663. * @since 1.0 Beta 3
  664. * @param bool $enable Force fsockopen() to be used
  665. */
  666. public function force_fsockopen($enable = false)
  667. {
  668. $this->force_fsockopen = (bool) $enable;
  669. }
  670. /**
  671. * Enable/disable caching in SimplePie.
  672. *
  673. * This option allows you to disable caching all-together in SimplePie.
  674. * However, disabling the cache can lead to longer load times.
  675. *
  676. * @since 1.0 Preview Release
  677. * @param bool $enable Enable caching
  678. */
  679. public function enable_cache($enable = true)
  680. {
  681. $this->cache = (bool) $enable;
  682. }
  683. /**
  684. * Set the length of time (in seconds) that the contents of a feed will be
  685. * cached
  686. *
  687. * @param int $seconds The feed content cache duration
  688. */
  689. public function set_cache_duration($seconds = 3600)
  690. {
  691. $this->cache_duration = (int) $seconds;
  692. }
  693. /**
  694. * Set the length of time (in seconds) that the autodiscovered feed URL will
  695. * be cached
  696. *
  697. * @param int $seconds The autodiscovered feed URL cache duration.
  698. */
  699. public function set_autodiscovery_cache_duration($seconds = 604800)
  700. {
  701. $this->autodiscovery_cache_duration = (int) $seconds;
  702. }
  703. /**
  704. * Set the file system location where the cached files should be stored
  705. *
  706. * @param string $location The file system location.
  707. */
  708. public function set_cache_location($location = './cache')
  709. {
  710. $this->cache_location = (string) $location;
  711. }
  712. /**
  713. * Set whether feed items should be sorted into reverse chronological order
  714. *
  715. * @param bool $enable Sort as reverse chronological order.
  716. */
  717. public function enable_order_by_date($enable = true)
  718. {
  719. $this->order_by_date = (bool) $enable;
  720. }
  721. /**
  722. * Set the character encoding used to parse the feed
  723. *
  724. * This overrides the encoding reported by the feed, however it will fall
  725. * back to the normal encoding detection if the override fails
  726. *
  727. * @param string $encoding Character encoding
  728. */
  729. public function set_input_encoding($encoding = false)
  730. {
  731. if ($encoding)
  732. {
  733. $this->input_encoding = (string) $encoding;
  734. }
  735. else
  736. {
  737. $this->input_encoding = false;
  738. }
  739. }
  740. /**
  741. * Set how much feed autodiscovery to do
  742. *
  743. * @see SIMPLEPIE_LOCATOR_NONE
  744. * @see SIMPLEPIE_LOCATOR_AUTODISCOVERY
  745. * @see SIMPLEPIE_LOCATOR_LOCAL_EXTENSION
  746. * @see SIMPLEPIE_LOCATOR_LOCAL_BODY
  747. * @see SIMPLEPIE_LOCATOR_REMOTE_EXTENSION
  748. * @see SIMPLEPIE_LOCATOR_REMOTE_BODY
  749. * @see SIMPLEPIE_LOCATOR_ALL
  750. * @param int $level Feed Autodiscovery Level (level can be a combination of the above constants, see bitwise OR operator)
  751. */
  752. public function set_autodiscovery_level($level = SIMPLEPIE_LOCATOR_ALL)
  753. {
  754. $this->autodiscovery = (int) $level;
  755. }
  756. /**
  757. * Get the class registry
  758. *
  759. * Use this to override SimplePie's default classes
  760. * @see SimplePie_Registry
  761. * @return SimplePie_Registry
  762. */
  763. public function &get_registry()
  764. {
  765. return $this->registry;
  766. }
  767. /**#@+
  768. * Useful when you are overloading or extending SimplePie's default classes.
  769. *
  770. * @deprecated Use {@see get_registry()} instead
  771. * @link http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.extends PHP5 extends documentation
  772. * @param string $class Name of custom class
  773. * @return boolean True on success, false otherwise
  774. */
  775. /**
  776. * Set which class SimplePie uses for caching
  777. */
  778. public function set_cache_class($class = 'SimplePie_Cache')
  779. {
  780. return $this->registry->register('Cache', $class, true);
  781. }
  782. /**
  783. * Set which class SimplePie uses for auto-discovery
  784. */
  785. public function set_locator_class($class = 'SimplePie_Locator')
  786. {
  787. return $this->registry->register('Locator', $class, true);
  788. }
  789. /**
  790. * Set which class SimplePie uses for XML parsing
  791. */
  792. public function set_parser_class($class = 'SimplePie_Parser')
  793. {
  794. return $this->registry->register('Parser', $class, true);
  795. }
  796. /**
  797. * Set which class SimplePie uses for remote file fetching
  798. */
  799. public function set_file_class($class = 'SimplePie_File')
  800. {
  801. return $this->registry->register('File', $class, true);
  802. }
  803. /**
  804. * Set which class SimplePie uses for data sanitization
  805. */
  806. public function set_sanitize_class($class = 'SimplePie_Sanitize')
  807. {
  808. return $this->registry->register('Sanitize', $class, true);
  809. }
  810. /**
  811. * Set which class SimplePie uses for handling feed items
  812. */
  813. public function set_item_class($class = 'SimplePie_Item')
  814. {
  815. return $this->registry->register('Item', $class, true);
  816. }
  817. /**
  818. * Set which class SimplePie uses for handling author data
  819. */
  820. public function set_author_class($class = 'SimplePie_Author')
  821. {
  822. return $this->registry->register('Author', $class, true);
  823. }
  824. /**
  825. * Set which class SimplePie uses for handling category data
  826. */
  827. public function set_category_class($class = 'SimplePie_Category')
  828. {
  829. return $this->registry->register('Category', $class, true);
  830. }
  831. /**
  832. * Set which class SimplePie uses for feed enclosures
  833. */
  834. public function set_enclosure_class($class = 'SimplePie_Enclosure')
  835. {
  836. return $this->registry->register('Enclosure', $class, true);
  837. }
  838. /**
  839. * Set which class SimplePie uses for `<media:text>` captions
  840. */
  841. public function set_caption_class($class = 'SimplePie_Caption')
  842. {
  843. return $this->registry->register('Caption', $class, true);
  844. }
  845. /**
  846. * Set which class SimplePie uses for `<media:copyright>`
  847. */
  848. public function set_copyright_class($class = 'SimplePie_Copyright')
  849. {
  850. return $this->registry->register('Copyright', $class, true);
  851. }
  852. /**
  853. * Set which class SimplePie uses for `<media:credit>`
  854. */
  855. public function set_credit_class($class = 'SimplePie_Credit')
  856. {
  857. return $this->registry->register('Credit', $class, true);
  858. }
  859. /**
  860. * Set which class SimplePie uses for `<media:rating>`
  861. */
  862. public function set_rating_class($class = 'SimplePie_Rating')
  863. {
  864. return $this->registry->register('Rating', $class, true);
  865. }
  866. /**
  867. * Set which class SimplePie uses for `<media:restriction>`
  868. */
  869. public function set_restriction_class($class = 'SimplePie_Restriction')
  870. {
  871. return $this->registry->register('Restriction', $class, true);
  872. }
  873. /**
  874. * Set which class SimplePie uses for content-type sniffing
  875. */
  876. public function set_content_type_sniffer_class($class = 'SimplePie_Content_Type_Sniffer')
  877. {
  878. return $this->registry->register('Content_Type_Sniffer', $class, true);
  879. }
  880. /**
  881. * Set which class SimplePie uses item sources
  882. */
  883. public function set_source_class($class = 'SimplePie_Source')
  884. {
  885. return $this->registry->register('Source', $class, true);
  886. }
  887. /**#@-*/
  888. /**
  889. * Set the user agent string
  890. *
  891. * @param string $ua New user agent string.
  892. */
  893. public function set_useragent($ua = SIMPLEPIE_USERAGENT)
  894. {
  895. $this->useragent = (string) $ua;
  896. }
  897. /**
  898. * Set callback function to create cache filename with
  899. *
  900. * @param mixed $function Callback function
  901. */
  902. public function set_cache_name_function($function = 'md5')
  903. {
  904. if (is_callable($function))
  905. {
  906. $this->cache_name_function = $function;
  907. }
  908. }
  909. /**
  910. * Set options to make SP as fast as possible
  911. *
  912. * Forgoes a substantial amount of data sanitization in favor of speed. This
  913. * turns SimplePie into a dumb parser of feeds.
  914. *
  915. * @param bool $set Whether to set them or not
  916. */
  917. public function set_stupidly_fast($set = false)
  918. {
  919. if ($set)
  920. {
  921. $this->enable_order_by_date(false);
  922. $this->remove_div(false);
  923. $this->strip_comments(false);
  924. $this->strip_htmltags(false);
  925. $this->strip_attributes(false);
  926. $this->set_image_handler(false);
  927. }
  928. }
  929. /**
  930. * Set maximum number of feeds to check with autodiscovery
  931. *
  932. * @param int $max Maximum number of feeds to check
  933. */
  934. public function set_max_checked_feeds($max = 10)
  935. {
  936. $this->max_checked_feeds = (int) $max;
  937. }
  938. public function remove_div($enable = true)
  939. {
  940. $this->sanitize->remove_div($enable);
  941. }
  942. public function strip_htmltags($tags = '', $encode = null)
  943. {
  944. if ($tags === '')
  945. {
  946. $tags = $this->strip_htmltags;
  947. }
  948. $this->sanitize->strip_htmltags($tags);
  949. if ($encode !== null)
  950. {
  951. $this->sanitize->encode_instead_of_strip($tags);
  952. }
  953. }
  954. public function encode_instead_of_strip($enable = true)
  955. {
  956. $this->sanitize->encode_instead_of_strip($enable);
  957. }
  958. public function strip_attributes($attribs = '')
  959. {
  960. if ($attribs === '')
  961. {
  962. $attribs = $this->strip_attributes;
  963. }
  964. $this->sanitize->strip_attributes($attribs);
  965. }
  966. /**
  967. * Set the output encoding
  968. *
  969. * Allows you to override SimplePie's output to match that of your webpage.
  970. * This is useful for times when your webpages are not being served as
  971. * UTF-8. This setting will be obeyed by {@see handle_content_type()}, and
  972. * is similar to {@see set_input_encoding()}.
  973. *
  974. * It should be noted, however, that not all character encodings can support
  975. * all characters. If your page is being served as ISO-8859-1 and you try
  976. * to display a Japanese feed, you'll likely see garbled characters.
  977. * Because of this, it is highly recommended to ensure that your webpages
  978. * are served as UTF-8.
  979. *
  980. * The number of supported character encodings depends on whether your web
  981. * host supports {@link http://php.net/mbstring mbstring},
  982. * {@link http://php.net/iconv iconv}, or both. See
  983. * {@link http://simplepie.org/wiki/faq/Supported_Character_Encodings} for
  984. * more information.
  985. *
  986. * @param string $encoding
  987. */
  988. public function set_output_encoding($encoding = 'UTF-8')
  989. {
  990. $this->sanitize->set_output_encoding($encoding);
  991. }
  992. public function strip_comments($strip = false)
  993. {
  994. $this->sanitize->strip_comments($strip);
  995. }
  996. /**
  997. * Set element/attribute key/value pairs of HTML attributes
  998. * containing URLs that need to be resolved relative to the feed
  999. *
  1000. * Defaults to |a|@href, |area|@href, |blockquote|@cite, |del|@cite,
  1001. * |form|@action, |img|@longdesc, |img|@src, |input|@src, |ins|@cite,
  1002. * |q|@cite
  1003. *
  1004. * @since 1.0
  1005. * @param array|null $element_attribute Element/attribute key/value pairs, null for default
  1006. */
  1007. public function set_url_replacements($element_attribute = null)
  1008. {
  1009. $this->sanitize->set_url_replacements($element_attribute);
  1010. }
  1011. /**
  1012. * Set the handler to enable the display of cached images.
  1013. *
  1014. * @param str $page Web-accessible path to the handler_image.php file.
  1015. * @param str $qs The query string that the value should be passed to.
  1016. */
  1017. public function set_image_handler($page = false, $qs = 'i')
  1018. {
  1019. if ($page !== false)
  1020. {
  1021. $this->sanitize->set_image_handler($page . '?' . $qs . '=');
  1022. }
  1023. else
  1024. {
  1025. $this->image_handler = '';
  1026. }
  1027. }
  1028. /**
  1029. * Set the limit for items returned per-feed with multifeeds
  1030. *
  1031. * @param integer $limit The maximum number of items to return.
  1032. */
  1033. public function set_item_limit($limit = 0)
  1034. {
  1035. $this->item_limit = (int) $limit;
  1036. }
  1037. /**
  1038. * Initialize the feed object
  1039. *
  1040. * This is what makes everything happen. Period. This is where all of the
  1041. * configuration options get processed, feeds are fetched, cached, and
  1042. * parsed, and all of that other good stuff.
  1043. *
  1044. * @return boolean True if successful, false otherwise
  1045. */
  1046. public function init()
  1047. {
  1048. // Check absolute bare minimum requirements.
  1049. if (!extension_loaded('xml') || !extension_loaded('pcre'))
  1050. {
  1051. return false;
  1052. }
  1053. // 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.
  1054. elseif (!extension_loaded('xmlreader'))
  1055. {
  1056. static $xml_is_sane = null;
  1057. if ($xml_is_sane === null)
  1058. {
  1059. $parser_check = xml_parser_create();
  1060. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  1061. xml_parser_free($parser_check);
  1062. $xml_is_sane = isset($values[0]['value']);
  1063. }
  1064. if (!$xml_is_sane)
  1065. {
  1066. return false;
  1067. }
  1068. }
  1069. if (method_exists($this->sanitize, 'set_registry'))
  1070. {
  1071. $this->sanitize->set_registry($this->registry);
  1072. }
  1073. // Pass whatever was set with config options over to the sanitizer.
  1074. // Pass the classes in for legacy support; new classes should use the registry instead
  1075. $this->sanitize->pass_cache_data($this->cache, $this->cache_location, $this->cache_name_function, $this->registry->get_class('Cache'));
  1076. $this->sanitize->pass_file_data($this->registry->get_class('File'), $this->timeout, $this->useragent, $this->force_fsockopen);
  1077. if (!empty($this->multifeed_url))
  1078. {
  1079. $i = 0;
  1080. $success = 0;
  1081. $this->multifeed_objects = array();
  1082. $this->error = array();
  1083. foreach ($this->multifeed_url as $url)
  1084. {
  1085. $this->multifeed_objects[$i] = clone $this;
  1086. $this->multifeed_objects[$i]->set_feed_url($url);
  1087. $single_success = $this->multifeed_objects[$i]->init();
  1088. $success |= $single_success;
  1089. if (!$single_success)
  1090. {
  1091. $this->error[$i] = $this->multifeed_objects[$i]->error();
  1092. }
  1093. $i++;
  1094. }
  1095. return (bool) $success;
  1096. }
  1097. elseif ($this->feed_url === null && $this->raw_data === null)
  1098. {
  1099. return false;
  1100. }
  1101. $this->error = null;
  1102. $this->data = array();
  1103. $this->multifeed_objects = array();
  1104. $cache = false;
  1105. if ($this->feed_url !== null)
  1106. {
  1107. $parsed_feed_url = $this->registry->call('Misc', 'parse_url', array($this->feed_url));
  1108. // Decide whether to enable caching
  1109. if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1110. {
  1111. $cache = $this->registry->call('Cache', 'create', array($this->cache_location, call_user_func($this->cache_name_function, $this->feed_url), 'spc'));
  1112. }
  1113. // Fetch the data via SimplePie_File into $this->raw_data
  1114. if (($fetched = $this->fetch_data($cache)) === true)
  1115. {
  1116. return true;
  1117. }
  1118. elseif ($fetched === false) {
  1119. return false;
  1120. }
  1121. list($headers, $sniffed) = $fetched;
  1122. }
  1123. // Set up array of possible encodings
  1124. $encodings = array();
  1125. // First check to see if input has been overridden.
  1126. if ($this->input_encoding !== false)
  1127. {
  1128. $encodings[] = $this->input_encoding;
  1129. }
  1130. $application_types = array('application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity');
  1131. $text_types = array('text/xml', 'text/xml-external-parsed-entity');
  1132. // RFC 3023 (only applies to sniffed content)
  1133. if (isset($sniffed))
  1134. {
  1135. if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml')
  1136. {
  1137. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1138. {
  1139. $encodings[] = strtoupper($charset[1]);
  1140. }
  1141. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1142. $encodings[] = 'UTF-8';
  1143. }
  1144. elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml')
  1145. {
  1146. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1147. {
  1148. $encodings[] = $charset[1];
  1149. }
  1150. $encodings[] = 'US-ASCII';
  1151. }
  1152. // Text MIME-type default
  1153. elseif (substr($sniffed, 0, 5) === 'text/')
  1154. {
  1155. $encodings[] = 'US-ASCII';
  1156. }
  1157. }
  1158. // Fallback to XML 1.0 Appendix F.1/UTF-8/ISO-8859-1
  1159. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1160. $encodings[] = 'UTF-8';
  1161. $encodings[] = 'ISO-8859-1';
  1162. // There's no point in trying an encoding twice
  1163. $encodings = array_unique($encodings);
  1164. // Loop through each possible encoding, till we return something, or run out of possibilities
  1165. foreach ($encodings as $encoding)
  1166. {
  1167. // Change the encoding to UTF-8 (as we always use UTF-8 internally)
  1168. if ($utf8_data = $this->registry->call('Misc', 'change_encoding', array($this->raw_data, $encoding, 'UTF-8')))
  1169. {
  1170. // Create new parser
  1171. $parser = $this->registry->create('Parser');
  1172. // If it's parsed fine
  1173. if ($parser->parse($utf8_data, 'UTF-8'))
  1174. {
  1175. $this->data = $parser->get_data();
  1176. if (!($this->get_type() & ~SIMPLEPIE_TYPE_NONE))
  1177. {
  1178. $this->error = "A feed could not be found at $this->feed_url. This does not appear to be a valid RSS or Atom feed.";
  1179. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1180. return false;
  1181. }
  1182. if (isset($headers))
  1183. {
  1184. $this->data['headers'] = $headers;
  1185. }
  1186. $this->data['build'] = SIMPLEPIE_BUILD;
  1187. // Cache the file if caching is enabled
  1188. if ($cache && !$cache->save($this))
  1189. {
  1190. 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);
  1191. }
  1192. return true;
  1193. }
  1194. }
  1195. }
  1196. if (isset($parser))
  1197. {
  1198. // We have an error, just set SimplePie_Misc::error to it and quit
  1199. $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());
  1200. }
  1201. else
  1202. {
  1203. $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.';
  1204. }
  1205. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1206. return false;
  1207. }
  1208. /**
  1209. * Fetch the data via SimplePie_File
  1210. *
  1211. * If the data is already cached, attempt to fetch it from there instead
  1212. * @param SimplePie_Cache|false $cache Cache handler, or false to not load from the cache
  1213. * @return array|true Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type
  1214. */
  1215. protected function fetch_data(&$cache)
  1216. {
  1217. // If it's enabled, use the cache
  1218. if ($cache)
  1219. {
  1220. // Load the Cache
  1221. $this->data = $cache->load();
  1222. if (!empty($this->data))
  1223. {
  1224. // If the cache is for an outdated build of SimplePie
  1225. if (!isset($this->data['build']) || $this->data['build'] !== SIMPLEPIE_BUILD)
  1226. {
  1227. $cache->unlink();
  1228. $this->data = array();
  1229. }
  1230. // If we've hit a collision just rerun it with caching disabled
  1231. elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url)
  1232. {
  1233. $cache = false;
  1234. $this->data = array();
  1235. }
  1236. // If we've got a non feed_url stored (if the page isn't actually a feed, or is a redirect) use that URL.
  1237. elseif (isset($this->data['feed_url']))
  1238. {
  1239. // If the autodiscovery cache is still valid use it.
  1240. if ($cache->mtime() + $this->autodiscovery_cache_duration > time())
  1241. {
  1242. // Do not need to do feed autodiscovery yet.
  1243. if ($this->data['feed_url'] !== $this->data['url'])
  1244. {
  1245. $this->set_feed_url($this->data['feed_url']);
  1246. return $this->init();
  1247. }
  1248. $cache->unlink();
  1249. $this->data = array();
  1250. }
  1251. }
  1252. // Check if the cache has been updated
  1253. elseif ($cache->mtime() + $this->cache_duration < time())
  1254. {
  1255. // If we have last-modified and/or etag set
  1256. if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))
  1257. {
  1258. $headers = array(
  1259. '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',
  1260. );
  1261. if (isset($this->data['headers']['last-modified']))
  1262. {
  1263. $headers['if-modified-since'] = $this->data['headers']['last-modified'];
  1264. }
  1265. if (isset($this->data['headers']['etag']))
  1266. {
  1267. $headers['if-none-match'] = $this->data['headers']['etag'];
  1268. }
  1269. $file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen));
  1270. if ($file->success)
  1271. {
  1272. if ($file->status_code === 304)
  1273. {
  1274. $cache->touch();
  1275. return true;
  1276. }
  1277. }
  1278. else
  1279. {
  1280. unset($file);
  1281. }
  1282. }
  1283. }
  1284. // If the cache is still valid, just return true
  1285. else
  1286. {
  1287. $this->raw_data = false;
  1288. return true;
  1289. }
  1290. }
  1291. // If the cache is empty, delete it
  1292. else
  1293. {
  1294. $cache->unlink();
  1295. $this->data = array();
  1296. }
  1297. }
  1298. // 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.
  1299. if (!isset($file))
  1300. {
  1301. if ($this->file instanceof SimplePie_File && $this->file->url === $this->feed_url)
  1302. {
  1303. $file =& $this->file;
  1304. }
  1305. else
  1306. {
  1307. $headers = array(
  1308. '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',
  1309. );
  1310. $file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen));
  1311. }
  1312. }
  1313. // If the file connection has an error, set SimplePie::error to that and quit
  1314. if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1315. {
  1316. $this->error = $file->error;
  1317. return !empty($this->data);
  1318. }
  1319. if (!$this->force_feed)
  1320. {
  1321. // Check if the supplied URL is a feed, if it isn't, look for it.
  1322. $locate = $this->registry->create('Locator', array(&$file, $this->timeout, $this->useragent, $this->max_checked_feeds));
  1323. if (!$locate->is_feed($file))
  1324. {
  1325. // We need to unset this so that if SimplePie::set_file() has been called that object is untouched
  1326. unset($file);
  1327. if (!($file = $locate->find($this->autodiscovery, $this->all_discovered_feeds)))
  1328. {
  1329. $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.";
  1330. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1331. return false;
  1332. }
  1333. if ($cache)
  1334. {
  1335. $this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
  1336. if (!$cache->save($this))
  1337. {
  1338. 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);
  1339. }
  1340. $cache = $this->registry->call('Cache', 'create', array($this->cache_location, call_user_func($this->cache_name_function, $file->url), 'spc'));
  1341. }
  1342. $this->feed_url = $file->url;
  1343. }
  1344. $locate = null;
  1345. }
  1346. $this->raw_data = $file->body;
  1347. $headers = $file->headers;
  1348. $sniffer = $this->registry->create('Content_Type_Sniffer', array(&$file));
  1349. $sniffed = $sniffer->get_type();
  1350. return array($headers, $sniffed);
  1351. }
  1352. /**
  1353. * Get the error message for the occured error
  1354. *
  1355. * @return string|array Error message, or array of messages for multifeeds
  1356. */
  1357. public function error()
  1358. {
  1359. return $this->error;
  1360. }
  1361. /**
  1362. * Get the raw XML
  1363. *
  1364. * This is the same as the old `$feed->enable_xml_dump(true)`, but returns
  1365. * the data instead of printing it.
  1366. *
  1367. * @return string|boolean Raw XML data, false if the cache is used
  1368. */
  1369. public function get_raw_data()
  1370. {
  1371. return $this->raw_data;
  1372. }
  1373. /**
  1374. * Get the character encoding used for output
  1375. *
  1376. * @since Preview Release
  1377. * @return string
  1378. */
  1379. public function get_encoding()
  1380. {
  1381. return $this->sanitize->output_encoding;
  1382. }
  1383. /**
  1384. * Send the content-type header with correct encoding
  1385. *
  1386. * This method ensures that the SimplePie-enabled page is being served with
  1387. * the correct {@link http://www.iana.org/assignments/media-types/ mime-type}
  1388. * and character encoding HTTP headers (character encoding determined by the
  1389. * {@see set_output_encoding} config option).
  1390. *
  1391. * This won't work properly if any content or whitespace has already been
  1392. * sent to the browser, because it relies on PHP's
  1393. * {@link http://php.net/header header()} function, and these are the
  1394. * circumstances under which the function works.
  1395. *
  1396. * Because it's setting these settings for the entire page (as is the nature
  1397. * of HTTP headers), this should only be used once per page (again, at the
  1398. * top).
  1399. *
  1400. * @param string $mime MIME type to serve the page as
  1401. */
  1402. public function handle_content_type($mime = 'text/html')
  1403. {
  1404. if (!headers_sent())
  1405. {
  1406. $header = "Content-type: $mime;";
  1407. if ($this->get_encoding())
  1408. {
  1409. $header .= ' charset=' . $this->get_encoding();
  1410. }
  1411. else
  1412. {
  1413. $header .= ' charset=UTF-8';
  1414. }
  1415. header($header);
  1416. }
  1417. }
  1418. /**
  1419. * Get the type of the feed
  1420. *
  1421. * This returns a SIMPLEPIE_TYPE_* constant, which can be tested against
  1422. * using {@link http://php.net/language.operators.bitwise bitwise operators}
  1423. *
  1424. * @since 0.8 (usage changed to using constants in 1.0)
  1425. * @see SIMPLEPIE_TYPE_NONE Unknown.
  1426. * @see SIMPLEPIE_TYPE_RSS_090 RSS 0.90.
  1427. * @see SIMPLEPIE_TYPE_RSS_091_NETSCAPE RSS 0.91 (Netscape).
  1428. * @see SIMPLEPIE_TYPE_RSS_091_USERLAND RSS 0.91 (Userland).
  1429. * @see SIMPLEPIE_TYPE_RSS_091 RSS 0.91.
  1430. * @see SIMPLEPIE_TYPE_RSS_092 RSS 0.92.
  1431. * @see SIMPLEPIE_TYPE_RSS_093 RSS 0.93.
  1432. * @see SIMPLEPIE_TYPE_RSS_094 RSS 0.94.
  1433. * @see SIMPLEPIE_TYPE_RSS_10 RSS 1.0.
  1434. * @see SIMPLEPIE_TYPE_RSS_20 RSS 2.0.x.
  1435. * @see SIMPLEPIE_TYPE_RSS_RDF RDF-based RSS.
  1436. * @see SIMPLEPIE_TYPE_RSS_SYNDICATION Non-RDF-based RSS (truly intended as syndication format).
  1437. * @see SIMPLEPIE_TYPE_RSS_ALL Any version of RSS.
  1438. * @see SIMPLEPIE_TYPE_ATOM_03 Atom 0.3.
  1439. * @see SIMPLEPIE_TYPE_ATOM_10 Atom 1.0.
  1440. * @see SIMPLEPIE_TYPE_ATOM_ALL Any version of Atom.
  1441. * @see SIMPLEPIE_TYPE_ALL Any known/supported feed type.
  1442. * @return int SIMPLEPIE_TYPE_* constant
  1443. */
  1444. public function get_type()
  1445. {
  1446. if (!isset($this->data['type']))
  1447. {
  1448. $this->data['type'] = SIMPLEPIE_TYPE_ALL;
  1449. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed']))
  1450. {
  1451. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_10;
  1452. }
  1453. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed']))
  1454. {
  1455. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_03;
  1456. }
  1457. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF']))
  1458. {
  1459. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['channel'])
  1460. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['image'])
  1461. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item'])
  1462. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['textinput']))
  1463. {
  1464. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_10;
  1465. }
  1466. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['channel'])
  1467. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['image'])
  1468. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item'])
  1469. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['textinput']))
  1470. {
  1471. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_090;
  1472. }
  1473. }
  1474. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss']))
  1475. {
  1476. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_ALL;
  1477. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1478. {
  1479. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1480. {
  1481. case '0.91':
  1482. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091;
  1483. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1484. {
  1485. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1486. {
  1487. case '0':
  1488. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_NETSCAPE;
  1489. break;
  1490. case '24':
  1491. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_USERLAND;
  1492. break;
  1493. }
  1494. }
  1495. break;
  1496. case '0.92':
  1497. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_092;
  1498. break;
  1499. case '0.93':
  1500. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_093;
  1501. break;
  1502. case '0.94':
  1503. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_094;
  1504. break;
  1505. case '2.0':
  1506. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_20;
  1507. break;
  1508. }
  1509. }
  1510. }
  1511. else
  1512. {
  1513. $this->data['type'] = SIMPLEPIE_TYPE_NONE;
  1514. }
  1515. }
  1516. return $this->data['type'];
  1517. }
  1518. /**
  1519. * Get the URL for the feed
  1520. *
  1521. * May or may not be different from the URL passed to {@see set_feed_url()},
  1522. * depending on whether auto-discovery was used.
  1523. *
  1524. * @since Preview Release (previously called `get_feed_url()` since SimplePie 0.8.)
  1525. * @todo If we have a perm redirect we should return the new URL
  1526. * @todo When we make the above change, let's support <itunes:new-feed-url> as well
  1527. * @todo Also, |atom:link|@rel=self
  1528. * @return string|null
  1529. */
  1530. public function subscribe_url()
  1531. {
  1532. if ($this->feed_url !== null)
  1533. {
  1534. return $this->sanitize($this->feed_url, SIMPLEPIE_CONSTRUCT_IRI);
  1535. }
  1536. else
  1537. {
  1538. return null;
  1539. }
  1540. }
  1541. /**
  1542. * Get data for an feed-level element
  1543. *
  1544. * This method allows you to get access to ANY element/attribute that is a
  1545. * sub-element of the opening feed tag.
  1546. *
  1547. * The return value is an indexed array of elements matching the given
  1548. * namespace and tag name. Each element has `attribs`, `data` and `child`
  1549. * subkeys. For `attribs` and `child`, these contain namespace subkeys.
  1550. * `attribs` then has one level of associative name => value data (where
  1551. * `value` is a string) after the namespace. `child` has tag-indexed keys
  1552. * after the namespace, each member of which is an indexed array matching
  1553. * this same format.
  1554. *
  1555. * For example:
  1556. * <pre>
  1557. * // This is probably a bad example because we already support
  1558. * // <media:content> natively, but it shows you how to parse through
  1559. * // the nodes.
  1560. * $group = $item->get_item_tags(SIMPLEPIE_NAMESPACE_MEDIARSS, 'group');
  1561. * $content = $group[0]['child'][SIMPLEPIE_NAMESPACE_MEDIARSS]['content'];
  1562. * $file = $content[0]['attribs']['']['url'];
  1563. * echo $file;
  1564. * </pre>
  1565. *
  1566. * @since 1.0
  1567. * @see http://simplepie.org/wiki/faq/supported_xml_namespaces
  1568. * @param string $namespace The URL of the XML namespace of the elements you're trying to access
  1569. * @param string $tag Tag name
  1570. * @return array
  1571. */
  1572. public function get_feed_tags($namespace, $tag)
  1573. {
  1574. $type = $this->get_type();
  1575. if ($type & SIMPLEPIE_TYPE_ATOM_10)
  1576. {
  1577. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag]))
  1578. {
  1579. return $this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag];
  1580. }
  1581. }
  1582. if ($type & SIMPLEPIE_TYPE_ATOM_03)
  1583. {
  1584. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag]))
  1585. {
  1586. return $this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag];
  1587. }
  1588. }
  1589. if ($type & SIMPLEPIE_TYPE_RSS_RDF)
  1590. {
  1591. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag]))
  1592. {
  1593. return $this->data['child'][SIMPLEPIE_NAMESPACE_RDF][

Large files files are truncated, but you can click here to view the full file