This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitize.php
467 lines (427 loc) · 8.96 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/**
* Sanitizing.
*
* Functions for sanitizing data.
*
* @package blobfolio/common
* @author Blobfolio, LLC <[email protected]>
*/
namespace blobfolio\common;
class sanitize {
/**
* Strip Accents
*
* @param string $str String.
* @return string String.
*/
public static function accents($str) {
ref\sanitize::accents($str);
return $str;
}
/**
* Attribute Value
*
* This will decode entities, strip control characters, and trim
* outside whitespace.
*
* Note: this should not be used for safe insertion into HTML. For
* that, use the html() function.
*
* @param string $str String.
* @return string String.
*/
public static function attribute_value($str='') {
ref\sanitize::attribute_value($str);
return $str;
}
/**
* CA Postal Code
*
* @param string $str Postal Code.
* @return string Postal Code.
*/
public static function ca_postal_code($str) {
ref\sanitize::ca_postal_code($str);
return $str;
}
/**
* Credit Card
*
* @param string $ccnum Card number.
* @return string|bool Card number or false.
*/
public static function cc($ccnum) {
ref\sanitize::cc($ccnum);
return $ccnum;
}
/**
* Control Characters
*
* @param string $str String.
* @return string String.
*/
public static function control_characters($str) {
ref\sanitize::control_characters($str);
return $str;
}
/**
* Country
*
* @param string $str Country.
* @return string ISO country code.
*/
public static function country($str) {
ref\sanitize::country($str);
return $str;
}
/**
* CSV Cell Data
*
* @param string $str String.
* @return string String.
*/
public static function csv($str='') {
ref\sanitize::csv($str);
return $str;
}
/**
* Datetime
*
* @param string|int $str Date or timestamp.
* @return string Date.
*/
public static function datetime($str) {
ref\sanitize::datetime($str);
return $str;
}
/**
* Date
*
* @param string|int $str Date or timestamp.
* @return string Date.
*/
public static function date($str) {
ref\sanitize::date($str);
return $str;
}
/**
* Domain Name.
*
* This locates the domain name portion of a URL,
* removes leading "www." subdomains, and ignores
* IP addresses.
*
* @param string $str Domain.
* @param bool $unicode Unicode.
* @return string Domain.
*/
public static function domain($str='', bool $unicode=false) {
ref\sanitize::domain($str, $unicode);
return $str;
}
/**
* EAN13
*
* Almost exactly like UPC, but not quite.
*
* @param string $str String.
* @param bool $formatted Formatted.
* @return string String.
*/
public static function ean($str, bool $formatted=false) {
ref\sanitize::ean($str, $formatted);
return $str;
}
/**
* Email
*
* Converts the email to lowercase, strips
* invalid characters, quotes, and apostrophes.
*
* @param string $str Email.
* @return string Email.
*/
public static function email($str) {
ref\sanitize::email($str);
return $str;
}
/**
* File Extension
*
* @param string $str Extension.
* @return string Extension.
*/
public static function file_extension($str) {
ref\sanitize::file_extension($str);
return $str;
}
/**
* Hostname
*
* @param string $domain Hostname.
* @param bool $www Keep leading www.
* @param bool $unicode Unicode.
* @return string|bool Hostname or false.
*/
public static function hostname($domain, bool $www=false, bool $unicode=false) {
ref\sanitize::hostname($domain, $www, $unicode);
return $domain;
}
/**
* HTML
*
* @param string $str HTML.
* @return string HTML.
*/
public static function html($str=null) {
ref\sanitize::html($str);
return $str;
}
/**
* IP Address
*
* @param string $str IP.
* @param bool $restricted Allow private/restricted values.
* @param bool $condense Condense IPv6.
* @return string IP.
*/
public static function ip($str='', bool $restricted=false, bool $condense=true) {
ref\sanitize::ip($str, $restricted, $condense);
return $str;
}
/**
* IRI Value
*
* @param string $str IRI value.
* @param array $protocols Allowed protocols.
* @param array $domains Allowed domains.
* @return string String.
*/
public static function iri_value($str='', $protocols=null, $domains=null) {
ref\sanitize::iri_value($str, $protocols, $domains);
return $str;
}
/**
* ISBN
*
* Validate an ISBN 10 or 13.
*
* @param string $str String.
* @return bool True/false.
*/
public static function isbn($str) {
ref\sanitize::isbn($str);
return $str;
}
/**
* JS Variable
*
* @param string $str String.
* @param string $quote Quote type.
* @return string String.
*/
public static function js($str='', $quote="'") {
ref\sanitize::js($str, $quote);
return $str;
}
/**
* IANA MIME Type
*
* @param string $str MIME.
* @return string MIME.
*/
public static function mime($str) {
ref\sanitize::mime($str);
return $str;
}
/**
* (Person's) Name
*
* A bit of a fool's goal, but this will attempt to
* strip out obviously bad data and convert to title
* casing.
*
* @param string $str Name.
* @return string Name.
*/
public static function name($str='') {
ref\sanitize::name($str);
return $str;
}
/**
* Password
*
* This simply removes excess whitespace and
* non-printable characters, which are likely
* only present because of user error.
*
* @param string $str Password.
* @return string Password.
*/
public static function password($str='') {
ref\sanitize::password($str);
return $str;
}
/**
* Printable
*
* Remove non-printable characters (except spaces).
*
* @param string $str String.
* @return string String.
*/
public static function printable($str) {
ref\sanitize::printable($str);
return $str;
}
/**
* Canadian Province
*
* @param string $str Province.
* @return string Province.
*/
public static function province($str) {
ref\sanitize::province($str);
return $str;
}
/**
* Quotes
*
* Replace those damn curly quotes with the straight
* ones Athena intended!
*
* @param string $str String.
* @return string String.
*/
public static function quotes($str) {
ref\sanitize::quotes($str);
return $str;
}
/**
* US State/Territory
*
* @param string $str State.
* @return string State.
*/
public static function state($str) {
ref\sanitize::state($str);
return $str;
}
/**
* Australian State/Territory
*
* @param string $str State.
* @return string State.
*/
public static function au_state($str) {
ref\sanitize::au_state($str);
return $str;
}
/**
* SVG
*
* @param string $str SVG code.
* @param array $tags Additional whitelist tags.
* @param array $attr Additional whitelist attributes.
* @param array $protocols Additional whitelist protocols.
* @param array $domains Additional whitelist domains.
* @return string SVG code.
*/
public static function svg($str='', $tags=null, $attr=null, $protocols=null, $domains=null) {
ref\sanitize::svg($str, $tags, $attr, $protocols, $domains);
return $str;
}
/**
* Timezone
*
* @param string $str Timezone.
* @return string Timezone or UTC on failure.
*/
public static function timezone($str='') {
ref\sanitize::timezone($str);
return $str;
}
/**
* Confine a Value to a Range
*
* @param mixed $value Value.
* @param mixed $min Min.
* @param mixed $max Max.
* @return mixed Value.
*/
public static function to_range($value, $min=null, $max=null) {
ref\sanitize::to_range($value, $min, $max);
return $value;
}
/**
* UPC
*
* @param string $str String.
* @param bool $formatted Formatted.
* @return string String.
*/
public static function upc($str, bool $formatted=false) {
ref\sanitize::upc($str, $formatted);
return $str;
}
/**
* URL
*
* Validate URLishness and convert // schemas.
*
* @param string $str URL.
* @return string URL.
*/
public static function url($str) {
ref\sanitize::url($str);
return $str;
}
/**
* UTF-8
*
* Ensure string contains valid UTF-8 encoding.
*
* @param string $str String.
* @return string String.
*/
public static function utf8($str) {
ref\sanitize::utf8($str);
return $str;
}
/**
* Whitespace
*
* Trim edges, replace all consecutive horizontal whitespace
* with a single space, and constrict consecutive newlines.
*
* @param string $str String.
* @param int $newlines Consecutive newlines allowed.
* @return string String.
*/
public static function whitespace($str='', int $newlines=0) {
ref\sanitize::whitespace($str, $newlines);
return $str;
}
/**
* Whitespace Multiline
*
* @param string $str String.
* @param int $newlines Consecutive newlines allowed.
* @return string String.
*/
public static function whitespace_multiline($str='', int $newlines=1) {
ref\sanitize::whitespace_multiline($str, $newlines);
return $str;
}
/**
* US ZIP5
*
* @param string $str ZIP Code.
* @return string ZIP Code.
*/
public static function zip5($str='') {
ref\sanitize::zip5($str);
return $str;
}
}