lib_rss.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 //Format RFC 4648 base64url
  11. function small_hash ($txt) {
  12. $t = rtrim (base64_encode (hash ('crc32', $txt, true)), '=');
  13. return strtr ($t, '+/', '-_');
  14. }
  15. function timestamptodate ($t, $hour = true) {
  16. $month = Translate::t (date('M', $t));
  17. if ($hour) {
  18. $date = Translate::t ('format_date_hour', $month);
  19. } else {
  20. $date = Translate::t ('format_date', $month);
  21. }
  22. return date ($date, $t);
  23. }
  24. function sortEntriesByDate ($entry1, $entry2) {
  25. return $entry2->date (true) - $entry1->date (true);
  26. }
  27. function sortReverseEntriesByDate ($entry1, $entry2) {
  28. return $entry1->date (true) - $entry2->date (true);
  29. }
  30. function get_domain ($url) {
  31. return parse_url($url, PHP_URL_HOST);
  32. }
  33. function opml_export ($cats) {
  34. $txt = '';
  35. foreach ($cats as $cat) {
  36. $txt .= '<outline text="' . $cat['name'] . '">' . "\n";
  37. foreach ($cat['feeds'] as $feed) {
  38. $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";
  39. }
  40. $txt .= '</outline>' . "\n";
  41. }
  42. return $txt;
  43. }
  44. function cleanText ($text) {
  45. return preg_replace ('/&[\w]+;/', '', $text);
  46. }
  47. function opml_import ($xml) {
  48. $opml = @simplexml_load_string ($xml);
  49. if (!$opml) {
  50. throw new OpmlException ();
  51. }
  52. $catDAO = new CategoryDAO();
  53. $catDAO->checkDefault();
  54. $defCat = $catDAO->getDefault();
  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 sans catégorie, on récupère l'ajoute dans la catégorie par défaut
  83. $feeds[] = getFeed ($outline, $defCat->id());
  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. $title = '';
  108. if (isset ($outline['text'])) {
  109. $title = (string) $outline['text'];
  110. } elseif (isset ($outline['title'])) {
  111. $title = (string) $outline['title'];
  112. }
  113. $feed = new Feed ($url);
  114. $feed->_category ($cat_id);
  115. $feed->_name ($title);
  116. return $feed;
  117. }
  118. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  119. function get_content_by_parsing ($url, $path) {
  120. $html = file_get_contents ($url);
  121. if ($html) {
  122. $doc = phpQuery::newDocument ($html);
  123. $content = $doc->find ($path);
  124. $content->find ('*')->removeAttr ('style')
  125. ->removeAttr ('id')
  126. ->removeAttr ('class')
  127. ->removeAttr ('onload')
  128. ->removeAttr ('target');
  129. $content->removeAttr ('style')
  130. ->removeAttr ('id')
  131. ->removeAttr ('class')
  132. ->removeAttr ('onload')
  133. ->removeAttr ('target');
  134. return $content->__toString ();
  135. } else {
  136. throw new Exception ();
  137. }
  138. }
  139. /* Télécharge le favicon d'un site, le place sur le serveur et retourne l'URL */
  140. function dowload_favicon ($website, $id) {
  141. $url = 'http://g.etfv.co/' . $website;
  142. $favicons_dir = PUBLIC_PATH . '/favicons';
  143. $dest = $favicons_dir . '/' . $id . '.ico';
  144. $favicon_url = '/favicons/' . $id . '.ico';
  145. if (!is_dir ($favicons_dir)) {
  146. if (!mkdir ($favicons_dir, 0755, true)) {
  147. return $url;
  148. }
  149. }
  150. if (!file_exists ($dest)) {
  151. $c = curl_init ($url);
  152. curl_setopt ($c, CURLOPT_HEADER, false);
  153. curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
  154. curl_setopt ($c, CURLOPT_BINARYTRANSFER, true);
  155. $imgRaw = curl_exec ($c);
  156. if (curl_getinfo ($c, CURLINFO_HTTP_CODE) == 200) {
  157. $file = fopen ($dest, 'w');
  158. if ($file === false) {
  159. return $url;
  160. }
  161. fwrite ($file, $imgRaw);
  162. fclose ($file);
  163. } else {
  164. return $url;
  165. }
  166. curl_close ($c);
  167. }
  168. return $favicon_url;
  169. }
  170. /**
  171. * Add support of image lazy loading
  172. * Move content from src attribute to data-original
  173. * @param content is the text we want to parse
  174. */
  175. function lazyimg($content) {
  176. return preg_replace(
  177. '/<img([^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  178. '<img$1src="' . Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  179. $content
  180. );
  181. }