lib_rss.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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="' . $feed->name () . '" type="rss" xmlUrl="' . $feed->url () . '" htmlUrl="' . $feed->website () . '" />' . "\n";
  39. }
  40. $txt .= '</outline>' . "\n";
  41. }
  42. return $txt;
  43. }
  44. function html_only_entity_decode($text) {
  45. static $htmlEntitiesOnly = null;
  46. if ($htmlEntitiesOnly === null) {
  47. $htmlEntitiesOnly = array_flip(array_diff(
  48. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  49. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  50. ));
  51. }
  52. return strtr($text, $htmlEntitiesOnly);
  53. }
  54. function opml_import ($xml) {
  55. $xml = html_only_entity_decode($xml); //!\ Assume UTF-8
  56. $opml = simplexml_load_string ($xml);
  57. if (!$opml) {
  58. throw new OpmlException ();
  59. }
  60. $catDAO = new CategoryDAO();
  61. $catDAO->checkDefault();
  62. $defCat = $catDAO->getDefault();
  63. $categories = array ();
  64. $feeds = array ();
  65. foreach ($opml->body->outline as $outline) {
  66. if (!isset ($outline['xmlUrl'])) {
  67. // Catégorie
  68. $title = '';
  69. if (isset ($outline['text'])) {
  70. $title = (string) $outline['text'];
  71. } elseif (isset ($outline['title'])) {
  72. $title = (string) $outline['title'];
  73. }
  74. if ($title) {
  75. // Permet d'éviter les soucis au niveau des id :
  76. // ceux-ci sont générés en fonction de la date,
  77. // un flux pourrait être dans une catégorie X avec l'id Y
  78. // alors qu'il existe déjà la catégorie X mais avec l'id Z
  79. // Y ne sera pas ajouté et le flux non plus vu que l'id
  80. // de sa catégorie n'exisera pas
  81. $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
  82. $catDAO = new CategoryDAO ();
  83. $cat = $catDAO->searchByName ($title);
  84. if ($cat === false) {
  85. $cat = new Category ($title);
  86. $values = array (
  87. 'name' => $cat->name (),
  88. 'color' => $cat->color ()
  89. );
  90. $cat->_id ($catDAO->addCategory ($values));
  91. }
  92. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  93. }
  94. } else {
  95. // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
  96. $feeds[] = getFeed ($outline, $defCat->id());
  97. }
  98. }
  99. return array ($categories, $feeds);
  100. }
  101. /**
  102. * import all feeds of a given outline tag
  103. */
  104. function getFeedsOutline ($outline, $cat_id) {
  105. $feeds = array ();
  106. foreach ($outline->children () as $child) {
  107. if (isset ($child['xmlUrl'])) {
  108. $feeds[] = getFeed ($child, $cat_id);
  109. } else {
  110. $feeds = array_merge(
  111. $feeds,
  112. getFeedsOutline ($child, $cat_id)
  113. );
  114. }
  115. }
  116. return $feeds;
  117. }
  118. function getFeed ($outline, $cat_id) {
  119. $url = (string) $outline['xmlUrl'];
  120. $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
  121. $title = '';
  122. if (isset ($outline['text'])) {
  123. $title = (string) $outline['text'];
  124. } elseif (isset ($outline['title'])) {
  125. $title = (string) $outline['title'];
  126. }
  127. $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
  128. $feed = new Feed ($url);
  129. $feed->_category ($cat_id);
  130. $feed->_name ($title);
  131. return $feed;
  132. }
  133. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  134. function get_content_by_parsing ($url, $path) {
  135. require_once (LIB_PATH . '/lib_phpQuery.php');
  136. $html = file_get_contents ($url);
  137. if ($html) {
  138. $doc = phpQuery::newDocument ($html);
  139. $content = $doc->find ($path);
  140. $content->find ('*')->removeAttr ('style')
  141. ->removeAttr ('id')
  142. ->removeAttr ('class')
  143. ->removeAttr ('onload')
  144. ->removeAttr ('target');
  145. $content->removeAttr ('style')
  146. ->removeAttr ('id')
  147. ->removeAttr ('class')
  148. ->removeAttr ('onload')
  149. ->removeAttr ('target');
  150. return $content->__toString ();
  151. } else {
  152. throw new Exception ();
  153. }
  154. }
  155. /* Télécharge le favicon d'un site, le place sur le serveur et retourne l'URL */
  156. function download_favicon ($website, $id) {
  157. $url = 'http://g.etfv.co/' . $website;
  158. $favicons_dir = PUBLIC_PATH . '/favicons';
  159. $dest = $favicons_dir . '/' . $id . '.ico';
  160. if (!is_dir ($favicons_dir)) {
  161. if (!mkdir ($favicons_dir, 0755, true)) {
  162. return $url;
  163. }
  164. }
  165. if (!file_exists ($dest)) {
  166. $c = curl_init ($url);
  167. curl_setopt ($c, CURLOPT_HEADER, false);
  168. curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
  169. curl_setopt ($c, CURLOPT_BINARYTRANSFER, true);
  170. $imgRaw = curl_exec ($c);
  171. if (curl_getinfo ($c, CURLINFO_HTTP_CODE) == 200) {
  172. $file = fopen ($dest, 'w');
  173. if ($file === false) {
  174. return $url;
  175. }
  176. fwrite ($file, $imgRaw);
  177. fclose ($file);
  178. } else {
  179. return $url;
  180. }
  181. curl_close ($c);
  182. }
  183. }
  184. /**
  185. * Add support of image lazy loading
  186. * Move content from src attribute to data-original
  187. * @param content is the text we want to parse
  188. */
  189. function lazyimg($content) {
  190. return preg_replace(
  191. '/<img([^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  192. '<img$1src="' . Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  193. $content
  194. );
  195. }
  196. function invalidateHttpCache() {
  197. file_put_contents(DATA_PATH . '/touch.txt', microtime(true));
  198. }