validation.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: validation.php 8126 2009-03-23 13:48:37Z jperras $ */
00003 /**
00004  * Validation Class.  Used for validation of model data
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00011  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00018  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package       cake
00020  * @subpackage    cake.cake.libs
00021  * @since         CakePHP(tm) v 1.2.0.3830
00022  * @version       $Revision: 8126 $
00023  * @modifiedby    $LastChangedBy: jperras $
00024  * @lastmodified  $Date: 2009-03-23 09:48:37 -0400 (Mon, 23 Mar 2009) $
00025  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * Deprecated
00029  */
00030 /**
00031  * Not empty.
00032  */
00033     define('VALID_NOT_EMPTY', '/.+/');
00034 /**
00035  * Numbers [0-9] only.
00036  */
00037     define('VALID_NUMBER', '/^[-+]?\\b[0-9]*\\.?[0-9]+\\b$/');
00038 /**
00039  * A valid email address.
00040  */
00041     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");
00042 /**
00043  * A valid year (1000-2999).
00044  */
00045     define('VALID_YEAR', '/^[12][0-9]{3}$/');
00046 /**
00047  * Offers different validation methods.
00048  *
00049  * Long description for file
00050  *
00051  * @package       cake
00052  * @subpackage    cake.cake.libs
00053  * @since         CakePHP v 1.2.0.3830
00054  */
00055 class Validation extends Object {
00056 /**
00057  * Set the the value of methods $check param.
00058  *
00059  * @var string
00060  * @access public
00061  */
00062     var $check = null;
00063 /**
00064  * Set to a valid regular expression in the class methods.
00065  * Can be set from $regex param also
00066  *
00067  * @var string
00068  * @access public
00069  */
00070     var $regex = null;
00071 /**
00072  * Some complex patterns needed in multiple places
00073  *
00074  * @var array
00075  * @access private
00076  */
00077     var $__pattern = array(
00078         '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])',
00079         'hostname' => '(?:[a-z0-9][-a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,4}|museum|travel)'
00080     );
00081 /**
00082  * Some class methods use a country to determine proper validation.
00083  * This can be passed to methods in the $country param
00084  *
00085  * @var string
00086  * @access public
00087  */
00088     var $country = null;
00089 /**
00090  * Some class methods use a deeper validation when set to true
00091  *
00092  * @var string
00093  * @access public
00094  */
00095     var $deep = null;
00096 /**
00097  * Some class methods use the $type param to determine which validation to perfom in the method
00098  *
00099  * @var string
00100  * @access public
00101  */
00102     var $type = null;
00103 /**
00104  * Holds an array of errors messages set in this class.
00105  * These are used for debugging purposes
00106  *
00107  * @var array
00108  * @access public
00109  */
00110     var $errors = array();
00111 /**
00112  * Gets a reference to the Validation object instance
00113  *
00114  * @return object Validation instance
00115  * @access public
00116  * @static
00117  */
00118     function &getInstance() {
00119         static $instance = array();
00120 
00121         if (!$instance) {
00122             $instance[0] =& new Validation();
00123         }
00124         return $instance[0];
00125     }
00126 /**
00127  * Checks that a string contains something other than whitespace
00128  *
00129  * Returns true if string contains something other than whitespace
00130  *
00131  * $check can be passed as an array:
00132  * array('check' => 'valueToCheck');
00133  *
00134  * @param mixed $check Value to check
00135  * @return boolean Success
00136  * @access public
00137  */
00138     function notEmpty($check) {
00139         $_this =& Validation::getInstance();
00140         $_this->__reset();
00141         $_this->check = $check;
00142 
00143         if (is_array($check)) {
00144             $_this->_extract($check);
00145         }
00146 
00147         if (empty($_this->check) && $_this->check != '0') {
00148             return false;
00149         }
00150         $_this->regex = '/[^\s]+/m';
00151         return $_this->_check();
00152     }
00153 /**
00154  * Checks that a string contains only integer or letters
00155  *
00156  * Returns true if string contains only integer or letters
00157  *
00158  * $check can be passed as an array:
00159  * array('check' => 'valueToCheck');
00160  *
00161  * @param mixed $check Value to check
00162  * @return boolean Success
00163  * @access public
00164  */
00165     function alphaNumeric($check) {
00166         $_this =& Validation::getInstance();
00167         $_this->__reset();
00168         $_this->check = $check;
00169 
00170         if (is_array($check)) {
00171             $_this->_extract($check);
00172         }
00173 
00174         if (empty($_this->check) && $_this->check != '0') {
00175             return false;
00176         }
00177         $_this->regex = '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/mu';
00178         return $_this->_check();
00179     }
00180 /**
00181  * Checks that a string length is within s specified range.
00182  * Spaces are included in the character count.
00183  * Returns true is string matches value min, max, or between min and max,
00184  *
00185  * @param string $check Value to check for length
00186  * @param integer $min Minimum value in range (inclusive)
00187  * @param integer $max Maximum value in range (inclusive)
00188  * @return boolean Success
00189  * @access public
00190  */
00191     function between($check, $min, $max) {
00192         $length = strlen($check);
00193         return ($length >= $min && $length <= $max);
00194     }
00195 /**
00196  * Returns true if field is left blank -OR- only whitespace characters are present in it's value
00197  * Whitespace characters include Space, Tab, Carriage Return, Newline
00198  *
00199  * $check can be passed as an array:
00200  * array('check' => 'valueToCheck');
00201  *
00202  * @param mixed $check Value to check
00203  * @return boolean Success
00204  * @access public
00205  */
00206     function blank($check) {
00207         $_this =& Validation::getInstance();
00208         $_this->__reset();
00209         $_this->check = $check;
00210 
00211         if (is_array($check)) {
00212             $_this->_extract($check);
00213         }
00214 
00215         $_this->regex = '/[^\\s]/';
00216         return !$_this->_check();
00217     }
00218 /**
00219  * Validation of credit card numbers.
00220  * Returns true if $check is in the proper credit card format.
00221  *
00222  * @param mixed $check credit card number to validate
00223  * @param mixed $type 'all' may be passed as a sting, defaults to fast which checks format of most major credit cards
00224  *                          if an array is used only the values of the array are checked.
00225  *                          Example: array('amex', 'bankcard', 'maestro')
00226  * @param boolean $deep set to true this will check the Luhn algorithm of the credit card.
00227  * @param string $regex A custom regex can also be passed, this will be used instead of the defined regex values
00228  * @return boolean Success
00229  * @access public
00230  * @see Validation::_luhn()
00231  */
00232     function cc($check, $type = 'fast', $deep = false, $regex = null) {
00233         $_this =& Validation::getInstance();
00234         $_this->__reset();
00235         $_this->check = $check;
00236         $_this->type = $type;
00237         $_this->deep = $deep;
00238         $_this->regex = $regex;
00239 
00240         if (is_array($check)) {
00241             $_this->_extract($check);
00242         }
00243         $_this->check = str_replace(array('-', ' '), '', $_this->check);
00244 
00245         if (strlen($_this->check) < 13) {
00246             return false;
00247         }
00248 
00249         if (!is_null($_this->regex)) {
00250             if ($_this->_check()) {
00251                 return $_this->_luhn();
00252             }
00253         }
00254         $cards = array('all' => array('amex' => '/^3[4|7]\\d{13}$/',
00255                                     'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/',
00256                                     'diners'   => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/',
00257                                     'disc'     => '/^(?:6011|650\\d)\\d{12}$/',
00258                                     'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/',
00259                                     'enroute'  => '/^2(?:014|149)\\d{11}$/',
00260                                     'jcb'      => '/^(3\\d{4}|2100|1800)\\d{11}$/',
00261                                     'maestro'  => '/^(?:5020|6\\d{3})\\d{12}$/',
00262                                     'mc'       => '/^5[1-5]\\d{14}$/',
00263                                     'solo'     => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/',
00264                                     '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})?)$/',
00265                                     'visa'     => '/^4\\d{12}(\\d{3})?$/',
00266                                     'voyager'  => '/^8699[0-9]{11}$/'),
00267                             '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})$/');
00268 
00269         if (is_array($_this->type)) {
00270             foreach ($_this->type as $value) {
00271                 $_this->regex = $cards['all'][strtolower($value)];
00272 
00273                 if ($_this->_check()) {
00274                     return $_this->_luhn();
00275                 }
00276             }
00277         } elseif ($_this->type == 'all') {
00278             foreach ($cards['all'] as $value) {
00279                 $_this->regex = $value;
00280 
00281                 if ($_this->_check()) {
00282                     return $_this->_luhn();
00283                 }
00284             }
00285         } else {
00286             $_this->regex = $cards['fast'];
00287 
00288             if ($_this->_check()) {
00289                 return $_this->_luhn();
00290             }
00291         }
00292     }
00293 /**
00294  * Used to compare 2 numeric values.
00295  *
00296  * @param mixed $check1 if string is passed for a string must also be passed for $check2
00297  *                          used as an array it must be passed as array('check1' => value, 'operator' => 'value', 'check2' -> value)
00298  * @param string $operator Can be either a word or operand
00299  *                              is greater >, is less <, greater or equal >=
00300  *                              less or equal <=, is less <, equal to ==, not equal !=
00301  * @param integer $check2 only needed if $check1 is a string
00302  * @return boolean Success
00303  * @access public
00304  */
00305     function comparison($check1, $operator = null, $check2 = null) {
00306         if (is_array($check1)) {
00307             extract($check1, EXTR_OVERWRITE);
00308         }
00309         $operator = str_replace(array(' ', "\t", "\n", "\r", "\0", "\x0B"), '', strtolower($operator));
00310 
00311         switch ($operator) {
00312             case 'isgreater':
00313             case '>':
00314                 if ($check1 > $check2) {
00315                     return true;
00316                 }
00317                 break;
00318             case 'isless':
00319             case '<':
00320                 if ($check1 < $check2) {
00321                     return true;
00322                 }
00323                 break;
00324             case 'greaterorequal':
00325             case '>=':
00326                 if ($check1 >= $check2) {
00327                     return true;
00328                 }
00329                 break;
00330             case 'lessorequal':
00331             case '<=':
00332                 if ($check1 <= $check2) {
00333                     return true;
00334                 }
00335                 break;
00336             case 'equalto':
00337             case '==':
00338                 if ($check1 == $check2) {
00339                     return true;
00340                 }
00341                 break;
00342             case 'notequal':
00343             case '!=':
00344                 if ($check1 != $check2) {
00345                     return true;
00346                 }
00347                 break;
00348             default:
00349                 $_this =& Validation::getInstance();
00350                 $_this->errors[] = __('You must define the $operator parameter for Validation::comparison()', true);
00351                 break;
00352         }
00353         return false;
00354     }
00355 /**
00356  * Used when a custom regular expression is needed.
00357  *
00358  * @param mixed $check When used as a string, $regex must also be a valid regular expression.
00359  *                              As and array: array('check' => value, 'regex' => 'valid regular expression')
00360  * @param string $regex If $check is passed as a string, $regex must also be set to valid regular expression
00361  * @return boolean Success
00362  * @access public
00363  */
00364     function custom($check, $regex = null) {
00365         $_this =& Validation::getInstance();
00366         $_this->__reset();
00367         $_this->check = $check;
00368         $_this->regex = $regex;
00369         if (is_array($check)) {
00370             $_this->_extract($check);
00371         }
00372         if ($_this->regex === null) {
00373             $_this->errors[] = __('You must define a regular expression for Validation::custom()', true);
00374             return false;
00375         }
00376         return $_this->_check();
00377     }
00378 /**
00379  * Date validation, determines if the string passed is a valid date.
00380  * keys that expect full month, day and year will validate leap years
00381  *
00382  * @param string $check a valid date string
00383  * @param mixed $format Use a string or an array of the keys below. Arrays should be passed as array('dmy', 'mdy', etc)
00384  *                  Keys: dmy 27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slash
00385  *                          mdy 12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash
00386  *                          ymd 2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash
00387  *                          dMy 27 December 2006 or 27 Dec 2006
00388  *                          Mdy December 27, 2006 or Dec 27, 2006 comma is optional
00389  *                          My December 2006 or Dec 2006
00390  *                          my 12/2006 separators can be a space, period, dash, forward slash
00391  * @param string $regex If a custom regular expression is used this is the only validation that will occur.
00392  * @return boolean Success
00393  * @access public
00394  */
00395     function date($check, $format = 'ymd', $regex = null) {
00396         $_this =& Validation::getInstance();
00397         $_this->__reset();
00398         $_this->check = $check;
00399         $_this->regex = $regex;
00400 
00401         if (!is_null($_this->regex)) {
00402             return $_this->_check();
00403         }
00404 
00405         $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})$%';
00406         $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})$%';
00407         $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]))))$%';
00408         $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})$/';
00409         $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}))$/';
00410         $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})$%';
00411         $regex['my'] = '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))))$%';
00412 
00413         $format = (is_array($format)) ? array_values($format) : array($format);
00414         foreach ($format as $key) {
00415             $_this->regex = $regex[$key];
00416 
00417             if ($_this->_check() === true) {
00418                 return true;
00419             }
00420         }
00421         return false;
00422     }
00423 
00424 /**
00425  * Time validation, determines if the string passed is a valid time.
00426  * Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m)
00427  * Does not allow/validate seconds.
00428  *
00429  * @param string $check a valid time string
00430  * @return boolean Success
00431  * @access public
00432  */
00433 
00434     function time($check) {
00435         $_this =& Validation::getInstance();
00436         $_this->__reset();
00437         $_this->check = $check;
00438         $_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}$%';
00439         return $_this->_check();
00440     }
00441 
00442 /**
00443  * Boolean validation, determines if value passed is a boolean integer or true/false.
00444  *
00445  * @param string $check a valid boolean
00446  * @return boolean Success
00447  * @access public
00448  */
00449     function boolean($check) {
00450         $booleanList = array(0, 1, '0', '1', true, false);
00451         return in_array($check, $booleanList, true);
00452     }
00453 
00454 /**
00455  * Checks that a value is a valid decimal. If $places is null, the $check is allowed to be a scientific float
00456  * If no decimal point is found a false will be returned. Both the sign and exponent are optional.
00457  *
00458  * @param integer $check The value the test for decimal
00459  * @param integer $places if set $check value must have exactly $places after the decimal point
00460  * @param string $regex If a custom regular expression is used this is the only validation that will occur.
00461  * @return boolean Success
00462  * @access public
00463  */
00464     function decimal($check, $places = null, $regex = null) {
00465         $_this =& Validation::getInstance();
00466         $_this->__reset();
00467         $_this->regex = $regex;
00468         $_this->check = $check;
00469 
00470         if (is_null($_this->regex)) {
00471             if (is_null($places)) {
00472                 $_this->regex = '/^[-+]?[0-9]*\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?$/';
00473             } else {
00474                 $_this->regex = '/^[-+]?[0-9]*\\.{1}[0-9]{'.$places.'}$/';
00475             }
00476         }
00477         return $_this->_check();
00478     }
00479 /**
00480  * Validates for an email address.
00481  *
00482  * @param string $check Value to check
00483  * @param boolean $deep Perform a deeper validation (if true), by also checking availability of host
00484  * @param string $regex Regex to use (if none it will use built in regex)
00485  * @return boolean Success
00486  * @access public
00487  */
00488     function email($check, $deep = false, $regex = null) {
00489         $_this =& Validation::getInstance();
00490         $_this->__reset();
00491         $_this->check = $check;
00492         $_this->regex = $regex;
00493         $_this->deep = $deep;
00494 
00495         if (is_array($check)) {
00496             $_this->_extract($check);
00497         }
00498 
00499         if (is_null($_this->regex)) {
00500             $_this->regex = '/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@' . $_this->__pattern['hostname'] . '$/i';
00501         }
00502         $return = $_this->_check();
00503 
00504         if ($_this->deep === false || $_this->deep === null) {
00505             return $return;
00506         }
00507 
00508         if ($return === true && preg_match('/@(' . $_this->__pattern['hostname'] . ')$/i', $_this->check, $regs)) {
00509             $host = gethostbynamel($regs[1]);
00510             return is_array($host);
00511         }
00512         return false;
00513     }
00514 /**
00515  * Check that value is exactly $comparedTo.
00516  *
00517  * @param mixed $check Value to check
00518  * @param mixed $comparedTo Value to compare
00519  * @return boolean Success
00520  * @access public
00521  */
00522     function equalTo($check, $comparedTo) {
00523         return ($check === $comparedTo);
00524     }
00525 /**
00526  * Check that value has a valid file extension.
00527  *
00528  * @param mixed $check Value to check
00529  * @param array $extensions file extenstions to allow
00530  * @return boolean Success
00531  * @access public
00532  */
00533     function extension($check, $extensions = array('gif', 'jpeg', 'png', 'jpg')) {
00534         if (is_array($check)) {
00535             return Validation::extension(array_shift($check), $extensions);
00536         }
00537         $extension = strtolower(array_pop(explode('.', $check)));
00538         foreach ($extensions as $value) {
00539             if ($extension == strtolower($value)) {
00540                 return true;
00541             }
00542         }
00543         return false;
00544     }
00545 /**
00546  * Check that value is a file name
00547  *
00548  * @param mixed $check Value to check
00549  * @access public
00550  * @todo finish implementation
00551  */
00552     function file($check) {
00553         // if (is_array($check)) {
00554         //  foreach ($check as $value) {
00555         //      if (!Validation::file($value)) {
00556         //          return false;
00557         //      }
00558         //  }
00559         //  return true;
00560         // }
00561         //
00562         // return preg_match('/[\w| |_]+\.[\w]+/', $check);
00563     }
00564 /**
00565  * Validation of an IPv4 address.
00566  *
00567  * @param string $check The string to test.
00568  * @return boolean Success
00569  * @access public
00570  */
00571     function ip($check) {
00572         $_this =& Validation::getInstance();
00573         $_this->check = $check;
00574         $_this->regex = '/^' . $_this->__pattern['ip'] . '$/';
00575         return $_this->_check();
00576     }
00577 /**
00578  * Checks whether the length of a string is greater or equal to a minimal length.
00579  *
00580  * @param string $check The string to test
00581  * @param integer $min The minimal string length
00582  * @return boolean Success
00583  * @access public
00584  */
00585     function minLength($check, $min) {
00586         $length = strlen($check);
00587         return ($length >= $min);
00588     }
00589 /**
00590  * Checks whether the length of a string is smaller or equal to a maximal length..
00591  *
00592  * @param string $check The string to test
00593  * @param integer $max The maximal string length
00594  * @return boolean Success
00595  * @access public
00596  */
00597     function maxLength($check, $max) {
00598         $length = strlen($check);
00599         return ($length <= $max);
00600     }
00601 /**
00602  * Checks that a value is a monetary amount.
00603  *
00604  * @param string $check Value to check
00605  * @param string $symbolPosition Where symbol is located (left/right)
00606  * @return boolean Success
00607  * @access public
00608  */
00609     function money($check, $symbolPosition = 'left') {
00610         $_this =& Validation::getInstance();
00611         $_this->check = $check;
00612 
00613         if ($symbolPosition == 'right') {
00614             $_this->regex = '/^(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?(?<!\x{00a2})\p{Sc}?$/u';
00615         } else {
00616             $_this->regex = '/^(?!\x{00a2})\p{Sc}?(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?$/u';
00617         }
00618         return $_this->_check();
00619     }
00620 /**
00621  * Validate a multiple select.
00622  *
00623  * @param mixed $check Value to check
00624  * @param mixed $options Options for the check.
00625  *  Valid options
00626  *    in => provide a list of choices that selections must be made from
00627  *    max => maximun number of non-zero choices that can be made
00628  *    min => minimum number of non-zero choices that can be made
00629  * @return boolean Success
00630  * @access public
00631  */
00632     function multiple($check, $options = array()) {
00633         $defaults = array('in' => null, 'max' => null, 'min' => null);
00634         $options = array_merge($defaults, $options);
00635         $check = array_filter((array)$check);
00636         if (empty($check)) {
00637             return false;
00638         }
00639         if ($options['max'] && sizeof($check) > $options['max']) {
00640             return false;
00641         }
00642         if ($options['min'] && sizeof($check) < $options['min']) {
00643             return false;
00644         }
00645         if ($options['in'] && is_array($options['in'])) {
00646             foreach ($check as $val) {
00647                 if (!in_array($val, $options['in'])) {
00648                     return false;
00649                 }
00650             }
00651         }
00652         return true;
00653     }
00654 /**
00655  * Checks if a value is numeric.
00656  *
00657  * @param string $check Value to check
00658  * @return boolean Succcess
00659  * @access public
00660  */
00661     function numeric($check) {
00662         return is_numeric($check);
00663     }
00664 /**
00665  * Check that a value is a valid phone number.
00666  *
00667  * @param mixed $check Value to check (string or array)
00668  * @param string $regex Regular expression to use
00669  * @param string $country Country code (defaults to 'all')
00670  * @return boolean Success
00671  * @access public
00672  */
00673     function phone($check, $regex = null, $country = 'all') {
00674         $_this =& Validation::getInstance();
00675         $_this->check = $check;
00676         $_this->regex = $regex;
00677         $_this->country = $country;
00678         if (is_array($check)) {
00679             $_this->_extract($check);
00680         }
00681 
00682         if (is_null($_this->regex)) {
00683             switch ($_this->country) {
00684                 case 'us':
00685                 // includes all NANPA members. see http://en.wikipedia.org/wiki/North_American_Numbering_Plan#List_of_NANPA_countries_and_territories
00686                 default:
00687                     $_this->regex  = '/^(?:\+?1)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$/';
00688                 break;
00689             }
00690         }
00691         return $_this->_check();
00692     }
00693 /**
00694  * Checks that a given value is a valid postal code.
00695  *
00696  * @param mixed $check Value to check
00697  * @param string $regex Regular expression to use
00698  * @param string $country Country to use for formatting
00699  * @return boolean Success
00700  * @access public
00701  */
00702     function postal($check, $regex = null, $country = null) {
00703         $_this =& Validation::getInstance();
00704         $_this->check = $check;
00705         $_this->regex = $regex;
00706         $_this->country = $country;
00707         if (is_array($check)) {
00708             $_this->_extract($check);
00709         }
00710 
00711         if (is_null($_this->regex)) {
00712             switch ($_this->country) {
00713                 case 'uk':
00714                     $_this->regex  = '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i';
00715                     break;
00716                 case 'ca':
00717                     $_this->regex  = '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]\\b\\z/i';
00718                     break;
00719                 case 'it':
00720                 case 'de':
00721                     $_this->regex  = '/^[0-9]{5}$/i';
00722                     break;
00723                 case 'be':
00724                     $_this->regex  = '/^[1-9]{1}[0-9]{3}$/i';
00725                     break;
00726                 case 'us':
00727                 default:
00728                     $_this->regex  = '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i';
00729                     break;
00730             }
00731         }
00732         return $_this->_check();
00733     }
00734 /**
00735  * Validate that a number is in specified range.
00736  * if $lower and $upper are not set, will return true if
00737  * $check is a legal finite on this platform
00738  *
00739  * @param string $check Value to check
00740  * @param integer $lower Lower limit
00741  * @param integer $upper Upper limit
00742  * @return boolean Success
00743  * @access public
00744  */
00745     function range($check, $lower = null, $upper = null ) {
00746         if (!is_numeric($check)) {
00747             return false;
00748         }
00749         if (isset($lower) && isset($upper)) {
00750             return ($check > $lower && $check < $upper);
00751         }
00752         return is_finite($check);
00753     }
00754 /**
00755  * Checks that a value is a valid Social Security Number.
00756  *
00757  * @param mixed $check Value to check
00758  * @param string $regex Regular expression to use
00759  * @param string $country Country
00760  * @return boolean Success
00761  * @access public
00762  */
00763     function ssn($check, $regex = null, $country = null) {
00764         $_this =& Validation::getInstance();
00765         $_this->check = $check;
00766         $_this->regex = $regex;
00767         $_this->country = $country;
00768         if (is_array($check)) {
00769             $_this->_extract($check);
00770         }
00771 
00772         if (is_null($_this->regex)) {
00773             switch ($_this->country) {
00774                 case 'dk':
00775                     $_this->regex  = '/\\A\\b[0-9]{6}-[0-9]{4}\\b\\z/i';
00776                     break;
00777                 case 'nl':
00778                     $_this->regex  = '/\\A\\b[0-9]{9}\\b\\z/i';
00779                     break;
00780                 case 'us':
00781                 default:
00782                     $_this->regex  = '/\\A\\b[0-9]{3}-[0-9]{2}-[0-9]{4}\\b\\z/i';
00783                     break;
00784             }
00785         }
00786         return $_this->_check();
00787     }
00788 /**
00789  * Checks that a value is a valid URL according to http://www.w3.org/Addressing/URL/url-spec.txt
00790  *
00791  * The regex checks for the following component parts:
00792  *  a valid, optional, scheme
00793  *      a valid ip address OR
00794  *      a valid domain name as defined by section 2.3.1 of http://www.ietf.org/rfc/rfc1035.txt
00795  *    with an optional port number
00796  *  an optional valid path
00797  *  an optional query string (get parameters)
00798  *  an optional fragment (anchor tag)
00799  *
00800  * @param string $check Value to check
00801  * @param boolean $strict Require URL to be prefixed by a valid scheme (one of http(s)/ftp(s)/file/news/gopher)
00802  * @return boolean Success
00803  * @access public
00804  */
00805     function url($check, $strict = false) {
00806         $_this =& Validation::getInstance();
00807         $_this->check = $check;
00808         $validChars = '([' . preg_quote('!"$&\'()*+,-.@_:;=') . '\/0-9a-z]|(%[0-9a-f]{2}))';
00809         $_this->regex = '/^(?:(?:https?|ftps?|file|news|gopher):\/\/)' . ife($strict, '', '?') .
00810             '(?:' . $_this->__pattern['ip'] . '|' . $_this->__pattern['hostname'] . ')(?::[1-9][0-9]{0,3})?' .
00811             '(?:\/?|\/' . $validChars . '*)?' .
00812             '(?:\?' . $validChars . '*)?' .
00813             '(?:#' . $validChars . '*)?$/i';
00814         return $_this->_check();
00815     }
00816 /**
00817  * Checks if a value is in a given list.
00818  *
00819  * @param string $check Value to check
00820  * @param array $list List to check against
00821  * @return boolean Succcess
00822  * @access public
00823  */
00824     function inList($check, $list) {
00825         return in_array($check, $list);
00826     }
00827 /**
00828  * Runs an user-defined validation.
00829  *
00830  * @param mixed $check value that will be validated in user-defined methods.
00831  * @param object $object class that holds validation method
00832  * @param string $method class method name for validation to run
00833  * @param array $args arguments to send to method
00834  * @return mixed user-defined class class method returns
00835  * @access public
00836  */
00837     function userDefined($check, $object, $method, $args = null) {
00838         return call_user_func_array(array(&$object, $method), array($check, $args));
00839     }
00840 /**
00841  * Runs a regular expression match.
00842  *
00843  * @return boolean Success of match
00844  * @access protected
00845  */
00846     function _check() {
00847         $_this =& Validation::getInstance();
00848         if (preg_match($_this->regex, $_this->check)) {
00849             $_this->error[] = false;
00850             return true;
00851         } else {
00852             $_this->error[] = true;
00853             return false;
00854         }
00855     }
00856 /**
00857  * Get the values to use when value sent to validation method is
00858  * an array.
00859  *
00860  * @param array $params Parameters sent to validation method
00861  * @return void
00862  * @access protected
00863  */
00864     function _extract($params) {
00865         $_this =& Validation::getInstance();
00866         extract($params, EXTR_OVERWRITE);
00867 
00868         if (isset($check)) {
00869             $_this->check = $check;
00870         }
00871         if (isset($regex)) {
00872             $_this->regex = $regex;
00873         }
00874         if (isset($country)) {
00875             $_this->country = strtolower($country);
00876         }
00877         if (isset($deep)) {
00878             $_this->deep = $deep;
00879         }
00880         if (isset($type)) {
00881             $_this->type = $type;
00882         }
00883     }
00884 /**
00885  * Luhn algorithm
00886  *
00887  * @see http://en.wikipedia.org/wiki/Luhn_algorithm
00888  * @return boolean Success
00889  * @access protected
00890  */
00891     function _luhn() {
00892         $_this =& Validation::getInstance();
00893         if ($_this->deep !== true) {
00894             return true;
00895         }
00896         if ($_this->check == 0) {
00897             return false;
00898         }
00899         $sum = 0;
00900         $length = strlen($_this->check);
00901 
00902         for ($position = 1 - ($length % 2); $position < $length; $position += 2) {
00903             $sum += $_this->check[$position];
00904         }
00905 
00906         for ($position = ($length % 2); $position < $length; $position += 2) {
00907             $number = $_this->check[$position] * 2;
00908             $sum += ($number < 10) ? $number : $number - 9;
00909         }
00910 
00911         return ($sum % 10 == 0);
00912     }
00913 /**
00914  * Reset internal variables for another validation run.
00915  *
00916  * @return void
00917  * @access private
00918  */
00919     function __reset() {
00920         $this->check = null;
00921         $this->regex = null;
00922         $this->country = null;
00923         $this->deep = null;
00924         $this->type = null;
00925         $this->error = array();
00926         $this->errors = array();
00927     }
00928 }
00929 ?>

Generated on Sun Nov 22 00:30:54 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7