summaryrefslogtreecommitdiff
path: root/public/index.php
blob: f674f0f69f411d4664c30f54ab969ca6c533c7ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
// index.php

$page = isset($_GET['page']) ? $_GET['page'] : 'accueil';
$from = isset($_GET['from']) ? $_GET['from'] : 'accueil'; // pour revenir au même endroit après un submit
$id_article = isset($_GET['id']) && !empty($_GET['id']) ? $_GET['id'] : '';


require '../src/service/ckeditor5/CKEditor.php';
$ckeditor = new CKEditor();
$ckeditor->setLanguage('fr'); // attention, exécute une autre fonction qui peut échouer
$ckeditor->setStorageMode('files');
$ckeditor->setPage($page);


// routage

// upload images AJAX
if(isset($_GET['action']) && $_GET['action'] === 'upload_image_editor')
{
    $ckeditor->checkAjaxRequest();
}

// submit normal
if(isset($_GET['action']) && $_GET['action'] === 'submit_editor' && isset($_POST["contenu"])) // HTML envoyé par l'éditeur
{
    $ckeditor->setFrom($from);
    $html_from_editor = $ckeditor->checkSubmitPOST();

    if($id_article == '') // nouvel article
    {
        $id_article = time();
    }
    
    if($ckeditor->getStorageMode() === 'database')
    {
        // => modèle
    }
    elseif($ckeditor->getStorageMode() === 'files')
    {
        file_put_contents(CKEditor::DATA_PATH . '/html/' . $id_article . '.html', $html_from_editor);
    }

    header('Location: index.php?page=' . $ckeditor->getFrom());
    die;
}

// modèle - récupération des articles
$id_array = []; // noms des id en BBD ou des fichiers
$articles_content = []; // contenu HTML

if(isset($_GET['action']) && $_GET['action'] === 'open_editor' && $id_article == '')
{
    $articles_content = $ckeditor->openEditor() . "\n";
}
else
{
    $articles_content = $ckeditor->displayNewArticleButton() . "\n";
}

if($ckeditor->getStorageMode() === 'database')
{
    // => modèle
}
elseif($ckeditor->getStorageMode() === 'files')
{
    foreach(scandir(CKEditor::DATA_PATH . '/html/') as $file_name)
    {
        if(str_ends_with($file_name, '.html')) // filtre . et .. et d'éventuels autres fichiers
        {
            $id_array[] = rtrim($file_name, '.html');
        }
    }

    // suppression et rechargement
    if(isset($_GET['action']) && $_GET['action'] === 'delete_article')
    {
        if(in_array($id_article, $id_array))
        {
            unlink(CKEditor::DATA_PATH . '/html/' . $id_article . '.html');
            //$ckeditor->deleteSideEffects();
        }
        header('Location: index.php?page=' . $from);
        die;
    }

    // affichage
    foreach($id_array as $id)
    {
        $texte = trim(file_get_contents(CKEditor::DATA_PATH . '/html/' . $id . '.html'));
        //$texte = addslashes($texte); // échappe ', ", \ et NULL, je sais pas si c'est bien

        // vue - afficher des articles et éventuellement l'éditeur dans un des articles
        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) . "\n";
            }
            else
            {
                $articles_content .= $ckeditor->displayArticle($id, $texte) . "\n";
            }
        }
        else
        {
            $articles_content .= $ckeditor->displayArticle($id, $texte) . "\n";
        }
    }
}
// vue
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="icon" type="image/png" href="">
    <?= $ckeditor->getCSSOutsideEditorTag() ?>
    <?= $ckeditor->getCSSEditorTag() ?>
</head>
<body>
    <div>
<?= $articles_content ?>
    </div>
</body>
</html>