-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFilter.php
153 lines (144 loc) · 4.29 KB
/
Filter.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
<?php
/**
@file
@brief Input Data Filter
@package Radix
*/
namespace Edoceo\Radix;
class Filter
{
/**
@param $x the email address data
@param $d default if not valid
@return normalized/validated email, false on failure
@todo want to use the imap_parser and have optional array return or out var
*/
static function email($x,$d=false)
{
// Match Email Address Pattern
$x = strtolower(trim($x));
if (preg_match('/^([\w\.\%\+\-]+[a-z0-9])@([a-z0-9][a-z0-9\.\-]*[a-z0-9]\.[a-z]{2,6})$/',$x,$m)) {
// More strict is to only check MX, we do both in example
if ( (checkdnsrr($m[2],'MX') == true) || (checkdnsrr($m[2],'A') == true) ) {
return sprintf('%s@%s',$m[1],$m[2]);
}
}
return false;
}
/**
Extracts and returns match(es)
@param $pat the pattern of characters to accept
@param $val the value to filter
@param $def default
@return sanatized value
*/
static function match($pat,$val,$def=false)
{
$ret = $def;
if (substr($pat,0,1)!='/') {
$pat = '/(' . $pat . ')/i';
}
if (preg_match($pat,$val,$m)) {
array_shift($m);
if (count($m)==1) {
$ret = $m[0]; // Single value
} else {
$ret = $m; // Array w/o full match
}
}
return $ret;
}
/**
Returns a String with only the specified characters
@param $pat the pattern of characters to accept
@param $val the value to filter
*/
static function only($pat,$val)
{
if (substr($pat,0,1)!='/') {
$pat = '/[^' . $pat . ']/i';
}
$ret = preg_replace($pat,null,$val);
return $ret;
}
/**
@param $val string to read
@param $pat pattern to match to, use ()
@param $def default value if no match
@return captured string from regex | array of matches | null
*/
static function regex($val,$pat,$def=null)
{
$ret = $def;
if (preg_match($pat,$val,$m)) {
switch (count($m)) {
case 0:
// Ignore
break;
case 1: // Never Happens
case 2:
$ret = $m[1];
break;
default:
// array_shift($m);
$ret = $m;
}
}
return $ret;
}
/**
Makes a stub from the text
@param $t input text
@param $s seperator, default '_'
*/
static function stub($t,$s='_')
{
$t = preg_replace('/[^a-z0-9\-' . preg_quote($s) . ' ]+/ims',null,trim($t)); // non-safe chars => null
// Radix::dump("1:$t");
$t = preg_replace('/[^a-z0-9]+/ims',$s,$t); // non-word => $s
// Radix::dump("2:$t");
// $t = preg_replace('/\b([a-z0-9])' . preg_quote($s) . '/ims','$1',$t);
// Radix::dump("r:$t");
$t = trim($t);
$t = strtolower($t);
return $t;
}
/**
@see http://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid
@return normalized URI, false on failure
*/
static function uri($uri,$def=false)
{
if (!preg_match('/^([\w\-]{2,8}):\/\//',$uri)) {
$uri = "http://$uri";
}
$buf = parse_url($uri);
if (empty($buf['scheme'])) $buf['scheme'] = 'http';
if (empty($buf['host'])) {
if (preg_match('/^[\w\.]+$/',$buf['path'])) {
$buf['host'] = $buf['path'];
$buf['path'] = '/';
}
}
// Here and empty means not valid, give null
if (empty($buf['host'])) {
return $def;
}
if (empty($buf['path'])) {
$buf['path'] = '/';
}
// Fix double slash
$buf['path'] = preg_replace('/\/+/','/',$buf['path']);
// Base
$uri = sprintf('%s://%s%s',strtolower($buf['scheme']),strtolower($buf['host']),$buf['path']);
// Query String?
if (!empty($buf['query'])) {
$uri.= '?' . $buf['query'];
}
// Fragment?
if (!empty($buf['fragment'])) {
$uri.= '#' . $buf['fragment'];
}
return $uri;
}
}