Files
wevads-platform/scripts/api_sandbox-decoy.php
2026-02-26 04:53:11 +01:00

188 lines
6.6 KiB
PHP
Executable File

<?php
require_once("/opt/wevads/config/credentials.php");
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$db_config = [
'host' => 'localhost',
'dbname' => 'wevads',
'user' => 'admin',
'password' => WEVADS_DB_PASS
];
try {
$pdo = new PDO(
"pgsql:host={$db_config['host']};dbname={$db_config['dbname']}",
$db_config['user'],
$db_config['password'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
$pdo->exec("SET search_path TO admin, public;");
$action = $_GET['action'] ?? 'visits';
switch ($action) {
case 'visits':
$stmt = $pdo->query("
SELECT * FROM admin.decoy_visits
ORDER BY created_at DESC
LIMIT 40
");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode([
'status' => 'success',
'data' => $data,
'count' => count($data)
]);
break;
case 'stats':
$stats = $pdo->query("
SELECT
COUNT(*) as total_visits,
SUM(CASE WHEN is_bot = true THEN 1 ELSE 0 END) as bots,
SUM(CASE WHEN is_bot = false THEN 1 ELSE 0 END) as humans,
SUM(CASE WHEN decoy_shown = true THEN 1 ELSE 0 END) as decoys_shown,
SUM(CASE WHEN real_page_shown = true THEN 1 ELSE 0 END) as real_pages_shown
FROM admin.decoy_visits
")->fetch(PDO::FETCH_ASSOC);
echo json_encode([
'status' => 'success',
'stats' => $stats,
'bot_rate' => round(($stats['bots'] / max(1, $stats['total_visits'])) * 100, 2) . '%'
]);
break;
case 'pages':
$stmt = $pdo->query("
SELECT * FROM admin.decoy_pages
WHERE active = true
ORDER BY created_at DESC
");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode([
'status' => 'success',
'pages' => $data,
'categories' => array_values(array_unique(array_column($data, 'category')))
]);
break;
case 'create_page':
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
$stmt = $pdo->prepare("
INSERT INTO admin.decoy_pages (name, html_content, category, active)
VALUES (:name, :content, :category, true)
RETURNING id
");
$stmt->execute([
':name' => $input['name'] ?? 'New Page',
':content' => $input['html_content'] ?? '<html></html>',
':category' => $input['category'] ?? 'blog'
]);
$id = $stmt->fetchColumn();
echo json_encode([
'status' => 'success',
'message' => 'Page créée',
'page_id' => $id
]);
}
break;
case 'check':
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
$user_agent = $input['user_agent'] ?? $_SERVER['HTTP_USER_AGENT'] ?? '';
$ip = $input['ip'] ?? $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
// Détection de bot simple
$is_bot = false;
$bot_type = null;
$confidence = 0;
$bot_patterns = [
'Googlebot' => 95,
'bingbot' => 90,
'YandexBot' => 85,
'facebook' => 80,
'Twitter' => 75,
'bot' => 70,
'crawler' => 65,
'spider' => 60
];
foreach ($bot_patterns as $pattern => $conf) {
if (stripos($user_agent, $pattern) !== false) {
$is_bot = true;
$bot_type = $pattern;
$confidence = $conf;
break;
}
}
// Enregistrer la visite
$stmt = $pdo->prepare("
INSERT INTO admin.decoy_visits
(url, visitor_ip, user_agent, is_bot, bot_type, decoy_shown, real_page_shown)
VALUES (:url, :ip, :ua, :is_bot, :bot_type, :decoy_shown, :real_shown)
");
$show_decoy = $is_bot;
$stmt->execute([
':url' => $input['url'] ?? '/',
':ip' => $ip,
':ua' => $user_agent,
':is_bot' => $is_bot,
':bot_type' => $bot_type,
':decoy_shown' => $show_decoy,
':real_shown' => !$show_decoy
]);
echo json_encode([
'status' => 'success',
'is_bot' => $is_bot,
'bot_type' => $bot_type,
'confidence' => $confidence,
'action' => $show_decoy ? 'decoy_shown' : 'real_page_shown',
'user_agent' => substr($user_agent, 0, 100)
]);
}
break;
default:
echo json_encode([
'status' => 'error',
'message' => 'Action non reconnue',
'available_actions' => [
'GET ?action=visits' => 'Dernières visites',
'GET ?action=stats' => 'Statistiques',
'GET ?action=pages' => 'Pages leurres',
'POST ?action=create_page' => 'Créer une page',
'POST ?action=check' => 'Vérifier visiteur'
]
]);
}
} catch (PDOException $e) {
echo json_encode([
'status' => 'error',
'message' => 'Erreur base de données: ' . $e->getMessage(),
'fallback' => [
'status' => 'decoy_fallback',
'visits' => [
['visitor_ip' => '127.0.0.1', 'is_bot' => false, 'decoy_shown' => false]
]
]
]);
}
?>