1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <contact@terryl.in>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20

21
declare(strict_types=1);
22

23
namespace Shieldon\Firewall\Panel;
24

25
use Psr\Http\Message\ResponseInterface;
26
use Shieldon\Firewall\Panel\BaseController;
27
use Shieldon\Firewall\Driver as Driver;
28
use ReflectionObject;
29
use function Shieldon\Firewall\__;
30
use function Shieldon\Firewall\get_request;
31
use function Shieldon\Firewall\unset_superglobal;
32
use function count;
33
use function date;
34
use function get_class;
35
use function ksort;
36
use function round;
37
use function sleep;
38
use function strrpos;
39
use function strtolower;
40
use function strtotime;
41
use function substr;
42

43
/**
44
 * Home
45
 */
46
class Home extends BaseController
47
{
48
    /**
49
     *   Public methods       | Desctiotion
50
     *  ----------------------|---------------------------------------------
51
     *   overview             | The overview page.
52
     *  ----------------------|---------------------------------------------
53
     */
54

55
    /**
56
     * Constructor
57
     */
58 3
    public function __construct() 
59
    {
60 3
        parent::__construct();
61
    }
62

63
    // @codeCoverageIgnoreStart
64

65
    /**
66
     * Default entry
67
     * 
68
     * @return ResponseInterface
69
     */
70
    public function index(): ResponseInterface
71
    {
72
        
73
        return $this->overview();
74
    }
75

76
    // @codeCoverageIgnoreEnd
77

78
    /**
79
     * Overview
80
     *
81
     * @return ResponseInterface
82
     */
83 3
    public function overview(): ResponseInterface
84
    {
85
        // Collection of the template variables.
86 3
        $data = [];
87

88
        // Handle the form post action.
89 3
        $this->overviewFormPost();
90

91
        /*
92
        |--------------------------------------------------------------------------
93
        | Logger
94
        |--------------------------------------------------------------------------
95
        |
96
        | All logs were recorded by ActionLogger.
97
        | Get the summary information from those logs.
98
        |
99
        */
100

101 3
        $data = $this->overviewTemplateVarsOfActionLogger($data);
102

103
        /*
104
        |--------------------------------------------------------------------------
105
        | Data circle
106
        |--------------------------------------------------------------------------
107
        |
108
        | A data circle includes the primary data tables of Shieldon.
109
        | They are ip_log_table, ip_rule_table and session_table.
110
        |
111
        */
112

113 3
        $data = $this->overviewTemplateVarsOfDataCircle($data);
114
 
115
        /*
116
        |--------------------------------------------------------------------------
117
        | Shieldon status
118
        |--------------------------------------------------------------------------
119
        |
120
        | 1. Components.
121
        | 2. Filters.
122
        | 3. Configuration.
123
        | 4. Data drivers.
124
        | 5. Captcha modules.
125
        | 6. Messenger modules.
126
        |
127
        */
128

129 3
        $data = $this->overviewTemplateVarsOfComponents($data);
130 3
        $data = $this->overviewTemplateVarsOfFilters($data);
131 3
        $data = $this->overviewTemplateVarsOfConfiguration($data);
132 3
        $data = $this->overviewTemplateVarsOfDataDrivers($data);
133 3
        $data = $this->overviewTemplateVarsOfCaptchas($data);
134 3
        $data = $this->overviewTemplateVarsOfMessengers($data);
135

136
        // Page title is also needed.
137 3
        $data['title'] = __('panel', 'title_overview', 'Overview');
138

139 3
        return $this->renderPage('panel/overview', $data);
140
    }
141

142
    /**
143
     * Detect and handle form post action.
144
     *
145
     * @return void
146
     */
147 3
    private function overviewFormPost()
148
    {
149 3
        $postParams = get_request()->getParsedBody();
150

151 3
        if (isset($postParams['action_type'])) {
152

153 3
            switch ($postParams['action_type']) {
154

155 3
                case 'reset_data_circle':
156 3
                    $this->setConfig('cronjob.reset_circle.config.last_update', date('Y-m-d H:i:s'));
157 3
                    $this->kernel->driver->rebuild();
158 3
                    sleep(2);
159

160 3
                    unset_superglobal('action_type', 'post');
161

162 3
                    $this->saveConfig();
163

164 3
                    $this->pushMessage(
165 3
                        'success',
166 3
                        __(
167 3
                            'panel',
168 3
                            'reset_data_circle',
169 3
                            'Data circle tables have been reset.'
170
                        )
171
                    );
172 3
                    break;
173

174 3
                case 'reset_action_logs':
175 3
                    $this->kernel->logger->purgeLogs();
176 3
                    sleep(2);
177

178 3
                    $this->pushMessage(
179 3
                        'success',
180 3
                        __(
181 3
                            'panel',
182 3
                            'reset_action_logs',
183 3
                            'Action logs have been removed.'
184
                        )
185
                    );
186 3
                    break;
187
            }
188
        }
189
    }
190

191
    /**
192
     * Template variables of the section of Action Logger.
193
     *
194
     * @param array $data The template varibles.
195
     *
196
     * @return array
197
     */
198 3
    private function overviewTemplateVarsOfActionLogger(array $data = []): array
199
    {
200 3
        $data['action_logger'] = false;
201

202 3
        if (!empty($this->kernel->logger)) {
203 3
            $loggerInfo = $this->kernel->logger->getCurrentLoggerInfo();
204 3
            $data['action_logger'] = true;
205
        }
206

207 3
        $data['logger_started_working_date'] = 'No record';
208 3
        $data['logger_work_days'] = '0 day';
209 3
        $data['logger_total_size'] = '0 MB';
210

211 3
        if (!empty($loggerInfo)) {
212

213 3
            $i = 0;
214 3
            ksort($loggerInfo);
215

216 3
            foreach ($loggerInfo as $filename => $size) {
217 3
                $filename = (string) $filename;
218 3
                if (false === strpos($filename, '.json')) {
219 3
                    if (0 === $i) {
220 3
                        $data['logger_started_working_date'] = date('Y-m-d', strtotime($filename));
221
                    }
222 3
                    $i += (int) $size;
223
                }
224
            }
225

226 3
            $data['logger_work_days'] = count($loggerInfo);
227 3
            $data['logger_total_size'] = round($i / (1024 * 1024), 5) . ' MB';
228
        }
229

230 3
        return $data;
231
    }
232

233
    /**
234
     * Template variables of the section of Data Circle.
235
     *
236
     * @param array $data The template varibles.
237
     *
238
     * @return array
239
     */
240 3
    private function overviewTemplateVarsOfDataCircle(array $data = []): array
241
    {
242 3
        $data['rule_list'] = $this->kernel->driver->getAll('rule');
243 3
        $data['ip_log_list'] = $this->kernel->driver->getAll('filter');
244 3
        $data['session_list'] = $this->kernel->driver->getAll('session');
245

246 3
        return $data;
247
    }
248

249
    /**
250
     * Template variables of the section of Shieldon Status.
251
     * Displayed on the Components area.
252
     *
253
     * @param array $data The template varibles.
254
     *
255
     * @return array
256
     */
257 3
    private function overviewTemplateVarsOfComponents(array $data = []): array
258
    {
259 3
        $data['components'] = [
260 3
            'Ip'         => (!empty($this->kernel->component['Ip']))         ? true : false,
261 3
            'TrustedBot' => (!empty($this->kernel->component['TrustedBot'])) ? true : false,
262 3
            'Header'     => (!empty($this->kernel->component['Header']))     ? true : false,
263 3
            'Rdns'       => (!empty($this->kernel->component['Rdns']))       ? true : false,
264 3
            'UserAgent'  => (!empty($this->kernel->component['UserAgent']))  ? true : false,
265
        ];
266

267 3
        return $data;
268
    }
269

270
    /**
271
     * Template variables of the section of Shieldon Status.
272
     * Displayed on the Filters area.
273
     *
274
     * @param array $data The template varibles.
275
     *
276
     * @return array
277
     */
278 3
    private function overviewTemplateVarsOfFilters(array $data = []): array
279
    {
280 3
        $reflection = new ReflectionObject($this->kernel);
281 3
        $t = $reflection->getProperty('filterStatus');
282 3
        $t->setAccessible(true);
283 3
        $filterStatus = $t->getValue($this->kernel);
284

285 3
        $data['filters'] = $filterStatus;
286

287 3
        return $data;
288
    }
289

290
    /**
291
     * Template variables of the section of Shieldon Status.
292
     * Displayed on the Configuration area.
293
     *
294
     * @param array $data The template varibles.
295
     *
296
     * @return array
297
     */
298 3
    private function overviewTemplateVarsOfConfiguration(array $data = []): array
299
    {
300 3
        $reflection = new ReflectionObject($this->kernel);
301 3
        $t = $reflection->getProperty('properties');
302 3
        $t->setAccessible(true);
303 3
        $properties = $t->getValue($this->kernel);
304
        
305 3
        $data['configuration'] = $properties;
306

307 3
        return $data;
308
    }
309

310
    /**
311
     * Template variables of the section of Shieldon Status.
312
     * Displayed on the Data Drivers area.
313
     *
314
     * @param array $data The template varibles.
315
     *
316
     * @return array
317
     */
318 3
    private function overviewTemplateVarsOfDataDrivers(array $data = []): array
319
    {
320 3
        $data['driver'] = [
321 3
            'mysql'  => ($this->kernel->driver instanceof Driver\MysqlDriver),
322 3
            'redis'  => ($this->kernel->driver instanceof Driver\RedisDriver),
323 3
            'file'   => ($this->kernel->driver instanceof Driver\FileDriver),
324 3
            'sqlite' => ($this->kernel->driver instanceof Driver\SqliteDriver),
325
        ];
326

327 3
        return $data;
328
    }
329

330
    /**
331
     * Template variables of the section of Shieldon Status.
332
     * Displayed on the Captchas area.
333
     *
334
     * @param array $data The template varibles.
335
     *
336
     * @return array
337
     */
338 3
    private function overviewTemplateVarsOfCaptchas(array $data = []): array
339
    {
340 3
        $reflection = new ReflectionObject($this->kernel);
341 3
        $t = $reflection->getProperty('captcha');
342 3
        $t->setAccessible(true);
343 3
        $captcha = $t->getValue($this->kernel);
344

345 3
        $data['captcha'] = [
346 3
            'recaptcha'    => (isset($captcha['ReCaptcha']) ? true : false),
347 3
            'imagecaptcha' => (isset($captcha['ImageCaptcha']) ? true : false),
348
        ];
349

350 3
        return $data;
351
    }
352

353
    /**
354
     * Template variables of the section of Shieldon Status.
355
     * Displayed on the Messengers area.
356
     *
357
     * @param array $data The template varibles.
358
     *
359
     * @return array
360
     */
361 3
    private function overviewTemplateVarsOfMessengers(array $data = []): array
362
    {
363 3
        $reflection = new ReflectionObject($this->kernel);
364 3
        $t = $reflection->getProperty('messenger');
365 3
        $t->setAccessible(true);
366 3
        $messengers = $t->getValue($this->kernel);
367

368
        $operatingMessengers = [
369 3
            'telegram'     => false,
370
            'linenotify'   => false,
371
            'sendgrid'     => false,
372
            'mailgun'      => false,
373
            'smtp'         => false,
374
            'slack'        => false,
375
            'slackwebhook' => false,
376
            'rocketchat'   => false,
377
            'mail'         => false,
378
        ];
379

380 3
        foreach ($messengers as $messenger) {
381 3
            $class = get_class($messenger);
382 3
            $class = strtolower(substr($class, strrpos($class, '\\') + 1));
383

384 3
            if (isset($operatingMessengers[$class])) {
385 3
                $operatingMessengers[$class] = true;
386
            }
387
        }
388

389 3
        $data['messengers'] = $operatingMessengers;
390

391 3
        return $data;
392
    }
393
}

Read our documentation on viewing source code .

Loading