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\WebServer; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Process\Process; |
15
|
|
|
use WebHelper\Parser\ParserInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base class for webserver classes. |
19
|
|
|
* |
20
|
|
|
* name can be either apache, nginx, lighttpd, php or other webserver name |
21
|
|
|
* |
22
|
|
|
* @author james <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class WebServer implements WebServerInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* the name of a webserver. |
28
|
|
|
* |
29
|
|
|
* @var string the name of a webserver |
30
|
|
|
*/ |
31
|
|
|
private $name; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* the version of a webserver. |
35
|
|
|
* |
36
|
|
|
* @var string the version of a webserver |
37
|
|
|
*/ |
38
|
|
|
private $version; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* binaries that can be used to control the webserver. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $binaries = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* the parameter string to use to detect version and config file. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $detectionParameter = ''; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* a web config parser. |
56
|
|
|
* |
57
|
|
|
* @var ParserInterface |
58
|
|
|
*/ |
59
|
|
|
protected $parser; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor. |
63
|
|
|
* |
64
|
|
|
* @param string $name the name of a webserver |
65
|
|
|
* @param string $version the version of a webserver |
66
|
|
|
*/ |
67
|
33 |
|
public function __construct($name, $version = '') |
68
|
|
|
{ |
69
|
33 |
|
$this->name = $name; |
70
|
33 |
|
$this->version = $version; |
71
|
33 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets the name of a webserver. |
75
|
|
|
* |
76
|
|
|
* @return string the name of the webserver |
77
|
|
|
*/ |
78
|
8 |
|
public function getName() |
79
|
|
|
{ |
80
|
8 |
|
return $this->name; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets the version of a webserver. |
85
|
|
|
* |
86
|
|
|
* @return string the version of the webserver |
87
|
|
|
*/ |
88
|
5 |
|
public function getVersion() |
89
|
|
|
{ |
90
|
5 |
|
return $this->version; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets the list of binaries that can be run to analyze. |
95
|
|
|
* |
96
|
|
|
* @return array the list of binaries that can be run |
97
|
|
|
*/ |
98
|
7 |
|
public function getBinaries() |
99
|
|
|
{ |
100
|
7 |
|
return $this->binaries; |
101
|
|
|
} |
102
|
|
|
|
103
|
21 |
|
public function setBinaries(array $binaries) |
104
|
|
|
{ |
105
|
21 |
|
$this->binaries = $binaries; |
106
|
|
|
|
107
|
21 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
21 |
|
public function setDetectionParameter($parameter = '') |
111
|
|
|
{ |
112
|
21 |
|
$this->detectionParameter = $parameter; |
113
|
|
|
|
114
|
21 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
public function getSettings($fullPathBinary) |
118
|
|
|
{ |
119
|
3 |
|
$process = new Process($fullPathBinary.$this->detectionParameter); |
120
|
3 |
|
$process->run(); |
121
|
|
|
|
122
|
3 |
|
return $process->getOutput(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
abstract public function extractVersion($settings = ''); |
126
|
|
|
|
127
|
|
|
abstract public function extractRootConfigurationFile($settings = ''); |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Finds and return a specific information in a string. |
131
|
|
|
* |
132
|
|
|
* the regexp must contain delimiters. |
133
|
|
|
* |
134
|
|
|
* @param string $regexp a regular expression to find |
135
|
|
|
* @param string $settings a string where to find |
136
|
|
|
* |
137
|
|
|
* @return string the found value or an empty string |
138
|
|
|
*/ |
139
|
11 |
|
protected function match($regexp, $settings) |
140
|
|
|
{ |
141
|
11 |
|
$matches = []; |
142
|
|
|
|
143
|
11 |
|
if (preg_match($regexp, $settings, $matches)) { |
144
|
6 |
|
return $matches[1]; |
145
|
|
|
} |
146
|
|
|
|
147
|
6 |
|
return ''; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Loads and cleans a config file. |
152
|
|
|
* |
153
|
|
|
* @param string $file a Configuration file |
154
|
|
|
* |
155
|
|
|
* @return array an array of the cleaned directives of a the config per entry |
156
|
|
|
*/ |
157
|
|
|
abstract public function getActiveConfig($file = ''); |
158
|
|
|
} |
159
|
|
|
|