diff --git a/public/index.php b/public/index.php index b457fe2..e43cc40 100644 --- a/public/index.php +++ b/public/index.php @@ -12,6 +12,8 @@ use Medoo\Medoo; use App\Controllers\PostController; use App\Repositories\PostRepository; use App\Services\HtmlSanitizer; +use App\Services\HtmlPurifierFactory; +use App\Database\Migration; use App\Routes; use App\Config; @@ -46,20 +48,15 @@ $db = new Medoo([ 'database' => $dbFile, ]); -// Créer la table si elle n'existe pas -$db->pdo->exec(" - CREATE TABLE IF NOT EXISTS post ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - title TEXT NOT NULL, - content TEXT NOT NULL, - slug TEXT UNIQUE NOT NULL DEFAULT '', - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - updated_at DATETIME DEFAULT CURRENT_TIMESTAMP - ) -"); +// Exécuter les migrations +Migration::run($db); -// HtmlSanitizer -$htmlSanitizer = new HtmlSanitizer(); +// HtmlPurifier (créé via la factory) +$htmlPurifierCacheDir = __DIR__ . '/../var/cache/htmlpurifier'; +$htmlPurifier = HtmlPurifierFactory::create($htmlPurifierCacheDir); + +// HtmlSanitizer (reçoit HTMLPurifier injecté) +$htmlSanitizer = new HtmlSanitizer($htmlPurifier); // PostRepository $postRepository = new PostRepository($db); diff --git a/src/Database/Migration.php b/src/Database/Migration.php new file mode 100644 index 0000000..ca45ed1 --- /dev/null +++ b/src/Database/Migration.php @@ -0,0 +1,24 @@ +pdo->exec(" + CREATE TABLE IF NOT EXISTS post ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT NOT NULL, + content TEXT NOT NULL, + slug TEXT UNIQUE NOT NULL DEFAULT '', + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP + ) + "); + } +} diff --git a/src/Services/HtmlPurifierFactory.php b/src/Services/HtmlPurifierFactory.php new file mode 100644 index 0000000..8ef7399 --- /dev/null +++ b/src/Services/HtmlPurifierFactory.php @@ -0,0 +1,44 @@ +set('HTML.Allowed', 'p,br,strong,em,u,h1,h2,h3,h4,h5,h6,ul,ol,li,blockquote,a[href],img[src|alt|width|height]'); + + // Attributs autorisés + $config->set('HTML.AllowedAttributes', 'href,src,alt,width,height,title'); + + // Configuration du cache + $config->set('Cache.DefinitionImpl', 'Serializer'); + $config->set('Cache.SerializerPath', $cacheDir); + + return new HTMLPurifier($config); + } +} diff --git a/src/Services/HtmlSanitizer.php b/src/Services/HtmlSanitizer.php index faa8ae6..1cc8bac 100644 --- a/src/Services/HtmlSanitizer.php +++ b/src/Services/HtmlSanitizer.php @@ -5,30 +5,14 @@ declare(strict_types=1); namespace App\Services; use HTMLPurifier; -use HTMLPurifier_Config; final class HtmlSanitizer { - private HTMLPurifier $purifier; - - public function __construct() + /** + * @param HTMLPurifier $purifier Instance préconfigurée d'HTMLPurifier + */ + public function __construct(private HTMLPurifier $purifier) { - // Créer le répertoire de cache s'il n'existe pas - $cacheDir = __DIR__ . '/../../var/cache/htmlpurifier'; - if (!is_dir($cacheDir)) { - @mkdir($cacheDir, 0755, true); - } - - $config = HTMLPurifier_Config::createDefault(); - // Autoriser les balises courantes de formatage - $config->set('HTML.Allowed', 'p,br,strong,em,u,h1,h2,h3,h4,h5,h6,ul,ol,li,blockquote,a[href],img[src|alt|width|height]'); - // Désactiver les attributs dangereux - $config->set('HTML.AllowedAttributes', 'href,src,alt,width,height,title'); - // Activer le cache - $config->set('Cache.DefinitionImpl', 'Serializer'); - $config->set('Cache.SerializerPath', $cacheDir); - - $this->purifier = new HTMLPurifier($config); } public function sanitize(string $html): string diff --git a/views/pages/post_detail.twig b/views/pages/post_detail.twig index 9270f17..912754d 100644 --- a/views/pages/post_detail.twig +++ b/views/pages/post_detail.twig @@ -18,6 +18,7 @@ {% endif %}