A tumblelog CMS built on AJAX, PHP and MySQL.

feeds.class.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. if (!defined('entry') || !entry) {
  3. die('Not a valid page');
  4. }
  5. /* ===========================
  6. gelato CMS - A PHP based tumblelog CMS
  7. development version
  8. http://www.gelatocms.com/
  9. gelato CMS is a free software licensed under the GPL 2.0
  10. Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
  11. =========================== */
  12. ?>
  13. <?php
  14. require_once("configuration.class.php");
  15. require_once("simplepie.class.php");
  16. class feeds extends Conexion_Mysql
  17. {
  18. public $conf;
  19. public function __construct()
  20. {
  21. parent::__construct(DB_name, DB_Server, DB_User, DB_Password);
  22. $this->conf = new configuration();
  23. }
  24. public function addFeed($url, $type, $source, $credits)
  25. {
  26. $f = array();
  27. $f['updated_at'] = '0000-00-00 00:00:00';
  28. $f['error'] = 0;
  29. $f['title'] = '';
  30. $f['url'] = '';
  31. $f['credits'] = $credits;
  32. $f['site_url'] = '';
  33. $f['id_user'] = $_SESSION['user_id'];
  34. switch ($source) {
  35. case 'Rss': $f['url'] = $url; $f['type'] = $type; break;
  36. case 'VOX': $f['url'] = 'http://{{username}}.vox.com/library/posts/atom.xml'; $f['type'] = 1; break;
  37. case 'Digg': $f['url'] = 'http://digg.com/rss/{{username}}/index2.xml'; $f['type'] = 1; break;
  38. case 'Tumblr': $f['url'] = 'http://{{username}}.tumblr.com/rss'; $f['type'] = 1; break;
  39. case 'Twitter': $f['url'] = 'http://twitter.com/{{username}}'; $f['type'] = 1; break;
  40. case 'Last.fm': $f['url'] = 'http://ws.audioscrobbler.com/1.0/user/{{username}}/recenttracks.rss'; $f['type'] = 1; break;
  41. case 'Blogger': $f['url'] = 'http://{{username}}.blogspot.com/feeds/posts/default'; $f['type'] = 1; break;
  42. case 'Youtube': $f['url'] = 'http://www.youtube.com/rss/user/{{username}}/videos.rss'; $f['type'] = 1; break;
  43. case 'Wordpress.com': $f['url'] = 'http://{{username}}.wordpress.com/feed/'; $f['type'] = 1; break;
  44. case 'Del.icio.us': $f['url'] = 'http://feeds.delicious.com/rss/{{username}}'; $f['type'] = 1; break;
  45. default: $f['url'] = ''; break;
  46. }
  47. if (!empty($f['url'])) {
  48. $f['url'] = str_replace('{{username}}', $url, $f['url']);
  49. return ($this->insertarDeFormulario($this->conf->tablePrefix."feeds", $f));
  50. }
  51. print_r($_POST);
  52. print_r($f);
  53. }
  54. public function removeFeed($id)
  55. {
  56. return ($this->ejecutarConsulta("DELETE FROM ".$this->conf->tablePrefix."feeds WHERE id_feed=".$id));
  57. }
  58. public function updateFeeds()
  59. {
  60. $timeToUpdate = trim($this->conf->rssImportFrec). ' ago';
  61. $feeds = $this->getFeedList("WHERE updated_at < '".date("Y-m-d H:i:s", strtotime($timeToUpdate))."'");
  62. foreach ($feeds as $feed) {
  63. $data = new SimplePie();
  64. $data->feed_url($feed['url']);
  65. $data->cache_location(Absolute_Path."/uploads/CACHE");
  66. $data->init();
  67. $temp = array();
  68. $temp['updated_at'] = date("Y-m-d H:i:s");
  69. if (!empty($data->error)) {
  70. // Error report
  71. $temp['error'] = 1;
  72. $this->modificarDeFormulario($this->conf->tablePrefix."feeds", $temp, 'id_feed = '.$feed['id_feed']);
  73. } else {
  74. if ($data->data) {
  75. $timeFilter = strtotime($timeToUpdate);
  76. foreach ($data->get_items() as $post) {
  77. if (strtotime($post->get_date("Y-m-d H:i:s")) > strtotime($feed['updated_at'])) {
  78. $newPost = array();
  79. $newPost['id_user'] = $feed['id_user'];
  80. $newPost['title'] = $post->get_title();
  81. $newPost['date'] = $post->get_date("Y-m-d H:i:s");
  82. if ($feed['type'] == 1) { //TEXT
  83. $newPost['type'] = 1;
  84. if ($post->get_title() != $post->get_description()) {
  85. if (strpos($feed['url'], 'twitter.com') <= 0) {
  86. $newPost['description'] = $post->get_description();
  87. // Youtube Fix to add a link to the image
  88. if (strpos($feed['url'], 'youtube.com') > 0) {
  89. $newPost['description'] = preg_replace('/\<img ([^\>]+)/', '<a href="'.$post->get_link().'"><img $1></a', $post->get_description());
  90. // Delicious fix to add a link post
  91. } elseif (strpos($feed['url'], 'delicious.com') > 0) {
  92. $newPost['type'] = 4;
  93. $newPost['url'] = $post->get_link();
  94. }
  95. }
  96. }
  97. } elseif ($feed['type'] == 2) { //IMAGES
  98. $ma = array();
  99. $url_image = '';
  100. @preg_match_all('/\<img ([^\>]+)/', $post->get_description(), $ma);
  101. @preg_match_all('/src\=\"([^\"]+)\"/', $ma[0][0], $ma);
  102. $url_image = $ma[1][0];
  103. if (empty($url_image)) {
  104. /* Theres no image, lets make a text post */
  105. $newPost['type'] = 1;
  106. if ($post->get_title() != $post->get_description()) {
  107. if (strpos($feed['url'], 'twitter.com') <= 0) {
  108. $newPost['description'] = $post->get_description();
  109. }
  110. }
  111. } else {
  112. $newPost['type'] = 2;
  113. $newPost['url'] = $url_image;
  114. $newPost['description'] = $post->get_title();
  115. }
  116. }
  117. if ($feed['credits'] == 1 && !empty($feed['title'])) {
  118. $newPost['description'] .= '<p class="rss-credits">(via <a href="'.((empty($feed['site_url']))? $feed['url'] : $feed['site_url']).'" title="'.$feed['title'].'">'.$feed['title'].'</a>)</p>';
  119. }
  120. $this->insertarDeFormulario($this->conf->tablePrefix."data", $newPost);
  121. }
  122. }
  123. $temp['title'] = (!empty($data->data['info']['title']))? $data->data['info']['title'] : '';
  124. $temp['site_url'] = (!empty($data->data['info']['link']['alternate'][0]))? $data->data['info']['link']['alternate'][0] : $data->data['info']['link'];
  125. $temp['error'] = 0;
  126. $this->modificarDeFormulario($this->conf->tablePrefix."feeds", $temp, 'id_feed = '.$feed['id_feed']);
  127. }
  128. }
  129. }
  130. }
  131. public function getFeedList($condition = '')
  132. {
  133. $feeds = array();
  134. $this->ejecutarConsulta('SELECT * FROM '.$this->conf->tablePrefix.'feeds '.$condition.' ORDER BY id_feed DESC');
  135. while ($feed = $this->mid_consulta->fetch(PDO::FETCH_ASSOC)) {
  136. $feeds[] = $feed;
  137. }
  138. return $feeds;
  139. }
  140. /**
  141. * Calculate the seconds until next update
  142. * @param $feed can be an ID, or the raw array from the DB
  143. * @return int
  144. */
  145. public function getNextUpdate($feed)
  146. {
  147. if (is_numeric($feed)) {
  148. $id = (int)$feed;
  149. } elseif (is_array($feed)) {
  150. $id = $feed['id_feed'];
  151. } else {
  152. return false;
  153. }
  154. $timeToUpdate = trim($this->conf->rssImportFrec). ' ago';
  155. $delta = time() - strtotime($timeToUpdate);
  156. $this->ejecutarConsulta('SELECT (UNIX_TIMESTAMP(updated_at) - '.$delta.') - UNIX_TIMESTAMP(NOW()) FROM '.$this->conf->tablePrefix.'feeds WHERE id_feed = '.$id);
  157. $time = $this->mid_consulta->fetch();
  158. return $time[0];
  159. }
  160. }
  161. ?>