Passed
Push — 2.x ( ba8268...1f390b )
by Terry
02:18
created

ActionLogParser::parse()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 35
nc 128
nop 3
dl 0
loc 54
rs 7.9288
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Log;
14
15
use Shieldon\Firewall\Log\ActionLogger as Logger;
16
17
use function date;
18
use function round;
19
use function strtotime;
20
21
/**
22
 * Parse the log files that created by ActionLogger.
23
 */
24
final class ActionLogParser
25
{
26
    // Log codes. Same as Shieldon action codes.
27
    const LOG_BAN = 0;
28
    const LOG_ALLOW = 1;    
29
    const LOG_TEMPORARILY_BAN = 2;
30
    const LOG_UNBAN = 9;
31
    
32
    const LOG_LIMIT = 3;
33
    const LOG_PAGEVIEW = 11;
34
    const LOG_BLACKLIST = 98;
35
    const LOG_CAPTCHA = 99;
36
37
    /**
38
     * Statistic data fields.
39
     *
40
     * @var array
41
     */
42
    protected $fields = [];
43
    
44
    /**
45
     * Period type of the statistic data.
46
     *
47
     * @var array
48
     */
49
    protected $periods = [];
50
51
    /**
52
     * Data detail.
53
     * 
54
     * For example:
55
     * $this->periodDetail['today']['12:00 am'][$field] = 7;
56
     *
57
     * @var array
58
     */
59
    protected $periodDetail = [];
60
61
    /**
62
     * IP Detail
63
     * 
64
     * For example:
65
     * $this->ipDetail['today']['127.0.0.1'][$fields] = 6;
66
     *
67
     * @var array
68
     */
69
    protected $ipDetail = [];
70
71
    /**
72
     * ActionLogger instance.
73
     *
74
     * @var ActionLogger
75
     */
76
    protected $logger;
77
78
    /**
79
     * Period type.
80
     *
81
     * @var string
82
     */
83
    protected $type = 'today';
84
85
    /**
86
     * Constructer.
87
     *
88
     * @param string $directory The directory where to store the logs in.
89
     */
90
    public function __construct(string $directory = '') 
91
    {
92
        if (!isset($this->logger)) {
93
            $this->logger = new Logger($directory);
94
        }
95
96
        $this->fields = [
97
            'captcha_count',
98
            'captcha_success_count',
99
            'captcha_failure_count',
100
            'pageview_count',
101
            'action_ban_count',
102
            'action_temp_ban_count',
103
            'action_unban_count',
104
            'blacklist_count',
105
            'session_limit_count',
106
            'captcha_failure_percentage',
107
            'captcha_success_percentage',
108
        ];
109
110
        // range: today ~ now
111
        $this->periods['today'] = [
112
            'timesamp_begin' => strtotime('today'),
113
            'timesamp_end'   => strtotime('tomorrow'),
114
            'display_format' =>'h:00 a',
115
            'display_count'  => 24,
116
            'period'         => 3600,
117
        ];
118
        
119
        // range: yesterday ~ today
120
        $this->periods['yesterday'] = [
121
            'timesamp_begin' => strtotime('yesterday'),
122
            'timesamp_end'   => strtotime('today'),
123
            'display_format' =>'H:00',
124
            'display_count'  => 24,
125
            'period'         => 3600,
126
        ];
127
128
        // range: past_seven_hours ~ now
129
        $this->periods['past_seven_hours'] = [
130
            'timesamp_begin' => strtotime(date('Y-m-d H:00:00', strtotime('-7 hours'))),
131
            'timesamp_end'   => strtotime(date('Y-m-d H:00:00', strtotime('-1 hours'))),
132
            'display_format' =>'H:00',
133
            'display_count'  => 7,
134
            'period'         => 3600,
135
        ];
136
137
        // range: past_seven_days ~ today
138
        $this->periods['past_seven_days'] = [
139
            'timesamp_begin' => strtotime(date('Ymd', strtotime('-7 days'))),
140
            'timesamp_end'   => strtotime('today'),
141
            'display_format' => 'D',
142
            'display_count'  => 7,
143
            'period'         => 86400,
144
        ];
145
146
        // range: last_month ~ today
147
        $this->periods['this_month'] = [
148
            'timesamp_begin' => strtotime(gmdate('Ym' . '01')),
149
            'timesamp_end'   => strtotime('today'),
150
            'display_format' =>'Y.m.d',
151
            'display_count'  => gmdate('j'),
152
            'period'         => 86400,   
153
        ];
154
155
        // range: last_month ~ this_month
156
        $this->periods['last_month'] = [
157
            'timesamp_begin' => strtotime(gmdate('Ym' . '01', strtotime('-1 months'))),
158
            'timesamp_end'   => strtotime(gmdate('Ym' . '01')),
159
            'display_format' =>'Y.m.d',
160
            'display_count'  => gmdate('j', strtotime('-1 months')),
161
            'period'         => 86400,          
162
        ];
163
    }
164
165
    /**
166
     * Get the start and end date depends on the log type.
167
     *
168
     * @return array
169
     */
170
    protected function getStartEndDate(): array
171
    {
172
        switch ($this->type) {
173
174
            case 'yesterday':
175
                // Set start date and end date.
176
                $startDate = date('Ymd', strtotime('yesterday'));
177
                $endDate = date('Ymd');
178
                break;
179
    
180
            case 'past_seven_days':
181
                $startDate = date('Ymd', strtotime('-7 days'));
182
                $endDate = date('Ymd');
183
                break;
184
185
            case 'this_month':
186
                $startDate = date('Ym') . '01';
187
                $endDate = date('Ym') . '31';
188
                break;
189
190
            case 'last_month':
191
                $startDate = date('Ym', strtotime('-1 month')) . '01';
192
                $endDate = date('Ym', strtotime('-1 month')) . '31';
193
                break;
194
195
            case 'past_seven_hours':
196
                $startDate = date('Ymd', strtotime('yesterday'));
197
                $endDate = date('Ymd');
198
                break;
199
200
            case 'today':
201
                $startDate = date('Ymd');
202
                $endDate = '';
203
                break;
204
205
            default:
206
207
                // We also accept querying N days data from logs. For example: `past_365_days`.
208
                if (preg_match('/past_([0-9]+)_days/', $this->type, $matches) ) {
209
210
                    $dayCount = $matches[1];
211
                    $startDate = date('Ymd', strtotime('-' . $dayCount . ' days'));
212
                    $endDate = date('Ymd');
213
214
                    $this->periods['past_' . $dayCount . '_days'] = [
215
                        'timesamp_begin' => strtotime(date('Ymd', strtotime('-' . $dayCount . ' days'))),
216
                        'timesamp_end'   => strtotime('today'),
217
                        'display_format' => 'D',
218
                        'display_count'  => $dayCount,
219
                        'period'         => 86400,
220
                    ];
221
222
                } else {
223
                    $startDate = date('Ymd');
224
                    $endDate = '';
225
                    $this->periods[$this->type] = $this->periods['today'];
226
                }
227
            // endswitch;
228
        }
229
230
        return [
231
            'start' => $startDate,
232
            'end' => $endDate,
233
        ];
234
    }
235
236
    /**
237
     * Parse specific period of time of data.
238
     * 
239
     * Warning: This method may take long time to generate real-time stats on a high-traffic website.
240
     * Aprroximately 10,000 rows for 3-5 seconds, depnonds on your server's CPU speed.
241
     *
242
     * @return void
243
     */
244
    public function parsePeriodData(): void
245
    {
246
        $dateRange = $this->getStartEndDate();
247
        $startDate = $dateRange['start'];
248
        $endDate = $dateRange['end'];
249
250
        // Fetch data from log files.
251
        $logs = $this->logger->get($startDate, $endDate);
252
253
        foreach ($logs as $log) {
254
255
            $logTimesamp = (int) $log['timesamp'];
256
            $logIp = $log['ip'];
257
258
            // Add a new field `datetime` that original logs don't have.
259
            $log['datetime'] = date('Y-m-d H:i:s', $logTimesamp);
260
            
261
            foreach (array_keys($this->periods) as $t) {
262
263
                for ($i = 0; $i < $this->periods[$t]['display_count']; $i++) {
264
265
                    $kTimesamp = $this->periods[$t]['timesamp_begin'] + ($i * $this->periods[$t]['period']);
266
267
                    $detailTimesampBegin = $kTimesamp;
268
                    $detailTimesampEnd = $kTimesamp + $this->periods[$t]['period'];
269
270
                    $k = date($this->periods[$t]['display_format'], $kTimesamp);
271
272
                    // Initialize all the counters.
273
                    foreach ($this->fields as $field) {
274
                        if (!isset($this->periodDetail[$t][$k][$field])) {
275
                            $this->periodDetail[$t][$k][$field] = 0;
276
                        }
277
278
                        if ($logTimesamp >= $detailTimesampBegin && $logTimesamp < $detailTimesampEnd) {
279
                            if (!isset($this->ipDetail[$t][$logIp][$field])) {
280
                                $this->ipDetail[$t][$logIp][$field] = 0;
281
                            }
282
                        }
283
                    }
284
285
                    // Initialize all the counters.
286
                    if ($logTimesamp >= $detailTimesampBegin && $logTimesamp < $detailTimesampEnd) {
287
                        $this->parse($log, $t, $k);
288
                    }
289
                }
290
            }
291
        }
292
    }
293
    
294
    /**
295
     * Prepare data.
296
     *
297
     * @param string $type Period type.
298
     *
299
     * @return void
300
     */
301
    public function prepare(string $type = 'today'): void
302
    {
303
        $this->type = $type;
304
305
        $this->parsePeriodData($this->type);
0 ignored issues
show
Unused Code introduced by
The call to Shieldon\Firewall\Log\Ac...rser::parsePeriodData() has too many arguments starting with $this->type. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

305
        $this->/** @scrutinizer ignore-call */ 
306
               parsePeriodData($this->type);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
306
    }
307
308
    /**
309
     * Get data
310
     *
311
     * @return array
312
     */
313
    public function getPeriodData()
314
    {
315
        if (!empty($this->periodDetail[$this->type])) {
316
            return $this->periodDetail[$this->type];
317
        }
318
        return [];
319
    }
320
321
    /**
322
     * Get data
323
     *
324
     * @return array
325
     */
326
    public function getIpData()
327
    {
328
        if (!empty($this->ipDetail[$this->type])) {
329
            return $this->ipDetail[$this->type];
330
        }
331
        return [];
332
    }
333
334
    /**
335
     * Get parsed perid data.
336
     *
337
     * @param string $ip   IP address.
338
     *
339
     * @return array
340
     */
341
    public function getParsedIpData($ip = ''): array
342
    {
343
        if (empty($ip)) {
344
            return [];
345
        }
346
347
        $results['captcha_chart_string']  = '';     // string
0 ignored issues
show
Comprehensibility Best Practice introduced by
$results was never initialized. Although not strictly required by PHP, it is generally a good practice to add $results = array(); before regardless.
Loading history...
348
        $results['pageview_chart_string'] = '';     // string
349
        $results['captcha_success_count'] = 0;      // integer
350
        $results['captcha_failure_count'] = 0;      // integer
351
        $results['captcha_count'] = 0;              // integer
352
        $results['pageview_count'] = 0;             // integer
353
        $results['captcha_percentageage'] = 0;      // integer
354
        $results['captcha_failure_percentage'] = 0; // integer
355
        $results['captcha_success_percentage'] = 0; // integer
356
357
        $results['action_ban_count'] = 0;           // integer
358
        $results['action_temp_ban_count'] = 0;      // integer
359
        $results['action_unban_count'] = 0;         // integer
360
        $results['blacklist_count'] = 0;            // integer
361
        $results['session_limit_count'] = 0;        // integer
362
363
        $ipdData = $this->getIpData();
364
365
        if (!empty($ipdData)) {
366
367
            foreach ($ipdData as $ipKey => $ipInfo) {
368
369
                if ($ipKey === $ip) {
370
                    $results['captcha_success_count'] += $ipInfo['captcha_success_count'];
371
                    $results['captcha_failure_count'] += $ipInfo['captcha_failure_count'];
372
                    $results['captcha_count'] += $ipInfo['captcha_count'];
373
                    $results['pageview_count'] += $ipInfo['pageview_count'];
374
375
                    $results['action_ban_count'] += $ipInfo['action_ban_count'];
376
                    $results['action_temp_ban_count'] += $ipInfo['action_temp_ban_count'];
377
                    $results['action_unban_count'] += $ipInfo['action_unban_count'];
378
                    $results['blacklist_count'] += $ipInfo['blacklist_count'];
379
                    $results['session_limit_count'] += $ipInfo['session_limit_count'];
380
                }
381
            }
382
383
            if ($results['captcha_count'] > 0) {
384
                $results['captcha_percentageage'] = (int) (round($results['captcha_count'] / ($results['captcha_count'] + $results['pageview_count']), 2) * 100);
385
                $results['captcha_failure_percentage'] = (int) (round($results['captcha_failure_count'] / $results['captcha_count'], 2) * 100);
386
                $results['captcha_success_percentage'] = (int) (round($results['captcha_success_count'] / $results['captcha_count'], 2) * 100);
387
            }
388
        }
389
390
        return $results;
391
    }
392
393
    /**
394
     * Get parsed perid data.
395
     *
396
     * @return array
397
     */
398
    public function getParsedPeriodData(): array
399
    {
400
        $periodData = $this->getPeriodData();
401
402
        $results['captcha_chart_string']  = '';     // string
0 ignored issues
show
Comprehensibility Best Practice introduced by
$results was never initialized. Although not strictly required by PHP, it is generally a good practice to add $results = array(); before regardless.
Loading history...
403
        $results['pageview_chart_string'] = '';     // string
404
        $results['label_chart_string'] = '';        // string
405
        $results['captcha_success_count'] = 0;      // integer
406
        $results['captcha_failure_count'] = 0;      // integer
407
        $results['captcha_count'] = 0;              // integer
408
        $results['pageview_count'] = 0;             // integer
409
        $results['captcha_percentageage'] = 0;      // integer
410
        $results['captcha_failure_percentage'] = 0; // integer
411
        $results['captcha_success_percentage'] = 0; // integer
412
413
        $results['action_ban_count'] = 0;           // integer
414
        $results['action_temp_ban_count'] = 0;      // integer
415
        $results['action_unban_count'] = 0;         // integer
416
        $results['blacklist_count'] = 0;            // integer
417
        $results['session_limit_count'] = 0;        // integer
418
419
        if (!empty($periodData)) {
420
421
            $chartCaptcha = [];
422
            $chartPageview = [];
423
            $chartCaptchaSuccess = [];
424
            $chartCaptchaFailure = [];
425
            $labels = [];
426
427
            foreach ($periodData as $label => $period) {
428
                $chartCaptcha[] = $period['captcha_count'];
429
                $chartPageview[] = $period['pageview_count'];
430
                $chartCaptchaSuccess[] = $period['captcha_success_count'];
431
                $chartCaptchaFailure[] = $period['captcha_failure_count'];
432
                $labels[] = $label;
433
434
                $results['captcha_success_count'] += $period['captcha_success_count'];
435
                $results['captcha_failure_count'] += $period['captcha_failure_count'];
436
                $results['captcha_count'] += $period['captcha_count'];
437
                $results['pageview_count'] += $period['pageview_count'];
438
439
                $results['action_ban_count'] += $period['action_ban_count'];
440
                $results['action_temp_ban_count'] += $period['action_temp_ban_count'];
441
                $results['action_unban_count'] += $period['action_unban_count'];
442
                $results['blacklist_count'] += $period['blacklist_count'];
443
                $results['session_limit_count'] += $period['session_limit_count'];
444
            }
445
446
            $results['captcha_chart_string'] = implode(',', $chartCaptcha);
447
            $results['pageview_chart_string']= implode(',', $chartPageview);
448
            $results['captcha_success_chart_string'] = implode(',', $chartCaptchaSuccess);
449
            $results['captcha_failure_chart_string'] = implode(',', $chartCaptchaFailure);
450
            $results['label_chart_string'] = "'" . implode("','", $labels) . "'";
451
452
            if ($results['captcha_count'] > 0) {
453
                $results['captcha_percentageage'] = (int) (round($results['captcha_count'] / ($results['captcha_count'] + $results['pageview_count']), 2) * 100);
454
                $results['captcha_failure_percentage'] = (int) (round($results['captcha_failure_count'] / $results['captcha_count'], 2) * 100);
455
                $results['captcha_success_percentage'] = (int) (round($results['captcha_success_count'] / $results['captcha_count'], 2) * 100);
456
            }
457
        }
458
459
        return $results;
460
    }
461
462
    /**
463
     * Parse log data for showing on dashboard.
464
     *
465
     * @param array  $logActionCode The log action code.
466
     * @param string $t             Time period type. (For example: `today`, `yesterday`, `past_seven_days`)
467
     * @param string $k             Time period key. (For example: `12:00 am`, `20190812`)
468
     *
469
     * @return void
470
     */
471
    private function parse($log, $t, $k): void
472
    {
473
        $logActionCode = (int) $log['action_code'];
474
        $ip = $log['ip'];
475
        $sessionId = $log['session_id'];
476
477
        $this->ipDetail[$t][$ip]['session_id'][$sessionId ] = 1;
478
479
        if ($logActionCode === self::LOG_TEMPORARILY_BAN) {
480
            $this->periodDetail[$t][$k]['action_temp_ban_count']++;
481
            $this->periodDetail[$t][$k]['captcha_count']++;
482
            $this->periodDetail[$t][$k]['captcha_failure_count']++;
483
484
            $this->ipDetail[$t][$ip]['action_temp_ban_count']++;
485
            $this->ipDetail[$t][$ip]['captcha_count']++;
486
            $this->ipDetail[$t][$ip]['captcha_failure_count']++;
487
        }
488
489
        if ($logActionCode === self::LOG_BAN) {
490
            $this->periodDetail[$t][$k]['action_ban_count']++;
491
            $this->ipDetail[$t][$ip]['action_ban_count']++;
492
        }
493
494
        if ($logActionCode === self::LOG_UNBAN) {
495
            $this->periodDetail[$t][$k]['action_unban_count']++;
496
            $this->periodDetail[$t][$k]['captcha_success_count']++;
497
            $this->periodDetail[$t][$k]['captcha_failure_count']--;
498
499
            $this->ipDetail[$t][$ip]['action_unban_count']++;
500
            $this->ipDetail[$t][$ip]['captcha_success_count']++;
501
            $this->ipDetail[$t][$ip]['captcha_failure_count']--;
502
        }
503
504
        if ($logActionCode === self::LOG_CAPTCHA) {
505
            $this->periodDetail[$t][$k]['captcha_count']++;
506
            $this->periodDetail[$t][$k]['captcha_failure_count']++;
507
508
            $this->ipDetail[$t][$ip]['captcha_count']++;
509
            $this->ipDetail[$t][$ip]['captcha_failure_count']++;
510
        }
511
512
        if ($logActionCode === self::LOG_BLACKLIST) {
513
            $this->periodDetail[$t][$k]['blacklist_count']++;
514
            $this->ipDetail[$t][$ip]['blacklist_count']++;
515
        }
516
517
        if ($logActionCode === self::LOG_PAGEVIEW) {
518
            $this->periodDetail[$t][$k]['pageview_count']++;
519
            $this->ipDetail[$t][$ip]['pageview_count']++;
520
        }
521
522
        if ($logActionCode === self::LOG_LIMIT) {
523
            $this->periodDetail[$t][$k]['session_limit_count']++;
524
            $this->ipDetail[$t][$ip]['session_limit_count']++;
525
        }
526
    }
527
528
    /**
529
     * Return current log's directory.
530
     *
531
     * @return string
532
     */
533
    public function getDirectory(): string
534
    {
535
        return $this->logger->getDirectory();
536
    }
537
}
538