|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* * Admin controller |
|
5
|
|
|
* * |
|
6
|
|
|
* The file contains all the functions used in all administration panel. |
|
7
|
|
|
* For admin posts function, please go to the Posts Controller |
|
8
|
|
|
* For admin pages function, please go to the Pages Controller |
|
9
|
|
|
* |
|
10
|
|
|
* * @category Controllers |
|
11
|
|
|
* * @package SuperHive |
|
12
|
|
|
* * @author Florent Kosmala <[email protected]> |
|
13
|
|
|
* * @license https://www.gnu.org/licenses/gpl-3.0.txt GPL-3.0 |
|
14
|
|
|
* */ |
|
15
|
|
|
|
|
16
|
|
|
declare(strict_types=1); |
|
17
|
|
|
|
|
18
|
|
|
namespace App\Controllers; |
|
19
|
|
|
|
|
20
|
|
|
use App\Controllers\CommonController as Common; |
|
21
|
|
|
use Hive\PhpLib\Hive\Condenser as HiveCondenser; |
|
22
|
|
|
use Psr\Container\ContainerInterface; |
|
23
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
24
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
25
|
|
|
|
|
26
|
|
|
final class AdminController |
|
27
|
|
|
{ |
|
28
|
|
|
private ContainerInterface $app; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Admin part contructor |
|
32
|
|
|
* |
|
33
|
|
|
* This constructor is not the same as other controllers. |
|
34
|
|
|
* Administration need to control if session exists with good account & encrypted key. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Psr\Container\ContainerInterface $app |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ContainerInterface $app) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->app = $app; |
|
41
|
|
|
$genPosts = new Common($this->app); |
|
42
|
|
|
$genPosts->genPostsFile(); |
|
43
|
|
|
|
|
44
|
|
|
/* |
|
45
|
|
|
* Check security in session for admin functions |
|
46
|
|
|
*/ |
|
47
|
|
|
$settings = $this->app->get('settings'); |
|
48
|
|
|
$session = $this->app->get('session'); |
|
49
|
|
|
|
|
50
|
|
|
$this->app->get('view')->getEnvironment()->addGlobal("user", [ |
|
51
|
|
|
'author' => $session['sh_author'], |
|
52
|
|
|
'signature' => $session['sh_sign'], |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
/* If sessons keys are not set */ |
|
56
|
|
|
if (!isset($session['sh_author']) || (!isset($session['sh_sign']))) { |
|
57
|
|
|
header('Location: /login'); |
|
58
|
|
|
die; |
|
59
|
|
|
} |
|
60
|
|
|
/* If session keys are not good */ |
|
61
|
|
|
if (preg_match('/(hive-\d{6})/i', $settings['author']) != 1) { |
|
62
|
|
|
$cred = unserialize(file_get_contents($this->app->get('password'))); |
|
63
|
|
|
$author = $settings['author']; |
|
64
|
|
|
$passwd = $cred[$author]; |
|
65
|
|
|
|
|
66
|
|
|
if (($settings['author'] !== $session['sh_author']) || ($passwd !== $session['sh_sign'])) { |
|
67
|
|
|
header('Location: /login'); |
|
68
|
|
|
die; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* * Admin index function |
|
75
|
|
|
* * |
|
76
|
|
|
* This function display the admin index with some settings ready to be changed. |
|
77
|
|
|
* It call the admin save() functionwhen the button is clicked. |
|
78
|
|
|
* |
|
79
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Psr\Http\Message\ResponseInterface $response |
|
82
|
|
|
* */ |
|
83
|
|
|
public function adminIndex(Response $response): Response |
|
84
|
|
|
{ |
|
85
|
|
|
// Create array from config file |
|
86
|
|
|
$settings = $this->app->get('settings'); |
|
87
|
|
|
$accountFile = $this->app->get('accountfile'); |
|
88
|
|
|
$blogFile = $this->app->get('blogfile'); |
|
89
|
|
|
|
|
90
|
|
|
$posts = json_decode(file_get_contents($blogFile), true); |
|
91
|
|
|
$nbPosts = count($posts); |
|
92
|
|
|
|
|
93
|
|
|
$apiConfig = [ |
|
94
|
|
|
'hiveNode' => $settings['api'], |
|
95
|
|
|
'debug' => false, |
|
96
|
|
|
]; |
|
97
|
|
|
$api = new HiveCondenser($apiConfig); |
|
98
|
|
|
|
|
99
|
|
|
$cache_interval = $settings['delay']; |
|
100
|
|
|
|
|
101
|
|
|
$current_time = time(); |
|
102
|
|
|
if ((!file_exists($accountFile)) || ($current_time - filemtime($accountFile) > $cache_interval)) { |
|
103
|
|
|
$result = json_encode($api->getAccounts($settings['author']), JSON_PRETTY_PRINT); |
|
104
|
|
|
file_put_contents($accountFile, $result); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$account = json_decode(file_get_contents($accountFile), true); |
|
108
|
|
|
|
|
109
|
|
|
return $this->app->get('view')->render($response, '/admin/admin-index.html', [ |
|
110
|
|
|
'settings' => $settings, |
|
111
|
|
|
'account' => $account[0], |
|
112
|
|
|
'nbPosts' => $nbPosts |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* * Admin settings function |
|
118
|
|
|
* * |
|
119
|
|
|
* This function display tthe settings page |
|
120
|
|
|
* This page contains every Superhive settings (not plugins settings).. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
123
|
|
|
* |
|
124
|
|
|
* @return \Psr\Http\Message\ResponseInterface $response |
|
125
|
|
|
* */ |
|
126
|
|
|
public function adminSettings(Response $response): Response |
|
127
|
|
|
{ |
|
128
|
|
|
// Create array from config file |
|
129
|
|
|
$settings = $this->app->get('settings'); |
|
130
|
|
|
$accountFile = $this->app->get('accountfile'); |
|
131
|
|
|
$langFile = $this->app->get('basedir') . 'resources/languages.json'; |
|
132
|
|
|
$nodesFile = $this->app->get('basedir') . 'resources/nodes.json'; |
|
133
|
|
|
|
|
134
|
|
|
$apiConfig = [ |
|
135
|
|
|
'hiveNode' => $settings['api'], |
|
136
|
|
|
'debug' => false, |
|
137
|
|
|
]; |
|
138
|
|
|
$api = new HiveCondenser($apiConfig); |
|
139
|
|
|
|
|
140
|
|
|
$cache_interval = $settings['delay']; |
|
141
|
|
|
|
|
142
|
|
|
$current_time = time(); |
|
143
|
|
|
if ((!file_exists($accountFile)) || ($current_time - filemtime($accountFile) > $cache_interval)) { |
|
144
|
|
|
$result = json_encode($api->getAccounts($settings['author']), JSON_PRETTY_PRINT); |
|
145
|
|
|
file_put_contents($accountFile, $result); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$account = json_decode(file_get_contents($accountFile), true); |
|
149
|
|
|
$langs = json_decode(file_get_contents($langFile), true); |
|
150
|
|
|
$nodes = json_decode(file_get_contents($nodesFile), true); |
|
151
|
|
|
|
|
152
|
|
|
return $this->app->get('view')->render($response, '/admin/admin-settings.html', [ |
|
153
|
|
|
'settings' => $settings, |
|
154
|
|
|
'account' => $account[0], |
|
155
|
|
|
'languages' => $langs, |
|
156
|
|
|
'nodes' => $nodes, |
|
157
|
|
|
]); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* * Admin theme function |
|
162
|
|
|
* * |
|
163
|
|
|
* This function is for the Theme page |
|
164
|
|
|
* |
|
165
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
166
|
|
|
* |
|
167
|
|
|
* @return \Psr\Http\Message\ResponseInterface $response |
|
168
|
|
|
* */ |
|
169
|
|
|
public function adminThemes(Response $response): Response |
|
170
|
|
|
{ |
|
171
|
|
|
// Create array from config file |
|
172
|
|
|
$settings = $this->app->get('settings'); |
|
173
|
|
|
|
|
174
|
|
|
$themes = array_map('basename', glob($this->app->get('themesdir') . '*', GLOB_ONLYDIR)); |
|
175
|
|
|
return $this->app->get('view')->render($response, '/admin/admin-themes.html', [ |
|
176
|
|
|
'settings' => $settings, |
|
177
|
|
|
'themes' => $themes, |
|
178
|
|
|
]); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* * Admin logout function |
|
183
|
|
|
* * |
|
184
|
|
|
* This function clear ther session, destroy it, and redirect to login page. |
|
185
|
|
|
* |
|
186
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
187
|
|
|
* |
|
188
|
|
|
* @return \Psr\Http\Message\ResponseInterface $response |
|
189
|
|
|
* */ |
|
190
|
|
|
public function logout(Response $response): Response |
|
191
|
|
|
{ |
|
192
|
|
|
$session = $this->app->get('session'); |
|
193
|
|
|
|
|
194
|
|
|
$session->delete('sh_author'); |
|
195
|
|
|
$session->delete('sh_sign'); |
|
196
|
|
|
$session::destroy(); |
|
197
|
|
|
|
|
198
|
|
|
return $response->withHeader('Location', '/login')->withStatus(302); |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* * Admin save function |
|
203
|
|
|
* * |
|
204
|
|
|
* This function Take every fields in the form and convert the into a (human-readable))JSON file. |
|
205
|
|
|
* the generated file will be save in config folder. |
|
206
|
|
|
* |
|
207
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
|
208
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
209
|
|
|
* |
|
210
|
|
|
* @return \Psr\Http\Message\ResponseInterface $response |
|
211
|
|
|
* */ |
|
212
|
|
|
public function save(Request $request, Response $response): Response |
|
213
|
|
|
{ |
|
214
|
|
|
$data = $request->getParsedBody(); |
|
215
|
|
|
if (isset($data['redirect'])) { |
|
216
|
|
|
$redirect = $data['redirect']; |
|
217
|
|
|
} else { |
|
218
|
|
|
$redirect = '/admin/'; |
|
219
|
|
|
} |
|
220
|
|
|
$settings = $this->app->get('settings'); |
|
221
|
|
|
|
|
222
|
|
|
foreach ($data as $key => $value) { |
|
223
|
|
|
if ($value === "true") { |
|
224
|
|
|
$value = (bool) true; |
|
225
|
|
|
} |
|
226
|
|
|
if ($value === "false") { |
|
227
|
|
|
$value = (bool) false; |
|
228
|
|
|
} |
|
229
|
|
|
if (mb_strpos($key, "-") !== false) { |
|
230
|
|
|
$pieces = explode("-", $key); |
|
231
|
|
|
if (array_key_exists($pieces[1], $settings[$pieces[0]])) { |
|
232
|
|
|
$settings[$pieces[0]][$pieces[1]] = $value; |
|
233
|
|
|
} |
|
234
|
|
|
} else { |
|
235
|
|
|
if (array_key_exists($key, $settings)) { |
|
236
|
|
|
$settings[$key] = $value; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
$file = json_encode($settings, JSON_PRETTY_PRINT); |
|
242
|
|
|
// Create array from config file |
|
243
|
|
|
file_put_contents($this->app->get('configfile'), $file); |
|
244
|
|
|
unlink($this->app->get('blogfile')); |
|
245
|
|
|
|
|
246
|
|
|
return $response->withHeader('Location', $redirect)->withStatus(302); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* * Admin theme save function |
|
251
|
|
|
* * |
|
252
|
|
|
* This function is for save the theme into the JSON config file |
|
253
|
|
|
* |
|
254
|
|
|
* @param string $theme |
|
255
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
256
|
|
|
* |
|
257
|
|
|
* @return \Psr\Http\Message\ResponseInterface $response |
|
258
|
|
|
* */ |
|
259
|
|
|
public function saveTheme(string $theme, Response $response): Response |
|
260
|
|
|
{ |
|
261
|
|
|
$settings = $this->app->get('settings'); |
|
262
|
|
|
$settings['theme'] = $theme; |
|
263
|
|
|
$file = json_encode($settings, JSON_PRETTY_PRINT); |
|
264
|
|
|
file_put_contents($this->app->get('configfile'), $file); |
|
265
|
|
|
return $response->withHeader('Location', '/admin/themes')->withStatus(302); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|