PageRenderTime 27ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/app/includes/lastRSS.php

https://github.com/amanai/next24
PHP | 229 lines | 135 code | 12 blank | 82 comment | 50 complexity | 58a75ed19adcedc151940e76db9e2ce7 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. $result = array();
  49. if ($this->cache_dir != '') {
  50. $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url);
  51. $timedif = @(time() - filemtime($cache_file));
  52. if ($timedif < $this->cache_time) {
  53. // cached file is fresh enough, return cached array
  54. $result = unserialize(join('', file($cache_file)));
  55. // set 'cached' to 1 only if cached file is correct
  56. if ($result) $result['cached'] = 1;
  57. } else {
  58. // cached file is too old, create new
  59. $result = $this->Parse($rss_url);
  60. $serialized = serialize($result);
  61. if ($f = @fopen($cache_file, 'w')) {
  62. fwrite ($f, $serialized, strlen($serialized));
  63. fclose($f);
  64. }
  65. if ($result) $result['cached'] = 0;
  66. }
  67. }
  68. // If CACHE DISABLED >> load and parse the file directly
  69. else {
  70. $result = $this->Parse($rss_url);
  71. if ($result) $result['cached'] = 0;
  72. }
  73. // return result
  74. return $result;
  75. }
  76. // -------------------------------------------------------------------
  77. // Modification of preg_match(); return trimed field with index 1
  78. // from 'classic' preg_match() array output
  79. // -------------------------------------------------------------------
  80. function my_preg_match ($pattern, $subject) {
  81. // start regullar expression
  82. preg_match($pattern, $subject, $out);
  83. // if there is some result... process it and return it
  84. if(isset($out[1])) {
  85. // Process CDATA (if present)
  86. if ($this->CDATA == 'content') { // Get CDATA content (without CDATA tag)
  87. $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
  88. } elseif ($this->CDATA == 'strip') { // Strip CDATA
  89. $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
  90. }
  91. // If code page is set convert character encoding to required
  92. if ($this->cp != '')
  93. //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]);
  94. $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]);
  95. // Return result
  96. return trim($out[1]);
  97. } else {
  98. // if there is NO result, return empty string
  99. return '';
  100. }
  101. }
  102. // -------------------------------------------------------------------
  103. // Replace HTML entities &something; by real characters
  104. // -------------------------------------------------------------------
  105. function unhtmlentities ($string) {
  106. // Get HTML entities table
  107. $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES);
  108. // Flip keys<==>values
  109. $trans_tbl = array_flip ($trans_tbl);
  110. // Add support for &apos; entity (missing in HTML_ENTITIES)
  111. $trans_tbl += array('&apos;' => "'");
  112. // Replace entities by values
  113. return strtr ($string, $trans_tbl);
  114. }
  115. // -------------------------------------------------------------------
  116. // Parse() is private method used by Get() to load and parse RSS file.
  117. // Don't use Parse() in your scripts - use Get($rss_file) instead.
  118. // -------------------------------------------------------------------
  119. function Parse ($rss_url) {
  120. // Open and load RSS file
  121. if ($f = @fopen($rss_url, 'r')) {
  122. $rss_content = '';
  123. while (!feof($f)) {
  124. $rss_content .= fgets($f, 4096);
  125. }
  126. fclose($f);
  127. // Parse document encoding
  128. $result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content);
  129. // if document codepage is specified, use it
  130. if ($result['encoding'] != '')
  131. { $this->rsscp = $result['encoding']; } // This is used in my_preg_match()
  132. // otherwise use the default codepage
  133. else
  134. { $this->rsscp = $this->default_cp; } // This is used in my_preg_match()
  135. // Parse CHANNEL info
  136. preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel);
  137. foreach($this->channeltags as $channeltag)
  138. {
  139. $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si", $out_channel[1]);
  140. if ($temp != '') $result[$channeltag] = $temp; // Set only if not empty
  141. }
  142. // If date_format is specified and lastBuildDate is valid
  143. if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
  144. // convert lastBuildDate to specified date format
  145. $result['lastBuildDate'] = date($this->date_format, $timestamp);
  146. }
  147. // Parse TEXTINPUT info
  148. preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo);
  149. // This a little strange regexp means:
  150. // Look for tag <textinput> with or without any attributes, but skip truncated version <textinput /> (it's not beggining tag)
  151. if (isset($out_textinfo[2])) {
  152. foreach($this->textinputtags as $textinputtag) {
  153. $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]);
  154. if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty
  155. }
  156. }
  157. // Parse IMAGE info
  158. preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
  159. if (isset($out_imageinfo[1])) {
  160. foreach($this->imagetags as $imagetag) {
  161. $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
  162. if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty
  163. }
  164. }
  165. // Parse ITEMS
  166. preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
  167. $rss_items = $items[2];
  168. $i = 0;
  169. $result['items'] = array(); // create array even if there are no items
  170. foreach($rss_items as $rss_item) {
  171. // If number of items is lower then limit: Parse one item
  172. if ($i < $this->items_limit || $this->items_limit == 0) {
  173. foreach($this->itemtags as $itemtag) {
  174. $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
  175. $temp2= $this -> my_preg_match("'<!\[CDATA\[(.*?)\]\]>'si", $temp);
  176. if ($temp2) $temp = $temp2;
  177. if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty
  178. elseif ($itemtag == 'enclosure'){ // media files
  179. $temp = $this->my_preg_match("'<enclosure\s*url=\"(.*?)\"'si", $rss_item);
  180. if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty
  181. $temp = $this->my_preg_match("'<enclosure\s*url=\".*?\"\s*type=\"(.*?)\"'si", $rss_item);
  182. if ($temp != '') $result['items'][$i][$itemtag."_type"] = $temp; // Set only if not empty
  183. }
  184. }
  185. // Strip HTML tags and other bullshit from DESCRIPTION
  186. if ($this->stripHTML && $result['items'][$i]['description'])
  187. $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
  188. // Strip HTML tags and other bullshit from TITLE
  189. if ($this->stripHTML && $result['items'][$i]['title'])
  190. $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
  191. // If date_format is specified and pubDate is valid
  192. if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) {
  193. // convert pubDate to specified date format
  194. $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp);
  195. }
  196. // Item counter
  197. $i++;
  198. }
  199. }
  200. $result['items_count'] = $i;
  201. return $result;
  202. }
  203. else // Error in opening return False
  204. {
  205. return False;
  206. }
  207. }
  208. }
  209. ?>