CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

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

Generated using CakePHP API Docs