Source for file lastRSS.php

Documentation is available at lastRSS.php

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

Documentation generated on Fri, 08 Jun 2007 12:21:25 +0300 by phpDocumentor 1.3.0RC3