_common.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. // Shared code of the crash-report endpoints (crash.php, reports.php). Not an endpoint itself (.htaccess denies it).
  3. // LYNX (AGPL-3.0-or-later). Reports are opt-in, anonymised by the app before sending (docs: PRIVACY.md).
  4. declare(strict_types=1);
  5. if (basename($_SERVER['SCRIPT_FILENAME'] ?? '') === basename(__FILE__)) {
  6. http_response_code(404);
  7. exit;
  8. }
  9. const LYNX_MAX_GZIP = 1048576; // 1 MB compressed body
  10. const LYNX_MAX_RAW = 4194304; // 4 MB decoded JSON
  11. const LYNX_PER_IP_PER_DAY = 30; // reports per (hashed) address per day
  12. const LYNX_PER_DAY = 3000; // all reports per day
  13. const LYNX_MAX_PER_MONTH = 50000;
  14. // Reports live OUTSIDE public_html: a site redeploy cannot touch them and the web server cannot serve them.
  15. // The first candidate PHP can create and write wins; the last one (inside public_html, denied by .htaccess) is only a
  16. // fallback for a host that forbids anything else.
  17. function lynx_storage_dir(): string
  18. {
  19. static $dir = null;
  20. if ($dir !== null) {
  21. return $dir;
  22. }
  23. $candidates = [
  24. dirname(__DIR__, 4) . '/lynx-crash-reports', // the account's home (/home/<user>)
  25. dirname(__DIR__, 2) . '/lynx-crash-reports', // the domain's folder, next to public_html
  26. __DIR__ . '/_storage', // fallback: public_html/api/_storage (Require all denied)
  27. ];
  28. foreach ($candidates as $c) {
  29. if ((is_dir($c) || @mkdir($c, 0700, true)) && is_writable($c)) {
  30. return $dir = $c;
  31. }
  32. }
  33. lynx_fail(503, 'storage unavailable');
  34. }
  35. function lynx_json(int $status, array $body): void
  36. {
  37. http_response_code($status);
  38. header('Content-Type: application/json; charset=utf-8');
  39. header('Cache-Control: no-store');
  40. header('X-Content-Type-Options: nosniff');
  41. echo json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  42. }
  43. function lynx_fail(int $status, string $error): void
  44. {
  45. lynx_json($status, ['ok' => false, 'error' => $error]);
  46. exit;
  47. }
  48. function lynx_valid_id(string $id): bool
  49. {
  50. return (bool) preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $id);
  51. }
  52. // Secret salt for the daily address hashes (created once, never leaves the server)
  53. function lynx_salt(): string
  54. {
  55. $file = lynx_storage_dir() . '/.salt';
  56. $salt = @file_get_contents($file);
  57. if ($salt === false || strlen($salt) < 32) {
  58. $salt = bin2hex(random_bytes(32));
  59. file_put_contents($file, $salt, LOCK_EX);
  60. @chmod($file, 0600);
  61. }
  62. return $salt;
  63. }
  64. // Only fatal errors are logged by the host; nothing is printed to the client.
  65. ini_set('display_errors', '0');
  66. set_exception_handler(function (Throwable $e): void {
  67. error_log('lynx crash api: ' . $e->getMessage());
  68. if (!headers_sent()) {
  69. lynx_json(500, ['ok' => false, 'error' => 'server error']);
  70. }
  71. });