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