A tumblelog CMS built on AJAX, PHP and MySQL.

feeds.class.php 6.5KB

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