ajout de la possibilité d'éditer le titre, la description du siteet son favicon
et affichage du logo dans le menu de l'administration
This commit is contained in:
parent
88be39b97a
commit
f909a48b92
@ -21,10 +21,19 @@ $stories = Stories::getAll();
|
||||
</head>
|
||||
<body>
|
||||
<nav class="admin-nav">
|
||||
<div class="nav-brand">Administration</div>
|
||||
<div class="nav-brand">
|
||||
<?php
|
||||
$config = Config::load();
|
||||
if (!empty($config['site']['logo'])): ?>
|
||||
<img src="<?= htmlspecialchars('../' . $config['site']['logo']) ?>"
|
||||
alt="<?= htmlspecialchars($config['site']['name']) ?>">
|
||||
<?php endif; ?>
|
||||
<span>Administration</span>
|
||||
</div>
|
||||
<div class="nav-menu">
|
||||
<a href="profile.php" class="button">Profil</a>
|
||||
<a href="story-edit.php" class="button">Nouveau roman</a>
|
||||
<a href="options.php" class="button">Options</a>
|
||||
<form method="POST" action="logout.php" class="logout-form">
|
||||
<button type="submit">Déconnexion</button>
|
||||
</form>
|
||||
|
156
admin/options.php
Normal file
156
admin/options.php
Normal file
@ -0,0 +1,156 @@
|
||||
<?php
|
||||
require_once '../includes/config.php';
|
||||
require_once '../includes/auth.php';
|
||||
require_once '../includes/site-upload.php';
|
||||
|
||||
// Vérification de l'authentification
|
||||
if (!Auth::check()) {
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
$success = '';
|
||||
$error = '';
|
||||
|
||||
// Traitement du formulaire
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
try {
|
||||
$config = Config::load();
|
||||
|
||||
// Mise à jour des valeurs textuelles
|
||||
$config['site']['name'] = trim($_POST['site_name'] ?? '');
|
||||
$config['site']['description'] = trim($_POST['site_description'] ?? '');
|
||||
|
||||
// Validation
|
||||
if (empty($config['site']['name'])) {
|
||||
throw new Exception('Le nom du site est requis');
|
||||
}
|
||||
|
||||
// Gestion de l'upload du logo
|
||||
if (isset($_FILES['site_logo']) && $_FILES['site_logo']['error'] !== UPLOAD_ERR_NO_FILE) {
|
||||
$uploadHandler = new SiteUploadHandler();
|
||||
$config['site']['logo'] = $uploadHandler->handleLogoUpload($_FILES['site_logo']);
|
||||
}
|
||||
|
||||
// Sauvegarde
|
||||
Config::save($config);
|
||||
$success = 'Configuration mise à jour avec succès';
|
||||
|
||||
} catch (Exception $e) {
|
||||
$error = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
// Chargement de la configuration actuelle
|
||||
$config = Config::load();
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Options du site - Administration</title>
|
||||
<link rel="stylesheet" href="../assets/css/main.css">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="admin-nav">
|
||||
<div class="nav-brand">
|
||||
<?php
|
||||
$config = Config::load();
|
||||
if (!empty($config['site']['logo'])): ?>
|
||||
<img src="<?= htmlspecialchars('../' . $config['site']['logo']) ?>"
|
||||
alt="<?= htmlspecialchars($config['site']['name']) ?>">
|
||||
<?php endif; ?>
|
||||
<span>Administration</span>
|
||||
</div>
|
||||
<div class="nav-menu">
|
||||
<a href="index.php" class="button">Retour</a>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main class="admin-main">
|
||||
<h1>Options du site</h1>
|
||||
|
||||
<?php if ($success): ?>
|
||||
<div class="success-message"><?= htmlspecialchars($success) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($error): ?>
|
||||
<div class="error-message"><?= htmlspecialchars($error) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<form method="POST" class="options-form" enctype="multipart/form-data">
|
||||
<div class="form-group">
|
||||
<label for="site_name">Nom du site</label>
|
||||
<input type="text"
|
||||
id="site_name"
|
||||
name="site_name"
|
||||
value="<?= htmlspecialchars($config['site']['name']) ?>"
|
||||
required>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="site_description">Description du site</label>
|
||||
<textarea id="site_description"
|
||||
name="site_description"
|
||||
rows="4"><?= htmlspecialchars($config['site']['description']) ?></textarea>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="site_logo">Logo du site</label>
|
||||
<?php if (!empty($config['site']['logo'])): ?>
|
||||
<div class="current-logo">
|
||||
<img src="<?= htmlspecialchars('../' . $config['site']['logo']) ?>"
|
||||
alt="Logo actuel"
|
||||
style="max-height: 100px; margin: 10px 0;">
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<input type="file"
|
||||
id="site_logo"
|
||||
name="site_logo"
|
||||
accept="image/jpeg,image/png,image/gif,image/svg+xml">
|
||||
<small>Formats acceptés : JPG, PNG, GIF, SVG. Taille maximum : 2MB</small>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="button">Enregistrer les modifications</button>
|
||||
</form>
|
||||
</main>
|
||||
|
||||
<script>
|
||||
// Prévisualisation du logo
|
||||
document.getElementById('site_logo').addEventListener('change', function(e) {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
let currentLogo = document.querySelector('.current-logo');
|
||||
if (!currentLogo) {
|
||||
currentLogo = document.createElement('div');
|
||||
currentLogo.className = 'current-logo';
|
||||
e.target.parentElement.insertBefore(currentLogo, e.target.nextSibling);
|
||||
}
|
||||
currentLogo.innerHTML = `
|
||||
<img src="${e.target.result}"
|
||||
alt="Aperçu du logo"
|
||||
style="max-height: 100px; margin: 10px 0;">
|
||||
`;
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
});
|
||||
|
||||
// Détection des changements non sauvegardés
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const form = document.querySelector('form');
|
||||
const initialState = new FormData(form).toString();
|
||||
|
||||
window.addEventListener('beforeunload', (e) => {
|
||||
if (new FormData(form).toString() !== initialState) {
|
||||
e.preventDefault();
|
||||
e.returnValue = '';
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -83,7 +83,15 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
</head>
|
||||
<body>
|
||||
<nav class="admin-nav">
|
||||
<div class="nav-brand">Administration</div>
|
||||
<div class="nav-brand">
|
||||
<?php
|
||||
$config = Config::load();
|
||||
if (!empty($config['site']['logo'])): ?>
|
||||
<img src="<?= htmlspecialchars('../' . $config['site']['logo']) ?>"
|
||||
alt="<?= htmlspecialchars($config['site']['name']) ?>">
|
||||
<?php endif; ?>
|
||||
<span>Administration</span>
|
||||
</div>
|
||||
<div class="nav-menu">
|
||||
<a href="index.php" class="button">Retour</a>
|
||||
</div>
|
||||
|
@ -67,7 +67,15 @@ function generateSlug($title) {
|
||||
</head>
|
||||
<body>
|
||||
<nav class="admin-nav">
|
||||
<div class="nav-brand">Administration</div>
|
||||
<div class="nav-brand">
|
||||
<?php
|
||||
$config = Config::load();
|
||||
if (!empty($config['site']['logo'])): ?>
|
||||
<img src="<?= htmlspecialchars('../' . $config['site']['logo']) ?>"
|
||||
alt="<?= htmlspecialchars($config['site']['name']) ?>">
|
||||
<?php endif; ?>
|
||||
<span>Administration</span>
|
||||
</div>
|
||||
<div class="nav-menu">
|
||||
<a href="index.php" class="button">Retour</a>
|
||||
</div>
|
||||
|
@ -12,6 +12,14 @@
|
||||
.nav-brand {
|
||||
font-size: 1.25rem;
|
||||
font-weight: bold;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.nav-brand img {
|
||||
height: 40px;
|
||||
width: auto;
|
||||
}
|
||||
|
||||
.nav-menu {
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 15 KiB |
@ -30,4 +30,28 @@ class Config {
|
||||
$config = self::load();
|
||||
return $config[$key] ?? $default;
|
||||
}
|
||||
|
||||
public static function save($newConfig) {
|
||||
$configFile = __DIR__ . '/../config.json';
|
||||
|
||||
// S'assurer que le fichier est accessible en écriture
|
||||
if (!is_writable($configFile)) {
|
||||
throw new Exception('Le fichier de configuration n\'est pas accessible en écriture');
|
||||
}
|
||||
|
||||
// Sauvegarder avec un formatage JSON lisible
|
||||
$success = file_put_contents(
|
||||
$configFile,
|
||||
json_encode($newConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
|
||||
);
|
||||
|
||||
if ($success === false) {
|
||||
throw new Exception('Erreur lors de la sauvegarde de la configuration');
|
||||
}
|
||||
|
||||
// Mettre à jour la configuration en mémoire
|
||||
self::$config = $newConfig;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
86
includes/site-upload.php
Normal file
86
includes/site-upload.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
class SiteUploadHandler {
|
||||
private $uploadDir;
|
||||
private $allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'];
|
||||
private $maxFileSize = 2097152; // 2MB
|
||||
|
||||
public function __construct() {
|
||||
$this->uploadDir = __DIR__ . '/../assets/images/site/';
|
||||
$this->ensureUploadDirectory();
|
||||
}
|
||||
|
||||
public function handleLogoUpload($file) {
|
||||
try {
|
||||
if ($file['error'] !== UPLOAD_ERR_OK) {
|
||||
throw new Exception($this->getUploadErrorMessage($file['error']));
|
||||
}
|
||||
|
||||
// Vérifier le type MIME
|
||||
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
||||
$mimeType = $finfo->file($file['tmp_name']);
|
||||
if (!in_array($mimeType, $this->allowedTypes)) {
|
||||
throw new Exception('Type de fichier non autorisé. Types acceptés : JPG, PNG, GIF, SVG');
|
||||
}
|
||||
|
||||
// Vérifier la taille
|
||||
if ($file['size'] > $this->maxFileSize) {
|
||||
throw new Exception('Fichier trop volumineux. Taille maximum : 2MB');
|
||||
}
|
||||
|
||||
// Générer le nom de fichier
|
||||
$extension = $this->getExtensionFromMimeType($mimeType);
|
||||
$filename = 'logo.' . $extension;
|
||||
$targetPath = $this->uploadDir . $filename;
|
||||
|
||||
// Supprimer l'ancien logo s'il existe
|
||||
$this->removeOldLogo();
|
||||
|
||||
// Déplacer le fichier
|
||||
if (!move_uploaded_file($file['tmp_name'], $targetPath)) {
|
||||
throw new Exception('Erreur lors du déplacement du fichier uploadé');
|
||||
}
|
||||
|
||||
return 'assets/images/site/' . $filename;
|
||||
|
||||
} catch (Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
private function ensureUploadDirectory() {
|
||||
if (!file_exists($this->uploadDir)) {
|
||||
if (!mkdir($this->uploadDir, 0755, true)) {
|
||||
throw new Exception('Impossible de créer le dossier d\'upload');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function removeOldLogo() {
|
||||
foreach (glob($this->uploadDir . 'logo.*') as $file) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
private function getExtensionFromMimeType($mimeType) {
|
||||
$map = [
|
||||
'image/jpeg' => 'jpg',
|
||||
'image/png' => 'png',
|
||||
'image/gif' => 'gif',
|
||||
'image/svg+xml' => 'svg'
|
||||
];
|
||||
return $map[$mimeType] ?? 'png';
|
||||
}
|
||||
|
||||
private function getUploadErrorMessage($error) {
|
||||
$errors = [
|
||||
UPLOAD_ERR_INI_SIZE => 'Le fichier dépasse la taille maximale autorisée par PHP',
|
||||
UPLOAD_ERR_FORM_SIZE => 'Le fichier dépasse la taille maximale autorisée par le formulaire',
|
||||
UPLOAD_ERR_PARTIAL => 'Le fichier n\'a été que partiellement uploadé',
|
||||
UPLOAD_ERR_NO_FILE => 'Aucun fichier n\'a été uploadé',
|
||||
UPLOAD_ERR_NO_TMP_DIR => 'Dossier temporaire manquant',
|
||||
UPLOAD_ERR_CANT_WRITE => 'Échec de l\'écriture du fichier sur le disque',
|
||||
UPLOAD_ERR_EXTENSION => 'Une extension PHP a arrêté l\'upload'
|
||||
];
|
||||
return $errors[$error] ?? 'Erreur inconnue lors de l\'upload';
|
||||
}
|
||||
}
|
@ -15,6 +15,10 @@ $stories = Stories::getAll();
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<?php if (!empty($config['site']['logo'])): ?>
|
||||
<img src="<?= htmlspecialchars($config['site']['logo']) ?>"
|
||||
alt="<?= htmlspecialchars($config['site']['name']) ?>">
|
||||
<?php endif; ?>
|
||||
<h1><?= htmlspecialchars($config['site']['name']) ?></h1>
|
||||
<p><?= htmlspecialchars($config['site']['description']) ?></p>
|
||||
</header>
|
||||
|
Loading…
x
Reference in New Issue
Block a user