/modules/engine/lastRSS.php

https://github.com/foxadmin/ReloadCMS · PHP · 191 lines · 126 code · 13 blank · 52 comment · 44 complexity · 624906a8ad18813c30766f3d4078a17a MD5 · raw file

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