|
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 Rule properties |
|
21
|
|
|
*/ |
|
22
|
|
|
public static $config = [ |
|
23
|
|
|
'full_class' => __CLASS__, |
|
24
|
|
|
'alias' => ['escape', 'escp', 'es'], |
|
25
|
|
|
'args_count' => 0, |
|
26
|
|
|
'args_type' => [] |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array Forbidden special chars in interger format. |
|
31
|
|
|
*/ |
|
32
|
|
|
private $special = [ |
|
33
|
|
|
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
|
34
|
|
|
58, 59, 60, 61, 62, 63, 64, |
|
35
|
|
|
91, 92, 93, 94, 95, 96, |
|
36
|
|
|
123, 124, 125, 126 |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string Error message |
|
41
|
|
|
*/ |
|
42
|
|
|
private $message = ''; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Sanitize. |
|
46
|
|
|
* |
|
47
|
|
|
* @param mixed $value |
|
48
|
|
|
*/ |
|
49
|
9 |
|
public function sanitize(&$value): void |
|
50
|
|
|
{ |
|
51
|
9 |
|
$value = $this->htmlEscape($value); |
|
52
|
9 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Convert char to html entities. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $string |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
9 |
|
private function htmlEscape(string $string): string |
|
62
|
|
|
{ |
|
63
|
9 |
|
$chars = \preg_split('//u', $string, \strlen($string), PREG_SPLIT_NO_EMPTY); |
|
64
|
9 |
|
$escaped = ''; |
|
65
|
|
|
|
|
66
|
9 |
|
if ($chars === false) { |
|
67
|
|
|
return $escaped; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
9 |
|
foreach ($chars as $char) { |
|
71
|
9 |
|
$ord = \ord($char); |
|
72
|
|
|
|
|
73
|
9 |
|
if (\in_array($ord, $this->special)) { |
|
74
|
5 |
|
$escaped .= "&#{$ord};"; |
|
75
|
5 |
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
9 |
|
$escaped .= $char; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
9 |
|
return $escaped; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Return error message. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string Error message |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function getMessage(): string |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->message; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|