PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT => 3]); if (!$sid) return false; $ip = $_SERVER['REMOTE_ADDR'] ?? ''; $ua = substr($_SERVER['HTTP_USER_AGENT'] ?? '', 0, 240); $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '', 0, 20); $device = (stripos($ua, 'Mobile') !== false) ? 'mobile' : 'desktop'; $browser = 'other'; if (stripos($ua, 'Chrome') !== false) $browser = 'chrome'; elseif (stripos($ua, 'Firefox') !== false) $browser = 'firefox'; elseif (stripos($ua, 'Safari') !== false) $browser = 'safari'; elseif (stripos($ua, 'Edge') !== false) $browser = 'edge'; /* find existing conversation or create */ $stmt = $pdo->prepare("SELECT id FROM public.conversations WHERE session_id=? ORDER BY updated_at DESC LIMIT 1"); $stmt->execute([$sid]); $cid = $stmt->fetchColumn(); if (!$cid) { $stmt = $pdo->prepare("INSERT INTO public.conversations (session_id, title, ip_address, user_agent, device, browser, language, source) VALUES (?,?,?,?,?,?,?,?) RETURNING id"); $stmt->execute([$sid, mb_substr($title ?: '(sans titre)', 0, 200), $ip, $ua, $device, $browser, $lang, $source]); $cid = $stmt->fetchColumn(); } else { $pdo->prepare("UPDATE public.conversations SET updated_at=NOW(), source=COALESCE(source,?) WHERE id=?")->execute([$source, $cid]); } if ($cid) { if ($user_msg !== '') $pdo->prepare("INSERT INTO public.messages (conversation_id, role, content) VALUES (?,?,?)")->execute([$cid, 'user', mb_substr($user_msg, 0, 8000)]); if ($assistant_msg !== '') $pdo->prepare("INSERT INTO public.messages (conversation_id, role, content) VALUES (?,?,?)")->execute([$cid, 'assistant', mb_substr($assistant_msg, 0, 32000)]); } return true; } catch (Throwable $e) { error_log("WEVIA_LOG_V137 fail: ".$e->getMessage()); return false; } } } require_once __DIR__ . '/_secrets.php'; error_reporting(E_ALL);ini_set("display_errors",0); header('Content-Type: application/json'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Headers: Content-Type'); if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') exit; if ($_SERVER['REQUEST_METHOD'] !== 'POST') die(json_encode(['error'=>'POST only'])); $data = json_decode(file_get_contents('php://input'), true); $email = filter_var($data['email'] ?? '', FILTER_VALIDATE_EMAIL); $name = substr($data['name'] ?? '', 0, 100); $form_id = substr($data['form_id'] ?? 'default', 0, 50); /* V142 early-log: trace ALL form submits including validation failures */ try { $__v142_sid = "form-" . ($data["form_id"] ?? "unknown") . "-" . substr(md5(($data["email"] ?? "") . ($_SERVER["REMOTE_ADDR"] ?? "")), 0, 12); $__v142_title = "Form " . ($data["form_id"] ?? "?") . " · " . ($data["email"] ?? "anon"); $__v142_msg = "name=" . ($data["name"] ?? "") . " email=" . ($data["email"] ?? "(invalid)") . " msg=" . substr($data["message"] ?? "", 0, 500); @wevia_log_session_v137($__v142_sid, $__v142_title, $__v142_msg, "", "form-inline"); } catch (Throwable $__e_v142) { /* silent */ } if (!$email) die(json_encode(['error'=>'Invalid email'])); $db = new PDO('pgsql:host=10.1.0.3;port=5432;dbname=adx_system','admin',weval_secret('WEVAL_PG_ADMIN_PASS')); $db->exec("SET search_path TO admin"); try { $db->exec("CREATE TABLE IF NOT EXISTS form_submissions (id SERIAL PRIMARY KEY, form_id TEXT, email TEXT, name TEXT, ip TEXT, created_at TIMESTAMP DEFAULT NOW())"); $db->prepare("INSERT INTO form_submissions (form_id, email, name, ip) VALUES (?, ?, ?, ?)") ->execute([$form_id, $email, $name, $_SERVER['REMOTE_ADDR'] ?? '']); // Also add to send_contacts if not exists $exists = $db->prepare("SELECT COUNT(*) FROM send_contacts WHERE email = ?"); $exists->execute([$email]); if ($exists->fetchColumn() == 0) { $db->prepare("INSERT INTO send_contacts (email, first_name, status, source, score) VALUES (?, ?, 'active', ?, 100)") ->execute([$email, $name, 'form_' . $form_id]); } echo json_encode(['ok'=>1, 'message'=>'Subscribed']); } catch (Exception $e) { echo json_encode(['error'=>'Server error']); } /* V137: log form submission to unified sessions */ try { $__form_sid = "form-" . ($_POST["form_id"] ?? "unknown") . "-" . substr(md5($_POST["email"] ?? $_SERVER["REMOTE_ADDR"] ?? ""), 0, 12); $__form_title = "Form " . ($_POST["form_id"] ?? "?") . " · " . ($_POST["email"] ?? "anon"); $__form_msg = "name=" . ($_POST["name"] ?? "") . " email=" . ($_POST["email"] ?? "") . " msg=" . substr($_POST["message"] ?? "", 0, 500); @wevia_log_session_v137($__form_sid, $__form_title, $__form_msg, "", "form-inline"); } catch (Throwable $__e) { /* silent */ }