-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathenv-disk-sources.php
More file actions
107 lines (96 loc) · 3.32 KB
/
Copy pathenv-disk-sources.php
File metadata and controls
107 lines (96 loc) · 3.32 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
/**
* ENV-driven disk log sources / DiskAllowed prefixes for Docker (write-config + seed-database).
*/
/**
* Parsed disk specs for seed-database.php inserts.
*
* @return list<array{name:string,path:string,kind:string,description:string}>
*/
function loganalyzer_disk_source_specs_from_env(): array
{
$out = [];
$seen = [];
$pathsEnv = getenv('LOGANALYZER_DISK_SOURCE_PATHS');
if ($pathsEnv !== false && trim($pathsEnv) !== '') {
foreach (explode(',', $pathsEnv) as $raw) {
$p = trim($raw);
if ($p === '') {
continue;
}
$pNorm = str_replace('\\', '/', $p);
if (isset($seen[$pNorm])) {
continue;
}
$seen[$pNorm] = true;
$name = basename($pNorm);
if ($name === '' || $name === '/') {
$name = $pNorm;
}
$out[] = ['name' => $name, 'path' => $pNorm, 'kind' => 'syslog', 'description' => ''];
}
}
$specEnv = getenv('LOGANALYZER_DISK_SOURCES');
if ($specEnv !== false && trim($specEnv) !== '') {
foreach (explode(';;', $specEnv) as $segment) {
$segment = trim($segment);
if ($segment === '') {
continue;
}
// Limit splits so descriptions may contain '|' (e.g. Auth | PAM notes).
$parts = array_map('trim', explode('|', $segment, 4));
if (count($parts) < 3) {
fwrite(STDERR, "Skipping LOGANALYZER_DISK_SOURCES record (need name|path|syslog|optional description): {$segment}\n");
continue;
}
$name = $parts[0];
$path = $parts[1];
$kind = strtolower($parts[2]);
$desc = $parts[3] ?? '';
if ($name === '' || $path === '') {
continue;
}
if (!in_array($kind, ['syslog', 'event'], true)) {
fwrite(STDERR, "Skipping disk source '$name': LOGANALYZER_DISK_SOURCES kind must be 'syslog' or 'event' (got '{$parts[2]}').\n");
continue;
}
$pathNorm = str_replace('\\', '/', $path);
if (isset($seen[$pathNorm])) {
continue;
}
$seen[$pathNorm] = true;
$out[] = ['name' => $name, 'path' => $pathNorm, 'kind' => $kind, 'description' => $desc];
}
}
return $out;
}
/**
* Paths LogAnalyzer PHP may scan for disk sources (trailing slashes).
*
* @return list<string>
*/
function loganalyzer_disk_allowed_directories(): array
{
$uniq = ['/var/log/' => true, '/samplelogs/' => true];
foreach (loganalyzer_disk_source_specs_from_env() as $s) {
$dir = dirname($s['path']);
$dir = str_replace('\\', '/', $dir);
if ($dir === '' || $dir === '.' || $dir === '/') {
continue;
}
$uniq[rtrim($dir, '/') . '/'] = true;
}
$extra = getenv('LOGANALYZER_DISK_ALLOWED_EXTRA');
if ($extra !== false && trim($extra) !== '') {
foreach (explode(',', $extra) as $raw) {
$dir = trim($raw);
if ($dir === '') {
continue;
}
$dir = str_replace('\\', '/', $dir);
$uniq[rtrim($dir, '/') . '/'] = true;
}
}
return array_keys($uniq);
}