lib_rss.php 6.0 KB

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