|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Linna Filter |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
|
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types = 1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Linna\Filter\Rules; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Return html entities. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Escape implements RuleSanitizeInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array Arguments expected. |
|
21
|
|
|
*/ |
|
22
|
|
|
private $arguments = []; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array Forbidden special chars in interger format. |
|
26
|
|
|
*/ |
|
27
|
|
|
private $special = [ |
|
28
|
|
|
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
|
29
|
|
|
58, 59, 60, 61, 62, 63, 64, |
|
30
|
|
|
91, 92, 93, 94, 95, 96, |
|
31
|
|
|
123, 124, 125, 126 |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string Error message |
|
36
|
|
|
*/ |
|
37
|
|
|
private $message = ''; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Sanitize. |
|
41
|
|
|
* |
|
42
|
|
|
* @param mixed $value |
|
43
|
|
|
*/ |
|
44
|
8 |
|
public function sanitize(&$value): void |
|
45
|
|
|
{ |
|
46
|
8 |
|
$value = $this->htmlEscape($value); |
|
47
|
8 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Convert char to html entities. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $string |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
8 |
|
private function htmlEscape(string $string): string |
|
57
|
|
|
{ |
|
58
|
8 |
|
$chars = preg_split('//u', $string, strlen($string), PREG_SPLIT_NO_EMPTY); |
|
59
|
8 |
|
$escaped = ''; |
|
60
|
|
|
|
|
61
|
8 |
|
if ($chars === false) { |
|
62
|
|
|
return $escaped; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
8 |
|
foreach ($chars as $char) { |
|
66
|
8 |
|
$ord = ord($char); |
|
67
|
|
|
|
|
68
|
8 |
|
if (in_array($ord, $this->special)) { |
|
69
|
4 |
|
$escaped .= "&#{$ord};"; |
|
70
|
4 |
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
8 |
|
$escaped .= $char; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
8 |
|
return $escaped; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Return error message. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string Error message |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getMessage(): string |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->message; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|