A tumblelog CMS built on AJAX, PHP and MySQL.

feeds.class.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. default : $f['url'] = ''; break;
  40. }
  41. if(!empty($f['url'])){
  42. $f['url'] = str_replace('{{username}}',$url,$f['url']);
  43. return ($this->insertarDeFormulario($this->conf->tablePrefix."feeds", $f));
  44. }
  45. print_r($_POST);
  46. print_r($f);
  47. }
  48. function removeFeed($id){
  49. return ($this->ejecutarConsulta("DELETE FROM ".$this->conf->tablePrefix."feeds WHERE id_feed=".$id));
  50. }
  51. function updateFeeds(){
  52. $timeToUpdate = trim($this->conf->rssImportFrec). ' ago';
  53. $feeds = $this->getFeedList("WHERE updated_at < '".date("Y-m-d H:i:s",strtotime($timeToUpdate))."'");
  54. foreach($feeds as $feed){
  55. $data = new SimplePie();
  56. $data->feed_url($feed['url']);
  57. $data->cache_location(Absolute_Path."/uploads/CACHE");
  58. $data->init();
  59. $temp = array();
  60. $temp['updated_at'] = date("Y-m-d H:i:s");
  61. if (!empty($data->error)){
  62. // Error report
  63. $temp['error'] = 1;
  64. $this->modificarDeFormulario($this->conf->tablePrefix."feeds", $temp, 'id_feed = '.$feed['id_feed']);
  65. }else{
  66. if($data->data){
  67. $timeFilter = strtotime($timeToUpdate);
  68. foreach($data->get_items() as $post){
  69. if (strtotime($post->get_date("Y-m-d H:i:s")) > strtotime($feed['updated_at'])) {
  70. $newPost = array();
  71. $newPost['id_user'] = $feed['id_user'];
  72. $newPost['title'] = $post->get_title();
  73. $newPost['date'] = $post->get_date("Y-m-d H:i:s");
  74. if($feed['type'] == 1){ //TEXT
  75. $newPost['type'] = 1;
  76. if($post->get_title() != $post->get_description()){
  77. if(strpos($feed['url'],'twitter.com') <= 0){
  78. $newPost['description'] = $post->get_description();
  79. }
  80. }
  81. }elseif($feed['type'] == 2){ //IMAGES
  82. $ma = array();
  83. $url_image = '';
  84. @preg_match_all('/\<img ([^\>]+)/',$post->get_description(), $ma);
  85. @preg_match_all('/src\=\"([^\"]+)\"/',$ma[0][0], $ma);
  86. $url_image = $ma[1][0];
  87. if(empty($url_image)){
  88. /* Theres no image, lets make a text post */
  89. $newPost['type'] = 1;
  90. if($post->get_title() != $post->get_description()){
  91. if(strpos($feed['url'],'twitter.com') <= 0){
  92. $newPost['description'] = $post->get_description();
  93. }
  94. }
  95. }else{
  96. $newPost['type'] = 2;
  97. $newPost['url'] = $url_image;
  98. $newPost['description'] = $post->get_title();
  99. }
  100. }
  101. if($feed['credits'] == 1 && !empty($feed['title'])){
  102. $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>';
  103. }
  104. $this->insertarDeFormulario($this->conf->tablePrefix."data", $newPost);
  105. }
  106. }
  107. $temp['title'] = (!empty($data->data['info']['title']))? $data->data['info']['title'] : '';
  108. $temp['site_url'] = (!empty($data->data['info']['link']['alternate'][0]))? $data->data['info']['link']['alternate'][0] : $data->data['info']['link'];
  109. $temp['error'] = 0;
  110. $this->modificarDeFormulario($this->conf->tablePrefix."feeds", $temp, 'id_feed = '.$feed['id_feed']);
  111. }
  112. }
  113. }
  114. }
  115. function getFeedList($condition = ''){
  116. $feeds = array();
  117. $this->ejecutarConsulta('SELECT * FROM '.$this->conf->tablePrefix.'feeds '.$condition.' ORDER BY id_feed DESC');
  118. while($feed = mysql_fetch_assoc($this->mid_consulta)){
  119. $feeds[] = $feed;
  120. }
  121. return $feeds;
  122. }
  123. /**
  124. Calculate the seconds until next update
  125. @param $feed can be an ID, or the raw array from the DB
  126. @raturn int
  127. */
  128. function getNextUpdate($feed){
  129. if(is_numeric($feed)){
  130. $id = (int)$feed;
  131. }elseif(is_array($feed)){
  132. $id = $feed['id_feed'];
  133. }else{
  134. return false;
  135. }
  136. $timeToUpdate = trim($this->conf->rssImportFrec). ' ago';
  137. $delta = time() - strtotime($timeToUpdate);
  138. $this->ejecutarConsulta('SELECT (UNIX_TIMESTAMP(updated_at) - '.$delta.') - UNIX_TIMESTAMP(NOW()) FROM '.$this->conf->tablePrefix.'feeds WHERE id_feed = '.$id);
  139. $time = mysql_fetch_array($this->mid_consulta);
  140. return $time[0];
  141. }
  142. }
  143. ?>