1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtoolboxuk\soteria\handlers; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use devtoolboxuk\soteria\classes\Filters; |
7
|
|
|
use devtoolboxuk\soteria\classes\Strings; |
8
|
|
|
use devtoolboxuk\soteria\classes\Url; |
9
|
|
|
use devtoolboxuk\soteria\models\SoteriaModel; |
10
|
|
|
|
11
|
|
|
class Sanitise |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
private $is_valid = null; |
15
|
|
|
private $_sanitised = null; |
16
|
|
|
private $filters; |
17
|
|
|
private $input; |
18
|
|
|
private $output; |
19
|
|
|
private $strings; |
20
|
|
|
private $urlService; |
21
|
|
|
|
22
|
|
|
function __construct() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$this->filters = new Filters(); |
25
|
|
|
$this->strings = new Strings(); |
26
|
|
|
$this->urlService = new Url(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* Removes URLs from strings |
32
|
|
|
* |
33
|
|
|
* @param array|string $data |
34
|
|
|
* @return array|string|string[]|null |
35
|
|
|
*/ |
36
|
|
|
public function removeUrl($data) |
37
|
|
|
{ |
38
|
|
|
$this->_sanitised = null; |
39
|
|
|
|
40
|
|
|
if (is_array($data)) { |
41
|
|
|
foreach ($data as $key => $value) { |
42
|
|
|
$data[$key] = $this->removeUrl($value); |
43
|
|
|
} |
44
|
|
|
return $data; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->input = $data; |
48
|
|
|
|
49
|
|
|
$data = $this->strings->clean($data); |
50
|
|
|
$data = $this->urlService->remove($data); |
51
|
|
|
|
52
|
|
|
if ($this->input != $data) { |
53
|
|
|
$this->_sanitised = true; |
54
|
|
|
} |
55
|
|
|
$this->is_valid = true; |
56
|
|
|
|
57
|
|
|
$this->output = $data; |
58
|
|
|
return $data; |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $data |
64
|
|
|
* @param string $toEncoding |
65
|
|
|
* @param string $fromEncoding |
66
|
|
|
* @return array|false|string|string[]|null |
67
|
|
|
*/ |
68
|
|
|
public function cleanse($data, $toEncoding = 'utf-8', $fromEncoding = 'auto') |
69
|
|
|
{ |
70
|
|
|
|
71
|
|
|
if (is_array($data)) { |
72
|
|
|
foreach ($data as $key => $value) { |
73
|
|
|
$data[$key] = $this->cleanse($value, $toEncoding, $fromEncoding); |
74
|
|
|
} |
75
|
|
|
return $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->input = $data; |
79
|
|
|
$data = $this->strings->clean($data); |
80
|
|
|
$data = mb_convert_encoding($data, $toEncoding, $fromEncoding); |
81
|
|
|
$data = htmlspecialchars_decode($data); |
82
|
|
|
$data = $this->strings->clean($data); |
83
|
|
|
if ($this->input != $data) { |
84
|
|
|
$this->_sanitised = true; |
85
|
|
|
} |
86
|
|
|
$this->output = $data; |
87
|
|
|
return $data; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $string |
92
|
|
|
* @param string $delimiter |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function cleanseCsv($string, $delimiter = "|") |
96
|
|
|
{ |
97
|
|
|
return trim(str_replace([$delimiter, "\n", "\r", "\t"], " ", $string)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $data |
102
|
|
|
* @param string $type |
103
|
|
|
* @param int $stringLength |
104
|
|
|
* @return mixed|string |
105
|
|
|
*/ |
106
|
1 |
|
public function disinfect($data, $type = 'special_chars', $stringLength = -1) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
|
109
|
1 |
|
$this->_sanitised = null; |
110
|
|
|
|
111
|
1 |
|
if (is_array($data)) { |
112
|
|
|
foreach ($data as $key => $value) { |
113
|
|
|
$data[$key] = $this->disinfect($value, $type, $stringLength); |
114
|
|
|
} |
115
|
|
|
return $data; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
$this->input = $data; |
119
|
|
|
|
120
|
1 |
|
$data = $this->strings->clean($data); |
121
|
1 |
|
$data = $this->strings->stringLength($data, $stringLength); |
122
|
|
|
|
123
|
|
|
switch ($type) { |
124
|
1 |
|
case "email": |
125
|
1 |
|
$filterResult = $this->filters->filterEmail($data); |
126
|
1 |
|
break; |
127
|
|
|
|
128
|
|
|
case "encoded": |
129
|
|
|
$filterResult = $this->filters->filterEncoded($data); |
130
|
|
|
break; |
131
|
|
|
|
132
|
|
|
case "number_float": |
133
|
|
|
case "float": |
134
|
|
|
$filterResult = $this->filters->filterFloat($data); |
135
|
|
|
break; |
136
|
|
|
|
137
|
|
|
case "number_int": |
138
|
|
|
case "int": |
139
|
|
|
$filterResult = $this->filters->filterInt($data); |
140
|
|
|
break; |
141
|
|
|
|
142
|
|
|
case "full_special_chars": |
143
|
|
|
$filterResult = $this->filters->filterFullSpecialChar($data); |
144
|
|
|
break; |
145
|
|
|
|
146
|
|
|
case "url": |
147
|
|
|
$filterResult = $this->filters->filterUrl($data); |
148
|
|
|
break; |
149
|
|
|
|
150
|
|
|
case "string": |
151
|
|
|
$filterResult = $this->filters->filterString($data); |
152
|
|
|
break; |
153
|
|
|
|
154
|
|
|
default: |
155
|
|
|
case "special_chars": |
|
|
|
|
156
|
|
|
$filterResult = $this->filters->filterSpecial($data); |
157
|
|
|
break; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
if ($this->input != $filterResult->getResult()) { |
161
|
|
|
$this->_sanitised = true; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
$this->is_valid = $filterResult->isValid(); |
165
|
1 |
|
$this->output = $filterResult->getResult(); |
166
|
1 |
|
return $this->output; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return null |
172
|
|
|
*/ |
173
|
|
|
public function isSanitised() |
174
|
|
|
{ |
175
|
|
|
return $this->_sanitised; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns true if the data is valid |
180
|
|
|
* @return null |
181
|
|
|
*/ |
182
|
|
|
public function isValid() |
183
|
|
|
{ |
184
|
|
|
return $this->is_valid; |
185
|
|
|
} |
186
|
|
|
|
187
|
1 |
|
function result() |
|
|
|
|
188
|
|
|
{ |
189
|
1 |
|
$valid = false; |
190
|
1 |
|
if (!$this->_sanitised && $this->is_valid) { |
|
|
|
|
191
|
1 |
|
$valid = true; |
192
|
1 |
|
} |
193
|
1 |
|
return new SoteriaModel($this->input, $this->output, $valid); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.