|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of WebHelper Parser. |
|
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 WebHelper\Parser\Server; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use Webmozart\Assert\Assert; |
|
16
|
|
|
use Webmozart\PathUtil\Path; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* A web server instance. |
|
20
|
|
|
* |
|
21
|
|
|
* @author James <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Server implements ServerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The filesystem path where the web server is installed. |
|
27
|
|
|
* |
|
28
|
|
|
* It has to be an absolute path. |
|
29
|
|
|
* |
|
30
|
|
|
* Apache httpd server does not accept a relative prefix path at compilation. |
|
31
|
|
|
* Nginx does, but this is a very risky practice... |
|
32
|
|
|
* So relative prefix path in nginx configuration will not be considered |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $prefix = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The string to match as a starting multi-line directive. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $startMultiLine = ''; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The string to match as an ending multi-line directive. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private $endMultiLine = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The string to match a simple directive. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
private $simpleDirective = ''; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* binaries that can be used to control the webserver. |
|
61
|
|
|
* |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
private $binaries = []; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* the parameter string to use to detect version and config file. |
|
68
|
|
|
* |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
private $detectionParameter = ''; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The ordered list of methods to apply before convertion. |
|
75
|
|
|
* |
|
76
|
|
|
* @var array |
|
77
|
|
|
*/ |
|
78
|
|
|
private $beforeMethods = []; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The ordered list of methods to apply after convertion. |
|
82
|
|
|
* |
|
83
|
|
|
* @var array |
|
84
|
|
|
*/ |
|
85
|
|
|
private $afterMethods = []; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Confirms if the server instance has valid parameters. |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool true if all parameters are initialized, false otherwise |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function isValid() |
|
93
|
|
|
{ |
|
94
|
2 |
|
$valid = $this->prefix != ''; |
|
95
|
2 |
|
$valid = $valid && $this->startMultiLine != ''; |
|
96
|
2 |
|
$valid = $valid && $this->endMultiLine != ''; |
|
97
|
2 |
|
$valid = $valid && $this->simpleDirective != ''; |
|
98
|
|
|
|
|
99
|
2 |
|
return $valid; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Getter for the prefix. |
|
104
|
|
|
* |
|
105
|
|
|
* @return string the filesystem path where the web server is installed |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function getPrefix() |
|
108
|
|
|
{ |
|
109
|
2 |
|
return $this->prefix; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Sets the prefix of a server isntance. |
|
114
|
|
|
* |
|
115
|
|
|
* @throws ServerException if the prefix is invalid |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $prefix the filesystem path where the web server is installed |
|
118
|
|
|
*/ |
|
119
|
11 |
|
public function setPrefix($prefix) |
|
120
|
|
|
{ |
|
121
|
|
|
try { |
|
122
|
11 |
|
Assert::string($prefix); |
|
123
|
11 |
|
} catch (InvalidArgumentException $e) { |
|
124
|
1 |
|
throw ServerException::forInvalidPrefix($prefix, 'The path is expected to be a string. Got: %s'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
10 |
|
if (!Path::isAbsolute($prefix)) { |
|
128
|
1 |
|
throw ServerException::forInvalidPrefix($prefix, 'The path is expected to be absolute. Got: %s'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
9 |
|
if (!is_dir($prefix)) { |
|
132
|
1 |
|
throw ServerException::forInvalidPrefix( |
|
133
|
1 |
|
$prefix, |
|
134
|
|
|
'The path is expected to be an existing directory. Got: %s' |
|
135
|
1 |
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
8 |
|
$this->prefix = $prefix; |
|
139
|
|
|
|
|
140
|
8 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Gets the regexp that will match the starting block directives. |
|
145
|
|
|
* |
|
146
|
|
|
* @return string the regexp that will match the starting block directives |
|
147
|
|
|
*/ |
|
148
|
8 |
|
public function getStartMultiLine() |
|
149
|
|
|
{ |
|
150
|
8 |
|
return $this->startMultiLine; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Sets the regexp that will match the starting block directives. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $startMultiLine the regexp that will match the starting block directives |
|
157
|
|
|
*/ |
|
158
|
13 |
|
public function setStartMultiLine($startMultiLine) |
|
159
|
|
|
{ |
|
160
|
13 |
|
$this->startMultiLine = $this->checkString( |
|
161
|
13 |
|
$startMultiLine, |
|
162
|
13 |
|
'The starting block directive matcher is expected to be a string. Got: %s', |
|
163
|
|
|
'The starting block directive matcher is expected to be a regexp '. |
|
164
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
|
165
|
13 |
|
); |
|
166
|
|
|
|
|
167
|
8 |
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Gets the regexp that will match the ending block directives. |
|
172
|
|
|
* |
|
173
|
|
|
* @return string the regexp that will match the ending block directives |
|
174
|
|
|
*/ |
|
175
|
8 |
|
public function getEndMultiLine() |
|
176
|
|
|
{ |
|
177
|
8 |
|
return $this->endMultiLine; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Sets the regexp that will match the ending block directives. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $endMultiLine the regexp that will match the ending block directives |
|
184
|
|
|
*/ |
|
185
|
10 |
|
public function setEndMultiLine($endMultiLine) |
|
186
|
|
|
{ |
|
187
|
10 |
|
$this->endMultiLine = $this->checkString( |
|
188
|
10 |
|
$endMultiLine, |
|
189
|
10 |
|
'The endind block directive matcher is expected to be a string. Got: %s', |
|
190
|
10 |
|
'The ending block directive matcher is expected to be a regexp . Got: %s', |
|
191
|
|
|
false |
|
192
|
10 |
|
); |
|
193
|
|
|
|
|
194
|
8 |
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Gets the regexp that will match the simple directives. |
|
199
|
|
|
* |
|
200
|
|
|
* @return string the regexp that will match the simple directives |
|
201
|
|
|
*/ |
|
202
|
8 |
|
public function getSimpleDirective() |
|
203
|
|
|
{ |
|
204
|
8 |
|
return $this->simpleDirective; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Sets the regexp that will match the simple directives. |
|
209
|
|
|
* |
|
210
|
|
|
* @param string $simpleDirective the regexp that will match the simple directives |
|
211
|
|
|
*/ |
|
212
|
13 |
|
public function setSimpleDirective($simpleDirective) |
|
213
|
|
|
{ |
|
214
|
13 |
|
$this->simpleDirective = $this->checkString( |
|
215
|
13 |
|
$simpleDirective, |
|
216
|
13 |
|
'The simple directive matcher is expected to be a string. Got: %s', |
|
217
|
|
|
'The simple directive matcher is expected to be a regexp '. |
|
218
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
|
219
|
13 |
|
); |
|
220
|
|
|
|
|
221
|
8 |
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Checks if the string matches some criterias. |
|
226
|
|
|
* |
|
227
|
|
|
* @param string $string the string to check |
|
228
|
|
|
* @param string $message1 message if not a string |
|
229
|
|
|
* @param string $message2 message if not a regexp |
|
230
|
|
|
* @param bool $subpattern confirms the presence of subpatterns "key" and "value" |
|
231
|
|
|
* |
|
232
|
|
|
* @throws ServerException if the string is invalid |
|
233
|
|
|
* |
|
234
|
|
|
* @return string the string |
|
235
|
|
|
*/ |
|
236
|
22 |
|
private function checkString($string, $message1, $message2, $subpattern = true) |
|
237
|
|
|
{ |
|
238
|
|
|
try { |
|
239
|
22 |
|
Assert::string($string); |
|
240
|
22 |
|
} catch (InvalidArgumentException $e) { |
|
241
|
3 |
|
throw ServerException::forInvalidMatcher($string, $message1); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
19 |
|
if (!$this->isValidRegex($string, $subpattern)) { |
|
245
|
9 |
|
throw ServerException::forInvalidMatcher($string, $message2); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
10 |
|
return $string; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Confirms if a matcher is a valid reguler expression. |
|
253
|
|
|
* |
|
254
|
|
|
* A directive matcher MUST contain a key and a value named subpattern. |
|
255
|
|
|
* |
|
256
|
|
|
* @param string $matcher the matcher to validate |
|
257
|
|
|
* @param bool $subpattern confirms the presence of subpatterns "key" and "value" |
|
258
|
|
|
* |
|
259
|
|
|
* @return bool true if the matcher is valid, false otherwise |
|
260
|
|
|
*/ |
|
261
|
19 |
|
private function isValidRegex($matcher, $subpattern = true) |
|
262
|
|
|
{ |
|
263
|
19 |
|
if (false === @preg_match($matcher, 'tester')) { |
|
264
|
3 |
|
return false; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
16 |
|
if ($subpattern && (false === strpos($matcher, '(?<key>') || false === strpos($matcher, '(?<value>'))) { |
|
268
|
6 |
|
return false; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
10 |
|
return true; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Gets the list of binaries that can be run to analyze. |
|
276
|
|
|
* |
|
277
|
|
|
* @return array the list of binaries that can be run |
|
278
|
|
|
*/ |
|
279
|
|
|
public function getBinaries() |
|
280
|
|
|
{ |
|
281
|
|
|
return $this->binaries; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Sets the list of binaries that can be run to analyze. |
|
286
|
|
|
* |
|
287
|
|
|
* @param array $binaries list of controlers |
|
288
|
|
|
*/ |
|
289
|
7 |
|
public function setBinaries(array $binaries) |
|
290
|
|
|
{ |
|
291
|
7 |
|
$this->binaries = $binaries; |
|
292
|
|
|
|
|
293
|
7 |
|
return $this; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Sets the parameter string to use to detect version and config file. |
|
298
|
|
|
* |
|
299
|
|
|
* @param string $parameter parameter string |
|
300
|
|
|
*/ |
|
301
|
7 |
|
public function setDetectionParameter($parameter = '') |
|
302
|
|
|
{ |
|
303
|
7 |
|
$this->detectionParameter = $parameter; |
|
304
|
|
|
|
|
305
|
7 |
|
return $this; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Gets the ordered list of methods to apply before the config file turns into an array. |
|
310
|
|
|
* |
|
311
|
|
|
* @return array the ordered list of methods to apply before convertion |
|
312
|
|
|
*/ |
|
313
|
11 |
|
public function getBeforeMethods() |
|
314
|
|
|
{ |
|
315
|
11 |
|
return $this->beforeMethods; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Sets the ordered list of methods to apply before the config file turns into an array. |
|
320
|
|
|
* |
|
321
|
|
|
* @param array $methods the ordered list of methods to apply before convertion |
|
322
|
|
|
*/ |
|
323
|
13 |
|
public function setBeforeMethods(array $methods) |
|
324
|
|
|
{ |
|
325
|
13 |
|
$this->beforeMethods = $methods; |
|
326
|
|
|
|
|
327
|
13 |
|
return $this; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Gets the ordered list of methods to apply after the config file has turned into an array. |
|
332
|
|
|
* |
|
333
|
|
|
* @return array the ordered list of methods to apply after convertion |
|
334
|
|
|
*/ |
|
335
|
11 |
|
public function getAfterMethods() |
|
336
|
|
|
{ |
|
337
|
11 |
|
return $this->afterMethods; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Sets the ordered list of methods to apply after the config file has turned into an array. |
|
342
|
|
|
* |
|
343
|
|
|
* @param array $methods the ordered list of methods to apply after convertion |
|
344
|
|
|
*/ |
|
345
|
13 |
|
public function setAfterMethods(array $methods) |
|
346
|
|
|
{ |
|
347
|
13 |
|
$this->afterMethods = $methods; |
|
348
|
|
|
|
|
349
|
13 |
|
return $this; |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|