PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/library/Simplepie/Feed.php

https://bitbucket.org/aukhanev/xdn-wordpress31
PHP | 2787 lines | 1909 code | 189 blank | 689 comment | 218 complexity | d32b589e88017991afc4aa63c5922794 MD5 | raw file

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

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

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