1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Config; |
4
|
|
|
|
5
|
|
|
use Cache\Adapter\Filesystem\FilesystemCachePool; |
6
|
|
|
use League\Flysystem\Adapter\Local; |
7
|
|
|
use League\Flysystem\Filesystem; |
8
|
|
|
use phm\HttpWebdriverClient\Http\Client\HttpClient; |
9
|
|
|
use phmLabs\Components\Annovent\Dispatcher; |
10
|
|
|
use PhmLabs\Components\Init\Init; |
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
12
|
|
|
use whm\Html\Uri; |
13
|
|
|
use whm\Smoke\Http\GuzzleClient; |
14
|
|
|
use whm\Smoke\Http\Session; |
15
|
|
|
use whm\Smoke\Rules\Rule; |
16
|
|
|
use whm\Smoke\Scanner\SessionContainer; |
17
|
|
|
|
18
|
|
|
class Configuration |
19
|
|
|
{ |
20
|
|
|
const DEFAULT_SETTINGS = 'analyze.yml'; |
21
|
|
|
|
22
|
|
|
const CONFIG_RULES_KEY = 'rules'; |
23
|
|
|
|
24
|
|
|
const CACHE_DIR = '/tmp/cache/smoke/'; |
25
|
|
|
|
26
|
|
|
private $startUri; |
27
|
|
|
|
28
|
|
|
private $rules = []; |
29
|
|
|
|
30
|
|
|
private $configArray; |
31
|
|
|
|
32
|
|
|
private $eventDispatcher; |
33
|
|
|
|
34
|
|
|
private $verbose = false; |
35
|
|
|
|
36
|
|
|
private $extensions = array(); |
37
|
|
|
|
38
|
|
|
private $runLevels = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var SessionContainer |
42
|
|
|
*/ |
43
|
|
|
private $sessionContainer; |
44
|
|
|
|
45
|
|
|
public function __construct(Uri $uri, Dispatcher $eventDispatcher, array $configArray, array $defaultSettings = null) |
46
|
|
|
{ |
47
|
|
|
$this->eventDispatcher = $eventDispatcher; |
48
|
|
|
Init::registerGlobalParameter('_configuration', $this); |
49
|
|
|
|
50
|
|
|
$this->initConfigArray($configArray, $defaultSettings); |
51
|
|
|
$this->initCache(); |
52
|
|
|
|
53
|
|
|
if (array_key_exists('sessions', $this->configArray)) { |
54
|
|
|
$this->initSessionContainer($this->configArray['sessions']); |
55
|
|
|
} else { |
56
|
|
|
$this->sessionContainer = new SessionContainer(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (array_key_exists('extensions', $this->configArray)) { |
60
|
|
|
$this->addListener($this->configArray['extensions']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!array_key_exists(self::CONFIG_RULES_KEY, $this->configArray)) { |
64
|
|
|
$this->configArray[self::CONFIG_RULES_KEY] = []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->startUri = $uri; |
68
|
|
|
$this->initRules($this->configArray[self::CONFIG_RULES_KEY]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function initCache() |
72
|
|
|
{ |
73
|
|
|
$cachePool = new FilesystemCachePool(new Filesystem(new Local(self::CACHE_DIR))); |
74
|
|
|
Init::registerGlobalParameter('_cacheItemPool', $cachePool); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function initLogger($loggerArray) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if ($loggerArray) { |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function initConfigArray(array $configArray, array $defaultSettings = null) |
85
|
|
|
{ |
86
|
|
|
if ($defaultSettings === null) { |
87
|
|
|
$defaultSettings = Yaml::parse(file_get_contents(__DIR__ . '/../settings/' . self::DEFAULT_SETTINGS)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (count($configArray) === 0) { |
91
|
|
|
$configArray = $defaultSettings; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (array_key_exists('options', $configArray)) { |
95
|
|
|
if (array_key_exists('extendDefault', $configArray['options'])) { |
96
|
|
|
if ($configArray['options']['extendDefault'] === true) { |
97
|
|
|
$configArray = array_replace_recursive($defaultSettings, $configArray); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (array_key_exists('verbose', $configArray['options']) && $configArray['options']['verbose'] === true) { |
102
|
|
|
$this->verbose = true; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->configArray = $configArray; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function initSessionContainer(array $sessionsArray) |
110
|
|
|
{ |
111
|
|
|
$this->sessionContainer = new SessionContainer(); |
112
|
|
|
|
113
|
|
|
foreach ($sessionsArray as $sessionName => $sessionsElement) { |
114
|
|
|
$session = new Session(); |
115
|
|
|
|
116
|
|
|
if (array_key_exists('cookies', $sessionsElement)) { |
117
|
|
|
foreach ($sessionsElement['cookies'] as $key => $value) { |
118
|
|
|
$session->addCookie($key, $value); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->sessionContainer->addSession($sessionName, $session); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $listenerArray |
128
|
|
|
* @throws \phmLabs\Components\Annovent\Exception |
129
|
|
|
*/ |
130
|
|
|
private function addListener(array $listenerArray) |
131
|
|
|
{ |
132
|
|
|
foreach ($listenerArray as $key => $listenerConfig) { |
133
|
|
|
$extension = Init::initialize($listenerConfig); |
134
|
|
|
$this->extensions[$key] = $extension; |
135
|
|
|
$this->eventDispatcher->connectListener($extension); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return Uri |
141
|
|
|
*/ |
142
|
|
|
public function getStartUri() |
143
|
|
|
{ |
144
|
|
|
return $this->startUri; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* This function initializes all the rules and sets the log level. |
149
|
|
|
* |
150
|
|
|
* @param array $rulesArray |
151
|
|
|
*/ |
152
|
|
|
private function initRules(array $rulesArray) |
153
|
|
|
{ |
154
|
|
|
$this->rules = Init::initializeAll($rulesArray); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns the log level of a given rule. |
159
|
|
|
* |
160
|
|
|
* @param string $key |
161
|
|
|
* |
162
|
|
|
* @return int |
163
|
|
|
*/ |
164
|
|
|
public function getRuleRunLevel($key) |
165
|
|
|
{ |
166
|
|
|
return $this->runLevels[$key]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return Rule[] |
171
|
|
|
*/ |
172
|
|
|
public function getRules() |
173
|
|
|
{ |
174
|
|
|
return $this->rules; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function getSessionContainer() |
178
|
|
|
{ |
179
|
|
|
return $this->sessionContainer; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function hasSection($section) |
183
|
|
|
{ |
184
|
|
|
return array_key_exists($section, $this->configArray); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param $section |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
public function getSection($section) |
193
|
|
|
{ |
194
|
|
|
if ($this->hasSection($section)) { |
195
|
|
|
return $this->configArray[$section]; |
196
|
|
|
} else { |
197
|
|
|
throw new \RuntimeException('The section (' . $section . ') you are trying to access does not exist.'); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
View Code Duplication |
public function getExtension($name) |
202
|
|
|
{ |
203
|
|
|
if (array_key_exists($name, $this->extensions)) { |
204
|
|
|
return $this->extensions[$name]; |
205
|
|
|
} else { |
206
|
|
|
throw new \RuntimeException('The extension ("' . $name . '") you are trying to access does not exist. Registered extensions are: ' . implode(' ,', array_keys($this->extensions)) . '.'); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param $name |
212
|
|
|
* @param $extension |
213
|
|
|
* @throws \phmLabs\Components\Annovent\Exception |
214
|
|
|
*/ |
215
|
|
|
public function addExtension($name, $extension) |
216
|
|
|
{ |
217
|
|
|
$this->extensions[$name] = $extension; |
218
|
|
|
$this->eventDispatcher->connectListener($extension); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function getConfigArray() |
222
|
|
|
{ |
223
|
|
|
return $this->configArray; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return HttpClient |
228
|
|
|
*/ |
229
|
|
|
public function getClient() |
230
|
|
|
{ |
231
|
|
|
if (array_key_exists('client', $this->configArray)) { |
232
|
|
|
|
233
|
|
|
try { |
234
|
|
|
$client = Init::initialize($this->configArray['client']); |
235
|
|
|
} catch (\Exception $e) { |
236
|
|
|
throw new ConfigurationException('Error initializing client (' . $e->getMessage() . ')'); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $client; |
240
|
|
|
} else { |
241
|
|
|
$client = new GuzzleClient(); |
242
|
|
|
$client->init(); |
243
|
|
|
return $client; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
|
|
public function isVerbose(): bool |
251
|
|
|
{ |
252
|
|
|
return $this->verbose; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|