Files
cmss-projet/forum-project/add_article.php
2025-11-03 21:53:58 +01:00

183 lines
4.9 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
session_start();
require_once 'config.php';
// ✅ Vérification que l'utilisateur est connecté
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['id'])) {
header("Location: login.php");
exit;
}
$author_id = $_SESSION['user']['id'];
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = trim($_POST['title'] ?? '');
$content = trim($_POST['content'] ?? '');
$imageUrl = null;
global $s3Client, $bucketName;
// ✅ Upload de l'image sur MinIO
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) {
$message = "❌ Erreur upload MinIO : " . $e->getMessage();
}
}
// ✅ Insertion de l'article dans la base de données
if ($title && $content) {
try {
$stmt = $pdo->prepare("
INSERT INTO posts (title, content, image_url, user_id, date_creation)
VALUES (?, ?, ?, ?, NOW())
");
$stmt->execute([$title, $content, $imageUrl, $author_id]);
// ✅ Redirection vers la page d'accueil
header("Location: index.php");
exit;
} catch (PDOException $e) {
$message = "❌ Erreur base de données : " . $e->getMessage();
}
} else {
$message = "⚠️ Tous les champs doivent être remplis.";
}
}
?>
<!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>
<?php if ($message): ?>
<p style="color:red;"><?= htmlspecialchars($message) ?></p>
<?php endif; ?>
<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>
<?php if ($_SESSION['user']['role'] === 'admin'): ?>
<a href="dashboard.php" class="retour">⬅ Retour au tableau de bord</a>
<?php else: ?>
<a href="index.php" class="retour">⬅ Retour à laccueil</a>
<?php endif; ?>
</div>
<footer>🕒 <?= date('Y') ?> — Mini CMS by Aya 💖</footer>
</body>
</html>