class_mcValidator.inc.php 6.23 KB
<?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;
        }
	}
}
?>