PageRenderTime 72ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 3ms

/libraries/simplepie/simplepie.php

https://bitbucket.org/ankursaxena_2/joomla-platform
PHP | 15001 lines | 11791 code | 1089 blank | 2121 comment | 1153 complexity | d1c8a277f0cc128b5610db721c70eabd MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause

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

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