summaryrefslogtreecommitdiff
path: root/public/index.php
diff options
context:
space:
mode:
Diffstat (limited to 'public/index.php')
-rw-r--r--public/index.php128
1 files changed, 128 insertions, 0 deletions
diff --git a/public/index.php b/public/index.php
new file mode 100644
index 0000000..f674f0f
--- /dev/null
+++ b/public/index.php
@@ -0,0 +1,128 @@
1<?php
2// index.php
3
4$page = isset($_GET['page']) ? $_GET['page'] : 'accueil';
5$from = isset($_GET['from']) ? $_GET['from'] : 'accueil'; // pour revenir au même endroit après un submit
6$id_article = isset($_GET['id']) && !empty($_GET['id']) ? $_GET['id'] : '';
7
8
9require '../src/service/ckeditor5/CKEditor.php';
10$ckeditor = new CKEditor();
11$ckeditor->setLanguage('fr'); // attention, exécute une autre fonction qui peut échouer
12$ckeditor->setStorageMode('files');
13$ckeditor->setPage($page);
14
15
16// routage
17
18// upload images AJAX
19if(isset($_GET['action']) && $_GET['action'] === 'upload_image_editor')
20{
21 $ckeditor->checkAjaxRequest();
22}
23
24// submit normal
25if(isset($_GET['action']) && $_GET['action'] === 'submit_editor' && isset($_POST["contenu"])) // HTML envoyé par l'éditeur
26{
27 $ckeditor->setFrom($from);
28 $html_from_editor = $ckeditor->checkSubmitPOST();
29
30 if($id_article == '') // nouvel article
31 {
32 $id_article = time();
33 }
34
35 if($ckeditor->getStorageMode() === 'database')
36 {
37 // => modèle
38 }
39 elseif($ckeditor->getStorageMode() === 'files')
40 {
41 file_put_contents(CKEditor::DATA_PATH . '/html/' . $id_article . '.html', $html_from_editor);
42 }
43
44 header('Location: index.php?page=' . $ckeditor->getFrom());
45 die;
46}
47
48// modèle - récupération des articles
49$id_array = []; // noms des id en BBD ou des fichiers
50$articles_content = []; // contenu HTML
51
52if(isset($_GET['action']) && $_GET['action'] === 'open_editor' && $id_article == '')
53{
54 $articles_content = $ckeditor->openEditor() . "\n";
55}
56else
57{
58 $articles_content = $ckeditor->displayNewArticleButton() . "\n";
59}
60
61if($ckeditor->getStorageMode() === 'database')
62{
63 // => modèle
64}
65elseif($ckeditor->getStorageMode() === 'files')
66{
67 foreach(scandir(CKEditor::DATA_PATH . '/html/') as $file_name)
68 {
69 if(str_ends_with($file_name, '.html')) // filtre . et .. et d'éventuels autres fichiers
70 {
71 $id_array[] = rtrim($file_name, '.html');
72 }
73 }
74
75 // suppression et rechargement
76 if(isset($_GET['action']) && $_GET['action'] === 'delete_article')
77 {
78 if(in_array($id_article, $id_array))
79 {
80 unlink(CKEditor::DATA_PATH . '/html/' . $id_article . '.html');
81 //$ckeditor->deleteSideEffects();
82 }
83 header('Location: index.php?page=' . $from);
84 die;
85 }
86
87 // affichage
88 foreach($id_array as $id)
89 {
90 $texte = trim(file_get_contents(CKEditor::DATA_PATH . '/html/' . $id . '.html'));
91 //$texte = addslashes($texte); // échappe ', ", \ et NULL, je sais pas si c'est bien
92
93 // vue - afficher des articles et éventuellement l'éditeur dans un des articles
94 if(isset($_GET['action']) && $_GET['action'] === 'open_editor')
95 {
96 if($id === $id_article)
97 {
98 $texte = addcslashes($texte, "'"); // échapper les simples quotes pour javascript
99 $articles_content .= $ckeditor->openEditor($id, $texte) . "\n";
100 }
101 else
102 {
103 $articles_content .= $ckeditor->displayArticle($id, $texte) . "\n";
104 }
105 }
106 else
107 {
108 $articles_content .= $ckeditor->displayArticle($id, $texte) . "\n";
109 }
110 }
111}
112// vue
113?>
114<!DOCTYPE html>
115<html lang="fr">
116<head>
117 <meta charset="utf-8">
118 <title></title>
119 <link rel="icon" type="image/png" href="">
120 <?= $ckeditor->getCSSOutsideEditorTag() ?>
121 <?= $ckeditor->getCSSEditorTag() ?>
122</head>
123<body>
124 <div>
125<?= $articles_content ?>
126 </div>
127</body>
128</html>