PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/SimplePie.php

http://github.com/simplepie/simplepie
PHP | 3245 lines | 1805 code | 270 blank | 1170 comment | 226 complexity | 59797bbdd062e87abbec7b5e7e405f27 MD5 | raw file
Possible License(s): LGPL-2.1

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-2017, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  22. * to endorse or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package SimplePie
  36. * @version 1.5.5
  37. * @copyright 2004-2017 Ryan Parman, Sam Sneddon, Ryan McCue
  38. * @author Ryan Parman
  39. * @author Sam Sneddon
  40. * @author Ryan McCue
  41. * @link http://simplepie.org/ SimplePie
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. */
  44. /**
  45. * SimplePie Name
  46. */
  47. define('SIMPLEPIE_NAME', 'SimplePie');
  48. /**
  49. * SimplePie Version
  50. */
  51. define('SIMPLEPIE_VERSION', '1.5.5');
  52. /**
  53. * SimplePie Build
  54. * @todo Hardcode for release (there's no need to have to call SimplePie_Misc::get_build() only every load of simplepie.inc)
  55. */
  56. define('SIMPLEPIE_BUILD', gmdate('YmdHis', SimplePie_Misc::get_build()));
  57. /**
  58. * SimplePie Website URL
  59. */
  60. define('SIMPLEPIE_URL', 'http://simplepie.org');
  61. /**
  62. * SimplePie Useragent
  63. * @see SimplePie::set_useragent()
  64. */
  65. define('SIMPLEPIE_USERAGENT', SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION . ' (Feed Parser; ' . SIMPLEPIE_URL . '; Allow like Gecko) Build/' . SIMPLEPIE_BUILD);
  66. /**
  67. * SimplePie Linkback
  68. */
  69. define('SIMPLEPIE_LINKBACK', '<a href="' . SIMPLEPIE_URL . '" title="' . SIMPLEPIE_NAME . ' ' . SIMPLEPIE_VERSION . '">' . SIMPLEPIE_NAME . '</a>');
  70. /**
  71. * No Autodiscovery
  72. * @see SimplePie::set_autodiscovery_level()
  73. */
  74. define('SIMPLEPIE_LOCATOR_NONE', 0);
  75. /**
  76. * Feed Link Element Autodiscovery
  77. * @see SimplePie::set_autodiscovery_level()
  78. */
  79. define('SIMPLEPIE_LOCATOR_AUTODISCOVERY', 1);
  80. /**
  81. * Local Feed Extension Autodiscovery
  82. * @see SimplePie::set_autodiscovery_level()
  83. */
  84. define('SIMPLEPIE_LOCATOR_LOCAL_EXTENSION', 2);
  85. /**
  86. * Local Feed Body Autodiscovery
  87. * @see SimplePie::set_autodiscovery_level()
  88. */
  89. define('SIMPLEPIE_LOCATOR_LOCAL_BODY', 4);
  90. /**
  91. * Remote Feed Extension Autodiscovery
  92. * @see SimplePie::set_autodiscovery_level()
  93. */
  94. define('SIMPLEPIE_LOCATOR_REMOTE_EXTENSION', 8);
  95. /**
  96. * Remote Feed Body Autodiscovery
  97. * @see SimplePie::set_autodiscovery_level()
  98. */
  99. define('SIMPLEPIE_LOCATOR_REMOTE_BODY', 16);
  100. /**
  101. * All Feed Autodiscovery
  102. * @see SimplePie::set_autodiscovery_level()
  103. */
  104. define('SIMPLEPIE_LOCATOR_ALL', 31);
  105. /**
  106. * No known feed type
  107. */
  108. define('SIMPLEPIE_TYPE_NONE', 0);
  109. /**
  110. * RSS 0.90
  111. */
  112. define('SIMPLEPIE_TYPE_RSS_090', 1);
  113. /**
  114. * RSS 0.91 (Netscape)
  115. */
  116. define('SIMPLEPIE_TYPE_RSS_091_NETSCAPE', 2);
  117. /**
  118. * RSS 0.91 (Userland)
  119. */
  120. define('SIMPLEPIE_TYPE_RSS_091_USERLAND', 4);
  121. /**
  122. * RSS 0.91 (both Netscape and Userland)
  123. */
  124. define('SIMPLEPIE_TYPE_RSS_091', 6);
  125. /**
  126. * RSS 0.92
  127. */
  128. define('SIMPLEPIE_TYPE_RSS_092', 8);
  129. /**
  130. * RSS 0.93
  131. */
  132. define('SIMPLEPIE_TYPE_RSS_093', 16);
  133. /**
  134. * RSS 0.94
  135. */
  136. define('SIMPLEPIE_TYPE_RSS_094', 32);
  137. /**
  138. * RSS 1.0
  139. */
  140. define('SIMPLEPIE_TYPE_RSS_10', 64);
  141. /**
  142. * RSS 2.0
  143. */
  144. define('SIMPLEPIE_TYPE_RSS_20', 128);
  145. /**
  146. * RDF-based RSS
  147. */
  148. define('SIMPLEPIE_TYPE_RSS_RDF', 65);
  149. /**
  150. * Non-RDF-based RSS (truly intended as syndication format)
  151. */
  152. define('SIMPLEPIE_TYPE_RSS_SYNDICATION', 190);
  153. /**
  154. * All RSS
  155. */
  156. define('SIMPLEPIE_TYPE_RSS_ALL', 255);
  157. /**
  158. * Atom 0.3
  159. */
  160. define('SIMPLEPIE_TYPE_ATOM_03', 256);
  161. /**
  162. * Atom 1.0
  163. */
  164. define('SIMPLEPIE_TYPE_ATOM_10', 512);
  165. /**
  166. * All Atom
  167. */
  168. define('SIMPLEPIE_TYPE_ATOM_ALL', 768);
  169. /**
  170. * All feed types
  171. */
  172. define('SIMPLEPIE_TYPE_ALL', 1023);
  173. /**
  174. * No construct
  175. */
  176. define('SIMPLEPIE_CONSTRUCT_NONE', 0);
  177. /**
  178. * Text construct
  179. */
  180. define('SIMPLEPIE_CONSTRUCT_TEXT', 1);
  181. /**
  182. * HTML construct
  183. */
  184. define('SIMPLEPIE_CONSTRUCT_HTML', 2);
  185. /**
  186. * XHTML construct
  187. */
  188. define('SIMPLEPIE_CONSTRUCT_XHTML', 4);
  189. /**
  190. * base64-encoded construct
  191. */
  192. define('SIMPLEPIE_CONSTRUCT_BASE64', 8);
  193. /**
  194. * IRI construct
  195. */
  196. define('SIMPLEPIE_CONSTRUCT_IRI', 16);
  197. /**
  198. * A construct that might be HTML
  199. */
  200. define('SIMPLEPIE_CONSTRUCT_MAYBE_HTML', 32);
  201. /**
  202. * All constructs
  203. */
  204. define('SIMPLEPIE_CONSTRUCT_ALL', 63);
  205. /**
  206. * Don't change case
  207. */
  208. define('SIMPLEPIE_SAME_CASE', 1);
  209. /**
  210. * Change to lowercase
  211. */
  212. define('SIMPLEPIE_LOWERCASE', 2);
  213. /**
  214. * Change to uppercase
  215. */
  216. define('SIMPLEPIE_UPPERCASE', 4);
  217. /**
  218. * PCRE for HTML attributes
  219. */
  220. define('SIMPLEPIE_PCRE_HTML_ATTRIBUTE', '((?:[\x09\x0A\x0B\x0C\x0D\x20]+[^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"(?:[^"]*)"|\'(?:[^\']*)\'|(?:[^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?)*)[\x09\x0A\x0B\x0C\x0D\x20]*');
  221. /**
  222. * PCRE for XML attributes
  223. */
  224. define('SIMPLEPIE_PCRE_XML_ATTRIBUTE', '((?:\s+(?:(?:[^\s:]+:)?[^\s:]+)\s*=\s*(?:"(?:[^"]*)"|\'(?:[^\']*)\'))*)\s*');
  225. /**
  226. * XML Namespace
  227. */
  228. define('SIMPLEPIE_NAMESPACE_XML', 'http://www.w3.org/XML/1998/namespace');
  229. /**
  230. * Atom 1.0 Namespace
  231. */
  232. define('SIMPLEPIE_NAMESPACE_ATOM_10', 'http://www.w3.org/2005/Atom');
  233. /**
  234. * Atom 0.3 Namespace
  235. */
  236. define('SIMPLEPIE_NAMESPACE_ATOM_03', 'http://purl.org/atom/ns#');
  237. /**
  238. * RDF Namespace
  239. */
  240. define('SIMPLEPIE_NAMESPACE_RDF', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  241. /**
  242. * RSS 0.90 Namespace
  243. */
  244. define('SIMPLEPIE_NAMESPACE_RSS_090', 'http://my.netscape.com/rdf/simple/0.9/');
  245. /**
  246. * RSS 1.0 Namespace
  247. */
  248. define('SIMPLEPIE_NAMESPACE_RSS_10', 'http://purl.org/rss/1.0/');
  249. /**
  250. * RSS 1.0 Content Module Namespace
  251. */
  252. define('SIMPLEPIE_NAMESPACE_RSS_10_MODULES_CONTENT', 'http://purl.org/rss/1.0/modules/content/');
  253. /**
  254. * RSS 2.0 Namespace
  255. * (Stupid, I know, but I'm certain it will confuse people less with support.)
  256. */
  257. define('SIMPLEPIE_NAMESPACE_RSS_20', '');
  258. /**
  259. * DC 1.0 Namespace
  260. */
  261. define('SIMPLEPIE_NAMESPACE_DC_10', 'http://purl.org/dc/elements/1.0/');
  262. /**
  263. * DC 1.1 Namespace
  264. */
  265. define('SIMPLEPIE_NAMESPACE_DC_11', 'http://purl.org/dc/elements/1.1/');
  266. /**
  267. * W3C Basic Geo (WGS84 lat/long) Vocabulary Namespace
  268. */
  269. define('SIMPLEPIE_NAMESPACE_W3C_BASIC_GEO', 'http://www.w3.org/2003/01/geo/wgs84_pos#');
  270. /**
  271. * GeoRSS Namespace
  272. */
  273. define('SIMPLEPIE_NAMESPACE_GEORSS', 'http://www.georss.org/georss');
  274. /**
  275. * Media RSS Namespace
  276. */
  277. define('SIMPLEPIE_NAMESPACE_MEDIARSS', 'http://search.yahoo.com/mrss/');
  278. /**
  279. * Wrong Media RSS Namespace. Caused by a long-standing typo in the spec.
  280. */
  281. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG', 'http://search.yahoo.com/mrss');
  282. /**
  283. * Wrong Media RSS Namespace #2. New namespace introduced in Media RSS 1.5.
  284. */
  285. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2', 'http://video.search.yahoo.com/mrss');
  286. /**
  287. * Wrong Media RSS Namespace #3. A possible typo of the Media RSS 1.5 namespace.
  288. */
  289. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3', 'http://video.search.yahoo.com/mrss/');
  290. /**
  291. * Wrong Media RSS Namespace #4. New spec location after the RSS Advisory Board takes it over, but not a valid namespace.
  292. */
  293. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4', 'http://www.rssboard.org/media-rss');
  294. /**
  295. * Wrong Media RSS Namespace #5. A possible typo of the RSS Advisory Board URL.
  296. */
  297. define('SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5', 'http://www.rssboard.org/media-rss/');
  298. /**
  299. * iTunes RSS Namespace
  300. */
  301. define('SIMPLEPIE_NAMESPACE_ITUNES', 'http://www.itunes.com/dtds/podcast-1.0.dtd');
  302. /**
  303. * XHTML Namespace
  304. */
  305. define('SIMPLEPIE_NAMESPACE_XHTML', 'http://www.w3.org/1999/xhtml');
  306. /**
  307. * IANA Link Relations Registry
  308. */
  309. define('SIMPLEPIE_IANA_LINK_RELATIONS_REGISTRY', 'http://www.iana.org/assignments/relation/');
  310. /**
  311. * No file source
  312. */
  313. define('SIMPLEPIE_FILE_SOURCE_NONE', 0);
  314. /**
  315. * Remote file source
  316. */
  317. define('SIMPLEPIE_FILE_SOURCE_REMOTE', 1);
  318. /**
  319. * Local file source
  320. */
  321. define('SIMPLEPIE_FILE_SOURCE_LOCAL', 2);
  322. /**
  323. * fsockopen() file source
  324. */
  325. define('SIMPLEPIE_FILE_SOURCE_FSOCKOPEN', 4);
  326. /**
  327. * cURL file source
  328. */
  329. define('SIMPLEPIE_FILE_SOURCE_CURL', 8);
  330. /**
  331. * file_get_contents() file source
  332. */
  333. define('SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS', 16);
  334. /**
  335. * SimplePie
  336. *
  337. * @package SimplePie
  338. * @subpackage API
  339. */
  340. class SimplePie
  341. {
  342. /**
  343. * @var array Raw data
  344. * @access private
  345. */
  346. public $data = array();
  347. /**
  348. * @var mixed Error string
  349. * @access private
  350. */
  351. public $error;
  352. /**
  353. * @var object Instance of SimplePie_Sanitize (or other class)
  354. * @see SimplePie::set_sanitize_class()
  355. * @access private
  356. */
  357. public $sanitize;
  358. /**
  359. * @var string SimplePie Useragent
  360. * @see SimplePie::set_useragent()
  361. * @access private
  362. */
  363. public $useragent = SIMPLEPIE_USERAGENT;
  364. /**
  365. * @var string Feed URL
  366. * @see SimplePie::set_feed_url()
  367. * @access private
  368. */
  369. public $feed_url;
  370. /**
  371. * @var string Original feed URL, or new feed URL iff HTTP 301 Moved Permanently
  372. * @see SimplePie::subscribe_url()
  373. * @access private
  374. */
  375. public $permanent_url = null;
  376. /**
  377. * @var object Instance of SimplePie_File to use as a feed
  378. * @see SimplePie::set_file()
  379. * @access private
  380. */
  381. public $file;
  382. /**
  383. * @var string Raw feed data
  384. * @see SimplePie::set_raw_data()
  385. * @access private
  386. */
  387. public $raw_data;
  388. /**
  389. * @var int Timeout for fetching remote files
  390. * @see SimplePie::set_timeout()
  391. * @access private
  392. */
  393. public $timeout = 10;
  394. /**
  395. * @var array Custom curl options
  396. * @see SimplePie::set_curl_options()
  397. * @access private
  398. */
  399. public $curl_options = array();
  400. /**
  401. * @var bool Forces fsockopen() to be used for remote files instead
  402. * of cURL, even if a new enough version is installed
  403. * @see SimplePie::force_fsockopen()
  404. * @access private
  405. */
  406. public $force_fsockopen = false;
  407. /**
  408. * @var bool Force the given data/URL to be treated as a feed no matter what
  409. * it appears like
  410. * @see SimplePie::force_feed()
  411. * @access private
  412. */
  413. public $force_feed = false;
  414. /**
  415. * @var bool Enable/Disable Caching
  416. * @see SimplePie::enable_cache()
  417. * @access private
  418. */
  419. public $cache = true;
  420. /**
  421. * @var bool Force SimplePie to fallback to expired cache, if enabled,
  422. * when feed is unavailable.
  423. * @see SimplePie::force_cache_fallback()
  424. * @access private
  425. */
  426. public $force_cache_fallback = false;
  427. /**
  428. * @var int Cache duration (in seconds)
  429. * @see SimplePie::set_cache_duration()
  430. * @access private
  431. */
  432. public $cache_duration = 3600;
  433. /**
  434. * @var int Auto-discovery cache duration (in seconds)
  435. * @see SimplePie::set_autodiscovery_cache_duration()
  436. * @access private
  437. */
  438. public $autodiscovery_cache_duration = 604800; // 7 Days.
  439. /**
  440. * @var string Cache location (relative to executing script)
  441. * @see SimplePie::set_cache_location()
  442. * @access private
  443. */
  444. public $cache_location = './cache';
  445. /**
  446. * @var string Function that creates the cache filename
  447. * @see SimplePie::set_cache_name_function()
  448. * @access private
  449. */
  450. public $cache_name_function = 'md5';
  451. /**
  452. * @var bool Reorder feed by date descending
  453. * @see SimplePie::enable_order_by_date()
  454. * @access private
  455. */
  456. public $order_by_date = true;
  457. /**
  458. * @var mixed Force input encoding to be set to the follow value
  459. * (false, or anything type-cast to false, disables this feature)
  460. * @see SimplePie::set_input_encoding()
  461. * @access private
  462. */
  463. public $input_encoding = false;
  464. /**
  465. * @var int Feed Autodiscovery Level
  466. * @see SimplePie::set_autodiscovery_level()
  467. * @access private
  468. */
  469. public $autodiscovery = SIMPLEPIE_LOCATOR_ALL;
  470. /**
  471. * Class registry object
  472. *
  473. * @var SimplePie_Registry
  474. */
  475. public $registry;
  476. /**
  477. * @var int Maximum number of feeds to check with autodiscovery
  478. * @see SimplePie::set_max_checked_feeds()
  479. * @access private
  480. */
  481. public $max_checked_feeds = 10;
  482. /**
  483. * @var array All the feeds found during the autodiscovery process
  484. * @see SimplePie::get_all_discovered_feeds()
  485. * @access private
  486. */
  487. public $all_discovered_feeds = array();
  488. /**
  489. * @var string Web-accessible path to the handler_image.php file.
  490. * @see SimplePie::set_image_handler()
  491. * @access private
  492. */
  493. public $image_handler = '';
  494. /**
  495. * @var array Stores the URLs when multiple feeds are being initialized.
  496. * @see SimplePie::set_feed_url()
  497. * @access private
  498. */
  499. public $multifeed_url = array();
  500. /**
  501. * @var array Stores SimplePie objects when multiple feeds initialized.
  502. * @access private
  503. */
  504. public $multifeed_objects = array();
  505. /**
  506. * @var array Stores the get_object_vars() array for use with multifeeds.
  507. * @see SimplePie::set_feed_url()
  508. * @access private
  509. */
  510. public $config_settings = null;
  511. /**
  512. * @var integer Stores the number of items to return per-feed with multifeeds.
  513. * @see SimplePie::set_item_limit()
  514. * @access private
  515. */
  516. public $item_limit = 0;
  517. /**
  518. * @var bool Stores if last-modified and/or etag headers were sent with the
  519. * request when checking a feed.
  520. */
  521. public $check_modified = false;
  522. /**
  523. * @var array Stores the default attributes to be stripped by strip_attributes().
  524. * @see SimplePie::strip_attributes()
  525. * @access private
  526. */
  527. public $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc');
  528. /**
  529. * @var array Stores the default attributes to add to different tags by add_attributes().
  530. * @see SimplePie::add_attributes()
  531. * @access private
  532. */
  533. public $add_attributes = array('audio' => array('preload' => 'none'), 'iframe' => array('sandbox' => 'allow-scripts allow-same-origin'), 'video' => array('preload' => 'none'));
  534. /**
  535. * @var array Stores the default tags to be stripped by strip_htmltags().
  536. * @see SimplePie::strip_htmltags()
  537. * @access private
  538. */
  539. public $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
  540. /**
  541. * @var bool Should we throw exceptions, or use the old-style error property?
  542. * @access private
  543. */
  544. public $enable_exceptions = false;
  545. /**
  546. * The SimplePie class contains feed level data and options
  547. *
  548. * To use SimplePie, create the SimplePie object with no parameters. You can
  549. * then set configuration options using the provided methods. After setting
  550. * them, you must initialise the feed using $feed->init(). At that point the
  551. * object's methods and properties will be available to you.
  552. *
  553. * Previously, it was possible to pass in the feed URL along with cache
  554. * options directly into the constructor. This has been removed as of 1.3 as
  555. * it caused a lot of confusion.
  556. *
  557. * @since 1.0 Preview Release
  558. */
  559. public function __construct()
  560. {
  561. if (version_compare(PHP_VERSION, '5.6', '<'))
  562. {
  563. trigger_error('Please upgrade to PHP 5.6 or newer.');
  564. die();
  565. }
  566. // Other objects, instances created here so we can set options on them
  567. $this->sanitize = new SimplePie_Sanitize();
  568. $this->registry = new SimplePie_Registry();
  569. if (func_num_args() > 0)
  570. {
  571. $level = defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_WARNING;
  572. trigger_error('Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_duration() directly.', $level);
  573. $args = func_get_args();
  574. switch (count($args)) {
  575. case 3:
  576. $this->set_cache_duration($args[2]);
  577. case 2:
  578. $this->set_cache_location($args[1]);
  579. case 1:
  580. $this->set_feed_url($args[0]);
  581. $this->init();
  582. }
  583. }
  584. }
  585. /**
  586. * Used for converting object to a string
  587. */
  588. public function __toString()
  589. {
  590. return md5(serialize($this->data));
  591. }
  592. /**
  593. * Remove items that link back to this before destroying this object
  594. */
  595. public function __destruct()
  596. {
  597. if (!gc_enabled())
  598. {
  599. if (!empty($this->data['items']))
  600. {
  601. foreach ($this->data['items'] as $item)
  602. {
  603. $item->__destruct();
  604. }
  605. unset($item, $this->data['items']);
  606. }
  607. if (!empty($this->data['ordered_items']))
  608. {
  609. foreach ($this->data['ordered_items'] as $item)
  610. {
  611. $item->__destruct();
  612. }
  613. unset($item, $this->data['ordered_items']);
  614. }
  615. }
  616. }
  617. /**
  618. * Force the given data/URL to be treated as a feed
  619. *
  620. * This tells SimplePie to ignore the content-type provided by the server.
  621. * Be careful when using this option, as it will also disable autodiscovery.
  622. *
  623. * @since 1.1
  624. * @param bool $enable Force the given data/URL to be treated as a feed
  625. */
  626. public function force_feed($enable = false)
  627. {
  628. $this->force_feed = (bool) $enable;
  629. }
  630. /**
  631. * Set the URL of the feed you want to parse
  632. *
  633. * This allows you to enter the URL of the feed you want to parse, or the
  634. * website you want to try to use auto-discovery on. This takes priority
  635. * over any set raw data.
  636. *
  637. * You can set multiple feeds to mash together by passing an array instead
  638. * of a string for the $url. Remember that with each additional feed comes
  639. * additional processing and resources.
  640. *
  641. * @since 1.0 Preview Release
  642. * @see set_raw_data()
  643. * @param string|array $url This is the URL (or array of URLs) that you want to parse.
  644. */
  645. public function set_feed_url($url)
  646. {
  647. $this->multifeed_url = array();
  648. if (is_array($url))
  649. {
  650. foreach ($url as $value)
  651. {
  652. $this->multifeed_url[] = $this->registry->call('Misc', 'fix_protocol', array($value, 1));
  653. }
  654. }
  655. else
  656. {
  657. $this->feed_url = $this->registry->call('Misc', 'fix_protocol', array($url, 1));
  658. $this->permanent_url = $this->feed_url;
  659. }
  660. }
  661. /**
  662. * Set an instance of {@see SimplePie_File} to use as a feed
  663. *
  664. * @param SimplePie_File &$file
  665. * @return bool True on success, false on failure
  666. */
  667. public function set_file(&$file)
  668. {
  669. if ($file instanceof SimplePie_File)
  670. {
  671. $this->feed_url = $file->url;
  672. $this->permanent_url = $this->feed_url;
  673. $this->file =& $file;
  674. return true;
  675. }
  676. return false;
  677. }
  678. /**
  679. * Set the raw XML data to parse
  680. *
  681. * Allows you to use a string of RSS/Atom data instead of a remote feed.
  682. *
  683. * If you have a feed available as a string in PHP, you can tell SimplePie
  684. * to parse that data string instead of a remote feed. Any set feed URL
  685. * takes precedence.
  686. *
  687. * @since 1.0 Beta 3
  688. * @param string $data RSS or Atom data as a string.
  689. * @see set_feed_url()
  690. */
  691. public function set_raw_data($data)
  692. {
  693. $this->raw_data = $data;
  694. }
  695. /**
  696. * Set the default timeout for fetching remote feeds
  697. *
  698. * This allows you to change the maximum time the feed's server to respond
  699. * and send the feed back.
  700. *
  701. * @since 1.0 Beta 3
  702. * @param int $timeout The maximum number of seconds to spend waiting to retrieve a feed.
  703. */
  704. public function set_timeout($timeout = 10)
  705. {
  706. $this->timeout = (int) $timeout;
  707. }
  708. /**
  709. * Set custom curl options
  710. *
  711. * This allows you to change default curl options
  712. *
  713. * @since 1.0 Beta 3
  714. * @param array $curl_options Curl options to add to default settings
  715. */
  716. public function set_curl_options(array $curl_options = array())
  717. {
  718. $this->curl_options = $curl_options;
  719. }
  720. /**
  721. * Force SimplePie to use fsockopen() instead of cURL
  722. *
  723. * @since 1.0 Beta 3
  724. * @param bool $enable Force fsockopen() to be used
  725. */
  726. public function force_fsockopen($enable = false)
  727. {
  728. $this->force_fsockopen = (bool) $enable;
  729. }
  730. /**
  731. * Enable/disable caching in SimplePie.
  732. *
  733. * This option allows you to disable caching all-together in SimplePie.
  734. * However, disabling the cache can lead to longer load times.
  735. *
  736. * @since 1.0 Preview Release
  737. * @param bool $enable Enable caching
  738. */
  739. public function enable_cache($enable = true)
  740. {
  741. $this->cache = (bool) $enable;
  742. }
  743. /**
  744. * SimplePie to continue to fall back to expired cache, if enabled, when
  745. * feed is unavailable.
  746. *
  747. * This tells SimplePie to ignore any file errors and fall back to cache
  748. * instead. This only works if caching is enabled and cached content
  749. * still exists.
  750. * @param bool $enable Force use of cache on fail.
  751. */
  752. public function force_cache_fallback($enable = false)
  753. {
  754. $this->force_cache_fallback= (bool) $enable;
  755. }
  756. /**
  757. * Set the length of time (in seconds) that the contents of a feed will be
  758. * cached
  759. *
  760. * @param int $seconds The feed content cache duration
  761. */
  762. public function set_cache_duration($seconds = 3600)
  763. {
  764. $this->cache_duration = (int) $seconds;
  765. }
  766. /**
  767. * Set the length of time (in seconds) that the autodiscovered feed URL will
  768. * be cached
  769. *
  770. * @param int $seconds The autodiscovered feed URL cache duration.
  771. */
  772. public function set_autodiscovery_cache_duration($seconds = 604800)
  773. {
  774. $this->autodiscovery_cache_duration = (int) $seconds;
  775. }
  776. /**
  777. * Set the file system location where the cached files should be stored
  778. *
  779. * @param string $location The file system location.
  780. */
  781. public function set_cache_location($location = './cache')
  782. {
  783. $this->cache_location = (string) $location;
  784. }
  785. /**
  786. * Set whether feed items should be sorted into reverse chronological order
  787. *
  788. * @param bool $enable Sort as reverse chronological order.
  789. */
  790. public function enable_order_by_date($enable = true)
  791. {
  792. $this->order_by_date = (bool) $enable;
  793. }
  794. /**
  795. * Set the character encoding used to parse the feed
  796. *
  797. * This overrides the encoding reported by the feed, however it will fall
  798. * back to the normal encoding detection if the override fails
  799. *
  800. * @param string $encoding Character encoding
  801. */
  802. public function set_input_encoding($encoding = false)
  803. {
  804. if ($encoding)
  805. {
  806. $this->input_encoding = (string) $encoding;
  807. }
  808. else
  809. {
  810. $this->input_encoding = false;
  811. }
  812. }
  813. /**
  814. * Set how much feed autodiscovery to do
  815. *
  816. * @see SIMPLEPIE_LOCATOR_NONE
  817. * @see SIMPLEPIE_LOCATOR_AUTODISCOVERY
  818. * @see SIMPLEPIE_LOCATOR_LOCAL_EXTENSION
  819. * @see SIMPLEPIE_LOCATOR_LOCAL_BODY
  820. * @see SIMPLEPIE_LOCATOR_REMOTE_EXTENSION
  821. * @see SIMPLEPIE_LOCATOR_REMOTE_BODY
  822. * @see SIMPLEPIE_LOCATOR_ALL
  823. * @param int $level Feed Autodiscovery Level (level can be a combination of the above constants, see bitwise OR operator)
  824. */
  825. public function set_autodiscovery_level($level = SIMPLEPIE_LOCATOR_ALL)
  826. {
  827. $this->autodiscovery = (int) $level;
  828. }
  829. /**
  830. * Get the class registry
  831. *
  832. * Use this to override SimplePie's default classes
  833. * @see SimplePie_Registry
  834. * @return SimplePie_Registry
  835. */
  836. public function &get_registry()
  837. {
  838. return $this->registry;
  839. }
  840. /**#@+
  841. * Useful when you are overloading or extending SimplePie's default classes.
  842. *
  843. * @deprecated Use {@see get_registry()} instead
  844. * @link http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.extends PHP5 extends documentation
  845. * @param string $class Name of custom class
  846. * @return boolean True on success, false otherwise
  847. */
  848. /**
  849. * Set which class SimplePie uses for caching
  850. */
  851. public function set_cache_class($class = 'SimplePie_Cache')
  852. {
  853. return $this->registry->register('Cache', $class, true);
  854. }
  855. /**
  856. * Set which class SimplePie uses for auto-discovery
  857. */
  858. public function set_locator_class($class = 'SimplePie_Locator')
  859. {
  860. return $this->registry->register('Locator', $class, true);
  861. }
  862. /**
  863. * Set which class SimplePie uses for XML parsing
  864. */
  865. public function set_parser_class($class = 'SimplePie_Parser')
  866. {
  867. return $this->registry->register('Parser', $class, true);
  868. }
  869. /**
  870. * Set which class SimplePie uses for remote file fetching
  871. */
  872. public function set_file_class($class = 'SimplePie_File')
  873. {
  874. return $this->registry->register('File', $class, true);
  875. }
  876. /**
  877. * Set which class SimplePie uses for data sanitization
  878. */
  879. public function set_sanitize_class($class = 'SimplePie_Sanitize')
  880. {
  881. return $this->registry->register('Sanitize', $class, true);
  882. }
  883. /**
  884. * Set which class SimplePie uses for handling feed items
  885. */
  886. public function set_item_class($class = 'SimplePie_Item')
  887. {
  888. return $this->registry->register('Item', $class, true);
  889. }
  890. /**
  891. * Set which class SimplePie uses for handling author data
  892. */
  893. public function set_author_class($class = 'SimplePie_Author')
  894. {
  895. return $this->registry->register('Author', $class, true);
  896. }
  897. /**
  898. * Set which class SimplePie uses for handling category data
  899. */
  900. public function set_category_class($class = 'SimplePie_Category')
  901. {
  902. return $this->registry->register('Category', $class, true);
  903. }
  904. /**
  905. * Set which class SimplePie uses for feed enclosures
  906. */
  907. public function set_enclosure_class($class = 'SimplePie_Enclosure')
  908. {
  909. return $this->registry->register('Enclosure', $class, true);
  910. }
  911. /**
  912. * Set which class SimplePie uses for `<media:text>` captions
  913. */
  914. public function set_caption_class($class = 'SimplePie_Caption')
  915. {
  916. return $this->registry->register('Caption', $class, true);
  917. }
  918. /**
  919. * Set which class SimplePie uses for `<media:copyright>`
  920. */
  921. public function set_copyright_class($class = 'SimplePie_Copyright')
  922. {
  923. return $this->registry->register('Copyright', $class, true);
  924. }
  925. /**
  926. * Set which class SimplePie uses for `<media:credit>`
  927. */
  928. public function set_credit_class($class = 'SimplePie_Credit')
  929. {
  930. return $this->registry->register('Credit', $class, true);
  931. }
  932. /**
  933. * Set which class SimplePie uses for `<media:rating>`
  934. */
  935. public function set_rating_class($class = 'SimplePie_Rating')
  936. {
  937. return $this->registry->register('Rating', $class, true);
  938. }
  939. /**
  940. * Set which class SimplePie uses for `<media:restriction>`
  941. */
  942. public function set_restriction_class($class = 'SimplePie_Restriction')
  943. {
  944. return $this->registry->register('Restriction', $class, true);
  945. }
  946. /**
  947. * Set which class SimplePie uses for content-type sniffing
  948. */
  949. public function set_content_type_sniffer_class($class = 'SimplePie_Content_Type_Sniffer')
  950. {
  951. return $this->registry->register('Content_Type_Sniffer', $class, true);
  952. }
  953. /**
  954. * Set which class SimplePie uses item sources
  955. */
  956. public function set_source_class($class = 'SimplePie_Source')
  957. {
  958. return $this->registry->register('Source', $class, true);
  959. }
  960. /**#@-*/
  961. /**
  962. * Set the user agent string
  963. *
  964. * @param string $ua New user agent string.
  965. */
  966. public function set_useragent($ua = SIMPLEPIE_USERAGENT)
  967. {
  968. $this->useragent = (string) $ua;
  969. }
  970. /**
  971. * Set callback function to create cache filename with
  972. *
  973. * @param mixed $function Callback function
  974. */
  975. public function set_cache_name_function($function = 'md5')
  976. {
  977. if (is_callable($function))
  978. {
  979. $this->cache_name_function = $function;
  980. }
  981. }
  982. /**
  983. * Set options to make SP as fast as possible
  984. *
  985. * Forgoes a substantial amount of data sanitization in favor of speed. This
  986. * turns SimplePie into a dumb parser of feeds.
  987. *
  988. * @param bool $set Whether to set them or not
  989. */
  990. public function set_stupidly_fast($set = false)
  991. {
  992. if ($set)
  993. {
  994. $this->enable_order_by_date(false);
  995. $this->remove_div(false);
  996. $this->strip_comments(false);
  997. $this->strip_htmltags(false);
  998. $this->strip_attributes(false);
  999. $this->add_attributes(false);
  1000. $this->set_image_handler(false);
  1001. }
  1002. }
  1003. /**
  1004. * Set maximum number of feeds to check with autodiscovery
  1005. *
  1006. * @param int $max Maximum number of feeds to check
  1007. */
  1008. public function set_max_checked_feeds($max = 10)
  1009. {
  1010. $this->max_checked_feeds = (int) $max;
  1011. }
  1012. public function remove_div($enable = true)
  1013. {
  1014. $this->sanitize->remove_div($enable);
  1015. }
  1016. public function strip_htmltags($tags = '', $encode = null)
  1017. {
  1018. if ($tags === '')
  1019. {
  1020. $tags = $this->strip_htmltags;
  1021. }
  1022. $this->sanitize->strip_htmltags($tags);
  1023. if ($encode !== null)
  1024. {
  1025. $this->sanitize->encode_instead_of_strip($tags);
  1026. }
  1027. }
  1028. public function encode_instead_of_strip($enable = true)
  1029. {
  1030. $this->sanitize->encode_instead_of_strip($enable);
  1031. }
  1032. public function strip_attributes($attribs = '')
  1033. {
  1034. if ($attribs === '')
  1035. {
  1036. $attribs = $this->strip_attributes;
  1037. }
  1038. $this->sanitize->strip_attributes($attribs);
  1039. }
  1040. public function add_attributes($attribs = '')
  1041. {
  1042. if ($attribs === '')
  1043. {
  1044. $attribs = $this->add_attributes;
  1045. }
  1046. $this->sanitize->add_attributes($attribs);
  1047. }
  1048. /**
  1049. * Set the output encoding
  1050. *
  1051. * Allows you to override SimplePie's output to match that of your webpage.
  1052. * This is useful for times when your webpages are not being served as
  1053. * UTF-8. This setting will be obeyed by {@see handle_content_type()}, and
  1054. * is similar to {@see set_input_encoding()}.
  1055. *
  1056. * It should be noted, however, that not all character encodings can support
  1057. * all characters. If your page is being served as ISO-8859-1 and you try
  1058. * to display a Japanese feed, you'll likely see garbled characters.
  1059. * Because of this, it is highly recommended to ensure that your webpages
  1060. * are served as UTF-8.
  1061. *
  1062. * The number of supported character encodings depends on whether your web
  1063. * host supports {@link http://php.net/mbstring mbstring},
  1064. * {@link http://php.net/iconv iconv}, or both. See
  1065. * {@link http://simplepie.org/wiki/faq/Supported_Character_Encodings} for
  1066. * more information.
  1067. *
  1068. * @param string $encoding
  1069. */
  1070. public function set_output_encoding($encoding = 'UTF-8')
  1071. {
  1072. $this->sanitize->set_output_encoding($encoding);
  1073. }
  1074. public function strip_comments($strip = false)
  1075. {
  1076. $this->sanitize->strip_comments($strip);
  1077. }
  1078. /**
  1079. * Set element/attribute key/value pairs of HTML attributes
  1080. * containing URLs that need to be resolved relative to the feed
  1081. *
  1082. * Defaults to |a|@href, |area|@href, |blockquote|@cite, |del|@cite,
  1083. * |form|@action, |img|@longdesc, |img|@src, |input|@src, |ins|@cite,
  1084. * |q|@cite
  1085. *
  1086. * @since 1.0
  1087. * @param array|null $element_attribute Element/attribute key/value pairs, null for default
  1088. */
  1089. public function set_url_replacements($element_attribute = null)
  1090. {
  1091. $this->sanitize->set_url_replacements($element_attribute);
  1092. }
  1093. /**
  1094. * Set the handler to enable the display of cached images.
  1095. *
  1096. * @param string $page Web-accessible path to the handler_image.php file.
  1097. * @param string $qs The query string that the value should be passed to.
  1098. */
  1099. public function set_image_handler($page = false, $qs = 'i')
  1100. {
  1101. if ($page !== false)
  1102. {
  1103. $this->sanitize->set_image_handler($page . '?' . $qs . '=');
  1104. }
  1105. else
  1106. {
  1107. $this->image_handler = '';
  1108. }
  1109. }
  1110. /**
  1111. * Set the limit for items returned per-feed with multifeeds
  1112. *
  1113. * @param integer $limit The maximum number of items to return.
  1114. */
  1115. public function set_item_limit($limit = 0)
  1116. {
  1117. $this->item_limit = (int) $limit;
  1118. }
  1119. /**
  1120. * Enable throwing exceptions
  1121. *
  1122. * @param boolean $enable Should we throw exceptions, or use the old-style error property?
  1123. */
  1124. public function enable_exceptions($enable = true)
  1125. {
  1126. $this->enable_exceptions = $enable;
  1127. }
  1128. /**
  1129. * Initialize the feed object
  1130. *
  1131. * This is what makes everything happen. Period. This is where all of the
  1132. * configuration options get processed, feeds are fetched, cached, and
  1133. * parsed, and all of that other good stuff.
  1134. *
  1135. * @return boolean True if successful, false otherwise
  1136. */
  1137. public function init()
  1138. {
  1139. // Check absolute bare minimum requirements.
  1140. if (!extension_loaded('xml') || !extension_loaded('pcre'))
  1141. {
  1142. $this->error = 'XML or PCRE extensions not loaded!';
  1143. return false;
  1144. }
  1145. // Then check the xml extension is sane (i.e., libxml 2.7.x issue on PHP < 5.2.9 and libxml 2.7.0 to 2.7.2 on any version) if we don't have xmlreader.
  1146. elseif (!extension_loaded('xmlreader'))
  1147. {
  1148. static $xml_is_sane = null;
  1149. if ($xml_is_sane === null)
  1150. {
  1151. $parser_check = xml_parser_create();
  1152. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  1153. xml_parser_free($parser_check);
  1154. $xml_is_sane = isset($values[0]['value']);
  1155. }
  1156. if (!$xml_is_sane)
  1157. {
  1158. return false;
  1159. }
  1160. }
  1161. // The default sanitize class gets set in the constructor, check if it has
  1162. // changed.
  1163. if ($this->registry->get_class('Sanitize') !== 'SimplePie_Sanitize') {
  1164. $this->sanitize = $this->registry->create('Sanitize');
  1165. }
  1166. if (method_exists($this->sanitize, 'set_registry'))
  1167. {
  1168. $this->sanitize->set_registry($this->registry);
  1169. }
  1170. // Pass whatever was set with config options over to the sanitizer.
  1171. // Pass the classes in for legacy support; new classes should use the registry instead
  1172. $this->sanitize->pass_cache_data($this->cache, $this->cache_location, $this->cache_name_function, $this->registry->get_class('Cache'));
  1173. $this->sanitize->pass_file_data($this->registry->get_class('File'), $this->timeout, $this->useragent, $this->force_fsockopen, $this->curl_options);
  1174. if (!empty($this->multifeed_url))
  1175. {
  1176. $i = 0;
  1177. $success = 0;
  1178. $this->multifeed_objects = array();
  1179. $this->error = array();
  1180. foreach ($this->multifeed_url as $url)
  1181. {
  1182. $this->multifeed_objects[$i] = clone $this;
  1183. $this->multifeed_objects[$i]->set_feed_url($url);
  1184. $single_success = $this->multifeed_objects[$i]->init();
  1185. $success |= $single_success;
  1186. if (!$single_success)
  1187. {
  1188. $this->error[$i] = $this->multifeed_objects[$i]->error();
  1189. }
  1190. $i++;
  1191. }
  1192. return (bool) $success;
  1193. }
  1194. elseif ($this->feed_url === null && $this->raw_data === null)
  1195. {
  1196. return false;
  1197. }
  1198. $this->error = null;
  1199. $this->data = array();
  1200. $this->check_modified = false;
  1201. $this->multifeed_objects = array();
  1202. $cache = false;
  1203. if ($this->feed_url !== null)
  1204. {
  1205. $parsed_feed_url = $this->registry->call('Misc', 'parse_url', array($this->feed_url));
  1206. // Decide whether to enable caching
  1207. if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1208. {
  1209. $url = $this->feed_url . ($this->force_feed ? '#force_feed' : '');
  1210. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $url), 'spc'));
  1211. }
  1212. // Fetch the data via SimplePie_File into $this->raw_data
  1213. if (($fetched = $this->fetch_data($cache)) === true)
  1214. {
  1215. return true;
  1216. }
  1217. elseif ($fetched === false) {
  1218. return false;
  1219. }
  1220. list($headers, $sniffed) = $fetched;
  1221. }
  1222. // Empty response check
  1223. if(empty($this->raw_data)){
  1224. $this->error = "A feed could not be found at `$this->feed_url`. Empty body.";
  1225. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1226. return false;
  1227. }
  1228. // Set up array of possible encodings
  1229. $encodings = array();
  1230. // First check to see if input has been overridden.
  1231. if ($this->input_encoding !== false)
  1232. {
  1233. $encodings[] = strtoupper($this->input_encoding);
  1234. }
  1235. $application_types = array('application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity');
  1236. $text_types = array('text/xml', 'text/xml-external-parsed-entity');
  1237. // RFC 3023 (only applies to sniffed content)
  1238. if (isset($sniffed))
  1239. {
  1240. if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml')
  1241. {
  1242. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1243. {
  1244. $encodings[] = strtoupper($charset[1]);
  1245. }
  1246. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1247. $encodings[] = 'UTF-8';
  1248. }
  1249. elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml')
  1250. {
  1251. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1252. {
  1253. $encodings[] = strtoupper($charset[1]);
  1254. }
  1255. $encodings[] = 'US-ASCII';
  1256. }
  1257. // Text MIME-type default
  1258. elseif (substr($sniffed, 0, 5) === 'text/')
  1259. {
  1260. $encodings[] = 'UTF-8';
  1261. }
  1262. }
  1263. // Fallback to XML 1.0 Appendix F.1/UTF-8/ISO-8859-1
  1264. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($this->raw_data, &$this->registry)));
  1265. $encodings[] = 'UTF-8';
  1266. $encodings[] = 'ISO-8859-1';
  1267. // There's no point in trying an encoding twice
  1268. $encodings = array_unique($encodings);
  1269. // Loop through each possible encoding, till we return something, or run out of possibilities
  1270. foreach ($encodings as $encoding)
  1271. {
  1272. // Change the encoding to UTF-8 (as we always use UTF-8 internally)
  1273. if ($utf8_data = $this->registry->call('Misc', 'change_encoding', array($this->raw_data, $encoding, 'UTF-8')))
  1274. {
  1275. // Create new parser
  1276. $parser = $this->registry->create('Parser');
  1277. // If it's parsed fine
  1278. if ($parser->parse($utf8_data, 'UTF-8', $this->permanent_url))
  1279. {
  1280. $this->data = $parser->get_data();
  1281. if (!($this->get_type() & ~SIMPLEPIE_TYPE_NONE))
  1282. {
  1283. $this->error = "A feed could not be found at `$this->feed_url`. This does not appear to be a valid RSS or Atom feed.";
  1284. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1285. return false;
  1286. }
  1287. if (isset($headers))
  1288. {
  1289. $this->data['headers'] = $headers;
  1290. }
  1291. $this->data['build'] = SIMPLEPIE_BUILD;
  1292. // Cache the file if caching is enabled
  1293. if ($cache && !$cache->save($this))
  1294. {
  1295. trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1296. }
  1297. return true;
  1298. }
  1299. }
  1300. }
  1301. if (isset($parser))
  1302. {
  1303. // We have an error, just set SimplePie_Misc::error to it and quit
  1304. $this->error = $this->feed_url;
  1305. $this->error .= sprintf(' is invalid XML, likely due to invalid characters. XML error: %s at line %d, column %d', $parser->get_error_string(), $parser->get_current_line(), $parser->get_current_column());
  1306. }
  1307. else
  1308. {
  1309. $this->error = 'The data could not be converted to UTF-8.';
  1310. if (!extension_loaded('mbstring') && !extension_loaded('iconv') && !class_exists('\UConverter')) {
  1311. $this->error .= ' You MUST have either the iconv, mbstring or intl (PHP 5.5+) extension installed and enabled.';
  1312. } else {
  1313. $missingExtensions = array();
  1314. if (!extension_loaded('iconv')) {
  1315. $missingExtensions[] = 'iconv';
  1316. }
  1317. if (!extension_loaded('mbstring')) {
  1318. $missingExtensions[] = 'mbstring';
  1319. }
  1320. if (!class_exists('\UConverter')) {
  1321. $missingExtensions[] = 'intl (PHP 5.5+)';
  1322. }
  1323. $this->error .= ' Try installing/enabling the ' . implode(' or ', $missingExtensions) . ' extension.';
  1324. }
  1325. }
  1326. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1327. return false;
  1328. }
  1329. /**
  1330. * Fetch the data via SimplePie_File
  1331. *
  1332. * If the data is already cached, attempt to fetch it from there instead
  1333. * @param SimplePie_Cache|false $cache Cache handler, or false to not load from the cache
  1334. * @return array|true Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type
  1335. */
  1336. protected function fetch_data(&$cache)
  1337. {
  1338. // If it's enabled, use the cache
  1339. if ($cache)
  1340. {
  1341. // Load the Cache
  1342. $this->data = $cache->load();
  1343. if (!empty($this->data))
  1344. {
  1345. // If the cache is for an outdated build of SimplePie
  1346. if (!isset($this->data['build']) || $this->data['build'] !== SIMPLEPIE_BUILD)
  1347. {
  1348. $cache->unlink();
  1349. $this->data = array();
  1350. }
  1351. // If we've hit a collision just rerun it with caching disabled
  1352. elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url)
  1353. {
  1354. $cache = false;
  1355. $this->data = array();
  1356. }
  1357. // If we've got a non feed_url stored (if the page isn't actually a feed, or is a redirect) use that URL.
  1358. elseif (isset($this->data['feed_url']))
  1359. {
  1360. // If the autodiscovery cache is still valid use it.
  1361. if ($cache->mtime() + $this->autodiscovery_cache_duration > time())
  1362. {
  1363. // Do not need to do feed autodiscovery yet.
  1364. if ($this->data['feed_url'] !== $this->data['url'])
  1365. {
  1366. $this->set_feed_url($this->data['feed_url']);
  1367. return $this->init();
  1368. }
  1369. $cache->unlink();
  1370. $this->data = array();
  1371. }
  1372. }
  1373. // Check if the cache has been updated
  1374. elseif ($cache->mtime() + $this->cache_duration < time())
  1375. {
  1376. // Want to know if we tried to send last-modified and/or etag headers
  1377. // when requesting this file. (Note that it's up to the file to
  1378. // support this, but we don't always send the headers either.)
  1379. $this->check_modified = true;
  1380. if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))
  1381. {
  1382. $headers = array(
  1383. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  1384. );
  1385. if (isset($this->data['headers']['last-modified']))
  1386. {
  1387. $headers['if-modified-since'] = $this->data['headers']['last-modified'];
  1388. }
  1389. if (isset($this->data['headers']['etag']))
  1390. {
  1391. $headers['if-none-match'] = $this->data['headers']['etag'];
  1392. }
  1393. $file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1394. if ($file->success)
  1395. {
  1396. if ($file->status_code === 304)
  1397. {
  1398. // Set raw_data to false here too, to signify that the cache
  1399. // is still valid.
  1400. $this->raw_data = false;
  1401. $cache->touch();
  1402. return true;
  1403. }
  1404. }
  1405. else
  1406. {
  1407. $this->check_modified = false;
  1408. if($this->force_cache_fallback)
  1409. {
  1410. $cache->touch();
  1411. return true;
  1412. }
  1413. unset($file);
  1414. }
  1415. }
  1416. }
  1417. // If the cache is still valid, just return true
  1418. else
  1419. {
  1420. $this->raw_data = false;
  1421. return true;
  1422. }
  1423. }
  1424. // If the cache is empty, delete it
  1425. else
  1426. {
  1427. $cache->unlink();
  1428. $this->data = array();
  1429. }
  1430. }
  1431. // If we don't already have the file (it'll only exist if we've opened it to check if the cache has been modified), open it.
  1432. if (!isset($file))
  1433. {
  1434. if ($this->file instanceof SimplePie_File && $this->file->url === $this->feed_url)
  1435. {
  1436. $file =& $this->file;
  1437. }
  1438. else
  1439. {
  1440. $headers = array(
  1441. 'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
  1442. );
  1443. $file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  1444. }
  1445. }
  1446. // If the file connection has an error, set SimplePie::error to that and quit
  1447. if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1448. {
  1449. $this->error = $file->error;
  1450. return !empty($this->data);
  1451. }
  1452. if (!$this->force_feed)
  1453. {
  1454. // Check if the supplied URL is a feed, if it isn't, look for it.
  1455. $locate = $this->registry->create('Locator', array(&$file, $this->timeout, $this->useragent, $this->max_checked_feeds, $this->force_fsockopen, $this->curl_options));
  1456. if (!$locate->is_feed($file))
  1457. {
  1458. $copyStatusCode = $file->status_code;
  1459. $copyContentType = $file->headers['content-type'];
  1460. try
  1461. {
  1462. $microformats = false;
  1463. if (class_exists('DOMXpath') && function_exists('Mf2\parse')) {
  1464. $doc = new DOMDocument();
  1465. @$doc->loadHTML($file->body);
  1466. $xpath = new DOMXpath($doc);
  1467. // Check for both h-feed and h-entry, as both a feed with no entries
  1468. // and a list of entries without an h-feed wrapper are both valid.
  1469. $query = '//*[contains(concat(" ", @class, " "), " h-feed ") or '.
  1470. 'contains(concat(" ", @class, " "), " h-entry ")]';
  1471. $result = $xpath->query($query);
  1472. $microformats = $result->length !== 0;
  1473. }
  1474. // Now also do feed discovery, but if microformats were found don't
  1475. // overwrite the current value of file.
  1476. $discovered = $locate->find($this->autodiscovery,
  1477. $this->all_discovered_feeds);
  1478. if ($microformats)
  1479. {
  1480. if ($hub = $locate->get_rel_link('hub'))
  1481. {
  1482. $self = $locate->get_rel_link('self');
  1483. $this->store_links($file, $hub, $self);
  1484. }
  1485. // Push the current file onto all_discovered feeds so the user can
  1486. // be shown this as one of the options.
  1487. if (isset($this->all_discovered_feeds)) {
  1488. $this->all_discovered_feeds[] = $file;
  1489. }
  1490. }
  1491. else
  1492. {
  1493. if ($discovered)
  1494. {
  1495. $file = $discovered;
  1496. }
  1497. else
  1498. {
  1499. // We need to unset this so that if SimplePie::set_file() has
  1500. // been called that object is untouched
  1501. unset($file);
  1502. $this->error = "A feed could not be found at `$this->feed_url`; the status code is `$copyStatusCode` and content-type is `$copyContentType`";
  1503. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1504. return false;
  1505. }
  1506. }
  1507. }
  1508. catch (SimplePie_Exception $e)
  1509. {
  1510. // We need to unset this so that if SimplePie::set_file() has been called that object is untouched
  1511. unset($file);
  1512. // This is usually because DOMDocument doesn't exist
  1513. $this->error = $e->getMessage();
  1514. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, $e->getFile(), $e->getLine()));
  1515. return false;
  1516. }
  1517. if ($cache)
  1518. {
  1519. $this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
  1520. if (!$cache->save($this))
  1521. {
  1522. trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  1523. }
  1524. $cache = $this->registry->call('Cache', 'get_handler', array($this->cache_location, call_user_func($this->cache_name_function, $file->url), 'spc'));
  1525. }
  1526. }
  1527. $this->feed_url = $file->url;
  1528. $locate = null;
  1529. }
  1530. $this->raw_data = $file->body;
  1531. $this->permanent_url = $file->permanent_url;
  1532. $headers = $file->headers;
  1533. $sniffer = $this->registry->create('Content_Type_Sniffer', array(&$file));
  1534. $sniffed = $sniffer->get_type();
  1535. return array($headers, $sniffed);
  1536. }
  1537. /**
  1538. * Get the error message for the occured error
  1539. *
  1540. * @return string|array Error message, or array of messages for multifeeds
  1541. */
  1542. public function error()
  1543. {
  1544. return $this->error;
  1545. }
  1546. /**
  1547. * Get the raw XML
  1548. *
  1549. * This is the same as the old `$feed->enable_xml_dump(true)`, but returns
  1550. * the data instead of printing it.
  1551. *
  1552. * @return string|boolean Raw XML data, false if the cache is used
  1553. */
  1554. public function get_raw_data()
  1555. {
  1556. return $this->raw_data;
  1557. }
  1558. /**
  1559. * Get the character encoding used for output
  1560. *
  1561. * @since Preview Release
  1562. * @return string
  1563. */
  1564. public function get_encoding()
  1565. {
  1566. return $this->sanitize->output_encoding;
  1567. }
  1568. /**
  1569. * Send the content-type header with correct encoding
  1570. *
  1571. * This method ensures that the SimplePie-enabled page is being served with
  1572. * the correct {@link http://www.iana.org/assignments/media-types/ mime-type}
  1573. * and character encoding HTTP headers (character encoding determined by the
  1574. * {@see set_output_encoding} config option).
  1575. *
  1576. * This won't work properly if any content or whitespace has already been
  1577. * sent to the browser, because it relies on PHP's
  1578. * {@link http://php.net/header header()} function, and these are the
  1579. * circumstances under which the function works.
  1580. *
  1581. * Because it's setting these settings for the entire page (as is the nature
  1582. * of HTTP headers), this should only be used once per page (again, at the
  1583. * top).
  1584. *
  1585. * @param string $mime MIME type to serve the page as
  1586. */
  1587. public function handle_content_type($mime = 'text/html')
  1588. {
  1589. if (!headers_sent())
  1590. {
  1591. $header = "Content-type: $mime;";
  1592. if ($this->get_encoding())
  1593. {
  1594. $header .= ' charset=' . $this->get_encoding();
  1595. }
  1596. else
  1597. {
  1598. $header .= ' charset=UTF-8';
  1599. }
  1600. header($header);
  1601. }
  1602. }
  1603. /**
  1604. * Get the type of the feed
  1605. *
  1606. * This returns a SIMPLEPIE_TYPE_* constant, which can be tested against
  1607. * using {@link http://php.net/language.operators.bitwise bitwise operators}
  1608. *
  1609. * @since 0.8 (usage changed to using constants in 1.0)
  1610. * @see SIMPLEPIE_TYPE_NONE Unknown.
  1611. * @see SIMPLEPIE_TYPE_RSS_090 RSS 0.90.
  1612. * @see SIMPLEPIE_TYPE_RSS_091_NETSCAPE RSS 0.91 (Netscape).
  1613. * @see SIMPLEPIE_TYPE_RSS_091_USERLAND RSS 0.91 (Userland).
  1614. * @see SIMPLEPIE_TYPE_RSS_091 RSS 0.91.
  1615. * @see SIMPLEPIE_TYPE_RSS_092 RSS 0.92.
  1616. * @see SIMPLEPIE_TYPE_RSS_093 RSS 0.93.
  1617. * @see SIMPLEPIE_TYPE_RSS_094 RSS 0.94.
  1618. * @see SIMPLEPIE_TYPE_RSS_10 RSS 1.0.
  1619. * @see SIMPLEPIE_TYPE_RSS_20 RSS 2.0.x.
  1620. * @see SIMPLEPIE_TY…

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