true]); Security::requireAdmin(); $db = getDatabase(); $error = ''; $isEdit = false; $project = [ 'id' => '', 'title' => '', 'slug' => '', 'short_description' => '', 'full_description' => '', 'image' => '', 'status' => 'published', 'badge' => 'development', 'technologies' => '', 'project_url' => '', 'display_order' => 0, ]; if (!empty($_GET['id'])) { $stmt = $db->prepare('SELECT * FROM projects WHERE id = :id LIMIT 1'); $stmt->execute(['id' => (int) $_GET['id']]); $existing = $stmt->fetch(); if (!$existing) { header('Location: index.php'); exit; } $project = $existing; $isEdit = true; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { Security::verifyCsrf($_POST['csrf_token'] ?? ''); $project['title'] = trim((string) ($_POST['title'] ?? '')); $project['slug'] = trim((string) ($_POST['slug'] ?? '')); $project['short_description'] = trim((string) ($_POST['short_description'] ?? '')); $project['full_description'] = trim((string) ($_POST['full_description'] ?? '')); $project['status'] = in_array($_POST['status'] ?? '', ['draft', 'published'], true) ? $_POST['status'] : 'draft'; $project['badge'] = in_array($_POST['badge'] ?? '', ['development', 'production'], true) ? $_POST['badge'] : 'development'; $project['technologies'] = trim((string) ($_POST['technologies'] ?? '')); $project['project_url'] = trim((string) ($_POST['project_url'] ?? '')); $project['display_order'] = max(0, (int) ($_POST['display_order'] ?? 0)); $project['slug'] = Security::slugify($project['slug'] !== '' ? $project['slug'] : $project['title']); if ($project['title'] === '') { $error = 'Le titre est requis.'; } elseif ($project['short_description'] === '') { $error = 'La description courte est requise.'; } elseif ($project['project_url'] !== '' && !preg_match('#^https://#i', $project['project_url'])) { $error = 'Le lien du projet doit commencer par https://'; } $imageFilename = (string) ($project['image'] ?? ''); if ($error === '' && !empty($_FILES['image_file']['name']) && (int) $_FILES['image_file']['error'] === UPLOAD_ERR_OK) { $finfo = new finfo(FILEINFO_MIME_TYPE); $mime = $finfo->file($_FILES['image_file']['tmp_name']) ?: ''; $allowed = [ 'image/jpeg' => 'jpg', 'image/png' => 'png', 'image/webp' => 'webp', ]; if (!isset($allowed[$mime])) { $error = 'Format d\'image non autorise. Utilisez JPG, PNG ou WebP.'; } elseif ((int) $_FILES['image_file']['size'] > 5 * 1024 * 1024) { $error = 'L\'image ne doit pas depasser 5 Mo.'; } elseif (!is_uploaded_file($_FILES['image_file']['tmp_name'])) { $error = 'Upload invalide.'; } else { $uploadDir = realpath(__DIR__ . '/../assets/img/projects'); if ($uploadDir === false) { $error = 'Dossier d\'upload introuvable.'; } else { $previousImage = $imageFilename; $imageFilename = $project['slug'] . '-' . bin2hex(random_bytes(8)) . '.' . $allowed[$mime]; $uploadPath = $uploadDir . DIRECTORY_SEPARATOR . $imageFilename; if (!move_uploaded_file($_FILES['image_file']['tmp_name'], $uploadPath)) { $error = 'Erreur lors de l\'upload de l\'image.'; $imageFilename = $previousImage; } else { @chmod($uploadPath, 0644); if ($previousImage !== '' && $previousImage !== $imageFilename) { $oldPath = $uploadDir . DIRECTORY_SEPARATOR . basename($previousImage); if (is_file($oldPath)) { @unlink($oldPath); } } } } } } if ($error === '') { try { if ($isEdit) { $stmt = $db->prepare( 'UPDATE projects SET title = :title, slug = :slug, short_description = :short_description, full_description = :full_description, image = :image, status = :status, badge = :badge, technologies = :technologies, project_url = :project_url, display_order = :display_order WHERE id = :id' ); $stmt->execute([ 'title' => $project['title'], 'slug' => $project['slug'], 'short_description' => $project['short_description'], 'full_description' => $project['full_description'], 'image' => $imageFilename, 'status' => $project['status'], 'badge' => $project['badge'], 'technologies' => $project['technologies'], 'project_url' => $project['project_url'], 'display_order' => $project['display_order'], 'id' => (int) $_GET['id'], ]); header('Location: index.php?success=updated'); exit; } $stmt = $db->prepare( 'INSERT INTO projects ( title, slug, short_description, full_description, image, status, badge, technologies, project_url, display_order ) VALUES ( :title, :slug, :short_description, :full_description, :image, :status, :badge, :technologies, :project_url, :display_order )' ); $stmt->execute([ 'title' => $project['title'], 'slug' => $project['slug'], 'short_description' => $project['short_description'], 'full_description' => $project['full_description'], 'image' => $imageFilename, 'status' => $project['status'], 'badge' => $project['badge'], 'technologies' => $project['technologies'], 'project_url' => $project['project_url'], 'display_order' => $project['display_order'], ]); header('Location: index.php?success=created'); exit; } catch (PDOException $e) { error_log('[ProjectForm] ' . $e->getMessage()); $error = 'Une erreur est survenue. Veuillez reessayer.'; } } } ?>