aboutsummaryrefslogtreecommitdiff
path: root/api/debug/config.php
blob: 9abc262f355c3c54d38d0f1e4c0e947f82d329d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
// api/debug/config.php — Shared config for debug backend
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: https://webx11.duckdns.org');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Max-Age: 86400');

// Handle preflight
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(204);
    exit;
}

date_default_timezone_set('UTC');

define('DEBUG_DATA_DIR', __DIR__ . '/../../data/debug');
define('MAX_ERRORS_PER_BATCH', 100);
define('MAX_BATCH_SIZE_KB', 1024);  // raised 64->1024 (2026-10-05): error batches with stacks legitimately exceed 64KB, causing endless 413s
define('CLUSTER_SIMILARITY_THRESHOLD', 0.35);
define('ERRORS_FILE', DEBUG_DATA_DIR . '/errors.json');
define('SESSIONS_FILE', DEBUG_DATA_DIR . '/sessions.json');
define('CLUSTERS_FILE', DEBUG_DATA_DIR . '/clusters.json');

if (!is_dir(DEBUG_DATA_DIR)) {
    mkdir(DEBUG_DATA_DIR, 0755, true);
}

function load_json($path) {
    if (!file_exists($path)) return [];
    $data = json_decode(file_get_contents($path), true);
    return is_array($data) ? $data : [];
}

function save_json($path, $data) {
    $encoded = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    $fp = fopen($path, 'c');
    if ($fp && flock($fp, LOCK_EX)) {
        ftruncate($fp, 0);
        fwrite($fp, $encoded);
        fflush($fp);
        flock($fp, LOCK_UN);
        fclose($fp);
    } else {
        // Fallback: write without lock
        file_put_contents($path, $encoded);
    }
    @chmod($path, 0664);
}

function json_response($data, $code = 200) {
    http_response_code($code);
    echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    exit;
}

function error_response($msg, $code = 400) {
    json_response(['success' => false, 'error' => $msg], $code);
}