A tumblelog CMS built on AJAX, PHP and MySQL.

feeds.class.php 8.1KB

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