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