-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sanitize.php
78 lines (73 loc) · 1.4 KB
/
Sanitize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace core;
trait SanitizeStrippers
{
public static function number()
{
return "/[^0-9]/";
}
public static function numberchars()
{
return "/[^A-Za-z0-9]/";
}
public static function slug()
{
return "/[^A-Za-z0-9_\-]/";
}
public static function text()
{
return "/[^[:alnum:][:space:]:\.\\\\\/_\-\(\)+]/u";
}
public static function chars()
{
return "/[^A-Za-z]/";
}
public static function xss($txt)
{
return htmlspecialchars(strip_tags($txt));
}
/**
* Replace non-ASCII with ASCII version else strip.
*/
public static function translit($txt)
{
return iconv('utf-8', 'us-ascii//TRANSLIT', $txt);
}
}
class Sanitize
{
use SanitizeStrippers;
/**
* Strip off non-matching characters
*/
public static function strip($txt, array $rules)
{
// Validation on test-env
// TODO: Skip on live?
if (count($rules) === 0) {
user_error("DevErr: No rules given to Sanitize");
}
if (is_array($txt) || is_object($txt)) {
user_error("DevErr: txt invalid");
}
$sane = $txt;
foreach ($rules as $rule) {
$sane = preg_replace(self::$rule(), '', $sane);
}
return $sane;
}
/**
* Convert given string into generic key
*/
public static function key($txt)
{
return strtoupper(self::strip($txt, ["chars"]));
}
/**
* Remove/Replace risky XSS-characters from the string
*/
public static function xss($txt)
{
return htmlspecialchars(strip_tags($txt));
}
}