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
|
|
|
function __construct() |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$this->soteria = new SoteriaService(); |
19
|
|
|
$this->setOptions(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
|
23
|
4 |
|
protected function processWrappers($handler) |
24
|
|
|
{ |
25
|
4 |
|
$options = $this->getOption('Detection'); |
26
|
|
|
|
27
|
4 |
|
foreach ($handler->getWrappers() as $wrapper) { |
|
|
|
|
28
|
|
|
|
29
|
4 |
|
$wrapper->setOptions($handler->getValue(), $options['Rules']); |
30
|
4 |
|
$wrapper->process(); |
31
|
4 |
|
$this->addResult($wrapper->getScore(), $wrapper->getResult()); |
32
|
|
|
} |
33
|
4 |
|
} |
34
|
|
|
|
35
|
4 |
|
private function getOption($name) |
36
|
|
|
{ |
37
|
4 |
|
if (!$this->hasOption($name)) { |
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
return $this->options[$name]; |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
private function hasOption($name) |
45
|
|
|
{ |
46
|
4 |
|
return isset($this->options[$name]); |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
protected function addResult($score, $result) |
50
|
|
|
{ |
51
|
4 |
|
if (is_array($result)) { |
52
|
2 |
|
$this->addScore($score); |
53
|
2 |
|
$this->result = array_merge($this->result, $result); |
54
|
|
|
} |
55
|
4 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $score |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
2 |
|
private function addScore($score) |
63
|
|
|
{ |
64
|
2 |
|
$this->score += $score; |
65
|
2 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
protected function clearResults() |
69
|
|
|
{ |
70
|
4 |
|
$this->results = []; |
71
|
4 |
|
$this->result = []; |
72
|
4 |
|
$this->score = 0; |
73
|
4 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
protected function getOptions() |
79
|
|
|
{ |
80
|
|
|
return $this->options; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param file|array $options |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
4 |
|
public function setOptions($options = []) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
|
90
|
4 |
|
$baseOptions = new BaseOptions(); |
91
|
4 |
|
$basic_options = $baseOptions->getOptions(); |
92
|
|
|
|
93
|
4 |
|
if (is_array($options)) { |
94
|
4 |
|
$options = $this->arrayMergeRecursiveDistinct($basic_options, $options); |
95
|
|
|
} else { |
|
|
|
|
96
|
|
|
if ($this->yaml_loaded()) { |
|
|
|
|
97
|
|
|
$this->soteria->sanitise()->disinfect($options, 'url'); |
98
|
|
|
if ($this->soteria->sanitise()->result()->isValid()) { |
99
|
|
|
$options = $this->arrayMergeRecursiveDistinct($basic_options, yaml_parse_file($options)); |
100
|
|
|
} else { |
|
|
|
|
101
|
|
|
if (file_exists($options)) { |
102
|
|
|
$options = $this->arrayMergeRecursiveDistinct($basic_options, yaml_parse_file($options)); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
4 |
|
$this->options = $options; |
|
|
|
|
109
|
4 |
|
} |
110
|
|
|
|
111
|
4 |
|
private function arrayMergeRecursiveDistinct($merged = [], $array2 = []) |
|
|
|
|
112
|
|
|
{ |
113
|
4 |
|
if (empty($array2)) { |
114
|
|
|
return $merged; |
115
|
|
|
} |
116
|
|
|
|
117
|
4 |
|
foreach ($array2 as $key => &$value) { |
118
|
4 |
|
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
119
|
4 |
|
$merged[$key] = $this->arrayMergeRecursiveDistinct($merged[$key], $value); |
120
|
|
|
} else { |
|
|
|
|
121
|
4 |
|
$merged[$key] = $value; |
122
|
|
|
} |
123
|
|
|
} |
124
|
4 |
|
return $merged; |
125
|
|
|
} |
126
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.