Repository navigation
Expand file tree
/
Copy pathSunFilter.php
More file actions
132 lines (124 loc) · 3.93 KB
/
Copy pathSunFilter.php
File metadata and controls
132 lines (124 loc) · 3.93 KB
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
<?php
/**
* SunFilter Class
*
* @category Filtering (Sanitization & Validation)
* @package SunFilter
* @author Mehmet Selcuk Batal <batalms@gmail.com>
* @copyright Copyright (c) 2021, Sunhill Technology <www.sunhillint.com>
* @license https://opensource.org/licenses/lgpl-3.0.html The GNU Lesser General Public License, version 3.0
* @link https://github.com/msbatal/PHP-Data-Security-Class
* @version 1.4.2
*/
class SunFilter
{
/**
* String that holds Sanitized/Validated value
* @var string
*/
private $result;
/**
* Set exception handler
*/
public function __construct() {
}
/**
* Sanitize data by type
*
* @param string $data
* @param string $type
* @throws exception
* @return object
*/
public function sanitize($data = null, $type = null) {
/*
if (empty($type) || empty($data)) {
throw new Exception('Missing parameter for sanitization.');
}
*/
$this->result = null;
switch ($type) {
case 'string':
if (version_compare(phpversion(), '8.1.0', '<')) {
$this->result = filter_var($data, FILTER_SANITIZE_STRING); //deprecated as of PHP 8.1.0
} else {
$this->result = htmlspecialchars(strip_tags($data)); //use htmlspecialchars() and strip_tags instead
}
break;
case 'float':
$this->result = (float) filter_var($data, FILTER_SANITIZE_NUMBER_FLOAT);
break;
case 'integer':
$this->result = (int) filter_var($data, FILTER_SANITIZE_NUMBER_INT);
break;
case 'url':
$this->result = filter_var($data, FILTER_SANITIZE_URL);
break;
case 'email':
$this->result = filter_var($data, FILTER_SANITIZE_EMAIL);
break;
case 'special':
$this->result = filter_var($data, FILTER_SANITIZE_SPECIAL_CHARS);
break;
default:
$this->result = null;
break;
}
return $this;
}
/**
* Validate data by type
*
* @param string $data
* @param string $type
* @throws exception
* @return object
*/
public function validate($data = null, $type = null) {
/*
if (empty($type) || empty($data)) {
throw new Exception('Missing parameter for validation.');
}
*/
$this->result = false;
switch ($type) {
case 'boolean':
$this->result = filter_var($data, FILTER_VALIDATE_BOOLEAN) == true ? true : false;
break;
case 'float':
$this->result = filter_var($data, FILTER_VALIDATE_FLOAT) == true ? true : false;
break;
case 'integer':
$this->result = filter_var($data, FILTER_VALIDATE_INT) == true ? true : false;
break;
case 'email':
$this->result = filter_var($data, FILTER_VALIDATE_EMAIL) == true ? true : false;
break;
case 'url':
$this->result = filter_var($data, FILTER_VALIDATE_URL) == true ? true : false;
break;
case 'domain':
$this->result = filter_var($data, FILTER_VALIDATE_DOMAIN) == true ? true : false;
break;
case 'ip':
$this->result = filter_var($data, FILTER_VALIDATE_IP) == true ? true : false;
break;
case 'mac':
$this->result = filter_var($data, FILTER_VALIDATE_MAC) == true ? true : false;
break;
default:
$this->result = false;
break;
}
return $this;
}
/**
* Return the Sanitized/Validated data
*
* @return string
*/
public function result() {
return $this->result;
}
}
?>