41 lines
1.7 KiB
PHP
41 lines
1.7 KiB
PHP
<?php
|
|
require_once __DIR__ . '/auth.php';
|
|
$errors = [];
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (!verify_csrf($_POST['csrf'] ?? '')) {
|
|
$errors[] = 'Jeton CSRF invalide.';
|
|
}
|
|
$titre = trim($_POST['titre'] ?? '');
|
|
$contenu = trim($_POST['contenu'] ?? '');
|
|
if (!$titre) $errors[] = 'Le titre est requis.';
|
|
if (!$contenu) $errors[] = 'Le contenu est requis.';
|
|
if (strlen($titre) > 255) $errors[] = 'Le titre est trop long.';
|
|
if (empty($errors)) {
|
|
$stmt = $pdo->prepare('INSERT INTO articles (titre, contenu, date_creation) VALUES (:titre, :contenu, NOW())');
|
|
$stmt->execute(['titre' => $titre, 'contenu' => $contenu]);
|
|
flash_set('success', 'Article ajouté.');
|
|
header('Location: /admin/dashboard.php');
|
|
exit;
|
|
}
|
|
}
|
|
$token = csrf_token();
|
|
?>
|
|
<!doctype html>
|
|
<html><head><meta charset="utf-8"><title>Ajouter un article</title>
|
|
<link rel="stylesheet" href="/public/assets/style.css"></head>
|
|
<body>
|
|
<h1>Ajouter un article</h1>
|
|
<?php if ($errors): ?> <ul class="message error"><?php foreach($errors as $e) echo "<li>".esc($e)."</li>"; ?></ul> <?php endif; ?>
|
|
<form method="post" id="addForm">
|
|
<input type="hidden" name="csrf" value="<?php echo $token; ?>">
|
|
<div><label>Titre<br><input type="text" name="titre" required></label></div>
|
|
<div><label>Contenu<br><textarea name="contenu" rows="10" required></textarea></label></div>
|
|
<button type="submit">Ajouter</button>
|
|
</form>
|
|
<script>
|
|
document.getElementById('addForm').addEventListener('submit', function(e){
|
|
if(!this.titre.value.trim() || !this.contenu.value.trim()){ alert('Remplis tous les champs'); e.preventDefault(); }
|
|
});
|
|
</script>
|
|
</body></html>
|