lib_rss.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. $catDAO = new CategoryDAO ();
  68. $cat = $catDAO->searchByName ($title);
  69. if ($cat === false) {
  70. $cat = new Category ($title);
  71. }
  72. $categories[] = $cat;
  73. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  74. }
  75. } else {
  76. // Flux rss
  77. $feeds[] = getFeed ($outline, '');
  78. }
  79. }
  80. return array ($categories, $feeds);
  81. }
  82. /**
  83. * import all feeds of a given outline tag
  84. */
  85. function getFeedsOutline ($outline, $cat_id) {
  86. $feeds = array ();
  87. foreach ($outline->children () as $child) {
  88. if (isset ($child['xmlUrl'])) {
  89. $feeds[] = getFeed ($child, $cat_id);
  90. } else {
  91. $feeds = array_merge(
  92. $feeds,
  93. getFeedsOutline ($child, $cat_id)
  94. );
  95. }
  96. }
  97. return $feeds;
  98. }
  99. function getFeed ($outline, $cat_id) {
  100. $url = (string) $outline['xmlUrl'];
  101. $feed = new Feed ($url);
  102. $feed->_category ($cat_id);
  103. return $feed;
  104. }
  105. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  106. function get_content_by_parsing ($url, $path) {
  107. $html = file_get_contents ($url);
  108. if ($html) {
  109. $doc = phpQuery::newDocument ($html);
  110. $content = $doc->find ($path);
  111. $content->find ('*')->removeAttr ('style')
  112. ->removeAttr ('id')
  113. ->removeAttr ('class')
  114. ->removeAttr ('onload')
  115. ->removeAttr ('target');
  116. $content->removeAttr ('style')
  117. ->removeAttr ('id')
  118. ->removeAttr ('class')
  119. ->removeAttr ('onload')
  120. ->removeAttr ('target');
  121. return $content->__toString ();
  122. } else {
  123. throw new Exception ();
  124. }
  125. }