1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\Firewall\Panel; |
14
|
|
|
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use Shieldon\Firewall\Panel\BaseController; |
17
|
|
|
use Shieldon\Firewall\Driver as Driver; |
18
|
|
|
use function Shieldon\Firewall\__; |
19
|
|
|
use function Shieldon\Firewall\get_request; |
20
|
|
|
use function Shieldon\Firewall\unset_superglobal; |
21
|
|
|
|
22
|
|
|
use ReflectionObject; |
23
|
|
|
use function count; |
24
|
|
|
use function date; |
25
|
|
|
use function get_class; |
26
|
|
|
use function ksort; |
27
|
|
|
use function round; |
28
|
|
|
use function sleep; |
29
|
|
|
use function strrpos; |
30
|
|
|
use function strtolower; |
31
|
|
|
use function strtotime; |
32
|
|
|
use function substr; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Home |
36
|
|
|
*/ |
37
|
|
|
class Home extends BaseController |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Constructor |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
parent::__construct(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Default entry |
49
|
|
|
* |
50
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
51
|
|
|
*/ |
52
|
|
|
public function index(): ResponseInterface |
53
|
|
|
{ |
54
|
|
|
return $this->overview(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Overview |
59
|
|
|
* |
60
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
61
|
|
|
*/ |
62
|
|
|
public function overview(): ResponseInterface |
63
|
|
|
{ |
64
|
|
|
// Collection of the template variables. |
65
|
|
|
$data = []; |
66
|
|
|
|
67
|
|
|
// Handle the form post action. |
68
|
|
|
$this->overviewFormPost(); |
69
|
|
|
|
70
|
|
|
/* |
71
|
|
|
|-------------------------------------------------------------------------- |
72
|
|
|
| Logger |
73
|
|
|
|-------------------------------------------------------------------------- |
74
|
|
|
| |
75
|
|
|
| All logs were recorded by ActionLogger. |
76
|
|
|
| Get the summary information from those logs. |
77
|
|
|
| |
78
|
|
|
*/ |
79
|
|
|
|
80
|
|
|
$data = $this->overviewTemplateVarsOfActionLogger($data); |
81
|
|
|
|
82
|
|
|
/* |
83
|
|
|
|-------------------------------------------------------------------------- |
84
|
|
|
| Data circle |
85
|
|
|
|-------------------------------------------------------------------------- |
86
|
|
|
| |
87
|
|
|
| A data circle includes the primary data tables of Shieldon. |
88
|
|
|
| They are ip_log_table, ip_rule_table and session_table. |
89
|
|
|
| |
90
|
|
|
*/ |
91
|
|
|
|
92
|
|
|
$data = $this->overviewTemplateVarsOfDataCircle($data); |
93
|
|
|
|
94
|
|
|
/* |
95
|
|
|
|-------------------------------------------------------------------------- |
96
|
|
|
| Shieldon status |
97
|
|
|
|-------------------------------------------------------------------------- |
98
|
|
|
| |
99
|
|
|
| 1. Components. |
100
|
|
|
| 2. Filters. |
101
|
|
|
| 3. Configuration. |
102
|
|
|
| 4. Data drivers. |
103
|
|
|
| 5. Captcha modules. |
104
|
|
|
| 6. Messenger modules. |
105
|
|
|
| |
106
|
|
|
*/ |
107
|
|
|
|
108
|
|
|
$data = $this->overviewTemplateVarsOfComponents($data); |
109
|
|
|
$data = $this->overviewTemplateVarsOfFilters($data); |
110
|
|
|
$data = $this->overviewTemplateVarsOfConfiguration($data); |
111
|
|
|
$data = $this->overviewTemplateVarsOfDataDrivers($data); |
112
|
|
|
$data = $this->overviewTemplateVarsOfCaptchas($data); |
113
|
|
|
$data = $this->overviewTemplateVarsOfMessengers($data); |
114
|
|
|
|
115
|
|
|
// Page title is also needed. |
116
|
|
|
$data['title'] = __('panel', 'title_overview', 'Overview'); |
117
|
|
|
|
118
|
|
|
return $this->renderPage('panel/overview', $data); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Detect and handle form post action. |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
private function overviewFormPost() |
127
|
|
|
{ |
128
|
|
|
$postParams = get_request()->getParsedBody(); |
129
|
|
|
|
130
|
|
|
if (isset($postParams['action_type'])) { |
131
|
|
|
|
132
|
|
|
switch ($postParams['action_type']) { |
133
|
|
|
|
134
|
|
|
case 'reset_data_circle': |
135
|
|
|
$this->setConfig('cronjob.reset_circle.config.last_update', date('Y-m-d H:i:s')); |
136
|
|
|
$this->kernel->driver->rebuild(); |
137
|
|
|
sleep(2); |
138
|
|
|
|
139
|
|
|
unset_superglobal('action_type', 'post'); |
140
|
|
|
|
141
|
|
|
$this->saveConfig(); |
142
|
|
|
|
143
|
|
|
$this->pushMessage('success', |
144
|
|
|
__( |
145
|
|
|
'panel', |
146
|
|
|
'reset_data_circle', |
147
|
|
|
'Data circle tables have been reset.' |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
break; |
151
|
|
|
|
152
|
|
|
case 'reset_action_logs': |
153
|
|
|
$this->kernel->logger->purgeLogs(); |
154
|
|
|
sleep(2); |
155
|
|
|
|
156
|
|
|
$this->pushMessage('success', |
157
|
|
|
__( |
158
|
|
|
'panel', |
159
|
|
|
'reset_action_logs', |
160
|
|
|
'Action logs have been removed.' |
161
|
|
|
) |
162
|
|
|
); |
163
|
|
|
break; |
164
|
|
|
|
165
|
|
|
default: |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Template variables of the section of Action Logger. |
172
|
|
|
* |
173
|
|
|
* @param array $data The template varibles. |
174
|
|
|
* |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
|
|
private function overviewTemplateVarsOfActionLogger(array $data = []): array |
178
|
|
|
{ |
179
|
|
|
$data['action_logger'] = false; |
180
|
|
|
|
181
|
|
|
if (!empty($this->kernel->logger)) { |
182
|
|
|
$loggerInfo = $this->kernel->logger->getCurrentLoggerInfo(); |
183
|
|
|
$data['action_logger'] = true; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$data['logger_started_working_date'] = 'No record'; |
187
|
|
|
$data['logger_work_days'] = '0 day'; |
188
|
|
|
$data['logger_total_size'] = '0 MB'; |
189
|
|
|
|
190
|
|
|
if (!empty($loggerInfo)) { |
191
|
|
|
|
192
|
|
|
$i = 0; |
193
|
|
|
ksort($loggerInfo); |
194
|
|
|
|
195
|
|
|
foreach ($loggerInfo as $filename => $size) { |
196
|
|
|
$filename = (string) $filename; |
197
|
|
|
if (false === strpos($filename, '.json')) { |
198
|
|
|
if (0 === $i) { |
199
|
|
|
$data['logger_started_working_date'] = date('Y-m-d', strtotime($filename)); |
200
|
|
|
} |
201
|
|
|
$i += (int) $size; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$data['logger_work_days'] = count($loggerInfo); |
206
|
|
|
$data['logger_total_size'] = round($i / (1024 * 1024), 5) . ' MB'; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $data; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Template variables of the section of Data Circle. |
214
|
|
|
* |
215
|
|
|
* @param array $data The template varibles. |
216
|
|
|
* |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
|
|
private function overviewTemplateVarsOfDataCircle(array $data = []): array |
220
|
|
|
{ |
221
|
|
|
$data['rule_list'] = $this->kernel->driver->getAll('rule'); |
222
|
|
|
$data['ip_log_list'] = $this->kernel->driver->getAll('filter'); |
223
|
|
|
$data['session_list'] = $this->kernel->driver->getAll('session'); |
224
|
|
|
|
225
|
|
|
return $data; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Template variables of the section of Shieldon Status. |
230
|
|
|
* Displayed on the Components area. |
231
|
|
|
* |
232
|
|
|
* @param array $data The template varibles. |
233
|
|
|
* |
234
|
|
|
* @return array |
235
|
|
|
*/ |
236
|
|
|
private function overviewTemplateVarsOfComponents(array $data = []): array |
237
|
|
|
{ |
238
|
|
|
$data['components'] = [ |
239
|
|
|
'Ip' => (!empty($this->kernel->component['Ip'])) ? true : false, |
240
|
|
|
'TrustedBot' => (!empty($this->kernel->component['TrustedBot'])) ? true : false, |
241
|
|
|
'Header' => (!empty($this->kernel->component['Header'])) ? true : false, |
242
|
|
|
'Rdns' => (!empty($this->kernel->component['Rdns'])) ? true : false, |
243
|
|
|
'UserAgent' => (!empty($this->kernel->component['UserAgent'])) ? true : false, |
244
|
|
|
]; |
245
|
|
|
|
246
|
|
|
return $data; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Template variables of the section of Shieldon Status. |
251
|
|
|
* Displayed on the Filters area. |
252
|
|
|
* |
253
|
|
|
* @param array $data The template varibles. |
254
|
|
|
* |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
|
|
private function overviewTemplateVarsOfFilters(array $data = []): array |
258
|
|
|
{ |
259
|
|
|
$reflection = new ReflectionObject($this->kernel); |
260
|
|
|
$t = $reflection->getProperty('filterStatus'); |
261
|
|
|
$t->setAccessible(true); |
262
|
|
|
$filterStatus = $t->getValue($this->kernel); |
263
|
|
|
|
264
|
|
|
$data['filters'] = $filterStatus; |
265
|
|
|
|
266
|
|
|
return $data; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Template variables of the section of Shieldon Status. |
271
|
|
|
* Displayed on the Configuration area. |
272
|
|
|
* |
273
|
|
|
* @param array $data The template varibles. |
274
|
|
|
* |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
|
|
private function overviewTemplateVarsOfConfiguration(array $data = []): array |
278
|
|
|
{ |
279
|
|
|
$reflection = new ReflectionObject($this->kernel); |
280
|
|
|
$t = $reflection->getProperty('properties'); |
281
|
|
|
$t->setAccessible(true); |
282
|
|
|
$properties = $t->getValue($this->kernel); |
283
|
|
|
|
284
|
|
|
$data['configuration'] = $properties; |
285
|
|
|
|
286
|
|
|
return $data; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Template variables of the section of Shieldon Status. |
291
|
|
|
* Displayed on the Data Drivers area. |
292
|
|
|
* |
293
|
|
|
* @param array $data The template varibles. |
294
|
|
|
* |
295
|
|
|
* @return array |
296
|
|
|
*/ |
297
|
|
|
private function overviewTemplateVarsOfDataDrivers(array $data = []): array |
298
|
|
|
{ |
299
|
|
|
$data['driver'] = [ |
300
|
|
|
'mysql' => ($this->kernel->driver instanceof Driver\MysqlDriver), |
301
|
|
|
'redis' => ($this->kernel->driver instanceof Driver\RedisDriver), |
302
|
|
|
'file' => ($this->kernel->driver instanceof Driver\FileDriver), |
303
|
|
|
'sqlite' => ($this->kernel->driver instanceof Driver\SqliteDriver), |
304
|
|
|
]; |
305
|
|
|
|
306
|
|
|
return $data; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Template variables of the section of Shieldon Status. |
311
|
|
|
* Displayed on the Captchas area. |
312
|
|
|
* |
313
|
|
|
* @param array $data The template varibles. |
314
|
|
|
* |
315
|
|
|
* @return array |
316
|
|
|
*/ |
317
|
|
|
private function overviewTemplateVarsOfCaptchas(array $data = []): array |
318
|
|
|
{ |
319
|
|
|
$reflection = new ReflectionObject($this->kernel); |
320
|
|
|
$t = $reflection->getProperty('captcha'); |
321
|
|
|
$t->setAccessible(true); |
322
|
|
|
$captcha = $t->getValue($this->kernel); |
323
|
|
|
|
324
|
|
|
$data['captcha'] = [ |
325
|
|
|
'recaptcha' => (isset($captcha['Recaptcha']) ? true : false), |
326
|
|
|
'imagecaptcha' => (isset($captcha['ImageCaptcha']) ? true : false), |
327
|
|
|
]; |
328
|
|
|
|
329
|
|
|
return $data; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Template variables of the section of Shieldon Status. |
334
|
|
|
* Displayed on the Messengers area. |
335
|
|
|
* |
336
|
|
|
* @param array $data The template varibles. |
337
|
|
|
* |
338
|
|
|
* @return array |
339
|
|
|
*/ |
340
|
|
|
private function overviewTemplateVarsOfMessengers(array $data = []): array |
341
|
|
|
{ |
342
|
|
|
$reflection = new ReflectionObject($this->kernel); |
343
|
|
|
$t = $reflection->getProperty('messenger'); |
344
|
|
|
$t->setAccessible(true); |
345
|
|
|
$messengers = $t->getValue($this->kernel); |
346
|
|
|
|
347
|
|
|
$operatingMessengers = [ |
348
|
|
|
'telegram' => false, |
349
|
|
|
'linenotify' => false, |
350
|
|
|
'sendgrid' => false, |
351
|
|
|
'mailgun' => false, |
352
|
|
|
'smtp' => false, |
353
|
|
|
'slack' => false, |
354
|
|
|
'slackwebhook' => false, |
355
|
|
|
'rocketchat' => false, |
356
|
|
|
'mail' => false, |
357
|
|
|
]; |
358
|
|
|
|
359
|
|
|
foreach ($messengers as $messenger) { |
360
|
|
|
$class = get_class($messenger); |
361
|
|
|
$class = strtolower(substr($class, strrpos($class, '\\') + 1)); |
362
|
|
|
|
363
|
|
|
if (isset($operatingMessengers[$class])) { |
364
|
|
|
$operatingMessengers[$class] = true; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
$data['messengers'] = $operatingMessengers; |
369
|
|
|
|
370
|
|
|
return $data; |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
|