-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSanitizer.php
executable file
·166 lines (111 loc) · 4.74 KB
/
Sanitizer.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
<?php
class Sanitizer
{
// *************
// Verification Methods
// *************
public function verifyPostalCode($postalcode, $country)
{
$regexPostalCode = '#[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}([-| |]?){1}[0-9]{1}[a-zA-Z]{1}[0-9]{1}#';
$regexZipCodeUS = '#[0-9]{5}([- /]?[0-9]{4})?#';
if (preg_match($regexPostalCode, $postalcode) || $matchesZIP = preg_match($regexZipCodeUS, $postalcode)) {
if (isset($matchesZIP) && $matchesZIP == true) {
if ($country == "Canada") {
return false;
}
}
// Postal or ZIP Code is fine
return $this->sanPC($postalcode);
} else {
$sanPC = $this->sanPC($postalcode);
// Check again to see if the postal code now matches after being sanitized.
if (preg_match($regexPostalCode, $sanPC) || preg_match($regexZipCodeUS, $sanPC)) {
return $sanPC;
} // If it still doesn't match, return false. It won't be used to query for more data
else {
return FALSE;
}
}
}
// Strip all delimiting chars, only keep numbers
public function verifyAddress($address)
{
$addressValRegex = array();
$addressValRegex['address'] = '~^[0-9th]+[\-]?[0-9th]*[ ]*([a-zA-Z0-9\-\.]*[ ]?)*([0-9]*[ ]?)*$~';
$addressValRegex['postalcode'] = '~([a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}([-| |]?){1}[0-9]{1}[a-zA-Z]{1}[0-9]{1})|([0-9]{5}([- /]?[0-9]{4})?)~';
$addressValRegex['postofficebox'] = '~(([\s|\.|,]+p[\s|\.|,]+| post[\s|\.]*)(o[\s|\.|,]*| office[\s|\.]*))|(box[.|\s]*\d+)~i';
$addressValRegex['invalidchars'] = '~[^a-zA-Z0-9#]~';
switch ($address) {
case (preg_match($addressValRegex['postofficebox'], $address) ? true : false):
return false;
default:
if (preg_match($addressValRegex['invalidchars'], $address)) {
$sanitizedAddress = $this->sanAddress($address);
// Now that the address has been sanitized, check the format again.
if (preg_match($addressValRegex['address'], $sanitizedAddress)) {
// After being sanitized, the address is now in the proper format
// Return the now clean address
return $sanitizedAddress;
}
} else {
return false;
}
}
}
public function isPostalCode($postalcode)
{
// Check to see if the postal code is a postal code.
if (preg_match('~([a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}([-| |]?){1}[0-9]{1}[a-zA-Z]{1}[0-9]{1})|([0-9]{5}([- /]?[0-9]{4})?)~', $postalcode)) {
// Postal code is a postal code so return that value
return $postalcode;
} else {
// Is not postal code. Return false;
return false;
}
}
public function verifyPhoneNumber($phone)
{
/* Matches US phone number format. 1 in the beginning is optional,
area code is required, spaces or dashes can be used as optional divider between number groups.
Also alphanumeric format is allowed after area code.*/
$regexPhoneVal = '~([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})~';
// Compare to regex
if (preg_match($regexPhoneVal, $phone)) {
$this->sanPC($phone);
return TRUE;
} else {
return FALSE;
}
}
// *************
// Sanitize Methods
// *************
private function sanPC($pc)
{
$regexPCfilter = '#[^0-9a-zA-Z]#';
$pc = preg_replace($regexPCfilter, '', $pc);
$pc = strtolower($pc);
return $pc;
}
public function sanPhone($phonenumber)
{
$regexPhoneChars = '/\D/u';
// Check for invalid characters
if (preg_match($regexPhoneChars, $phonenumber, $matches)) {
// Strip non decimal characters
$address = preg_replace($regexPhoneChars, '', $phonenumber);
}
// Return appropriate response
return $regexPhoneChars;
}
public function sanAddress($address)
{
// Strip Everything but letters, numbers, and octothorp
$addressRegex['invalidchars'] = '~[^a-zA-Z0-9#]~';
$addressRegex['morespaces'] = "~[ ]{2,}~";
// Find and replace based on the invalid chars regular expression
$sanitizedAddress = preg_replace($addressRegex['invalidchars'], " ", $address);
$evenMoreSanitizedAddress = preg_replace($addressRegex['morespaces'], " ", $sanitizedAddress);
return $evenMoreSanitizedAddress;
}
}