class_mcValidator.inc.php
6.23 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
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
<?php
/**
* @filesource class_mcValidator.inc.php
*
* @category losp
* @copyright Copyright by mensch.coop e.G. 2011
* @mailto dev [at] mensch.coop
* @link http://mensch.coop
*/
class mcValidator {
const VALIDATOR_TYPE_EMAIL = 'email';
const VALIDATOR_TYPE_URL = 'url';
const VALIDATOR_TYPE_SINGELTONNAME = 'singeltonName';
const VALIDATOR_TYPE_PHONE = 'phone';
const VALIDATOR_TYPE_GERPLZ = 'gerPlz';
const VALIDATOR_TYPE_YOUTUBEID = 'youtubeID';
const VALIDATOR_TYPE_VIMEOID = 'vimeoID';
const VALIDATOR_TYPE_PASSWORD = 'password';
private static $regExpArray = array(
self::VALIDATOR_TYPE_URL => array(
'regExp' => '(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?',
'match' => true
),
self::VALIDATOR_TYPE_SINGELTONNAME => array(
'regExp' => '[^A-Za-z0-9\-]',
'match' => false
),
self::VALIDATOR_TYPE_PHONE => array(
'regExp' => '[^-\s\d.\(\)\/+]',
'match' => false
),
self::VALIDATOR_TYPE_GERPLZ => array(
'regExp' => '\d{5}$',
'match' => true
),
self::VALIDATOR_TYPE_YOUTUBEID => array(
'regExp' => '[^A-Za-z0-9\-\_]',
'match' => false
),
self::VALIDATOR_TYPE_VIMEOID => array(
'regExp' => '[\D]',
'match' => false
),
self::VALIDATOR_TYPE_PASSWORD => array(
'regExp' => '^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}',
'match' => true
)
);
public static function getRegExp($param_type){
if(array_key_exists($param_type,self::$regExpArray)){
return self::$regExpArray[$param_type];
}else{
return null;
}
}
/**
* Attention: function only checks value exists if $param_type is not const of VALIDATOR_TYPE_*
*
* @param String $param_value
* @param String $param_type - the regexpression-type which should be used
* @param boolean $param_allowNoValue - default false, allow blank value
* @param boolean $param_utf8 - default true, $param_value format is utf8
*
* @return boolean
*/
public static function checkValue($param_value, $param_type, $param_allowNoValue = false, $param_utf8 = true){
if($param_type == self::VALIDATOR_TYPE_EMAIL){
return self::checkEmail($param_value, $param_allowNoValue);
}
$var_value = trim($param_value);
if(!isset($var_value) || is_null($var_value) || empty($var_value) || $var_value === '') {
return $param_allowNoValue;
}
else{
$var_regExp = '';
$var_match = true;
if($param_type){
switch($param_type){
case self::VALIDATOR_TYPE_EMAIL : return self::checkEmail($param_value, $param_allowNoValue);
default :
if(array_key_exists($param_type,self::$regExpArray)){
$var_regExp = '/'+self::$regExpArray[$param_type]['regExp']+'/';
$var_match = self::$regExpArray[$param_type]['match'];
}
else{
return true;
}
break;
}
}
if($param_utf8){
$var_regExp +='u';
}
$tmp_return = preg_match($param_value,$var_regExp);
if(!$var_match){
return !$tmp_return;
}else{
return $tmp_return;
}
}
return true;
}
/**
* checks if a String is a valid email address
*
* @param $param_email
* @param boolean $param_allowNoValue - default false, allow blank value
* @return boolean
*/
public static function checkEmail($param_email,$param_allowNoValue = false) {
function valid_dot_pos($email) {
$str_len = strlen($email);
for($i=0; $i<$str_len; $i++) {
$current_element = $email[$i];
if($current_element == "." && ($email[$i+1] == ".")) {
return false;
break;
}
else {
}
}
return true;
}
function valid_local_part($local_part) {
if(preg_match("/[^a-zA-Z0-9-_ß@.!#$%&'*\/+=?^`{\|}~]/",$local_part)) {
return false;
}
else {
return true;
}
}
function valid_domain_part($domain_part) {
if(preg_match("/[^a-zA-Z0-9-_ß@#\[\].]/", $domain_part)) {
return false;
}
elseif(preg_match("/[@]/", $domain_part) && preg_match("/[#]/", $domain_part)) {
return false;
}
elseif(preg_match("/[\[]/", $domain_part) || preg_match("/[\]]/", $domain_part)) {
$dot_pos = strrpos($domain_part, ".");
if(($dot_pos < strrpos($domain_part, "]")) || (strrpos($domain_part, "]") < strrpos($domain_part, "["))) {
return true;
}
elseif(preg_match("/[^0-9.]/", $domain_part)) {
return false;
}
else {
return false;
}
}
else {
return true;
}
}
// trim() the entered E-Mail
$str_trimmed = trim($param_email);
// find the @ position
$at_pos = strrpos($str_trimmed, "@");
// find the . position
$dot_pos = strrpos($str_trimmed, ".");
// this will cut the local part and return it in $local_part
$local_part = substr($str_trimmed, 0, $at_pos);
// this will cut the domain part and return it in $domain_part
$domain_part = substr($str_trimmed, $at_pos);
if(!isset($str_trimmed) || is_null($str_trimmed) || empty($str_trimmed) || $str_trimmed == "") {
return $param_allowNoValue;
}
elseif(!valid_local_part($local_part)) {
return false;
}
elseif(!valid_domain_part($domain_part)) {
return false;
}
elseif($at_pos > $dot_pos) {
return false;
}
elseif(!valid_local_part($local_part)) {
return false;
}
elseif(($str_trimmed[$at_pos + 1]) == ".") {
return false;
}
elseif(!preg_match("/[(@)]/", $str_trimmed) || !preg_match("/[(.)]/", $str_trimmed)) {
return false;
}
else {
return true;
}
}
}
?>