lib_rss.php 13 KB

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