lib_rss.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. <?php
  2. declare(strict_types=1);
  3. if (!function_exists('mb_strcut')) {
  4. function mb_strcut(string $str, int $start, ?int $length = null, string $encoding = 'UTF-8'): string {
  5. return substr($str, $start, $length) ?: '';
  6. }
  7. }
  8. if (!function_exists('syslog')) {
  9. if (COPY_SYSLOG_TO_STDERR && !defined('STDERR')) {
  10. define('STDERR', fopen('php://stderr', 'w'));
  11. }
  12. function syslog(int $priority, string $message): bool {
  13. if (COPY_SYSLOG_TO_STDERR && defined('STDERR') && is_resource(STDERR)) {
  14. return fwrite(STDERR, $message . "\n") != false;
  15. }
  16. return false;
  17. }
  18. }
  19. if (function_exists('openlog')) {
  20. if (COPY_SYSLOG_TO_STDERR) {
  21. openlog('FreshRSS', LOG_CONS | LOG_ODELAY | LOG_PID | LOG_PERROR, LOG_USER);
  22. } else {
  23. openlog('FreshRSS', LOG_CONS | LOG_ODELAY | LOG_PID, LOG_USER);
  24. }
  25. }
  26. /**
  27. * Build a directory path by concatenating a list of directory names.
  28. *
  29. * @param string ...$path_parts a list of directory names
  30. * @return string corresponding to the final pathname
  31. */
  32. function join_path(...$path_parts): string {
  33. return join(DIRECTORY_SEPARATOR, $path_parts);
  34. }
  35. //<Auto-loading>
  36. function classAutoloader(string $class): void {
  37. if (str_starts_with($class, 'FreshRSS')) {
  38. $components = explode('_', $class);
  39. switch (count($components)) {
  40. case 1:
  41. include APP_PATH . '/' . $components[0] . '.php';
  42. return;
  43. case 2:
  44. include APP_PATH . '/Models/' . $components[1] . '.php';
  45. return;
  46. case 3: //Controllers, Exceptions
  47. include APP_PATH . '/' . $components[2] . 's/' . $components[1] . $components[2] . '.php';
  48. return;
  49. }
  50. } elseif (str_starts_with($class, 'Minz')) {
  51. include LIB_PATH . '/' . str_replace('_', '/', $class) . '.php';
  52. } elseif (str_starts_with($class, 'SimplePie\\')) {
  53. $prefix = 'SimplePie\\';
  54. $base_dir = LIB_PATH . '/simplepie/simplepie/src/';
  55. $relative_class_name = substr($class, strlen($prefix));
  56. include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  57. } elseif (str_starts_with($class, 'Gt\\CssXPath\\')) {
  58. $prefix = 'Gt\\CssXPath\\';
  59. $base_dir = LIB_PATH . '/phpgt/cssxpath/src/';
  60. $relative_class_name = substr($class, strlen($prefix));
  61. include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  62. } elseif (str_starts_with($class, 'marienfressinaud\\LibOpml\\')) {
  63. $prefix = 'marienfressinaud\\LibOpml\\';
  64. $base_dir = LIB_PATH . '/marienfressinaud/lib_opml/src/LibOpml/';
  65. $relative_class_name = substr($class, strlen($prefix));
  66. include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  67. } elseif (str_starts_with($class, 'PHPMailer\\PHPMailer\\')) {
  68. $prefix = 'PHPMailer\\PHPMailer\\';
  69. $base_dir = LIB_PATH . '/phpmailer/phpmailer/src/';
  70. $relative_class_name = substr($class, strlen($prefix));
  71. include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  72. }
  73. }
  74. spl_autoload_register('classAutoloader');
  75. //</Auto-loading>
  76. /**
  77. * @param array<mixed,mixed> $array
  78. * @phpstan-assert-if-true array<string,mixed> $array
  79. */
  80. function is_array_keys_string(array $array): bool {
  81. foreach ($array as $key => $value) {
  82. if (!is_string($key)) {
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88. /**
  89. * @param array<mixed,mixed> $array
  90. * @phpstan-assert-if-true array<mixed,string> $array
  91. */
  92. function is_array_values_string(array $array): bool {
  93. foreach ($array as $value) {
  94. if (!is_string($value)) {
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. /**
  101. * Memory efficient replacement of `echo json_encode(...)`
  102. * @param array<mixed>|mixed $json
  103. * @param int $optimisationDepth Number of levels for which to perform memory optimisation
  104. * before calling the faster native JSON serialisation.
  105. * Set to negative value for infinite depth.
  106. */
  107. function echoJson($json, int $optimisationDepth = -1): void {
  108. if ($optimisationDepth === 0 || !is_array($json)) {
  109. echo json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  110. return;
  111. }
  112. $first = true;
  113. if (array_is_list($json)) {
  114. echo '[';
  115. foreach ($json as $item) {
  116. if ($first) {
  117. $first = false;
  118. } else {
  119. echo ',';
  120. }
  121. echoJson($item, $optimisationDepth - 1);
  122. }
  123. echo ']';
  124. } else {
  125. echo '{';
  126. foreach ($json as $key => $value) {
  127. if ($first) {
  128. $first = false;
  129. } else {
  130. echo ',';
  131. }
  132. echo json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), ':';
  133. echoJson($value, $optimisationDepth - 1);
  134. }
  135. echo '}';
  136. }
  137. }
  138. function idn_to_puny(string $url): string {
  139. if (function_exists('idn_to_ascii')) {
  140. $idn = parse_url($url, PHP_URL_HOST);
  141. if (is_string($idn) && $idn != '') {
  142. $puny = idn_to_ascii($idn);
  143. $pos = strpos($url, $idn);
  144. if ($puny != false && $pos !== false) {
  145. $url = substr_replace($url, $puny, $pos, strlen($idn));
  146. }
  147. }
  148. }
  149. return $url;
  150. }
  151. function checkUrl(string $url, bool $fixScheme = true): string|false {
  152. $url = trim($url);
  153. if ($url == '') {
  154. return '';
  155. }
  156. if ($fixScheme && preg_match('#^https?://#i', $url) !== 1) {
  157. $url = 'https://' . ltrim($url, '/');
  158. }
  159. $url = idn_to_puny($url); // https://bugs.php.net/bug.php?id=53474
  160. $urlRelaxed = str_replace('_', 'z', $url); //PHP discussion #64948 Underscore
  161. if (is_string(filter_var($urlRelaxed, FILTER_VALIDATE_URL))) {
  162. return $url;
  163. } else {
  164. return false;
  165. }
  166. }
  167. function safe_ascii(?string $text): string {
  168. return $text === null ? '' : (filter_var($text, FILTER_DEFAULT, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH) ?: '');
  169. }
  170. if (function_exists('mb_convert_encoding')) {
  171. function safe_utf8(string $text): string {
  172. return mb_convert_encoding($text, 'UTF-8', 'UTF-8') ?: '';
  173. }
  174. } elseif (function_exists('iconv')) {
  175. function safe_utf8(string $text): string {
  176. return iconv('UTF-8', 'UTF-8//IGNORE', $text) ?: '';
  177. }
  178. } else {
  179. function safe_utf8(string $text): string {
  180. return $text;
  181. }
  182. }
  183. function escapeToUnicodeAlternative(string $text, bool $extended = true): string {
  184. $text = htmlspecialchars_decode($text, ENT_QUOTES);
  185. //Problematic characters
  186. $problem = ['&', '<', '>'];
  187. //Use their fullwidth Unicode form instead:
  188. $replace = ['&', '<', '>'];
  189. // https://raw.githubusercontent.com/mihaip/google-reader-api/master/wiki/StreamId.wiki
  190. if ($extended) {
  191. $problem += ["'", '"', '^', '?', '\\', '/', ',', ';'];
  192. $replace += ["’", '"', '^', '?', '\', '/', ',', ';'];
  193. }
  194. return trim(str_replace($problem, $replace, $text));
  195. }
  196. function format_number(int|float $n, int $precision = 0): string {
  197. // number_format does not seem to be Unicode-compatible
  198. return str_replace(' ', ' ', // Thin non-breaking space
  199. number_format((float)$n, $precision, '.', ' ')
  200. );
  201. }
  202. function format_bytes(int $bytes, int $precision = 2, string $system = 'IEC'): string {
  203. if ($system === 'IEC') {
  204. $base = 1024;
  205. $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
  206. } elseif ($system === 'SI') {
  207. $base = 1000;
  208. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  209. } else {
  210. return format_number($bytes, $precision);
  211. }
  212. $bytes = max(intval($bytes), 0);
  213. $pow = $bytes === 0 ? 0 : (int)floor(log($bytes) / log($base));
  214. $pow = min(max(0, $pow), count($units) - 1);
  215. $bytes /= pow($base, $pow);
  216. return format_number($bytes, $precision) . ' ' . $units[$pow];
  217. }
  218. function timestamptodate(int $t, bool $hour = true): string {
  219. $month = _t('gen.date.' . date('M', $t));
  220. if ($hour) {
  221. $date = _t('gen.date.format_date_hour', $month);
  222. } else {
  223. $date = _t('gen.date.format_date', $month);
  224. }
  225. return @date($date, $t) ?: '';
  226. }
  227. /**
  228. * Decode HTML entities but preserve XML entities.
  229. */
  230. function html_only_entity_decode(?string $text): string {
  231. /** @var array<string,string>|null $htmlEntitiesOnly */
  232. static $htmlEntitiesOnly = null;
  233. if ($htmlEntitiesOnly === null) {
  234. $htmlEntitiesOnly = array_flip(array_diff(
  235. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  236. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  237. ));
  238. }
  239. return $text == null ? '' : strtr($text, $htmlEntitiesOnly);
  240. }
  241. /**
  242. * Remove passwords in FreshRSS logs.
  243. * See also ../cli/sensitive-log.sh for Web server logs.
  244. * @param array<string,mixed>|string $log
  245. * @return array<string,mixed>|string
  246. */
  247. function sensitive_log(array|string $log): array|string {
  248. if (is_array($log)) {
  249. foreach ($log as $k => $v) {
  250. if (in_array($k, ['api_key', 'Passwd', 'T'], true)) {
  251. $log[$k] = '██';
  252. } elseif ((is_array($v) && is_array_keys_string($v)) || is_string($v)) {
  253. $log[$k] = sensitive_log($v);
  254. } else {
  255. return '';
  256. }
  257. }
  258. } elseif (is_string($log)) {
  259. $log = preg_replace([
  260. '/\b(auth=.*?\/)[^&]+/i',
  261. '/\b(Passwd=)[^&]+/i',
  262. '/\b(Authorization)[^&]+/i',
  263. ], '$1█', $log) ?? '';
  264. }
  265. return $log;
  266. }
  267. /**
  268. * @param array<mixed> $curl_params
  269. * @return array<mixed>
  270. */
  271. function sanitizeCurlParams(array $curl_params): array {
  272. $safe_params = [
  273. CURLOPT_COOKIE,
  274. CURLOPT_COOKIEFILE,
  275. CURLOPT_FOLLOWLOCATION,
  276. CURLOPT_HTTPHEADER,
  277. CURLOPT_MAXREDIRS,
  278. CURLOPT_POST,
  279. CURLOPT_POSTFIELDS,
  280. CURLOPT_PROXY,
  281. CURLOPT_PROXYTYPE,
  282. CURLOPT_USERAGENT,
  283. ];
  284. foreach ($curl_params as $k => $_) {
  285. if (!in_array($k, $safe_params, true)) {
  286. unset($curl_params[$k]);
  287. continue;
  288. }
  289. // Allow only an empty value just to enable the libcurl cookie engine
  290. if ($k === CURLOPT_COOKIEFILE) {
  291. $curl_params[$k] = '';
  292. }
  293. }
  294. return $curl_params;
  295. }
  296. /**
  297. * @param array<string,mixed> $attributes
  298. * @param array<int,mixed> $curl_options
  299. * @throws FreshRSS_Context_Exception
  300. */
  301. function customSimplePie(array $attributes = [], array $curl_options = []): \SimplePie\SimplePie {
  302. $limits = FreshRSS_Context::systemConf()->limits;
  303. $simplePie = new \SimplePie\SimplePie();
  304. if (FreshRSS_Context::systemConf()->simplepie_syslog_enabled) {
  305. $simplePie->get_registry()->register(\SimplePie\File::class, FreshRSS_SimplePieResponse::class);
  306. }
  307. $simplePie->set_useragent(FRESHRSS_USERAGENT);
  308. $simplePie->set_cache_name_function('sha1');
  309. $simplePie->set_cache_location(CACHE_PATH);
  310. $simplePie->set_cache_duration($limits['cache_duration'], $limits['cache_duration_min'], $limits['cache_duration_max']);
  311. $simplePie->enable_order_by_date(false);
  312. $feed_timeout = empty($attributes['timeout']) || !is_numeric($attributes['timeout']) ? 0 : (int)$attributes['timeout'];
  313. $simplePie->set_timeout($feed_timeout > 0 ? $feed_timeout : $limits['timeout']);
  314. $curl_options = array_replace(FreshRSS_Context::systemConf()->curl_options, $curl_options);
  315. if (isset($attributes['ssl_verify'])) {
  316. $curl_options[CURLOPT_SSL_VERIFYHOST] = empty($attributes['ssl_verify']) ? 0 : 2;
  317. $curl_options[CURLOPT_SSL_VERIFYPEER] = (bool)$attributes['ssl_verify'];
  318. if (empty($attributes['ssl_verify'])) {
  319. $curl_options[CURLOPT_SSL_CIPHER_LIST] = 'DEFAULT@SECLEVEL=1';
  320. }
  321. }
  322. $attributes['curl_params'] = sanitizeCurlParams(is_array($attributes['curl_params'] ?? null) ? $attributes['curl_params'] : []);
  323. if (!empty($attributes['curl_params']) && is_array($attributes['curl_params'])) {
  324. foreach ($attributes['curl_params'] as $co => $v) {
  325. if (is_int($co)) {
  326. $curl_options[$co] = $v;
  327. }
  328. }
  329. }
  330. if (!empty($curl_options[CURLOPT_PROXYTYPE]) && ($curl_options[CURLOPT_PROXYTYPE] < 0 || $curl_options[CURLOPT_PROXYTYPE] === 3)) {
  331. // 3 is legacy for NONE
  332. unset($curl_options[CURLOPT_PROXYTYPE]);
  333. if (isset($curl_options[CURLOPT_PROXY])) {
  334. unset($curl_options[CURLOPT_PROXY]);
  335. }
  336. }
  337. $simplePie->set_curl_options($curl_options);
  338. $simplePie->strip_comments(true);
  339. $simplePie->rename_attributes(['id', 'class']);
  340. $simplePie->allow_aria_attr(true);
  341. $simplePie->allow_data_attr(true);
  342. $simplePie->allowed_html_attributes([
  343. // HTML
  344. 'dir', 'draggable', 'hidden', 'lang', 'role', 'title',
  345. // MathML
  346. 'displaystyle', 'mathsize', 'scriptlevel',
  347. ]);
  348. $simplePie->allowed_html_elements_with_attributes([
  349. // HTML
  350. 'a' => ['href', 'hreflang', 'type'],
  351. 'abbr' => [],
  352. 'acronym' => [],
  353. 'address' => [],
  354. // 'area' => [], // TODO: support <area> after rewriting ids with a format like #ugc-<insert original id here> (maybe)
  355. 'article' => [],
  356. 'aside' => [],
  357. 'audio' => ['controlslist', 'loop', 'muted', 'src'],
  358. 'b' => [],
  359. 'bdi' => [],
  360. 'bdo' => [],
  361. 'big' => [],
  362. 'blink' => [],
  363. 'blockquote' => ['cite'],
  364. 'br' => ['clear'],
  365. 'button' => ['disabled'],
  366. 'canvas' => ['width', 'height'],
  367. 'caption' => ['align'],
  368. 'center' => [],
  369. 'cite' => [],
  370. 'code' => [],
  371. 'col' => ['span', 'align', 'valign', 'width'],
  372. 'colgroup' => ['span', 'align', 'valign', 'width'],
  373. 'data' => ['value'],
  374. 'datalist' => [],
  375. 'dd' => [],
  376. 'del' => ['cite', 'datetime'],
  377. 'details' => ['open'],
  378. 'dfn' => [],
  379. 'dialog' => [],
  380. 'dir' => [],
  381. 'div' => ['align'],
  382. 'dl' => [],
  383. 'dt' => [],
  384. 'em' => [],
  385. 'fieldset' => ['disabled'],
  386. 'figcaption' => [],
  387. 'figure' => [],
  388. 'footer' => [],
  389. 'h1' => [],
  390. 'h2' => [],
  391. 'h3' => [],
  392. 'h4' => [],
  393. 'h5' => [],
  394. 'h6' => [],
  395. 'header' => [],
  396. 'hgroup' => [],
  397. 'hr' => ['align', 'noshade', 'size', 'width'],
  398. 'i' => [],
  399. 'iframe' => ['src', 'align', 'frameborder', 'longdesc', 'marginheight', 'marginwidth', 'scrolling'],
  400. 'image' => ['src', 'alt', 'width', 'height', 'align', 'border', 'hspace', 'longdesc', 'vspace'],
  401. 'img' => ['src', 'alt', 'width', 'height', 'align', 'border', 'hspace', 'longdesc', 'vspace'],
  402. 'ins' => ['cite', 'datetime'],
  403. 'kbd' => [],
  404. 'label' => [],
  405. 'legend' => [],
  406. 'li' => ['value', 'type'],
  407. 'main' => [],
  408. // 'map' => [], // TODO: support <map> after rewriting ids with a format like #ugc-<insert original id here> (maybe)
  409. 'mark' => [],
  410. 'marquee' => ['behavior', 'direction', 'height', 'hspace', 'loop', 'scrollamount', 'scrolldelay', 'truespeed', 'vspace', 'width'],
  411. 'menu' => [],
  412. 'meter' => ['value', 'min', 'max', 'low', 'high', 'optimum'],
  413. 'nav' => [],
  414. 'nobr' => [],
  415. // 'noembed' => [], // <embed> is not allowed, so we want to display the contents of <noembed>
  416. 'noframes' => [],
  417. // 'noscript' => [], // From the perspective of the feed content, JS isn't allowed so we want to display the contents of <noscript>
  418. 'ol' => ['reversed', 'start', 'type'],
  419. 'optgroup' => ['disabled', 'label'],
  420. 'option' => ['disabled', 'label', 'selected', 'value'],
  421. 'output' => [],
  422. 'p' => ['align'],
  423. 'picture' => [],
  424. // 'plaintext' => [], // Can't be closed. See: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/plaintext
  425. 'pre' => ['width', 'wrap'],
  426. 'progress' => ['max', 'value'],
  427. 'q' => ['cite'],
  428. 'rb' => [],
  429. 'rp' => [],
  430. 'rt' => [],
  431. 'rtc' => [],
  432. 'ruby' => [],
  433. 's' => [],
  434. 'samp' => [],
  435. 'search' => [],
  436. 'section' => [],
  437. 'select' => ['disabled', 'multiple', 'size'],
  438. 'small' => [],
  439. 'source' => ['type', 'src', 'media', 'height', 'width'],
  440. 'span' => [],
  441. 'strike' => [],
  442. 'strong' => [],
  443. 'sub' => [],
  444. 'summary' => [],
  445. 'sup' => [],
  446. 'table' => ['align', 'border', 'cellpadding', 'cellspacing', 'rules', 'summary', 'width'],
  447. 'tbody' => ['align', 'char', 'charoff', 'valign'],
  448. 'td' => ['colspan', 'headers', 'rowspan', 'abbr', 'align', 'height', 'scope', 'valign', 'width'],
  449. 'textarea' => ['cols', 'disabled', 'maxlength', 'minlength', 'placeholder', 'readonly', 'rows', 'wrap'],
  450. 'tfoot' => ['align', 'valign'],
  451. 'th' => ['abbr', 'colspan', 'rowspan', 'scope', 'align', 'height', 'valign', 'width'],
  452. 'thead' => ['align', 'valign'],
  453. 'time' => ['datetime'],
  454. 'tr' => ['align', 'valign'],
  455. 'track' => ['default', 'kind', 'srclang', 'label', 'src'],
  456. 'tt' => [],
  457. 'u' => [],
  458. 'ul' => ['type'],
  459. 'var' => [],
  460. 'video' => ['src', 'poster', 'controlslist', 'height', 'loop', 'muted', 'playsinline', 'width'],
  461. 'wbr' => [],
  462. 'xmp' => [],
  463. // MathML
  464. 'maction' => ['actiontype', 'selection'],
  465. 'math' => ['display'],
  466. 'menclose' => ['notation'],
  467. 'merror' => [],
  468. 'mfenced' => ['close', 'open', 'separators'],
  469. 'mfrac' => ['denomalign', 'linethickness', 'numalign'],
  470. 'mi' => ['mathvariant'],
  471. 'mmultiscripts' => ['subscriptshift', 'superscriptshift'],
  472. 'mn' => [],
  473. 'mo' => ['accent', 'fence', 'form', 'largeop', 'lspace', 'maxsize', 'minsize', 'movablelimits', 'rspace', 'separator', 'stretchy', 'symmetric'],
  474. 'mover' => ['accent'],
  475. 'mpadded' => ['depth', 'height', 'lspace', 'voffset', 'width'],
  476. 'mphantom' => [],
  477. 'mprescripts' => [],
  478. 'mroot' => [],
  479. 'mrow' => [],
  480. 'ms' => [],
  481. 'mspace' => ['depth', 'height', 'width'],
  482. 'msqrt' => [],
  483. 'msub' => [],
  484. 'msubsup' => ['subscriptshift', 'superscriptshift'],
  485. 'msup' => ['superscriptshift'],
  486. 'mtable' => ['align', 'columnalign', 'columnlines', 'columnspacing', 'frame', 'framespacing', 'rowalign', 'rowlines', 'rowspacing', 'width'],
  487. 'mtd' => ['columnspan', 'rowspan', 'columnalign', 'rowalign'],
  488. 'mtext' => [],
  489. 'mtr' => ['columnalign', 'rowalign'],
  490. 'munder' => ['accentunder'],
  491. 'munderover' => ['accent', 'accentunder'],
  492. // TODO: Support SVG after sanitizing and URL rewriting of xlink:href
  493. ]);
  494. $simplePie->strip_attributes([
  495. 'data-auto-leave-validation', 'data-leave-validation', 'data-no-leave-validation', 'data-original',
  496. ]);
  497. $simplePie->add_attributes([
  498. 'audio' => ['controls' => 'controls', 'preload' => 'none'],
  499. 'iframe' => [
  500. 'allow' => 'accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share',
  501. 'sandbox' => 'allow-scripts allow-same-origin',
  502. ],
  503. 'video' => ['controls' => 'controls', 'preload' => 'none'],
  504. ]);
  505. $simplePie->set_url_replacements([
  506. 'a' => 'href',
  507. 'area' => 'href',
  508. 'audio' => 'src',
  509. 'blockquote' => 'cite',
  510. 'del' => 'cite',
  511. 'form' => 'action',
  512. 'iframe' => 'src',
  513. 'img' => [
  514. 'longdesc',
  515. 'src',
  516. ],
  517. 'image' => [
  518. 'longdesc',
  519. 'src',
  520. ],
  521. 'input' => 'src',
  522. 'ins' => 'cite',
  523. 'q' => 'cite',
  524. 'source' => 'src',
  525. 'track' => 'src',
  526. 'video' => [
  527. 'poster',
  528. 'src',
  529. ],
  530. ]);
  531. $https_domains = [];
  532. $force = @file(FRESHRSS_PATH . '/force-https.default.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  533. if (is_array($force)) {
  534. $https_domains = array_merge($https_domains, $force);
  535. }
  536. $force = @file(DATA_PATH . '/force-https.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  537. if (is_array($force)) {
  538. $https_domains = array_merge($https_domains, $force);
  539. }
  540. // Remove whitespace and comments starting with # / ;
  541. $https_domains = preg_replace('%\\s+|[\/#;].*$%', '', $https_domains) ?? $https_domains;
  542. $https_domains = array_filter($https_domains, fn(string $v) => $v !== '');
  543. $simplePie->set_https_domains($https_domains);
  544. return $simplePie;
  545. }
  546. function sanitizeHTML(string $data, string $base = '', ?int $maxLength = null): string {
  547. if ($data === '' || ($maxLength !== null && $maxLength <= 0)) {
  548. return '';
  549. }
  550. if ($maxLength !== null) {
  551. $data = mb_strcut($data, 0, $maxLength, 'UTF-8');
  552. }
  553. /** @var \SimplePie\SimplePie|null $simplePie */
  554. static $simplePie = null;
  555. if ($simplePie === null) {
  556. $simplePie = customSimplePie();
  557. $simplePie->enable_cache(false);
  558. $simplePie->init();
  559. }
  560. $sanitized = $simplePie->sanitize->sanitize($data, \SimplePie\SimplePie::CONSTRUCT_HTML, $base);
  561. if (!is_string($sanitized)) {
  562. return '';
  563. }
  564. $result = html_only_entity_decode($sanitized);
  565. if ($maxLength !== null && strlen($result) > $maxLength) {
  566. //Sanitizing has made the result too long so try again shorter
  567. $data = mb_strcut($result, 0, (2 * $maxLength) - strlen($result) - 2, 'UTF-8');
  568. return sanitizeHTML($data, $base, $maxLength);
  569. }
  570. return $result;
  571. }
  572. function cleanCache(int $hours = 720): void {
  573. // N.B.: GLOB_BRACE is not available on all platforms
  574. $files = glob(CACHE_PATH . '/*.*', GLOB_NOSORT) ?: [];
  575. foreach ($files as $file) {
  576. if (str_ends_with($file, 'index.html')) {
  577. continue;
  578. }
  579. $cacheMtime = @filemtime($file);
  580. if ($cacheMtime !== false && $cacheMtime < time() - (3600 * $hours)) {
  581. unlink($file);
  582. }
  583. }
  584. }
  585. /**
  586. * Remove the charset meta information of an HTML document, e.g.:
  587. * `<meta charset="..." />`
  588. * `<meta http-equiv="Content-Type" content="text/html; charset=...">`
  589. */
  590. function stripHtmlMetaCharset(string $html): string {
  591. return preg_replace('/<meta\s[^>]*charset\s*=\s*[^>]+>/i', '', $html, 1) ?? '';
  592. }
  593. /**
  594. * Set an XML preamble to enforce the HTML content type charset received by HTTP.
  595. * @param string $html the raw downloaded HTML content
  596. * @param string $contentType an HTTP Content-Type such as 'text/html; charset=utf-8'
  597. * @return string an HTML string with XML encoding information for DOMDocument::loadHTML()
  598. */
  599. function enforceHttpEncoding(string $html, string $contentType = ''): string {
  600. $httpCharset = preg_match('/\bcharset=([0-9a-z_-]{2,12})$/i', $contentType, $matches) === 1 ? $matches[1] : '';
  601. if ($httpCharset == '') {
  602. // No charset defined by HTTP
  603. if (preg_match('/<meta\s[^>]*charset\s*=[\s\'"]*UTF-?8\b/i', substr($html, 0, 2048))) {
  604. // Detect UTF-8 even if declared too deep in HTML for DOMDocument
  605. $httpCharset = 'UTF-8';
  606. } else {
  607. // Do nothing
  608. return $html;
  609. }
  610. }
  611. $httpCharsetNormalized = \SimplePie\Misc::encoding($httpCharset);
  612. if (in_array($httpCharsetNormalized, ['windows-1252', 'US-ASCII'], true)) {
  613. // Default charset for HTTP, do nothing
  614. return $html;
  615. }
  616. if (substr($html, 0, 3) === "\xEF\xBB\xBF" || // UTF-8 BOM
  617. substr($html, 0, 2) === "\xFF\xFE" || // UTF-16 Little Endian BOM
  618. substr($html, 0, 2) === "\xFE\xFF" || // UTF-16 Big Endian BOM
  619. substr($html, 0, 4) === "\xFF\xFE\x00\x00" || // UTF-32 Little Endian BOM
  620. substr($html, 0, 4) === "\x00\x00\xFE\xFF") { // UTF-32 Big Endian BOM
  621. // Existing byte order mark, do nothing
  622. return $html;
  623. }
  624. if (preg_match('/^<[?]xml[^>]+encoding\b/', substr($html, 0, 64))) {
  625. // Existing XML declaration, do nothing
  626. return $html;
  627. }
  628. if ($httpCharsetNormalized !== 'UTF-8') {
  629. // Try to change encoding to UTF-8 using mbstring or iconv or intl
  630. $utf8 = \SimplePie\Misc::change_encoding($html, $httpCharsetNormalized, 'UTF-8');
  631. if (is_string($utf8)) {
  632. $html = stripHtmlMetaCharset($utf8);
  633. $httpCharsetNormalized = 'UTF-8';
  634. }
  635. }
  636. if ($httpCharsetNormalized === 'UTF-8') {
  637. // Save encoding information as XML declaration
  638. return '<' . '?xml version="1.0" encoding="' . $httpCharsetNormalized . '" ?' . ">\n" . $html;
  639. }
  640. // Give up
  641. return $html;
  642. }
  643. /**
  644. * Set an HTML base URL to the HTML content if there is none.
  645. * @param string $html the raw downloaded HTML content
  646. * @param string $href the HTML base URL
  647. * @return string an HTML string
  648. */
  649. function enforceHtmlBase(string $html, string $href): string {
  650. $doc = new DOMDocument();
  651. $doc->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING);
  652. if ($doc->documentElement === null) {
  653. return '';
  654. }
  655. $xpath = new DOMXPath($doc);
  656. $bases = $xpath->evaluate('//base');
  657. if (!($bases instanceof DOMNodeList) || $bases->length === 0) {
  658. $base = $doc->createElement('base');
  659. if ($base === false) {
  660. return $html;
  661. }
  662. $base->setAttribute('href', $href);
  663. $head = null;
  664. $heads = $xpath->evaluate('//head');
  665. if ($heads instanceof DOMNodeList && $heads->length > 0) {
  666. $head = $heads->item(0);
  667. }
  668. if ($head instanceof DOMElement) {
  669. $head->insertBefore($base, $head->firstChild);
  670. } else {
  671. $doc->documentElement->insertBefore($base, $doc->documentElement->firstChild);
  672. }
  673. }
  674. return $doc->saveHTML() ?: $html;
  675. }
  676. /**
  677. * @param non-empty-string $url
  678. * @param string $type {html,ico,json,opml,xml}
  679. * @param array<string,mixed> $attributes
  680. * @param array<int,mixed> $curl_options
  681. * @return array{body:string,effective_url:string,redirect_count:int,fail:bool}
  682. */
  683. function httpGet(string $url, string $cachePath, string $type = 'html', array $attributes = [], array $curl_options = []): array {
  684. $limits = FreshRSS_Context::systemConf()->limits;
  685. $feed_timeout = empty($attributes['timeout']) || !is_numeric($attributes['timeout']) ? 0 : intval($attributes['timeout']);
  686. $cacheMtime = @filemtime($cachePath);
  687. if ($cacheMtime !== false && $cacheMtime > time() - intval($limits['cache_duration'])) {
  688. $body = @file_get_contents($cachePath);
  689. if ($body != false) {
  690. syslog(LOG_DEBUG, 'FreshRSS uses cache for ' . \SimplePie\Misc::url_remove_credentials($url));
  691. return ['body' => $body, 'effective_url' => $url, 'redirect_count' => 0, 'fail' => false];
  692. }
  693. }
  694. if (rand(0, 30) === 1) { // Remove old cache once in a while
  695. cleanCache(CLEANCACHE_HOURS);
  696. }
  697. if (($retryAfter = FreshRSS_http_Util::getRetryAfter($url)) > 0) {
  698. Minz_Log::warning('For that domain, will first retry after ' . date('c', $retryAfter) . '. ' . \SimplePie\Misc::url_remove_credentials($url));
  699. return ['body' => '', 'effective_url' => $url, 'redirect_count' => 0, 'fail' => true];
  700. }
  701. if (FreshRSS_Context::systemConf()->simplepie_syslog_enabled) {
  702. syslog(LOG_INFO, 'FreshRSS GET ' . $type . ' ' . \SimplePie\Misc::url_remove_credentials($url));
  703. }
  704. $accept = '';
  705. switch ($type) {
  706. case 'json':
  707. $accept = 'application/json,application/feed+json,application/javascript;q=0.9,text/javascript;q=0.8,*/*;q=0.7';
  708. break;
  709. case 'opml':
  710. $accept = 'text/x-opml,text/xml;q=0.9,application/xml;q=0.9,*/*;q=0.8';
  711. break;
  712. case 'xml':
  713. $accept = 'application/xml,application/xhtml+xml,text/xml;q=0.9,*/*;q=0.8';
  714. break;
  715. case 'ico':
  716. $accept = 'image/x-icon,image/vnd.microsoft.icon,image/ico,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.1';
  717. break;
  718. case 'html':
  719. default:
  720. $accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
  721. break;
  722. }
  723. // TODO: Implement HTTP 1.1 conditional GET If-Modified-Since
  724. $ch = curl_init();
  725. if ($ch === false) {
  726. return ['body' => '', 'effective_url' => '', 'redirect_count' => 0, 'fail' => true];
  727. }
  728. curl_setopt_array($ch, [
  729. CURLOPT_URL => $url,
  730. CURLOPT_HTTPHEADER => ['Accept: ' . $accept],
  731. CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
  732. CURLOPT_CONNECTTIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  733. CURLOPT_TIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  734. CURLOPT_MAXREDIRS => 4,
  735. CURLOPT_RETURNTRANSFER => true,
  736. CURLOPT_FOLLOWLOCATION => true,
  737. CURLOPT_ENCODING => '', //Enable all encodings
  738. //CURLOPT_VERBOSE => 1, // To debug sent HTTP headers
  739. ]);
  740. $responseHeaders = '';
  741. curl_setopt($ch, CURLOPT_HEADERFUNCTION, function (\CurlHandle $ch, string $header) use (&$responseHeaders) {
  742. if (trim($header) !== '') { // Skip e.g. separation with trailer headers
  743. $responseHeaders .= $header;
  744. }
  745. return strlen($header);
  746. });
  747. curl_setopt_array($ch, FreshRSS_Context::systemConf()->curl_options);
  748. if (is_array($attributes['curl_params'] ?? null)) {
  749. $options = sanitizeCurlParams($attributes['curl_params']);
  750. if (is_array($options[CURLOPT_HTTPHEADER] ?? null)) {
  751. // Remove headers problematic for security
  752. $options[CURLOPT_HTTPHEADER] = array_filter($options[CURLOPT_HTTPHEADER],
  753. fn($header) => is_string($header) && !preg_match('/^(Remote-User|X-WebAuth-User)\\s*:/i', $header));
  754. // Add Accept header if it is not set
  755. if (preg_grep('/^Accept\\s*:/i', $options[CURLOPT_HTTPHEADER]) === false) {
  756. $options[CURLOPT_HTTPHEADER][] = 'Accept: ' . $accept;
  757. }
  758. }
  759. curl_setopt_array($ch, $options);
  760. }
  761. if (isset($attributes['ssl_verify'])) {
  762. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, empty($attributes['ssl_verify']) ? 0 : 2);
  763. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (bool)$attributes['ssl_verify']);
  764. if (empty($attributes['ssl_verify'])) {
  765. curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'DEFAULT@SECLEVEL=1');
  766. }
  767. }
  768. curl_setopt_array($ch, $curl_options);
  769. $body = curl_exec($ch);
  770. $c_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  771. $c_content_type = '' . curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  772. $c_effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  773. $c_redirect_count = curl_getinfo($ch, CURLINFO_REDIRECT_COUNT);
  774. $c_error = curl_error($ch);
  775. $headers = [];
  776. if ($body !== false) {
  777. assert($c_redirect_count >= 0);
  778. $responseHeaders = \SimplePie\HTTP\Parser::prepareHeaders($responseHeaders, $c_redirect_count + 1);
  779. $parser = new \SimplePie\HTTP\Parser($responseHeaders);
  780. if ($parser->parse()) {
  781. $headers = $parser->headers;
  782. }
  783. }
  784. $fail = $c_status != 200 || $c_error != '' || $body === false;
  785. if ($fail) {
  786. $body = '';
  787. Minz_Log::warning('Error fetching content: HTTP code ' . $c_status . ': ' . $c_error . ' ' . $url);
  788. if (in_array($c_status, [429, 503], true)) {
  789. $retryAfter = FreshRSS_http_Util::setRetryAfter($url, $headers['retry-after'] ?? '');
  790. if ($c_status === 429) {
  791. $errorMessage = 'HTTP 429 Too Many Requests! [' . \SimplePie\Misc::url_remove_credentials($url) . ']';
  792. } elseif ($c_status === 503) {
  793. $errorMessage = 'HTTP 503 Service Unavailable! [' . \SimplePie\Misc::url_remove_credentials($url) . ']';
  794. }
  795. if ($retryAfter > 0) {
  796. $errorMessage .= ' We may retry after ' . date('c', $retryAfter);
  797. }
  798. }
  799. // TODO: Implement HTTP 410 Gone
  800. } elseif (!is_string($body) || strlen($body) === 0) {
  801. $body = '';
  802. } else {
  803. if (in_array($type, ['html', 'json', 'opml', 'xml'], true)) {
  804. $body = trim($body, " \n\r\t\v"); // Do not trim \x00 to avoid breaking a BOM
  805. }
  806. if (in_array($type, ['html', 'xml', 'opml'], true)) {
  807. $body = enforceHttpEncoding($body, $c_content_type);
  808. }
  809. if (in_array($type, ['html'], true)) {
  810. $body = enforceHtmlBase($body, $c_effective_url);
  811. }
  812. }
  813. if (file_put_contents($cachePath, $body) === false) {
  814. Minz_Log::warning("Error saving cache $cachePath for $url");
  815. }
  816. return ['body' => $body, 'effective_url' => $c_effective_url, 'redirect_count' => $c_redirect_count, 'fail' => $fail];
  817. }
  818. /**
  819. * Validate an email address, supports internationalized addresses.
  820. *
  821. * @param string $email The address to validate
  822. * @return bool true if email is valid, else false
  823. */
  824. function validateEmailAddress(string $email): bool {
  825. $mailer = new PHPMailer\PHPMailer\PHPMailer();
  826. $mailer->CharSet = 'utf-8';
  827. $punyemail = $mailer->punyencodeAddress($email);
  828. return PHPMailer\PHPMailer\PHPMailer::validateAddress($punyemail, 'html5');
  829. }
  830. /**
  831. * Add support of image lazy loading
  832. * Move content from src/poster attribute to data-original
  833. * @param string $content is the text we want to parse
  834. */
  835. function lazyimg(string $content): string {
  836. return preg_replace([
  837. '/<((?:img|image|iframe|track)[^>]+?)src="([^"]+)"([^>]*)>/i',
  838. "/<((?:img|image|iframe|track)[^>]+?)src='([^']+)'([^>]*)>/i",
  839. '/<((?:video)[^>]+?)poster="([^"]+)"([^>]*)>/i',
  840. "/<((?:video)[^>]+?)poster='([^']+)'([^>]*)>/i",
  841. ], [
  842. '<$1src="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  843. "<$1src='" . Minz_Url::display('/themes/icons/grey.gif') . "' data-original='$2'$3>",
  844. '<$1poster="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  845. "<$1poster='" . Minz_Url::display('/themes/icons/grey.gif') . "' data-original='$2'$3>",
  846. ],
  847. $content
  848. ) ?? '';
  849. }
  850. /** @return numeric-string */
  851. function uTimeString(): string {
  852. $t = gettimeofday();
  853. // @phpstan-ignore return.type
  854. return ((string)$t['sec']) . str_pad((string)$t['usec'], 6, '0', STR_PAD_LEFT);
  855. }
  856. function invalidateHttpCache(string $username = ''): bool {
  857. if (!FreshRSS_user_Controller::checkUsername($username)) {
  858. Minz_Session::_param('touch', uTimeString());
  859. $username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
  860. }
  861. return FreshRSS_UserDAO::ctouch($username);
  862. }
  863. /**
  864. * @return list<string>
  865. */
  866. function listUsers(): array {
  867. $final_list = [];
  868. $base_path = join_path(DATA_PATH, 'users');
  869. $dir_list = array_values(array_diff(
  870. scandir($base_path) ?: [],
  871. ['..', '.', Minz_User::INTERNAL_USER]
  872. ));
  873. foreach ($dir_list as $file) {
  874. if ($file[0] !== '.' && is_dir(join_path($base_path, $file)) && file_exists(join_path($base_path, $file, 'config.php'))) {
  875. $final_list[] = $file;
  876. }
  877. }
  878. return $final_list;
  879. }
  880. /**
  881. * Return if the maximum number of registrations has been reached.
  882. * Note a max_registrations of 0 means there is no limit.
  883. *
  884. * @return bool true if number of users >= max registrations, false else.
  885. */
  886. function max_registrations_reached(): bool {
  887. $limit_registrations = FreshRSS_Context::systemConf()->limits['max_registrations'];
  888. $number_accounts = count(listUsers());
  889. return $limit_registrations > 0 && $number_accounts >= $limit_registrations;
  890. }
  891. /**
  892. * Register and return the configuration for a given user.
  893. *
  894. * Note this function has been created to generate temporary configuration
  895. * objects. If you need a long-time configuration, please don't use this function.
  896. *
  897. * @param string $username the name of the user of which we want the configuration.
  898. * @return FreshRSS_UserConfiguration|null object, or null if the configuration cannot be loaded.
  899. * @throws Minz_ConfigurationNamespaceException
  900. */
  901. function get_user_configuration(string $username): ?FreshRSS_UserConfiguration {
  902. if (!FreshRSS_user_Controller::checkUsername($username)) {
  903. return null;
  904. }
  905. $namespace = 'user_' . $username;
  906. try {
  907. FreshRSS_UserConfiguration::register($namespace,
  908. USERS_PATH . '/' . $username . '/config.php',
  909. FRESHRSS_PATH . '/config-user.default.php');
  910. } catch (Minz_FileNotExistException $e) {
  911. Minz_Log::warning($e->getMessage(), ADMIN_LOG);
  912. return null;
  913. }
  914. $user_conf = FreshRSS_UserConfiguration::get($namespace);
  915. return $user_conf;
  916. }
  917. /**
  918. * Converts an IP (v4 or v6) to a binary representation using inet_pton
  919. *
  920. * @param string $ip the IP to convert
  921. * @return string a binary representation of the specified IP
  922. */
  923. function ipToBits(string $ip): string {
  924. $binaryip = '';
  925. foreach (str_split(inet_pton($ip) ?: '') as $char) {
  926. $binaryip .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT);
  927. }
  928. return $binaryip;
  929. }
  930. /**
  931. * Check if an ip belongs to the provided range (in CIDR format)
  932. *
  933. * @param string $ip the IP that we want to verify (ex: 192.168.16.1)
  934. * @param string $range the range to check against (ex: 192.168.16.0/24)
  935. * @return bool true if the IP is in the range, otherwise false
  936. */
  937. function checkCIDR(string $ip, string $range): bool {
  938. $binary_ip = ipToBits($ip);
  939. $split = explode('/', $range);
  940. $subnet = $split[0] ?? '';
  941. if ($subnet == '') {
  942. return false;
  943. }
  944. $binary_subnet = ipToBits($subnet);
  945. $mask_bits = $split[1] ?? '';
  946. $mask_bits = (int)$mask_bits;
  947. if ($mask_bits === 0) {
  948. $mask_bits = null;
  949. }
  950. $ip_net_bits = substr($binary_ip, 0, $mask_bits);
  951. $subnet_bits = substr($binary_subnet, 0, $mask_bits);
  952. return $ip_net_bits === $subnet_bits;
  953. }
  954. /**
  955. * Use CONN_REMOTE_ADDR (if available, to be robust even when using Apache mod_remoteip) or REMOTE_ADDR environment variable to determine the connection IP.
  956. */
  957. function connectionRemoteAddress(): string {
  958. $remoteIp = is_string($_SERVER['CONN_REMOTE_ADDR'] ?? null) ? $_SERVER['CONN_REMOTE_ADDR'] : '';
  959. if ($remoteIp == '') {
  960. $remoteIp = is_string($_SERVER['REMOTE_ADDR'] ?? null) ? $_SERVER['REMOTE_ADDR'] : '';
  961. }
  962. if ($remoteIp == 0) {
  963. $remoteIp = '';
  964. }
  965. return $remoteIp;
  966. }
  967. /**
  968. * Check if the client (e.g. last proxy) is allowed to send unsafe headers.
  969. * This uses the `TRUSTED_PROXY` environment variable or the `trusted_sources` configuration option to get an array of the authorized ranges,
  970. * The connection IP is obtained from the `CONN_REMOTE_ADDR` (if available, to be robust even when using Apache mod_remoteip) or `REMOTE_ADDR` environment variables.
  971. * @return bool true if the sender’s IP is in one of the ranges defined in the configuration, else false
  972. */
  973. function checkTrustedIP(): bool {
  974. if (!FreshRSS_Context::hasSystemConf()) {
  975. return false;
  976. }
  977. $remoteIp = connectionRemoteAddress();
  978. if ($remoteIp === '') {
  979. return false;
  980. }
  981. $trusted = getenv('TRUSTED_PROXY');
  982. if ($trusted != 0 && is_string($trusted)) {
  983. $trusted = preg_split('/\s+/', $trusted, -1, PREG_SPLIT_NO_EMPTY);
  984. }
  985. if (!is_array($trusted) || empty($trusted)) {
  986. $trusted = FreshRSS_Context::systemConf()->trusted_sources;
  987. }
  988. foreach ($trusted as $cidr) {
  989. if (checkCIDR($remoteIp, $cidr)) {
  990. return true;
  991. }
  992. }
  993. return false;
  994. }
  995. function httpAuthUser(bool $onlyTrusted = true): string {
  996. $auths = array_unique(array_intersect_key($_SERVER, ['REMOTE_USER' => '', 'REDIRECT_REMOTE_USER' => '', 'HTTP_REMOTE_USER' => '', 'HTTP_X_WEBAUTH_USER' => '']));
  997. if (count($auths) > 1) {
  998. Minz_Log::warning('Multiple HTTP authentication headers!');
  999. return '';
  1000. }
  1001. if (!empty($_SERVER['REMOTE_USER']) && is_string($_SERVER['REMOTE_USER'])) {
  1002. return $_SERVER['REMOTE_USER'];
  1003. }
  1004. if (!empty($_SERVER['REDIRECT_REMOTE_USER']) && is_string($_SERVER['REDIRECT_REMOTE_USER'])) {
  1005. return $_SERVER['REDIRECT_REMOTE_USER'];
  1006. }
  1007. if (!$onlyTrusted || checkTrustedIP()) {
  1008. if (!empty($_SERVER['HTTP_REMOTE_USER']) && is_string($_SERVER['HTTP_REMOTE_USER'])) {
  1009. return $_SERVER['HTTP_REMOTE_USER'];
  1010. }
  1011. if (!empty($_SERVER['HTTP_X_WEBAUTH_USER']) && is_string($_SERVER['HTTP_X_WEBAUTH_USER'])) {
  1012. return $_SERVER['HTTP_X_WEBAUTH_USER'];
  1013. }
  1014. }
  1015. return '';
  1016. }
  1017. function cryptAvailable(): bool {
  1018. $hash = '$2y$04$usesomesillystringfore7hnbRJHxXVLeakoG8K30oukPsA.ztMG';
  1019. return $hash === @crypt('password', $hash);
  1020. }
  1021. /**
  1022. * Check PHP and its extensions are well-installed.
  1023. *
  1024. * @return array<string,bool> of tested values.
  1025. */
  1026. function check_install_php(): array {
  1027. $pdo_mysql = extension_loaded('pdo_mysql');
  1028. $pdo_pgsql = extension_loaded('pdo_pgsql');
  1029. $pdo_sqlite = extension_loaded('pdo_sqlite');
  1030. return [
  1031. 'php' => version_compare(PHP_VERSION, FRESHRSS_MIN_PHP_VERSION) >= 0,
  1032. 'curl' => extension_loaded('curl'),
  1033. 'pdo' => $pdo_mysql || $pdo_sqlite || $pdo_pgsql,
  1034. 'pcre' => extension_loaded('pcre'),
  1035. 'ctype' => extension_loaded('ctype'),
  1036. 'fileinfo' => extension_loaded('fileinfo'),
  1037. 'dom' => class_exists('DOMDocument'),
  1038. 'json' => extension_loaded('json'),
  1039. 'mbstring' => extension_loaded('mbstring'),
  1040. 'zip' => extension_loaded('zip'),
  1041. ];
  1042. }
  1043. /**
  1044. * Check different data files and directories exist.
  1045. * @return array<string,bool> of tested values.
  1046. */
  1047. function check_install_files(): array {
  1048. return [
  1049. 'data' => is_dir(DATA_PATH) && touch(DATA_PATH . '/index.html'), // is_writable() is not reliable for a folder on NFS
  1050. 'cache' => is_dir(CACHE_PATH) && touch(CACHE_PATH . '/index.html'),
  1051. 'users' => is_dir(USERS_PATH) && touch(USERS_PATH . '/index.html'),
  1052. 'favicons' => is_dir(DATA_PATH) && touch(DATA_PATH . '/favicons/index.html'),
  1053. 'tokens' => is_dir(DATA_PATH) && touch(DATA_PATH . '/tokens/index.html'),
  1054. ];
  1055. }
  1056. /**
  1057. * Check database is well-installed.
  1058. *
  1059. * @return array<string,bool> of tested values.
  1060. */
  1061. function check_install_database(): array {
  1062. $status = [
  1063. 'connection' => true,
  1064. 'tables' => false,
  1065. 'categories' => false,
  1066. 'feeds' => false,
  1067. 'entries' => false,
  1068. 'entrytmp' => false,
  1069. 'tag' => false,
  1070. 'entrytag' => false,
  1071. ];
  1072. try {
  1073. $dbDAO = FreshRSS_Factory::createDatabaseDAO();
  1074. $status['tables'] = $dbDAO->tablesAreCorrect();
  1075. $status['categories'] = $dbDAO->categoryIsCorrect();
  1076. $status['feeds'] = $dbDAO->feedIsCorrect();
  1077. $status['entries'] = $dbDAO->entryIsCorrect();
  1078. $status['entrytmp'] = $dbDAO->entrytmpIsCorrect();
  1079. $status['tag'] = $dbDAO->tagIsCorrect();
  1080. $status['entrytag'] = $dbDAO->entrytagIsCorrect();
  1081. } catch (Minz_PDOConnectionException $e) {
  1082. $status['connection'] = false;
  1083. }
  1084. return $status;
  1085. }
  1086. /**
  1087. * Remove a directory recursively.
  1088. * From http://php.net/rmdir#110489
  1089. */
  1090. function recursive_unlink(string $dir): bool {
  1091. if (!is_dir($dir)) {
  1092. return true;
  1093. }
  1094. if (is_link($dir)) {
  1095. if (PHP_OS_FAMILY === "Windows") {
  1096. return rmdir($dir);
  1097. }
  1098. return unlink($dir);
  1099. }
  1100. $files = array_diff(scandir($dir) ?: [], ['.', '..']);
  1101. foreach ($files as $filename) {
  1102. $filename = $dir . '/' . $filename;
  1103. if (is_dir($filename)) {
  1104. @chmod($filename, 0777);
  1105. recursive_unlink($filename);
  1106. } else {
  1107. unlink($filename);
  1108. }
  1109. }
  1110. return rmdir($dir);
  1111. }
  1112. /**
  1113. * Remove queries where $get is appearing.
  1114. * @param string $get the get attribute which should be removed.
  1115. * @param array<int,array{get?:string,name?:string,order?:string,search?:string,state?:int,url?:string,token?:string,
  1116. * shareRss?:bool,shareOpml?:bool,description?:string,imageUrl?:string}> $queries an array of queries.
  1117. * @return array<int,array{get?:string,name?:string,order?:string,search?:string,state?:int,url?:string,token?:string,
  1118. * shareRss?:bool,shareOpml?:bool,description?:string,imageUrl?:string}> without queries where $get is appearing.
  1119. */
  1120. function remove_query_by_get(string $get, array $queries): array {
  1121. $final_queries = [];
  1122. foreach ($queries as $query) {
  1123. if (empty($query['get']) || $query['get'] !== $get) {
  1124. $final_queries[] = $query;
  1125. }
  1126. }
  1127. return $final_queries;
  1128. }
  1129. function _i(string $icon, int $type = FreshRSS_Themes::ICON_DEFAULT): string {
  1130. return FreshRSS_Themes::icon($icon, $type);
  1131. }
  1132. const SHORTCUT_KEYS = [
  1133. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  1134. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  1135. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  1136. 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12',
  1137. 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'Backspace', 'Delete',
  1138. 'End', 'Enter', 'Escape', 'Home', 'Insert', 'PageDown', 'PageUp', 'Space', 'Tab',
  1139. ];
  1140. /**
  1141. * @param array<string> $shortcuts
  1142. * @return list<string>
  1143. */
  1144. function getNonStandardShortcuts(array $shortcuts): array {
  1145. $standard = strtolower(implode(' ', SHORTCUT_KEYS));
  1146. $nonStandard = array_filter($shortcuts, static function (string $shortcut) use ($standard) {
  1147. $shortcut = trim($shortcut);
  1148. return $shortcut !== '' && stripos($standard, $shortcut) === false;
  1149. });
  1150. return array_values($nonStandard);
  1151. }
  1152. function errorMessageInfo(string $errorTitle, string $error = ''): string {
  1153. $errorTitle = htmlspecialchars($errorTitle, ENT_NOQUOTES, 'UTF-8');
  1154. $message = '';
  1155. $details = '';
  1156. $error = trim($error);
  1157. // Prevent empty tags by checking if error is not empty first
  1158. if ($error !== '') {
  1159. $error = htmlspecialchars($error, ENT_NOQUOTES, 'UTF-8') . "\n";
  1160. // First line is the main message, other lines are the details
  1161. list($message, $details) = explode("\n", $error, 2);
  1162. $message = "<h2>{$message}</h2>";
  1163. $details = "<pre>{$details}</pre>";
  1164. }
  1165. header("Content-Security-Policy: default-src 'self'; frame-ancestors " .
  1166. (FreshRSS_Context::systemConf()->attributeString('csp.frame-ancestors') ?? "'none'"));
  1167. header('Referrer-Policy: same-origin');
  1168. return <<<MSG
  1169. <!DOCTYPE html><html><header><title>HTTP 500: {$errorTitle}</title></header><body>
  1170. <h1>HTTP 500: {$errorTitle}</h1>
  1171. {$message}
  1172. {$details}
  1173. <hr />
  1174. <small>For help see the documentation: <a href="https://freshrss.github.io/FreshRSS/en/admins/logs_and_errors.html" target="_blank">
  1175. https://freshrss.github.io/FreshRSS/en/admins/logs_and_errors.html</a></small>
  1176. </body></html>
  1177. MSG;
  1178. }