PDO::ERRMODE_EXCEPTION]); function j($ok, $extra=[]) { echo json_encode(array_merge(['ok'=>$ok], $extra)); exit; } function now() { return date('Y-m-d H:i:s'); } function row($pdo,$sql,$p){ $st=$pdo->prepare($sql); $st->execute($p); return $st->fetch(PDO::FETCH_ASSOC); } // activate.php require 'db.php'; $in = json_decode(file_get_contents('php://input'), true) ?: []; $key = trim($in['key'] ?? ''); $dev = trim($in['device_id'] ?? ''); if (!$key || !$dev) j(false,['message'=>'bad request']); $lic = row($pdo,'SELECT * FROM licenses WHERE license_key=?',[$key]); if (!$lic || $lic['revoked']) j(false,['message'=>'invalid']); if ($lic['expires_at'] && strtotime($lic['expires_at']) < time()) j(false,['message'=>'expired']); $cnt = row($pdo,'SELECT COUNT(*) c FROM license_activations WHERE license_key=?',[$key])['c']; $has = row($pdo,'SELECT 1 FROM license_activations WHERE license_key=? AND device_id=?',[$key,$dev]); if (!$has && $cnt >= (int)$lic['max_devices']) j(false,['message'=>'device limit reached']); // upsert activation $pdo->prepare('INSERT INTO license_activations(license_key,device_id,activated_at,last_seen_at) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE last_seen_at=VALUES(last_seen_at)') ->execute([$key,$dev,now(),now()]); j(true,['plan'=>$lic['plan'],'expires_at'=>$lic['expires_at']]); // deactivate.php require 'db.php'; $in = json_decode(file_get_contents('php://input'), true) ?: []; $key = trim($in['key'] ?? ''); $dev = trim($in['device_id'] ?? ''); if (!$key || !$dev) j(false,['message'=>'bad request']); $pdo->prepare('DELETE FROM license_activations WHERE license_key=? AND device_id=?')->execute([$key,$dev]); j(true); // verify.php require 'db.php'; $in = json_decode(file_get_contents('php://input'), true) ?: []; $key = trim($in['key'] ?? ''); $dev = trim($in['device_id'] ?? ''); if (!$key || !$dev) j(false,['ok'=>false]); $lic = row($pdo,'SELECT * FROM licenses WHERE license_key=?',[$key]); if (!$lic || $lic['revoked']) j(false); if ($lic['expires_at'] && strtotime($lic['expires_at']) < time()) j(false); $has = row($pdo,'SELECT 1 FROM license_activations WHERE license_key=? AND device_id=?',[$key,$dev]); if (!$has) j(false); $pdo->prepare('UPDATE license_activations SET last_seen_at=? WHERE license_key=? AND device_id=?')->execute([now(),$key,$dev]); j(true,['plan'=>$lic['plan'],'expires_at'=>$lic['expires_at']]);