PageRenderTime 72ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 2ms

/social/extensions/yii-feed-widget/SimplePie.php

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

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