00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010 class Validation {
00011
00012
00013
00014 public static function is_email($value) {
00015 $parts = explode('@', $value, 2);
00016 $local_part = array_shift($parts);
00017 $domain = array_shift($parts);
00018
00019 $ret = self::is_domain($domain);
00020
00021 $ret = $ret && (strlen($local_part) <= 64);
00022
00023
00024
00025
00026 $ret = $ret && (substr($local_part, 0, 1) !== '.');
00027 $ret = $ret && (substr($local_part, -1) !== '.');
00028
00029
00030 $pattern = "@^[a-zA-Z0-9!#$%&'*+\-/=?^_`{|}~.]+$@s";
00031 $ret = $ret && preg_match($pattern, strtr($local_part, "\r\n", ' '));
00032
00033 return $ret;
00034 }
00035
00036
00037
00038
00039 public static function is_domain($value) {
00040 $ret = true;
00041 $ret = $ret && (strlen($value) <= 255);
00042
00043 $elements = explode('.', $value);
00044 $ret = $ret && (count($elements) > 1);
00045
00046
00047 foreach($elements as $e) {
00048 $l = strlen($e);
00049 $ret = $ret && ($l > 0);
00050 $ret = $ret && ($l <= 63);
00051 $ret = $ret && preg_match('|^[^@ ]+$|', $e);
00052 }
00053
00054
00055 $tld = array_pop($elements);
00056 $ret = $ret && !preg_match('|^\d+$|', $tld);
00057
00058 return $ret;
00059 }
00060
00061
00062
00063
00064
00065
00066 public static function is_string_of_length($value, $min_length = 0, $max_length = -1) {
00067 if (is_null($value)) {
00068 $value = '';
00069 }
00070 $ret = is_string($value);
00071 if ($ret) {
00072 $ret = $ret && ( String::length($value) >= $min_length);
00073 if ($max_length > 0) {
00074 $ret = $ret && ( String::length($value) <= $max_length);
00075 }
00076 }
00077 return $ret;
00078 }
00079
00080
00081
00082
00083 public static function is_url($value) {
00084 $test = new Url($value);
00085 return $test->is_valid();
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095 public static function is_double($value, $min = false, $max = false) {
00096 $ret = is_numeric($value);
00097 if ($ret) {
00098 $value = Cast::float($value);
00099 if ($min !== false) {
00100 $ret = $ret && ($value >= $min);
00101 }
00102 if ($max !== false) {
00103 $ret = $ret && ($value <= $max);
00104 }
00105 }
00106 return $ret;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 public static function is_int($value, $min = false, $max = false) {
00118 $ret = ($value == strval(intval($value)));
00119 if ($ret) {
00120 $value = Cast::int($value);
00121 if ($min !== false) {
00122 $ret = $ret && ($value >= $min);
00123 }
00124 if ($max !== false) {
00125 $ret = $ret && ($value <= $max);
00126 }
00127 }
00128 return $ret;
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 public static function is_ip4(&$ip) {
00144
00145
00146 $test = explode('.', $ip);
00147 $ints = array();
00148 $ret = (count($test) == 4);
00149 foreach($test as $datum) {
00150 $ret = $ret && self::is_int($datum, 0, 255);
00151 if ($ret) { $ints[] = intval($datum); }
00152 }
00153
00154 $regex = '@^[0.]*$@';
00155 $ret = $ret && !preg_match($regex, $ip);
00156
00157 if ($ret) {
00158 $ip = implode('.', $ints);
00159 }
00160
00161 return $ret;
00162 }
00163
00164
00165
00166
00167 public static function is_ip6($ip) {
00168 $ret = false;
00169 if (function_exists('filter_var')) {
00170
00171 $ret = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
00172 }
00173 else {
00174
00175
00176 $regex = "@^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){1,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){1}:([0-9A-Fa-f]{1,4}:){0,4}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,2}:([0-9A-Fa-f]{1,4}:){0,3}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,3}:([0-9A-Fa-f]{1,4}:){0,2}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,4}:([0-9A-Fa-f]{1,4}:){1}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$@";
00177 $ret = preg_match($regex, $ip);
00178 }
00179 if ($ret) {
00180
00181 $regex = '@^[0.:]*$@';
00182 $ret = !preg_match($regex, $ip);
00183 }
00184 return $ret;
00185 }
00186
00187
00188
00189
00190 public static function is_ip(&$ip) {
00191 return self::is_ip4($ip) || self::is_ip6($ip);
00192 }
00193
00194 }