From: Banana Date: Wed, 11 Jan 2012 14:13:15 +0000 (+0100) Subject: improved validate functions X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=c9f734d4e83a7e890597168cb6eb06b552dcdd4d;p=dolphin.git improved validate functions --- diff --git a/simple-framework/lib/function.library.php b/simple-framework/lib/function.library.php index de16f70..a516eaa 100644 --- a/simple-framework/lib/function.library.php +++ b/simple-framework/lib/function.library.php @@ -13,37 +13,76 @@ /** - * validate given string if it is in given format + * validate the given string with the given type. Optional check the string + * length + * + * @param string $input The string to check + * @param string $mode How the string should be checked + * @param mixed $limit If int given the string is checked for length + * + * @see http://de.php.net/manual/en/regexp.reference.unicode.php + * http://www.sql-und-xml.de/unicode-database/#pc + * + * the pattern replaces all that is allowed. the correct result after + * the replace should be empty, otherwise are there chars which are not + * allowed * - * @param string $str - * @param string $mode - * @return bool */ -function validateString($str,$mode='alnum') { + function validate($input,$mode='text',$limit=false) { + // check if we have input + $input = trim($input); + + if($input == "") return false; + $ret = false; - if(!empty($str)) { - $check = ''; - switch($mode) { - case 'alnumWhitespace': - $check = preg_replace('/[^\p{L}\p{N}\p{P}\s]/u','',$str); - if($str === $check) { - $ret = true; - } - break; - - case 'digit': - $check = preg_replace('/[\p{^N}]/u','',$str); - if($str === $check) { - $ret = true; - } - break; - - case 'alnum': - default: - $check = preg_replace('/[^\p{L}\p{N}\p{P}]/u','',$str); - if($str === $check) { - $ret = true; - } + + switch ($mode) { + case 'mail': + return self::check_email_address($input); + break; + + case 'url': + return filter_var($input,FILTER_VALIDATE_URL); + break; + + case 'nospace': + // text without any whitespace and special chars + $pattern = '/[\p{L}\p{N}]/u'; + break; + + case 'nospaceP': + // text without any whitespace and special chars + // but with Punctuation + $pattern = '/[\p{L}\p{N}\p{Po}]/u'; + break; + + case 'digit': + // only numbers and digit + $pattern = '/[\p{Nd}]/'; + break; + + case 'pageTitle': + // text with whitespace and without special chars + // but with Punctuation + $pattern = '/[\p{L}\p{N}\p{Po}\p{Z}\s]/u'; + break; + + case 'text': + default: + $pattern = '/[\p{L}\p{N}\p{P}\p{S}\p{Z}\p{M}\s]/u'; + } + + $value = preg_replace($pattern, '', $input); + #if($input === $value) { + if($value === "") { + $ret = true; + } + + if(!empty($limit)) { + # isset starts with 0 + if(isset($input[$limit])) { + # too long + $ret = false; } } diff --git a/single-functions/validate-input.php b/single-functions/validate-input.php index f0a9831..21ddfd9 100644 --- a/single-functions/validate-input.php +++ b/single-functions/validate-input.php @@ -12,39 +12,79 @@ */ /** - * validate if given string is correct - * this can easily expanded with more match patterns + * validate the given string with the given type. Optional check the string + * length + * + * @param string $input The string to check + * @param string $mode How the string should be checked + * @param mixed $limit If int given the string is checked for length + * + * @see http://de.php.net/manual/en/regexp.reference.unicode.php + * http://www.sql-und-xml.de/unicode-database/#pc + * + * the pattern replaces all that is allowed. the correct result after + * the replace should be empty, otherwise are there chars which are not + * allowed * - * @param string $string - * @param string $mode */ - function validateInput($string,$mode) { + function validate($input,$mode='text',$limit=false) { + // check if we have input + $input = trim($input); + + if($input == "") return false; + $ret = false; - if(!empty($string) && !empty($mode)) { - switch ($mode) { - case 'nospace': - $pattern = '/[^\p{L}\p{N}\p{P}]/u'; - $value = preg_replace($pattern, '', $string); - if($string === $value) { - $ret = true; - } - break; - case 'digit': - $pattern = '/[^\p{N}]/u'; - $value = preg_replace($pattern, '', $string); - if($string === $value) { - $ret = true; - } - break; - case 'text': - $pattern = '/[^\p{L}\p{N}\p{P}]/u'; - $value = preg_replace($pattern, '', $string); - if($string === $value) { - $ret = true; - } - break; + + switch ($mode) { + case 'mail': + return self::check_email_address($input); + break; + + case 'url': + return filter_var($input,FILTER_VALIDATE_URL); + break; + + case 'nospace': + // text without any whitespace and special chars + $pattern = '/[\p{L}\p{N}]/u'; + break; + + case 'nospaceP': + // text without any whitespace and special chars + // but with Punctuation + $pattern = '/[\p{L}\p{N}\p{Po}]/u'; + break; + + case 'digit': + // only numbers and digit + $pattern = '/[\p{Nd}]/'; + break; + + case 'pageTitle': + // text with whitespace and without special chars + // but with Punctuation + $pattern = '/[\p{L}\p{N}\p{Po}\p{Z}\s]/u'; + break; + + case 'text': + default: + $pattern = '/[\p{L}\p{N}\p{P}\p{S}\p{Z}\p{M}\s]/u'; + } + + $value = preg_replace($pattern, '', $input); + #if($input === $value) { + if($value === "") { + $ret = true; + } + + if(!empty($limit)) { + # isset starts with 0 + if(isset($input[$limit])) { + # too long + $ret = false; } - } - return $ret; + } + + return $ret; } ?>