Blame view

class_mcValidator.inc.php 6.23 KB
meier committed
1 2 3 4 5 6 7 8 9 10 11 12 13
<?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';
14
	const VALIDATOR_TYPE_PHONE = 'phone';
meier committed
15 16 17
	const VALIDATOR_TYPE_GERPLZ = 'gerPlz';
	const VALIDATOR_TYPE_YOUTUBEID = 'youtubeID';
	const VALIDATOR_TYPE_VIMEOID = 'vimeoID';
18
	const VALIDATOR_TYPE_PASSWORD = 'password';
meier committed
19 20 21 22
	
	
	private static $regExpArray = array(
		self::VALIDATOR_TYPE_URL => array(
23
			'regExp' => '(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?',
meier committed
24 25 26
			'match' => true
		),
		self::VALIDATOR_TYPE_SINGELTONNAME => array(
27
			'regExp' => '[^A-Za-z0-9\-]',
meier committed
28 29 30
			'match' => false
		),
		self::VALIDATOR_TYPE_PHONE => array(
31
			'regExp' => '[^-\s\d.\(\)\/+]',
meier committed
32 33 34
			'match' => false
		),
		self::VALIDATOR_TYPE_GERPLZ => array(
35
			'regExp' => '\d{5}$',
meier committed
36 37 38
			'match' => true
		),
		self::VALIDATOR_TYPE_YOUTUBEID => array(
39
			'regExp' => '[^A-Za-z0-9\-\_]',
meier committed
40 41 42
			'match' => false
		),
		self::VALIDATOR_TYPE_VIMEOID => array(
43
			'regExp' => '[\D]',
meier committed
44
			'match' => false
45 46 47 48
		),
		self::VALIDATOR_TYPE_PASSWORD => array(
			'regExp' => '^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}',
			'match' => true
meier committed
49
		)
50
			
meier committed
51
	);
52 53 54 55 56 57 58 59
	
	public static function getRegExp($param_type){
		if(array_key_exists($param_type,self::$regExpArray)){
			return self::$regExpArray[$param_type];
		}else{
			return null;
		}
	}
meier committed
60
	/**
61
	 * Attention: function only checks value exists if $param_type is not const of VALIDATOR_TYPE_*
meier committed
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
	 *  
	 * @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)){
89
        					$var_regExp = '/'+self::$regExpArray[$param_type]['regExp']+'/';
meier committed
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
        					$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;
        }
	}
}
?>