A tumblelog CMS built on AJAX, PHP and MySQL.

feeds.class.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 'Flickr': $f['url'] = 'http:/flickr.com/photos/{{username}}'; $f['type'] = 2; break;
  35. /*>>*/ case 'Twitter': $f['url'] = 'http://twitter.com/{{username}}'; $f['type'] = 1; break;
  36. case 'Last.fm': $f['url'] = 'http://ws.audioscrobbler.com/1.0/user/{{username}}/recenttracks.rss'; $f['type'] = 1; break;
  37. case 'Blogger': $f['url'] = 'http://{{username}}.blogspot.com/feeds/posts/default'; $f['type'] = 1; break;
  38. case 'Youtube': $f['url'] = 'http://www.youtube.com/rss/user/{{username}}/videos.rss'; $f['type'] = 1; break;
  39. case 'Wordpress.com': $f['url'] = 'http://{{username}}.wordpress.com/feed/'; $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. }
  81. }
  82. }elseif($feed['type'] == 2){ //IMAGES
  83. $ma = array();
  84. $url_image = '';
  85. @preg_match_all('/\<img ([^\>]+)/',$post->get_description(), $ma);
  86. @preg_match_all('/src\=\"([^\"]+)\"/',$ma[0][0], $ma);
  87. $url_image = $ma[1][0];
  88. if(empty($url_image)){
  89. /* Theres no image, lets make a text post */
  90. $newPost['type'] = 1;
  91. if($post->get_title() != $post->get_description()){
  92. if(strpos($feed['url'],'twitter.com') <= 0){
  93. $newPost['description'] = $post->get_description();
  94. }
  95. }
  96. }else{
  97. $newPost['type'] = 2;
  98. $newPost['url'] = $url_image;
  99. $newPost['description'] = $post->get_title();
  100. }
  101. }
  102. if($feed['credits'] == 1 && !empty($feed['title'])){
  103. $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>';
  104. }
  105. $this->insertarDeFormulario($this->conf->tablePrefix."data", $newPost);
  106. }
  107. }
  108. $temp['title'] = (!empty($data->data['info']['title']))? $data->data['info']['title'] : '';
  109. $temp['site_url'] = (!empty($data->data['info']['link']['alternate'][0]))? $data->data['info']['link']['alternate'][0] : $data->data['info']['link'];
  110. $temp['error'] = 0;
  111. $this->modificarDeFormulario($this->conf->tablePrefix."feeds", $temp, 'id_feed = '.$feed['id_feed']);
  112. }
  113. }
  114. }
  115. }
  116. function getFeedList($condition = ''){
  117. $feeds = array();
  118. $this->ejecutarConsulta('SELECT * FROM '.$this->conf->tablePrefix.'feeds '.$condition.' ORDER BY id_feed DESC');
  119. while($feed = mysql_fetch_assoc($this->mid_consulta)){
  120. $feeds[] = $feed;
  121. }
  122. return $feeds;
  123. }
  124. }
  125. ?>