PageRenderTime 55ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 2ms

/wp-includes/class-simplepie.php

https://bitbucket.org/stratworkouts/wordpress
PHP | 15300 lines | 12060 code | 1084 blank | 2156 comment | 1174 complexity | 05891a7601c417cb8f548d5d2bbd74fa MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

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

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

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