1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is, guess what, part of WebHelper. |
5
|
|
|
* |
6
|
|
|
* (c) James <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace JamesRezo\WebHelper\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use JamesRezo\WebHelper\Factory; |
18
|
|
|
use JamesRezo\WebHelper\WebServer\WebServerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Detects webservers. |
22
|
|
|
*/ |
23
|
|
|
class DetectCommand extends BaseCommand |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
5 |
|
protected function configure() |
29
|
|
|
{ |
30
|
5 |
|
$this |
31
|
5 |
|
->setName('detect') |
32
|
5 |
|
->setDescription('Detect webservers on this platform.') |
33
|
5 |
|
->setHelp('The <info>detect</info> command finds webserver binaries in the PATH'. |
34
|
5 |
|
' or in the path passed in argument.') |
35
|
5 |
|
->addArgument('path', InputArgument::IS_ARRAY, 'path list to look into for a binary instead of PATH.') |
36
|
|
|
; |
37
|
5 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Execute the command. |
41
|
|
|
* |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
* |
44
|
|
|
* @param InputInterface $input the input interface |
45
|
|
|
* @param OutputInterface $output the output interface |
46
|
|
|
*/ |
47
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
48
|
|
|
{ |
49
|
4 |
|
$path = $this->getPath($input); |
50
|
|
|
|
51
|
4 |
|
$factory = new Factory(); |
52
|
4 |
|
$files = false; |
53
|
4 |
|
foreach ($factory->getKnownWebServers() as $webservername) { |
54
|
4 |
|
$webserver = $factory->createWebServer($webservername); |
55
|
4 |
|
$file = $this->searchBinary($path, $webserver, $output); |
56
|
4 |
|
$files = $files || (strlen($file) > 0); |
57
|
4 |
|
} |
58
|
4 |
|
if (!$files) { |
59
|
3 |
|
$output->writeln('<error>No Web Server Found.</error>'); |
60
|
|
|
|
61
|
3 |
|
return 1; |
62
|
|
|
} |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Finds the first binary available to manage a webserver. |
67
|
|
|
* |
68
|
|
|
* Validity of a binary is to retrieve a version and and default config file |
69
|
|
|
* |
70
|
|
|
* @param array $path a list of directories to scan |
71
|
|
|
* @param WebServerInterface $webserver the webserver to detect |
72
|
|
|
* @param OutputInterface $output the output interface |
73
|
|
|
* |
74
|
|
|
* @return string the absolute path of the binary if found and valid, empty string elsewhere |
75
|
|
|
*/ |
76
|
4 |
|
private function searchBinary(array $path, WebServerInterface $webserver, OutputInterface $output) |
77
|
|
|
{ |
78
|
4 |
|
$file = ''; |
79
|
|
|
|
80
|
4 |
|
foreach ($webserver->getBinaries() as $binary) { |
81
|
4 |
|
$file = $this->which($binary, $path); |
82
|
4 |
|
if ($file) { |
83
|
3 |
|
$output->writeln('<comment>'.$file.' detected for '.$webserver->getName().'.</comment>'); |
84
|
3 |
|
$settings = $webserver->getSettings($file); |
85
|
|
|
|
86
|
3 |
|
$version = $webserver->extractVersion($settings); |
87
|
3 |
|
if (!$version) { |
88
|
1 |
|
$output->writeln('<error>No version found for "'.$file.'".</error>'); |
89
|
1 |
|
$file = ''; |
90
|
1 |
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
$configFile = $webserver->extractRootConfigurationFile($settings); |
94
|
2 |
|
if (!$configFile) { |
95
|
1 |
|
$output->writeln('<error>No conf. file found for "'.$file.'".</error>'); |
96
|
1 |
|
$file = ''; |
97
|
1 |
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) { |
101
|
1 |
|
$output->writeln('<comment>That says:</comment>'); |
102
|
1 |
|
$output->write($settings); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
1 |
|
$output->writeln('<info>Detected version:</info>'.$version); |
106
|
1 |
|
$output->writeln('<info>Detected conf. file:</info>'.$configFile); |
107
|
|
|
|
108
|
1 |
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
109
|
1 |
|
$output->writeln( |
110
|
|
|
'<comment>You should analyze this with </comment>'. |
111
|
2 |
|
'wh analyze '.$webserver->getName().':'.$version.' '.$configFile |
112
|
1 |
|
); |
113
|
1 |
|
} |
114
|
1 |
|
break; |
115
|
|
|
} |
116
|
4 |
|
} |
117
|
|
|
|
118
|
4 |
|
return $file; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* A `which`-like function. |
123
|
|
|
* |
124
|
|
|
* Finds the absolute path of a binary in a list of directories |
125
|
|
|
* |
126
|
|
|
* @param string $binary the binary to find in a list of directories |
127
|
|
|
* @param array $paths a list of directories to scan |
128
|
|
|
* |
129
|
|
|
* @return string the absolute path of the binary if found, empty string elsewhere |
130
|
|
|
*/ |
131
|
4 |
|
private function which($binary, array $paths = array()) |
132
|
|
|
{ |
133
|
4 |
|
$file = ''; |
134
|
|
|
|
135
|
4 |
|
foreach ($paths as $path) { |
136
|
4 |
|
$path = preg_replace('#('.preg_quote(DIRECTORY_SEPARATOR).')*$#', '', $path).DIRECTORY_SEPARATOR; |
137
|
4 |
|
if (is_executable($path.$binary) && !is_dir($path.$binary)) { |
138
|
3 |
|
$file = $path.$binary; |
139
|
3 |
|
} |
140
|
4 |
|
} |
141
|
|
|
|
142
|
4 |
|
return $file; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|