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