468 lines
20 KiB
PHP
468 lines
20 KiB
PHP
<?php
|
|
/**
|
|
* BackOffice - messages de contact et reponses.
|
|
*/
|
|
|
|
require_once __DIR__ . '/../includes/Security.php';
|
|
require_once __DIR__ . '/../config/database.php';
|
|
require_once __DIR__ . '/../config/smtp.php';
|
|
require_once __DIR__ . '/../includes/Mailer.php';
|
|
|
|
Security::bootstrap(true, ['no_store' => true]);
|
|
Security::requireAdmin();
|
|
|
|
$db = getDatabase();
|
|
|
|
$db->exec("CREATE TABLE IF NOT EXISTS message_replies (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
message_id INT NOT NULL,
|
|
reply_text TEXT NOT NULL,
|
|
direction ENUM('out','in') NOT NULL DEFAULT 'out',
|
|
sender_email VARCHAR(255) DEFAULT NULL,
|
|
message_id_sent VARCHAR(255) DEFAULT NULL,
|
|
sent_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
foreach ([
|
|
"ALTER TABLE message_replies ADD COLUMN direction ENUM('out','in') NOT NULL DEFAULT 'out'",
|
|
"ALTER TABLE message_replies ADD COLUMN sender_email VARCHAR(255) DEFAULT NULL",
|
|
"ALTER TABLE message_replies ADD COLUMN message_id_sent VARCHAR(255) DEFAULT NULL",
|
|
] as $alterSql) {
|
|
try {
|
|
$db->exec($alterSql);
|
|
} catch (Throwable) {
|
|
}
|
|
}
|
|
|
|
$db->exec("CREATE TABLE IF NOT EXISTS sent_message_ids (
|
|
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
|
message_id VARCHAR(255) NOT NULL,
|
|
contact_message_id INT UNSIGNED NOT NULL,
|
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE KEY uk_message_id (message_id),
|
|
INDEX idx_smid_contact (contact_message_id)
|
|
) ENGINE=InnoDB");
|
|
|
|
$feedback = '';
|
|
$feedbackType = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'], $_POST['msg_id'])) {
|
|
Security::verifyCsrf($_POST['csrf_token'] ?? '');
|
|
|
|
$action = (string) $_POST['action'];
|
|
$msgId = (int) $_POST['msg_id'];
|
|
|
|
if ($msgId > 0) {
|
|
if ($action === 'mark_read') {
|
|
$db->prepare('UPDATE contact_messages SET is_read = 1 WHERE id = :id')->execute(['id' => $msgId]);
|
|
} elseif ($action === 'mark_unread') {
|
|
$db->prepare('UPDATE contact_messages SET is_read = 0 WHERE id = :id')->execute(['id' => $msgId]);
|
|
} elseif ($action === 'delete') {
|
|
$db->prepare('DELETE FROM contact_messages WHERE id = :id')->execute(['id' => $msgId]);
|
|
$db->prepare('DELETE FROM message_replies WHERE message_id = :id')->execute(['id' => $msgId]);
|
|
$feedback = 'Message supprime.';
|
|
$feedbackType = 'success';
|
|
} elseif ($action === 'reply') {
|
|
$replyText = trim((string) ($_POST['reply_text'] ?? ''));
|
|
|
|
if ($replyText === '') {
|
|
$feedback = 'La reponse ne peut pas etre vide.';
|
|
$feedbackType = 'error';
|
|
} else {
|
|
$messageStmt = $db->prepare('SELECT * FROM contact_messages WHERE id = :id LIMIT 1');
|
|
$messageStmt->execute(['id' => $msgId]);
|
|
$message = $messageStmt->fetch();
|
|
|
|
if ($message) {
|
|
$db->prepare('UPDATE contact_messages SET is_read = 1 WHERE id = :id')->execute(['id' => $msgId]);
|
|
|
|
$repliesStmt = $db->prepare(
|
|
'SELECT * FROM message_replies WHERE message_id = :id ORDER BY sent_at ASC'
|
|
);
|
|
$repliesStmt->execute(['id' => $msgId]);
|
|
$previousReplies = $repliesStmt->fetchAll();
|
|
|
|
$subject = 'Re: ' . ($message['subject'] ?: 'Votre message');
|
|
$bodyHtml = buildReplyHtml(
|
|
(string) $message['sender_name'],
|
|
$replyText,
|
|
$message,
|
|
$previousReplies
|
|
);
|
|
$bodyText = buildReplyText(
|
|
(string) $message['sender_name'],
|
|
$replyText,
|
|
$message,
|
|
$previousReplies
|
|
);
|
|
|
|
$mailer = new Mailer();
|
|
$sent = $mailer->sendHtml(
|
|
(string) $message['sender_email'],
|
|
$subject,
|
|
$bodyHtml,
|
|
$bodyText,
|
|
NOTIFY_EMAIL
|
|
);
|
|
|
|
$replyMsgId = $mailer->getLastMessageId();
|
|
$db->prepare(
|
|
'INSERT INTO message_replies (message_id, reply_text, direction, message_id_sent)
|
|
VALUES (:message_id, :reply_text, :direction, :message_id_sent)'
|
|
)->execute([
|
|
'message_id' => $msgId,
|
|
'reply_text' => $replyText,
|
|
'direction' => 'out',
|
|
'message_id_sent' => $replyMsgId !== '' ? $replyMsgId : null,
|
|
]);
|
|
|
|
if ($replyMsgId !== '') {
|
|
try {
|
|
$db->prepare(
|
|
'INSERT IGNORE INTO sent_message_ids (message_id, contact_message_id)
|
|
VALUES (:message_id, :contact_message_id)'
|
|
)->execute([
|
|
'message_id' => $replyMsgId,
|
|
'contact_message_id' => $msgId,
|
|
]);
|
|
} catch (Throwable) {
|
|
}
|
|
}
|
|
|
|
$feedback = $sent
|
|
? 'Reponse envoyee et enregistree.'
|
|
: 'Reponse enregistree, mais l\'envoi email a echoue.';
|
|
$feedbackType = $sent ? 'success' : 'warning';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$filter = (string) ($_GET['filter'] ?? 'all');
|
|
$where = match ($filter) {
|
|
'unread' => 'WHERE is_read = 0',
|
|
'read' => 'WHERE is_read = 1',
|
|
default => '',
|
|
};
|
|
|
|
$messages = $db->query("SELECT * FROM contact_messages {$where} ORDER BY created_at DESC")->fetchAll();
|
|
$unreadCount = (int) $db->query('SELECT COUNT(*) FROM contact_messages WHERE is_read = 0')->fetchColumn();
|
|
$totalCount = (int) $db->query('SELECT COUNT(*) FROM contact_messages')->fetchColumn();
|
|
|
|
$repliesMap = [];
|
|
$allReplies = $db->query('SELECT * FROM message_replies ORDER BY sent_at ASC')->fetchAll();
|
|
foreach ($allReplies as $reply) {
|
|
$repliesMap[(int) $reply['message_id']][] = $reply;
|
|
}
|
|
|
|
function buildReplyHtml(string $clientName, string $replyText, array $message, array $previousReplies): string
|
|
{
|
|
$safeClientName = Security::e($clientName);
|
|
$safeReply = nl2br(Security::e($replyText));
|
|
$threadHtml = '';
|
|
|
|
foreach (array_reverse($previousReplies) as $reply) {
|
|
$isIncoming = ($reply['direction'] ?? 'out') === 'in';
|
|
$who = $isIncoming ? $safeClientName : 'Tony AVERTY';
|
|
$date = date('d/m/Y H:i', strtotime($reply['sent_at']));
|
|
$bg = $isIncoming ? '#f0fdf4' : '#eff6ff';
|
|
$border = $isIncoming ? '#16a34a' : '#2563eb';
|
|
$text = nl2br(Security::e((string) $reply['reply_text']));
|
|
|
|
$threadHtml .= <<<HTML
|
|
<tr>
|
|
<td style="padding:0 28px 12px;">
|
|
<div style="padding:14px 16px;background:{$bg};border-left:3px solid {$border};border-radius:0 8px 8px 0;">
|
|
<p style="margin:0 0 8px;font-size:12px;color:#6b7280;"><strong>{$who}</strong> · {$date}</p>
|
|
<div style="font-size:14px;line-height:1.7;color:#1f2937;">{$text}</div>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
HTML;
|
|
}
|
|
|
|
$originalText = nl2br(Security::e((string) $message['message']));
|
|
$originalDate = date('d/m/Y H:i', strtotime($message['created_at']));
|
|
$threadHtml .= <<<HTML
|
|
<tr>
|
|
<td style="padding:0 28px 24px;">
|
|
<div style="padding:14px 16px;background:#f8fafc;border-left:3px solid #94a3b8;border-radius:0 8px 8px 0;">
|
|
<p style="margin:0 0 8px;font-size:12px;color:#6b7280;"><strong>{$safeClientName}</strong> · {$originalDate} (message initial)</p>
|
|
<div style="font-size:14px;line-height:1.7;color:#1f2937;">{$originalText}</div>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
HTML;
|
|
|
|
return <<<HTML
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"></head>
|
|
<body style="margin:0;padding:24px;background:#f4f4f5;font-family:Arial,sans-serif;">
|
|
<table width="100%" cellpadding="0" cellspacing="0">
|
|
<tr>
|
|
<td align="center">
|
|
<table width="620" cellpadding="0" cellspacing="0" style="max-width:620px;width:100%;background:#ffffff;border:1px solid #e5e7eb;border-radius:14px;overflow:hidden;">
|
|
<tr>
|
|
<td style="padding:28px;background:#111827;color:#ffffff;">
|
|
<strong>Tony AVERTY</strong><br>
|
|
<span style="font-size:13px;opacity:.8;">Reponse a votre message</span>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding:28px;color:#111827;">
|
|
<p style="margin:0 0 14px;">Bonjour {$safeClientName},</p>
|
|
<div style="font-size:15px;line-height:1.75;color:#374151;">{$safeReply}</div>
|
|
</td>
|
|
</tr>
|
|
{$threadHtml}
|
|
<tr>
|
|
<td style="padding:18px 28px;background:#f9fafb;border-top:1px solid #e5e7eb;color:#6b7280;font-size:12px;">
|
|
Tony AVERTY · <a href="mailto:contact@tony-averty.dev" style="color:#2563eb;text-decoration:none;">contact@tony-averty.dev</a> · <a href="https://tony-averty.dev" style="color:#2563eb;text-decoration:none;">tony-averty.dev</a>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</body>
|
|
</html>
|
|
HTML;
|
|
}
|
|
|
|
function buildReplyText(string $clientName, string $replyText, array $message, array $previousReplies): string
|
|
{
|
|
$lines = [
|
|
"Bonjour {$clientName},",
|
|
'',
|
|
$replyText,
|
|
'',
|
|
'---',
|
|
'Tony AVERTY - contact@tony-averty.dev - tony-averty.dev',
|
|
'',
|
|
];
|
|
|
|
if (!empty($previousReplies)) {
|
|
$lines[] = 'Conversation precedente';
|
|
$lines[] = '-----------------------';
|
|
|
|
foreach (array_reverse($previousReplies) as $reply) {
|
|
$who = ($reply['direction'] ?? 'out') === 'in' ? $clientName : 'Tony AVERTY';
|
|
$date = date('d/m/Y H:i', strtotime($reply['sent_at']));
|
|
$lines[] = "Le {$date}, {$who} a ecrit :";
|
|
$lines[] = (string) $reply['reply_text'];
|
|
$lines[] = '';
|
|
}
|
|
}
|
|
|
|
$lines[] = "Message initial de {$clientName} :";
|
|
$lines[] = (string) $message['message'];
|
|
|
|
return implode("\n", $lines);
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="robots" content="noindex, nofollow">
|
|
<title>Messages - BackOffice Tony AVERTY</title>
|
|
<link rel="stylesheet" href="assets/admin.css">
|
|
</head>
|
|
<body>
|
|
<div class="admin-page">
|
|
<?php require __DIR__ . '/includes/nav.php'; ?>
|
|
|
|
<main class="admin-content">
|
|
<div class="admin-topbar">
|
|
<h1 class="admin-page-title">
|
|
Messages
|
|
<?php if ($unreadCount > 0): ?>
|
|
<span class="msg-unread-badge"><?= $unreadCount ?></span>
|
|
<?php endif; ?>
|
|
</h1>
|
|
<span class="admin-topbar-sub"><?= $totalCount ?> message(s) au total</span>
|
|
</div>
|
|
|
|
<?php if ($feedback !== ''): ?>
|
|
<div class="admin-alert admin-alert--<?= $feedbackType ?>"><?= Security::e($feedback) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="msg-filters">
|
|
<a href="?filter=all" class="msg-filter <?= $filter === 'all' ? 'is-active' : '' ?>">Tous (<?= $totalCount ?>)</a>
|
|
<a href="?filter=unread" class="msg-filter <?= $filter === 'unread' ? 'is-active' : '' ?>">Non lus (<?= $unreadCount ?>)</a>
|
|
<a href="?filter=read" class="msg-filter <?= $filter === 'read' ? 'is-active' : '' ?>">Lus</a>
|
|
</div>
|
|
|
|
<?php if (empty($messages)): ?>
|
|
<div class="msg-empty">
|
|
<p>Aucun message<?= $filter !== 'all' ? ' dans cette categorie' : '' ?>.</p>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="msg-list">
|
|
<?php foreach ($messages as $message): ?>
|
|
<?php $messageReplies = $repliesMap[(int) $message['id']] ?? []; ?>
|
|
<article class="msg-card <?= $message['is_read'] ? '' : 'is-unread' ?>">
|
|
<div class="msg-card__head">
|
|
<div class="msg-card__meta">
|
|
<span class="msg-card__name"><?= Security::e((string) $message['sender_name']) ?></span>
|
|
<a href="mailto:<?= Security::e((string) $message['sender_email']) ?>" class="msg-card__email">
|
|
<?= Security::e((string) $message['sender_email']) ?>
|
|
</a>
|
|
</div>
|
|
<span class="msg-card__date"><?= date('d/m/Y H:i', strtotime($message['created_at'])) ?></span>
|
|
</div>
|
|
|
|
<?php if (!empty($message['subject'])): ?>
|
|
<div class="msg-card__subject">Sujet : <?= Security::e((string) $message['subject']) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="msg-card__body"><?= nl2br(Security::e((string) $message['message'])) ?></div>
|
|
|
|
<?php if (!empty($messageReplies)): ?>
|
|
<div class="msg-replies">
|
|
<?php foreach ($messageReplies as $reply): ?>
|
|
<?php $incoming = ($reply['direction'] ?? 'out') === 'in'; ?>
|
|
<div class="msg-reply msg-reply--<?= $incoming ? 'in' : 'out' ?>">
|
|
<div class="msg-reply__meta">
|
|
<?= $incoming ? 'Reponse recue' : 'Reponse envoyee' ?>
|
|
<?php if ($incoming && !empty($reply['sender_email'])): ?>
|
|
de <strong><?= Security::e((string) $reply['sender_email']) ?></strong>
|
|
<?php endif; ?>
|
|
le <?= date('d/m/Y H:i', strtotime($reply['sent_at'])) ?>
|
|
</div>
|
|
<div class="msg-reply__text"><?= nl2br(Security::e((string) $reply['reply_text'])) ?></div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="msg-card__actions">
|
|
<button
|
|
type="button"
|
|
class="admin-btn admin-btn--primary admin-btn--sm"
|
|
data-open-reply
|
|
data-message-id="<?= (int) $message['id'] ?>"
|
|
data-sender-name="<?= Security::e((string) $message['sender_name']) ?>"
|
|
data-sender-email="<?= Security::e((string) $message['sender_email']) ?>">
|
|
Repondre
|
|
</button>
|
|
|
|
<?php if (empty($message['is_read'])): ?>
|
|
<form method="post" style="display:inline;">
|
|
<?= Security::csrfInput() ?>
|
|
<input type="hidden" name="msg_id" value="<?= (int) $message['id'] ?>">
|
|
<input type="hidden" name="action" value="mark_read">
|
|
<button type="submit" class="admin-btn admin-btn--outline admin-btn--sm">Marquer lu</button>
|
|
</form>
|
|
<?php else: ?>
|
|
<form method="post" style="display:inline;">
|
|
<?= Security::csrfInput() ?>
|
|
<input type="hidden" name="msg_id" value="<?= (int) $message['id'] ?>">
|
|
<input type="hidden" name="action" value="mark_unread">
|
|
<button type="submit" class="admin-btn admin-btn--outline admin-btn--sm">Marquer non lu</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
|
|
<button
|
|
type="button"
|
|
class="admin-btn admin-btn--danger admin-btn--sm"
|
|
data-delete-message
|
|
data-message-id="<?= (int) $message['id'] ?>"
|
|
data-sender-name="<?= Security::e((string) $message['sender_name']) ?>">
|
|
Supprimer
|
|
</button>
|
|
</div>
|
|
</article>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</main>
|
|
</div>
|
|
|
|
<div class="admin-modal-overlay" id="replyModal">
|
|
<div class="admin-modal admin-modal--wide">
|
|
<h2 class="admin-modal__title">Repondre a <span id="replyName"></span></h2>
|
|
<p class="admin-modal__text" id="replyEmailLabel" style="margin-bottom:1rem;"></p>
|
|
<form method="post" id="replyForm">
|
|
<?= Security::csrfInput() ?>
|
|
<input type="hidden" name="msg_id" id="replyMsgId">
|
|
<input type="hidden" name="action" value="reply">
|
|
<textarea name="reply_text" id="replyText" class="admin-textarea" rows="7" placeholder="Votre reponse..." required></textarea>
|
|
<div class="admin-modal__actions" style="margin-top:1rem;">
|
|
<button type="button" class="admin-btn admin-btn--outline" id="closeReplyModal">Annuler</button>
|
|
<button type="submit" class="admin-btn admin-btn--primary">Envoyer la reponse</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="admin-modal-overlay" id="deleteModal">
|
|
<div class="admin-modal">
|
|
<h2 class="admin-modal__title">Supprimer ce message ?</h2>
|
|
<p class="admin-modal__text">Le message de "<span id="deleteName"></span>" et toutes ses reponses seront supprimes.</p>
|
|
<div class="admin-modal__actions">
|
|
<button type="button" class="admin-btn admin-btn--outline" id="closeDeleteModal">Annuler</button>
|
|
<form method="post" id="deleteForm">
|
|
<?= Security::csrfInput() ?>
|
|
<input type="hidden" name="msg_id" id="deleteMsgId">
|
|
<input type="hidden" name="action" value="delete">
|
|
<button type="submit" class="admin-btn admin-btn--danger">Supprimer</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script nonce="<?= Security::cspNonce() ?>">
|
|
function openReplyModal(id, name, email) {
|
|
document.getElementById('replyMsgId').value = id;
|
|
document.getElementById('replyName').textContent = name;
|
|
document.getElementById('replyEmailLabel').textContent = 'A : ' + email;
|
|
document.getElementById('replyText').value = '';
|
|
document.getElementById('replyModal').classList.add('is-active');
|
|
document.getElementById('replyText').focus();
|
|
}
|
|
|
|
function closeReplyModal() {
|
|
document.getElementById('replyModal').classList.remove('is-active');
|
|
}
|
|
|
|
function confirmDelete(id, name) {
|
|
document.getElementById('deleteMsgId').value = id;
|
|
document.getElementById('deleteName').textContent = name;
|
|
document.getElementById('deleteModal').classList.add('is-active');
|
|
}
|
|
|
|
function closeDeleteModal() {
|
|
document.getElementById('deleteModal').classList.remove('is-active');
|
|
}
|
|
|
|
document.querySelectorAll('[data-open-reply]').forEach(function (button) {
|
|
button.addEventListener('click', function () {
|
|
openReplyModal(button.dataset.messageId, button.dataset.senderName, button.dataset.senderEmail);
|
|
});
|
|
});
|
|
|
|
document.querySelectorAll('[data-delete-message]').forEach(function (button) {
|
|
button.addEventListener('click', function () {
|
|
confirmDelete(button.dataset.messageId, button.dataset.senderName);
|
|
});
|
|
});
|
|
|
|
document.getElementById('closeReplyModal').addEventListener('click', closeReplyModal);
|
|
document.getElementById('closeDeleteModal').addEventListener('click', closeDeleteModal);
|
|
document.getElementById('replyModal').addEventListener('click', function (event) {
|
|
if (event.target === this) {
|
|
closeReplyModal();
|
|
}
|
|
});
|
|
document.getElementById('deleteModal').addEventListener('click', function (event) {
|
|
if (event.target === this) {
|
|
closeDeleteModal();
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|