false, 'error' => 'Database connection failed.']); exit; } if ($endpoint === 'activate') { $key = strtoupper(trim($input['license_key'] ?? '')); $fingerprint = $input['fingerprint'] ?? ''; $domain = $input['domain'] ?? ''; $product = $input['product'] ?? ''; if (empty($key) || empty($fingerprint)) { echo json_encode(['success' => false, 'error' => 'Missing required parameters.']); exit; } // 1. Find License $stmt = $pdo->prepare("SELECT * FROM licenses WHERE license_key = ? LIMIT 1"); $stmt->execute([$key]); $license = $stmt->fetch(); if (!$license) { echo json_encode(['success' => false, 'error' => 'Invalid license key.']); exit; } if ($license['status'] !== 'active') { echo json_encode(['success' => false, 'error' => 'License is ' . $license['status'] . '.']); exit; } // 2. Check current activations $stmt = $pdo->prepare("SELECT COUNT(*) FROM activations WHERE license_id = ?"); $stmt->execute([$license['id']]); $current_activations = $stmt->fetchColumn(); // 3. Check if this machine is already activated $stmt = $pdo->prepare("SELECT * FROM activations WHERE license_id = ? AND fingerprint = ?"); $stmt->execute([$license['id'], $fingerprint]); $existing = $stmt->fetch(); if (!$existing) { if ($current_activations >= $license['max_activations']) { echo json_encode(['success' => false, 'error' => 'Maximum activation limit reached.']); exit; } // Record new activation $stmt = $pdo->prepare("INSERT INTO activations (license_id, fingerprint, domain, product) VALUES (?, ?, ?, ?)"); $stmt->execute([$license['id'], $fingerprint, $domain, $product]); } // Success: Return signed token $token = hash_hmac('sha256', $key . $fingerprint, SERVER_SECRET); echo json_encode([ 'success' => true, 'activation_token' => $token ]); exit; } if ($endpoint === 'verify') { $key = strtoupper(trim($input['license_key'] ?? '')); $fingerprint = $input['fingerprint'] ?? ''; $token = $input['token'] ?? ''; // Simple validation: re-calculate token and check DB status $expected_token = hash_hmac('sha256', $key . $fingerprint, SERVER_SECRET); if ($token !== $expected_token) { echo json_encode(['success' => false, 'error' => 'Invalid activation token.']); exit; } $stmt = $pdo->prepare("SELECT status FROM licenses WHERE license_key = ?"); $stmt->execute([$key]); $status = $stmt->fetchColumn(); if ($status === 'active') { echo json_encode(['success' => true]); } else { echo json_encode(['success' => false, 'error' => 'License is no longer active.']); } exit; } echo json_encode(['success' => false, 'error' => 'Invalid endpoint.']);