lib_rss.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 ()) . '" htmlUrl="' . htmlentities ($feed->website ()) . '" />' . "\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. return array (array (), array ());
  54. }
  55. $categories = array ();
  56. $feeds = array ();
  57. foreach ($opml->body->outline as $outline) {
  58. if (!isset ($outline['xmlUrl'])) {
  59. // Catégorie
  60. $title = '';
  61. if (isset ($outline['text'])) {
  62. $title = (string) $outline['text'];
  63. } elseif (isset ($outline['title'])) {
  64. $title = (string) $outline['title'];
  65. }
  66. if ($title) {
  67. // Permet d'éviter les soucis au niveau des id :
  68. // ceux-ci sont générés en fonction de la date,
  69. // un flux pourrait être dans une catégorie X avec l'id Y
  70. // alors qu'il existe déjà la catégorie X mais avec l'id Z
  71. // Y ne sera pas ajouté et le flux non plus vu que l'id
  72. // de sa catégorie n'exisera pas
  73. $catDAO = new CategoryDAO ();
  74. $cat = $catDAO->searchByName ($title);
  75. if ($cat === false) {
  76. $cat = new Category ($title);
  77. }
  78. $categories[] = $cat;
  79. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  80. }
  81. } else {
  82. // Flux rss
  83. $feeds[] = getFeed ($outline, '');
  84. }
  85. }
  86. return array ($categories, $feeds);
  87. }
  88. /**
  89. * import all feeds of a given outline tag
  90. */
  91. function getFeedsOutline ($outline, $cat_id) {
  92. $feeds = array ();
  93. foreach ($outline->children () as $child) {
  94. if (isset ($child['xmlUrl'])) {
  95. $feeds[] = getFeed ($child, $cat_id);
  96. } else {
  97. $feeds = array_merge(
  98. $feeds,
  99. getFeedsOutline ($child, $cat_id)
  100. );
  101. }
  102. }
  103. return $feeds;
  104. }
  105. function getFeed ($outline, $cat_id) {
  106. $url = (string) $outline['xmlUrl'];
  107. $feed = new Feed ($url);
  108. $feed->_category ($cat_id);
  109. return $feed;
  110. }
  111. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  112. function get_content_by_parsing ($url, $path) {
  113. $html = file_get_contents ($url);
  114. if ($html) {
  115. $doc = phpQuery::newDocument ($html);
  116. $content = $doc->find ($path);
  117. $content->find ('*')->removeAttr ('style')
  118. ->removeAttr ('id')
  119. ->removeAttr ('class')
  120. ->removeAttr ('onload')
  121. ->removeAttr ('target');
  122. $content->removeAttr ('style')
  123. ->removeAttr ('id')
  124. ->removeAttr ('class')
  125. ->removeAttr ('onload')
  126. ->removeAttr ('target');
  127. return $content->__toString ();
  128. } else {
  129. throw new Exception ();
  130. }
  131. }
  132. /* Télécharge le favicon d'un site, le place sur le serveur et retourne l'URL */
  133. function dowload_favicon ($website, $id) {
  134. $url = 'http://g.etfv.co/' . $website;
  135. $favicons_dir = PUBLIC_PATH . '/data/favicons';
  136. $dest = $favicons_dir . '/' . $id . '.ico';
  137. $favicon_url = '/data/favicons/' . $id . '.ico';
  138. if (!is_dir ($favicons_dir)) {
  139. if (!mkdir ($favicons_dir, 0755, true)) {
  140. return $url;
  141. }
  142. }
  143. if (!file_exists ($dest)) {
  144. $c = curl_init ($url);
  145. curl_setopt ($c, CURLOPT_HEADER, false);
  146. curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
  147. curl_setopt ($c, CURLOPT_BINARYTRANSFER, true);
  148. $imgRaw = curl_exec ($c);
  149. if (curl_getinfo ($c, CURLINFO_HTTP_CODE) == 200) {
  150. $file = fopen ($dest, 'w');
  151. if ($file === false) {
  152. return $url;
  153. }
  154. fwrite ($file, $imgRaw);
  155. fclose ($file);
  156. } else {
  157. return $url;
  158. }
  159. curl_close ($c);
  160. }
  161. return $favicon_url;
  162. }