PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Portal_XL50_Premod-3.0.8/phpBB3/portal/block/lastRSS.php

https://github.com/Lucky65/phpBB-Portal-XL-5.0-italian
PHP | 220 lines | 126 code | 12 blank | 82 comment | 44 complexity | 1f5e0791806c8d1251b8aef33da553b6 MD5 | raw file
  1. <?php
  2. /*
  3. ======================================================================
  4. lastRSS 0.9.1
  5. Simple yet powerfull PHP class to parse RSS files.
  6. by Vojtech Semecky, webmaster @ webdot . cz
  7. Latest version, features, manual and examples:
  8. http://lastrss.webdot.cz/
  9. ----------------------------------------------------------------------
  10. LICENSE
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License (GPL)
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  20. ======================================================================
  21. */
  22. /**
  23. * lastRSS
  24. * Simple yet powerfull PHP class to parse RSS files.
  25. */
  26. class lastRSS {
  27. // -------------------------------------------------------------------
  28. // Public properties
  29. // -------------------------------------------------------------------
  30. var $default_cp = 'UTF-8';
  31. var $CDATA = 'nochange';
  32. var $cp = '';
  33. var $items_limit = 0;
  34. var $stripHTML = False;
  35. var $date_format = '';
  36. // -------------------------------------------------------------------
  37. // Private variables
  38. // -------------------------------------------------------------------
  39. var $channeltags = array ('title', 'link', 'description', 'language', 'copyright', 'managingEditor', 'webMaster', 'lastBuildDate', 'rating', 'docs');
  40. var $itemtags = array('title', 'link', 'description', 'author', 'category', 'comments', 'enclosure', 'guid', 'pubDate', 'source');
  41. var $imagetags = array('title', 'url', 'link', 'width', 'height');
  42. var $textinputtags = array('title', 'description', 'name', 'link');
  43. // -------------------------------------------------------------------
  44. // Parse RSS file and returns associative array.
  45. // -------------------------------------------------------------------
  46. function Get ($rss_url) {
  47. // If CACHE ENABLED
  48. if ($this->cache_dir != '') {
  49. $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url) . '.dat';
  50. $timedif = @(time() - filemtime($cache_file));
  51. if ($timedif < $this->cache_time) {
  52. // cached file is fresh enough, return cached array
  53. $result = unserialize(join('', file($cache_file)));
  54. // set 'cached' to 1 only if cached file is correct
  55. if ($result) $result['cached'] = 1;
  56. } else {
  57. // cached file is too old, create new
  58. $result = $this->Parse($rss_url);
  59. $serialized = serialize($result);
  60. if ($f = @fopen($cache_file, 'w')) {
  61. fwrite ($f, $serialized, strlen($serialized));
  62. fclose($f);
  63. }
  64. if ($result) $result['cached'] = 0;
  65. }
  66. }
  67. // If CACHE DISABLED >> load and parse the file directly
  68. else {
  69. $result = $this->Parse($rss_url);
  70. if ($result) $result['cached'] = 0;
  71. }
  72. // return result
  73. return $result;
  74. }
  75. // -------------------------------------------------------------------
  76. // Modification of preg_match(); return trimed field with index 1
  77. // from 'classic' preg_match() array output
  78. // -------------------------------------------------------------------
  79. function my_preg_match ($pattern, $subject) {
  80. // start regullar expression
  81. preg_match($pattern, $subject, $out);
  82. // if there is some result... process it and return it
  83. if(isset($out[1])) {
  84. // Process CDATA (if present)
  85. if ($this->CDATA == 'content') { // Get CDATA content (without CDATA tag)
  86. $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
  87. } elseif ($this->CDATA == 'strip') { // Strip CDATA
  88. $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
  89. }
  90. // If code page is set convert character encoding to required
  91. if ($this->cp != '')
  92. //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]);
  93. $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]);
  94. // Return result
  95. return trim($out[1]);
  96. } else {
  97. // if there is NO result, return empty string
  98. return '';
  99. }
  100. }
  101. // -------------------------------------------------------------------
  102. // Replace HTML entities &something; by real characters
  103. // -------------------------------------------------------------------
  104. function unhtmlentities ($string) {
  105. // Get HTML entities table
  106. $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES);
  107. // Flip keys<==>values
  108. $trans_tbl = array_flip ($trans_tbl);
  109. // Add support for &apos; entity (missing in HTML_ENTITIES)
  110. $trans_tbl += array('&apos;' => "'");
  111. // Replace entities by values
  112. return strtr ($string, $trans_tbl);
  113. }
  114. // -------------------------------------------------------------------
  115. // Parse() is private method used by Get() to load and parse RSS file.
  116. // Don't use Parse() in your scripts - use Get($rss_file) instead.
  117. // -------------------------------------------------------------------
  118. function Parse ($rss_url) {
  119. // Open and load RSS file
  120. if ($f = @fopen($rss_url, 'r')) {
  121. $rss_content = '';
  122. while (!feof($f)) {
  123. $rss_content .= fgets($f, 4096);
  124. }
  125. fclose($f);
  126. // Parse document encoding
  127. $result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content);
  128. // if document codepage is specified, use it
  129. if ($result['encoding'] != '')
  130. { $this->rsscp = $result['encoding']; } // This is used in my_preg_match()
  131. // otherwise use the default codepage
  132. else
  133. { $this->rsscp = $this->default_cp; } // This is used in my_preg_match()
  134. // Parse CHANNEL info
  135. preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel);
  136. foreach($this->channeltags as $channeltag)
  137. {
  138. $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si", $out_channel[1]);
  139. if ($temp != '') $result[$channeltag] = $temp; // Set only if not empty
  140. }
  141. // If date_format is specified and lastBuildDate is valid
  142. if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
  143. // convert lastBuildDate to specified date format
  144. $result['lastBuildDate'] = date($this->date_format, $timestamp);
  145. }
  146. // Parse TEXTINPUT info
  147. preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo);
  148. // This a little strange regexp means:
  149. // Look for tag <textinput> with or without any attributes, but skip truncated version <textinput /> (it's not beggining tag)
  150. if (isset($out_textinfo[2])) {
  151. foreach($this->textinputtags as $textinputtag) {
  152. $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]);
  153. if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty
  154. }
  155. }
  156. // Parse IMAGE info
  157. preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
  158. if (isset($out_imageinfo[1])) {
  159. foreach($this->imagetags as $imagetag) {
  160. $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
  161. if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty
  162. }
  163. }
  164. // Parse ITEMS
  165. preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
  166. $rss_items = $items[2];
  167. $i = 0;
  168. $result['items'] = array(); // create array even if there are no items
  169. foreach($rss_items as $rss_item) {
  170. // If number of items is lower then limit: Parse one item
  171. if ($i < $this->items_limit || $this->items_limit == 0) {
  172. foreach($this->itemtags as $itemtag) {
  173. $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
  174. if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty
  175. }
  176. // Strip HTML tags and other bullshit from DESCRIPTION
  177. if ($this->stripHTML && $result['items'][$i]['description'])
  178. $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
  179. // Strip HTML tags and other bullshit from TITLE
  180. if ($this->stripHTML && $result['items'][$i]['title'])
  181. $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
  182. // If date_format is specified and pubDate is valid
  183. if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) {
  184. // convert pubDate to specified date format
  185. $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp);
  186. }
  187. // Item counter
  188. $i++;
  189. }
  190. }
  191. $result['items_count'] = $i;
  192. return $result;
  193. }
  194. else // Error in opening return False
  195. {
  196. return False;
  197. }
  198. }
  199. }
  200. ?>