Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
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 / 50
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 / 14
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        $index_path = "{$file_path}.index.json.gz";
30        if (is_file($file_path)) {
31            return new PlainLogFile($file_path, $index_path);
32        }
33        if (is_file("{$file_path}.gz")) {
34            return new HybridLogFile("{$file_path}.gz", $file_path, $index_path);
35        }
36        throw new \Exception("No such file: {$file_path}");
37    }
38
39    protected function getIndexForFilePath(string $file_path): int {
40        $log_name = 'access_ssl_log';
41        $syslog_path = $this->envUtils()->getSyslogPath();
42        $esc_syslog_path = preg_quote($syslog_path, '/');
43        $pattern = "/^{$esc_syslog_path}{$log_name}($|\\.processed$|\\.processed\\.(\\d+)$)/";
44        $res = preg_match($pattern, $file_path, $matches);
45        if (!$res) {
46            throw new \Exception("Not an OLZ Log file path: {$file_path}");
47        }
48        if ($matches[1] === '') {
49            return -1;
50        }
51        if ($matches[1] === '.processed') {
52            return 0;
53        }
54        return intval($matches[2]);
55    }
56
57    protected function parseDateTimeOfLine(string $line): ?\DateTime {
58        $res = preg_match('/(\d{2})\/(\w{3})\/(\d{4})(:|T|\s+)(\d{2}\:\d{2}\:\d{2})/', $line, $matches);
59        $month_mapping = [
60            'jan' => '01',
61            'feb' => '02',
62            'mar' => '03',
63            'apr' => '04',
64            'may' => '05',
65            'jun' => '06',
66            'jul' => '07',
67            'aug' => '08',
68            'sep' => '09',
69            'oct' => '10',
70            'nov' => '11',
71            'dec' => '12',
72        ];
73        if (!$res) {
74            return null;
75        }
76        try {
77            $month = $month_mapping[strtolower($matches[2])];
78            $date = "{$matches[3]}-{$month}-{$matches[1]}";
79            return new \DateTime("{$date} {$matches[5]}");
80        } catch (\Throwable $th) {
81            return null;
82        }
83    }
84}