lib_rss.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. $jour = date ('d', $t);
  20. $mois = date ('m', $t);
  21. $annee = date ('Y', $t);
  22. switch ($mois) {
  23. case 1:
  24. $mois = 'janvier';
  25. break;
  26. case 2:
  27. $mois = 'février';
  28. break;
  29. case 3:
  30. $mois = 'mars';
  31. break;
  32. case 4:
  33. $mois = 'avril';
  34. break;
  35. case 5:
  36. $mois = 'mai';
  37. break;
  38. case 6:
  39. $mois = 'juin';
  40. break;
  41. case 7:
  42. $mois = 'juillet';
  43. break;
  44. case 8:
  45. $mois = 'août';
  46. break;
  47. case 9:
  48. $mois = 'septembre';
  49. break;
  50. case 10:
  51. $mois = 'octobre';
  52. break;
  53. case 11:
  54. $mois = 'novembre';
  55. break;
  56. case 12:
  57. $mois = 'décembre';
  58. break;
  59. }
  60. $date = $jour . ' ' . $mois . ' ' . $annee;
  61. if ($hour) {
  62. return $date . date (' \à H\:i', $t);
  63. } else {
  64. return $date;
  65. }
  66. }
  67. function sortEntriesByDate ($entry1, $entry2) {
  68. return $entry2->date (true) - $entry1->date (true);
  69. }
  70. function sortReverseEntriesByDate ($entry1, $entry2) {
  71. return $entry1->date (true) - $entry2->date (true);
  72. }
  73. function get_domain ($url) {
  74. return parse_url($url, PHP_URL_HOST);
  75. }
  76. function opml_export ($cats) {
  77. $txt = '';
  78. foreach ($cats as $cat) {
  79. $txt .= '<outline text="' . $cat['name'] . '">' . "\n";
  80. foreach ($cat['feeds'] as $feed) {
  81. $txt .= "\t" . '<outline text="' . cleanText ($feed->name ()) . '" type="rss" xmlUrl="' . $feed->url () . '" htmlUrl="' . $feed->website () . '" />' . "\n";
  82. }
  83. $txt .= '</outline>' . "\n";
  84. }
  85. return $txt;
  86. }
  87. function cleanText ($text) {
  88. return preg_replace ('/&[\w]+;/', '', $text);
  89. }
  90. function opml_import ($xml) {
  91. $opml = @simplexml_load_string ($xml);
  92. if (!$opml) {
  93. return array (array (), array ());
  94. }
  95. $categories = array ();
  96. $feeds = array ();
  97. foreach ($opml->body->outline as $outline) {
  98. if (!isset ($outline['xmlUrl'])) {
  99. // Catégorie
  100. $title = '';
  101. if (isset ($outline['text'])) {
  102. $title = (string) $outline['text'];
  103. } elseif (isset ($outline['title'])) {
  104. $title = (string) $outline['title'];
  105. }
  106. if ($title) {
  107. $cat = new Category ($title);
  108. $categories[] = $cat;
  109. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  110. }
  111. } else {
  112. // Flux rss
  113. $feeds[] = getFeed ($outline, '');
  114. }
  115. }
  116. return array ($categories, $feeds);
  117. }
  118. /**
  119. * import all feeds of a given outline tag
  120. */
  121. function getFeedsOutline ($outline, $cat_id) {
  122. $feeds = array ();
  123. foreach ($outline->children () as $child) {
  124. if (isset ($child['xmlUrl'])) {
  125. $feeds[] = getFeed ($child, $cat_id);
  126. } else {
  127. $feeds = array_merge(
  128. $feeds,
  129. getFeedsOutline ($child, $cat_id)
  130. );
  131. }
  132. }
  133. return $feeds;
  134. }
  135. function getFeed ($outline, $cat_id) {
  136. $url = (string) $outline['xmlUrl'];
  137. $feed = new Feed ($url);
  138. $feed->_category ($cat_id);
  139. return $feed;
  140. }
  141. /*
  142. * Vérifie pour un site donné s'il faut aller parser directement sur le site
  143. * Renvoie le path (id et class html) pour récupérer le contenu, false si pas besoin
  144. * On se base sur une base connue de sites
  145. */
  146. function get_path ($url) {
  147. $list_sites_parse = include (PUBLIC_PATH . '/data/Sites.array.php');
  148. if (isset ($list_sites_parse[$url])) {
  149. return $list_sites_parse[$url];
  150. } else {
  151. return false;
  152. }
  153. }
  154. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  155. function get_content_by_parsing ($url, $path) {
  156. $html = file_get_contents ($url);
  157. if ($html) {
  158. $doc = phpQuery::newDocument ($html);
  159. $content = $doc->find ($path);
  160. $content->find ('*')->removeAttr ('style')
  161. ->removeAttr ('id')
  162. ->removeAttr ('class')
  163. ->removeAttr ('onload')
  164. ->removeAttr ('target');
  165. $content->removeAttr ('style')
  166. ->removeAttr ('id')
  167. ->removeAttr ('class')
  168. ->removeAttr ('onload')
  169. ->removeAttr ('target');
  170. return $content->__toString ();
  171. } else {
  172. throw new Exception ();
  173. }
  174. }