-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanitize.php
76 lines (66 loc) · 1.75 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
<?php
/**
* Classe que contem os métodos que iram
* filtrar as entradas enviadas via GET e POST
*
* @filesource
* @author Pedro Elsner <pedro.elsner@gmail.com>
* @license http://creativecommons.org/licenses/by/3.0/br/ Creative Commons 3.0
* @abstract
* @version 1.0
*/
abstract class Sanitize {
/**
* Filter
*
* @param mixed $value
* @param array $modes
* @return mixed
* @static
* @since 1.0
*/
static public function filter($value, $modes = array('sql', 'html')) {
if (!is_array($modes)) {
$modes = array($modes);
}
if (is_string($value)) {
foreach ($modes as $type) {
$value = self::_doFilter($value, $type);
}
return $value;
}
foreach ($value as $key => $toSanatize) {
if (is_array($toSanatize)) {
$value[$key]= self::filter($toSanatize, $modes);
} else {
foreach ($modes as $type) {
$value[$key] = self::_doFilter($toSanatize, $type);
}
}
}
return $value;
}
/**
* DoFilter
*
* @param mixed $value
* @param array $modes
* @return mixed
* @static
* @since 1.0
*/
static protected function _doFilter($value, $mode) {
switch ($mode) {
case 'html':
$value = strip_tags($value);
$value = addslashes($value);
$value = htmlspecialchars($value);
break;
case 'sql':
$value = preg_replace(sql_regcase('/(from|select|insert|delete|where|drop table|show tables|#|\*| |\\\\)/'),'',$value);
$value = trim($value);
break;
}
return $value;
}
}