-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.php
More file actions
64 lines (62 loc) · 1.75 KB
/
parser.php
File metadata and controls
64 lines (62 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
if (empty($argv[1])) {
echo "path is empty". PHP_EOL;
exit(1);
}
$file = fopen($argv[1], "r");
if ($file === false) {
echo "could not open file ". $argv[1]. PHP_EOL;
exit(1);
}
$views = 0;
$traffic = 0;
$statusCodes = [];
$urls = [];
$crawlers = [
"Google" => 0,
"Bing" => 0,
"Baidu" => 0,
"Yandex" => 0,
];
while ($row = fgets($file, 4096)) {
$views++;
$data = combinedLogFormat($row);
$traffic += (int) $data["body_bytes_sent"];
if (isset($statusCodes[$data["status_code"]])) {
$statusCodes[$data["status_code"]]++;
} else {
$statusCodes[$data["status_code"]] = 1;
}
$urls[$data['path']] = 1;
if (stripos($data['http_user_agent'], "GoogleBot") !== false) {
$crawlers['Google']++;
}
if (strpos($data['http_user_agent'], "YandexBot") !== false) {
$crawlers['Yandex']++;
}
if (strpos($data['http_user_agent'], "BingBot") !== false) {
$crawlers['Bing']++;
}
if (strpos($data['http_user_agent'], "Baiduspider")) {
$crawlers['Baidu']++;
}
}
fclose($file);
$result = [
"views" => $views,
"urls" => count($urls),
"traffic" => $traffic,
"crawlers" => $crawlers,
"statusCodes" => $statusCodes,
];
echo json_encode($result). PHP_EOL;
function combinedLogFormat(string $row): array
{
$result = [];
preg_match(
"/(?P<remote_addr>((?:[0-9]{1,3}\.){3}[0-9]{1,3})) (?P<dash>\S+) (?P<remote_user>\S+) \[(?P<time_local>[\w:\/]+\s[+|-]\d{4})\] \"(?P<request>\S+)\s?(?P<path>\S+)?\s?(?P<protocol>\S+)?\" (?P<status_code>\d{3}|-) (?P<body_bytes_sent>\d+|-)\s?\"?(?P<http_referer>[^\"]*)\"?\s\"?(?P<http_user_agent>[^\"]*)\"\s\"?(?P<http_x_forwarded_for>[^\"]*)/m",
$row,
$result
);
return $result;
}