PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/rss_parser/libraries/SimplePie/Core.php

https://github.com/cswebgrl/RSS-Parser
PHP | 2576 lines | 1672 code | 210 blank | 694 comment | 179 complexity | f7483e98aaa1a8651088817420a102a4 MD5 | raw file

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-dev
  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-dev');
  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. * Whether we're running on PHP5
  312. */
  313. define('SIMPLEPIE_PHP5', version_compare(PHP_VERSION, '5.0.0', '>='));
  314. /**
  315. * No file source
  316. */
  317. define('SIMPLEPIE_FILE_SOURCE_NONE', 0);
  318. /**
  319. * Remote file source
  320. */
  321. define('SIMPLEPIE_FILE_SOURCE_REMOTE', 1);
  322. /**
  323. * Local file source
  324. */
  325. define('SIMPLEPIE_FILE_SOURCE_LOCAL', 2);
  326. /**
  327. * fsockopen() file source
  328. */
  329. define('SIMPLEPIE_FILE_SOURCE_FSOCKOPEN', 4);
  330. /**
  331. * cURL file source
  332. */
  333. define('SIMPLEPIE_FILE_SOURCE_CURL', 8);
  334. /**
  335. * file_get_contents() file source
  336. */
  337. define('SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS', 16);
  338. /**
  339. * SimplePie
  340. *
  341. * @package SimplePie
  342. */
  343. class SimplePie_Core
  344. {
  345. /**
  346. * @var array Raw data
  347. * @access private
  348. */
  349. public $data = array();
  350. /**
  351. * @var mixed Error string
  352. * @access private
  353. */
  354. public $error;
  355. /**
  356. * @var object Instance of SimplePie_Sanitize (or other class)
  357. * @see SimplePie::set_sanitize_class()
  358. * @access private
  359. */
  360. public $sanitize;
  361. /**
  362. * @var string SimplePie Useragent
  363. * @see SimplePie::set_useragent()
  364. * @access private
  365. */
  366. public $useragent = SIMPLEPIE_USERAGENT;
  367. /**
  368. * @var string Feed URL
  369. * @see SimplePie::set_feed_url()
  370. * @access private
  371. */
  372. public $feed_url;
  373. /**
  374. * @var object Instance of SimplePie_File to use as a feed
  375. * @see SimplePie::set_file()
  376. * @access private
  377. */
  378. public $file;
  379. /**
  380. * @var string Raw feed data
  381. * @see SimplePie::set_raw_data()
  382. * @access private
  383. */
  384. public $raw_data;
  385. /**
  386. * @var int Timeout for fetching remote files
  387. * @see SimplePie::set_timeout()
  388. * @access private
  389. */
  390. public $timeout = 10;
  391. /**
  392. * @var bool Forces fsockopen() to be used for remote files instead
  393. * of cURL, even if a new enough version is installed
  394. * @see SimplePie::force_fsockopen()
  395. * @access private
  396. */
  397. public $force_fsockopen = false;
  398. /**
  399. * @var bool Force the given data/URL to be treated as a feed no matter what
  400. * it appears like
  401. * @see SimplePie::force_feed()
  402. * @access private
  403. */
  404. public $force_feed = false;
  405. /**
  406. * @var bool Enable/Disable XML dump
  407. * @see SimplePie::enable_xml_dump()
  408. * @access private
  409. */
  410. public $xml_dump = false;
  411. /**
  412. * @var bool Enable/Disable Caching
  413. * @see SimplePie::enable_cache()
  414. * @access private
  415. */
  416. public $cache = true;
  417. /**
  418. * @var int Cache duration (in seconds)
  419. * @see SimplePie::set_cache_duration()
  420. * @access private
  421. */
  422. public $cache_duration = 3600;
  423. /**
  424. * @var int Auto-discovery cache duration (in seconds)
  425. * @see SimplePie::set_autodiscovery_cache_duration()
  426. * @access private
  427. */
  428. public $autodiscovery_cache_duration = 604800; // 7 Days.
  429. /**
  430. * @var string Cache location (relative to executing script)
  431. * @see SimplePie::set_cache_location()
  432. * @access private
  433. */
  434. public $cache_location = './cache';
  435. /**
  436. * @var string Function that creates the cache filename
  437. * @see SimplePie::set_cache_name_function()
  438. * @access private
  439. */
  440. public $cache_name_function = 'md5';
  441. /**
  442. * @var bool Reorder feed by date descending
  443. * @see SimplePie::enable_order_by_date()
  444. * @access private
  445. */
  446. public $order_by_date = true;
  447. /**
  448. * @var mixed Force input encoding to be set to the follow value
  449. * (false, or anything type-cast to false, disables this feature)
  450. * @see SimplePie::set_input_encoding()
  451. * @access private
  452. */
  453. public $input_encoding = false;
  454. /**
  455. * @var int Feed Autodiscovery Level
  456. * @see SimplePie::set_autodiscovery_level()
  457. * @access private
  458. */
  459. public $autodiscovery = SIMPLEPIE_LOCATOR_ALL;
  460. public $registry;
  461. /**
  462. * @var int Maximum number of feeds to check with autodiscovery
  463. * @see SimplePie::set_max_checked_feeds()
  464. * @access private
  465. */
  466. public $max_checked_feeds = 10;
  467. /**
  468. * @var array All the feeds found during the autodiscovery process
  469. * @see SimplePie::get_all_discovered_feeds()
  470. * @access private
  471. */
  472. public $all_discovered_feeds = array();
  473. /**
  474. * @var string Web-accessible path to the handler_image.php file.
  475. * @see SimplePie::set_image_handler()
  476. * @access private
  477. */
  478. public $image_handler = '';
  479. /**
  480. * @var array Stores the URLs when multiple feeds are being initialized.
  481. * @see SimplePie::set_feed_url()
  482. * @access private
  483. */
  484. public $multifeed_url = array();
  485. /**
  486. * @var array Stores SimplePie objects when multiple feeds initialized.
  487. * @access private
  488. */
  489. public $multifeed_objects = array();
  490. /**
  491. * @var array Stores the get_object_vars() array for use with multifeeds.
  492. * @see SimplePie::set_feed_url()
  493. * @access private
  494. */
  495. public $config_settings = null;
  496. /**
  497. * @var integer Stores the number of items to return per-feed with multifeeds.
  498. * @see SimplePie::set_item_limit()
  499. * @access private
  500. */
  501. public $item_limit = 0;
  502. /**
  503. * @var array Stores the default attributes to be stripped by strip_attributes().
  504. * @see SimplePie::strip_attributes()
  505. * @access private
  506. */
  507. public $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc');
  508. /**
  509. * @var array Stores the default tags to be stripped by strip_htmltags().
  510. * @see SimplePie::strip_htmltags()
  511. * @access private
  512. */
  513. public $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
  514. /**
  515. * The SimplePie class contains feed level data and options
  516. *
  517. * There are two ways that you can create a new SimplePie object. The first
  518. * is by passing a feed URL as a parameter to the SimplePie constructor
  519. * (as well as optionally setting the cache location and cache expiry). This
  520. * will initialise the whole feed with all of the default settings, and you
  521. * can begin accessing methods and properties immediately.
  522. *
  523. * The second way is to create the SimplePie object with no parameters
  524. * at all. This will enable you to set configuration options. After setting
  525. * them, you must initialise the feed using $feed->init(). At that point the
  526. * object's methods and properties will be available to you. This format is
  527. * what is used throughout this documentation.
  528. *
  529. * @access public
  530. * @since 1.0 Preview Release
  531. */
  532. public function __construct()
  533. {
  534. if (version_compare(PHP_VERSION, '5.0', '<'))
  535. {
  536. trigger_error('PHP 4.x is no longer supported. Please upgrade to PHP 5.2 or newer.');
  537. die();
  538. }
  539. // Other objects, instances created here so we can set options on them
  540. $this->sanitize = new SimplePie_Sanitize();
  541. $this->registry = new SimplePie_Registry();
  542. if (func_num_args() > 0)
  543. {
  544. trigger_error('Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_location() directly.');
  545. }
  546. }
  547. /**
  548. * Used for converting object to a string
  549. */
  550. public function __toString()
  551. {
  552. return md5(serialize($this->data));
  553. }
  554. /**
  555. * Remove items that link back to this before destroying this object
  556. */
  557. public function __destruct()
  558. {
  559. if ((version_compare(PHP_VERSION, '5.3', '<') || !gc_enabled()) && !ini_get('zend.ze1_compatibility_mode'))
  560. {
  561. if (!empty($this->data['items']))
  562. {
  563. foreach ($this->data['items'] as $item)
  564. {
  565. $item->__destruct();
  566. }
  567. unset($item, $this->data['items']);
  568. }
  569. if (!empty($this->data['ordered_items']))
  570. {
  571. foreach ($this->data['ordered_items'] as $item)
  572. {
  573. $item->__destruct();
  574. }
  575. unset($item, $this->data['ordered_items']);
  576. }
  577. }
  578. }
  579. /**
  580. * Force the given data/URL to be treated as a feed no matter what it
  581. * appears like
  582. *
  583. * @access public
  584. * @since 1.1
  585. * @param bool $enable Force the given data/URL to be treated as a feed
  586. */
  587. public function force_feed($enable = false)
  588. {
  589. $this->force_feed = (bool) $enable;
  590. }
  591. /**
  592. * This is the URL of the feed you want to parse.
  593. *
  594. * This allows you to enter the URL of the feed you want to parse, or the
  595. * website you want to try to use auto-discovery on. This takes priority
  596. * over any set raw data.
  597. *
  598. * You can set multiple feeds to mash together by passing an array instead
  599. * of a string for the $url. Remember that with each additional feed comes
  600. * additional processing and resources.
  601. *
  602. * @access public
  603. * @since 1.0 Preview Release
  604. * @param mixed $url This is the URL (or array of URLs) that you want to parse.
  605. * @see SimplePie::set_raw_data()
  606. */
  607. public function set_feed_url($url)
  608. {
  609. $this->multifeed_url = array();
  610. if (is_array($url))
  611. {
  612. foreach ($url as $value)
  613. {
  614. $this->multifeed_url[] = $this->registry->call('Misc', 'fix_protocol', array($value, 1));
  615. }
  616. }
  617. else
  618. {
  619. $this->feed_url = $this->registry->call('Misc', 'fix_protocol', array($url, 1));
  620. }
  621. }
  622. /**
  623. * Provides an instance of SimplePie_File to use as a feed
  624. *
  625. * @access public
  626. * @param object &$file Instance of SimplePie_File (or subclass)
  627. * @return bool True on success, false on failure
  628. */
  629. public function set_file(&$file)
  630. {
  631. if ($file instanceof SimplePie_File)
  632. {
  633. $this->feed_url = $file->url;
  634. $this->file =& $file;
  635. return true;
  636. }
  637. return false;
  638. }
  639. /**
  640. * Allows you to use a string of RSS/Atom data instead of a remote feed.
  641. *
  642. * If you have a feed available as a string in PHP, you can tell SimplePie
  643. * to parse that data string instead of a remote feed. Any set feed URL
  644. * takes precedence.
  645. *
  646. * @access public
  647. * @since 1.0 Beta 3
  648. * @param string $data RSS or Atom data as a string.
  649. * @see SimplePie::set_feed_url()
  650. */
  651. public function set_raw_data($data)
  652. {
  653. $this->raw_data = $data;
  654. }
  655. /**
  656. * Allows you to override the default timeout for fetching remote feeds.
  657. *
  658. * This allows you to change the maximum time the feed's server to respond
  659. * and send the feed back.
  660. *
  661. * @access public
  662. * @since 1.0 Beta 3
  663. * @param int $timeout The maximum number of seconds to spend waiting to retrieve a feed.
  664. */
  665. public function set_timeout($timeout = 10)
  666. {
  667. $this->timeout = (int) $timeout;
  668. }
  669. /**
  670. * Forces SimplePie to use fsockopen() instead of the preferred cURL
  671. * functions.
  672. *
  673. * @access public
  674. * @since 1.0 Beta 3
  675. * @param bool $enable Force fsockopen() to be used
  676. */
  677. public function force_fsockopen($enable = false)
  678. {
  679. $this->force_fsockopen = (bool) $enable;
  680. }
  681. /**
  682. * Enables/disables caching in SimplePie.
  683. *
  684. * This option allows you to disable caching all-together in SimplePie.
  685. * However, disabling the cache can lead to longer load times.
  686. *
  687. * @access public
  688. * @since 1.0 Preview Release
  689. * @param bool $enable Enable caching
  690. */
  691. public function enable_cache($enable = true)
  692. {
  693. $this->cache = (bool) $enable;
  694. }
  695. /**
  696. * Set the length of time (in seconds) that the contents of a feed
  697. * will be cached.
  698. *
  699. * @access public
  700. * @param int $seconds The feed content cache duration.
  701. */
  702. public function set_cache_duration($seconds = 3600)
  703. {
  704. $this->cache_duration = (int) $seconds;
  705. }
  706. /**
  707. * Set the length of time (in seconds) that the autodiscovered feed
  708. * URL will be cached.
  709. *
  710. * @access public
  711. * @param int $seconds The autodiscovered feed URL cache duration.
  712. */
  713. public function set_autodiscovery_cache_duration($seconds = 604800)
  714. {
  715. $this->autodiscovery_cache_duration = (int) $seconds;
  716. }
  717. /**
  718. * Set the file system location where the cached files should be stored.
  719. *
  720. * @access public
  721. * @param string $location The file system location.
  722. */
  723. public function set_cache_location($location = './cache')
  724. {
  725. $this->cache_location = (string) $location;
  726. }
  727. /**
  728. * Determines whether feed items should be sorted into reverse chronological order.
  729. *
  730. * @access public
  731. * @param bool $enable Sort as reverse chronological order.
  732. */
  733. public function enable_order_by_date($enable = true)
  734. {
  735. $this->order_by_date = (bool) $enable;
  736. }
  737. /**
  738. * Allows you to override the character encoding reported by the feed.
  739. *
  740. * @access public
  741. * @param string $encoding Character encoding.
  742. */
  743. public function set_input_encoding($encoding = false)
  744. {
  745. if ($encoding)
  746. {
  747. $this->input_encoding = (string) $encoding;
  748. }
  749. else
  750. {
  751. $this->input_encoding = false;
  752. }
  753. }
  754. /**
  755. * Set how much feed autodiscovery to do
  756. *
  757. * @access public
  758. * @see SIMPLEPIE_LOCATOR_NONE
  759. * @see SIMPLEPIE_LOCATOR_AUTODISCOVERY
  760. * @see SIMPLEPIE_LOCATOR_LOCAL_EXTENSION
  761. * @see SIMPLEPIE_LOCATOR_LOCAL_BODY
  762. * @see SIMPLEPIE_LOCATOR_REMOTE_EXTENSION
  763. * @see SIMPLEPIE_LOCATOR_REMOTE_BODY
  764. * @see SIMPLEPIE_LOCATOR_ALL
  765. * @param int $level Feed Autodiscovery Level (level can be a
  766. * combination of the above constants, see bitwise OR operator)
  767. */
  768. public function set_autodiscovery_level($level = SIMPLEPIE_LOCATOR_ALL)
  769. {
  770. $this->autodiscovery = (int) $level;
  771. }
  772. /**
  773. * Get the class registry
  774. *
  775. * Use this to override SimplePie's default classes
  776. * @see SimplePie_Registry
  777. * @return SimplePie_Registry
  778. */
  779. public function &get_registry()
  780. {
  781. return $this->registry;
  782. }
  783. /**#@+
  784. * Useful when you are overloading or extending SimplePie's default classes.
  785. *
  786. * @deprecated Use {@see get_registry()} instead
  787. * @param string $class Name of custom class.
  788. * @link http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.extends PHP5 extends documentation
  789. */
  790. /**
  791. * Allows you to change which class SimplePie uses for caching.
  792. */
  793. public function set_cache_class($class = 'SimplePie_Cache')
  794. {
  795. return $this->registry->register('Cache', $class, true);
  796. }
  797. /**
  798. * Allows you to change which class SimplePie uses for auto-discovery.
  799. */
  800. public function set_locator_class($class = 'SimplePie_Locator')
  801. {
  802. return $this->registry->register('Locator', $class, true);
  803. }
  804. /**
  805. * Allows you to change which class SimplePie uses for XML parsing.
  806. */
  807. public function set_parser_class($class = 'SimplePie_Parser')
  808. {
  809. return $this->registry->register('Parser', $class, true);
  810. }
  811. /**
  812. * Allows you to change which class SimplePie uses for remote file fetching.
  813. */
  814. public function set_file_class($class = 'SimplePie_File')
  815. {
  816. return $this->registry->register('File', $class, true);
  817. }
  818. /**
  819. * Allows you to change which class SimplePie uses for data sanitization.
  820. */
  821. public function set_sanitize_class($class = 'SimplePie_Sanitize')
  822. {
  823. return $this->registry->register('Sanitize', $class, true);
  824. }
  825. /**
  826. * Allows you to change which class SimplePie uses for handling feed items.
  827. */
  828. public function set_item_class($class = 'SimplePie_Item')
  829. {
  830. return $this->registry->register('Item', $class, true);
  831. }
  832. /**
  833. * Allows you to change which class SimplePie uses for handling author data.
  834. */
  835. public function set_author_class($class = 'SimplePie_Author')
  836. {
  837. return $this->registry->register('Author', $class, true);
  838. }
  839. /**
  840. * Allows you to change which class SimplePie uses for handling category data.
  841. */
  842. public function set_category_class($class = 'SimplePie_Category')
  843. {
  844. return $this->registry->register('Category', $class, true);
  845. }
  846. /**
  847. * Allows you to change which class SimplePie uses for feed enclosures.
  848. */
  849. public function set_enclosure_class($class = 'SimplePie_Enclosure')
  850. {
  851. return $this->registry->register('Enclosure', $class, true);
  852. }
  853. /**
  854. * Allows you to change which class SimplePie uses for <media:text> captions
  855. */
  856. public function set_caption_class($class = 'SimplePie_Caption')
  857. {
  858. return $this->registry->register('Caption', $class, true);
  859. }
  860. /**
  861. * Allows you to change which class SimplePie uses for <media:copyright>
  862. */
  863. public function set_copyright_class($class = 'SimplePie_Copyright')
  864. {
  865. return $this->registry->register('Copyright', $class, true);
  866. }
  867. /**
  868. * Allows you to change which class SimplePie uses for <media:credit>
  869. */
  870. public function set_credit_class($class = 'SimplePie_Credit')
  871. {
  872. return $this->registry->register('Credit', $class, true);
  873. }
  874. /**
  875. * Allows you to change which class SimplePie uses for <media:rating>
  876. */
  877. public function set_rating_class($class = 'SimplePie_Rating')
  878. {
  879. return $this->registry->register('Rating', $class, true);
  880. }
  881. /**
  882. * Allows you to change which class SimplePie uses for <media:restriction>
  883. */
  884. public function set_restriction_class($class = 'SimplePie_Restriction')
  885. {
  886. return $this->registry->register('Restriction', $class, true);
  887. }
  888. /**
  889. * Allows you to change which class SimplePie uses for content-type sniffing.
  890. */
  891. public function set_content_type_sniffer_class($class = 'SimplePie_Content_Type_Sniffer')
  892. {
  893. return $this->registry->register('Content_Type_Sniffer', $class, true);
  894. }
  895. /**
  896. * Allows you to change which class SimplePie uses item sources.
  897. */
  898. public function set_source_class($class = 'SimplePie_Source')
  899. {
  900. return $this->registry->register('Source', $class, true);
  901. }
  902. /**#@-*/
  903. /**
  904. * Allows you to override the default user agent string.
  905. *
  906. * @access public
  907. * @param string $ua New user agent string.
  908. */
  909. public function set_useragent($ua = SIMPLEPIE_USERAGENT)
  910. {
  911. $this->useragent = (string) $ua;
  912. }
  913. /**
  914. * Set callback function to create cache filename with
  915. *
  916. * @access public
  917. * @param mixed $function Callback function
  918. */
  919. public function set_cache_name_function($function = 'md5')
  920. {
  921. if (is_callable($function))
  922. {
  923. $this->cache_name_function = $function;
  924. }
  925. }
  926. /**
  927. * Set options to make SP as fast as possible. Forgoes a
  928. * substantial amount of data sanitization in favor of speed.
  929. *
  930. * @access public
  931. * @param bool $set Whether to set them or not
  932. */
  933. public function set_stupidly_fast($set = false)
  934. {
  935. if ($set)
  936. {
  937. $this->enable_order_by_date(false);
  938. $this->remove_div(false);
  939. $this->strip_comments(false);
  940. $this->strip_htmltags(false);
  941. $this->strip_attributes(false);
  942. $this->set_image_handler(false);
  943. }
  944. }
  945. /**
  946. * Set maximum number of feeds to check with autodiscovery
  947. *
  948. * @access public
  949. * @param int $max Maximum number of feeds to check
  950. */
  951. public function set_max_checked_feeds($max = 10)
  952. {
  953. $this->max_checked_feeds = (int) $max;
  954. }
  955. public function remove_div($enable = true)
  956. {
  957. $this->sanitize->remove_div($enable);
  958. }
  959. public function strip_htmltags($tags = '', $encode = null)
  960. {
  961. if ($tags === '')
  962. {
  963. $tags = $this->strip_htmltags;
  964. }
  965. $this->sanitize->strip_htmltags($tags);
  966. if ($encode !== null)
  967. {
  968. $this->sanitize->encode_instead_of_strip($tags);
  969. }
  970. }
  971. public function encode_instead_of_strip($enable = true)
  972. {
  973. $this->sanitize->encode_instead_of_strip($enable);
  974. }
  975. public function strip_attributes($attribs = '')
  976. {
  977. if ($attribs === '')
  978. {
  979. $attribs = $this->strip_attributes;
  980. }
  981. $this->sanitize->strip_attributes($attribs);
  982. }
  983. public function set_output_encoding($encoding = 'UTF-8')
  984. {
  985. $this->sanitize->set_output_encoding($encoding);
  986. }
  987. public function strip_comments($strip = false)
  988. {
  989. $this->sanitize->strip_comments($strip);
  990. }
  991. /**
  992. * Set element/attribute key/value pairs of HTML attributes
  993. * containing URLs that need to be resolved relative to the feed
  994. *
  995. * @access public
  996. * @since 1.0
  997. * @param array $element_attribute Element/attribute key/value pairs
  998. */
  999. public function set_url_replacements($element_attribute = array('a' => 'href', 'area' => 'href', 'blockquote' => 'cite', 'del' => 'cite', 'form' => 'action', 'img' => array('longdesc', 'src'), 'input' => 'src', 'ins' => 'cite', 'q' => 'cite'))
  1000. {
  1001. $this->sanitize->set_url_replacements($element_attribute);
  1002. }
  1003. /**
  1004. * Set the handler to enable the display of cached images.
  1005. *
  1006. * @access public
  1007. * @param str $page Web-accessible path to the handler_image.php file.
  1008. * @param str $qs The query string that the value should be passed to.
  1009. */
  1010. public function set_image_handler($page = false, $qs = 'i')
  1011. {
  1012. if ($page !== false)
  1013. {
  1014. $this->sanitize->set_image_handler($page . '?' . $qs . '=');
  1015. }
  1016. else
  1017. {
  1018. $this->image_handler = '';
  1019. }
  1020. }
  1021. /**
  1022. * Set the limit for items returned per-feed with multifeeds.
  1023. *
  1024. * @access public
  1025. * @param integer $limit The maximum number of items to return.
  1026. */
  1027. public function set_item_limit($limit = 0)
  1028. {
  1029. $this->item_limit = (int) $limit;
  1030. }
  1031. public function init()
  1032. {
  1033. // Check absolute bare minimum requirements.
  1034. if ((function_exists('version_compare') && version_compare(PHP_VERSION, '5.0', '<')) || !extension_loaded('xml') || !extension_loaded('pcre'))
  1035. {
  1036. return false;
  1037. }
  1038. // 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.
  1039. elseif (!extension_loaded('xmlreader'))
  1040. {
  1041. static $xml_is_sane = null;
  1042. if ($xml_is_sane === null)
  1043. {
  1044. $parser_check = xml_parser_create();
  1045. xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
  1046. xml_parser_free($parser_check);
  1047. $xml_is_sane = isset($values[0]['value']);
  1048. }
  1049. if (!$xml_is_sane)
  1050. {
  1051. return false;
  1052. }
  1053. }
  1054. if (method_exists($this->sanitize, 'set_registry'))
  1055. {
  1056. $this->sanitize->set_registry($this->registry);
  1057. }
  1058. // Pass whatever was set with config options over to the sanitizer.
  1059. // Pass the classes in for legacy support; new classes should use the registry instead
  1060. $this->sanitize->pass_cache_data($this->cache, $this->cache_location, $this->cache_name_function, $this->registry->get_class('Cache'));
  1061. $this->sanitize->pass_file_data($this->registry->get_class('File'), $this->timeout, $this->useragent, $this->force_fsockopen);
  1062. if (!empty($this->multifeed_url))
  1063. {
  1064. $i = 0;
  1065. $success = 0;
  1066. $this->multifeed_objects = array();
  1067. foreach ($this->multifeed_url as $url)
  1068. {
  1069. $this->multifeed_objects[$i] = clone $this;
  1070. $this->multifeed_objects[$i]->set_feed_url($url);
  1071. $success |= $this->multifeed_objects[$i]->init();
  1072. $i++;
  1073. }
  1074. return (bool) $success;
  1075. }
  1076. elseif ($this->feed_url === null && $this->raw_data === null)
  1077. {
  1078. return false;
  1079. }
  1080. $this->error = null;
  1081. $this->data = array();
  1082. $this->multifeed_objects = array();
  1083. $cache = false;
  1084. if ($this->feed_url !== null)
  1085. {
  1086. $parsed_feed_url = $this->registry->call('Misc', 'parse_url', array($this->feed_url));
  1087. // Decide whether to enable caching
  1088. if ($this->cache && $parsed_feed_url['scheme'] !== '')
  1089. {
  1090. $cache = $this->registry->call('Cache', 'create', array($this->cache_location, call_user_func($this->cache_name_function, $this->feed_url), 'spc'));
  1091. }
  1092. // If it's enabled and we don't want an XML dump, use the cache
  1093. if ($cache && !$this->xml_dump)
  1094. {
  1095. // Load the Cache
  1096. $this->data = $cache->load();
  1097. if (!empty($this->data))
  1098. {
  1099. // If the cache is for an outdated build of SimplePie
  1100. if (!isset($this->data['build']) || $this->data['build'] !== SIMPLEPIE_BUILD)
  1101. {
  1102. $cache->unlink();
  1103. $this->data = array();
  1104. }
  1105. // If we've hit a collision just rerun it with caching disabled
  1106. elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url)
  1107. {
  1108. $cache = false;
  1109. $this->data = array();
  1110. }
  1111. // If we've got a non feed_url stored (if the page isn't actually a feed, or is a redirect) use that URL.
  1112. elseif (isset($this->data['feed_url']))
  1113. {
  1114. // If the autodiscovery cache is still valid use it.
  1115. if ($cache->mtime() + $this->autodiscovery_cache_duration > time())
  1116. {
  1117. // Do not need to do feed autodiscovery yet.
  1118. if ($this->data['feed_url'] !== $this->data['url']) {
  1119. $this->set_feed_url($this->data['feed_url']);
  1120. return $this->init();
  1121. }
  1122. $cache->unlink();
  1123. $this->data = array();
  1124. }
  1125. }
  1126. // Check if the cache has been updated
  1127. elseif ($cache->mtime() + $this->cache_duration < time())
  1128. {
  1129. // If we have last-modified and/or etag set
  1130. if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))
  1131. {
  1132. $headers = array(
  1133. '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',
  1134. );
  1135. if (isset($this->data['headers']['last-modified']))
  1136. {
  1137. $headers['if-modified-since'] = $this->data['headers']['last-modified'];
  1138. }
  1139. if (isset($this->data['headers']['etag']))
  1140. {
  1141. $headers['if-none-match'] = $this->data['headers']['etag'];
  1142. }
  1143. $file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen));
  1144. if ($file->success)
  1145. {
  1146. if ($file->status_code === 304)
  1147. {
  1148. $cache->touch();
  1149. return true;
  1150. }
  1151. $headers = $file->headers;
  1152. }
  1153. else
  1154. {
  1155. unset($file);
  1156. }
  1157. }
  1158. }
  1159. // If the cache is still valid, just return true
  1160. else
  1161. {
  1162. $this->raw_data = false;
  1163. return true;
  1164. }
  1165. }
  1166. // If the cache is empty, delete it
  1167. else
  1168. {
  1169. $cache->unlink();
  1170. $this->data = array();
  1171. }
  1172. }
  1173. // 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.
  1174. if (!isset($file))
  1175. {
  1176. if ($this->file instanceof SimplePie_File && $this->file->url === $this->feed_url)
  1177. {
  1178. $file =& $this->file;
  1179. }
  1180. else
  1181. {
  1182. $headers = array(
  1183. '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',
  1184. );
  1185. $file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen));
  1186. }
  1187. }
  1188. // If the file connection has an error, set SimplePie::error to that and quit
  1189. if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  1190. {
  1191. $this->error = $file->error;
  1192. return !empty($this->data);
  1193. }
  1194. if (!$this->force_feed)
  1195. {
  1196. // Check if the supplied URL is a feed, if it isn't, look for it.
  1197. $locate = $this->registry->create('Locator', array(&$file, $this->timeout, $this->useragent, $this->max_checked_feeds));
  1198. if (!$locate->is_feed($file))
  1199. {
  1200. // We need to unset this so that if SimplePie::set_file() has been called that object is untouched
  1201. unset($file);
  1202. if (!($file = $locate->find($this->autodiscovery, $this->all_discovered_feeds)))
  1203. {
  1204. $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.";
  1205. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1206. return false;
  1207. }
  1208. if ($cache)
  1209. {
  1210. $this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
  1211. if (!$cache->save($this))
  1212. {
  1213. 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);
  1214. }
  1215. $cache = $this->registry->call('Cache', 'create', array($this->cache_location, call_user_func($this->cache_name_function, $file->url), 'spc'));
  1216. }
  1217. $this->feed_url = $file->url;
  1218. }
  1219. $locate = null;
  1220. }
  1221. $headers = $file->headers;
  1222. $data = $file->body;
  1223. $sniffer = $this->registry->create('Content_Type_Sniffer', array(&$file));
  1224. $sniffed = $sniffer->get_type();
  1225. }
  1226. else
  1227. {
  1228. $data = $this->raw_data;
  1229. }
  1230. // This is exposed via get_raw_data()
  1231. $this->raw_data = $data;
  1232. // Set up array of possible encodings
  1233. $encodings = array();
  1234. // First check to see if input has been overridden.
  1235. if ($this->input_encoding !== false)
  1236. {
  1237. $encodings[] = $this->input_encoding;
  1238. }
  1239. $application_types = array('application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity');
  1240. $text_types = array('text/xml', 'text/xml-external-parsed-entity');
  1241. // RFC 3023 (only applies to sniffed content)
  1242. if (isset($sniffed))
  1243. {
  1244. if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml')
  1245. {
  1246. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1247. {
  1248. $encodings[] = strtoupper($charset[1]);
  1249. }
  1250. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($data, &$this->registry)));
  1251. $encodings[] = 'UTF-8';
  1252. }
  1253. elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml')
  1254. {
  1255. if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset))
  1256. {
  1257. $encodings[] = $charset[1];
  1258. }
  1259. $encodings[] = 'US-ASCII';
  1260. }
  1261. // Text MIME-type default
  1262. elseif (substr($sniffed, 0, 5) === 'text/')
  1263. {
  1264. $encodings[] = 'US-ASCII';
  1265. }
  1266. }
  1267. // Fallback to XML 1.0 Appendix F.1/UTF-8/ISO-8859-1
  1268. $encodings = array_merge($encodings, $this->registry->call('Misc', 'xml_encoding', array($data, &$this->registry)));
  1269. $encodings[] = 'UTF-8';
  1270. $encodings[] = 'ISO-8859-1';
  1271. // There's no point in trying an encoding twice
  1272. $encodings = array_unique($encodings);
  1273. // If we want the XML, just output that with the most likely encoding and quit
  1274. if ($this->xml_dump)
  1275. {
  1276. header('Content-type: text/xml; charset=' . $encodings[0]);
  1277. echo $data;
  1278. exit;
  1279. }
  1280. // Loop through each possible encoding, till we return something, or run out of possibilities
  1281. foreach ($encodings as $encoding)
  1282. {
  1283. // Change the encoding to UTF-8 (as we always use UTF-8 internally)
  1284. if ($utf8_data = $this->registry->call('Misc', 'change_encoding', array($data, $encoding, 'UTF-8')))
  1285. {
  1286. // Create new parser
  1287. $parser = $this->registry->create('Parser');
  1288. // If it's parsed fine
  1289. if ($parser->parse($utf8_data, 'UTF-8'))
  1290. {
  1291. $this->data = $parser->get_data();
  1292. if (!($this->get_type() & ~SIMPLEPIE_TYPE_NONE))
  1293. {
  1294. $this->error = "A feed could not be found at $this->feed_url. This does not appear to be a valid RSS or Atom feed.";
  1295. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1296. return false;
  1297. }
  1298. if (isset($headers))
  1299. {
  1300. $this->data['headers'] = $headers;
  1301. }
  1302. $this->data['build'] = SIMPLEPIE_BUILD;
  1303. // Cache the file if caching is enabled
  1304. if ($cache && !$cache->save($this))
  1305. {
  1306. 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);
  1307. }
  1308. return true;
  1309. }
  1310. }
  1311. }
  1312. if (isset($parser))
  1313. {
  1314. // We have an error, just set SimplePie_Misc::error to it and quit
  1315. $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());
  1316. }
  1317. else
  1318. {
  1319. $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.';
  1320. }
  1321. $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
  1322. return false;
  1323. }
  1324. /**
  1325. * Return the error message for the occured error
  1326. *
  1327. * @access public
  1328. * @return string Error message
  1329. */
  1330. public function error()
  1331. {
  1332. return $this->error;
  1333. }
  1334. /**
  1335. * Return the raw XML
  1336. *
  1337. * This is the same as setting `$xml_dump = true;`, but returns
  1338. * the data instead of printing it.
  1339. *
  1340. * @return string|boolean Raw XML data, false if the cache is used
  1341. */
  1342. public function get_raw_data()
  1343. {
  1344. return $this->raw_data;
  1345. }
  1346. public function get_encoding()
  1347. {
  1348. return $this->sanitize->output_encoding;
  1349. }
  1350. public function handle_content_type($mime = 'text/html')
  1351. {
  1352. if (!headers_sent())
  1353. {
  1354. $header = "Content-type: $mime;";
  1355. if ($this->get_encoding())
  1356. {
  1357. $header .= ' charset=' . $this->get_encoding();
  1358. }
  1359. else
  1360. {
  1361. $header .= ' charset=UTF-8';
  1362. }
  1363. header($header);
  1364. }
  1365. }
  1366. public function get_type()
  1367. {
  1368. if (!isset($this->data['type']))
  1369. {
  1370. $this->data['type'] = SIMPLEPIE_TYPE_ALL;
  1371. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed']))
  1372. {
  1373. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_10;
  1374. }
  1375. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed']))
  1376. {
  1377. $this->data['type'] &= SIMPLEPIE_TYPE_ATOM_03;
  1378. }
  1379. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF']))
  1380. {
  1381. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['channel'])
  1382. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['image'])
  1383. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item'])
  1384. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_10]['textinput']))
  1385. {
  1386. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_10;
  1387. }
  1388. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['channel'])
  1389. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['image'])
  1390. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item'])
  1391. || isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_090]['textinput']))
  1392. {
  1393. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_090;
  1394. }
  1395. }
  1396. elseif (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss']))
  1397. {
  1398. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_ALL;
  1399. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1400. {
  1401. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version']))
  1402. {
  1403. case '0.91':
  1404. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091;
  1405. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1406. {
  1407. switch (trim($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['skiphours']['hour'][0]['data']))
  1408. {
  1409. case '0':
  1410. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_NETSCAPE;
  1411. break;
  1412. case '24':
  1413. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_091_USERLAND;
  1414. break;
  1415. }
  1416. }
  1417. break;
  1418. case '0.92':
  1419. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_092;
  1420. break;
  1421. case '0.93':
  1422. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_093;
  1423. break;
  1424. case '0.94':
  1425. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_094;
  1426. break;
  1427. case '2.0':
  1428. $this->data['type'] &= SIMPLEPIE_TYPE_RSS_20;
  1429. break;
  1430. }
  1431. }
  1432. }
  1433. else
  1434. {
  1435. $this->data['type'] = SIMPLEPIE_TYPE_NONE;
  1436. }
  1437. }
  1438. return $this->data['type'];
  1439. }
  1440. /**
  1441. * @todo If we have a perm redirect we should return the new URL
  1442. * @todo When we make the above change, let's support <itunes:new-feed-url> as well
  1443. * @todo Also, |atom:link|@rel=self
  1444. */
  1445. public function subscribe_url()
  1446. {
  1447. if ($this->feed_url !== null)
  1448. {
  1449. return $this->sanitize($this->feed_url, SIMPLEPIE_CONSTRUCT_IRI);
  1450. }
  1451. else
  1452. {
  1453. return null;
  1454. }
  1455. }
  1456. public function get_feed_tags($namespace, $tag)
  1457. {
  1458. $type = $this->get_type();
  1459. if ($type & SIMPLEPIE_TYPE_ATOM_10)
  1460. {
  1461. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag]))
  1462. {
  1463. return $this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag];
  1464. }
  1465. }
  1466. if ($type & SIMPLEPIE_TYPE_ATOM_03)
  1467. {
  1468. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag]))
  1469. {
  1470. return $this->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag];
  1471. }
  1472. }
  1473. if ($type & SIMPLEPIE_TYPE_RSS_RDF)
  1474. {
  1475. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag]))
  1476. {
  1477. return $this->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag];
  1478. }
  1479. }
  1480. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1481. {
  1482. if (isset($this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][$namespace][$tag]))
  1483. {
  1484. return $this->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][$namespace][$tag];
  1485. }
  1486. }
  1487. return null;
  1488. }
  1489. public function get_channel_tags($namespace, $tag)
  1490. {
  1491. $type = $this->get_type();
  1492. if ($type & SIMPLEPIE_TYPE_ATOM_ALL)
  1493. {
  1494. if ($return = $this->get_feed_tags($namespace, $tag))
  1495. {
  1496. return $return;
  1497. }
  1498. }
  1499. if ($type & SIMPLEPIE_TYPE_RSS_10)
  1500. {
  1501. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'channel'))
  1502. {
  1503. if (isset($channel[0]['child'][$namespace][$tag]))
  1504. {
  1505. return $channel[0]['child'][$namespace][$tag];
  1506. }
  1507. }
  1508. }
  1509. if ($type & SIMPLEPIE_TYPE_RSS_090)
  1510. {
  1511. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'channel'))
  1512. {
  1513. if (isset($channel[0]['child'][$namespace][$tag]))
  1514. {
  1515. return $channel[0]['child'][$namespace][$tag];
  1516. }
  1517. }
  1518. }
  1519. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1520. {
  1521. if ($channel = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'channel'))
  1522. {
  1523. if (isset($channel[0]['child'][$namespace][$tag]))
  1524. {
  1525. return $channel[0]['child'][$namespace][$tag];
  1526. }
  1527. }
  1528. }
  1529. return null;
  1530. }
  1531. public function get_image_tags($namespace, $tag)
  1532. {
  1533. $type = $this->get_type();
  1534. if ($type & SIMPLEPIE_TYPE_RSS_10)
  1535. {
  1536. if ($image = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'image'))
  1537. {
  1538. if (isset($image[0]['child'][$namespace][$tag]))
  1539. {
  1540. return $image[0]['child'][$namespace][$tag];
  1541. }
  1542. }
  1543. }
  1544. if ($type & SIMPLEPIE_TYPE_RSS_090)
  1545. {
  1546. if ($image = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'image'))
  1547. {
  1548. if (isset($image[0]['child'][$namespace][$tag]))
  1549. {
  1550. return $image[0]['child'][$namespace][$tag];
  1551. }
  1552. }
  1553. }
  1554. if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
  1555. {
  1556. if ($image = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'image'))
  1557. {
  1558. if (isset($image[0]['child'][$namespace][$tag]))
  1559. {
  1560. return $image[0]['child'][$namespace][$tag];
  1561. }
  1562. }
  1563. }
  1564. return null;
  1565. }
  1566. public function get_base($element = array())
  1567. {
  1568. if (!($this->get_type() & SIMPLEPIE_TYPE_RSS_SYNDICATION) && !empty($element['xml_base_explicit']) && isset($element['xml_base']))
  1569. {
  1570. return $element['xml_base'];
  1571. }
  1572. elseif ($this->get_link() !== null)
  1573. {
  1574. return $this->get_link();
  1575. }
  1576. else
  1577. {
  1578. return $this->subscribe_url();
  1579. }
  1580. }
  1581. public function sanitize($data, $type, $base = '')
  1582. {
  1583. return $this->sanitize->sanitize($data, $type, $base);
  1584. }
  1585. public function get_title()
  1586. {
  1587. if ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'title'))
  1588. {
  1589. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_10_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  1590. }
  1591. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'title'))
  1592. {
  1593. return $this->sanitize($return[0]['data'], $this->registry->call('Misc', 'atom_03_construct_type', array($return[0]['attribs'])), $this->get_base($return[0]));
  1594. }
  1595. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'title'))
  1596. {
  1597. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  1598. }
  1599. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'title'))
  1600. {
  1601. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  1602. }
  1603. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'title'))
  1604. {
  1605. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_MAYBE_HTML, $this->get_base($return[0]));
  1606. }
  1607. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_11, 'title'))
  1608. {
  1609. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  1610. }
  1611. elseif ($return = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_DC_10, 'title'))
  1612. {
  1613. return $this->sanitize($return[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
  1614. }
  1615. else
  1616. {
  1617. return null;
  1618. }
  1619. }
  1620. public function get_category($key = 0)
  1621. {
  1622. $categories = $this->get_categories();
  1623. if (isset($categories[$key]))
  1624. {
  1625. return $categories[$key];
  1626. }
  1627. else
  1628. {
  1629. return null;
  1630. }
  1631. }
  1632. public function

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