lib_rss.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. if (!function_exists('json_decode')) {
  3. require_once('JSON.php');
  4. function json_decode($var) {
  5. $JSON = new Services_JSON;
  6. return (array)($JSON->decode($var));
  7. }
  8. }
  9. if (!function_exists('json_encode')) {
  10. require_once('JSON.php');
  11. function json_encode($var) {
  12. $JSON = new Services_JSON;
  13. return $JSON->encodeUnsafe($var);
  14. }
  15. }
  16. // vérifie qu'on est connecté
  17. function is_logged () {
  18. return Session::param ('mail') != false;
  19. }
  20. // vérifie que le système d'authentification est configuré
  21. function login_is_conf ($conf) {
  22. return $conf->mailLogin () != false;
  23. }
  24. // tiré de Shaarli de Seb Sauvage //Format RFC 4648 base64url
  25. function small_hash ($txt) {
  26. $t = rtrim (base64_encode (hash ('crc32', $txt, true)), '=');
  27. return strtr ($t, '+/', '-_');
  28. }
  29. function timestamptodate ($t, $hour = true) {
  30. $month = Translate::t (date('M', $t));
  31. if ($hour) {
  32. $date = Translate::t ('format_date_hour', $month);
  33. } else {
  34. $date = Translate::t ('format_date', $month);
  35. }
  36. return date ($date, $t);
  37. }
  38. function sortEntriesByDate ($entry1, $entry2) {
  39. return $entry2->date (true) - $entry1->date (true);
  40. }
  41. function sortReverseEntriesByDate ($entry1, $entry2) {
  42. return $entry1->date (true) - $entry2->date (true);
  43. }
  44. function get_domain ($url) {
  45. return parse_url($url, PHP_URL_HOST);
  46. }
  47. function opml_export ($cats) {
  48. $txt = '';
  49. foreach ($cats as $cat) {
  50. $txt .= '<outline text="' . $cat['name'] . '">' . "\n";
  51. foreach ($cat['feeds'] as $feed) {
  52. $txt .= "\t" . '<outline text="' . $feed->name () . '" type="rss" xmlUrl="' . $feed->url () . '" htmlUrl="' . $feed->website () . '" />' . "\n";
  53. }
  54. $txt .= '</outline>' . "\n";
  55. }
  56. return $txt;
  57. }
  58. function html_only_entity_decode($text) {
  59. static $htmlEntitiesOnly = null;
  60. if ($htmlEntitiesOnly === null) {
  61. $htmlEntitiesOnly = array_flip(array_diff(
  62. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  63. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  64. ));
  65. }
  66. return strtr($text, $htmlEntitiesOnly);
  67. }
  68. function opml_import ($xml) {
  69. $xml = html_only_entity_decode($xml); //!\ Assume UTF-8
  70. $opml = simplexml_load_string ($xml);
  71. if (!$opml) {
  72. throw new OpmlException ();
  73. }
  74. $catDAO = new CategoryDAO();
  75. $catDAO->checkDefault();
  76. $defCat = $catDAO->getDefault();
  77. $categories = array ();
  78. $feeds = array ();
  79. foreach ($opml->body->outline as $outline) {
  80. if (!isset ($outline['xmlUrl'])) {
  81. // Catégorie
  82. $title = '';
  83. if (isset ($outline['text'])) {
  84. $title = (string) $outline['text'];
  85. } elseif (isset ($outline['title'])) {
  86. $title = (string) $outline['title'];
  87. }
  88. if ($title) {
  89. // Permet d'éviter les soucis au niveau des id :
  90. // ceux-ci sont générés en fonction de la date,
  91. // un flux pourrait être dans une catégorie X avec l'id Y
  92. // alors qu'il existe déjà la catégorie X mais avec l'id Z
  93. // Y ne sera pas ajouté et le flux non plus vu que l'id
  94. // de sa catégorie n'exisera pas
  95. $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
  96. $catDAO = new CategoryDAO ();
  97. $cat = $catDAO->searchByName ($title);
  98. if ($cat === false) {
  99. $cat = new Category ($title);
  100. $values = array (
  101. 'name' => $cat->name (),
  102. 'color' => $cat->color ()
  103. );
  104. $cat->_id ($catDAO->addCategory ($values));
  105. }
  106. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  107. }
  108. } else {
  109. // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
  110. $feeds[] = getFeed ($outline, $defCat->id());
  111. }
  112. }
  113. return array ($categories, $feeds);
  114. }
  115. /**
  116. * import all feeds of a given outline tag
  117. */
  118. function getFeedsOutline ($outline, $cat_id) {
  119. $feeds = array ();
  120. foreach ($outline->children () as $child) {
  121. if (isset ($child['xmlUrl'])) {
  122. $feeds[] = getFeed ($child, $cat_id);
  123. } else {
  124. $feeds = array_merge(
  125. $feeds,
  126. getFeedsOutline ($child, $cat_id)
  127. );
  128. }
  129. }
  130. return $feeds;
  131. }
  132. function getFeed ($outline, $cat_id) {
  133. $url = (string) $outline['xmlUrl'];
  134. $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
  135. $title = '';
  136. if (isset ($outline['text'])) {
  137. $title = (string) $outline['text'];
  138. } elseif (isset ($outline['title'])) {
  139. $title = (string) $outline['title'];
  140. }
  141. $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
  142. $feed = new Feed ($url);
  143. $feed->_category ($cat_id);
  144. $feed->_name ($title);
  145. return $feed;
  146. }
  147. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  148. function get_content_by_parsing ($url, $path) {
  149. require_once (LIB_PATH . '/lib_phpQuery.php');
  150. $html = file_get_contents ($url);
  151. if ($html) {
  152. $doc = phpQuery::newDocument ($html);
  153. $content = $doc->find ($path);
  154. $content->find ('*')->removeAttr ('style')
  155. ->removeAttr ('id')
  156. ->removeAttr ('class')
  157. ->removeAttr ('onload')
  158. ->removeAttr ('target');
  159. $content->removeAttr ('style')
  160. ->removeAttr ('id')
  161. ->removeAttr ('class')
  162. ->removeAttr ('onload')
  163. ->removeAttr ('target');
  164. return $content->__toString ();
  165. } else {
  166. throw new Exception ();
  167. }
  168. }
  169. /**
  170. * Add support of image lazy loading
  171. * Move content from src attribute to data-original
  172. * @param content is the text we want to parse
  173. */
  174. function lazyimg($content) {
  175. return preg_replace(
  176. '/<img([^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  177. '<img$1src="' . Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  178. $content
  179. );
  180. }
  181. function invalidateHttpCache() {
  182. file_put_contents(DATA_PATH . '/touch.txt', microtime(true));
  183. }