lib_rss.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. declare(strict_types=1);
  3. // Polyfill PHP 8.4+ `Dom\` classes, including the CSS selector API,
  4. // maintained together with the vendored SimplePie fork:
  5. require_once LIB_PATH . '/simplepie/simplepie/compat/dom.php';
  6. if (!function_exists('mb_strcut')) {
  7. function mb_strcut(string $str, int $start, ?int $length = null, string $encoding = 'UTF-8'): string {
  8. return substr($str, $start, $length) ?: '';
  9. }
  10. }
  11. if (!function_exists('syslog')) {
  12. if (COPY_SYSLOG_TO_STDERR && !defined('STDERR')) {
  13. define('STDERR', fopen('php://stderr', 'w'));
  14. }
  15. function syslog(int $priority, string $message): bool {
  16. if (COPY_SYSLOG_TO_STDERR && defined('STDERR') && is_resource(STDERR)) {
  17. return fwrite(STDERR, $message . "\n") != false;
  18. }
  19. return false;
  20. }
  21. }
  22. if (function_exists('openlog')) {
  23. if (COPY_SYSLOG_TO_STDERR) {
  24. openlog('FreshRSS', LOG_CONS | LOG_ODELAY | LOG_PID | LOG_PERROR, LOG_USER);
  25. } else {
  26. openlog('FreshRSS', LOG_CONS | LOG_ODELAY | LOG_PID, LOG_USER);
  27. }
  28. }
  29. /**
  30. * Build a directory path by concatenating a list of directory names.
  31. *
  32. * @param string ...$path_parts a list of directory names
  33. * @return string corresponding to the final pathname
  34. */
  35. function join_path(...$path_parts): string {
  36. return join(DIRECTORY_SEPARATOR, $path_parts);
  37. }
  38. /**
  39. * Build the mutex path for an actualisation run.
  40. *
  41. * The data path identifies a FreshRSS instance, while the temporary path only
  42. * determines where its mutex is stored.
  43. */
  44. function actualize_mutex_file(string $tmpPath, string $dataPath): string {
  45. return $tmpPath . '/actualize.' . hash('sha256', realpath($dataPath) ?: $dataPath) . '.freshrss.lock';
  46. }
  47. /**
  48. * Disable stream wrappers that the application does not need.
  49. *
  50. * For temporary usage of a previously unregistered wrapper, `stream_wrapper_restore()` can be used.
  51. */
  52. function unregister_unsafe_protocols(): void {
  53. $registered_wrappers = stream_get_wrappers();
  54. $allowed_wrappers = ['file', 'php'];
  55. if (defined('__PHPSTAN_RUNNING__')) {
  56. // Needed for PHPStan compatibility PHP < 8.4
  57. $allowed_wrappers[] = 'phar';
  58. }
  59. foreach ($registered_wrappers as $protocol) {
  60. if (!in_array($protocol, $allowed_wrappers, true)) {
  61. stream_wrapper_unregister($protocol);
  62. }
  63. }
  64. }
  65. if (!defined('WITH_COMPOSER')) {
  66. unregister_unsafe_protocols();
  67. }
  68. //<Auto-loading>
  69. function classAutoloader(string $class): void {
  70. if (str_starts_with($class, 'FreshRSS')) {
  71. $components = explode('_', $class);
  72. switch (count($components)) {
  73. case 1:
  74. include APP_PATH . '/' . $components[0] . '.php';
  75. return;
  76. case 2:
  77. include APP_PATH . '/Models/' . $components[1] . '.php';
  78. return;
  79. case 3: //Controllers, Exceptions
  80. include APP_PATH . '/' . $components[2] . 's/' . $components[1] . $components[2] . '.php';
  81. return;
  82. }
  83. } elseif (str_starts_with($class, 'Minz')) {
  84. include LIB_PATH . '/' . str_replace('_', '/', $class) . '.php';
  85. } elseif (str_starts_with($class, 'SimplePie\\')) {
  86. $prefix = 'SimplePie\\';
  87. $base_dir = LIB_PATH . '/simplepie/simplepie/src/';
  88. $relative_class_name = substr($class, strlen($prefix));
  89. include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  90. } elseif (str_starts_with($class, 'GT\\CssXPath\\')) {
  91. $prefix = 'GT\\CssXPath\\';
  92. $base_dir = LIB_PATH . '/phpgt/cssxpath/src/';
  93. $relative_class_name = substr($class, strlen($prefix));
  94. include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  95. } elseif (str_starts_with($class, 'marienfressinaud\\LibOpml\\')) {
  96. $prefix = 'marienfressinaud\\LibOpml\\';
  97. $base_dir = LIB_PATH . '/marienfressinaud/lib_opml/src/LibOpml/';
  98. $relative_class_name = substr($class, strlen($prefix));
  99. include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  100. } elseif (str_starts_with($class, 'PHPMailer\\PHPMailer\\')) {
  101. $prefix = 'PHPMailer\\PHPMailer\\';
  102. $base_dir = LIB_PATH . '/phpmailer/phpmailer/src/';
  103. $relative_class_name = substr($class, strlen($prefix));
  104. include $base_dir . str_replace('\\', '/', $relative_class_name) . '.php';
  105. }
  106. }
  107. spl_autoload_register('classAutoloader');
  108. //</Auto-loading>
  109. /**
  110. * @param array<mixed,mixed> $array
  111. * @phpstan-assert-if-true array<string,mixed> $array
  112. */
  113. function is_array_keys_string(array $array): bool {
  114. foreach ($array as $key => $value) {
  115. if (!is_string($key)) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. /**
  122. * @param array<mixed,mixed> $array
  123. * @phpstan-assert-if-true array<mixed,string> $array
  124. */
  125. function is_array_values_string(array $array): bool {
  126. foreach ($array as $value) {
  127. if (!is_string($value)) {
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. /**
  134. * Memory efficient replacement of `echo json_encode(...)`
  135. * @param array<mixed>|mixed $json
  136. * @param int $optimisationDepth Number of levels for which to perform memory optimisation
  137. * before calling the faster native JSON serialisation.
  138. * Set to negative value for infinite depth.
  139. */
  140. function echoJson($json, int $optimisationDepth = -1): void {
  141. if ($optimisationDepth === 0 || !is_array($json)) {
  142. echo json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  143. return;
  144. }
  145. $first = true;
  146. if (array_is_list($json)) {
  147. echo '[';
  148. foreach ($json as $item) {
  149. if ($first) {
  150. $first = false;
  151. } else {
  152. echo ',';
  153. }
  154. echoJson($item, $optimisationDepth - 1);
  155. }
  156. echo ']';
  157. } else {
  158. echo '{';
  159. foreach ($json as $key => $value) {
  160. if ($first) {
  161. $first = false;
  162. } else {
  163. echo ',';
  164. }
  165. echo json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), ':';
  166. echoJson($value, $optimisationDepth - 1);
  167. }
  168. echo '}';
  169. }
  170. }
  171. function safe_ascii(?string $text): string {
  172. return $text === null ? '' : (filter_var($text, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH) ?: '');
  173. }
  174. if (function_exists('mb_convert_encoding')) {
  175. function safe_utf8(?string $text): string {
  176. return $text === null ? '' : (mb_convert_encoding($text, 'UTF-8', 'UTF-8') ?: '');
  177. }
  178. } elseif (function_exists('iconv')) {
  179. function safe_utf8(?string $text): string {
  180. return $text === null ? '' : (iconv('UTF-8', 'UTF-8//IGNORE', $text) ?: '');
  181. }
  182. } else {
  183. function safe_utf8(?string $text): string {
  184. return $text ?? '';
  185. }
  186. }
  187. function escapeToUnicodeAlternative(string $text, bool $extended = true): string {
  188. $text = htmlspecialchars_decode($text, ENT_QUOTES);
  189. //Problematic characters
  190. $problem = ['&', '<', '>'];
  191. //Use their fullwidth Unicode form instead:
  192. $replace = ['&', '<', '>'];
  193. // https://raw.githubusercontent.com/mihaip/google-reader-api/master/wiki/StreamId.wiki
  194. if ($extended) {
  195. $problem += ["'", '"', '^', '?', '\\', '/', ',', ';'];
  196. $replace += ["’", '"', '^', '?', '\', '/', ',', ';'];
  197. }
  198. return trim(str_replace($problem, $replace, $text));
  199. }
  200. function format_number(int|float $n, int $precision = 0): string {
  201. // number_format does not seem to be Unicode-compatible
  202. return str_replace(' ', ' ', // Thin non-breaking space
  203. number_format((float)$n, $precision, '.', ' ')
  204. );
  205. }
  206. function format_bytes(int $bytes, int $precision = 2, string $system = 'IEC'): string {
  207. if ($system === 'IEC') {
  208. $base = 1024;
  209. $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
  210. } elseif ($system === 'SI') {
  211. $base = 1000;
  212. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  213. } else {
  214. return format_number($bytes, $precision);
  215. }
  216. $bytes = max(intval($bytes), 0);
  217. $pow = $bytes === 0 ? 0 : (int)floor(log($bytes) / log($base));
  218. $pow = min(max(0, $pow), count($units) - 1);
  219. $bytes /= pow($base, $pow);
  220. return format_number($bytes, $precision) . ' ' . $units[$pow];
  221. }
  222. function timestamptodate(int $t, bool $hour = true): string {
  223. $month = _t('gen.date.' . date('M', $t));
  224. if ($hour) {
  225. $date = _t('gen.date.format_date_hour', $month);
  226. } else {
  227. $date = _t('gen.date.format_date', $month);
  228. }
  229. return @date($date, $t) ?: '';
  230. }
  231. function timestampToMachineDate(int $t): string {
  232. return @date(DATE_ATOM, $t);
  233. }
  234. /**
  235. * Human readable string how long this timestamp is ago ("5 years ago").
  236. */
  237. function timeago(int $timestamp, ?int $baseTimestamp = null): string {
  238. $baseTimestamp ??= time();
  239. $delta = abs($baseTimestamp - $timestamp);
  240. $units = [
  241. [31536000, 'year'],
  242. [2592000, 'month'],
  243. [86400, 'day'],
  244. [3600, 'hour'],
  245. [60, 'minute'],
  246. ];
  247. $diff = '';
  248. foreach ($units as [$unitSeconds, $unit]) {
  249. if ($delta >= $unitSeconds) {
  250. $unitValue = intdiv($delta, $unitSeconds);
  251. $diff = Minz_Translate::plural('gen.interval.' . $unit, $unitValue) ?? ($unitValue . ' ' . $unit . ' ago');
  252. break;
  253. }
  254. }
  255. if ($diff === '') {
  256. return Minz_Translate::t('gen.interval.justnow');
  257. }
  258. return $diff;
  259. }
  260. /**
  261. * Decode HTML entities but preserve XML entities.
  262. */
  263. function html_only_entity_decode(?string $text): string {
  264. /** @var array<string,string>|null $htmlEntitiesOnly */
  265. static $htmlEntitiesOnly = null;
  266. if ($htmlEntitiesOnly === null) {
  267. $htmlEntitiesOnly = array_flip(array_diff(
  268. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  269. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  270. ));
  271. }
  272. return $text == null ? '' : strtr($text, $htmlEntitiesOnly);
  273. }
  274. /**
  275. * Remove passwords in FreshRSS logs.
  276. * See also ../cli/sensitive-log.sh for Web server logs.
  277. * @param array<string,mixed>|string $log
  278. * @return array<string,mixed>|string
  279. */
  280. function sensitive_log(array|string $log): array|string {
  281. if (is_array($log)) {
  282. foreach ($log as $k => $v) {
  283. if (in_array($k, ['api_key', 'Passwd', 'T'], true)) {
  284. $log[$k] = '██';
  285. } elseif ((is_array($v) && is_array_keys_string($v)) || is_string($v)) {
  286. $log[$k] = sensitive_log($v);
  287. } else {
  288. return '';
  289. }
  290. }
  291. } elseif (is_string($log)) {
  292. $log = preg_replace([
  293. '/\b(auth=.*?\/)[^&]+/i',
  294. '/\b(Passwd=)[^&]+/i',
  295. '/\b(Authorization)[^&]+/i',
  296. ], '$1█', $log) ?? '';
  297. }
  298. return $log;
  299. }
  300. function cleanCache(int $hours = 720): void {
  301. // N.B.: GLOB_BRACE is not available on all platforms
  302. $files = glob(CACHE_PATH . '/*.*', GLOB_NOSORT) ?: [];
  303. foreach ($files as $file) {
  304. if (str_ends_with($file, 'index.html')) {
  305. continue;
  306. }
  307. $cacheMtime = @filemtime($file);
  308. if ($cacheMtime !== false && $cacheMtime < time() - (3600 * $hours)) {
  309. unlink($file);
  310. }
  311. }
  312. }
  313. /**
  314. * Add native lazy loading by inserting a `loading="lazy"` attribute right after
  315. * the tag name of img/image/iframe/track/video elements that do not have one.
  316. * @param string $content is the text we want to parse
  317. */
  318. function lazyimg(string $content): string {
  319. return preg_replace(
  320. '/<((?:img|image|iframe|track|video)\b)(?![^>]*\b(?:loading|data-original)=)([^>]*+)>/i',
  321. '<$1 loading="lazy"$2>',
  322. $content
  323. ) ?? '';
  324. }
  325. /** @return numeric-string */
  326. function uTimeString(): string {
  327. $t = gettimeofday();
  328. // @phpstan-ignore return.type
  329. return ((string)$t['sec']) . str_pad((string)$t['usec'], 6, '0', STR_PAD_LEFT);
  330. }
  331. function invalidateHttpCache(string $username = ''): bool {
  332. if (!FreshRSS_user_Controller::checkUsername($username)) {
  333. Minz_Session::_param('touch', uTimeString());
  334. $username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
  335. }
  336. return FreshRSS_UserDAO::ctouch($username);
  337. }
  338. #[Deprecated('Use Minz_Request::connectionRemoteAddress() instead.')]
  339. function connectionRemoteAddress(): string {
  340. return Minz_Request::connectionRemoteAddress();
  341. }
  342. #[Deprecated('Use FreshRSS_http_Util::checkTrustedIP() instead.')]
  343. function checkTrustedIP(): bool {
  344. return FreshRSS_http_Util::checkTrustedIP();
  345. }
  346. /**
  347. * Remove a directory recursively.
  348. * From https://www.php.net/rmdir#110489
  349. */
  350. function recursive_unlink(string $dir): bool {
  351. if (!is_dir($dir)) {
  352. return true;
  353. }
  354. if (is_link($dir)) {
  355. if (PHP_OS_FAMILY === "Windows") {
  356. return rmdir($dir);
  357. }
  358. return unlink($dir);
  359. }
  360. $files = array_diff(scandir($dir) ?: [], ['.', '..']);
  361. foreach ($files as $filename) {
  362. $filename = $dir . '/' . $filename;
  363. if (is_dir($filename)) {
  364. @chmod($filename, 0777);
  365. recursive_unlink($filename);
  366. } else {
  367. unlink($filename);
  368. }
  369. }
  370. return rmdir($dir);
  371. }
  372. function _i(string $icon, int $type = FreshRSS_Themes::ICON_DEFAULT): string {
  373. return FreshRSS_Themes::icon($icon, $type);
  374. }
  375. function errorMessageInfo(string $errorTitle, string $error = ''): string {
  376. $errorTitle = htmlspecialchars($errorTitle, ENT_NOQUOTES, 'UTF-8');
  377. $message = '';
  378. $details = '';
  379. $error = trim($error);
  380. // Prevent empty tags by checking if error is not empty first
  381. if ($error !== '') {
  382. $error = htmlspecialchars($error, ENT_NOQUOTES, 'UTF-8') . "\n";
  383. // First line is the main message, other lines are the details
  384. list($message, $details) = explode("\n", $error, 2);
  385. $message = "<h2>{$message}</h2>";
  386. $details = "<pre>{$details}</pre>";
  387. }
  388. header("Content-Security-Policy: default-src 'self'; frame-ancestors " .
  389. (FreshRSS_Context::systemConf()->attributeString('csp.frame-ancestors') ?? "'none'"));
  390. header('Referrer-Policy: same-origin');
  391. return <<<MSG
  392. <!DOCTYPE html><html><header><title>HTTP 500: {$errorTitle}</title></header><body>
  393. <h1>HTTP 500: {$errorTitle}</h1>
  394. {$message}
  395. {$details}
  396. <hr />
  397. <small>For help see the documentation: <a href="https://freshrss.github.io/FreshRSS/en/admins/logs_and_errors.html" target="_blank">
  398. https://freshrss.github.io/FreshRSS/en/admins/logs_and_errors.html</a></small>
  399. </body></html>
  400. MSG;
  401. }