1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* * Pages controller |
5
|
|
|
* * |
6
|
|
|
* The file contains all the functions used for off-chain pages. |
7
|
|
|
* Display / Save / update / ... |
8
|
|
|
* |
9
|
|
|
* * @category Controllers |
10
|
|
|
* * @package SuperHive |
11
|
|
|
* * @author Florent Kosmala <[email protected]> |
12
|
|
|
* * @license https://www.gnu.org/licenses/gpl-3.0.txt GPL-3.0 |
13
|
|
|
* */ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace App\Controllers; |
18
|
|
|
|
19
|
|
|
use Psr\Container\ContainerInterface; |
20
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
22
|
|
|
|
23
|
|
|
final class PagesController |
24
|
|
|
{ |
25
|
|
|
private ContainerInterface $app; |
26
|
|
|
|
27
|
|
|
public function __construct(ContainerInterface $app) |
28
|
|
|
{ |
29
|
|
|
$this->app = $app; |
30
|
|
|
|
31
|
|
|
$session = $this->app->get('session'); |
32
|
|
|
|
33
|
|
|
$this->app->get('view')->getEnvironment()->addGlobal("user", [ |
34
|
|
|
'author' => $session['sh_author'], |
35
|
|
|
'signature' => $session['sh_sign'], |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* * Admin pages function |
41
|
|
|
* * |
42
|
|
|
* This function display the already written pages and a button to create one. |
43
|
|
|
* |
44
|
|
|
* @param Response $response |
45
|
|
|
*/ |
46
|
|
|
public function adminPages(Response $response): Response |
47
|
|
|
{ |
48
|
|
|
$pagesDir = $this->app->get('pagesdir'); |
49
|
|
|
$settings = $this->app->get('settings'); |
50
|
|
|
$pages = []; |
51
|
|
|
|
52
|
|
|
$allPages = preg_grep('~\.(html)$~', scandir($pagesDir)); |
53
|
|
|
foreach ($allPages as $page) { |
54
|
|
|
$pages[] = substr($page, 0, strrpos($page, '.')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->app->get('view')->render($response, '/admin/admin-pages.html', [ |
58
|
|
|
'settings' => $settings, |
59
|
|
|
'pages' => $pages, |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* * Administration new page function |
65
|
|
|
* * |
66
|
|
|
* This function just display editor to write new page. |
67
|
|
|
* |
68
|
|
|
* @param Response $response |
69
|
|
|
*/ |
70
|
|
|
public function adminNewPage(Response $response): Response |
71
|
|
|
{ |
72
|
|
|
$settings = $this->app->get('settings'); |
73
|
|
|
|
74
|
|
|
return $this->app->get('view')->render($response, '/admin/admin-newpage.html', [ |
75
|
|
|
'settings' => $settings, |
76
|
|
|
]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* * Administration edit page function |
81
|
|
|
* * |
82
|
|
|
* Same as adminNewPage but with already written content from already written page. |
83
|
|
|
* |
84
|
|
|
* @param string $file |
85
|
|
|
* @param Response $response |
86
|
|
|
*/ |
87
|
|
|
public function adminEditPage(string $file, Response $response): Response |
88
|
|
|
{ |
89
|
|
|
$pageTitle = []; |
|
|
|
|
90
|
|
|
|
91
|
|
|
$pagesDir = $this->app->get('pagesdir'); |
92
|
|
|
$settings = $this->app->get('settings'); |
93
|
|
|
|
94
|
|
|
$content = file_get_contents($pagesDir . $file . '.html'); |
95
|
|
|
|
96
|
|
|
$pageTitle = preg_match('/\{% block title %\}(.*?)\{% endblock %\}/s', $content, $match); |
97
|
|
|
$pageTitle = $match[1]; |
98
|
|
|
$pageContent = strstr($content, '{% block page %}'); |
99
|
|
|
$pageContent = preg_replace("/\{%(.*?)%\}/", '', $pageContent); |
100
|
|
|
|
101
|
|
|
return $this->app->get('view')->render($response, '/admin/admin-newpage.html', [ |
102
|
|
|
'pageTitle' => $pageTitle, |
103
|
|
|
'pageFile' => $file, |
104
|
|
|
'pageContent' => $pageContent, |
105
|
|
|
'settings' => $settings, |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* * Administration delete page function |
111
|
|
|
* * |
112
|
|
|
* called to delete fpage and return to administration Pages section |
113
|
|
|
* |
114
|
|
|
* @param string $file |
115
|
|
|
* @param Response $response |
116
|
|
|
* @param array<string, string> $args |
117
|
|
|
*/ |
118
|
|
|
public function adminDelPage(string $file, Response $response): Response |
119
|
|
|
{ |
120
|
|
|
$name = $file; |
121
|
|
|
$pagesDir = $this->app->get('pagesdir'); |
122
|
|
|
$filePath = $pagesDir . $name . '.html'; |
123
|
|
|
|
124
|
|
|
if (unlink($filePath)) { |
125
|
|
|
$response->getBody()->write('OK'); |
126
|
|
|
} else { |
127
|
|
|
$response->getBody()->write('Error'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $response; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* * Administration save page function |
135
|
|
|
* * |
136
|
|
|
* Used to generate slug, html code and save page in file. |
137
|
|
|
* |
138
|
|
|
* @param Request $request |
139
|
|
|
* @param Response $response |
140
|
|
|
*/ |
141
|
|
|
public function adminSavePage(Request $request, Response $response): Response |
142
|
|
|
{ |
143
|
|
|
$data = $request->getParsedBody(); |
144
|
|
|
$pagesDir = $this->app->get('pagesdir'); |
145
|
|
|
|
146
|
|
|
if (!empty($data['title'])) { |
147
|
|
|
$pageTitle = $data['title']; |
148
|
|
|
} else { |
149
|
|
|
$pageTitle = "No Title"; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (!empty($data['mde'])) { |
153
|
|
|
$pageContent = $data['mde']; |
154
|
|
|
} else { |
155
|
|
|
$pageContent = 'No content in this page'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Some functions to slugify title to create very cool URL |
159
|
|
|
$acc = 'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'; |
160
|
|
|
$noAcc = 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY'; |
161
|
|
|
$title = mb_convert_encoding($pageTitle, 'UTF-8', mb_list_encodings()); |
162
|
|
|
$acc = mb_convert_encoding($acc, 'UTF-8', mb_list_encodings()); |
163
|
|
|
$slug = mb_strtolower(strtr($title, $acc, $noAcc)); |
|
|
|
|
164
|
|
|
$slug = preg_replace('~[^\pL\d]+~u', '-', $slug); |
165
|
|
|
$slug = preg_replace('~[^-\w]+~', '', $slug); |
166
|
|
|
$slug = strtolower($slug); |
167
|
|
|
$slug = preg_replace('~-+~', '-', $slug); |
168
|
|
|
|
169
|
|
|
// apply Twig to the page to display with selected theme |
170
|
|
|
$page = '{% extends settings.theme ~ "/page.html" %}'; |
171
|
|
|
$page .= "\n{% block title %}" . $title . "{% endblock %}\n"; |
|
|
|
|
172
|
|
|
$page .= "\n{% block page %}\n" . $pageContent . "\n{% endblock %}\n"; |
173
|
|
|
|
174
|
|
|
$file = $pagesDir . $slug . '.html'; |
175
|
|
|
|
176
|
|
|
if (file_put_contents($file, $page)) { |
177
|
|
|
if (isset($_SERVER['HTTPS'])) { |
178
|
|
|
$isSecure = $_SERVER['HTTPS']; |
179
|
|
|
} |
180
|
|
|
if (isset($isSecure) && $isSecure === 'on') { |
181
|
|
|
$scheme = 'https'; |
182
|
|
|
} else { |
183
|
|
|
$scheme = 'http'; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (isset($_SERVER['HTTP_HOST'])) { |
187
|
|
|
$host = $_SERVER['HTTP_HOST']; |
188
|
|
|
} else { |
189
|
|
|
$host = "unknown_host.com"; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
$pageUrl = $scheme . '://' . $host . '/pages/' . $slug; |
194
|
|
|
$response->getBody()->write($pageUrl); |
195
|
|
|
} else { |
196
|
|
|
$response->getBody()->write('Error'); |
197
|
|
|
} |
198
|
|
|
return $response; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|