lib_rss.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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', $url)) {
  21. $url = 'http://' . $url;
  22. }
  23. if (filter_var($url, FILTER_VALIDATE_URL) ||
  24. (version_compare(PHP_VERSION, '5.3.3', '<') && (strpos($url, '-') > 0) && //PHP bug #51192
  25. ($url === filter_var($url, 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 formatBytes($bytes, $precision = 2, $system = 'IEC') {
  45. if ($system === 'IEC') {
  46. $base = 1024;
  47. $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB');
  48. } elseif ($system === 'SI') {
  49. $base = 1000;
  50. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  51. }
  52. $bytes = max(intval($bytes), 0);
  53. $pow = $bytes === 0 ? 0 : floor(log($bytes) / log($base));
  54. $pow = min($pow, count($units) - 1);
  55. $bytes /= pow($base, $pow);
  56. return round($bytes, $precision) . ' ' . $units[$pow];
  57. }
  58. function timestamptodate ($t, $hour = true) {
  59. $month = Translate::t (date('M', $t));
  60. if ($hour) {
  61. $date = Translate::t ('format_date_hour', $month);
  62. } else {
  63. $date = Translate::t ('format_date', $month);
  64. }
  65. return @date ($date, $t);
  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="' . $feed->name () . '" type="rss" xmlUrl="' . $feed->url () . '" htmlUrl="' . $feed->website () . '" description="' . $feed->description () . '" />' . "\n";
  82. }
  83. $txt .= '</outline>' . "\n";
  84. }
  85. return $txt;
  86. }
  87. function html_only_entity_decode($text) {
  88. static $htmlEntitiesOnly = null;
  89. if ($htmlEntitiesOnly === null) {
  90. $htmlEntitiesOnly = array_flip(array_diff(
  91. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  92. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  93. ));
  94. }
  95. return strtr($text, $htmlEntitiesOnly);
  96. }
  97. function opml_import ($xml) {
  98. $xml = html_only_entity_decode($xml); //!\ Assume UTF-8
  99. $dom = new DOMDocument();
  100. $dom->recover = true;
  101. $dom->strictErrorChecking = false;
  102. $dom->loadXML($xml);
  103. $dom->encoding = 'UTF-8';
  104. $opml = simplexml_import_dom($dom);
  105. if (!$opml) {
  106. throw new OpmlException ();
  107. }
  108. $catDAO = new CategoryDAO();
  109. $catDAO->checkDefault();
  110. $defCat = $catDAO->getDefault();
  111. $categories = array ();
  112. $feeds = array ();
  113. foreach ($opml->body->outline as $outline) {
  114. if (!isset ($outline['xmlUrl'])) {
  115. // Catégorie
  116. $title = '';
  117. if (isset ($outline['text'])) {
  118. $title = (string) $outline['text'];
  119. } elseif (isset ($outline['title'])) {
  120. $title = (string) $outline['title'];
  121. }
  122. if ($title) {
  123. // Permet d'éviter les soucis au niveau des id :
  124. // ceux-ci sont générés en fonction de la date,
  125. // un flux pourrait être dans une catégorie X avec l'id Y
  126. // alors qu'il existe déjà la catégorie X mais avec l'id Z
  127. // Y ne sera pas ajouté et le flux non plus vu que l'id
  128. // de sa catégorie n'exisera pas
  129. $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
  130. $catDAO = new CategoryDAO ();
  131. $cat = $catDAO->searchByName ($title);
  132. if ($cat === false) {
  133. $cat = new Category ($title);
  134. $values = array (
  135. 'name' => $cat->name (),
  136. 'color' => $cat->color ()
  137. );
  138. $cat->_id ($catDAO->addCategory ($values));
  139. }
  140. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  141. }
  142. } else {
  143. // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
  144. $feeds[] = getFeed ($outline, $defCat->id());
  145. }
  146. }
  147. return array ($categories, $feeds);
  148. }
  149. /**
  150. * import all feeds of a given outline tag
  151. */
  152. function getFeedsOutline ($outline, $cat_id) {
  153. $feeds = array ();
  154. foreach ($outline->children () as $child) {
  155. if (isset ($child['xmlUrl'])) {
  156. $feeds[] = getFeed ($child, $cat_id);
  157. } else {
  158. $feeds = array_merge(
  159. $feeds,
  160. getFeedsOutline ($child, $cat_id)
  161. );
  162. }
  163. }
  164. return $feeds;
  165. }
  166. function getFeed ($outline, $cat_id) {
  167. $url = (string) $outline['xmlUrl'];
  168. $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
  169. $title = '';
  170. if (isset ($outline['text'])) {
  171. $title = (string) $outline['text'];
  172. } elseif (isset ($outline['title'])) {
  173. $title = (string) $outline['title'];
  174. }
  175. $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
  176. $feed = new Feed ($url);
  177. $feed->_category ($cat_id);
  178. $feed->_name ($title);
  179. if (isset($outline['htmlUrl'])) {
  180. $feed->_website(htmlspecialchars((string)$outline['htmlUrl'], ENT_QUOTES, 'UTF-8'));
  181. }
  182. if (isset($outline['description'])) {
  183. $feed->_description(htmlspecialchars((string)$outline['description'], ENT_QUOTES, 'UTF-8'));
  184. }
  185. return $feed;
  186. }
  187. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  188. function get_content_by_parsing ($url, $path) {
  189. require_once (LIB_PATH . '/lib_phpQuery.php');
  190. $html = file_get_contents ($url);
  191. if ($html) {
  192. $doc = phpQuery::newDocument ($html);
  193. $content = $doc->find ($path);
  194. $content->find ('*')->removeAttr ('style')
  195. ->removeAttr ('id')
  196. ->removeAttr ('class')
  197. ->removeAttr ('onload')
  198. ->removeAttr ('target');
  199. $content->removeAttr ('style')
  200. ->removeAttr ('id')
  201. ->removeAttr ('class')
  202. ->removeAttr ('onload')
  203. ->removeAttr ('target');
  204. return $content->__toString ();
  205. } else {
  206. throw new Exception ();
  207. }
  208. }
  209. /**
  210. * Add support of image lazy loading
  211. * Move content from src attribute to data-original
  212. * @param content is the text we want to parse
  213. */
  214. function lazyimg($content) {
  215. return preg_replace(
  216. '/<img([^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  217. '<img$1src="' . Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  218. $content
  219. );
  220. }
  221. function invalidateHttpCache() {
  222. file_put_contents(DATA_PATH . '/touch.txt', microtime(true));
  223. }