4
0

reports.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. // Admin access to the stored crash reports (the LYNX maintainer only).
  3. // Authorization: Bearer <key> (or X-Admin-Key: <key>); only the key's SHA-256 is here.
  4. // GET reports.php?action=list[&month=YYYY-MM] -> {"ok":true,"reports":[{id,month,received,kind,test,version,bytes}]}
  5. // GET reports.php?action=get&id=<id> -> the report (application/gzip, the JSON gzip-compressed)
  6. // POST reports.php?action=delete&id=<id> -> {"ok":true}
  7. declare(strict_types=1);
  8. require __DIR__ . '/_common.php';
  9. const LYNX_ADMIN_KEY_SHA256 = '4fc10d1735ddf65f7449f29db399757092890adfb508acbac372692151c4a9d3';
  10. $key = '';
  11. $auth = $_SERVER['HTTP_AUTHORIZATION'] ?? ($_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ?? '');
  12. if (preg_match('/^Bearer\s+(\S+)$/', (string) $auth, $m)) {
  13. $key = $m[1];
  14. } elseif (!empty($_SERVER['HTTP_X_ADMIN_KEY'])) {
  15. $key = (string) $_SERVER['HTTP_X_ADMIN_KEY'];
  16. }
  17. if ($key === '' || !hash_equals(LYNX_ADMIN_KEY_SHA256, hash('sha256', $key))) {
  18. usleep(300000);
  19. lynx_fail(401, 'unauthorized');
  20. }
  21. $dir = lynx_storage_dir();
  22. $action = (string) ($_GET['action'] ?? 'list');
  23. function report_file(string $dir, string $id): ?string
  24. {
  25. if (!lynx_valid_id($id)) {
  26. lynx_fail(400, 'bad id');
  27. }
  28. $found = glob($dir . '/[0-9][0-9][0-9][0-9]-[0-9][0-9]/' . $id . '.json.gz') ?: [];
  29. return $found[0] ?? null;
  30. }
  31. switch ($action) {
  32. case 'list':
  33. $month = (string) ($_GET['month'] ?? '');
  34. if ($month !== '' && !preg_match('/^\d{4}-\d{2}$/', $month)) {
  35. lynx_fail(400, 'bad month');
  36. }
  37. $list = [];
  38. $seen = [];
  39. $index = @file($dir . '/index.jsonl', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [];
  40. foreach ($index as $line) {
  41. $r = json_decode($line, true);
  42. if (!is_array($r) || !lynx_valid_id((string) ($r['id'] ?? '')) || isset($seen[$r['id']])) {
  43. continue;
  44. }
  45. if ($month !== '' && ($r['month'] ?? '') !== $month) {
  46. continue;
  47. }
  48. if (is_file($dir . '/' . $r['month'] . '/' . $r['id'] . '.json.gz')) {
  49. $seen[$r['id']] = true;
  50. $list[] = $r;
  51. }
  52. }
  53. lynx_json(200, ['ok' => true, 'reports' => $list]);
  54. break;
  55. case 'get':
  56. $file = report_file($dir, (string) ($_GET['id'] ?? ''));
  57. if ($file === null) {
  58. lynx_fail(404, 'not found');
  59. }
  60. header('Content-Type: application/gzip');
  61. header('Cache-Control: no-store');
  62. header('Content-Length: ' . filesize($file));
  63. readfile($file);
  64. break;
  65. case 'delete':
  66. if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
  67. lynx_fail(405, 'POST only');
  68. }
  69. $file = report_file($dir, (string) ($_GET['id'] ?? ''));
  70. if ($file === null) {
  71. lynx_fail(404, 'not found');
  72. }
  73. unlink($file);
  74. lynx_json(200, ['ok' => true]);
  75. break;
  76. case 'where': // the storage folder (to check it is outside public_html)
  77. lynx_json(200, ['ok' => true, 'storage' => $dir, 'public' => str_starts_with(realpath($dir) ?: $dir, realpath(dirname(__DIR__)) ?: dirname(__DIR__))]);
  78. break;
  79. default:
  80. lynx_fail(400, 'unknown action');
  81. }