|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace devtoolboxuk\cerberus; |
|
4
|
|
|
|
|
5
|
|
|
use devtoolboxuk\soteria\SoteriaService; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractCerberus |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
protected $options = []; |
|
11
|
|
|
protected $results = []; |
|
12
|
|
|
protected $result = []; |
|
13
|
|
|
protected $score = 0; |
|
14
|
|
|
protected $soteria; |
|
15
|
|
|
|
|
16
|
|
|
protected $output; |
|
17
|
|
|
protected $handlerName; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->soteria = new SoteriaService(); |
|
22
|
|
|
$this->setOptions(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
4 |
|
/** |
|
26
|
|
|
* @TODO - Add pass in json file |
|
27
|
4 |
|
* @param $file |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function setOptionsWithFile($file) |
|
30
|
|
|
{ |
|
31
|
4 |
|
if ($this->isYamlLoaded()) { |
|
32
|
4 |
|
$this->soteria->sanitise()->disinfect($file, 'url'); |
|
33
|
4 |
|
if ($this->soteria->sanitise()->result()->isValid()) { |
|
34
|
|
|
$this->options = $this->arrayMergeRecursiveDistinct($this->options, yaml_parse_file($file)); |
|
35
|
4 |
|
} else { |
|
36
|
|
|
if (file_exists($file)) { |
|
37
|
|
|
$this->options = $this->arrayMergeRecursiveDistinct($this->options, yaml_parse_file($file)); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
4 |
|
} |
|
42
|
|
|
|
|
43
|
4 |
|
/** |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
private function isYamlLoaded() |
|
47
|
4 |
|
{ |
|
48
|
|
|
return extension_loaded('yaml'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param array $merged |
|
53
|
|
|
* @param array $array2 |
|
54
|
4 |
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
4 |
|
private function arrayMergeRecursiveDistinct($merged = [], $array2 = []) |
|
57
|
|
|
{ |
|
58
|
|
|
if (empty($array2)) { |
|
59
|
|
|
return $merged; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
foreach ($array2 as $key => &$value) { |
|
63
|
|
|
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
|
64
|
4 |
|
$merged[$key] = $this->arrayMergeRecursiveDistinct($merged[$key], $value); |
|
65
|
|
|
} else { |
|
66
|
4 |
|
$merged[$key] = $value; |
|
67
|
2 |
|
} |
|
68
|
2 |
|
} |
|
69
|
|
|
return $merged; |
|
70
|
4 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param $handler |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function processWrappers($handler) |
|
76
|
|
|
{ |
|
77
|
2 |
|
$options = $this->getOption('Detection'); |
|
78
|
|
|
|
|
79
|
2 |
|
foreach ($handler->getWrappers() as $wrapper) { |
|
80
|
2 |
|
|
|
81
|
|
|
$wrapper->setOptions($handler->getInput(), $options['Rules']); |
|
82
|
|
|
$wrapper->process(); |
|
83
|
|
|
$this->addResult($wrapper->getScore(), $wrapper->getResult()); |
|
84
|
4 |
|
$this->addOutput($wrapper->getOutput()); |
|
85
|
|
|
$this->addHandlerName($handler->getName()); |
|
86
|
4 |
|
} |
|
87
|
4 |
|
} |
|
88
|
4 |
|
|
|
89
|
4 |
|
/** |
|
90
|
|
|
* @param $name |
|
91
|
|
|
* @return mixed|null |
|
92
|
|
|
*/ |
|
93
|
|
|
private function getOption($name) |
|
94
|
|
|
{ |
|
95
|
|
|
if (!$this->hasOption($name)) { |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->options[$name]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param $name |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
|
|
private function hasOption($name) |
|
107
|
|
|
{ |
|
108
|
|
|
return isset($this->options[$name]); |
|
109
|
|
|
} |
|
110
|
4 |
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param $score |
|
113
|
4 |
|
* @param $result |
|
114
|
4 |
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
4 |
|
protected function addResult($score, $result) |
|
117
|
4 |
|
{ |
|
118
|
|
|
if (is_array($result)) { |
|
119
|
|
|
$this->addScore($score); |
|
120
|
|
|
$this->result = array_merge($this->result, $result); |
|
121
|
|
|
} |
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param $score |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
|
|
private function addScore($score) |
|
130
|
|
|
{ |
|
131
|
4 |
|
$this->score += $score; |
|
132
|
4 |
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
protected function addOutput($value) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->output = $value; |
|
138
|
|
|
return $this; |
|
139
|
4 |
|
} |
|
140
|
|
|
|
|
141
|
4 |
|
protected function getOutput() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->output; |
|
144
|
|
|
} |
|
145
|
4 |
|
|
|
146
|
4 |
|
protected function addHandlerName($handlerName) |
|
147
|
4 |
|
{ |
|
148
|
|
|
$this->handlerName = $handlerName; |
|
149
|
4 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
4 |
|
protected function getHandlerName() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->handlerName; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
protected function clearResults() |
|
158
|
|
|
{ |
|
159
|
|
|
$this->results = []; |
|
160
|
|
|
$this->result = []; |
|
161
|
|
|
$this->score = 0; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function getOptions() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->options; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param array $options |
|
174
|
|
|
*/ |
|
175
|
|
|
public function setOptions($options = []) |
|
176
|
|
|
{ |
|
177
|
|
|
$baseOptions = new BaseOptions(); |
|
178
|
|
|
$this->options = $this->arrayMergeRecursiveDistinct($baseOptions->getOptions(), $options); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
} |