lib_rss.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 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 () . '" />' . "\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. $opml = simplexml_load_string ($xml);
  100. if (!$opml) {
  101. throw new OpmlException ();
  102. }
  103. $catDAO = new CategoryDAO();
  104. $catDAO->checkDefault();
  105. $defCat = $catDAO->getDefault();
  106. $categories = array ();
  107. $feeds = array ();
  108. foreach ($opml->body->outline as $outline) {
  109. if (!isset ($outline['xmlUrl'])) {
  110. // Catégorie
  111. $title = '';
  112. if (isset ($outline['text'])) {
  113. $title = (string) $outline['text'];
  114. } elseif (isset ($outline['title'])) {
  115. $title = (string) $outline['title'];
  116. }
  117. if ($title) {
  118. // Permet d'éviter les soucis au niveau des id :
  119. // ceux-ci sont générés en fonction de la date,
  120. // un flux pourrait être dans une catégorie X avec l'id Y
  121. // alors qu'il existe déjà la catégorie X mais avec l'id Z
  122. // Y ne sera pas ajouté et le flux non plus vu que l'id
  123. // de sa catégorie n'exisera pas
  124. $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
  125. $catDAO = new CategoryDAO ();
  126. $cat = $catDAO->searchByName ($title);
  127. if ($cat === false) {
  128. $cat = new Category ($title);
  129. $values = array (
  130. 'name' => $cat->name (),
  131. 'color' => $cat->color ()
  132. );
  133. $cat->_id ($catDAO->addCategory ($values));
  134. }
  135. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  136. }
  137. } else {
  138. // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
  139. $feeds[] = getFeed ($outline, $defCat->id());
  140. }
  141. }
  142. return array ($categories, $feeds);
  143. }
  144. /**
  145. * import all feeds of a given outline tag
  146. */
  147. function getFeedsOutline ($outline, $cat_id) {
  148. $feeds = array ();
  149. foreach ($outline->children () as $child) {
  150. if (isset ($child['xmlUrl'])) {
  151. $feeds[] = getFeed ($child, $cat_id);
  152. } else {
  153. $feeds = array_merge(
  154. $feeds,
  155. getFeedsOutline ($child, $cat_id)
  156. );
  157. }
  158. }
  159. return $feeds;
  160. }
  161. function getFeed ($outline, $cat_id) {
  162. $url = (string) $outline['xmlUrl'];
  163. $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
  164. $title = '';
  165. if (isset ($outline['text'])) {
  166. $title = (string) $outline['text'];
  167. } elseif (isset ($outline['title'])) {
  168. $title = (string) $outline['title'];
  169. }
  170. $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
  171. $feed = new Feed ($url);
  172. $feed->_category ($cat_id);
  173. $feed->_name ($title);
  174. if (isset($outline['htmlUrl'])) {
  175. $feed->_website((string)$outline['htmlUrl']);
  176. }
  177. return $feed;
  178. }
  179. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  180. function get_content_by_parsing ($url, $path) {
  181. require_once (LIB_PATH . '/lib_phpQuery.php');
  182. $html = file_get_contents ($url);
  183. if ($html) {
  184. $doc = phpQuery::newDocument ($html);
  185. $content = $doc->find ($path);
  186. $content->find ('*')->removeAttr ('style')
  187. ->removeAttr ('id')
  188. ->removeAttr ('class')
  189. ->removeAttr ('onload')
  190. ->removeAttr ('target');
  191. $content->removeAttr ('style')
  192. ->removeAttr ('id')
  193. ->removeAttr ('class')
  194. ->removeAttr ('onload')
  195. ->removeAttr ('target');
  196. return $content->__toString ();
  197. } else {
  198. throw new Exception ();
  199. }
  200. }
  201. /**
  202. * Add support of image lazy loading
  203. * Move content from src attribute to data-original
  204. * @param content is the text we want to parse
  205. */
  206. function lazyimg($content) {
  207. return preg_replace(
  208. '/<img([^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  209. '<img$1src="' . Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  210. $content
  211. );
  212. }
  213. function invalidateHttpCache() {
  214. file_put_contents(DATA_PATH . '/touch.txt', microtime(true));
  215. }