All checks were successful
Deploy / Deploy to Production (push) Successful in 22s
- Remplace le message inline par un écran centré (smiley + heading + signature) - Ajout clés i18n wp_success_heading / wp_success_sub - Ajustements blueprints white-paper / white-papers / site - Route : stockage leads sur page livres-blancs Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
93 lines
3.5 KiB
PHP
93 lines
3.5 KiB
PHP
<?php
|
|
|
|
function wpReject(int $code, string $message): void {
|
|
http_response_code($code);
|
|
header('Content-Type: application/json');
|
|
echo json_encode(['error' => $message]);
|
|
exit;
|
|
}
|
|
|
|
return [
|
|
'pattern' => ['(:any)/(:any)/download', 'en/(:any)/(:any)/download'],
|
|
'method' => 'POST',
|
|
'action' => function (string $parent, string $slug) {
|
|
|
|
$page = page($parent . '/' . $slug);
|
|
if (!$page || $page->intendedTemplate()->name() !== 'white-paper') {
|
|
wpReject(404, 'Not found');
|
|
}
|
|
|
|
$body = kirby()->request()->body()->toArray();
|
|
|
|
// ── Honeypot ──────────────────────────────────────────────
|
|
if (!empty($body['_hp'])) {
|
|
wpReject(400, 'Bad request');
|
|
}
|
|
|
|
// ── Rate limiting (5 req / hour / IP) ─────────────────────
|
|
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? 'unknown';
|
|
$cacheKey = 'wp-dl-' . md5($ip);
|
|
$cache = kirby()->cache('pages');
|
|
$hits = (int)($cache->get($cacheKey) ?? 0);
|
|
if ($hits >= 5) {
|
|
wpReject(429, 'Too many requests');
|
|
}
|
|
$cache->set($cacheKey, $hits + 1, 60); // TTL 60 min
|
|
|
|
// ── Validation des champs requis ──────────────────────────
|
|
$firstName = trim($body['firstName'] ?? '');
|
|
$lastName = trim($body['lastName'] ?? '');
|
|
$email = trim($body['email'] ?? '');
|
|
|
|
if ($firstName === '' || $lastName === '' || $email === '') {
|
|
wpReject(422, 'Missing required fields');
|
|
}
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
wpReject(422, 'Invalid email');
|
|
}
|
|
|
|
// ── Stocker le lead dans contactDatabase ──────────────────
|
|
$company = trim($body['company'] ?? '');
|
|
$role = trim($body['role'] ?? '');
|
|
$entries = $page->contactDatabase()->toStructure()->toArray();
|
|
|
|
$existingIndex = null;
|
|
foreach ($entries as $i => $entry) {
|
|
if (strtolower($entry['email'] ?? '') === strtolower($email)) {
|
|
$existingIndex = $i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($existingIndex !== null) {
|
|
// Contact déjà présent — on enrichit les champs vides uniquement
|
|
if ($company !== '' && empty($entries[$existingIndex]['company'])) {
|
|
$entries[$existingIndex]['company'] = $company;
|
|
}
|
|
if ($role !== '' && empty($entries[$existingIndex]['role'])) {
|
|
$entries[$existingIndex]['role'] = $role;
|
|
}
|
|
} else {
|
|
$entries[] = [
|
|
'firstName' => $firstName,
|
|
'lastName' => $lastName,
|
|
'email' => $email,
|
|
'company' => $company,
|
|
'role' => $role,
|
|
'downloadedAt' => date('d/m/Y H:i'),
|
|
];
|
|
}
|
|
|
|
kirby()->impersonate('kirby', function () use ($page, $entries) {
|
|
page('livres-blancs')->update(['contactDatabase' => \Kirby\Data\Data::encode($entries, 'yaml')]);
|
|
});
|
|
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'success' => true,
|
|
'fileUrl' => $page->downloadFile()->toFile()?->url(),
|
|
]);
|
|
exit;
|
|
}
|
|
];
|