Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: validation.php 8004 2009-01-16 20:15:21Z gwoo $ */
3
/**
4
 * Validation Class.  Used for validation of model data
5
 *
6
 * Long description for file
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
11
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
19
 * @package       cake
20
 * @subpackage    cake.cake.libs
21
 * @since         CakePHP(tm) v 1.2.0.3830
22
 * @version       $Revision: 8004 $
23
 * @modifiedby    $LastChangedBy: gwoo $
24
 * @lastmodified  $Date: 2009-01-16 12:15:21 -0800 (Fri, 16 Jan 2009) $
25
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
26
 */
27
/**
28
 * Deprecated
29
 */
30
/**
31
 * Not empty.
32
 */
33
	define('VALID_NOT_EMPTY', '/.+/');
34
/**
35
 * Numbers [0-9] only.
36
 */
37
	define('VALID_NUMBER', '/^[-+]?\\b[0-9]*\\.?[0-9]+\\b$/');
38
/**
39
 * A valid email address.
40
 */
41
	define('VALID_EMAIL', "/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2,4}|museum|travel)$/i");
42
/**
43
 * A valid year (1000-2999).
44
 */
45
	define('VALID_YEAR', '/^[12][0-9]{3}$/');
46
/**
47
 * Offers different validation methods.
48
 *
49
 * Long description for file
50
 *
51
 * @package       cake
52
 * @subpackage    cake.cake.libs
53
 * @since         CakePHP v 1.2.0.3830
54
 */
55
class Validation extends Object {
56
/**
57
 * Set the the value of methods $check param.
58
 *
59
 * @var string
60
 * @access public
61
 */
62
	var $check = null;
63
/**
64
 * Set to a valid regular expression in the class methods.
65
 * Can be set from $regex param also
66
 *
67
 * @var string
68
 * @access public
69
 */
70
	var $regex = null;
71
/**
72
 * Some complex patterns needed in multiple places
73
 *
74
 * @var array
75
 * @access private
76
 */
77
	var $__pattern = array(
78
		'ip' => '(?:(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])',
79
		'hostname' => '(?:[a-z0-9][-a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,4}|museum|travel)'
80
	);
81
/**
82
 * Some class methods use a country to determine proper validation.
83
 * This can be passed to methods in the $country param
84
 *
85
 * @var string
86
 * @access public
87
 */
88
	var $country = null;
89
/**
90
 * Some class methods use a deeper validation when set to true
91
 *
92
 * @var string
93
 * @access public
94
 */
95
	var $deep = null;
96
/**
97
 * Some class methods use the $type param to determine which validation to perfom in the method
98
 *
99
 * @var string
100
 * @access public
101
 */
102
	var $type = null;
103
/**
104
 * Holds an array of errors messages set in this class.
105
 * These are used for debugging purposes
106
 *
107
 * @var array
108
 * @access public
109
 */
110
	var $errors = array();
111
/**
112
 * Gets a reference to the Validation object instance
113
 *
114
 * @return object Validation instance
115
 * @access public
116
 * @static
117
 */
118
	function &getInstance() {
119
		static $instance = array();
120
 
121
		if (!$instance) {
122
			$instance[0] =& new Validation();
123
		}
124
		return $instance[0];
125
	}
126
/**
127
 * Checks that a string contains something other than whitespace
128
 *
129
 * Returns true if string contains something other than whitespace
130
 *
131
 * $check can be passed as an array:
132
 * array('check' => 'valueToCheck');
133
 *
134
 * @param mixed $check Value to check
135
 * @return boolean Success
136
 * @access public
137
 */
138
	function notEmpty($check) {
139
		$_this =& Validation::getInstance();
140
		$_this->__reset();
141
		$_this->check = $check;
142
 
143
		if (is_array($check)) {
144
			$_this->_extract($check);
145
		}
146
 
147
		if (empty($_this->check) && $_this->check != '0') {
148
			return false;
149
		}
150
		$_this->regex = '/[^\s]+/m';
151
		return $_this->_check();
152
	}
153
/**
154
 * Checks that a string contains only integer or letters
155
 *
156
 * Returns true if string contains only integer or letters
157
 *
158
 * $check can be passed as an array:
159
 * array('check' => 'valueToCheck');
160
 *
161
 * @param mixed $check Value to check
162
 * @return boolean Success
163
 * @access public
164
 */
165
	function alphaNumeric($check) {
166
		$_this =& Validation::getInstance();
167
		$_this->__reset();
168
		$_this->check = $check;
169
 
170
		if (is_array($check)) {
171
			$_this->_extract($check);
172
		}
173
 
174
		if (empty($_this->check) && $_this->check != '0') {
175
			return false;
176
		}
177
		$_this->regex = '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/mu';
178
		return $_this->_check();
179
	}
180
/**
181
 * Checks that a string length is within s specified range.
182
 * Spaces are included in the character count.
183
 * Returns true is string matches value min, max, or between min and max,
184
 *
185
 * @param string $check Value to check for length
186
 * @param integer $min Minimum value in range (inclusive)
187
 * @param integer $max Maximum value in range (inclusive)
188
 * @return boolean Success
189
 * @access public
190
 */
191
	function between($check, $min, $max) {
192
		$length = strlen($check);
193
		return ($length >= $min && $length <= $max);
194
	}
195
/**
196
 * Returns true if field is left blank -OR- only whitespace characters are present in it's value
197
 * Whitespace characters include Space, Tab, Carriage Return, Newline
198
 *
199
 * $check can be passed as an array:
200
 * array('check' => 'valueToCheck');
201
 *
202
 * @param mixed $check Value to check
203
 * @return boolean Success
204
 * @access public
205
 */
206
	function blank($check) {
207
		$_this =& Validation::getInstance();
208
		$_this->__reset();
209
		$_this->check = $check;
210
 
211
		if (is_array($check)) {
212
			$_this->_extract($check);
213
		}
214
 
215
		$_this->regex = '/[^\\s]/';
216
		return !$_this->_check();
217
	}
218
/**
219
 * Validation of credit card numbers.
220
 * Returns true if $check is in the proper credit card format.
221
 *
222
 * @param mixed $check credit card number to validate
223
 * @param mixed $type 'all' may be passed as a sting, defaults to fast which checks format of most major credit cards
224
 * 							if an array is used only the values of the array are checked.
225
 * 							Example: array('amex', 'bankcard', 'maestro')
226
 * @param boolean $deep set to true this will check the Luhn algorithm of the credit card.
227
 * @param string $regex A custom regex can also be passed, this will be used instead of the defined regex values
228
 * @return boolean Success
229
 * @access public
230
 * @see Validation::_luhn()
231
 */
232
	function cc($check, $type = 'fast', $deep = false, $regex = null) {
233
		$_this =& Validation::getInstance();
234
		$_this->__reset();
235
		$_this->check = $check;
236
		$_this->type = $type;
237
		$_this->deep = $deep;
238
		$_this->regex = $regex;
239
 
240
		if (is_array($check)) {
241
			$_this->_extract($check);
242
		}
243
		$_this->check = str_replace(array('-', ' '), '', $_this->check);
244
 
245
		if (strlen($_this->check) < 13) {
246
			return false;
247
		}
248
 
249
		if (!is_null($_this->regex)) {
250
			if ($_this->_check()) {
251
				return $_this->_luhn();
252
			}
253
		}
254
		$cards = array('all' => array('amex' => '/^3[4|7]\\d{13}$/',
255
									'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/',
256
									'diners'   => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/',
257
									'disc'     => '/^(?:6011|650\\d)\\d{12}$/',
258
									'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/',
259
									'enroute'  => '/^2(?:014|149)\\d{11}$/',
260
									'jcb'      => '/^(3\\d{4}|2100|1800)\\d{11}$/',
261
									'maestro'  => '/^(?:5020|6\\d{3})\\d{12}$/',
262
									'mc'       => '/^5[1-5]\\d{14}$/',
263
									'solo'     => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/',
264
									'switch'   => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4][0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/',
265
									'visa'     => '/^4\\d{12}(\\d{3})?$/',
266
									'voyager'  => '/^8699[0-9]{11}$/'),
267
							'fast'   => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/');
268
 
269
		if (is_array($_this->type)) {
270
			foreach ($_this->type as $value) {
271
				$_this->regex = $cards['all'][strtolower($value)];
272
 
273
				if ($_this->_check()) {
274
					return $_this->_luhn();
275
				}
276
			}
277
		} elseif ($_this->type == 'all') {
278
			foreach ($cards['all'] as $value) {
279
				$_this->regex = $value;
280
 
281
				if ($_this->_check()) {
282
					return $_this->_luhn();
283
				}
284
			}
285
		} else {
286
			$_this->regex = $cards['fast'];
287
 
288
			if ($_this->_check()) {
289
				return $_this->_luhn();
290
			}
291
		}
292
	}
293
/**
294
 * Used to compare 2 numeric values.
295
 *
296
 * @param mixed $check1 if string is passed for a string must also be passed for $check2
297
 * 							used as an array it must be passed as array('check1' => value, 'operator' => 'value', 'check2' -> value)
298
 * @param string $operator Can be either a word or operand
299
 * 								is greater >, is less <, greater or equal >=
300
 * 								less or equal <=, is less <, equal to ==, not equal !=
301
 * @param integer $check2 only needed if $check1 is a string
302
 * @return boolean Success
303
 * @access public
304
 */
305
	function comparison($check1, $operator = null, $check2 = null) {
306
		if (is_array($check1)) {
307
			extract($check1, EXTR_OVERWRITE);
308
		}
309
		$operator = str_replace(array(' ', "\t", "\n", "\r", "\0", "\x0B"), '', strtolower($operator));
310
 
311
		switch ($operator) {
312
			case 'isgreater':
313
			case '>':
314
				if ($check1 > $check2) {
315
					return true;
316
				}
317
				break;
318
			case 'isless':
319
			case '<':
320
				if ($check1 < $check2) {
321
					return true;
322
				}
323
				break;
324
			case 'greaterorequal':
325
			case '>=':
326
				if ($check1 >= $check2) {
327
					return true;
328
				}
329
				break;
330
			case 'lessorequal':
331
			case '<=':
332
				if ($check1 <= $check2) {
333
					return true;
334
				}
335
				break;
336
			case 'equalto':
337
			case '==':
338
				if ($check1 == $check2) {
339
					return true;
340
				}
341
				break;
342
			case 'notequal':
343
			case '!=':
344
				if ($check1 != $check2) {
345
					return true;
346
				}
347
				break;
348
			default:
349
				$_this =& Validation::getInstance();
350
				$_this->errors[] = __('You must define the $operator parameter for Validation::comparison()', true);
351
				break;
352
		}
353
		return false;
354
	}
355
/**
356
 * Used when a custom regular expression is needed.
357
 *
358
 * @param mixed $check When used as a string, $regex must also be a valid regular expression.
359
 *								As and array: array('check' => value, 'regex' => 'valid regular expression')
360
 * @param string $regex If $check is passed as a string, $regex must also be set to valid regular expression
361
 * @return boolean Success
362
 * @access public
363
 */
364
	function custom($check, $regex = null) {
365
		$_this =& Validation::getInstance();
366
		$_this->__reset();
367
		$_this->check = $check;
368
		$_this->regex = $regex;
369
		if (is_array($check)) {
370
			$_this->_extract($check);
371
		}
372
		if ($_this->regex === null) {
373
			$_this->errors[] = __('You must define a regular expression for Validation::custom()', true);
374
			return false;
375
		}
376
		return $_this->_check();
377
	}
378
/**
379
 * Date validation, determines if the string passed is a valid date.
380
 * keys that expect full month, day and year will validate leap years
381
 *
382
 * @param string $check a valid date string
383
 * @param mixed $format Use a string or an array of the keys below. Arrays should be passed as array('dmy', 'mdy', etc)
384
 * 					Keys: dmy 27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slash
385
 * 							mdy 12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash
386
 * 							ymd 2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash
387
 * 							dMy 27 December 2006 or 27 Dec 2006
388
 * 							Mdy December 27, 2006 or Dec 27, 2006 comma is optional
389
 * 							My December 2006 or Dec 2006
390
 * 							my 12/2006 separators can be a space, period, dash, forward slash
391
 * @param string $regex If a custom regular expression is used this is the only validation that will occur.
392
 * @return boolean Success
393
 * @access public
394
 */
395
	function date($check, $format = 'ymd', $regex = null) {
396
		$_this =& Validation::getInstance();
397
		$_this->__reset();
398
		$_this->check = $check;
399
		$_this->regex = $regex;
400
 
401
		if (!is_null($_this->regex)) {
402
			return $_this->_check();
403
		}
404
 
405
		$regex['dmy'] = '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.|\\x20)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.|\\x20)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\x20)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%';
406
		$regex['mdy'] = '%^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.|\\x20)31)\\1|(?:(?:0?[13-9]|1[0-2])(\\/|-|\\.|\\x20)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:0?2(\\/|-|\\.|\\x20)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.|\\x20)(?:0?[1-9]|1\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%';
407
		$regex['ymd'] = '%^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.|\\x20)(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.|\\x20)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]))))$%';
408
		$regex['dMy'] = '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ (((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]\\d)\\d{2})$/';
409
		$regex['Mdy'] = '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/';
410
		$regex['My'] = '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)[ /]((1[6-9]|[2-9]\\d)\\d{2})$%';
411
		$regex['my'] = '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))))$%';
412
 
413
		$format = (is_array($format)) ? array_values($format) : array($format);
414
		foreach ($format as $key) {
415
			$_this->regex = $regex[$key];
416
 
417
			if ($_this->_check() === true) {
418
				return true;
419
			}
420
		}
421
		return false;
422
	}
423
 
424
/**
425
 * Time validation, determines if the string passed is a valid time.
426
 * Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m)
427
 * Does not allow/validate seconds.
428
 *
429
 * @param string $check a valid time string
430
 * @return boolean Success
431
 * @access public
432
 */
433
 
434
	function time($check) {
435
		$_this =& Validation::getInstance();
436
		$_this->__reset();
437
		$_this->check = $check;
438
		$_this->regex = '%^((0?[1-9]|1[012])(:[0-5]\d){0,2}([AP]M|[ap]m))$|^([01]\d|2[0-3])(:[0-5]\d){0,2}$%';
439
		return $_this->_check();
440
	}
441
 
442
/**
443
 * Boolean validation, determines if value passed is a boolean integer or true/false.
444
 *
445
 * @param string $check a valid boolean
446
 * @return boolean Success
447
 * @access public
448
 */
449
	function boolean($check) {
450
		$booleanList = array(0, 1, '0', '1', true, false);
451
		return in_array($check, $booleanList, true);
452
	}
453
 
454
/**
455
 * Checks that a value is a valid decimal. If $places is null, the $check is allowed to be a scientific float
456
 * If no decimal point is found a false will be returned. Both the sign and exponent are optional.
457
 *
458
 * @param integer $check The value the test for decimal
459
 * @param integer $places if set $check value must have exactly $places after the decimal point
460
 * @param string $regex If a custom regular expression is used this is the only validation that will occur.
461
 * @return boolean Success
462
 * @access public
463
 */
464
	function decimal($check, $places = null, $regex = null) {
465
		$_this =& Validation::getInstance();
466
		$_this->__reset();
467
		$_this->regex = $regex;
468
		$_this->check = $check;
469
 
470
		if (is_null($_this->regex)) {
471
			if (is_null($places)) {
472
				$_this->regex = '/^[-+]?[0-9]*\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?$/';
473
			} else {
474
				$_this->regex = '/^[-+]?[0-9]*\\.{1}[0-9]{'.$places.'}$/';
475
			}
476
		}
477
		return $_this->_check();
478
	}
479
/**
480
 * Validates for an email address.
481
 *
482
 * @param string $check Value to check
483
 * @param boolean $deep Perform a deeper validation (if true), by also checking availability of host
484
 * @param string $regex Regex to use (if none it will use built in regex)
485
 * @return boolean Success
486
 * @access public
487
 */
488
	function email($check, $deep = false, $regex = null) {
489
		$_this =& Validation::getInstance();
490
		$_this->__reset();
491
		$_this->check = $check;
492
		$_this->regex = $regex;
493
		$_this->deep = $deep;
494
 
495
		if (is_array($check)) {
496
			$_this->_extract($check);
497
		}
498
 
499
		if (is_null($_this->regex)) {
500
			$_this->regex = '/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@' . $_this->__pattern['hostname'] . '$/i';
501
		}
502
		$return = $_this->_check();
503
 
504
		if ($_this->deep === false || $_this->deep === null) {
505
			return $return;
506
		}
507
 
508
		if ($return === true && preg_match('/@(' . $_this->__pattern['hostname'] . ')$/i', $_this->check, $regs)) {
509
			$host = gethostbynamel($regs[1]);
510
			return is_array($host);
511
		}
512
		return false;
513
	}
514
/**
515
 * Check that value is exactly $comparedTo.
516
 *
517
 * @param mixed $check Value to check
518
 * @param mixed $comparedTo Value to compare
519
 * @return boolean Success
520
 * @access public
521
 */
522
	function equalTo($check, $comparedTo) {
523
		return ($check === $comparedTo);
524
	}
525
/**
526
 * Check that value has a valid file extension.
527
 *
528
 * @param mixed $check Value to check
529
 * @param array $extensions file extenstions to allow
530
 * @return boolean Success
531
 * @access public
532
 */
533
	function extension($check, $extensions = array('gif', 'jpeg', 'png', 'jpg')) {
534
		if (is_array($check)) {
535
			return Validation::extension(array_shift($check), $extensions);
536
		}
537
		$extension = strtolower(array_pop(explode('.', $check)));
538
		foreach ($extensions as $value) {
539
			if ($extension == strtolower($value)) {
540
				return true;
541
			}
542
		}
543
		return false;
544
	}
545
/**
546
 * Check that value is a file name
547
 *
548
 * @param mixed $check Value to check
549
 * @access public
550
 * @todo finish implementation
551
 */
552
	function file($check) {
553
		// if (is_array($check)) {
554
		// 	foreach ($check as $value) {
555
		// 		if (!Validation::file($value)) {
556
		// 			return false;
557
		// 		}
558
		// 	}
559
		// 	return true;
560
		// }
561
		//
562
		// return preg_match('/[\w| |_]+\.[\w]+/', $check);
563
	}
564
/**
565
 * Validation of an IPv4 address.
566
 *
567
 * @param string $check The string to test.
568
 * @return boolean Success
569
 * @access public
570
 */
571
	function ip($check) {
572
		$_this =& Validation::getInstance();
573
		$_this->check = $check;
574
		$_this->regex = '/^' . $_this->__pattern['ip'] . '$/';
575
		return $_this->_check();
576
	}
577
/**
578
 * Checks whether the length of a string is greater or equal to a minimal length.
579
 *
580
 * @param string $check The string to test
581
 * @param integer $min The minimal string length
582
 * @return boolean Success
583
 * @access public
584
 */
585
	function minLength($check, $min) {
586
		$length = strlen($check);
587
		return ($length >= $min);
588
	}
589
/**
590
 * Checks whether the length of a string is smaller or equal to a maximal length..
591
 *
592
 * @param string $check The string to test
593
 * @param integer $max The maximal string length
594
 * @return boolean Success
595
 * @access public
596
 */
597
	function maxLength($check, $max) {
598
		$length = strlen($check);
599
		return ($length <= $max);
600
	}
601
/**
602
 * Checks that a value is a monetary amount.
603
 *
604
 * @param string $check Value to check
605
 * @param string $symbolPosition Where symbol is located (left/right)
606
 * @return boolean Success
607
 * @access public
608
 */
609
	function money($check, $symbolPosition = 'left') {
610
		$_this =& Validation::getInstance();
611
		$_this->check = $check;
612
 
613
		if ($symbolPosition == 'right') {
614
			$_this->regex = '/^(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?(?<!\x{00a2})\p{Sc}?$/u';
615
		} else {
616
			$_this->regex = '/^(?!\x{00a2})\p{Sc}?(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?$/u';
617
		}
618
		return $_this->_check();
619
	}
620
/**
621
 * Validate a multiple select.
622
 *
623
 * @param mixed $check Value to check
624
 * @param mixed $options Options for the check.
625
 * 	Valid options
626
 *	  in => provide a list of choices that selections must be made from
627
 *	  max => maximun number of non-zero choices that can be made
628
 * 	  min => minimum number of non-zero choices that can be made
629
 * @return boolean Success
630
 * @access public
631
 */
632
	function multiple($check, $options = array()) {
633
		$defaults = array('in' => null, 'max' => null, 'min' => null);
634
		$options = array_merge($defaults, $options);
635
		$check = array_filter((array)$check);
636
		if (empty($check)) {
637
			return false;
638
		}
639
		if ($options['max'] && sizeof($check) > $options['max']) {
640
			return false;
641
		}
642
		if ($options['min'] && sizeof($check) < $options['min']) {
643
			return false;
644
		}
645
		if ($options['in'] && is_array($options['in'])) {
646
			foreach ($check as $val) {
647
				if (!in_array($val, $options['in'])) {
648
					return false;
649
				}
650
			}
651
		}
652
		return true;
653
	}
654
/**
655
 * Checks if a value is numeric.
656
 *
657
 * @param string $check Value to check
658
 * @return boolean Succcess
659
 * @access public
660
 */
661
	function numeric($check) {
662
		return is_numeric($check);
663
	}
664
/**
665
 * Check that a value is a valid phone number.
666
 *
667
 * @param mixed $check Value to check (string or array)
668
 * @param string $regex Regular expression to use
669
 * @param string $country Country code (defaults to 'all')
670
 * @return boolean Success
671
 * @access public
672
 */
673
	function phone($check, $regex = null, $country = 'all') {
674
		$_this =& Validation::getInstance();
675
		$_this->check = $check;
676
		$_this->regex = $regex;
677
		$_this->country = $country;
678
		if (is_array($check)) {
679
			$_this->_extract($check);
680
		}
681
 
682
		if (is_null($_this->regex)) {
683
			switch ($_this->country) {
684
				case 'us':
685
				// includes all NANPA members. see http://en.wikipedia.org/wiki/North_American_Numbering_Plan#List_of_NANPA_countries_and_territories
686
				default:
687
					$_this->regex  = '/^(?:\+?1)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$/';
688
				break;
689
			}
690
		}
691
		return $_this->_check();
692
	}
693
/**
694
 * Checks that a given value is a valid postal code.
695
 *
696
 * @param mixed $check Value to check
697
 * @param string $regex Regular expression to use
698
 * @param string $country Country to use for formatting
699
 * @return boolean Success
700
 * @access public
701
 */
702
	function postal($check, $regex = null, $country = null) {
703
		$_this =& Validation::getInstance();
704
		$_this->check = $check;
705
		$_this->regex = $regex;
706
		$_this->country = $country;
707
		if (is_array($check)) {
708
			$_this->_extract($check);
709
		}
710
 
711
		if (is_null($_this->regex)) {
712
			switch ($_this->country) {
713
				case 'uk':
714
					$_this->regex  = '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i';
715
					break;
716
				case 'ca':
717
					$_this->regex  = '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]\\b\\z/i';
718
					break;
719
				case 'it':
720
				case 'de':
721
					$_this->regex  = '/^[0-9]{5}$/i';
722
					break;
723
				case 'be':
724
					$_this->regex  = '/^[1-9]{1}[0-9]{3}$/i';
725
					break;
726
				case 'us':
727
				default:
728
					$_this->regex  = '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i';
729
					break;
730
			}
731
		}
732
		return $_this->_check();
733
	}
734
/**
735
 * Validate that a number is in specified range.
736
 * if $lower and $upper are not set, will return true if
737
 * $check is a legal finite on this platform
738
 *
739
 * @param string $check Value to check
740
 * @param integer $lower Lower limit
741
 * @param integer $upper Upper limit
742
 * @return boolean Success
743
 * @access public
744
 */
745
	function range($check, $lower = null, $upper = null ) {
746
		if (!is_numeric($check)) {
747
			return false;
748
		}
749
		if (isset($lower) && isset($upper)) {
750
			return ($check > $lower && $check < $upper);
751
		}
752
		return is_finite($check);
753
	}
754
/**
755
 * Checks that a value is a valid Social Security Number.
756
 *
757
 * @param mixed $check Value to check
758
 * @param string $regex Regular expression to use
759
 * @param string $country Country
760
 * @return boolean Success
761
 * @access public
762
 */
763
	function ssn($check, $regex = null, $country = null) {
764
		$_this =& Validation::getInstance();
765
		$_this->check = $check;
766
		$_this->regex = $regex;
767
		$_this->country = $country;
768
		if (is_array($check)) {
769
			$_this->_extract($check);
770
		}
771
 
772
		if (is_null($_this->regex)) {
773
			switch ($_this->country) {
774
				case 'dk':
775
					$_this->regex  = '/\\A\\b[0-9]{6}-[0-9]{4}\\b\\z/i';
776
					break;
777
				case 'nl':
778
					$_this->regex  = '/\\A\\b[0-9]{9}\\b\\z/i';
779
					break;
780
				case 'us':
781
				default:
782
					$_this->regex  = '/\\A\\b[0-9]{3}-[0-9]{2}-[0-9]{4}\\b\\z/i';
783
					break;
784
			}
785
		}
786
		return $_this->_check();
787
	}
788
/**
789
 * Checks that a value is a valid URL according to http://www.w3.org/Addressing/URL/url-spec.txt
790
 *
791
 * The regex checks for the following component parts:
792
 * 	a valid, optional, scheme
793
 * 		a valid ip address OR
794
 * 		a valid domain name as defined by section 2.3.1 of http://www.ietf.org/rfc/rfc1035.txt
795
 *	  with an optional port number
796
 *	an optional valid path
797
 *	an optional query string (get parameters)
798
 *	an optional fragment (anchor tag)
799
 *
800
 * @param string $check Value to check
801
 * @return boolean Success
802
 * @access public
803
 */
804
	function url($check, $strict = false) {
805
		$_this =& Validation::getInstance();
806
		$_this->check = $check;
807
		$validChars = '([' . preg_quote('!"$&\'()*+,-.@_:;=') . '\/0-9a-z]|(%[0-9a-f]{2}))';
808
		$_this->regex = '/^(?:(?:https?|ftps?|file|news|gopher):\/\/)' . ife($strict, '', '?') .
809
			'(?:' . $_this->__pattern['ip'] . '|' . $_this->__pattern['hostname'] . ')(?::[1-9][0-9]{0,3})?' .
810
			'(?:\/?|\/' . $validChars . '*)?' .
811
			'(?:\?' . $validChars . '*)?' .
812
			'(?:#' . $validChars . '*)?$/i';
813
		return $_this->_check();
814
	}
815
/**
816
 * Checks if a value is in a given list.
817
 *
818
 * @param string $check Value to check
819
 * @param array $list List to check against
820
 * @return boolean Succcess
821
 * @access public
822
 */
823
	function inList($check, $list) {
824
		return in_array($check, $list);
825
	}
826
/**
827
 * Runs an user-defined validation.
828
 *
829
 * @param mixed $check value that will be validated in user-defined methods.
830
 * @param object $object class that holds validation method
831
 * @param string $method class method name for validation to run
832
 * @param array $args arguments to send to method
833
 * @return mixed user-defined class class method returns
834
 * @access public
835
 */
836
	function userDefined($check, $object, $method, $args = null) {
837
		return call_user_func_array(array(&$object, $method), array($check, $args));
838
	}
839
/**
840
 * Runs a regular expression match.
841
 *
842
 * @return boolean Success of match
843
 * @access protected
844
 */
845
	function _check() {
846
		$_this =& Validation::getInstance();
847
		if (preg_match($_this->regex, $_this->check)) {
848
			$_this->error[] = false;
849
			return true;
850
		} else {
851
			$_this->error[] = true;
852
			return false;
853
		}
854
	}
855
/**
856
 * Get the values to use when value sent to validation method is
857
 * an array.
858
 *
859
 * @param array $params Parameters sent to validation method
860
 * @return void
861
 * @access protected
862
 */
863
	function _extract($params) {
864
		$_this =& Validation::getInstance();
865
		extract($params, EXTR_OVERWRITE);
866
 
867
		if (isset($check)) {
868
			$_this->check = $check;
869
		}
870
		if (isset($regex)) {
871
			$_this->regex = $regex;
872
		}
873
		if (isset($country)) {
874
			$_this->country = strtolower($country);
875
		}
876
		if (isset($deep)) {
877
			$_this->deep = $deep;
878
		}
879
		if (isset($type)) {
880
			$_this->type = $type;
881
		}
882
	}
883
/**
884
 * Luhn algorithm
885
 *
886
 * @see http://en.wikipedia.org/wiki/Luhn_algorithm
887
 * @return boolean Success
888
 * @access protected
889
 */
890
	function _luhn() {
891
		$_this =& Validation::getInstance();
892
		if ($_this->deep !== true) {
893
			return true;
894
		}
895
		if ($_this->check == 0) {
896
			return false;
897
		}
898
		$sum = 0;
899
		$length = strlen($_this->check);
900
 
901
		for ($position = 1 - ($length % 2); $position < $length; $position += 2) {
902
			$sum += $_this->check[$position];
903
		}
904
 
905
		for ($position = ($length % 2); $position < $length; $position += 2) {
906
			$number = $_this->check[$position] * 2;
907
			$sum += ($number < 10) ? $number : $number - 9;
908
		}
909
 
910
		return ($sum % 10 == 0);
911
	}
912
/**
913
 * Reset internal variables for another validation run.
914
 *
915
 * @return void
916
 * @access private
917
 */
918
	function __reset() {
919
		$this->check = null;
920
		$this->regex = null;
921
		$this->country = null;
922
		$this->deep = null;
923
		$this->type = null;
924
		$this->error = array();
925
		$this->errors = array();
926
	}
927
}
928
?>