lib_rss.php 6.5 KB

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