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
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->soteria = new SoteriaService(); |
19
|
|
|
$this->setOptions(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param $handler |
24
|
|
|
*/ |
25
|
4 |
|
protected function processWrappers($handler) |
26
|
|
|
{ |
27
|
4 |
|
$options = $this->getOption('Detection'); |
28
|
|
|
|
29
|
4 |
|
foreach ($handler->getWrappers() as $wrapper) { |
|
|
|
|
30
|
|
|
|
31
|
4 |
|
$wrapper->setOptions($handler->getValue(), $options['Rules']); |
32
|
4 |
|
$wrapper->process(); |
33
|
4 |
|
$this->addResult($wrapper->getScore(), $wrapper->getResult()); |
34
|
|
|
} |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $name |
39
|
|
|
* @return mixed|null |
40
|
|
|
*/ |
41
|
4 |
|
private function getOption($name) |
42
|
|
|
{ |
43
|
4 |
|
if (!$this->hasOption($name)) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
return $this->options[$name]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $name |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
4 |
|
private function hasOption($name) |
55
|
|
|
{ |
56
|
4 |
|
return isset($this->options[$name]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $score |
61
|
|
|
* @param $result |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
4 |
|
protected function addResult($score, $result) |
65
|
|
|
{ |
66
|
4 |
|
if (is_array($result)) { |
67
|
2 |
|
$this->addScore($score); |
68
|
2 |
|
$this->result = array_merge($this->result, $result); |
69
|
|
|
} |
70
|
4 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $score |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
2 |
|
private function addScore($score) |
78
|
|
|
{ |
79
|
2 |
|
$this->score += $score; |
80
|
2 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
4 |
|
protected function clearResults() |
85
|
|
|
{ |
86
|
4 |
|
$this->results = []; |
87
|
4 |
|
$this->result = []; |
88
|
4 |
|
$this->score = 0; |
89
|
4 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
protected function getOptions() |
95
|
|
|
{ |
96
|
|
|
return $this->options; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
private function isYamlLoaded() |
103
|
|
|
{ |
104
|
|
|
return extension_loaded('yaml'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param null $options |
109
|
|
|
*/ |
110
|
4 |
|
public function setOptions($options = null) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
|
113
|
4 |
|
$baseOptions = new BaseOptions(); |
114
|
4 |
|
$basic_options = $baseOptions->getOptions(); |
115
|
|
|
|
116
|
4 |
|
if (is_array($options)) { |
117
|
4 |
|
$options = $this->arrayMergeRecursiveDistinct($basic_options, $options); |
118
|
|
|
} else { |
|
|
|
|
119
|
|
|
if ($this->isYamlLoaded()) { |
120
|
|
|
$this->soteria->sanitise()->disinfect($options, 'url'); |
121
|
|
|
if ($this->soteria->sanitise()->result()->isValid()) { |
122
|
|
|
$options = $this->arrayMergeRecursiveDistinct($basic_options, yaml_parse_file($options)); |
123
|
|
|
} else { |
|
|
|
|
124
|
|
|
if (file_exists($options)) { |
125
|
|
|
$options = $this->arrayMergeRecursiveDistinct($basic_options, yaml_parse_file($options)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
$this->options = $options; |
|
|
|
|
132
|
4 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param array $merged |
136
|
|
|
* @param array $array2 |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
4 |
|
private function arrayMergeRecursiveDistinct($merged = [], $array2 = []) |
|
|
|
|
140
|
|
|
{ |
141
|
4 |
|
if (empty($array2)) { |
142
|
|
|
return $merged; |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
foreach ($array2 as $key => &$value) { |
146
|
4 |
|
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
147
|
4 |
|
$merged[$key] = $this->arrayMergeRecursiveDistinct($merged[$key], $value); |
148
|
|
|
} else { |
|
|
|
|
149
|
4 |
|
$merged[$key] = $value; |
150
|
|
|
} |
151
|
|
|
} |
152
|
4 |
|
return $merged; |
153
|
|
|
} |
154
|
|
|
} |