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