PageRenderTime 29ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/reference/3.1/tutorials/RssReader/php/lastRSS.php

https://github.com/JxLib/jxlib.github.com
PHP | 226 lines | 132 code | 12 blank | 82 comment | 44 complexity | 0907eea8795b0183387d4ec2a28a4578 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);
  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 != '') {
  154. $k = 'textinput_'.$textinputtag;
  155. $result->$k = $temp; // Set only if not empty
  156. }
  157. }
  158. }
  159. // Parse IMAGE info
  160. preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
  161. if (isset($out_imageinfo[1])) {
  162. foreach($this->imagetags as $imagetag) {
  163. $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
  164. if ($temp != '') {
  165. $k = 'image_'.$imagetag;
  166. $result->$k = $temp; // Set only if not empty
  167. }
  168. }
  169. }
  170. // Parse ITEMS
  171. preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
  172. $rss_items = $items[2];
  173. $i = 0;
  174. $result->items = array(); // create array even if there are no items
  175. foreach($rss_items as $rss_item) {
  176. // If number of items is lower then limit: Parse one item
  177. if ($i < $this->items_limit || $this->items_limit == 0) {
  178. foreach($this->itemtags as $itemtag) {
  179. $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
  180. if ($temp != '') $result->items[$i]->$itemtag = $temp; // Set only if not empty
  181. }
  182. // Strip HTML tags and other bullshit from DESCRIPTION
  183. if ($this->stripHTML && $result->items[$i]->description)
  184. $result->items[$i]->description = strip_tags($this->unhtmlentities(strip_tags($result->items[$i]->description)));
  185. // Strip HTML tags and other bullshit from TITLE
  186. if ($this->stripHTML && $result->items[$i]->title)
  187. $result->items[$i]->title = strip_tags($this->unhtmlentities(strip_tags($result->items[$i]->title)));
  188. // If date_format is specified and pubDate is valid
  189. if ($this->date_format != '' && ($timestamp = strtotime($result->items[$i]->pubDate)) !==-1) {
  190. // convert pubDate to specified date format
  191. $result->items[$i]->pubDate = date($this->date_format, $timestamp);
  192. }
  193. // Item counter
  194. $i++;
  195. }
  196. }
  197. $result->items_count = $i;
  198. return $result;
  199. }
  200. else // Error in opening return False
  201. {
  202. return False;
  203. }
  204. }
  205. }
  206. ?>