From 63759dbbf023daa8f7f310b914a04310a4770154 Mon Sep 17 00:00:00 2001 From: git-pc-greta Date: Thu, 30 Jan 2025 14:52:59 +0100 Subject: =?UTF-8?q?foreach=20articles,=20mod=C3=A8le=20et=20id=20=C3=A0=20?= =?UTF-8?q?l'ext=C3=A9rieur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/page/html/1646148842.html | 1 + data/page/images/garfield4.png | Bin 0 -> 339304 bytes index.php | 51 +++++++++++++--------- src/integration/ckeditor5/CKEditor.php | 65 +++++++++++------------------ src/integration/ckeditor5/image_upload.php | 6 +-- src/integration/ckeditor5/view.php | 6 +-- src/view/templates/page.php | 7 +++- 7 files changed, 69 insertions(+), 67 deletions(-) create mode 100644 data/page/html/1646148842.html create mode 100644 data/page/images/garfield4.png diff --git a/data/page/html/1646148842.html b/data/page/html/1646148842.html new file mode 100644 index 0000000..d9ac00b --- /dev/null +++ b/data/page/html/1646148842.html @@ -0,0 +1 @@ +
image
image

good bye

okah ouibon

c'est pas

grave

on verra

plus tard

salut

goodbye

\ No newline at end of file diff --git a/data/page/images/garfield4.png b/data/page/images/garfield4.png new file mode 100644 index 0000000..466e226 Binary files /dev/null and b/data/page/images/garfield4.png differ diff --git a/index.php b/index.php index f388081..d998d96 100644 --- a/index.php +++ b/index.php @@ -25,35 +25,48 @@ if(isset($_GET['action']) && $_GET['action'] === 'upload_image_editor') if(isset($_GET['action']) && $_GET['action'] === 'submit_editor') // HTML envoyé par l'éditeur { $ckeditor->setFrom($from); - $ckeditor->setIdArticle($id_article); $ckeditor->checkSubmitPOST(); } -/*$articles = []; -foreach($articles as $article) -{}*/ - -// ouvrir l'éditeur -if(isset($_GET['action']) && $_GET['action'] === 'open_editor') +// modèle - récupération des articles +$articles_id = []; // noms des id en BBD ou des fichiers +$articles_content = []; // contenu HTML +if($ckeditor->getStorageMode() === 'database') { - $ckeditor->setIdArticle($id_article); - $editeurHTML = $ckeditor->openEditor(); + // => modèle } -else // affichage article +elseif($ckeditor->getStorageMode() === 'files') { - $ckeditor->setIdArticle('1646148841'); - if($ckeditor->getStorageMode() === 'database') + foreach(scandir(CKEditor::DATA_PATH . '/html/') as $file_name) { - // ton code + if(str_ends_with($file_name, '.html')) // filtre . et .. et d'éventuels autres fichiers + { + $articles_id[] = rtrim($file_name, '.html'); + } } - elseif($ckeditor->getStorageMode() === 'files') + foreach($articles_id as $id) { - $texte = file_get_contents($ckeditor->getDataPath() . '/html/' . $ckeditor->getIdArticle() . '.html'); - //$texte = trim(addcslashes($texte, "'")); // échappe seulement les simples quotes - $texte = trim($texte); + $texte = trim(file_get_contents(CKEditor::DATA_PATH . '/html/' . $id . '.html')); + //$texte = addslashes($texte); // échappe ', ", \ et NULL, je sais pas si c'est bien + + // ouvrir l'éditeur sur un des articles ou aucun + if(isset($_GET['action']) && $_GET['action'] === 'open_editor') + { + if($id === $id_article) + { + $texte = addcslashes($texte, "'"); // échapper les simples quotes pour javascript + $articles_content[] = $ckeditor->openEditor($id, $texte); + } + else + { + $articles_content[] = $ckeditor->displayArticle($id, $texte); + } + } + else // affichage article + { + $articles_content[] = $ckeditor->displayArticle($id, $texte); + } } - $editeurHTML = '
' . $texte . '
-

ouvrir l\'éditeur

'; } require 'src/view/templates/page.php'; diff --git a/src/integration/ckeditor5/CKEditor.php b/src/integration/ckeditor5/CKEditor.php index 6f630c8..9fa0f42 100644 --- a/src/integration/ckeditor5/CKEditor.php +++ b/src/integration/ckeditor5/CKEditor.php @@ -4,9 +4,10 @@ class CKEditor { public const string INTEGRATION_PATH = 'src/integration/ckeditor5/'; - + public const string DATA_PATH = 'data/page'; //public const string SERVER_PATH = '/'; private const string SERVER_PATH = '/ckeditor5/'; + private string $server_root; // pour les "imports" en javascript // pour l'importmap: j'ai modifié la version "installation avec CDN de la doc pour utiliser les fichiers locaux // l'"importmap" permet d'utiliser "import" (ça ressemble pas mal au python) dans le navigateur comme n'importe quel langage de programmation normal @@ -14,13 +15,12 @@ class CKEditor private string $storage_mode = 'files'; private string $page = 'accueil'; private string $from = ''; - private string $id_article = ''; + //private string $id_article = ''; private string $php_ini_max_size; private string $css_editor_tag = ''; private string $css_outside_editor_tag = ''; private string $toolbar_language = 'fr'; - private string $data_path = 'data/page'; public function __construct() @@ -41,14 +41,6 @@ class CKEditor { return $this->from; } - public function getIdArticle(): string - { - return $this->id_article; - } - public function getDataPath(): string - { - return $this->data_path; - } public function getCSSOutsideEditorTag(): string { return $this->css_outside_editor_tag; @@ -71,10 +63,6 @@ class CKEditor { $this->from = $from; } - public function setIdArticle(string $id_article): void - { - $this->id_article = $id_article; - } public function setStorageMode($mode): void { $this->storage_mode = $mode; @@ -87,40 +75,36 @@ class CKEditor die; } - - public function checkSubmitPOST(): void + public function displayArticle(string $id, string $texte): string { - $html_from_editor = $this->getAndCleanEditorOutput($_POST["contenu"]); // manipule $_POST['contenu']; - - // enregistrement des données - //var_dump($html_from_editor); - echo "ce programme n'enregistre rien, faîtes-le dans votre application et supprimer cette ligne dans index.php"; die; - - header('Location: ' . $this->from); - die; + $html = '
' . $texte . '
+

ouvrir l\'éditeur

'; + return $html; } - public function openEditor(): string + public function openEditor(string $id, string $texte): string { // version "minifiée" $this->css_editor_tag = ''; // version normale aérée et commentée" $this->css_editor_tag = ''; - if($this->getStorageMode() === 'database') - {} - elseif($this->getStorageMode() === 'files') - { - $texte = file_get_contents($this->data_path . '/html/' . $this->id_article . '.html'); - - $texte = trim(addcslashes($texte, "'")); // échapper les simples quotes pour javascript - //$texte = trim(addslashes($texte)); // échappe ', ", \ et NULL, je sais pas si c'est bien - } - require self::INTEGRATION_PATH . 'view.php'; // html + JS return $editeurHTML; } + public function checkSubmitPOST(): void + { + $html_from_editor = $this->getAndCleanEditorOutput($_POST["contenu"]); // manipule $_POST['contenu']; + + // enregistrement des données + //var_dump($html_from_editor); // debug + echo "ce programme n'enregistre rien, faîtes-le dans votre application et supprimer cette ligne dans index.php"; die; + + header('Location: ' . $this->from); + die; + } + /* lien symbolique des traductions l'éditeur recherche un dans module/ckeditor5/dist/browser/translations un fichier se trouvant dans module/ckeditor5/dist/translations @@ -171,7 +155,7 @@ class CKEditor // bugs possibles sans trim() lorsqu'on insère le HTML dans l'éditeur $html = trim($html); - //checkContentInFile($html, 'avant'); + //$this->checkContentInFile($html, 'avant'); // sécurisation du HTML (faille XSS) require 'vendor/htmlawed/htmlawed/htmLawed.php'; @@ -188,7 +172,7 @@ class CKEditor $specHtmLawed = ''; // optionnel: faire qu'un certain élément puisse n'avoir que certains attributs $html = htmLawed($html, $configHtmLawed, $specHtmLawed); - //checkContentInFile($html, 'après'); + //$this->checkContentInFile($html, 'après'); return $html; } @@ -196,11 +180,10 @@ class CKEditor private function checkContentInFile($html, $file_name): void { - $page = 'page'; $nom_fichier = $file_name . ".html"; - $fichier = fopen('data/' . $page . '/' . $nom_fichier, 'w'); // w peut créer un fichier, si il existe déjà, il est effacé par le nouveau contenu + $fichier = fopen(self::DATA_PATH . '/' . $nom_fichier, 'w'); // w peut créer un fichier, si il existe déjà, il est effacé par le nouveau contenu fputs($fichier, $html); fclose($fichier); - chmod('data/' . $page . '/' . $nom_fichier, 0666); + chmod(self::DATA_PATH . '/' . $nom_fichier, 0666); } } diff --git a/src/integration/ckeditor5/image_upload.php b/src/integration/ckeditor5/image_upload.php index 5764569..d549690 100644 --- a/src/integration/ckeditor5/image_upload.php +++ b/src/integration/ckeditor5/image_upload.php @@ -19,8 +19,8 @@ if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['upload']) // on prend la même liste que celle côté javascript, le SVG est bloqué pour raison de sécurité (javascript à l'intérieur) if(in_array($extension, $extautorisées)) { - move_uploaded_file ($_FILES['upload']['tmp_name'], $this->data_path . '/images/' . $_FILES['upload']['name']); - chmod($this->data_path . '/images/' . $_FILES['upload']['name'], 0666); + move_uploaded_file ($_FILES['upload']['tmp_name'], self::DATA_PATH . '/images/' . $_FILES['upload']['name']); + chmod(self::DATA_PATH . '/images/' . $_FILES['upload']['name'], 0666); } else { @@ -39,7 +39,7 @@ if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['upload']) if(empty($erreur)) { - $chemin = '{"url": "' . $this->data_path . '/images/' . $_FILES['upload']['name'] . '"}'; + $chemin = '{"url": "' . self::DATA_PATH . '/images/' . $_FILES['upload']['name'] . '"}'; echo $chemin; } else diff --git a/src/integration/ckeditor5/view.php b/src/integration/ckeditor5/view.php index bb1f6a2..4bcad52 100644 --- a/src/integration/ckeditor5/view.php +++ b/src/integration/ckeditor5/view.php @@ -1,13 +1,13 @@ page . '&action=submit_editor&id=' . $this->id_article; +$submit_action = 'index.php?from=' . $this->page . '&action=submit_editor&id=' . $id; $cancel_button = 'index.php?page=' . $this->page; -$cancel_button .= !empty($this->id_article) ? '&id=' . $this->id_article : ''; +//$cancel_button .= !empty($id) ? '&id=' . $id : ''; // à ajouter si on a besoin de revenir sur un article spécifique $upload_ajax = 'index.php?action=upload_image_editor'; -$upload_ajax .= !empty($this->id_article) ? '&id=' . $this->id_article : ''; +$upload_ajax .= !empty($id) ? '&id=' . $id : ''; ob_start(); ?> diff --git a/src/view/templates/page.php b/src/view/templates/page.php index 05c7444..3ea227a 100644 --- a/src/view/templates/page.php +++ b/src/view/templates/page.php @@ -9,7 +9,12 @@
- +
-- cgit v1.2.3