PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Rssparser.php

https://bitbucket.org/gaqs/telecentros
PHP | 215 lines | 159 code | 10 blank | 46 comment | 15 complexity | a03f428ceb5f7decc5c391fe6010eda4 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. * This class is written based entirely on the work found below
  4. * www.techbytes.co.in/blogs/2006/01/15/consuming-rss-with-php-the-simple-way/
  5. * All credit should be given to the original author
  6. *
  7. * Example:
  8. $this->load->library('rssparser');
  9. $this->rssparser->set_feed_url('http://example.com/feed');
  10. $this->rssparser->set_cache_life(30);
  11. $rss = $this->rssparser->getFeed(6); // Get six items from the feed
  12. // Using a callback function to parse addictional XML fields
  13. $this->load->library('rssparser', array($this, 'parseFile')); // parseFile method of current class
  14. function parseFile($data, $item)
  15. {
  16. $data['summary'] = (string)$item->summary;
  17. return $data;
  18. }
  19. */
  20. class RSSParser {
  21. public $feed_uri = NULL; // Feed URI
  22. public $data = FALSE; // Associative array containing all the feed items
  23. public $channel_data = array(); // Store RSS Channel Data in an array
  24. public $feed_unavailable = NULL; // Boolean variable which indicates whether an RSS feed was unavailable
  25. public $cache_life = 0; // Cache lifetime
  26. public $cache_dir = './application/cache/'; // Cache directory
  27. public $write_cache_flag = FALSE; // Flag to write to cache
  28. public $callback = FALSE; // Callback to read custom data
  29. function __construct($callback = FALSE)
  30. {
  31. if ($callback)
  32. {
  33. $this->callback = $callback;
  34. }
  35. }
  36. // --------------------------------------------------------------------
  37. function parse()
  38. {
  39. // Are we caching?
  40. if ($this->cache_life != 0)
  41. {
  42. $filename = $this->cache_dir.'rss_Parse_'.md5($this->feed_uri);
  43. // Is there a cache file ?
  44. if (!file_exists($filename))
  45. {
  46. // Has it expired?
  47. $timedif = (time() - filemtime($filename));
  48. if ($timedif < ( $this->cache_life * 60))
  49. {
  50. $rawFeed = file_get_contents($filename);
  51. }
  52. else
  53. {
  54. // So raise the falg
  55. $this->write_cache_flag = true;
  56. }
  57. }
  58. else
  59. {
  60. // Raise the flag to write the cache
  61. $this->write_cache_flag = true;
  62. }
  63. }
  64. // Reset
  65. $this->data = array();
  66. $this->channel_data = array();
  67. // Parse the document
  68. if (!isset($rawFeed))
  69. {
  70. $rawFeed = file_get_contents($this->feed_uri);
  71. }
  72. $xml = new SimpleXmlElement($rawFeed);
  73. if ($xml->channel)
  74. {
  75. // Assign the channel data
  76. $this->channel_data['title'] = $xml->channel->title;
  77. $this->channel_data['description'] = $xml->channel->description;
  78. // Build the item array
  79. foreach ($xml->channel->item as $item)
  80. {
  81. $data = array();
  82. $data['title'] = (string)$item->title;
  83. $data['category'] = (string)$item->category;
  84. $data['description'] = (string)$item->description;
  85. $data['pubDate'] = (string)$item->pubDate;
  86. $data['link'] = (string)$item->link;
  87. $dc = $item->children('http://purl.org/dc/elements/1.1/');
  88. $data['author'] = (string)$dc->creator;
  89. if ($this->callback)
  90. {
  91. $data = call_user_func($this->callback, $data, $item);
  92. }
  93. $this->data[] = $data;
  94. }
  95. }
  96. else
  97. {
  98. // Assign the channel data
  99. $this->channel_data['title'] = $xml->title;
  100. $this->channel_data['description'] = $xml->subtitle;
  101. // Build the item array
  102. foreach ($xml->entry as $item)
  103. {
  104. $data = array();
  105. $data['id'] = (string)$item->id;
  106. $data['title'] = (string)$item->title;
  107. $data['category'] = (string)$item->category;
  108. $data['description'] = (string)$item->content;
  109. $data['pubDate'] = (string)$item->published;
  110. $data['link'] = (string)$item->link['href'];
  111. $dc = $item->children('http://purl.org/dc/elements/1.1/');
  112. $data['author'] = (string)$dc->creator;
  113. if ($this->callback)
  114. {
  115. $data = call_user_func($this->callback, $data, $item);
  116. }
  117. $this->data[] = $data;
  118. }
  119. }
  120. // Do we need to write the cache file?
  121. if ($this->write_cache_flag)
  122. {
  123. if (!$fp = @fopen($filename, 'wb'))
  124. {
  125. echo "RSSParser error";
  126. log_message('error', "Unable to write cache file: ".$filename);
  127. return;
  128. }
  129. flock($fp, LOCK_EX);
  130. fwrite($fp, $rawFeed);
  131. flock($fp, LOCK_UN);
  132. fclose($fp);
  133. }
  134. return TRUE;
  135. }
  136. // --------------------------------------------------------------------
  137. function set_cache_life($period = NULL)
  138. {
  139. $this->cache_life = $period;
  140. return $this;
  141. }
  142. // --------------------------------------------------------------------
  143. function set_feed_url($url = NULL)
  144. {
  145. $this->feed_uri = $url;
  146. return $this;
  147. }
  148. // --------------------------------------------------------------------
  149. /* Return the feeds one at a time: when there are no more feeds return false
  150. * @param No of items to return from the feed
  151. * @return Associative array of items
  152. */
  153. function getFeed($num)
  154. {
  155. $this->parse();
  156. $c = 0;
  157. $return = array();
  158. foreach ($this->data AS $item)
  159. {
  160. $return[] = $item;
  161. $c++;
  162. if ($c == $num)
  163. {
  164. break;
  165. }
  166. }
  167. return $return;
  168. }
  169. // --------------------------------------------------------------------
  170. /* Return channel data for the feed */
  171. function & getChannelData()
  172. {
  173. $flag = false;
  174. if (!empty($this->channel_data))
  175. {
  176. return $this->channel_data;
  177. }
  178. else
  179. {
  180. return $flag;
  181. }
  182. }
  183. // --------------------------------------------------------------------
  184. /* Were we unable to retreive the feeds ? */
  185. function errorInResponse()
  186. {
  187. return $this->feed_unavailable;
  188. }
  189. // --------------------------------------------------------------------
  190. /* Initialize the feed data */
  191. function clear()
  192. {
  193. $this->feed_uri = NULL;
  194. $this->data = FALSE;
  195. $this->channel_data = array();
  196. $this->cache_life = 0;
  197. $this->callback = FALSE;
  198. return $this;
  199. }
  200. function getFeedByDate($date)
  201. {
  202. }
  203. }
  204. /* End of file RSSParser.php */
  205. /* Location: ./application/libraries/RSSParser.php */