ActionLogParser::getIpData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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
 * php version 7.1.0
11
 *
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
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\Log;
24
25
use Shieldon\Firewall\Log\ActionLogger as Logger;
26
27
use function date;
28
use function round;
29
use function strtotime;
30
31
/**
32
 * Parse the log files that created by ActionLogger.
33
 */
34
final class ActionLogParser
35
{
36
    // Log codes. Same as Shieldon action codes.
37
    const LOG_BAN = 0;
38
    const LOG_ALLOW = 1;
39
    const LOG_TEMPORARILY_BAN = 2;
40
    const LOG_UNBAN = 9;
41
    
42
    const LOG_LIMIT = 3;
43
    const LOG_PAGEVIEW = 11;
44
    const LOG_BLACKLIST = 98;
45
    const LOG_CAPTCHA = 99;
46
47
    /**
48
     * Statistic data fields.
49
     *
50
     * @var array
51
     */
52
    protected $fields = [];
53
    
54
    /**
55
     * Period type of the statistic data.
56
     *
57
     * @var array
58
     */
59
    protected $periods = [];
60
61
    /**
62
     * Data detail.
63
     *
64
     * For example:
65
     * $this->periodDetail['today']['12:00 am'][$field] = 7;
66
     *
67
     * @var array
68
     */
69
    protected $periodDetail = [];
70
71
    /**
72
     * IP Detail
73
     *
74
     * For example:
75
     * $this->ipDetail['today']['127.0.0.1'][$fields] = 6;
76
     *
77
     * @var array
78
     */
79
    protected $ipDetail = [];
80
81
    /**
82
     * ActionLogger instance.
83
     *
84
     * @var ActionLogger
85
     */
86
    protected $logger;
87
88
    /**
89
     * Period type.
90
     *
91
     * @var string
92
     */
93
    protected $type = 'today';
94
95
    /**
96
     * Constructer.
97
     *
98
     * @param string $directory The directory where to store the logs in.
99
     */
100 79
    public function __construct(string $directory = '')
101
    {
102 79
        if (!isset($this->logger)) {
103 79
            $this->logger = new Logger($directory);
104
        }
105
106 79
        $this->fields = [
107 79
            'captcha_count',
108 79
            'captcha_success_count',
109 79
            'captcha_failure_count',
110 79
            'pageview_count',
111 79
            'action_ban_count',
112 79
            'action_temp_ban_count',
113 79
            'action_unban_count',
114 79
            'blacklist_count',
115 79
            'session_limit_count',
116 79
            'captcha_failure_percentage',
117 79
            'captcha_success_percentage',
118 79
        ];
119
120
        // range: today ~ now
121 79
        $this->periods['today'] = [
122 79
            'timestamp_begin' => strtotime('today'),
123 79
            'timestamp_end'   => strtotime('tomorrow'),
124 79
            'display_format'  => 'h:00 a',
125 79
            'display_count'   => 24,
126 79
            'period'          => 3600,
127 79
        ];
128
        
129
        // range: yesterday ~ today
130 79
        $this->periods['yesterday'] = [
131 79
            'timestamp_begin' => strtotime('yesterday'),
132 79
            'timestamp_end'   => strtotime('today'),
133 79
            'display_format'  => 'H:00',
134 79
            'display_count'   => 24,
135 79
            'period'          => 3600,
136 79
        ];
137
138
        // range: past_seven_hours ~ now
139 79
        $this->periods['past_seven_hours'] = [
140 79
            'timestamp_begin' => strtotime(date('Y-m-d H:00:00', strtotime('-7 hours'))),
141 79
            'timestamp_end'   => strtotime(date('Y-m-d H:00:00', strtotime('-1 hours'))),
142 79
            'display_format'  => 'H:00',
143 79
            'display_count'   => 7,
144 79
            'period'          => 3600,
145 79
        ];
146
147
        // range: past_seven_days ~ today
148 79
        $this->periods['past_seven_days'] = [
149 79
            'timestamp_begin' => strtotime(date('Ymd', strtotime('-7 days'))),
150 79
            'timestamp_end'   => strtotime('today'),
151 79
            'display_format'  => 'D',
152 79
            'display_count'   => 7,
153 79
            'period'          => 86400,
154 79
        ];
155
156
        // range: last_month ~ today
157 79
        $this->periods['this_month'] = [
158 79
            'timestamp_begin' => strtotime(gmdate('Ym' . '01')),
159 79
            'timestamp_end'   => strtotime('today'),
160 79
            'display_format'  => 'Y.m.d',
161 79
            'display_count'   => gmdate('j'),
162 79
            'period'          => 86400,
163 79
        ];
164
165
        // range: last_month ~ this_month
166 79
        $this->periods['last_month'] = [
167 79
            'timestamp_begin' => strtotime(gmdate('Ym' . '01', strtotime('-1 months'))),
168 79
            'timestamp_end'   => strtotime(gmdate('Ym' . '01')),
169 79
            'display_format'  => 'Y.m.d',
170 79
            'display_count'   => gmdate('j', strtotime('-1 months')),
171 79
            'period'          => 86400,
172 79
        ];
173
    }
174
175
    /**
176
     * Get the start and end date depends on the log type.
177
     *
178
     * @return array
179
     */
180 9
    protected function getStartEndDate(): array
181
    {
182 9
        $dataRange = [
183 9
            'yesterday' => [
184 9
                'start' => date('Ymd', strtotime('yesterday')),
185 9
                'end'   => date('Ymd'),
186 9
            ],
187 9
            'past_seven_days' => [
188 9
                'start' => date('Ymd', strtotime('-7 days')),
189 9
                'end'   => date('Ymd'),
190 9
            ],
191 9
            'this_month' => [
192 9
                'start' => date('Ym') . '01',
193 9
                'end'   => date('Ym') . '31',
194 9
            ],
195 9
            'last_month' => [
196 9
                'start' => date('Ym', strtotime('-1 month')) . '01',
197 9
                'end'   => date('Ym', strtotime('-1 month')) . '31',
198 9
            ],
199 9
            'past_seven_hours' => [
200 9
                'start' => date('Ymd', strtotime('yesterday')),
201 9
                'end'   => date('Ymd'),
202 9
            ],
203 9
            'today' => [
204 9
                'start' => date('Ymd'),
205 9
                'end'   => '',
206 9
            ],
207 9
        ];
208
209 9
        if (empty($dataRange[$this->type])) {
210 2
            if (preg_match('/past_([0-9]+)_days/', $this->type, $matches)) {
211 1
                $dayCount = $matches[1];
212 1
                $startDate = date('Ymd', strtotime('-' . $dayCount . ' days'));
213 1
                $endDate = date('Ymd');
214
215 1
                $this->periods['past_' . $dayCount . '_days'] = [
216 1
                    'timestamp_begin' => strtotime(date('Ymd', strtotime('-' . $dayCount . ' days'))),
217 1
                    'timestamp_end'   => strtotime('today'),
218 1
                    'display_format' => 'D',
219 1
                    'display_count'  => $dayCount,
220 1
                    'period'         => 86400,
221 1
                ];
222
            } else {
223 1
                $startDate = date('Ymd');
224 1
                $endDate = '';
225 2
                $this->periods[$this->type] = $this->periods['today'];
226
            }
227
        } else {
228 7
            $startDate = $dataRange[$this->type]['start'];
229 7
            $endDate = $dataRange[$this->type]['end'];
230
        }
231
232 9
        return [
233 9
            'start' => $startDate,
234 9
            'end' => $endDate,
235 9
        ];
236
    }
237
238
    /**
239
     * Parse specific period of time of data.
240
     *
241
     * Warning: This method may take long time to generate real-time stats on a high-traffic website.
242
     * Aprroximately 10,000 rows for 3-5 seconds, depnonds on your server's CPU speed.
243
     *
244
     * @return void
245
     */
246 9
    public function parsePeriodData(): void
247
    {
248 9
        $dateRange = $this->getStartEndDate();
249 9
        $startDate = $dateRange['start'];
250 9
        $endDate = $dateRange['end'];
251
252
        // Fetch data from log files.
253 9
        $logs = $this->logger->get($startDate, $endDate);
254
255 9
        foreach ($logs as $log) {
256
            $logTimesamp = (int) $log['timestamp'];
257 8
            $logIp = $log['ip'];
258 8
259
            // Add a new field `datetime` that original logs don't have.
260
            $log['datetime'] = date('Y-m-d H:i:s', $logTimesamp);
261 8
            
262
            foreach (array_keys($this->periods) as $t) {
263 8
                for ($i = 0; $i < $this->periods[$t]['display_count']; $i++) {
264
                    $kTimesamp = $this->periods[$t]['timestamp_begin'] + ($i * $this->periods[$t]['period']);
265 8
266
                    $detailTimesampBegin = $kTimesamp;
267 8
                    $detailTimesampEnd = $kTimesamp + $this->periods[$t]['period'];
268
269 8
                    $k = date($this->periods[$t]['display_format'], $kTimesamp);
270 8
271
                    // Initialize all the counters.
272 8
                    foreach ($this->fields as $field) {
273
                        if (!isset($this->periodDetail[$t][$k][$field])) {
274
                            $this->periodDetail[$t][$k][$field] = 0;
275 8
                        }
276 8
277 8
                        if ($logTimesamp >= $detailTimesampBegin && $logTimesamp < $detailTimesampEnd) {
278
                            if (!isset($this->ipDetail[$t][$logIp][$field])) {
279
                                $this->ipDetail[$t][$logIp][$field] = 0;
280 8
                            }
281 8
                        }
282 8
                    }
283
284
                    // Initialize all the counters.
285
                    if ($logTimesamp >= $detailTimesampBegin && $logTimesamp < $detailTimesampEnd) {
286
                        $this->parse($log, $t, $k);
287
                    }
288 8
                }
289 8
            }
290
        }
291
    }
292
    
293
    /**
294
     * Prepare data.
295
     *
296
     * @param string $type Period type.
297
     *
298
     * @return void
299
     */
300
    public function prepare(string $type = 'today'): void
301
    {
302
        $this->type = $type;
303 9
304
        $this->parsePeriodData();
305 9
    }
306
307 9
    /**
308
     * Get data
309
     *
310
     * @return array
311
     */
312
    public function getPeriodData()
313
    {
314
        if (!empty($this->periodDetail[$this->type])) {
315 10
            return $this->periodDetail[$this->type];
316
        }
317 10
        return [];
318 8
    }
319
320 2
    /**
321
     * Get data
322
     *
323
     * @return array
324
     */
325
    public function getIpData()
326
    {
327
        if (!empty($this->ipDetail[$this->type])) {
328 9
            return $this->ipDetail[$this->type];
329
        }
330 9
        return [];
331 8
    }
332
333 9
    /**
334
     * Get parsed perid data.
335
     *
336
     * @param string $ip The IP address.
337
     *
338
     * @return array
339
     */
340
    public function getParsedIpData($ip = ''): array
341
    {
342
        if (empty($ip)) {
343 9
            return [];
344
        }
345 9
346 1
        $results = [];
347
348
        $results['captcha_chart_string']       = ''; // string
349 8
        $results['pageview_chart_string']      = ''; // string
350
        $results['captcha_success_count']      = 0;  // integer
351 8
        $results['captcha_failure_count']      = 0;  // integer
352 8
        $results['captcha_count']              = 0;  // integer
353 8
        $results['pageview_count']             = 0;  // integer
354 8
        $results['captcha_percentageage']      = 0;  // integer
355 8
        $results['captcha_failure_percentage'] = 0;  // integer
356 8
        $results['captcha_success_percentage'] = 0;  // integer
357 8
        $results['action_ban_count']           = 0;  // integer
358 8
        $results['action_temp_ban_count']      = 0;  // integer
359 8
        $results['action_unban_count']         = 0;  // integer
360 8
        $results['blacklist_count']            = 0;  // integer
361 8
        $results['session_limit_count']        = 0;  // integer
362 8
363 8
        $ipdData = $this->getIpData();
364 8
365
        if (!empty($ipdData)) {
366 8
            foreach ($ipdData as $ipKey => $ipInfo) {
367
                if ($ipKey === $ip) {
368 8
                    $results['captcha_success_count'] += $ipInfo['captcha_success_count'];
369
                    $results['captcha_failure_count'] += $ipInfo['captcha_failure_count'];
370 8
                    $results['captcha_count']         += $ipInfo['captcha_count'];
371
                    $results['pageview_count']        += $ipInfo['pageview_count'];
372 8
                    $results['action_ban_count']      += $ipInfo['action_ban_count'];
373 8
                    $results['action_temp_ban_count'] += $ipInfo['action_temp_ban_count'];
374 8
                    $results['action_unban_count']    += $ipInfo['action_unban_count'];
375 8
                    $results['blacklist_count']       += $ipInfo['blacklist_count'];
376 8
                    $results['session_limit_count']   += $ipInfo['session_limit_count'];
377 8
                }
378 8
            }
379 8
380 8
            if ($results['captcha_count'] > 0) {
381 8
                $results['captcha_percentageage'] = (int) (
382
                    round($results['captcha_count'] / ($results['captcha_count'] + $results['pageview_count']), 2) * 100
383
                );
384
                $results['captcha_failure_percentage'] = (int) (
385 8
                    round($results['captcha_failure_count'] / $results['captcha_count'], 2) * 100
386 8
                );
387 8
                $results['captcha_success_percentage'] = (int) (
388 8
                    round($results['captcha_success_count'] / $results['captcha_count'], 2) * 100
389
                );
390
            }
391
        }
392 8
393
        return $results;
394
    }
395
396
    /**
397
     * Get parsed perid data.
398
     *
399
     * @return array
400 9
     */
401
    public function getParsedPeriodData(): array
402 9
    {
403
        $periodData = $this->getPeriodData();
404 9
405
        $results = [];
406 9
407 9
        $results['label_chart_string']         = ''; // string
408 9
        $results['captcha_chart_string']       = ''; // string
409 9
        $results['pageview_chart_string']      = ''; // string
410 9
        $results['captcha_success_count']      = 0;  // integer
411 9
        $results['captcha_failure_count']      = 0;  // integer
412 9
        $results['captcha_count']              = 0;  // integer
413 9
        $results['pageview_count']             = 0;  // integer
414 9
        $results['captcha_percentageage']      = 0;  // integer
415 9
        $results['captcha_failure_percentage'] = 0;  // integer
416 9
        $results['captcha_success_percentage'] = 0;  // integer
417 9
        $results['action_ban_count']           = 0;  // integer
418 9
        $results['action_temp_ban_count']      = 0;  // integer
419 9
        $results['action_unban_count']         = 0;  // integer
420 9
        $results['blacklist_count']            = 0;  // integer
421
        $results['session_limit_count']        = 0;  // integer
422 9
423
        if (!empty($periodData)) {
424 8
            $chartCaptcha = [];
425 8
            $chartPageview = [];
426 8
            $chartCaptchaSuccess = [];
427 8
            $chartCaptchaFailure = [];
428 8
            $labels = [];
429
430 8
            foreach ($periodData as $label => $period) {
431 8
                $labels[] = $label;
432
433 8
                $chartCaptcha[]        = $period['captcha_count'];
434 8
                $chartPageview[]       = $period['pageview_count'];
435 8
                $chartCaptchaSuccess[] = $period['captcha_success_count'];
436 8
                $chartCaptchaFailure[] = $period['captcha_failure_count'];
437
    
438 8
                $results['captcha_success_count'] += $period['captcha_success_count'];
439 8
                $results['captcha_failure_count'] += $period['captcha_failure_count'];
440 8
                $results['captcha_count']         += $period['captcha_count'];
441 8
                $results['pageview_count']        += $period['pageview_count'];
442 8
                $results['action_ban_count']      += $period['action_ban_count'];
443 8
                $results['action_temp_ban_count'] += $period['action_temp_ban_count'];
444 8
                $results['action_unban_count']    += $period['action_unban_count'];
445 8
                $results['blacklist_count']       += $period['blacklist_count'];
446 8
                $results['session_limit_count']   += $period['session_limit_count'];
447
            }
448
449 8
            $results['captcha_chart_string']         = implode(',', $chartCaptcha);
450 8
            $results['pageview_chart_string']        = implode(',', $chartPageview);
451 8
            $results['captcha_success_chart_string'] = implode(',', $chartCaptchaSuccess);
452 8
            $results['captcha_failure_chart_string'] = implode(',', $chartCaptchaFailure);
453 8
            $results['label_chart_string']           = "'" . implode("','", $labels) . "'";
454
455 8
            if ($results['captcha_count'] > 0) {
456 8
                $results['captcha_percentageage'] = (int) (
457 8
                    round($results['captcha_count'] / ($results['captcha_count'] + $results['pageview_count']), 2) * 100
458 8
                );
459
                $results['captcha_failure_percentage'] = (int) (
460
                    round($results['captcha_failure_count'] / $results['captcha_count'], 2) * 100
461
                );
462 9
                $results['captcha_success_percentage'] = (int) (
463
                    round($results['captcha_success_count'] / $results['captcha_count'], 2) * 100
464
                );
465
            }
466
        }
467
468
        return $results;
469
    }
470
471
    /**
472
     * Parse log data for showing on dashboard.
473
     *
474 8
     * @param array  $log The log action code.
475
     * @param string $t   Time period type. (For example: `today`, `yesterday`, `past_seven_days`)
476 8
     * @param string $k   Time period key. (For example: `12:00 am`, `20190812`)
477 8
     *
478 8
     * @return void
479
     */
480 8
    private function parse($log, $t, $k): void
481
    {
482 8
        $logActionCode = (int) $log['action_code'];
483 8
        $ip = $log['ip'];
484 8
        $sessionId = $log['session_id'];
485 8
486
        $this->ipDetail[$t][$ip]['session_id'][$sessionId ] = 1;
487 8
488 8
        if ($logActionCode === self::LOG_TEMPORARILY_BAN) {
489 8
            $this->periodDetail[$t][$k]['action_temp_ban_count']++;
490
            $this->periodDetail[$t][$k]['captcha_count']++;
491
            $this->periodDetail[$t][$k]['captcha_failure_count']++;
492 8
493 8
            $this->ipDetail[$t][$ip]['action_temp_ban_count']++;
494 8
            $this->ipDetail[$t][$ip]['captcha_count']++;
495
            $this->ipDetail[$t][$ip]['captcha_failure_count']++;
496
        }
497 8
498 8
        if ($logActionCode === self::LOG_BAN) {
499 8
            $this->periodDetail[$t][$k]['action_ban_count']++;
500 8
            $this->ipDetail[$t][$ip]['action_ban_count']++;
501
        }
502 8
503 8
        if ($logActionCode === self::LOG_UNBAN) {
504 8
            $this->periodDetail[$t][$k]['action_unban_count']++;
505
            $this->periodDetail[$t][$k]['captcha_success_count']++;
506
            $this->periodDetail[$t][$k]['captcha_failure_count']--;
507 8
508 8
            $this->ipDetail[$t][$ip]['action_unban_count']++;
509 8
            $this->ipDetail[$t][$ip]['captcha_success_count']++;
510
            $this->ipDetail[$t][$ip]['captcha_failure_count']--;
511 8
        }
512 8
513
        if ($logActionCode === self::LOG_CAPTCHA) {
514
            $this->periodDetail[$t][$k]['captcha_count']++;
515 8
            $this->periodDetail[$t][$k]['captcha_failure_count']++;
516 8
517 8
            $this->ipDetail[$t][$ip]['captcha_count']++;
518
            $this->ipDetail[$t][$ip]['captcha_failure_count']++;
519
        }
520 8
521 8
        if ($logActionCode === self::LOG_BLACKLIST) {
522 8
            $this->periodDetail[$t][$k]['blacklist_count']++;
523
            $this->ipDetail[$t][$ip]['blacklist_count']++;
524
        }
525 8
526 8
        if ($logActionCode === self::LOG_PAGEVIEW) {
527 8
            $this->periodDetail[$t][$k]['pageview_count']++;
528
            $this->ipDetail[$t][$ip]['pageview_count']++;
529
        }
530
531
        if ($logActionCode === self::LOG_LIMIT) {
532
            $this->periodDetail[$t][$k]['session_limit_count']++;
533
            $this->ipDetail[$t][$ip]['session_limit_count']++;
534
        }
535
    }
536 3
537
    /**
538 3
     * Return current log's directory.
539
     *
540
     * @return string
541
     */
542
    public function getDirectory(): string
543
    {
544
        return $this->logger->getDirectory();
545
    }
546
}
547