lib_rss.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. //<Auto-loading>
  17. function classAutoloader($class) {
  18. if (strpos($class, 'FreshRSS') === 0) {
  19. $components = explode('_', $class);
  20. switch (count($components)) {
  21. case 1:
  22. include(APP_PATH . '/' . $components[0] . '.php');
  23. return;
  24. case 2:
  25. include(APP_PATH . '/Models/' . $components[1] . '.php');
  26. return;
  27. case 3: //Controllers, Exceptions
  28. include(APP_PATH . '/' . $components[2] . 's/' . $components[1] . $components[2] . '.php');
  29. return;
  30. }
  31. } elseif (strpos($class, 'Minz') === 0) {
  32. include(LIB_PATH . '/' . str_replace('_', '/', $class) . '.php');
  33. } elseif (strpos($class, 'SimplePie') === 0) {
  34. include(LIB_PATH . '/SimplePie/' . str_replace('_', '/', $class) . '.php');
  35. }
  36. }
  37. spl_autoload_register('classAutoloader');
  38. //</Auto-loading>
  39. function checkUrl($url) {
  40. if (empty ($url)) {
  41. return '';
  42. }
  43. if (!preg_match ('#^https?://#i', $url)) {
  44. $url = 'http://' . $url;
  45. }
  46. if (filter_var($url, FILTER_VALIDATE_URL) ||
  47. (version_compare(PHP_VERSION, '5.3.3', '<') && (strpos($url, '-') > 0) && //PHP bug #51192
  48. ($url === filter_var($url, FILTER_SANITIZE_URL)))) {
  49. return $url;
  50. } else {
  51. return false;
  52. }
  53. }
  54. // vérifie qu'on est connecté
  55. function is_logged () {
  56. return Minz_Session::param ('mail') != false;
  57. }
  58. // vérifie que le système d'authentification est configuré
  59. function login_is_conf ($conf) {
  60. return $conf->mailLogin () != false;
  61. }
  62. // tiré de Shaarli de Seb Sauvage //Format RFC 4648 base64url
  63. function small_hash ($txt) {
  64. $t = rtrim (base64_encode (hash ('crc32', $txt, true)), '=');
  65. return strtr ($t, '+/', '-_');
  66. }
  67. function formatBytes($bytes, $precision = 2, $system = 'IEC') {
  68. if ($system === 'IEC') {
  69. $base = 1024;
  70. $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB');
  71. } elseif ($system === 'SI') {
  72. $base = 1000;
  73. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  74. }
  75. $bytes = max(intval($bytes), 0);
  76. $pow = $bytes === 0 ? 0 : floor(log($bytes) / log($base));
  77. $pow = min($pow, count($units) - 1);
  78. $bytes /= pow($base, $pow);
  79. return round($bytes, $precision) . ' ' . $units[$pow];
  80. }
  81. function timestamptodate ($t, $hour = true) {
  82. $month = Minz_Translate::t (date('M', $t));
  83. if ($hour) {
  84. $date = Minz_Translate::t ('format_date_hour', $month);
  85. } else {
  86. $date = Minz_Translate::t ('format_date', $month);
  87. }
  88. return @date ($date, $t);
  89. }
  90. function html_only_entity_decode($text) {
  91. static $htmlEntitiesOnly = null;
  92. if ($htmlEntitiesOnly === null) {
  93. $htmlEntitiesOnly = array_flip(array_diff(
  94. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  95. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  96. ));
  97. }
  98. return strtr($text, $htmlEntitiesOnly);
  99. }
  100. function sanitizeHTML($data) {
  101. static $simplePie = null;
  102. if ($simplePie == null) {
  103. $simplePie = new SimplePie();
  104. }
  105. return html_only_entity_decode($simplePie->sanitize->sanitize($data, SIMPLEPIE_CONSTRUCT_MAYBE_HTML));
  106. }
  107. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  108. function get_content_by_parsing ($url, $path) {
  109. require_once (LIB_PATH . '/lib_phpQuery.php');
  110. $html = file_get_contents ($url);
  111. if ($html) {
  112. $doc = phpQuery::newDocument ($html);
  113. $content = $doc->find ($path);
  114. $content->find ('*')->removeAttr ('style')
  115. ->removeAttr ('id')
  116. ->removeAttr ('class')
  117. ->removeAttr ('onload')
  118. ->removeAttr ('target');
  119. $content->removeAttr ('style')
  120. ->removeAttr ('id')
  121. ->removeAttr ('class')
  122. ->removeAttr ('onload')
  123. ->removeAttr ('target');
  124. return $content->__toString ();
  125. } else {
  126. throw new Exception ();
  127. }
  128. }
  129. /**
  130. * Add support of image lazy loading
  131. * Move content from src attribute to data-original
  132. * @param content is the text we want to parse
  133. */
  134. function lazyimg($content) {
  135. return preg_replace(
  136. '/<img([^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  137. '<img$1src="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  138. $content
  139. );
  140. }
  141. function lazyIframe($content) {
  142. return preg_replace(
  143. '/<iframe([^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  144. '<iframe$1src="about:blank" data-original="$2"$3>',
  145. $content
  146. );
  147. }
  148. function uTimeString() {
  149. $t = @gettimeofday();
  150. return $t['sec'] . str_pad($t['usec'], 6, '0');
  151. }
  152. function uSecString() {
  153. $t = @gettimeofday();
  154. return str_pad($t['usec'], 6, '0');
  155. }
  156. function invalidateHttpCache() {
  157. file_put_contents(DATA_PATH . '/touch.txt', uTimeString());
  158. }