Passed
Push — 2.x ( 7afaed...b9e32f )
by Terry
02:12
created

ActionLogParser::getStartEndDate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 55
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 40
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 55
rs 9.28

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
        $dataRange = [
173
            'yesterday' => [
174
                'start' => date('Ymd', strtotime('yesterday')),
175
                'end'   => date('Ymd'),
176
            ],
177
            'past_seven_days' => [
178
                'start' => date('Ymd', strtotime('-7 days')),
179
                'end'   => date('Ymd'),
180
            ],
181
            'this_month' => [
182
                'start' => date('Ym') . '01',
183
                'end'   => date('Ym') . '31',
184
            ],
185
            'last_month' => [
186
                'start' => date('Ym', strtotime('-1 month')) . '01',
187
                'end'   => date('Ym', strtotime('-1 month')) . '31',
188
            ],
189
            'past_seven_hours' => [
190
                'start' => date('Ymd', strtotime('yesterday')),
191
                'end'   => date('Ymd'),
192
            ],
193
            'today' => [
194
                'start' => date('Ymd'),
195
                'end'   => '',
196
            ],
197
        ];
198
199
        if (empty($dataRange[$this->type])) {
200
            if (preg_match('/past_([0-9]+)_days/', $this->type, $matches) ) {
201
                $dayCount = $matches[1];
202
                $startDate = date('Ymd', strtotime('-' . $dayCount . ' days'));
203
                $endDate = date('Ymd');
204
205
                $this->periods['past_' . $dayCount . '_days'] = [
206
                    'timesamp_begin' => strtotime(date('Ymd', strtotime('-' . $dayCount . ' days'))),
207
                    'timesamp_end'   => strtotime('today'),
208
                    'display_format' => 'D',
209
                    'display_count'  => $dayCount,
210
                    'period'         => 86400,
211
                ];
212
            } else {
213
                $startDate = date('Ymd');
214
                $endDate = '';
215
                $this->periods[$this->type] = $this->periods['today'];
216
            }
217
        } else {
218
            $startDate = $dataRange[$this->type]['start'];
219
            $endDate = $dataRange[$this->type]['end'];
220
        }
221
222
        return [
223
            'start' => $startDate,
224
            'end' => $endDate,
225
        ];
226
    }
227
228
    /**
229
     * Parse specific period of time of data.
230
     * 
231
     * Warning: This method may take long time to generate real-time stats on a high-traffic website.
232
     * Aprroximately 10,000 rows for 3-5 seconds, depnonds on your server's CPU speed.
233
     *
234
     * @return void
235
     */
236
    public function parsePeriodData(): void
237
    {
238
        $dateRange = $this->getStartEndDate();
239
        $startDate = $dateRange['start'];
240
        $endDate = $dateRange['end'];
241
242
        // Fetch data from log files.
243
        $logs = $this->logger->get($startDate, $endDate);
244
245
        foreach ($logs as $log) {
246
247
            $logTimesamp = (int) $log['timesamp'];
248
            $logIp = $log['ip'];
249
250
            // Add a new field `datetime` that original logs don't have.
251
            $log['datetime'] = date('Y-m-d H:i:s', $logTimesamp);
252
            
253
            foreach (array_keys($this->periods) as $t) {
254
255
                for ($i = 0; $i < $this->periods[$t]['display_count']; $i++) {
256
257
                    $kTimesamp = $this->periods[$t]['timesamp_begin'] + ($i * $this->periods[$t]['period']);
258
259
                    $detailTimesampBegin = $kTimesamp;
260
                    $detailTimesampEnd = $kTimesamp + $this->periods[$t]['period'];
261
262
                    $k = date($this->periods[$t]['display_format'], $kTimesamp);
263
264
                    // Initialize all the counters.
265
                    foreach ($this->fields as $field) {
266
                        if (!isset($this->periodDetail[$t][$k][$field])) {
267
                            $this->periodDetail[$t][$k][$field] = 0;
268
                        }
269
270
                        if ($logTimesamp >= $detailTimesampBegin && $logTimesamp < $detailTimesampEnd) {
271
                            if (!isset($this->ipDetail[$t][$logIp][$field])) {
272
                                $this->ipDetail[$t][$logIp][$field] = 0;
273
                            }
274
                        }
275
                    }
276
277
                    // Initialize all the counters.
278
                    if ($logTimesamp >= $detailTimesampBegin && $logTimesamp < $detailTimesampEnd) {
279
                        $this->parse($log, $t, $k);
280
                    }
281
                }
282
            }
283
        }
284
    }
285
    
286
    /**
287
     * Prepare data.
288
     *
289
     * @param string $type Period type.
290
     *
291
     * @return void
292
     */
293
    public function prepare(string $type = 'today'): void
294
    {
295
        $this->type = $type;
296
297
        $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

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