1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: class Validate {
13: public static function number($number)
14: {
15: $number = trim($number);
16:
17: if ($number === '0'
18: or $number === 0) {
19: return (float) $number;
20: }
21:
22: $number = ltrim($number, '0');
23: $number = str_replace(array(',', ' ', chr(160), chr(9)), array('.', '', '', ''), $number);
24: if (strpos($number, '.') == TRUE) {
25: $number = trim($number, '0');
26: }
27:
28: $number_tmp = (float) $number;
29:
30: if ((string) $number_tmp !== (string) $number) {
31: return FALSE;
32: }
33:
34: return $number_tmp;
35: }
36:
37: public static function integer($number) {
38: $number = trim($number);
39:
40: if ($number === '0'
41: or $number === 0) {
42: return (int) $number;
43: }
44:
45: $number = ltrim($number, '0');
46: $number = str_replace(array(',', ' ', chr(160), chr(9)), array('.', '', '', ''), $number);
47: if (strpos($number, '.') == TRUE) {
48: $number = trim($number, '0');
49: }
50:
51: $number_tmp = (int) $number;
52:
53: if ((string) $number_tmp !== (string) $number) {
54: return FALSE;
55: }
56:
57: return $number_tmp;
58: }
59:
60: public static function alpha($string)
61: {
62: if (preg_match('/[^a-zA-Z]/', $string) == TRUE) {
63: return FALSE;
64: }
65:
66: return $string;
67: }
68:
69: public static function email($value)
70: {
71: $value = trim($value);
72:
73:
74: $atom = '[-a-z0-9!#$%&\'*+\/=?\^_`{|}~]';
75: $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])';
76: if (preg_match("/^$atom+(\\.$atom+)*@($domain?\\.)+$domain\$/", $value) == FALSE) {
77: return FALSE;
78: }
79:
80: return $value;
81: }
82:
83: public static function url($url)
84: {
85: $url = trim($url);
86: if (preg_match('/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i', $url) === FALSE) {
87: return FALSE;
88: }
89:
90: return $url;
91: }
92:
93: public static function date($date, $convert = TRUE)
94: {
95: $date = trim($date);
96: $date = str_replace(' ', '', $date);
97: $date = str_replace(chr(160), '', $date);
98:
99: if (strpos($date, '-') !== FALSE) {
100: $date_tmp = strtotime($date);
101: $date_tmp = date('Y-m-d', $date_tmp);
102:
103: if ($date_tmp !== $date) {
104: return FALSE;
105: }
106:
107: } else {
108: if (preg_match('~^([1-9]|19|[12][0-8]|29(?=\\.([^2]|2\\.(([02468][048]|[13579][26])00|[0-9]{2}(0[48]|[2468][048]|[13579][26]))))|30(?=\\.[^2])|31(?=\\.([13578][02]?\\.)))\\.([1-9]|1[012])\\.[0-9]{4}$~D', $value) == FALSE) {
109: return FALSE;
110: }
111:
112: if ($convert === TRUE) {
113: $date = explode('.', $date);
114: $date = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
115: $date = date('Y-m-d', $date);
116: }
117: }
118:
119: return $date;
120: }
121:
122: public static function range($value, $min, $max)
123: {
124: $value = (float) $value;
125: $min = (float) $min;
126: $max = (float) $max;
127:
128: if ($value < $min
129: OR $value > $max) {
130:
131: return FALSE;
132: }
133:
134: return $value;
135: }
136:
137: public static function min_size($value, $min)
138: {
139: $value = (float) $value;
140: $min = (float) $min;
141:
142: if ($value < $min) {
143: return FALSE;
144: }
145:
146: return $value;
147: }
148:
149: public static function max_size($value, $max)
150: {
151: $value = (float) $value;
152: $max = (float) $max;
153:
154: if ($value > $ax) {
155: return FALSE;
156: }
157:
158: return $value;
159: }
160:
161: public static function min_length($value, $min)
162: {
163: $value = (string) $value;
164: $min = (int) $min;
165:
166: if (strlen($value) < $min) {
167: return FALSE;
168: }
169:
170: return $value;
171: }
172:
173: public static function max_length($value, $max)
174: {
175: $value = (string) $value;
176: $max = (int) $max;
177:
178: if (strlen($value) > $max) {
179: return FALSE;
180: }
181:
182: return $value;
183: }
184:
185: public static function equal_length($value, $length)
186: {
187: $value = (string) $value;
188: $length = (int) $length;
189:
190: if (strlen($value) !== $length) {
191: return FALSE;
192: }
193:
194: return $value;
195: }
196:
197: public static function is_empty($value)
198: {
199: if (is_scalar($value) === TRUE) {
200: if ($value == '0') {
201: return FALSE;
202: }
203: }
204:
205: return empty($value);
206: }
207:
208: public static function regexp($value, $regexp)
209: {
210: if (preg_match($regexp, $value) === FALSE) {
211: return FALSE;
212: }
213:
214: return $value;
215: }
216:
217: public static function ip($ip)
218: {
219: $ip = trim($ip);
220:
221: if (empty($ip) == FALSE
222: AND ip2long($ip) <> -1) {
223:
224: $reserved_ips = array (
225: array('0.0.0.0','2.255.255.255'),
226: array('10.0.0.0','10.255.255.255'),
227: array('127.0.0.0','127.255.255.255'),
228: array('169.254.0.0','169.254.255.255'),
229: array('172.16.0.0','172.31.255.255'),
230: array('192.0.2.0','192.0.2.255'),
231: array('192.168.0.0','192.168.255.255'),
232: array('255.255.255.0','255.255.255.255')
233: );
234:
235: foreach ($reserved_ips as $r) {
236: $min = ip2long($r[0]);
237: $max = ip2long($r[1]);
238: if (ip2long($ip) >= $min
239: AND ip2long($ip) <= $max) {
240:
241: return FALSE;
242: }
243: }
244:
245: return $ip;
246:
247: } else {
248: return false;
249:
250: }
251: }
252: }
253: