-
Notifications
You must be signed in to change notification settings - Fork 1
/
sanitize-input-helper.php
183 lines (173 loc) · 7.09 KB
/
sanitize-input-helper.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
class Sanitize_input{
/**
*
* Created by: [email protected]
* Modified on: 2017/11/27 16:44:35
*
*
* == Following types of sanitizations
*
* coordinates
* money
* id
* string
* html
* session
* alphanumeric
* password
* mobile
* date
* email
* datetime
* html_array
* multiple_select
*
*
* If values are not filtered or validated then "NULL" is returned
* else validated and filtered input is returned
*
*
*/
public function __construct(){
}
private static function filters($var_type){
$input_filters = array(
'coordinates' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
$input = preg_replace("#[^0-9\.]#",'',$input);
$input = (float) sprintf("%.12f", $input);
return $input;
}
),
'money' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
$input = preg_replace("#[^0-9\.]#",'',$input);
$input = (float) sprintf("%.2f", $input);
return $input;
}
),
'id' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
$input = preg_replace("#[^0-9\.]#",'',$input);
return $input ? (int) $input : 0 ;
}
),
'string' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
$input = filter_var($input, FILTER_SANITIZE_STRING);
return $input;
}
),
'html' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
$input = htmlentities($input);
return str_replace('\\\\', '\\',$input);
// return htmlentities(stripslashes($input)); //htmlspecialchars_decode();
}
),
'session' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
return preg_replace("#[^0-9-]#",'',$input);
}
),
'alphanumeric' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
return preg_replace("#[^0-9a-z_]#i",'',$input);
}
),
'password' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
return $input;
// return md5($input);
// password_verify($input, PASSWORD_BCRYPT);
// return password_hash($input, PASSWORD_BCRYPT, ["cost" => 10]);
}
),
'mobile' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
$input = preg_replace("#[^0-9]#",'',$input);
$input = strlen($input) === 10 ? $input : NULL;
return $input;
}
),
'date' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
// $input = preg_replace('/(\d{1,4})([^\d]{0,})(\d{1,2})([^\d]{0,})(\d{1,2})/',"$1-$3-$5", $input);
preg_match("/(\d{1,4})([^\d]{0,})(\d{1,2})([^\d]{0,})(\d{1,2})/", $input, $input_date);
$input = sprintf("%04d-%02d-%02d", $input_date[1], $input_date[3], $input_date[5]);
$format = 'Y-m-d';
$d = @DateTime::createFromFormat($format, $input);
// var_dump($d);
if($d && $d->format($format) == $input){
return $input;
}
return NULL;
}
),
'email' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
$input = filter_var($input, FILTER_SANITIZE_EMAIL);
if(filter_var($input, FILTER_VALIDATE_EMAIL)){
return $input;
}
return NULL;
}
),
'datetime' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($input) {
if(substr_count($input ,"00") > 1) return false;
return preg_replace("#[^0-9a-z\:\,\'\-\.\/]#i",'',$input);
}
),
'html_array' => array(
'filter' => FILTER_CALLBACK,
'flags' => FILTER_FORCE_ARRAY,
'options' => function ($input) {
$input = htmlentities($input);
return str_replace('\\\\', '\\',$input);
// $filtered = htmlentities(stripslashes($input)) ;
return $filtered ? $filtered: "";
}
),
'multiple_select' => array(
'filter' => FILTER_SANITIZE_STRING,
'flags' => FILTER_FORCE_ARRAY,
),
);
$form_filter = array(
'driverId'=> $input_filters['id'],
'rideId'=> $input_filters['id'],
'passengerId'=> $input_filters['id'],
);
// var_dump($var_type);
if(is_associative($var_type)){
foreach ($var_type as $key => $type){
// var_dump($key , $type);
if(in_array($type, array_keys($input_filters)) && preg_match("/[a-z]/i", $key)){
$form_filter = array_merge($form_filter, [$key => $input_filters[$type]]);
}
}
}
return $form_filter;
}
public static function process($data_array = [], $var_type = []){
$form_filter = self::filters($var_type);
// var_dump($form_filter);
// var_dump($form_filter);
$filter = filter_var_array($data_array, $form_filter, true);
// var_dump($data_array);
return $filter;
}
}