Location: PHPKode > scripts > Mix2ool > mix2ool/framework/mixValidator.php
<?php

class mixValidator{
	public function validateEmail($email){
		return preg_match("/^[a-z0-9][a-z0-9-_.]*@[a-z0-9][a-z0-9-_]*\.[a-z0-9-_.]*[a-z]$/i", $email);
	}
	public function validateURL($url){
		return preg_match("/(https|http|ftp|rtsp|mms)?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?/i", $url);
	}
	public function urlExist($url){
		return (@fopen($url, "r") !== FALSE);
	}
	public function hostExist($host, $port = 80, $timeout = 5){
		return (@ fsockopen($host, $port, $errno, $err, $timeout) !== FALSE);
	}
	public function validateIP($ip){
		if (preg_match("/[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}/", $ip)){
			$ips = explode(".", $ip);
			if ($ips[0] < 0 && $ips[0] > 255)
				return FALSE;
			if ($ips[1] < 0 && $ips[1] > 255)
				return FALSE;
			if ($ips[2] < 0 && $ips[2] > 255)
				return FALSE;
			if ($ips[3] < 0 && $ips[3] > 255)
				return FALSE;
			return TRUE;
		}else
			return FALSE;
	}
	public function validateNotEmpty($str){
		return (strlen($str) > 0);
	}
	public function validateLength($str, $min = FALSE, $max = FALSE){
		$a = strlen($str);
		if ($min === FALSE)
			$min = $a - 1;
		if ($max === FALSE)
			$max = $a + 1;
		return ($a >= $min && $a <= $max);
	}
	public function validateRegex($str, $regex){
		return (preg_match($regex, $str) > 0);
	}
	public function validateArray($v, $min = FALSE, $max = FALSE){
		if (! is_array($v))
			return FALSE;
		
		$a = count($v);
		if ($min === FALSE)
			$min = $a - 1;
		if ($max === FALSE)
			$max = $a + 1;
		
		return ($a >= $min && $a <= $max);
	}
	public function validateArrayUnique($v){
		return count(array_unique($v)) == count($v);
	}
	public function validateOne($v, $validator){
		if (is_string($validator))
			$validator = array($validator);
		for ($i = 0; $i < count($validator); $i++){
			$validator[$i] = trim($validator[$i]);
			if (strpos($validator[$i], "notEmpty") !== false){
				if (! mixValidator::validateNotEmpty($v))
					return false;
			}
			if (strpos($validator[$i], "length") !== false){
				$pattern = "/length\[(.*)\]/";
				preg_match($pattern, $validator[$i], $match);
				$range = explode("-", $match[1]);
				if ($range[0] == "u")
					$range[0] = FALSE;
				if ($range[1] == "u")
					$range[1] = FALSE;
				if (! mixValidator::validateLength($v, $range[0], $range[1]))
					return false;
			}
			if (strpos($validator[$i], "unique") !== false){
				if (! mixValidator::validateArrayUnique($v))
					return false;
			}
			if (strpos($validator[$i], "array") !== false){
				if ($validator[$i] == "array"){
					if (! mixValidator::validateArray($v))
						return FALSE;
					continue;
				}
				$pattern = "/array\[(.*)\]/";
				preg_match($pattern, $validator[$i], $match);
				$range = explode("-", $match[1]);
				if ($range[0] == "u")
					$range[0] = FALSE;
				if ($range[1] == "u")
					$range[1] = FALSE;
				if (! mixValidator::validateArray($v, $range[0], $range[1]))
					return false;
			}
			if (strpos($validator[$i], "regex") !== false){
				$pattern = substr($validator[$i], 6, -1);
				if (! mixValidator::validateRegex($v, $pattern))
					return false;
			}
			if (strpos($validator[$i], "email") !== false){
				if (! mixValidator::validateEmail($v))
					return false;
			}
			if (strpos($validator[$i], "ip") !== false){
				if (! mixValidator::validateIP($v))
					return false;
			}
			if (strpos($validator[$i], "url") !== false){
				if (! mixValidator::validateURL($v))
					return false;
			}
		}
		return true;
	}
}

?>
Return current item: Mix2ool