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; |
13
|
|
|
|
14
|
|
|
use WebHelper\Parser\Server\ServerInterface; |
15
|
|
|
use WebHelper\Parser\Exception\ParserException; |
16
|
|
|
use WebHelper\Parser\Exception\InvalidConfigException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Web server configuration generic parser. |
20
|
|
|
* |
21
|
|
|
* @author James <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class Parser implements ParserInterface |
24
|
|
|
{ |
25
|
|
|
/** @var Server\ServerInterface a server instance */ |
26
|
|
|
private $server; |
27
|
|
|
|
28
|
|
|
/** @var string configuration file */ |
29
|
|
|
private $configFile = ''; |
30
|
|
|
|
31
|
|
|
/** @var array active directives in an array */ |
32
|
|
|
protected $activeConfig = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Setter for the server instance. |
36
|
|
|
* |
37
|
|
|
* @see Server\ServerInterface Server Documentation |
38
|
|
|
* |
39
|
|
|
* @param Server\ServerInterface $server the server instance |
40
|
|
|
*/ |
41
|
8 |
|
public function setServer(ServerInterface $server) |
42
|
|
|
{ |
43
|
8 |
|
$this->server = $server; |
44
|
|
|
|
45
|
8 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Setter for the config file to parse. |
50
|
|
|
* |
51
|
|
|
* @param string $configFile configuration file |
52
|
|
|
* |
53
|
|
|
* @throws Exception\ParserException if configuration file is not readable |
54
|
|
|
* @throws Exception\InvalidConfigException if active configuration is empty |
55
|
|
|
*/ |
56
|
12 |
|
public function setConfigFile($configFile = '') |
57
|
|
|
{ |
58
|
12 |
|
if (!is_readable($configFile)) { |
59
|
1 |
|
$this->activeConfig = []; |
60
|
|
|
|
61
|
1 |
|
throw ParserException::forFileUnreadable($configFile); |
62
|
|
|
} |
63
|
|
|
|
64
|
11 |
|
$this->configFile = $configFile; |
65
|
11 |
|
if (!$this->parseConfigFile()) { |
66
|
2 |
|
throw InvalidConfigException::forEmptyConfig($configFile); |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Getter for the server instance. |
74
|
|
|
* |
75
|
|
|
* @see Server\ServerInterface Server Documentation |
76
|
|
|
* |
77
|
|
|
* @param Server\ServerInterface the server instance |
78
|
|
|
*/ |
79
|
1 |
|
public function getServer() |
80
|
|
|
{ |
81
|
1 |
|
return $this->server; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Getter for the active config main context. |
86
|
|
|
* |
87
|
|
|
* @return Directive\DirectiveInterface the active config |
88
|
|
|
*/ |
89
|
|
|
abstract public function getActiveConfig(); |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Getter for the content of the configuration file. |
93
|
|
|
* |
94
|
|
|
* @return string content of the configuration file |
95
|
|
|
*/ |
96
|
11 |
|
public function getOriginalConfig() |
97
|
|
|
{ |
98
|
11 |
|
return file_get_contents($this->configFile); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Does some extra parsing before the active config turns into an array. |
103
|
|
|
* |
104
|
|
|
* @param string $config a config file content |
105
|
|
|
* |
106
|
|
|
* @return string a config file content |
107
|
|
|
*/ |
108
|
11 |
|
protected function beforeExplode($config) |
109
|
|
|
{ |
110
|
11 |
|
$config = $this->deleteComments($config); |
111
|
|
|
|
112
|
11 |
|
return $config; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Does some extra parsing after the active config has turned into an array. |
117
|
|
|
* |
118
|
|
|
* @param array $activeConfig an active config |
119
|
|
|
* |
120
|
|
|
* @return array an active config |
121
|
|
|
*/ |
122
|
11 |
|
protected function afterExplode(array $activeConfig) |
123
|
|
|
{ |
124
|
11 |
|
$activeConfig = $this->deleteBlankLines($activeConfig); |
125
|
|
|
|
126
|
11 |
|
return $activeConfig; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Comon parsing to both apache and nginx. |
131
|
|
|
* |
132
|
|
|
* @return bool true if active lines were found |
133
|
|
|
*/ |
134
|
11 |
|
private function parseConfigFile() |
135
|
|
|
{ |
136
|
11 |
|
$activeConfig = $this->getOriginalConfig(); |
137
|
11 |
|
$activeConfig = $this->beforeExplode($activeConfig); |
138
|
|
|
|
139
|
|
|
//convert into an array |
140
|
11 |
|
$activeConfig = explode("\n", $activeConfig); |
141
|
|
|
|
142
|
11 |
|
$this->activeConfig = $this->afterExplode($activeConfig); |
143
|
|
|
|
144
|
11 |
|
return !empty($this->activeConfig); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Deletes commented lines and end line comments. |
149
|
|
|
* |
150
|
|
|
* @param string $config a file content |
151
|
|
|
* |
152
|
|
|
* @return string a file content without comments |
153
|
|
|
*/ |
154
|
11 |
|
private function deleteComments($config = '') |
155
|
|
|
{ |
156
|
11 |
|
$config = preg_replace('/^\s*#.*/m', '', $config); |
157
|
11 |
|
$config = preg_replace('/^([^#]+)#.*/m', '$1', $config); |
158
|
|
|
|
159
|
11 |
|
return $config; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Trim all blank lines. |
164
|
|
|
* |
165
|
|
|
* @param array $activeConfig config file exploded in an array of lines |
166
|
|
|
* |
167
|
|
|
* @return array an array cleaned of blank lines |
168
|
|
|
*/ |
169
|
11 |
|
private function deleteBlankLines(array $activeConfig = array()) |
170
|
|
|
{ |
171
|
11 |
|
$cleanedActiveConfig = []; |
172
|
|
|
|
173
|
11 |
|
foreach (array_map('trim', $activeConfig) as $line) { |
174
|
11 |
|
if ($line != '') { |
175
|
9 |
|
$cleanedActiveConfig[] = $line; |
176
|
9 |
|
} |
177
|
11 |
|
} |
178
|
|
|
|
179
|
11 |
|
return $cleanedActiveConfig; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|