lib_rss.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. <?php
  2. if (version_compare(PHP_VERSION, FRESHRSS_MIN_PHP_VERSION, '<')) {
  3. die(sprintf('FreshRSS error: FreshRSS requires PHP %s+!', FRESHRSS_MIN_PHP_VERSION));
  4. }
  5. if (!function_exists('mb_strcut')) {
  6. function mb_strcut($str, $start, $length = null, $encoding = 'UTF-8') {
  7. return substr($str, $start, $length);
  8. }
  9. }
  10. if (!function_exists('str_starts_with')) {
  11. /** Polyfill for PHP <8.0 */
  12. function str_starts_with(string $haystack, string $needle): bool {
  13. return strncmp($haystack, $needle, strlen($needle)) === 0;
  14. }
  15. }
  16. // @phpstan-ignore-next-line
  17. if (COPY_SYSLOG_TO_STDERR) {
  18. openlog('FreshRSS', LOG_CONS | LOG_ODELAY | LOG_PID | LOG_PERROR, LOG_USER);
  19. } else {
  20. openlog('FreshRSS', LOG_CONS | LOG_ODELAY | LOG_PID, LOG_USER);
  21. }
  22. /**
  23. * Build a directory path by concatenating a list of directory names.
  24. *
  25. * @param string ...$path_parts a list of directory names
  26. * @return string corresponding to the final pathname
  27. */
  28. function join_path(...$path_parts): string {
  29. return join(DIRECTORY_SEPARATOR, $path_parts);
  30. }
  31. //<Auto-loading>
  32. function classAutoloader($class) {
  33. if (strpos($class, 'FreshRSS') === 0) {
  34. $components = explode('_', $class);
  35. switch (count($components)) {
  36. case 1:
  37. include(APP_PATH . '/' . $components[0] . '.php');
  38. return;
  39. case 2:
  40. include(APP_PATH . '/Models/' . $components[1] . '.php');
  41. return;
  42. case 3: //Controllers, Exceptions
  43. include(APP_PATH . '/' . $components[2] . 's/' . $components[1] . $components[2] . '.php');
  44. return;
  45. }
  46. } elseif (strpos($class, 'Minz') === 0) {
  47. include(LIB_PATH . '/' . str_replace('_', '/', $class) . '.php');
  48. } elseif (strpos($class, 'SimplePie') === 0) {
  49. include(LIB_PATH . '/SimplePie/' . str_replace('_', '/', $class) . '.php');
  50. } elseif (str_starts_with($class, 'Gt\\CssXPath\\')) {
  51. $prefix = 'Gt\\CssXPath\\';
  52. $base_dir = LIB_PATH . '/phpgt/cssxpath/src/';
  53. $relative_class_name = substr($class, strlen($prefix));
  54. require $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  55. } elseif (str_starts_with($class, 'marienfressinaud\\LibOpml\\')) {
  56. $prefix = 'marienfressinaud\\LibOpml\\';
  57. $base_dir = LIB_PATH . '/marienfressinaud/lib_opml/src/LibOpml/';
  58. $relative_class_name = substr($class, strlen($prefix));
  59. require $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  60. } elseif (str_starts_with($class, 'PHPMailer\\PHPMailer\\')) {
  61. $prefix = 'PHPMailer\\PHPMailer\\';
  62. $base_dir = LIB_PATH . '/phpmailer/phpmailer/src/';
  63. $relative_class_name = substr($class, strlen($prefix));
  64. require $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  65. }
  66. }
  67. spl_autoload_register('classAutoloader');
  68. //</Auto-loading>
  69. /**
  70. * @param string $url
  71. * @return string
  72. */
  73. function idn_to_puny($url) {
  74. if (function_exists('idn_to_ascii')) {
  75. $idn = parse_url($url, PHP_URL_HOST);
  76. if ($idn != '') {
  77. // https://wiki.php.net/rfc/deprecate-and-remove-intl_idna_variant_2003
  78. if (defined('INTL_IDNA_VARIANT_UTS46')) {
  79. $puny = idn_to_ascii($idn, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
  80. } elseif (defined('INTL_IDNA_VARIANT_2003')) {
  81. $puny = idn_to_ascii($idn, IDNA_DEFAULT, INTL_IDNA_VARIANT_2003);
  82. } else {
  83. $puny = idn_to_ascii($idn);
  84. }
  85. $pos = strpos($url, $idn);
  86. if ($puny != '' && $pos !== false) {
  87. $url = substr_replace($url, $puny, $pos, strlen($idn));
  88. }
  89. }
  90. }
  91. return $url;
  92. }
  93. /**
  94. * @param string $url
  95. * @param bool $fixScheme
  96. * @return string|false
  97. */
  98. function checkUrl($url, $fixScheme = true) {
  99. $url = trim($url);
  100. if ($url == '') {
  101. return '';
  102. }
  103. if ($fixScheme && !preg_match('#^https?://#i', $url)) {
  104. $url = 'https://' . ltrim($url, '/');
  105. }
  106. $url = idn_to_puny($url); //PHP bug #53474 IDN
  107. $urlRelaxed = str_replace('_', 'z', $url); //PHP discussion #64948 Underscore
  108. if (filter_var($urlRelaxed, FILTER_VALIDATE_URL)) {
  109. return $url;
  110. } else {
  111. return false;
  112. }
  113. }
  114. /**
  115. * @param string $text
  116. * @return string
  117. */
  118. function safe_ascii($text) {
  119. return filter_var($text, FILTER_DEFAULT, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
  120. }
  121. if (function_exists('mb_convert_encoding')) {
  122. /**
  123. * @param string $text
  124. * @return string
  125. */
  126. function safe_utf8($text) {
  127. return mb_convert_encoding($text, 'UTF-8', 'UTF-8');
  128. }
  129. } elseif (function_exists('iconv')) {
  130. /**
  131. * @param string $text
  132. * @return string
  133. */
  134. function safe_utf8($text) {
  135. return iconv('UTF-8', 'UTF-8//IGNORE', $text);
  136. }
  137. } else {
  138. /**
  139. * @param string $text
  140. * @return string
  141. */
  142. function safe_utf8($text) {
  143. return $text;
  144. }
  145. }
  146. /**
  147. * @param string $text
  148. * @param bool $extended
  149. * @return string
  150. */
  151. function escapeToUnicodeAlternative($text, $extended = true) {
  152. $text = htmlspecialchars_decode($text, ENT_QUOTES);
  153. //Problematic characters
  154. $problem = array('&', '<', '>');
  155. //Use their fullwidth Unicode form instead:
  156. $replace = array('&', '<', '>');
  157. // https://raw.githubusercontent.com/mihaip/google-reader-api/master/wiki/StreamId.wiki
  158. if ($extended) {
  159. $problem += array("'", '"', '^', '?', '\\', '/', ',', ';');
  160. $replace += array("’", '"', '^', '?', '\', '/', ',', ';');
  161. }
  162. return trim(str_replace($problem, $replace, $text));
  163. }
  164. function format_number($n, $precision = 0) {
  165. // number_format does not seem to be Unicode-compatible
  166. return str_replace(' ', ' ', // Thin non-breaking space
  167. number_format($n, $precision, '.', ' ')
  168. );
  169. }
  170. function format_bytes($bytes, $precision = 2, $system = 'IEC') {
  171. if ($system === 'IEC') {
  172. $base = 1024;
  173. $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB');
  174. } elseif ($system === 'SI') {
  175. $base = 1000;
  176. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  177. } else {
  178. return format_number($bytes, $precision);
  179. }
  180. $bytes = max(intval($bytes), 0);
  181. $pow = $bytes === 0 ? 0 : floor(log($bytes) / log($base));
  182. $pow = min($pow, count($units) - 1);
  183. $bytes /= pow($base, $pow);
  184. return format_number($bytes, $precision) . ' ' . $units[$pow];
  185. }
  186. function timestamptodate ($t, $hour = true) {
  187. $month = _t('gen.date.' . date('M', $t));
  188. if ($hour) {
  189. $date = _t('gen.date.format_date_hour', $month);
  190. } else {
  191. $date = _t('gen.date.format_date', $month);
  192. }
  193. return @date ($date, $t);
  194. }
  195. /**
  196. * Decode HTML entities but preserve XML entities.
  197. * @param string|null $text
  198. */
  199. function html_only_entity_decode($text): string {
  200. static $htmlEntitiesOnly = null;
  201. if ($htmlEntitiesOnly === null) {
  202. $htmlEntitiesOnly = array_flip(array_diff(
  203. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  204. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  205. ));
  206. }
  207. return $text == '' ? '' : strtr($text, $htmlEntitiesOnly);
  208. }
  209. /**
  210. * Remove passwords in FreshRSS logs.
  211. * See also ../cli/sensitive-log.sh for Web server logs.
  212. * @param array<string,mixed>|string $log
  213. * @return array<string,mixed>|string
  214. */
  215. function sensitive_log($log) {
  216. if (is_array($log)) {
  217. foreach ($log as $k => $v) {
  218. if (in_array($k, ['api_key', 'Passwd', 'T'])) {
  219. $log[$k] = '██';
  220. } else {
  221. $log[$k] = sensitive_log($v);
  222. }
  223. }
  224. } elseif (is_string($log)) {
  225. $log = preg_replace([
  226. '/\b(auth=.*?\/)[^&]+/i',
  227. '/\b(Passwd=)[^&]+/i',
  228. '/\b(Authorization)[^&]+/i',
  229. ], '$1█', $log);
  230. }
  231. return $log;
  232. }
  233. /**
  234. * @param array<string,mixed> $attributes
  235. */
  236. function customSimplePie($attributes = array()): SimplePie {
  237. $limits = FreshRSS_Context::$system_conf->limits;
  238. $simplePie = new SimplePie();
  239. $simplePie->set_useragent(FRESHRSS_USERAGENT);
  240. $simplePie->set_syslog(FreshRSS_Context::$system_conf->simplepie_syslog_enabled);
  241. $simplePie->set_cache_name_function('sha1');
  242. $simplePie->set_cache_location(CACHE_PATH);
  243. $simplePie->set_cache_duration($limits['cache_duration']);
  244. $simplePie->enable_order_by_date(false);
  245. $feed_timeout = empty($attributes['timeout']) ? 0 : intval($attributes['timeout']);
  246. $simplePie->set_timeout($feed_timeout > 0 ? $feed_timeout : $limits['timeout']);
  247. $curl_options = FreshRSS_Context::$system_conf->curl_options;
  248. if (isset($attributes['ssl_verify'])) {
  249. $curl_options[CURLOPT_SSL_VERIFYHOST] = $attributes['ssl_verify'] ? 2 : 0;
  250. $curl_options[CURLOPT_SSL_VERIFYPEER] = $attributes['ssl_verify'] ? true : false;
  251. if (!$attributes['ssl_verify']) {
  252. $curl_options[CURLOPT_SSL_CIPHER_LIST] = 'DEFAULT@SECLEVEL=1';
  253. }
  254. }
  255. if (!empty($attributes['curl_params']) && is_array($attributes['curl_params'])) {
  256. foreach ($attributes['curl_params'] as $co => $v) {
  257. $curl_options[$co] = $v;
  258. }
  259. }
  260. $simplePie->set_curl_options($curl_options);
  261. $simplePie->strip_comments(true);
  262. $simplePie->strip_htmltags(array(
  263. 'base', 'blink', 'body', 'doctype', 'embed',
  264. 'font', 'form', 'frame', 'frameset', 'html',
  265. 'link', 'input', 'marquee', 'meta', 'noscript',
  266. 'object', 'param', 'plaintext', 'script', 'style',
  267. 'svg', //TODO: Support SVG after sanitizing and URL rewriting of xlink:href
  268. ));
  269. $simplePie->rename_attributes(array('id', 'class'));
  270. $simplePie->strip_attributes(array_merge($simplePie->strip_attributes, array(
  271. 'autoplay', 'class', 'onload', 'onunload', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup',
  272. 'onmouseover', 'onmousemove', 'onmouseout', 'onfocus', 'onblur',
  273. 'onkeypress', 'onkeydown', 'onkeyup', 'onselect', 'onchange', 'seamless', 'sizes', 'srcset')));
  274. $simplePie->add_attributes(array(
  275. 'audio' => array('controls' => 'controls', 'preload' => 'none'),
  276. 'iframe' => array('sandbox' => 'allow-scripts allow-same-origin'),
  277. 'video' => array('controls' => 'controls', 'preload' => 'none'),
  278. ));
  279. $simplePie->set_url_replacements(array(
  280. 'a' => 'href',
  281. 'area' => 'href',
  282. 'audio' => 'src',
  283. 'blockquote' => 'cite',
  284. 'del' => 'cite',
  285. 'form' => 'action',
  286. 'iframe' => 'src',
  287. 'img' => array(
  288. 'longdesc',
  289. 'src'
  290. ),
  291. 'input' => 'src',
  292. 'ins' => 'cite',
  293. 'q' => 'cite',
  294. 'source' => 'src',
  295. 'track' => 'src',
  296. 'video' => array(
  297. 'poster',
  298. 'src',
  299. ),
  300. ));
  301. $https_domains = array();
  302. $force = @file(FRESHRSS_PATH . '/force-https.default.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  303. if (is_array($force)) {
  304. $https_domains = array_merge($https_domains, $force);
  305. }
  306. $force = @file(DATA_PATH . '/force-https.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  307. if (is_array($force)) {
  308. $https_domains = array_merge($https_domains, $force);
  309. }
  310. $simplePie->set_https_domains($https_domains);
  311. return $simplePie;
  312. }
  313. /**
  314. * @param int|false $maxLength
  315. */
  316. function sanitizeHTML($data, string $base = '', $maxLength = false) {
  317. if (!is_string($data) || ($maxLength !== false && $maxLength <= 0)) {
  318. return '';
  319. }
  320. if ($maxLength !== false) {
  321. $data = mb_strcut($data, 0, $maxLength, 'UTF-8');
  322. }
  323. static $simplePie = null;
  324. if ($simplePie == null) {
  325. $simplePie = customSimplePie();
  326. $simplePie->init();
  327. }
  328. $result = html_only_entity_decode($simplePie->sanitize->sanitize($data, SIMPLEPIE_CONSTRUCT_HTML, $base));
  329. if ($maxLength !== false && strlen($result) > $maxLength) {
  330. //Sanitizing has made the result too long so try again shorter
  331. $data = mb_strcut($result, 0, (2 * $maxLength) - strlen($result) - 2, 'UTF-8');
  332. return sanitizeHTML($data, $base, $maxLength);
  333. }
  334. return $result;
  335. }
  336. function cleanCache(int $hours = 720) {
  337. // N.B.: GLOB_BRACE is not available on all platforms
  338. $files = array_merge(glob(CACHE_PATH . '/*.html', GLOB_NOSORT), glob(CACHE_PATH . '/*.spc', GLOB_NOSORT));
  339. foreach ($files as $file) {
  340. if (substr($file, -10) === 'index.html') {
  341. continue;
  342. }
  343. $cacheMtime = @filemtime($file);
  344. if ($cacheMtime !== false && $cacheMtime < time() - (3600 * $hours)) {
  345. unlink($file);
  346. }
  347. }
  348. }
  349. /**
  350. * Set an XML preamble to enforce the HTML content type charset received by HTTP.
  351. * @param string $html the row downloaded HTML content
  352. * @param string $contentType an HTTP Content-Type such as 'text/html; charset=utf-8'
  353. * @return string an HTML string with XML encoding information for DOMDocument::loadHTML()
  354. */
  355. function enforceHttpEncoding(string $html, string $contentType = ''): string {
  356. $httpCharset = preg_match('/\bcharset=([0-9a-z_-]{2,12})$/i', $contentType, $matches) === 1 ? $matches[1] : '';
  357. if ($httpCharset == '') {
  358. // No charset defined by HTTP, do nothing
  359. return $html;
  360. }
  361. $httpCharsetNormalized = SimplePie_Misc::encoding($httpCharset);
  362. if ($httpCharsetNormalized === 'windows-1252') {
  363. // Default charset for HTTP, do nothing
  364. return $html;
  365. }
  366. if (substr($html, 0, 3) === "\xEF\xBB\xBF" || // UTF-8 BOM
  367. substr($html, 0, 2) === "\xFF\xFE" || // UTF-16 Little Endian BOM
  368. substr($html, 0, 2) === "\xFE\xFF" || // UTF-16 Big Endian BOM
  369. substr($html, 0, 4) === "\xFF\xFE\x00\x00" || // UTF-32 Little Endian BOM
  370. substr($html, 0, 4) === "\x00\x00\xFE\xFF") { // UTF-32 Big Endian BOM
  371. // Existing byte order mark, do nothing
  372. return $html;
  373. }
  374. if (preg_match('/^<[?]xml[^>]+encoding\b/', substr($html, 0, 64))) {
  375. // Existing XML declaration, do nothing
  376. return $html;
  377. }
  378. return '<' . '?xml version="1.0" encoding="' . $httpCharsetNormalized . '" ?' . ">\n" . $html;
  379. }
  380. /**
  381. * @param string $type {html,opml}
  382. * @param array<string,mixed> $attributes
  383. */
  384. function httpGet(string $url, string $cachePath, string $type = 'html', array $attributes = []): string {
  385. $limits = FreshRSS_Context::$system_conf->limits;
  386. $feed_timeout = empty($attributes['timeout']) ? 0 : intval($attributes['timeout']);
  387. $cacheMtime = @filemtime($cachePath);
  388. if ($cacheMtime !== false && $cacheMtime > time() - intval($limits['cache_duration'])) {
  389. $body = @file_get_contents($cachePath);
  390. if ($body != '') {
  391. syslog(LOG_DEBUG, 'FreshRSS uses cache for ' . SimplePie_Misc::url_remove_credentials($url));
  392. return $body;
  393. }
  394. }
  395. if (mt_rand(0, 30) === 1) { // Remove old entries once in a while
  396. cleanCache();
  397. }
  398. if (FreshRSS_Context::$system_conf->simplepie_syslog_enabled) {
  399. syslog(LOG_INFO, 'FreshRSS GET ' . $type . ' ' . SimplePie_Misc::url_remove_credentials($url));
  400. }
  401. $accept = '*/*;q=0.8';
  402. switch ($type) {
  403. case 'opml':
  404. $accept = 'text/x-opml,text/xml;q=0.9,application/xml;q=0.9,*/*;q=0.8';
  405. break;
  406. case 'html':
  407. default:
  408. $accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
  409. break;
  410. }
  411. // TODO: Implement HTTP 1.1 conditional GET If-Modified-Since
  412. $ch = curl_init();
  413. curl_setopt_array($ch, [
  414. CURLOPT_URL => $url,
  415. CURLOPT_HTTPHEADER => array('Accept: ' . $accept),
  416. CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
  417. CURLOPT_CONNECTTIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  418. CURLOPT_TIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  419. CURLOPT_MAXREDIRS => 4,
  420. CURLOPT_RETURNTRANSFER => true,
  421. CURLOPT_FOLLOWLOCATION => true,
  422. CURLOPT_ENCODING => '', //Enable all encodings
  423. ]);
  424. curl_setopt_array($ch, FreshRSS_Context::$system_conf->curl_options);
  425. if (isset($attributes['curl_params']) && is_array($attributes['curl_params'])) {
  426. curl_setopt_array($ch, $attributes['curl_params']);
  427. }
  428. if (isset($attributes['ssl_verify'])) {
  429. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $attributes['ssl_verify'] ? 2 : 0);
  430. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $attributes['ssl_verify'] ? true : false);
  431. if (!$attributes['ssl_verify']) {
  432. curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'DEFAULT@SECLEVEL=1');
  433. }
  434. }
  435. $body = curl_exec($ch);
  436. $c_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  437. $c_content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); //TODO: Check if that may be null
  438. $c_error = curl_error($ch);
  439. curl_close($ch);
  440. if ($c_status != 200 || $c_error != '' || $body === false) {
  441. Minz_Log::warning('Error fetching content: HTTP code ' . $c_status . ': ' . $c_error . ' ' . $url);
  442. $body = '';
  443. // TODO: Implement HTTP 410 Gone
  444. }
  445. if ($body == false) {
  446. $body = '';
  447. } else {
  448. $body = enforceHttpEncoding($body, $c_content_type);
  449. }
  450. if (file_put_contents($cachePath, $body) === false) {
  451. Minz_Log::warning("Error saving cache $cachePath for $url");
  452. }
  453. return $body;
  454. }
  455. /**
  456. * Validate an email address, supports internationalized addresses.
  457. *
  458. * @param string $email The address to validate
  459. *
  460. * @return bool true if email is valid, else false
  461. */
  462. function validateEmailAddress($email) {
  463. $mailer = new PHPMailer\PHPMailer\PHPMailer();
  464. $mailer->CharSet = 'utf-8';
  465. $punyemail = $mailer->punyencodeAddress($email);
  466. return PHPMailer\PHPMailer\PHPMailer::validateAddress($punyemail, 'html5');
  467. }
  468. /**
  469. * Add support of image lazy loading
  470. * Move content from src attribute to data-original
  471. * @param string $content is the text we want to parse
  472. * @return string
  473. */
  474. function lazyimg($content) {
  475. return preg_replace([
  476. '/<((?:img|iframe)[^>]+?)src="([^"]+)"([^>]*)>/i',
  477. "/<((?:img|iframe)[^>]+?)src='([^']+)'([^>]*)>/i",
  478. ], [
  479. '<$1src="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  480. "<$1src='" . Minz_Url::display('/themes/icons/grey.gif') . "' data-original='$2'$3>",
  481. ],
  482. $content
  483. );
  484. }
  485. /**
  486. * @return string
  487. */
  488. function uTimeString() {
  489. $t = @gettimeofday();
  490. return $t['sec'] . str_pad('' . $t['usec'], 6, '0', STR_PAD_LEFT);
  491. }
  492. function invalidateHttpCache($username = '') {
  493. if (!FreshRSS_user_Controller::checkUsername($username)) {
  494. Minz_Session::_param('touch', uTimeString());
  495. $username = Minz_Session::param('currentUser', '_');
  496. }
  497. $ok = @touch(DATA_PATH . '/users/' . $username . '/' . LOG_FILENAME);
  498. //if (!$ok) {
  499. //TODO: Display notification error on front-end
  500. //}
  501. return $ok;
  502. }
  503. /**
  504. * @return array<string>
  505. */
  506. function listUsers() {
  507. $final_list = array();
  508. $base_path = join_path(DATA_PATH, 'users');
  509. $dir_list = array_values(array_diff(
  510. scandir($base_path),
  511. array('..', '.', '_')
  512. ));
  513. foreach ($dir_list as $file) {
  514. if ($file[0] !== '.' && is_dir(join_path($base_path, $file)) && file_exists(join_path($base_path, $file, 'config.php'))) {
  515. $final_list[] = $file;
  516. }
  517. }
  518. return $final_list;
  519. }
  520. /**
  521. * Return if the maximum number of registrations has been reached.
  522. *
  523. * Note a max_regstrations of 0 means there is no limit.
  524. *
  525. * @return boolean true if number of users >= max registrations, false else.
  526. */
  527. function max_registrations_reached() {
  528. $limit_registrations = FreshRSS_Context::$system_conf->limits['max_registrations'];
  529. $number_accounts = count(listUsers());
  530. return $limit_registrations > 0 && $number_accounts >= $limit_registrations;
  531. }
  532. /**
  533. * Register and return the configuration for a given user.
  534. *
  535. * Note this function has been created to generate temporary configuration
  536. * objects. If you need a long-time configuration, please don't use this function.
  537. *
  538. * @param string $username the name of the user of which we want the configuration.
  539. * @return FreshRSS_UserConfiguration|null object, or null if the configuration cannot be loaded.
  540. */
  541. function get_user_configuration($username) {
  542. if (!FreshRSS_user_Controller::checkUsername($username)) {
  543. return null;
  544. }
  545. $namespace = 'user_' . $username;
  546. try {
  547. Minz_Configuration::register($namespace,
  548. USERS_PATH . '/' . $username . '/config.php',
  549. FRESHRSS_PATH . '/config-user.default.php');
  550. } catch (Minz_ConfigurationNamespaceException $e) {
  551. // namespace already exists, do nothing.
  552. Minz_Log::warning($e->getMessage(), ADMIN_LOG);
  553. } catch (Minz_FileNotExistException $e) {
  554. Minz_Log::warning($e->getMessage(), ADMIN_LOG);
  555. return null;
  556. }
  557. /**
  558. * @var FreshRSS_UserConfiguration $user_conf
  559. */
  560. $user_conf = Minz_Configuration::get($namespace);
  561. return $user_conf;
  562. }
  563. /**
  564. * Converts an IP (v4 or v6) to a binary representation using inet_pton
  565. *
  566. * @param string $ip the IP to convert
  567. * @return string a binary representation of the specified IP
  568. */
  569. function ipToBits(string $ip): string {
  570. $binaryip = '';
  571. foreach (str_split(inet_pton($ip)) as $char) {
  572. $binaryip .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT);
  573. }
  574. return $binaryip;
  575. }
  576. /**
  577. * Check if an ip belongs to the provided range (in CIDR format)
  578. *
  579. * @param string $ip the IP that we want to verify (ex: 192.168.16.1)
  580. * @param string $range the range to check against (ex: 192.168.16.0/24)
  581. * @return boolean true if the IP is in the range, otherwise false
  582. */
  583. function checkCIDR(string $ip, string $range): bool {
  584. $binary_ip = ipToBits($ip);
  585. list($subnet, $mask_bits) = explode('/', $range);
  586. $mask_bits = intval($mask_bits);
  587. $binary_subnet = ipToBits($subnet);
  588. $ip_net_bits = substr($binary_ip, 0, $mask_bits);
  589. $subnet_bits = substr($binary_subnet, 0, $mask_bits);
  590. return $ip_net_bits === $subnet_bits;
  591. }
  592. /**
  593. * Check if the client is allowed to send unsafe headers
  594. * This uses the REMOTE_ADDR header to determine the sender's IP
  595. * and the configuration option "trusted_sources" to get an array of the authorized ranges
  596. *
  597. * @return boolean, true if the sender's IP is in one of the ranges defined in the configuration, else false
  598. */
  599. function checkTrustedIP(): bool {
  600. if (!empty($_SERVER['REMOTE_ADDR'])) {
  601. foreach (FreshRSS_Context::$system_conf->trusted_sources as $cidr) {
  602. if (checkCIDR($_SERVER['REMOTE_ADDR'], $cidr)) {
  603. return true;
  604. }
  605. }
  606. }
  607. return false;
  608. }
  609. /**
  610. * @return string
  611. */
  612. function httpAuthUser() {
  613. if (!empty($_SERVER['REMOTE_USER'])) {
  614. return $_SERVER['REMOTE_USER'];
  615. } elseif (!empty($_SERVER['HTTP_REMOTE_USER']) && checkTrustedIP()) {
  616. return $_SERVER['HTTP_REMOTE_USER'];
  617. } elseif (!empty($_SERVER['REDIRECT_REMOTE_USER'])) {
  618. return $_SERVER['REDIRECT_REMOTE_USER'];
  619. } elseif (!empty($_SERVER['HTTP_X_WEBAUTH_USER']) && checkTrustedIP()) {
  620. return $_SERVER['HTTP_X_WEBAUTH_USER'];
  621. }
  622. return '';
  623. }
  624. /**
  625. * @return bool
  626. */
  627. function cryptAvailable() {
  628. try {
  629. $hash = '$2y$04$usesomesillystringfore7hnbRJHxXVLeakoG8K30oukPsA.ztMG';
  630. return $hash === @crypt('password', $hash);
  631. } catch (Exception $e) {
  632. Minz_Log::warning($e->getMessage());
  633. }
  634. return false;
  635. }
  636. /**
  637. * Check PHP and its extensions are well-installed.
  638. *
  639. * @return array<string,bool> of tested values.
  640. */
  641. function check_install_php() {
  642. $pdo_mysql = extension_loaded('pdo_mysql');
  643. $pdo_pgsql = extension_loaded('pdo_pgsql');
  644. $pdo_sqlite = extension_loaded('pdo_sqlite');
  645. return array(
  646. 'php' => version_compare(PHP_VERSION, FRESHRSS_MIN_PHP_VERSION) >= 0,
  647. 'curl' => extension_loaded('curl'),
  648. 'pdo' => $pdo_mysql || $pdo_sqlite || $pdo_pgsql,
  649. 'pcre' => extension_loaded('pcre'),
  650. 'ctype' => extension_loaded('ctype'),
  651. 'fileinfo' => extension_loaded('fileinfo'),
  652. 'dom' => class_exists('DOMDocument'),
  653. 'json' => extension_loaded('json'),
  654. 'mbstring' => extension_loaded('mbstring'),
  655. 'zip' => extension_loaded('zip'),
  656. );
  657. }
  658. /**
  659. * Check different data files and directories exist.
  660. *
  661. * @return array<string,bool> of tested values.
  662. */
  663. function check_install_files() {
  664. return array(
  665. // @phpstan-ignore-next-line
  666. 'data' => DATA_PATH && touch(DATA_PATH . '/index.html'), // is_writable() is not reliable for a folder on NFS
  667. // @phpstan-ignore-next-line
  668. 'cache' => CACHE_PATH && touch(CACHE_PATH . '/index.html'),
  669. // @phpstan-ignore-next-line
  670. 'users' => USERS_PATH && touch(USERS_PATH . '/index.html'),
  671. 'favicons' => touch(DATA_PATH . '/favicons/index.html'),
  672. 'tokens' => touch(DATA_PATH . '/tokens/index.html'),
  673. );
  674. }
  675. /**
  676. * Check database is well-installed.
  677. *
  678. * @return array<string,bool> of tested values.
  679. */
  680. function check_install_database() {
  681. $status = array(
  682. 'connection' => true,
  683. 'tables' => false,
  684. 'categories' => false,
  685. 'feeds' => false,
  686. 'entries' => false,
  687. 'entrytmp' => false,
  688. 'tag' => false,
  689. 'entrytag' => false,
  690. );
  691. try {
  692. $dbDAO = FreshRSS_Factory::createDatabaseDAO();
  693. $status['tables'] = $dbDAO->tablesAreCorrect();
  694. $status['categories'] = $dbDAO->categoryIsCorrect();
  695. $status['feeds'] = $dbDAO->feedIsCorrect();
  696. $status['entries'] = $dbDAO->entryIsCorrect();
  697. $status['entrytmp'] = $dbDAO->entrytmpIsCorrect();
  698. $status['tag'] = $dbDAO->tagIsCorrect();
  699. $status['entrytag'] = $dbDAO->entrytagIsCorrect();
  700. } catch(Minz_PDOConnectionException $e) {
  701. $status['connection'] = false;
  702. }
  703. return $status;
  704. }
  705. /**
  706. * Remove a directory recursively.
  707. *
  708. * From http://php.net/rmdir#110489
  709. *
  710. * @param string $dir the directory to remove
  711. */
  712. function recursive_unlink($dir) {
  713. if (!is_dir($dir)) {
  714. return true;
  715. }
  716. $files = array_diff(scandir($dir), array('.', '..'));
  717. foreach ($files as $filename) {
  718. $filename = $dir . '/' . $filename;
  719. if (is_dir($filename)) {
  720. @chmod($filename, 0777);
  721. recursive_unlink($filename);
  722. } else {
  723. unlink($filename);
  724. }
  725. }
  726. return rmdir($dir);
  727. }
  728. /**
  729. * Remove queries where $get is appearing.
  730. * @param string $get the get attribute which should be removed.
  731. * @param array<int,array<string,string>> $queries an array of queries.
  732. * @return array<int,array<string,string>> without queries where $get is appearing.
  733. */
  734. function remove_query_by_get($get, $queries) {
  735. $final_queries = array();
  736. foreach ($queries as $key => $query) {
  737. if (empty($query['get']) || $query['get'] !== $get) {
  738. $final_queries[$key] = $query;
  739. }
  740. }
  741. return $final_queries;
  742. }
  743. function _i(string $icon, int $type = FreshRSS_Themes::ICON_DEFAULT): string {
  744. return FreshRSS_Themes::icon($icon, $type);
  745. }
  746. const SHORTCUT_KEYS = [
  747. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  748. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  749. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  750. 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12',
  751. 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'Backspace', 'Delete',
  752. 'End', 'Enter', 'Escape', 'Home', 'Insert', 'PageDown', 'PageUp', 'Space', 'Tab',
  753. ];
  754. function getNonStandardShortcuts($shortcuts) {
  755. $standard = strtolower(implode(' ', SHORTCUT_KEYS));
  756. $nonStandard = array_filter($shortcuts, function ($shortcut) use ($standard) {
  757. $shortcut = trim($shortcut);
  758. return $shortcut !== '' & stripos($standard, $shortcut) === false;
  759. });
  760. return $nonStandard;
  761. }
  762. function errorMessageInfo($errorTitle, $error = '') {
  763. $errorTitle = htmlspecialchars($errorTitle, ENT_NOQUOTES, 'UTF-8');
  764. $message = '';
  765. $details = '';
  766. // Prevent empty tags by checking if error isn not empty first
  767. if ($error) {
  768. $error = htmlspecialchars($error, ENT_NOQUOTES, 'UTF-8') . "\n";
  769. // First line is the main message, other lines are the details
  770. list($message, $details) = explode("\n", $error, 2);
  771. $message = "<h2>{$message}</h2>";
  772. $details = "<pre>{$details}</pre>";
  773. }
  774. header("Content-Security-Policy: default-src 'self'");
  775. return <<<MSG
  776. <!DOCTYPE html><html><header><title>HTTP 500: {$errorTitle}</title></header><body>
  777. <h1>HTTP 500: {$errorTitle}</h1>
  778. {$message}
  779. {$details}
  780. <hr />
  781. <small>For help see the documentation: <a href="https://freshrss.github.io/FreshRSS/en/admins/logs_and_errors.html" target="_blank">
  782. https://freshrss.github.io/FreshRSS/en/admins/logs_and_errors.html</a></small>
  783. </body></html>
  784. MSG;
  785. }