PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/html/AppCode/expressionengine/plugins/pi.magpie.php

https://github.com/w3bg/www.hsifin.com
PHP | 2733 lines | 1940 code | 326 blank | 467 comment | 253 complexity | b2421ff0bdca21ec11b61e1f5c11f8ac MD5 | raw file
Possible License(s): AGPL-3.0

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

  1. <?php
  2. /*
  3. =====================================================
  4. ExpressionEngine - by EllisLab
  5. -----------------------------------------------------
  6. http://expressionengine.com/
  7. -----------------------------------------------------
  8. Copyright (c) 2004 - 2010 EllisLab, Inc.
  9. =====================================================
  10. THIS IS COPYRIGHTED SOFTWARE
  11. PLEASE READ THE LICENSE AGREEMENT
  12. http://expressionengine.com/user_guide/license.html
  13. =====================================================
  14. File: pi.magpie.php
  15. -----------------------------------------------------
  16. Purpose: Magpie RSS plugin
  17. =====================================================
  18. */
  19. $plugin_info = array(
  20. 'pi_name' => 'Magpie RSS Parser',
  21. 'pi_version' => '1.3.5',
  22. 'pi_author' => 'Paul Burdick',
  23. 'pi_author_url' => 'http://expressionengine.com/',
  24. 'pi_description' => 'Retrieves and Parses RSS/Atom Feeds',
  25. 'pi_usage' => Magpie::usage()
  26. );
  27. Class Magpie {
  28. var $cache_name = 'magpie_cache'; // Name of cache directory
  29. var $cache_refresh = 360; // Period between cache refreshes (in minutes)
  30. var $cache_data = ''; // Data from cache file
  31. var $cache_path = ''; // Path to cache file.
  32. var $cache_tpath = ''; // Path to cache file's time file.
  33. var $page_url = ''; // URL being requested
  34. var $items = array(); // Information about items returned
  35. var $dates = array('lastupdate','linkcreated'); // Date elements
  36. var $return_data = ''; // Data sent back to Template parser
  37. /** -------------------------------------
  38. /** Constructor
  39. /** -------------------------------------*/
  40. function Magpie()
  41. {
  42. // Make a local reference of the ExpressionEngine super object
  43. $this->EE =& get_instance();
  44. /** -------------------------------
  45. /** Set Parameters
  46. /** -------------------------------*/
  47. $this->cache_refresh = ( ! $this->EE->TMPL->fetch_param('refresh')) ? $this->cache_refresh : $this->EE->TMPL->fetch_param('refresh');
  48. $this->page_url = ( ! $this->EE->TMPL->fetch_param('url')) ? '' : trim($this->EE->TMPL->fetch_param('url'));
  49. $limit = ( ! $this->EE->TMPL->fetch_param('limit')) ? 20 : $this->EE->TMPL->fetch_param('limit');
  50. $offset = ( ! $this->EE->TMPL->fetch_param('offset')) ? 0 : $this->EE->TMPL->fetch_param('offset');
  51. $template = $this->EE->TMPL->tagdata;
  52. if ($this->page_url == '')
  53. {
  54. return $this->return_data;
  55. }
  56. if ($this->EE->config->item('debug') == 2 OR ($this->EE->config->item('debug') == 1 && $this->EE->session->userdata['group_id'] == 1))
  57. {
  58. if( ! defined('MAGPIE_DEBUG'))
  59. {
  60. define('MAGPIE_DEBUG', 1);
  61. }
  62. }
  63. else
  64. {
  65. if ( ! defined('MAGPIE_DEBUG'))
  66. {
  67. define('MAGPIE_DEBUG', 0);
  68. }
  69. }
  70. /** -------------------------------
  71. /** Check and Retrive Cache
  72. /** -------------------------------*/
  73. if ( ! defined('MAGPIE_CACHE_DIR'))
  74. {
  75. define('MAGPIE_CACHE_DIR', APPPATH.'cache/'.$this->cache_name.'/');
  76. }
  77. if ( ! defined('MAGPIE_CACHE_AGE'))
  78. {
  79. define('MAGPIE_CACHE_AGE', $this->cache_refresh * 60);
  80. }
  81. $this->RSS = fetch_rss($this->page_url);
  82. if (count($this->RSS->items) == 0)
  83. {
  84. return $this->return_data;
  85. }
  86. /** -----------------------------------
  87. /** Parse Template - ITEMS
  88. /** -----------------------------------*/
  89. if (preg_match("/(".LD."items".RD."(.*?)".LD.'\/'.'items'.RD."|".LD."magpie:items".RD."(.*?)".LD.'\/'.'magpie:items'.RD.")/s", $template, $matches))
  90. {
  91. $items_data = '';
  92. $i = 0;
  93. if (count($this->RSS->items) > 0)
  94. {
  95. foreach($this->RSS->items as $item)
  96. {
  97. $i++;
  98. if ($i <= $offset) continue;
  99. $temp_data = $matches['1'];
  100. /** ----------------------------------------
  101. /** Quick and Dirty Conditionals
  102. /** ----------------------------------------*/
  103. if (stristr($temp_data, LD.'if'))
  104. {
  105. $tagdata = $this->EE->functions->prep_conditionals($temp_data, $item, '', 'magpie:');
  106. }
  107. /** ----------------------------------------
  108. /** Single Variables
  109. /** ----------------------------------------*/
  110. foreach($item as $key => $value)
  111. {
  112. if ( ! is_array($value))
  113. {
  114. $temp_data = str_replace(LD.$key.RD, $value, $temp_data);
  115. $temp_data = str_replace(LD.'magpie:'.$key.RD, $value, $temp_data);
  116. if ($key == 'atom_content')
  117. {
  118. $temp_data = str_replace(LD.'content'.RD, $value, $temp_data);
  119. $temp_data = str_replace(LD.'magpie:content'.RD, $value, $temp_data);
  120. }
  121. }
  122. else
  123. {
  124. foreach ($value as $vk => $vv)
  125. {
  126. $temp_data = str_replace(LD.$key.'_'.$vk.RD, $vv, $temp_data);
  127. $temp_data = str_replace(LD.'magpie:'.$key.'_'.$vk.RD, $vv, $temp_data);
  128. if ($key == 'dc')
  129. {
  130. $temp_data = str_replace(LD.$vk.RD, $vv, $temp_data);
  131. $temp_data = str_replace(LD.'magpie:'.$vk.RD, $vv, $temp_data);
  132. }
  133. }
  134. }
  135. }
  136. $items_data .= $temp_data;
  137. if ($i >= ($limit + $offset))
  138. {
  139. break;
  140. }
  141. }
  142. }
  143. /** ----------------------------------------
  144. /** Clean up left over variables
  145. /** ----------------------------------------*/
  146. $items_data = str_replace(LD.'exp:', 'TgB903He0mnv3dd098', $items_data);
  147. $items_data = str_replace(LD.'/exp:', 'Mu87ddk2QPoid990iod', $items_data);
  148. $items_data = preg_replace("/".LD."if.*?".RD.".+?".LD.'\/'."if".RD."/s", '', $items_data);
  149. $items_data = preg_replace("/".LD.".+?".RD."/", '', $items_data);
  150. $items_data = str_replace('TgB903He0mnv3dd098', LD.'exp:', $items_data);
  151. $items_data = str_replace('Mu87ddk2QPoid990iod', LD.'/exp:', $items_data);
  152. $template = str_replace($matches['0'], $items_data, $template);
  153. }
  154. /** -----------------------------------
  155. /** Parse Template
  156. /** -----------------------------------*/
  157. $channel_variables = array('title', 'link', 'modified', 'generator',
  158. 'copyright', 'description', 'language',
  159. 'pubdate', 'lastbuilddate', 'generator',
  160. 'tagline', 'creator', 'date', 'rights');
  161. $image_variables = array('title','url', 'link','description', 'width', 'height');
  162. foreach ($this->EE->TMPL->var_single as $key => $val)
  163. {
  164. /** ----------------------------------------
  165. /** {feed_version} - Version of RSS/Atom Feed
  166. /** ----------------------------------------*/
  167. if ($key == "feed_version" OR $key == "magpie:feed_version")
  168. {
  169. if ( ! isset($this->RSS->feed_version)) $this->RSS->feed_version = '';
  170. $template = $this->EE->TMPL->swap_var_single($val, $this->RSS->feed_version, $template);
  171. }
  172. /** ----------------------------------------
  173. /** {feed_type}
  174. /** ----------------------------------------*/
  175. if (($key == "feed_type" OR $key == "magpie:feed_type") && isset($this->RSS->feed_type))
  176. {
  177. if ( ! isset($this->RSS->feed_type)) $this->RSS->feed_type = '';
  178. $template = $this->EE->TMPL->swap_var_single($val, $this->RSS->feed_type, $template);
  179. }
  180. /** ----------------------------------------
  181. /** Image related variables
  182. /** ----------------------------------------*/
  183. foreach ($image_variables as $variable)
  184. {
  185. if ($key == 'image_'.$variable OR $key == 'magpie:image_'.$variable)
  186. {
  187. if ( ! isset($this->RSS->image[$variable])) $this->RSS->image[$variable] = '';
  188. $template = $this->EE->TMPL->swap_var_single($val, $this->RSS->image[$variable], $template);
  189. }
  190. }
  191. /** ----------------------------------------
  192. /** Channel related variables
  193. /** ----------------------------------------*/
  194. foreach ($channel_variables as $variable)
  195. {
  196. if ($key == 'channel_'.$variable OR $key == 'magpie:channel_'.$variable)
  197. {
  198. if ( ! isset($this->RSS->channel[$variable]))
  199. {
  200. $this->RSS->channel[$variable] = ( ! isset($this->RSS->channel['dc'][$variable])) ? '' : $this->RSS->channel['dc'][$variable];
  201. }
  202. $template = $this->EE->TMPL->swap_var_single($val, $this->RSS->channel[$variable], $template);
  203. }
  204. }
  205. /** ----------------------------------------
  206. /** {page_url}
  207. /** ----------------------------------------*/
  208. if ($key == 'page_url' OR $key == 'magpie:page_url')
  209. {
  210. $template = $this->EE->TMPL->swap_var_single($val, $this->page_url, $template);
  211. }
  212. }
  213. $this->return_data = &$template;
  214. }
  215. /** ----------------------------------------
  216. /** Plugin Usage
  217. /** ----------------------------------------*/
  218. function usage()
  219. {
  220. ob_start();
  221. ?>
  222. STEP ONE:
  223. Insert plugin tag into your template. Set parameters and variables.
  224. PARAMETERS:
  225. The tag has three parameters:
  226. 1. url - The URL of the RSS or Atom feed.
  227. 2. limit - Number of items to display from feed.
  228. 3. offset - Skip a certain number of items in the display of the feed.
  229. 4. refresh - How often to refresh the cache file in minutes. The plugin default is to refresh the cached file every three hours.
  230. Example opening tag: {exp:magpie url="http://expressionengine.com/feeds/rss/full/" limit="8" refresh="720"}
  231. SINGLE VARIABLES:
  232. feed_version - What version of RSS or Atom is this feed
  233. feed_type - What type of feed is this, Atom or RSS
  234. page_url - Page URL of the feed.
  235. image_title - [RSS] The contents of the &lt;title&gt; element contained within the sub-element &lt;channel&gt;
  236. image_url - [RSS] The contents of the &lt;url&gt; element contained within the sub-element &lt;channel&gt;
  237. image_link - [RSS] The contents of the &lt;link&gt; element contained within the sub-element &lt;channel&gt;
  238. image_description - [RSS] The contents of the optional &lt;description&gt; element contained within the sub-element &lt;channel&gt;
  239. image_width - [RSS] The contents of the optional &lt;width&gt; element contained within the sub-element &lt;channel&gt;
  240. image_height - [RSS] The contents of the optional &lt;height&gt; element contained within the sub-element &lt;channel&gt;
  241. channel_title - [ATOM/RSS-0.91/RSS-1.0/RSS-2.0]
  242. channel_link - [ATOM/RSS-0.91/RSS-1.0/RSS-2.0]
  243. channel_modified - [ATOM]
  244. channel_generator - [ATOM]
  245. channel_copyright - [ATOM]
  246. channel_description - [RSS-0.91/ATOM]
  247. channel_language - [RSS-0.91/RSS-1.0/RSS-2.0]
  248. channel_pubdate - [RSS-0.91]
  249. channel_lastbuilddate - [RSS-0.91]
  250. channel_tagline - [RSS-0.91/RSS-1.0/RSS-2.0]
  251. channel_creator - [RSS-1.0/RSS-2.0]
  252. channel_date - [RSS-1.0/RSS-2.0]
  253. channel_rights - [RSS-2.0]
  254. PAIR VARIABLES:
  255. Only one pair variable, {items}, is available, and it is for the entries/items in the RSS/Atom Feeds. This pair
  256. variable allows many different other single variables to be contained within it depending on the type of feed.
  257. title - [ATOM/RSS-0.91/RSS-1.0/RSS-2.0]
  258. link - [ATOM/RSS-0.91/RSS-1.0/RSS-2.0]
  259. description - [RSS-0.91/RSS-1.0/RSS-2.0]
  260. about - [RSS-1.0]
  261. atom_content - [ATOM]
  262. author_name - [ATOM]
  263. author_email - [ATOM]
  264. content - [ATOM/RSS-2.0]
  265. created - [ATOM]
  266. creator - [RSS-1.0]
  267. pubdate/date - (varies by feed design)
  268. description - [ATOM]
  269. id - [ATOM]
  270. issued - [ATOM]
  271. modified - [ATOM]
  272. subject - [ATOM/RSS-1.0]
  273. summary - [ATOM/RSS-1.0/RSS-2.0]
  274. EXAMPLE:
  275. {exp:magpie url="http://expressionengine.com/feeds/rss/full/" limit="10" refresh="720"}
  276. <ul>
  277. {items}
  278. <li><a href="{link}">{title}</a></li>
  279. {/items}
  280. </ul>
  281. {/exp:magpie}
  282. ***************************
  283. Version 1.2
  284. ***************************
  285. Complete Rewrite That Improved the Caching System Dramatically
  286. ***************************
  287. Version 1.2.1 + 1.2.2
  288. ***************************
  289. Bug Fixes
  290. ***************************
  291. Version 1.2.3
  292. ***************************
  293. Modified the code so that one can put 'magpie:' as a prefix on all plugin variables,
  294. which allows the embedding of this plugin in a {exp:channel:entries} tag and using
  295. that tag's variables in this plugin's parameter (url="" parameter, specifically).
  296. {exp:magpie url="http://expressionengine.com/feeds/rss/full/" limit="10" refresh="720"}
  297. <ul>
  298. {magpie:items}
  299. <li><a href="{magpie:link}">{magpie:title}</a></li>
  300. {/magpie:items}
  301. </ul>
  302. {/exp:magpie}
  303. ***************************
  304. Version 1.2.4
  305. ***************************
  306. Added the ability for the encoding to be parsed out of the XML feed and used to
  307. convert the feed's data into the encoding specified in the preferences. Requires
  308. that the Multibyte String (mbstring: http://us4.php.net/manual/en/ref.mbstring.php)
  309. library be compiled into PHP.
  310. ***************************
  311. Version 1.2.5
  312. ***************************
  313. Fixed a bug where the Magpie library was adding slashes to the cache directory
  314. without doing any sort of double slash checking.
  315. ***************************
  316. Version 1.3
  317. ***************************
  318. Fixed a bug where the channel and image variables were not showing up because of a bug
  319. introuced in 1.2.
  320. ***************************
  321. Version 1.3.1
  322. ***************************
  323. New parameter convert_entities="y" which will have any entities in the RSS feed converted
  324. before being parsed by the PHP XML parser. This is helpful because sometimes the XML
  325. Parser converts entities incorrectly. You have to empty your Magpie cache after enabling this setting.
  326. New parameter encoding="ISO-8859-1". Allows you to specify the encoding of the RSS
  327. feed, which is sometimes helpful when using the convert_encoding="y" parameter.
  328. ***************************
  329. Version 1.3.2
  330. ***************************
  331. Eliminated all of the darn encoding parameters previously being used and used the
  332. encoding abilities recently added to the Magpie library that attempts to do all of the
  333. converting early on.
  334. ***************************
  335. Version 1.3.3
  336. ***************************
  337. The Snoopy library that is included with the Magpie plugin by default was causing
  338. problems with the Snoopy library included in the Third Party Linklist module, so
  339. the name was changed to eliminate the conflict.
  340. ***************************
  341. Version 1.3.4
  342. ***************************
  343. The offset="" parameter was undocumented and had a bug. Fixed.
  344. ***************************
  345. Version 1.3.5
  346. ***************************
  347. Added ability to override caching options when using fetch_rss() directly.
  348. <?php
  349. $buffer = ob_get_contents();
  350. ob_end_clean();
  351. return $buffer;
  352. }
  353. } // END Magpie class
  354. /*
  355. // -------------------------------------------
  356. // BEGIN MagpieRSS Class
  357. // -------------------------------------------
  358. * Project: MagpieRSS: a simple RSS integration tool
  359. * File: rss_parse.inc - parse an RSS or Atom feed
  360. * return as a simple object.
  361. *
  362. * Handles RSS 0.9x, RSS 2.0, RSS 1.0, and Atom 0.3
  363. *
  364. * The lastest version of MagpieRSS can be obtained from:
  365. * http://magpierss.sourceforge.net
  366. *
  367. * For questions, help, comments, discussion, etc., please join the
  368. * Magpie mailing list:
  369. * magpierss-general@lists.sourceforge.net
  370. *
  371. * Author: Kellan Elliott-McCrea <kellan@protest.net>
  372. * Version: 0.6a
  373. * License: GPL
  374. *
  375. *
  376. * ABOUT MAGPIE's APPROACH TO PARSING:
  377. * - Magpie is based on expat, an XML parser, and therefore will only parse
  378. * valid XML files. This includes all properly constructed RSS or Atom.
  379. *
  380. * - Magpie is an inclusive parser. It will include any elements that
  381. * it can turn into a key value pair in the parsed feed object it returns.
  382. *
  383. * - Magpie supports namespaces, and will return any elements found in a
  384. * namespace in a sub-array, with the key point to that array being the
  385. * namespace prefix.
  386. * (e.g. if an item contains a <dc:date> element, then that date can
  387. * be accessed at $item['dc']['date']
  388. *
  389. * - Magpie supports nested elements by combining the names. If an item
  390. * includes XML like:
  391. * <author>
  392. * <name>Kellan</name>
  393. * </author>
  394. *
  395. * The name field is accessible at $item['author_name']
  396. *
  397. * - Magpie makes no attempt validate a feed beyond insuring that it
  398. * is valid XML.
  399. * RSS validators are readily available on the web at:
  400. * http://feeds.archive.org/validator/
  401. * http://www.ldodds.com/rss_validator/1.0/validator.html
  402. *
  403. *
  404. * EXAMPLE PARSED RSS ITEM:
  405. *
  406. * Magpie tries to parse RSS into easy to use PHP datastructures.
  407. *
  408. * For example, Magpie on encountering (a rather complex) RSS 1.0 item entry:
  409. *
  410. * <item rdf:about="http://protest.net/NorthEast/calendrome.cgi?span=event&#38;ID=210257">
  411. * <title>Weekly Peace Vigil</title>
  412. * <link>http://protest.net/NorthEast/calendrome.cgi?span=event&#38;ID=210257</link>
  413. * <description>Wear a white ribbon</description>
  414. * <dc:subject>Peace</dc:subject>
  415. * <ev:startdate>2002-06-01T11:00:00</ev:startdate>
  416. * <ev:location>Northampton, MA</ev:location>
  417. * <ev:type>Protest</ev:type>
  418. * </item>
  419. *
  420. * Would transform it into the following associative array, and push it
  421. * onto the array $rss-items
  422. *
  423. * array(
  424. * title => 'Weekly Peace Vigil',
  425. * link => 'http://protest.net/NorthEast/calendrome.cgi?span=event&#38;ID=210257',
  426. * description => 'Wear a white ribbon',
  427. * dc => array (
  428. * subject => 'Peace'
  429. * ),
  430. * ev => array (
  431. * startdate => '2002-06-01T11:00:00',
  432. * enddate => '2002-06-01T12:00:00',
  433. * type => 'Protest',
  434. * location => 'Northampton, MA'
  435. * )
  436. * )
  437. *
  438. *
  439. *
  440. * A FEW NOTES ON PARSING Atom FEEDS
  441. *
  442. * Atom support is considered alpha. Atom elements will be often be available
  443. * as their RSS equivalent, summary is available as description for example.
  444. *
  445. * Elements of mode=xml, as flattened into a single string, just as if they
  446. * had been wrapped in a CDATA container.
  447. *
  448. * See: http://laughingmeme.org/archives/001676.html
  449. *
  450. */
  451. define('RSS', 'RSS');
  452. define('ATOM', 'Atom');
  453. class MagpieRSS {
  454. /*
  455. * Hybrid parser, and object. (probably a bad idea! :)
  456. *
  457. * Useage Example:
  458. *
  459. * $some_rss = "<?xml version="1.0"......
  460. *
  461. * $rss = new MagpieRSS( $some_rss );
  462. *
  463. * // print rss chanel title
  464. * echo $rss->channel['title'];
  465. *
  466. * // print the title of each item
  467. * foreach ($rss->items as $item ) {
  468. * echo $item[title];
  469. * }
  470. *
  471. * see: rss_fetch.inc for a simpler interface
  472. */
  473. var $parser;
  474. var $current_item = array(); // item currently being parsed
  475. var $items = array(); // collection of parsed items
  476. var $channel = array(); // hash of channel fields
  477. var $textinput = array();
  478. var $image = array();
  479. var $feed_type;
  480. var $feed_version;
  481. var $encoding;
  482. // parser variables
  483. var $stack = array(); // parser stack
  484. var $inchannel = false;
  485. var $initem = false;
  486. var $incontent = false; // if in Atom <content mode="xml"> field
  487. var $intextinput = false;
  488. var $inimage = false;
  489. var $current_field = '';
  490. var $current_namespace = false;
  491. var $etag = false;
  492. var $ERROR = "";
  493. var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
  494. var $_KNOWN_ENCODINGS = array('UTF-8', 'US-ASCII', 'ISO-8859-1');
  495. /*======================================================================*\
  496. Function: MagpieRSS
  497. Purpose: Constructor, sets up XML parser,parses source,
  498. and populates object..
  499. Input: String containing the RSS to be parsed
  500. \*======================================================================*/
  501. function MagpieRSS ($source, $output_encoding='ISO-8859-1',
  502. $input_encoding=null, $detect_encoding=true) {
  503. // Make a local reference of the ExpressionEngine super object
  504. $this->EE =& get_instance();
  505. # if PHP xml isn't compiled in, die
  506. #
  507. if ( ! function_exists('xml_parser_create')) {
  508. $this->error( "Failed to load PHP's XML Extension. " .
  509. "http://www.php.net/manual/en/ref.xml.php",
  510. E_USER_ERROR );
  511. }
  512. list($parser, $source) = $this->create_parser($source,
  513. $output_encoding, $input_encoding, $detect_encoding);
  514. if ( ! is_resource($parser))
  515. {
  516. $this->error( "Failed to create an instance of PHP's XML parser. " .
  517. "http://www.php.net/manual/en/ref.xml.php",
  518. E_USER_ERROR );
  519. }
  520. # pass in parser, and a reference to this object
  521. # setup handlers
  522. #
  523. xml_set_object( $parser, $this );
  524. xml_set_element_handler($parser,
  525. 'feed_start_element', 'feed_end_element' );
  526. xml_set_character_data_handler( $parser, 'feed_cdata' );
  527. $status = @xml_parse($parser, $source);
  528. if ( ! $status ) {
  529. $errorcode = xml_get_error_code( $parser );
  530. if ( $errorcode != XML_ERROR_NONE ) {
  531. $xml_error = xml_error_string( $errorcode );
  532. $error_line = xml_get_current_line_number($parser);
  533. $error_col = xml_get_current_column_number($parser);
  534. $errormsg = "$xml_error at line $error_line, column $error_col";
  535. $this->error( $errormsg );
  536. return FALSE;
  537. }
  538. }
  539. xml_parser_free( $parser );
  540. $this->normalize();
  541. }
  542. function change_key_case($array)
  543. {
  544. $new_array = array();
  545. foreach($array as $key => $value)
  546. {
  547. $new_array[strtolower($key)] = $value;
  548. }
  549. return $new_array;
  550. }
  551. function feed_start_element($p, $element, &$attrs) {
  552. $el = $element = strtolower($element);
  553. if ( ! function_exists('array_change_key_case'))
  554. {
  555. $attrs = $this->change_key_case($attrs);
  556. }
  557. else
  558. {
  559. $attrs = array_change_key_case($attrs, CASE_LOWER);
  560. }
  561. // check for a namespace, and split if found
  562. $ns = false;
  563. if ( strpos( $element, ':' ) ) {
  564. list($ns, $el) = explode( ':', $element, 2);
  565. }
  566. if ( $ns and $ns != 'rdf' ) {
  567. $this->current_namespace = $ns;
  568. }
  569. # if feed type isn't set, then this is first element of feed
  570. # identify feed from root element
  571. #
  572. if ( ! isset($this->feed_type) ) {
  573. if ( $el == 'rdf' ) {
  574. $this->feed_type = RSS;
  575. $this->feed_version = '1.0';
  576. }
  577. elseif ( $el == 'rss' ) {
  578. $this->feed_type = RSS;
  579. $this->feed_version = $attrs['version'];
  580. }
  581. elseif ( $el == 'feed' ) {
  582. $this->feed_type = ATOM;
  583. $this->feed_version = $attrs['version'];
  584. $this->inchannel = true;
  585. }
  586. return;
  587. }
  588. if ( $el == 'channel' )
  589. {
  590. $this->inchannel = true;
  591. }
  592. elseif ($el == 'item' or $el == 'entry' )
  593. {
  594. $this->initem = true;
  595. if ( isset($attrs['rdf:about']) ) {
  596. $this->current_item['about'] = $attrs['rdf:about'];
  597. }
  598. }
  599. // if we're in the default namespace of an RSS feed,
  600. // record textinput or image fields
  601. elseif (
  602. $this->feed_type == RSS and
  603. $this->current_namespace == '' and
  604. $el == 'textinput' )
  605. {
  606. $this->intextinput = true;
  607. }
  608. elseif (
  609. $this->feed_type == RSS and
  610. $this->current_namespace == '' and
  611. $el == 'image' )
  612. {
  613. $this->inimage = true;
  614. }
  615. # handle atom content constructs
  616. elseif ( $this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
  617. {
  618. // avoid clashing w/ RSS mod_content
  619. if ($el == 'content' ) {
  620. $el = 'atom_content';
  621. }
  622. $this->incontent = $el;
  623. }
  624. // if inside an Atom content construct (e.g. content or summary) field treat tags as text
  625. elseif ($this->feed_type == ATOM and $this->incontent )
  626. {
  627. // if tags are inlined, then flatten
  628. $attrs_str = join(' ',
  629. array_map('map_attrs',
  630. array_keys($attrs),
  631. array_values($attrs) ) );
  632. $this->append_content( "<$element $attrs_str>" );
  633. array_unshift( $this->stack, $el );
  634. }
  635. // Atom support many links per containging element.
  636. // Magpie treats link elements of type rel='alternate'
  637. // as being equivalent to RSS's simple link element.
  638. //
  639. elseif ($this->feed_type == ATOM and $el == 'link' )
  640. {
  641. if ( isset($attrs['rel']) and $attrs['rel'] == 'alternate' )
  642. {
  643. $link_el = 'link';
  644. }
  645. else {
  646. $link_el = 'link_' . $attrs['rel'];
  647. }
  648. $this->append($link_el, $attrs['href']);
  649. }
  650. // set stack[0] to current element
  651. else {
  652. array_unshift($this->stack, $el);
  653. }
  654. }
  655. function feed_cdata ($p, $text) {
  656. if ($this->feed_type == ATOM and $this->incontent)
  657. {
  658. $this->append_content( $text );
  659. }
  660. else {
  661. $current_el = join('_', array_reverse($this->stack));
  662. $this->append($current_el, $text);
  663. }
  664. }
  665. function feed_end_element ($p, $el) {
  666. $el = strtolower($el);
  667. if ( $el == 'item' or $el == 'entry' )
  668. {
  669. $this->items[] = $this->current_item;
  670. $this->current_item = array();
  671. $this->initem = false;
  672. }
  673. elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'textinput' )
  674. {
  675. $this->intextinput = false;
  676. }
  677. elseif ($this->feed_type == RSS and $this->current_namespace == '' and $el == 'image' )
  678. {
  679. $this->inimage = false;
  680. }
  681. elseif ($this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
  682. {
  683. $this->incontent = false;
  684. }
  685. elseif ($el == 'channel' or $el == 'feed' )
  686. {
  687. $this->inchannel = false;
  688. }
  689. elseif ($this->feed_type == ATOM and $this->incontent ) {
  690. // balance tags properly
  691. // note: i don't think this is actually neccessary
  692. if ( $this->stack[0] == $el )
  693. {
  694. $this->append_content("</$el>");
  695. }
  696. else {
  697. $this->append_content("<$el />");
  698. }
  699. array_shift( $this->stack );
  700. }
  701. else {
  702. array_shift( $this->stack );
  703. }
  704. $this->current_namespace = false;
  705. }
  706. function concat (&$str1, $str2="") {
  707. if ( ! isset($str1) ) {
  708. $str1="";
  709. }
  710. $str1 .= $str2;
  711. }
  712. function append_content($text) {
  713. if ( $this->initem ) {
  714. $this->concat( $this->current_item[ $this->incontent ], $text );
  715. }
  716. elseif ( $this->inchannel ) {
  717. $this->concat( $this->channel[ $this->incontent ], $text );
  718. }
  719. }
  720. // smart append - field and namespace aware
  721. function append($el, $text) {
  722. if ( ! $el) {
  723. return;
  724. }
  725. if ( $this->current_namespace )
  726. {
  727. if ( $this->initem ) {
  728. $this->concat(
  729. $this->current_item[ $this->current_namespace ][ $el ], $text);
  730. }
  731. elseif ($this->inchannel) {
  732. $this->concat(
  733. $this->channel[ $this->current_namespace][ $el ], $text );
  734. }
  735. elseif ($this->intextinput) {
  736. $this->concat(
  737. $this->textinput[ $this->current_namespace][ $el ], $text );
  738. }
  739. elseif ($this->inimage) {
  740. $this->concat(
  741. $this->image[ $this->current_namespace ][ $el ], $text );
  742. }
  743. }
  744. else {
  745. if ( $this->initem ) {
  746. $this->concat(
  747. $this->current_item[ $el ], $text);
  748. }
  749. elseif ($this->intextinput) {
  750. $this->concat(
  751. $this->textinput[ $el ], $text );
  752. }
  753. elseif ($this->inimage) {
  754. $this->concat(
  755. $this->image[ $el ], $text );
  756. }
  757. elseif ($this->inchannel) {
  758. $this->concat(
  759. $this->channel[ $el ], $text );
  760. }
  761. }
  762. }
  763. function normalize () {
  764. // if atom populate rss fields
  765. if ( $this->is_atom() ) {
  766. $this->channel['descripton'] = ( ! isset($this->channel['tagline'])) ? '' : $this->channel['tagline'];
  767. for ( $i = 0; $i < count($this->items); $i++) {
  768. $item = $this->items[$i];
  769. if ( isset($item['summary']) )
  770. $item['description'] = $item['summary'];
  771. if ( isset($item['atom_content']))
  772. $item['content']['encoded'] = $item['atom_content'];
  773. $this->items[$i] = $item;
  774. }
  775. }
  776. elseif ( $this->is_rss() ) {
  777. $this->channel['tagline'] = ( ! isset($this->channel['description'])) ? '' : $this->channel['description'];
  778. for ( $i = 0; $i < count($this->items); $i++) {
  779. $item = $this->items[$i];
  780. if ( isset($item['description']))
  781. $item['summary'] = $item['description'];
  782. if ( isset($item['content']['encoded'] ) )
  783. $item['atom_content'] = $item['content']['encoded'];
  784. $this->items[$i] = $item;
  785. }
  786. }
  787. }
  788. function error ($errormsg, $lvl=E_USER_WARNING) {
  789. // append PHP's error message if track_errors enabled
  790. if ( isset($php_errormsg) )
  791. {
  792. $errormsg .= " ($php_errormsg)";
  793. }
  794. $this->ERROR = $errormsg;
  795. if (MAGPIE_DEBUG)
  796. {
  797. trigger_error($errormsg, $lvl);
  798. }
  799. else
  800. {
  801. error_log($errormsg, 0);
  802. }
  803. }
  804. function is_rss () {
  805. if ( $this->feed_type == RSS ) {
  806. return $this->feed_version;
  807. }
  808. else {
  809. return false;
  810. }
  811. }
  812. function is_atom() {
  813. if ( $this->feed_type == ATOM ) {
  814. return $this->feed_version;
  815. }
  816. else {
  817. return false;
  818. }
  819. }
  820. /**
  821. * return XML parser, and possibly re-encoded source
  822. *
  823. */
  824. function create_parser($source, $out_enc, $in_enc, $detect)
  825. {
  826. if ( substr(phpversion(),0,1) == 5) {
  827. $parser = $this->php5_create_parser($in_enc, $detect);
  828. }
  829. else {
  830. list($parser, $source) = $this->php4_create_parser($source, $in_enc, $detect);
  831. }
  832. $this->encoding = $this->EE->config->item('charset');
  833. if (in_array(strtolower($this->encoding), array('iso-8859-1', 'us-ascii', 'utf-8')))
  834. {
  835. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $this->encoding);
  836. }
  837. return array($parser, $source);
  838. }
  839. /**
  840. * Instantiate an XML parser under PHP5
  841. *
  842. * PHP5 will do a fine job of detecting input encoding
  843. * if passed an empty string as the encoding.
  844. *
  845. * All hail libxml2!
  846. *
  847. */
  848. function php5_create_parser($in_enc, $detect) {
  849. // by default php5 does a fine job of detecting input encodings
  850. if( ! $detect && $in_enc) {
  851. return xml_parser_create($in_enc);
  852. }
  853. else {
  854. return xml_parser_create('');
  855. }
  856. }
  857. /**
  858. * Instaniate an XML parser under PHP4
  859. *
  860. * Unfortunately PHP4's support for character encodings
  861. * and especially XML and character encodings sucks. As
  862. * long as the documents you parse only contain characters
  863. * from the ISO-8859-1 character set (a superset of ASCII,
  864. * and a subset of UTF-8) you're fine. However once you
  865. * step out of that comfy little world things get mad, bad,
  866. * and dangerous to know.
  867. *
  868. * The following code is based on SJM's work with FoF
  869. * @see http://minutillo.com/steve/channel/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
  870. *
  871. */
  872. function php4_create_parser($source, $in_enc, $detect) {
  873. if ( ! $detect ) {
  874. return array(xml_parser_create($in_enc), $source);
  875. }
  876. if ( ! $in_enc) {
  877. if (preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m', $source, $m)) {
  878. $in_enc = strtoupper($m[1]);
  879. $this->source_encoding = $in_enc;
  880. }
  881. else {
  882. $in_enc = 'UTF-8';
  883. }
  884. }
  885. if ($this->known_encoding($in_enc)) {
  886. return array(xml_parser_create($in_enc), $source);
  887. }
  888. // the dectected encoding is not one of the simple encodings PHP knows
  889. // attempt to use the iconv extension to
  890. // cast the XML to a known encoding
  891. // @see http://php.net/iconv
  892. if (function_exists('iconv')) {
  893. $encoded_source = iconv($in_enc,'UTF-8', $source);
  894. if ($encoded_source) {
  895. return array(xml_parser_create('UTF-8'), $encoded_source);
  896. }
  897. }
  898. // iconv didn't work, try mb_convert_encoding
  899. // @see http://php.net/mbstring
  900. if(function_exists('mb_convert_encoding')) {
  901. $encoded_source = mb_convert_encoding($source, 'UTF-8', $in_enc );
  902. if ($encoded_source) {
  903. return array(xml_parser_create('UTF-8'), $encoded_source);
  904. }
  905. }
  906. // else
  907. $this->error("Feed is in an unsupported character encoding. ($in_enc) " .
  908. "You may see strange artifacts, and mangled characters.",
  909. E_USER_NOTICE);
  910. return array(xml_parser_create(), $source);
  911. }
  912. function known_encoding($enc) {
  913. $enc = strtoupper($enc);
  914. if ( in_array($enc, $this->_KNOWN_ENCODINGS) ) {
  915. return $enc;
  916. }
  917. else {
  918. return false;
  919. }
  920. }
  921. /*======================================================================*\
  922. EVERYTHING BELOW HERE IS FOR DEBUGGING PURPOSES
  923. \*======================================================================*/
  924. function show_list () {
  925. echo "<ol>\n";
  926. foreach ($this->items as $item) {
  927. echo "<li>", $this->show_item( $item );
  928. }
  929. echo "</ol>";
  930. }
  931. function show_channel () {
  932. echo "channel:<br>";
  933. echo "<ul>";
  934. while ( list($key, $value) = each( $this->channel ) ) {
  935. echo "<li> $key: $value";
  936. }
  937. echo "</ul>";
  938. }
  939. function show_item ($item) {
  940. echo "item: $item[title]";
  941. echo "<ul>";
  942. while ( list($key, $value) = each($item) ) {
  943. if ( is_array($value) ) {
  944. echo "<br><b>$key</b>";
  945. echo "<ul>";
  946. while ( list( $ns_key, $ns_value) = each( $value ) ) {
  947. echo "<li>$ns_key: $ns_value";
  948. }
  949. echo "</ul>";
  950. }
  951. else {
  952. echo "<li> $key: $value";
  953. }
  954. }
  955. echo "</ul>";
  956. }
  957. /*======================================================================*\
  958. END DEBUGGING FUNCTIONS
  959. \*======================================================================*/
  960. } # end class RSS
  961. function map_attrs($k, $v) {
  962. return "$k=\"$v\"";
  963. }
  964. /*
  965. * Project: MagpieRSS: a simple RSS integration tool
  966. * File: rss_fetch.inc, a simple functional interface
  967. to fetching and parsing RSS files, via the
  968. function fetch_rss()
  969. * Author: Kellan Elliott-McCrea <kellan@protest.net>
  970. * License: GPL
  971. *
  972. * The lastest version of MagpieRSS can be obtained from:
  973. * http://magpierss.sourceforge.net
  974. *
  975. * For questions, help, comments, discussion, etc., please join the
  976. * Magpie mailing list:
  977. * magpierss-general@lists.sourceforge.net
  978. *
  979. */
  980. // Setup MAGPIE_DIR for use on hosts that don't include
  981. // the current path in include_path.
  982. // with thanks to rajiv and smarty
  983. if ( ! defined('DIR_SEP')) {
  984. define('DIR_SEP', DIRECTORY_SEPARATOR);
  985. }
  986. if ( ! defined('MAGPIE_DIR')) {
  987. define('MAGPIE_DIR', dirname(__FILE__) . DIR_SEP);
  988. }
  989. /*
  990. * CONSTANTS - redefine these in your script to change the
  991. * behaviour of fetch_rss() currently, most options effect the cache
  992. *
  993. * MAGPIE_CACHE_ON - Should Magpie cache parsed RSS objects?
  994. * For me a built in cache was essential to creating a "PHP-like"
  995. * feel to Magpie, see rss_cache.inc for rationale
  996. *
  997. *
  998. * MAGPIE_CACHE_DIR - Where should Magpie cache parsed RSS objects?
  999. * This should be a location that the webserver can write to. If this
  1000. * directory does not already exist Mapie will try to be smart and create
  1001. * it. This will often fail for permissions reasons.
  1002. *
  1003. *
  1004. * MAGPIE_CACHE_AGE - How long to store cached RSS objects? In seconds.
  1005. *
  1006. *
  1007. * MAGPIE_CACHE_FRESH_ONLY - If remote fetch fails, throw error
  1008. * instead of returning stale object?
  1009. *
  1010. * MAGPIE_DEBUG - Display debugging notices?
  1011. *
  1012. */
  1013. /*=======================================================================*\
  1014. Function: fetch_rss:
  1015. Purpose: return RSS object for the give url
  1016. maintain the cache
  1017. Input: url of RSS file
  1018. Output: parsed RSS object (see rss_parse.inc)
  1019. NOTES ON CACHEING:
  1020. If caching is on (MAGPIE_CACHE_ON) fetch_rss will first check the cache.
  1021. NOTES ON RETRIEVING REMOTE FILES:
  1022. If conditional gets are on (MAGPIE_CONDITIONAL_GET_ON) fetch_rss will
  1023. return a cached object, and touch the cache object upon recieving a
  1024. 304.
  1025. NOTES ON FAILED REQUESTS:
  1026. If there is an HTTP error while fetching an RSS object, the cached
  1027. version will be return, if it exists (and if MAGPIE_CACHE_FRESH_ONLY is off)
  1028. \*=======================================================================*/
  1029. define('MAGPIE_VERSION', '0.61');
  1030. $MAGPIE_ERROR = "";
  1031. function fetch_rss ($url, $cache_age = '') {
  1032. // initialize constants
  1033. init();
  1034. if ( ! isset($url) ) {
  1035. error("fetch_rss called without a url");
  1036. return false;
  1037. }
  1038. // if cache is disabled
  1039. if ( !MAGPIE_CACHE_ON ) {
  1040. // fetch file, and parse it
  1041. $resp = _fetch_remote_file( $url );
  1042. if ( is_success( $resp->status ) ) {
  1043. return _response_to_rss( $resp );
  1044. }
  1045. else {
  1046. error("Failed to fetch $url and cache is off");
  1047. return false;
  1048. }
  1049. }
  1050. // else cache is ON
  1051. else {
  1052. // Flow
  1053. // 1. check cache
  1054. // 2. if there is a hit, make sure its fresh
  1055. // 3. if cached obj fails freshness check, fetch remote
  1056. // 4. if remote fails, return stale object, or error
  1057. $cache = new RSSCache( MAGPIE_CACHE_DIR, ($cache_age != '') ? $cache_age : MAGPIE_CACHE_AGE );
  1058. if (MAGPIE_DEBUG and $cache->ERROR) {
  1059. debug($cache->ERROR, E_USER_WARNING);
  1060. }
  1061. $cache_status = 0; // response of check_cache
  1062. $request_headers = array(); // HTTP headers to send with fetch
  1063. $rss = 0; // parsed RSS object
  1064. $errormsg = 0; // errors, if any
  1065. if ( ! $cache->ERROR) {
  1066. // return cache HIT, MISS, or STALE
  1067. $cache_status = $cache->check_cache( $url );
  1068. }
  1069. // if object cached, and cache is fresh, return cached obj
  1070. if ( $cache_status == 'HIT' ) {
  1071. $rss = $cache->get( $url );
  1072. if ( isset($rss) and $rss ) {
  1073. $rss->from_cache = 1;
  1074. if ( MAGPIE_DEBUG > 1) {
  1075. debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
  1076. }
  1077. return $rss;
  1078. }
  1079. }
  1080. // else attempt a conditional get
  1081. // setup headers
  1082. if ( $cache_status == 'STALE' ) {
  1083. $rss = $cache->get( $url );
  1084. if (isset($rss->etag) && isset($rss->last_modified)) {
  1085. $request_headers['If-None-Match'] = $rss->etag;
  1086. $request_headers['If-Last-Modified'] = $rss->last_modified;
  1087. }
  1088. }
  1089. $resp = _fetch_remote_file( $url, $request_headers );
  1090. if (isset($resp) and $resp) {
  1091. if ($resp->status == '304' ) {
  1092. // we have the most current copy
  1093. if ( MAGPIE_DEBUG > 1) {
  1094. debug("Got 304 for $url");
  1095. }
  1096. // reset cache on 304 (at minutillo insistent prodding)
  1097. $cache->set($url, $rss);
  1098. return $rss;
  1099. }
  1100. elseif ( is_success( $resp->status ) ) {
  1101. $rss = _response_to_rss( $resp );
  1102. if ( $rss ) {
  1103. if (MAGPIE_DEBUG > 1) {
  1104. debug("Fetch successful");
  1105. }
  1106. // add object to cache
  1107. $cache->set( $url, $rss );
  1108. return $rss;
  1109. }
  1110. }
  1111. else {
  1112. $errormsg = "Failed to fetch $url. ";
  1113. if ( $resp->error ) {
  1114. # compensate for Snoopy's annoying habbit to tacking
  1115. # on '\n'
  1116. $http_error = substr($resp->error, 0, -2);
  1117. $errormsg .= "(HTTP Error: $http_error)";
  1118. }
  1119. else {
  1120. $errormsg .= "(HTTP Response: " . $resp->response_code .')';
  1121. }
  1122. }
  1123. }
  1124. else {
  1125. $errormsg = "Unable to retrieve RSS file for unknown reasons.";
  1126. }
  1127. // else fetch failed
  1128. // attempt to return cached object
  1129. if ($rss) {
  1130. if ( MAGPIE_DEBUG ) {
  1131. //debug("Returning STALE object for $url");
  1132. }
  1133. return $rss;
  1134. }
  1135. // else we totally failed
  1136. error( $errormsg );
  1137. return false;
  1138. } // end if ( !MAGPIE_CACHE_ON ) {
  1139. } // end fetch_rss()
  1140. /*=======================================================================*\
  1141. Function: error
  1142. Purpose: set MAGPIE_ERROR, and trigger error
  1143. \*=======================================================================*/
  1144. function error ($errormsg, $lvl=E_USER_WARNING) {
  1145. global $MAGPIE_ERROR;
  1146. // append PHP's error message if track_errors enabled
  1147. if ( isset($php_errormsg) ) {
  1148. $errormsg .= " ($php_errormsg)";
  1149. }
  1150. if ( $errormsg ) {
  1151. $errormsg = "MagpieRSS: $errormsg";
  1152. $MAGPIE_ERROR = $errormsg;
  1153. if (MAGPIE_DEBUG)
  1154. {
  1155. trigger_error($errormsg, $lvl);
  1156. }
  1157. else
  1158. {
  1159. error_log($errormsg, 0);
  1160. }
  1161. }
  1162. }
  1163. function debug ($debugmsg, $lvl=E_USER_NOTICE) {
  1164. trigger_error("MagpieRSS [debug] $debugmsg", $lvl);
  1165. }
  1166. /*=======================================================================*\
  1167. Function: magpie_error
  1168. Purpose: accessor for the magpie error variable
  1169. \*=======================================================================*/
  1170. function magpie_error ($errormsg="") {
  1171. global $MAGPIE_ERROR;
  1172. if ( isset($errormsg) and $errormsg ) {
  1173. $MAGPIE_ERROR = $errormsg;
  1174. }
  1175. return $MAGPIE_ERROR;
  1176. }
  1177. /*=======================================================================*\
  1178. Function: _fetch_remote_file
  1179. Purpose: retrieve an arbitrary remote file
  1180. Input: url of the remote file
  1181. headers to send along with the request (optional)
  1182. Output: an HTTP response object (see Snoopy.class.inc)
  1183. \*=======================================================================*/
  1184. function _fetch_remote_file ($url, $headers = "" ) {
  1185. // Snoopy is an HTTP client in PHP
  1186. $client = new M_Snoopy();
  1187. $client->agent = MAGPIE_USER_AGENT;
  1188. $client->read_timeout = MAGPIE_FETCH_TIME_OUT;
  1189. $client->use_gzip = MAGPIE_USE_GZIP;
  1190. if (is_array($headers) ) {
  1191. $client->rawheaders = $headers;
  1192. }
  1193. @$client->fetch($url);
  1194. return $client;
  1195. }
  1196. /*=======================================================================*\
  1197. Function: _response_to_rss
  1198. Purpose: parse an HTTP response object into an RSS object
  1199. Input: an HTTP response object (see Snoopy)
  1200. Output: parsed RSS object (see rss_parse)
  1201. \*=======================================================================*/
  1202. function _response_to_rss ($resp) {
  1203. $rss = new MagpieRSS( $resp->results );
  1204. // if RSS parsed successfully
  1205. if ( $rss and ! $rss->ERROR) {
  1206. // find Etag, and Last-Modified
  1207. foreach($resp->headers as $h) {
  1208. // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
  1209. if (strpos($h, ": ")) {
  1210. list($field, $val) = explode(": ", $h, 2);
  1211. }
  1212. else {
  1213. $field = $h;
  1214. $val = "";
  1215. }
  1216. if ( $field == 'ETag' ) {
  1217. $rss->etag = $val;
  1218. }
  1219. if ( $field == 'Last-Modified' ) {
  1220. $rss->last_modified = $val;
  1221. }
  1222. }
  1223. return $rss;
  1224. } // else construct error message
  1225. else {
  1226. $errormsg = "Failed to parse RSS file.";
  1227. if ($rss) {
  1228. $errormsg .= " (" . $rss->ERROR . ")";
  1229. }
  1230. error($errormsg);
  1231. return false;
  1232. } // end if ($rss and ! $rss->error)
  1233. }
  1234. /*=======================================================================*\
  1235. Function: init
  1236. Purpose: setup constants with default values
  1237. check for user overrides
  1238. \*=======================================================================*/
  1239. function init () {
  1240. if ( defined('MAGPIE_INITALIZED') ) {
  1241. return;
  1242. }
  1243. else {
  1244. define('MAGPIE_INITALIZED', 1);
  1245. }
  1246. if ( ! defined('MAGPIE_CACHE_ON') ) {
  1247. define('MAGPIE_CACHE_ON', 1);
  1248. }
  1249. if ( ! defined('MAGPIE_CACHE_DIR') ) {
  1250. define('MAGPIE_CACHE_DIR', './cache');
  1251. }
  1252. if ( ! defined('MAGPIE_CACHE_AGE') ) {
  1253. define('MAGPIE_CACHE_AGE', 60*60); // one hour
  1254. }
  1255. if ( ! defined('MAGPIE_CACHE_FRESH_ONLY') ) {
  1256. define('MAGPIE_CACHE_FRESH_ONLY', 0);
  1257. }
  1258. if ( ! defined('MAGPIE_DEBUG') ) {
  1259. define('MAGPIE_DEBUG', 0);
  1260. }
  1261. if ( ! defined('MAGPIE_USER_AGENT') ) {
  1262. $ua = 'MagpieRSS/'. MAGPIE_VERSION . ' (+http://magpierss.sf.net';
  1263. if ( MAGPIE_CACHE_ON ) {
  1264. $ua = $ua . ')';
  1265. }
  1266. else {
  1267. $ua = $ua . '; No cache)';
  1268. }
  1269. define('MAGPIE_USER_AGENT', $ua);
  1270. }
  1271. if ( ! defined('MAGPIE_FETCH_TIME_OUT') ) {
  1272. define('MAGPIE_FETCH_TIME_OUT', 5); // 5 second timeout
  1273. }
  1274. // use gzip encoding to fetch rss files if supported?
  1275. if ( ! defined('MAGPIE_USE_GZIP') ) {
  1276. define('MAGPIE_USE_GZIP', true);
  1277. }
  1278. }
  1279. // NOTE: the following code should really be in Snoopy, or at least
  1280. // somewhere other then rss_fetch!
  1281. /*=======================================================================*\
  1282. HTTP STATUS CODE PREDICATES
  1283. These functions attempt to classify an HTTP status code
  1284. based on RFC 2616 and RFC 2518.
  1285. All of them take an HTTP status code as input, and return true or false
  1286. All this code is adapted from LWP's HTTP::Status.
  1287. \*=======================================================================*/
  1288. /*=======================================================================*\
  1289. Function: is_info
  1290. Purpose: return true if Informational status code
  1291. \*=======================================================================*/
  1292. function is_info ($sc) {
  1293. return $sc >= 100 && $sc < 200;
  1294. }
  1295. /*=======================================================================*\
  1296. Function: is_success
  1297. Purpose: return true if Successful status code
  1298. \*=======================================================================*/
  1299. function is_success ($sc) {
  1300. return $sc >= 200 && $sc < 300;
  1301. }
  1302. /*=======================================================================*\
  1303. Function: is_redirect
  1304. Purpose: return true if Redirection status code
  1305. \*=======================================================================*/
  1306. function is_redirect ($sc) {
  1307. return $sc >= 300 && $sc < 400;
  1308. }
  1309. /*=======================================================================*\
  1310. Function: is_error
  1311. Purpose: return true if Error status code
  1312. \*=======================================================================*/
  1313. function is_error ($sc) {
  1314. return $sc >= 400 && $sc < 600;
  1315. }
  1316. /*=======================================================================*\
  1317. Function: is_client_error
  1318. Purpose: return true if Error status code, and its a client error
  1319. \*=======================================================================*/
  1320. function is_client_error ($sc) {
  1321. return $sc >= 400 && $sc < 500;
  1322. }
  1323. /*=======================================================================*\
  1324. Function: is_client_error
  1325. Purpose: return true if Error status code, and its a server error
  1326. \*=======================================================================*/
  1327. function is_server_error ($sc) {
  1328. return $sc >= 500 && $sc < 600;
  1329. }
  1330. /*
  1331. * Project: MagpieRSS: a simple RSS integration tool
  1332. * File: rss_cache.inc, a simple, rolling(no GC), cache
  1333. * for RSS objects, keyed on URL.
  1334. * Author: Kellan Elliott-McCrea <kellan@protest.net>
  1335. * Version: 0.51
  1336. * License: GPL
  1337. *
  1338. * The lastest version of MagpieRSS can be obtained from:
  1339. * http://magpierss.sourceforge.net
  1340. *
  1341. * For questions, help, comments, discussion, etc., please join the
  1342. * Magpie mailing list:
  1343. * http://lists.sourceforge.net/lists/listinfo/magpierss-general
  1344. *
  1345. */
  1346. class RSSCache {
  1347. var $BASE_CACHE = './cache'; // where the cache files are stored
  1348. var $MAX_AGE = 3600; // when are files stale, default one hour
  1349. var $ERROR = ""; // accumulate error messages
  1350. function RSSCache ($base='', $age='') {
  1351. // Make a local reference of the ExpressionEngine super object
  1352. $this->EE =& get_instance();
  1353. if ( $base ) {
  1354. $this->BASE_CACHE = $base;
  1355. }
  1356. if ( $age ) {
  1357. $this->MAX_AGE = $age;
  1358. }
  1359. // attempt to make the cache directory
  1360. if ( ! file_exists( $this->BASE_CACHE ) ) {
  1361. $status = @mkdir( $this->BASE_CACHE, DIR_READ_MODE );
  1362. @chmod($this->BASE_CACHE, DIR_WRITE_MODE);
  1363. // if make failed
  1364. if ( ! $status ) {
  1365. $this->error(
  1366. "Cache couldn't make dir '" . $this->BASE_CACHE . "'."
  1367. );
  1368. }
  1369. }
  1370. else
  1371. {
  1372. // EE - Make sure cache is 777
  1373. @chmod($this->BASE_CACHE, DIR_WRITE_MODE);
  1374. }
  1375. }
  1376. /*=======================================================================*\
  1377. Function: set
  1378. Purpose: add an item to the cache, keyed on url
  1379. Input: url from wich the rss file was fetched
  1380. Output: true on sucess
  1381. \*=======================================================================*/
  1382. function set ($url, $rss) {
  1383. $this->ERROR = "";
  1384. $cache_file = $this->file_name( $url );
  1385. $fp = @fopen( $cache_file, 'w' );
  1386. if ( ! $fp ) {
  1387. $this->error(
  1388. "Cache unable to open file for writing: $cache_file"
  1389. );
  1390. return 0;
  1391. }
  1392. $data = serialize( $rss );
  1393. fwrite( $fp, $data );
  1394. fclose( $fp );
  1395. @chmod($cache_file, FILE_WRITE_MODE);
  1396. return $cache_file;
  1397. }
  1398. /*=======================================================================*\
  1399. Function: get
  1400. Purpose: fetch an item from the cache
  1401. Input: url from wich the rss file was fetched
  1402. Output: cached object on HIT, false on MISS
  1403. \*=======================================================================*/
  1404. function get ($url) {
  1405. $this->ERROR = "";
  1406. $cache_file = $this->file_name( $url );
  1407. if ( ! file_exists( $cache_file ) ) {
  1408. $this->debug(
  1409. "Cache doesn't contain: $url (cache file: $cache_file)"
  1410. );
  1411. return 0;
  1412. }
  1413. $fp = @fopen($cache_file, 'r');
  1414. if ( ! $fp ) {
  1415. $this->error(
  1416. "Failed to open cache file for reading: $cache_file"
  1417. );
  1418. return 0;
  1419. }
  1420. if (($file_size = filesize($cache_file)) == 0)
  1421. {
  1422. return 0;
  1423. }
  1424. $data = fread( $fp, $file_size );
  1425. $rss = unserialize( $data );
  1426. @chmod($cache_file, FILE_WRITE_MODE);
  1427. return $rss;
  1428. }
  1429. /*=======================================================================*\
  1430. Function: check_cache
  1431. Purpose: check a url for membership in the cache
  1432. and whether the object is older then MAX_AGE (ie. STALE)
  1433. Input: url from wich the rss file was fetched
  1434. Output: cached object on HIT, false on MISS
  1435. \*=======================================================================*/
  1436. function check_cache ( $url ) {
  1437. $this->ERROR = "";
  1438. $filename = $this->file_name( $url );
  1439. if ( file_exists( $filename ) ) {
  1440. // find how long ago the file was added to the cache
  1441. // and whether that is longer then MAX_AGE
  1442. $mtime = filemtime( $filename );
  1443. $age = time() - $mtime;
  1444. if ( $this->MAX_AGE > $age ) {
  1445. // object exists and is current
  1446. return 'HIT';
  1447. }
  1448. else {
  1449. // object exists but is old
  1450. return 'STALE';
  1451. }
  1452. }
  1453. else {
  1454. // object does not exist
  1455. return 'MISS';
  1456. }
  1457. }
  1458. /*=======================================================================*\
  1459. Function: file_name
  1460. Purpose: map url to location in cache
  1461. Input: url from wich the rss file was fetched
  1462. Output: a file name
  1463. \*=======================================================================*/
  1464. function file_name ($url)
  1465. {
  1466. $filename = md5( $url );
  1467. return $this->EE->functions->remove_double_slashes(join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename)));
  1468. }
  1469. /*=======================================================================*\
  1470. Function: error
  1471. Purpose: register error
  1472. \*=======================================================================*/
  1473. function error ($errormsg, $lvl=E_USER_WARNING) {
  1474. // append PHP's error message if track_errors enabled
  1475. if ( isset($php_errormsg) ) {
  1476. $errormsg .= " ($php_errormsg)";
  1477. }
  1478. $this->ERROR = $errormsg;
  1479. if ( MAGPIE_DEBUG ) {
  1480. trigger_error( $errormsg, $lvl);
  1481. }
  1482. else {
  1483. error_log( $errormsg, 0);
  1484. }
  1485. }
  1486. function debug ($debugmsg, $lvl=E_USER_NOTICE) {
  1487. if ( MAGPIE_DEBUG ) {
  1488. $this->error("MagpieRSS [debug] $debugmsg", $lvl);
  1489. }
  1490. }
  1491. }
  1492. /*************************************************
  1493. Snoopy - the PHP net client
  1494. Author: Monte Ohrt <monte@ispi.net>
  1495. Copyright (c): 1999-2008 New Digital Group, all rights reserved
  1496. Ve…

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