]> 91.132.146.200 Git - dolphin.git/commitdiff
improved validate functions
authorBanana <banana@starscream.de>
Wed, 11 Jan 2012 14:13:15 +0000 (15:13 +0100)
committerBanana <banana@starscream.de>
Wed, 11 Jan 2012 14:13:15 +0000 (15:13 +0100)
simple-framework/lib/function.library.php
single-functions/validate-input.php

index de16f7086f223e3d830c1cbd1fa69884cd8c8279..a516eaa774083e1fd9cff99073cef1921cb1d525 100644 (file)
 
 
 /**
- * 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;
                }
        }
 
index f0a9831bde2bce8277a15997e9c8e00819e8f121..21ddfd95243bc2d7903a681e7ae733e4720698f0 100644 (file)
  */
 
 /**
- * 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;
 }
 ?>