lib_rss.php 13 KB

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