Files
mini-cms/forum-project/add_article.php
2025-11-01 16:42:38 +01:00

162 lines
4.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = trim($_POST['title'] ?? '');
$content = trim($_POST['content'] ?? '');
$author_id = $_SESSION['user_id'] ?? null;
if (!$author_id) {
echo "❌ Erreur : utilisateur non connecté.";
exit;
}
$imageUrl = null;
global $s3Client, $bucketName;
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$fileName = time() . '_' . basename($_FILES['image']['name']);
$tmpPath = $_FILES['image']['tmp_name'];
try {
$s3Client->putObject([
'Bucket' => $bucketName,
'Key' => $fileName,
'SourceFile' => $tmpPath,
'ACL' => 'public-read'
]);
$imageUrl = "http://localhost:9000/$bucketName/$fileName";
} catch (Exception $e) {
echo "❌ Erreur upload MinIO : " . $e->getMessage();
exit;
}
}
try {
$stmt = $pdo->prepare("INSERT INTO posts (title, content, image_url, user_id, date_creation)
VALUES (?, ?, ?, ?, NOW())");
$stmt->execute([$title, $content, $imageUrl, $author_id]);
header("Location: dashboard.php");
exit;
} catch (PDOException $e) {
echo "❌ Erreur base de données : " . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>📝 Publier un article</title>
<style>
body {
font-family: "Poppins", sans-serif;
background: linear-gradient(120deg, #ffe0ec, #fff6f8);
color: #333;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 80px auto;
background: white;
padding: 40px;
border-radius: 15px;
box-shadow: 0 6px 20px rgba(255, 100, 150, 0.2);
text-align: center;
}
h2 {
color: #ff6f91;
margin-bottom: 25px;
font-size: 1.8em;
}
form {
display: flex;
flex-direction: column;
gap: 15px;
}
label {
text-align: left;
font-weight: 600;
color: #444;
}
input[type="text"], textarea {
padding: 10px 15px;
border: 1px solid #ddd;
border-radius: 8px;
font-size: 1em;
resize: vertical;
}
input[type="file"] {
border: none;
font-size: 0.9em;
}
button {
background: linear-gradient(45deg, #ff6f91, #ff9671, #ffc75f);
border: none;
color: white;
font-weight: bold;
padding: 12px;
border-radius: 50px;
cursor: pointer;
transition: 0.3s ease;
font-size: 1em;
margin-top: 10px;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(255, 105, 135, 0.4);
}
a.retour {
display: inline-block;
margin-top: 20px;
text-decoration: none;
color: #ff6f91;
font-weight: bold;
}
a.retour:hover {
text-decoration: underline;
}
footer {
text-align: center;
color: #999;
margin-top: 30px;
}
</style>
</head>
<body>
<div class="container">
<h2>📝 Publier un nouvel article</h2>
<form method="POST" enctype="multipart/form-data">
<label for="title">Titre :</label>
<input type="text" id="title" name="title" required>
<label for="content">Contenu :</label>
<textarea id="content" name="content" rows="6" required></textarea>
<label for="image">Image :</label>
<input type="file" id="image" name="image" accept="image/png, image/jpeg">
<button type="submit">Publier larticle</button>
</form>
<a href="dashboard.php" class="retour">⬅ Retour au tableau de bord</a>
</div>
<footer>🕒 <?= date('Y') ?> — Mini CMS by Aya 💖</footer>
</body>
</html>