-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.class.php
284 lines (251 loc) · 7.38 KB
/
Utility.class.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
class Utility {
/**
* List of all known HTTP response codes - used to
* translate numeric codes to messages.
*
* @var array
*/
protected static $messages = array(
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',
// Client Error 4xx
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
// Server Error 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
);
/**
* List of all acceptable HTTP response codes - used to
* check the passed error code is handled by the class
*
* @var array
*/
public static $codes = array(
// 100
100
, 101
// 200
, 200
, 201
, 202
, 203
, 204
, 205
, 206
// 300
, 300
, 301
, 302
, 303
, 304
, 305
, 307
// 400
, 400
, 401
, 402
, 403
, 404
, 405
, 406
, 410
// 500
, 500
, 501
, 502
, 503
, 509
);
/**
* Validate an Email Address
*
* @param $email
* @return mixed - $email if valid, FALSE if not
*/
public static function validateEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
/**
* Validate an IP Address and make sure that it is not in any private or reserved ranges
*
* @param $ip
* @return mixed - $ip if valid, FALSE if not
*/
public static function validateIP($ip) {
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
}
/**
* Validate an CIDR Range and make sure that it is not in any private or reserved ranges
*
* @param $cidr
* @return boolean
*/
public static function validateCIDR($cidr) {
// check that we are actually dealing with a CIDR here
if (!self::isCIDR($cidr)) {
return false;
}
// Get the base and the bits from the CIDR
list($base, $bits) = explode('/', $cidr);
list($low, $high) = self::getCidrRange($cidr, true);
// CIDR base must start at the low end of the range
if (ip2long($base) != $low) {
return false;
} else {
return true;
}
}
/**
* Check to see if a string is a valid CIDR
*
* @param cidr the string to check
* @return bool
*/
public static function isCIDR($cidr) {
$cidr_regex = '/^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]))\/([1-9]|[1-2]\d|3[0-2])$/';
$matches = array();
if (preg_match($cidr_regex, $cidr, $matches)) {
if ($matches[1] && $matches[6]) {
return (bool) self::validateIP($matches[1]);
}
}
return false;
}
/**
* Break a CIDR into its high and low ranges
*
* @param $cidr string the cidr to get the low and high ranges for
* @param $int bool return the results as integers (true) or strings (false)
* @return array an array of ($low, $high)
*/
public static function getCidrRange($cidr, $int = true) {
// Get the base and the bits from the CIDR
list($base, $bits) = explode('/', $cidr);
// Now split it up into it's classes
list($a, $b, $c, $d) = explode('.', $base);
// Now do some bit shifting/switching to convert to ints
$i = ($a << 24) + ($b << 16) + ($c << 8) + $d;
$mask = ($bits == 0) ? 0 : (~0 << (32 - $bits));
// Here's our lowest int
$low = $i & $mask;
// Here's our highest int
$high = $i | (~$mask & 0xFFFFFFFF);
if ($int) {
return array($low, $high);
} else {
return array(long2ip($low), long2ip($high));
}
}
/**
* Redirect the user's browser to another page
*
* @param $uri
*/
public static function redirect($uri) {
header('Location: ' . $uri);
exit();
}
/**
* Escapes HTML in a string
*
* @param string $string
* @return string
*/
public static function escapeHTML($string) {
return filter_var($string, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
}
/**
* Sanitize An Email
*
* @param string $email
* @return string
*/
public static function sanitizeEmail($email) {
return filter_var($email, FILTER_SANITIZE_EMAIL);
}
/**
* Send an HTTP Status Code Header
*
* @param $code - the http code you want to send
* @return bool - if the status code was sent
*/
public static function HTTPStatus($code = 200) {
if (in_array($code, self::$codes)) {
header($_SERVER['SERVER_PROTOCOL'] .' '. $code .' '. self::$messages[$code], true, $code);
return true;
}
return false;
}
/**
* Generate a random string of characters
*
* @param $length - the number of random characters to return
* @return string - the random string of characters
*/
public static function RandomString($length = 16) {
$string = '';
//create confirmation id, 16 chars long
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$len = strlen($chars) - 1;
$salt = '';
for ($p = 0; $p < $length; $p++) {
$string .= $chars[mt_rand(0, $len)];
}
return $string;
}
/**
* humanFilesize method
*
* Turn a byte filesize into a human readable fileszie
*
* @param int bytes the bytes we are formatting
* @param int decimals the number of decimals we want to return
* @return string
*/
public static function humanFilesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
}