lib_rss.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. // vérifie qu'on est connecté
  3. function is_logged () {
  4. return Session::param ('mail') != false;
  5. }
  6. // vérifie que le système d'authentification est configuré
  7. function login_is_conf ($conf) {
  8. return $conf->mailLogin () != false;
  9. }
  10. // tiré de Shaarli de Seb Sauvage
  11. function small_hash ($txt) {
  12. $t = rtrim (base64_encode (hash ('crc32', $txt, true)), '=');
  13. $t = str_replace ('+', '-', $t); // Get rid of characters which need encoding in URLs.
  14. $t = str_replace ('/', '_', $t);
  15. $t = str_replace ('=', '@', $t);
  16. return $t;
  17. }
  18. function timestamptodate ($t, $hour = true) {
  19. $month = Translate::t (date('M', $t));
  20. if ($hour) {
  21. $date = Translate::t ('format_date_hour', $month);
  22. } else {
  23. $date = Translate::t ('format_date', $month);
  24. }
  25. return date ($date, $t);
  26. }
  27. function sortEntriesByDate ($entry1, $entry2) {
  28. return $entry2->date (true) - $entry1->date (true);
  29. }
  30. function sortReverseEntriesByDate ($entry1, $entry2) {
  31. return $entry1->date (true) - $entry2->date (true);
  32. }
  33. function get_domain ($url) {
  34. return parse_url($url, PHP_URL_HOST);
  35. }
  36. function opml_export ($cats) {
  37. $txt = '';
  38. foreach ($cats as $cat) {
  39. $txt .= '<outline text="' . $cat['name'] . '">' . "\n";
  40. foreach ($cat['feeds'] as $feed) {
  41. $txt .= "\t" . '<outline text="' . cleanText ($feed->name ()) . '" type="rss" xmlUrl="' . htmlentities ($feed->url (), ENT_COMPAT, 'UTF-8') . '" htmlUrl="' . htmlentities ($feed->website (), ENT_COMPAT, 'UTF-8') . '" />' . "\n";
  42. }
  43. $txt .= '</outline>' . "\n";
  44. }
  45. return $txt;
  46. }
  47. function cleanText ($text) {
  48. return preg_replace ('/&[\w]+;/', '', $text);
  49. }
  50. function opml_import ($xml) {
  51. $opml = @simplexml_load_string ($xml);
  52. if (!$opml) {
  53. throw new OpmlException ();
  54. }
  55. $catDAO = new CategoryDAO();
  56. $catDAO->checkDefault();
  57. $defCat = $catDAO->getDefault();
  58. $categories = array ();
  59. $feeds = array ();
  60. foreach ($opml->body->outline as $outline) {
  61. if (!isset ($outline['xmlUrl'])) {
  62. // Catégorie
  63. $title = '';
  64. if (isset ($outline['text'])) {
  65. $title = (string) $outline['text'];
  66. } elseif (isset ($outline['title'])) {
  67. $title = (string) $outline['title'];
  68. }
  69. if ($title) {
  70. // Permet d'éviter les soucis au niveau des id :
  71. // ceux-ci sont générés en fonction de la date,
  72. // un flux pourrait être dans une catégorie X avec l'id Y
  73. // alors qu'il existe déjà la catégorie X mais avec l'id Z
  74. // Y ne sera pas ajouté et le flux non plus vu que l'id
  75. // de sa catégorie n'exisera pas
  76. $catDAO = new CategoryDAO ();
  77. $cat = $catDAO->searchByName ($title);
  78. if ($cat === false) {
  79. $cat = new Category ($title);
  80. }
  81. $categories[] = $cat;
  82. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  83. }
  84. } else {
  85. // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
  86. $feeds[] = getFeed ($outline, $defCat->id());
  87. }
  88. }
  89. return array ($categories, $feeds);
  90. }
  91. /**
  92. * import all feeds of a given outline tag
  93. */
  94. function getFeedsOutline ($outline, $cat_id) {
  95. $feeds = array ();
  96. foreach ($outline->children () as $child) {
  97. if (isset ($child['xmlUrl'])) {
  98. $feeds[] = getFeed ($child, $cat_id);
  99. } else {
  100. $feeds = array_merge(
  101. $feeds,
  102. getFeedsOutline ($child, $cat_id)
  103. );
  104. }
  105. }
  106. return $feeds;
  107. }
  108. function getFeed ($outline, $cat_id) {
  109. $url = (string) $outline['xmlUrl'];
  110. $title = '';
  111. if (isset ($outline['text'])) {
  112. $title = (string) $outline['text'];
  113. } elseif (isset ($outline['title'])) {
  114. $title = (string) $outline['title'];
  115. }
  116. $feed = new Feed ($url);
  117. $feed->_category ($cat_id);
  118. $feed->_name ($title);
  119. return $feed;
  120. }
  121. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  122. function get_content_by_parsing ($url, $path) {
  123. $html = file_get_contents ($url);
  124. if ($html) {
  125. $doc = phpQuery::newDocument ($html);
  126. $content = $doc->find ($path);
  127. $content->find ('*')->removeAttr ('style')
  128. ->removeAttr ('id')
  129. ->removeAttr ('class')
  130. ->removeAttr ('onload')
  131. ->removeAttr ('target');
  132. $content->removeAttr ('style')
  133. ->removeAttr ('id')
  134. ->removeAttr ('class')
  135. ->removeAttr ('onload')
  136. ->removeAttr ('target');
  137. return $content->__toString ();
  138. } else {
  139. throw new Exception ();
  140. }
  141. }
  142. /* Télécharge le favicon d'un site, le place sur le serveur et retourne l'URL */
  143. function dowload_favicon ($website, $id) {
  144. $url = 'http://g.etfv.co/' . $website;
  145. $favicons_dir = PUBLIC_PATH . '/data/favicons';
  146. $dest = $favicons_dir . '/' . $id . '.ico';
  147. $favicon_url = '/data/favicons/' . $id . '.ico';
  148. if (!is_dir ($favicons_dir)) {
  149. if (!mkdir ($favicons_dir, 0755, true)) {
  150. return $url;
  151. }
  152. }
  153. if (!file_exists ($dest)) {
  154. $c = curl_init ($url);
  155. curl_setopt ($c, CURLOPT_HEADER, false);
  156. curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
  157. curl_setopt ($c, CURLOPT_BINARYTRANSFER, true);
  158. $imgRaw = curl_exec ($c);
  159. if (curl_getinfo ($c, CURLINFO_HTTP_CODE) == 200) {
  160. $file = fopen ($dest, 'w');
  161. if ($file === false) {
  162. return $url;
  163. }
  164. fwrite ($file, $imgRaw);
  165. fclose ($file);
  166. } else {
  167. return $url;
  168. }
  169. curl_close ($c);
  170. }
  171. return $favicon_url;
  172. }