PageRenderTime 38ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/sahana/inc/lib_gis/lib_georss.inc

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