|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace devtoolboxuk\soteria\handlers; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use devtoolboxuk\soteria\classes\Filters; |
|
7
|
|
|
use devtoolboxuk\soteria\models\SoteriaModel; |
|
8
|
|
|
|
|
9
|
|
|
class Sanitise |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
private $is_valid = null; |
|
13
|
|
|
private $_sanitised = null; |
|
14
|
|
|
private $filters; |
|
15
|
|
|
private $input; |
|
16
|
|
|
private $output; |
|
17
|
|
|
|
|
18
|
|
|
private $urlRegEx = '/\b((https?|ftp|file):\/\/|www\.)[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i'; |
|
19
|
|
|
|
|
20
|
|
|
function __construct() |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$this->filters = new Filters(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* Removes URLs from strings |
|
28
|
|
|
* |
|
29
|
|
|
* @param array|string $data |
|
30
|
|
|
* @return array|string|string[]|null |
|
31
|
|
|
*/ |
|
32
|
|
|
public function removeUrl($data) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->_sanitised = null; |
|
35
|
|
|
|
|
36
|
|
|
if (is_array($data)) { |
|
37
|
|
|
foreach ($data as $key => $value) { |
|
38
|
|
|
$data[$key] = $this->removeUrl($value); |
|
39
|
|
|
} |
|
40
|
|
|
return $data; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->input = $data; |
|
44
|
|
|
|
|
45
|
|
|
$data = $this->cleanString($data); |
|
46
|
|
|
$data = preg_replace($this->urlRegEx, ' ', $data); |
|
47
|
|
|
|
|
48
|
|
|
if ($this->input != $data) { |
|
49
|
|
|
$this->_sanitised = true; |
|
50
|
|
|
} |
|
51
|
|
|
$this->is_valid = true; |
|
52
|
|
|
|
|
53
|
|
|
$this->output = $data; |
|
54
|
|
|
return $data; |
|
55
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $data |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
1 |
|
private function cleanString($data) |
|
63
|
|
|
{ |
|
64
|
1 |
|
$data = implode("", explode("\\", $data)); |
|
65
|
1 |
|
return strip_tags(trim(stripslashes($data))); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $data |
|
70
|
|
|
* @return array|false|string|string[]|null |
|
71
|
|
|
*/ |
|
72
|
|
|
public function cleanse($data) |
|
73
|
|
|
{ |
|
74
|
|
|
|
|
75
|
|
|
if (is_array($data)) { |
|
76
|
|
|
foreach ($data as $key => $value) { |
|
77
|
|
|
$data[$key] = $this->cleanse($value); |
|
78
|
|
|
} |
|
79
|
|
|
return $data; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->input = $data; |
|
83
|
|
|
$data = $this->cleanString($data); |
|
84
|
|
|
$data = mb_convert_encoding($data, "utf-8", "auto"); |
|
85
|
|
|
$data = htmlspecialchars_decode($data); |
|
86
|
|
|
$data = $this->cleanString($data); |
|
87
|
|
|
if ($this->input != $data) { |
|
88
|
|
|
$this->_sanitised = true; |
|
89
|
|
|
} |
|
90
|
|
|
$this->output = $data; |
|
91
|
|
|
return $data; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $string |
|
96
|
|
|
* @param string $delimiter |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function cleanseCsv($string, $delimiter = "|") |
|
100
|
|
|
{ |
|
101
|
|
|
return trim(str_replace([$delimiter, "\n", "\r", "\t"], " ", $string)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param $data |
|
106
|
|
|
* @param string $type |
|
107
|
|
|
* @param int $stringLength |
|
108
|
|
|
* @return mixed|string |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function disinfect($data, $type = 'special_chars', $stringLength = -1) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
|
|
113
|
1 |
|
$this->_sanitised = null; |
|
114
|
|
|
|
|
115
|
1 |
|
if (is_array($data)) { |
|
116
|
|
|
foreach ($data as $key => $value) { |
|
117
|
|
|
$data[$key] = $this->disinfect($value, $type, $stringLength); |
|
118
|
|
|
} |
|
119
|
|
|
return $data; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
$this->input = $data; |
|
123
|
|
|
|
|
124
|
1 |
|
$data = $this->cleanString($data); |
|
125
|
1 |
|
$data = $this->stringLength($data, $stringLength); |
|
126
|
|
|
|
|
127
|
|
|
switch ($type) { |
|
128
|
1 |
|
case "email": |
|
129
|
1 |
|
$filterResult = $this->filters->filterEmail($data); |
|
130
|
1 |
|
break; |
|
131
|
|
|
|
|
132
|
|
|
case "encoded": |
|
133
|
|
|
$filterResult = $this->filters->filterEncoded($data); |
|
134
|
|
|
break; |
|
135
|
|
|
|
|
136
|
|
|
case "number_float": |
|
137
|
|
|
case "float": |
|
138
|
|
|
$filterResult = $this->filters->filterFloat($data); |
|
139
|
|
|
break; |
|
140
|
|
|
|
|
141
|
|
|
case "number_int": |
|
142
|
|
|
case "int": |
|
143
|
|
|
$filterResult = $this->filters->filtetInt($data); |
|
|
|
|
|
|
144
|
|
|
break; |
|
145
|
|
|
|
|
146
|
|
|
case "full_special_chars": |
|
147
|
|
|
$filterResult = $this->filters->filterFullSpecialChar($data); |
|
148
|
|
|
break; |
|
149
|
|
|
|
|
150
|
|
|
case "url": |
|
151
|
|
|
$filterResult = $this->filters->filterUrl($data); |
|
152
|
|
|
break; |
|
153
|
|
|
|
|
154
|
|
|
case "string": |
|
155
|
|
|
$filterResult = $this->filters->filterString($data); |
|
156
|
|
|
break; |
|
157
|
|
|
|
|
158
|
|
|
default: |
|
159
|
|
|
case "special_chars": |
|
|
|
|
|
|
160
|
|
|
$filterResult = $this->filters->filterSpecial($data); |
|
161
|
|
|
break; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
1 |
|
if ($this->input != $filterResult->getResult()) { |
|
165
|
|
|
$this->_sanitised = true; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
$this->is_valid = $filterResult->isValid(); |
|
169
|
1 |
|
$this->output = $filterResult->getResult(); |
|
170
|
1 |
|
return $this->output; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param $data |
|
175
|
|
|
* @param int $length |
|
176
|
|
|
* @return bool|string |
|
177
|
|
|
*/ |
|
178
|
1 |
|
private function stringLength($data, $length = -1) |
|
179
|
|
|
{ |
|
180
|
1 |
|
if ($length > 0) { |
|
181
|
|
|
if (mb_strlen($data) > $length) { |
|
182
|
|
|
$data = substr($data, 0, $length); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
1 |
|
return $data; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @return null |
|
191
|
|
|
*/ |
|
192
|
|
|
public function isSanitised() |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->_sanitised; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Returns true if the data is valid |
|
199
|
|
|
* @return null |
|
200
|
|
|
*/ |
|
201
|
|
|
public function isValid() |
|
202
|
|
|
{ |
|
203
|
|
|
return $this->is_valid; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
1 |
|
function result() |
|
|
|
|
|
|
207
|
|
|
{ |
|
208
|
1 |
|
$valid = false; |
|
209
|
1 |
|
if (!$this->_sanitised && $this->is_valid) { |
|
|
|
|
|
|
210
|
1 |
|
$valid = true; |
|
211
|
1 |
|
} |
|
212
|
1 |
|
return new SoteriaModel($this->input, $this->output, $valid); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
} |
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.