1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Collector\Application\WebServer\Nginx; |
4
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Collector\Collector; |
6
|
|
|
use Startwind\Inventorio\Exec\File; |
7
|
|
|
use Startwind\Inventorio\Exec\Runner; |
8
|
|
|
|
9
|
|
|
class NginxServerNameCollector implements Collector |
10
|
|
|
{ |
11
|
|
|
private const CONFIG_DIRECTORY = '/etc/nginx/sites-enabled'; |
12
|
|
|
|
13
|
|
|
public const COLLECTION_IDENTIFIER = 'NginxServerName'; |
14
|
|
|
|
15
|
|
|
public const FIELD_DOCUMENT_ROOT = 'documentRoot'; |
16
|
|
|
public const FIELD_SERVER_NAME = 'serverName'; |
17
|
|
|
|
18
|
|
|
public function getIdentifier(): string |
19
|
|
|
{ |
20
|
|
|
return self::COLLECTION_IDENTIFIER; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function collect(): array |
24
|
|
|
{ |
25
|
|
|
$runner = Runner::getInstance(); |
26
|
|
|
|
27
|
|
|
if (!$runner->fileExists(self::CONFIG_DIRECTORY)) { |
28
|
|
|
return []; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$configurations = $this->getAllConfigurations(self::CONFIG_DIRECTORY); |
32
|
|
|
$result = []; |
33
|
|
|
|
34
|
|
|
foreach ($configurations as $configuration) { |
35
|
|
|
$config = $this->extractServerData($configuration); |
36
|
|
|
if (!empty($config['serverName'])) { |
37
|
|
|
$result[$config['serverName']] = $config; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return array_values($result); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function extractServerData(string $configFile): array |
45
|
|
|
{ |
46
|
|
|
$lines = File::getInstance()->getContents($configFile, true); |
47
|
|
|
|
48
|
|
|
$serverName = ''; |
49
|
|
|
$documentRoot = ''; |
50
|
|
|
$aliases = []; |
51
|
|
|
|
52
|
|
|
foreach ($lines as $line) { |
53
|
|
|
$line = trim($line); |
54
|
|
|
|
55
|
|
|
if (preg_match('/^\s*(#|\/\/)/', $line)) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (preg_match('/^\s*server_name\s+([^;]+);/i', $line, $match)) { |
60
|
|
|
$names = preg_split('/\s+/', trim($match[1])); |
61
|
|
|
$serverName = $names[0] ?? ''; |
62
|
|
|
$aliases = array_slice($names, 1); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (preg_match('/^\s*root\s+([^;]+);/i', $line, $match)) { |
66
|
|
|
$documentRoot = trim($match[1]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return [ |
71
|
|
|
self::FIELD_SERVER_NAME => $serverName, |
72
|
|
|
self::FIELD_DOCUMENT_ROOT => $documentRoot, |
73
|
|
|
'configFile' => $configFile, |
74
|
|
|
'aliases' => $aliases |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function getAllConfigurations(string $vhostDir): array |
79
|
|
|
{ |
80
|
|
|
$fileHandler = File::getInstance(); |
81
|
|
|
$files = $fileHandler->scanDir($vhostDir); |
82
|
|
|
$configurations = []; |
83
|
|
|
|
84
|
|
|
foreach ($files as $file) { |
85
|
|
|
if (pathinfo($file, PATHINFO_EXTENSION) === 'conf') { |
86
|
|
|
$configurations[] = $vhostDir . '/' . $file; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $configurations; |
91
|
|
|
} |
92
|
|
|
} |