) dirname(__DIR__, 2) . '/lynx-crash-reports', // the domain's folder, next to public_html __DIR__ . '/_storage', // fallback: public_html/api/_storage (Require all denied) ]; foreach ($candidates as $c) { if ((is_dir($c) || @mkdir($c, 0700, true)) && is_writable($c)) { return $dir = $c; } } lynx_fail(503, 'storage unavailable'); } function lynx_json(int $status, array $body): void { http_response_code($status); header('Content-Type: application/json; charset=utf-8'); header('Cache-Control: no-store'); header('X-Content-Type-Options: nosniff'); echo json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } function lynx_fail(int $status, string $error): void { lynx_json($status, ['ok' => false, 'error' => $error]); exit; } function lynx_valid_id(string $id): bool { 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); } // Secret salt for the daily address hashes (created once, never leaves the server) function lynx_salt(): string { $file = lynx_storage_dir() . '/.salt'; $salt = @file_get_contents($file); if ($salt === false || strlen($salt) < 32) { $salt = bin2hex(random_bytes(32)); file_put_contents($file, $salt, LOCK_EX); @chmod($file, 0600); } return $salt; } // Only fatal errors are logged by the host; nothing is printed to the client. ini_set('display_errors', '0'); set_exception_handler(function (Throwable $e): void { error_log('lynx crash api: ' . $e->getMessage()); if (!headers_sent()) { lynx_json(500, ['ok' => false, 'error' => 'server error']); } });