Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AccessSslLogsChannel
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 5
210
0.00% covered (danger)
0.00%
0 / 1
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLogFileForIndex
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 getIndexForFilePath
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 parseDateTimeOfLine
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Olz\Apps\Logs\Utils;
4
5use Olz\Utils\WithUtilsTrait;
6
7class AccessSslLogsChannel extends LogrotateLogsChannel {
8    use WithUtilsTrait;
9
10    public static function getId(): string {
11        return 'access-ssl-logs';
12    }
13
14    public static function getName(): string {
15        return "Access SSL Logs";
16    }
17
18    protected function getLogFileForIndex(int $index): LogFileInterface {
19        $log_name = 'access_ssl_log';
20        $syslog_path = $this->envUtils()->getSyslogPath();
21        $basename = "{$log_name}.processed.{$index}";
22        $file_path = "{$syslog_path}{$basename}";
23        if ($index === -1) {
24            $file_path = "{$syslog_path}{$log_name}";
25        }
26        if ($index === 0) {
27            $file_path = "{$syslog_path}{$log_name}.processed";
28        }
29        if (is_file($file_path)) {
30            return new PlainLogFile($file_path, $file_path);
31        }
32        if (is_file("{$file_path}.gz")) {
33            return new HybridLogFile("{$file_path}.gz", $file_path, $file_path);
34        }
35        throw new \Exception("No such file: {$file_path}");
36    }
37
38    protected function getIndexForFilePath(string $file_path): int {
39        $log_name = 'access_ssl_log';
40        $syslog_path = $this->envUtils()->getSyslogPath();
41        $esc_syslog_path = preg_quote($syslog_path, '/');
42        $pattern = "/^{$esc_syslog_path}{$log_name}($|\\.processed$|\\.processed\\.(\\d+)$)/";
43        $res = preg_match($pattern, $file_path, $matches);
44        if (!$res) {
45            throw new \Exception("Not an OLZ Log file path: {$file_path}");
46        }
47        if ($matches[1] === '') {
48            return -1;
49        }
50        if ($matches[1] === '.processed') {
51            return 0;
52        }
53        return intval($matches[2]);
54    }
55
56    protected function parseDateTimeOfLine(string $line): ?\DateTime {
57        $res = preg_match('/(\d{2})\/(\w{3})\/(\d{4})(:|T|\s+)(\d{2}\:\d{2}\:\d{2})/', $line, $matches);
58        $month_mapping = [
59            'jan' => '01',
60            'feb' => '02',
61            'mar' => '03',
62            'apr' => '04',
63            'may' => '05',
64            'jun' => '06',
65            'jul' => '07',
66            'aug' => '08',
67            'sep' => '09',
68            'oct' => '10',
69            'nov' => '11',
70            'dec' => '12',
71        ];
72        if (!$res) {
73            return null;
74        }
75        try {
76            $month = $month_mapping[strtolower($matches[2])];
77            $date = "{$matches[3]}-{$month}-{$matches[1]}";
78            return new \DateTime("{$date} {$matches[5]}");
79        } catch (\Throwable $th) {
80            return null;
81        }
82    }
83}