lib_rss.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. function formatNumber($n, $precision = 0) {
  55. return str_replace(' ', ' ', //Espace insécable //TODO: remplacer par une espace _fine_ insécable
  56. number_format($n, $precision, '.', ' ')); //number_format does not seem to be Unicode-compatible
  57. }
  58. function formatBytes($bytes, $precision = 2, $system = 'IEC') {
  59. if ($system === 'IEC') {
  60. $base = 1024;
  61. $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB');
  62. } elseif ($system === 'SI') {
  63. $base = 1000;
  64. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  65. }
  66. $bytes = max(intval($bytes), 0);
  67. $pow = $bytes === 0 ? 0 : floor(log($bytes) / log($base));
  68. $pow = min($pow, count($units) - 1);
  69. $bytes /= pow($base, $pow);
  70. return formatNumber($bytes, $precision) . ' ' . $units[$pow];
  71. }
  72. function timestamptodate ($t, $hour = true) {
  73. $month = Minz_Translate::t (date('M', $t));
  74. if ($hour) {
  75. $date = Minz_Translate::t ('format_date_hour', $month);
  76. } else {
  77. $date = Minz_Translate::t ('format_date', $month);
  78. }
  79. return @date ($date, $t);
  80. }
  81. function html_only_entity_decode($text) {
  82. static $htmlEntitiesOnly = null;
  83. if ($htmlEntitiesOnly === null) {
  84. if (version_compare(PHP_VERSION, '5.3.4') >= 0) {
  85. $htmlEntitiesOnly = array_flip(array_diff(
  86. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  87. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  88. ));
  89. } else {
  90. $htmlEntitiesOnly = array_map('utf8_encode', array_flip(array_diff(
  91. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES), //Decode HTML entities
  92. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES) //Preserve XML entities
  93. )));
  94. }
  95. }
  96. return strtr($text, $htmlEntitiesOnly);
  97. }
  98. function customSimplePie() {
  99. $simplePie = new SimplePie();
  100. $simplePie->set_useragent(Minz_Translate::t('freshrss') . '/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ') ' . SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION);
  101. $simplePie->set_cache_location(CACHE_PATH);
  102. $simplePie->set_cache_duration(800);
  103. $simplePie->strip_htmltags(array(
  104. 'base', 'blink', 'body', 'doctype', 'embed',
  105. 'font', 'form', 'frame', 'frameset', 'html',
  106. 'link', 'input', 'marquee', 'meta', 'noscript',
  107. 'object', 'param', 'plaintext', 'script', 'style',
  108. ));
  109. $simplePie->strip_attributes(array_merge($simplePie->strip_attributes, array(
  110. 'autoplay', 'onload', 'onunload', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup',
  111. 'onmouseover', 'onmousemove', 'onmouseout', 'onfocus', 'onblur',
  112. 'onkeypress', 'onkeydown', 'onkeyup', 'onselect', 'onchange', 'seamless')));
  113. $simplePie->add_attributes(array(
  114. 'img' => array('lazyload' => '', 'postpone' => ''), //http://www.w3.org/TR/resource-priorities/
  115. 'audio' => array('lazyload' => '', 'postpone' => '', 'preload' => 'none'),
  116. 'iframe' => array('lazyload' => '', 'postpone' => '', 'sandbox' => 'allow-scripts allow-same-origin'),
  117. 'video' => array('lazyload' => '', 'postpone' => '', 'preload' => 'none'),
  118. ));
  119. $simplePie->set_url_replacements(array(
  120. 'a' => 'href',
  121. 'area' => 'href',
  122. 'audio' => 'src',
  123. 'blockquote' => 'cite',
  124. 'del' => 'cite',
  125. 'form' => 'action',
  126. 'iframe' => 'src',
  127. 'img' => array(
  128. 'longdesc',
  129. 'src'
  130. ),
  131. 'input' => 'src',
  132. 'ins' => 'cite',
  133. 'q' => 'cite',
  134. 'source' => 'src',
  135. 'track' => 'src',
  136. 'video' => array(
  137. 'poster',
  138. 'src',
  139. ),
  140. ));
  141. return $simplePie;
  142. }
  143. function sanitizeHTML($data, $base = '') {
  144. static $simplePie = null;
  145. if ($simplePie == null) {
  146. $simplePie = customSimplePie();
  147. $simplePie->init();
  148. }
  149. return html_only_entity_decode($simplePie->sanitize->sanitize($data, SIMPLEPIE_CONSTRUCT_HTML, $base));
  150. }
  151. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  152. function get_content_by_parsing ($url, $path) {
  153. require_once (LIB_PATH . '/lib_phpQuery.php');
  154. syslog(LOG_INFO, 'FreshRSS GET ' . $url);
  155. $html = file_get_contents ($url);
  156. if ($html) {
  157. $doc = phpQuery::newDocument ($html);
  158. $content = $doc->find ($path);
  159. return sanitizeHTML($content->__toString(), $url);
  160. } else {
  161. throw new Exception ();
  162. }
  163. }
  164. /**
  165. * Add support of image lazy loading
  166. * Move content from src attribute to data-original
  167. * @param content is the text we want to parse
  168. */
  169. function lazyimg($content) {
  170. return preg_replace(
  171. '/<((?:img|iframe)[^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  172. '<$1src="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  173. $content
  174. );
  175. }
  176. function uTimeString() {
  177. $t = @gettimeofday();
  178. return $t['sec'] . str_pad($t['usec'], 6, '0');
  179. }
  180. function uSecString() {
  181. $t = @gettimeofday();
  182. return str_pad($t['usec'], 6, '0');
  183. }
  184. function invalidateHttpCache() {
  185. Minz_Session::_param('touch', uTimeString());
  186. return touch(LOG_PATH . '/' . Minz_Session::param('currentUser', '_') . '.log');
  187. }
  188. function usernameFromPath($userPath) {
  189. if (preg_match('%/([A-Za-z0-9]{1,16})_user\.php$%', $userPath, $matches)) {
  190. return $matches[1];
  191. } else {
  192. return '';
  193. }
  194. }
  195. function listUsers() {
  196. return array_map('usernameFromPath', glob(DATA_PATH . '/*_user.php'));
  197. }
  198. function httpAuthUser() {
  199. return isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '';
  200. }
  201. function cryptAvailable() {
  202. if (version_compare(PHP_VERSION, '5.3.3', '>=')) {
  203. try {
  204. $hash = '$2y$04$usesomesillystringfore7hnbRJHxXVLeakoG8K30oukPsA.ztMG';
  205. return $hash === @crypt('password', $hash);
  206. } catch (Exception $e) {
  207. }
  208. }
  209. return false;
  210. }
  211. function html_chars_utf8($str) {
  212. return htmlspecialchars($str, ENT_COMPAT, 'UTF-8');
  213. }