Changeset 550 for trunk/lib/FormValidator.inc.php
- Timestamp:
- Nov 24, 2015 5:38:54 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/FormValidator.inc.php
r534 r550 37 37 $fv = new FormValidator(); 38 38 39 $fv->empty('field_name', sprintf(_("%s cannot be blank."), _("Field name")) );40 $fv->stringLength('field_name', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Field name"), 0, 255) );41 $fv->isInteger('field_name', sprintf(_("%s must be an integer."), _("Field name")) );42 $fv->checkRegex('field_name', '/^\d{4}$|^$/', true, sprintf(_("%s must be in MMYY format."), _("Field name")) );43 $fv->numericRange('field_name', 0, 65535, sprintf(_("%s must be a number between %d and %d."), _("Field name"), 0, 65535) );44 $fv->validatePhone('field_name' );45 $fv->validateEmail('field_name' );46 $fv->validateStrDate('field_name', sprintf(_("%s must be a valid date in YYYY-MM-DD format."), _("Field name")) );39 $fv->empty('field_name', sprintf(_("%s cannot be blank."), _("Field name")), MSG_ERR, __FILE__, __LINE__); 40 $fv->stringLength('field_name', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Field name"), 0, 255), MSG_ERR, __FILE__, __LINE__); 41 $fv->isInteger('field_name', sprintf(_("%s must be an integer."), _("Field name")), MSG_ERR, __FILE__, __LINE__); 42 $fv->checkRegex('field_name', '/^\d{4}$|^$/', true, sprintf(_("%s must be in MMYY format."), _("Field name")), MSG_ERR, __FILE__, __LINE__); 43 $fv->numericRange('field_name', 0, 65535, sprintf(_("%s must be a number between %d and %d."), _("Field name"), 0, 65535), MSG_ERR, __FILE__, __LINE__); 44 $fv->validatePhone('field_name', MSG_ERR, __FILE__, __LINE__); 45 $fv->validateEmail('field_name', MSG_ERR, __FILE__, __LINE__); 46 $fv->validateStrDate('field_name', sprintf(_("%s must be a valid date in YYYY-MM-DD format."), _("Field name")), MSG_ERR, __FILE__, __LINE__); 47 47 if (is_null($var)) { 48 $fv->addError('field_name', sprintf(_("%s is invalid."), _("Field name")) );48 $fv->addError('field_name', sprintf(_("%s is invalid."), _("Field name")), MSG_ERR, __FILE__, __LINE__); 49 49 } 50 50 if ($fv->anyErrors()) { … … 73 73 74 74 /** 75 76 77 78 79 75 * FormValidator constructor. 76 * 77 * @access public 78 * @param array $params Configuration parameters for this object. 79 */ 80 80 public function __construct($params=array()) 81 81 { … … 85 85 86 86 /** 87 88 89 90 91 87 * Set (or overwrite existing) parameters by passing an array of new parameters. 88 * 89 * @access public 90 * @param array $params Array of parameters (key => val pairs). 91 */ 92 92 public function setParam($params) 93 93 { … … 103 103 104 104 /** 105 * Return the value of a parameter, if it exists. 106 * 107 * @access public 108 * @param string $param Which parameter to return. 109 * @return mixed Configured parameter value. 110 */ 105 * Return the value of a parameter, if it exists. 106 * 107 * @access public 108 * @param string $param Which parameter to return. 109 * @param const $type A LOG_* constant (see App->logMsg()) 110 * @param const $file Filename to log (usually __FILE__) 111 * @param const $line Line number to log (usually __LINE__) 112 * @return mixed Configured parameter value. 113 */ 111 114 public function getParam($param) 112 115 { … … 122 125 123 126 /** 124 * Return the current list of errors. 125 * 126 * @return array an array of errors in the following arrangement: 127 * keys: the name of the variable with an error 128 * vals: the message to display for that error 129 */ 127 * Return the current list of errors. 128 * 129 * @param const $type A LOG_* constant (see App->logMsg()) 130 * @param const $file Filename to log (usually __FILE__) 131 * @param const $line Line number to log (usually __LINE__) 132 * @return array an array of errors in the following arrangement: 133 * keys: the name of the variable with an error 134 * vals: the message to display for that error 135 */ 130 136 public function getErrorList() 131 137 { … … 134 140 135 141 /** 136 137 138 139 140 141 142 143 144 142 * Add an error to the errors stack. 143 * 144 * @param string $form_name The name of the incoming form variable. 145 * @param string $msg The error message for that form. 146 * @param int $type The type of message: MSG_NOTICE, 147 * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. 148 * @param string $file __FILE__. 149 * @param string $line __LINE__. 150 */ 145 151 public function addError($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) 146 152 { … … 155 161 156 162 /** 157 * Check whether any errors have been triggered. 158 * 159 * @param string $form_name the name of the incoming form variable 160 * 161 * @return bool true if any errors were found, or if found for 162 * a variable of $form_name, false otherwise 163 */ 163 * Check whether any errors have been triggered. 164 * 165 * @param string $form_name the name of the incoming form variable 166 * @param const $type A LOG_* constant (see App->logMsg()) 167 * @param const $file Filename to log (usually __FILE__) 168 * @param const $line Line number to log (usually __LINE__) 169 * @return bool true if any errors were found, or if found for 170 * a variable of $form_name, false otherwise 171 */ 164 172 public function anyErrors($form_name=null) 165 173 { … … 177 185 178 186 /** 179 180 187 * Reset the error list. 188 */ 181 189 public function resetErrorList() 182 190 { … … 185 193 186 194 /** 187 188 189 190 191 192 193 194 195 196 195 * Prints the HTML for displaying error messages. 196 * 197 * @param string $above Additional message to print above error messages (e.g. "Oops!"). 198 * @param string $below Additional message to print below error messages (e.g. "Please fix and resubmit"). 199 * @param string $print_gotohash_js Print a line of javascript that scrolls the browser window down to view any error messages. 200 * @param string $hash The #hashtag to scroll to. 201 * @access public 202 * @author Quinn Comendant <quinn@strangecode.com> 203 * @since 15 Jul 2005 01:39:14 204 */ 197 205 public function printErrorMessages($above='', $below='', $print_gotohash_js=false, $hash='sc-msg-formvalidator') 198 206 { 199 207 $app =& App::getInstance(); 200 208 if ($this->anyErrors()) { 201 ?><div class="sc-msg" id="sc-msg-formvalidator"><?php 209 // data-gotohash="âŠ" is to be used by sites that refuse inline JS (via CSP) but needs to be referenced from an external JS. 210 ?><div class="sc-msg" id="sc-msg-formvalidator" data-gotohash="<?php echo oTxt($hash); ?>"><?php 202 211 if ('' != $above) { 203 212 ?><div class="sc-above"><?php echo oTxt($above); ?></div><?php … … 245 254 246 255 /** 247 248 249 250 251 252 256 * If this form has an error, print an error marker like "<<". 257 * 258 * @param string $form_name the name of the incoming form variable 259 * @param string $marker A string to print if there is an error. if 260 * not provided, use default. 261 */ 253 262 public function err($form_name, $marker=null) 254 263 { … … 280 289 281 290 /** 282 * Ensure the length of string is non-zero. 283 * 284 * @param string $form_name the name of the incoming form variable 285 * @param string $msg the message to display on error 286 * 287 * @return bool true if form is not empty, false otherwise. 288 */ 289 public function notEmpty($form_name, $msg='') 290 { 291 if (!Validator::isEmpty(getFormData($form_name))) { 292 return true; 293 } else { 294 $this->addError($form_name, $msg); 291 * Ensure the length of string is non-zero. 292 * 293 * @param string $form_name the name of the incoming form variable 294 * @param string $msg the message to display on error 295 * @param const $type A LOG_* constant (see App->logMsg()) 296 * @param const $file Filename to log (usually __FILE__) 297 * @param const $line Line number to log (usually __LINE__) 298 * @return bool true if form is not empty, false otherwise. 299 */ 300 public function notEmpty($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) 301 { 302 if (Validator::notEmpty(getFormData($form_name), LOG_NOTICE, $file, $line)) { 303 return true; 304 } else { 305 $this->addError($form_name, $msg, $type, $file, $line); 295 306 return false; 296 307 } … … 298 309 299 310 /* 300 * We were using the isEmpty method *wrong* all these years and should have been using notEmpty. 301 * But the fact is the only use is to ensure a value is not empty, so this function simply becomes 302 * an alias of the one-true notEmpty() function. 311 * We were using the isEmpty method *wrong* for years and should have been using notEmpty becuase it is more grammatically correct. 312 * Because the only use is to ensure a value is not empty, we're simply going to alias this method to notEmpty(). 303 313 * @since 03 Jun 2006 22:56:46 304 314 */ 305 public function isEmpty($form_name, $msg='') 306 { 307 return $this->notEmpty($form_name, $msg); 308 } 309 310 /** 311 * Check whether input is a string. 312 * 313 * @param string $form_name the name of the incoming form variable 314 * @param string $msg the message to display on error 315 * 316 * @return bool true if form is a string, false otherwise. 317 */ 318 public function isString($form_name, $msg='') 319 { 320 if (Validator::isString(getFormData($form_name))) { 321 return true; 322 } else { 323 $this->addError($form_name, $msg); 324 return false; 325 } 326 } 327 328 /** 329 * Check whether input is a number. Allows negative numbers. 330 * 331 * @param string $form_name the name of the incoming form variable 332 * @param string $msg the message to display on error 333 * 334 * @return bool true if no errors found, false otherwise 335 */ 336 public function isNumber($form_name, $msg='') 337 { 338 if (Validator::isNumber(getFormData($form_name))) { 339 return true; 340 } else { 341 $this->addError($form_name, $msg); 342 return false; 343 } 344 } 345 346 /** 347 * addError if input is NOT an integer. Don't just use is_int() because the 348 * data coming from the user is *really* a string. 349 * 350 * @param string $form_name the name of the incoming form variable 351 * @param string $msg the message to display on error 352 * 353 * @return bool true if value is an integer 354 */ 355 public function isInteger($form_name, $msg='', $negative_ok=false) 356 { 357 if (Validator::isInteger(getFormData($form_name), $negative_ok)) { 358 return true; 359 } else { 360 $this->addError($form_name, $msg); 361 return false; 362 } 363 } 364 365 /** 366 * Check whether input is a float. Don't just use is_float() because the 367 * data coming from the user is *really* a string. Integers will also 368 * pass this test. 369 * 370 * @param string $form_name the name of the incoming form variable 371 * @param string $msg the message to display on error 372 * 373 * @return bool true if value is a float 374 */ 375 public function isFloat($form_name, $msg='', $negative_ok=false) 376 { 377 if (Validator::isFloat(getFormData($form_name), $negative_ok)) { 378 return true; 379 } else { 380 $this->addError($form_name, $msg); 381 return false; 382 } 383 } 384 385 /** 386 * Check whether input is a Decimal or Fixed type. Check values to be stored in mysql decimal, numeric, num, or fixed types. 387 * Note: some integers and floats will also pass this test. 388 * https://dev.mysql.com/doc/refman/5.5/en/fixed-point-types.html 389 * 390 * @param string $form_name the name of the incoming form variable 391 * @param string $msg the message to display on error 392 * @param string $val The input data to validate. 393 * @param bool $negative_ok If the value can be unsigned. 394 * @param int $max Total max number of digits. 395 * @param int $dec Total max number of digits after the decimal place. 396 * @return bool true if value is a float 397 */ 398 public function isDecimal($form_name, $msg='', $negative_ok=false, $max=10, $dec=2) 399 { 400 if (Validator::isDecimal(getFormData($form_name), $negative_ok, $max, $dec)) { 401 return true; 402 } else { 403 $this->addError($form_name, $msg); 404 return false; 405 } 406 } 407 408 /** 409 * Check whether input is an array. 410 * 411 * @param string $form_name the name of the incoming form variable 412 * @param string $msg the message to display on error 413 * 414 * @return bool true if value is a float 415 */ 416 public function isArray($form_name, $msg='') 417 { 418 if (Validator::isArray(getFormData($form_name))) { 419 return true; 420 } else { 421 $this->addError($form_name, $msg); 422 return false; 423 } 424 } 425 426 /** 427 * Check whether input matches the specified perl regular expression 428 * pattern. 429 * 430 * @param string $form_name The name of the incoming form variable 431 * @param int $regex Perl regex that the string must match 432 * @param bool $valid_on_match Set to true to be valid if match, or false to be valid if the match fails. 433 * @param string $msg The message to display on error 434 * 435 * @return bool true if value passes regex test 436 */ 437 public function checkRegex($form_name, $regex, $valid_on_match=true, $msg='') 438 { 439 if (Validator::checkRegex(getFormData($form_name), $regex, $valid_on_match)) { 440 return true; 441 } else { 442 $this->addError($form_name, $msg); 443 return false; 444 } 445 } 446 447 /** 448 * Tests if the string length is between specified values. Whitespace excluded for min. 449 * 450 * @param string $form_name the name of the incoming form variable 451 * @param int $min minimum length of string, inclusive 452 * @param int $max maximum length of string, inclusive 453 * @param string $msg the message to display on error 454 * 455 * @return bool true if string length is within given boundaries 456 */ 457 public function stringLength($form_name, $min, $max, $msg='') 458 { 459 if (Validator::stringLength(getFormData($form_name), $min, $max)) { 460 return true; 461 } else { 462 $this->addError($form_name, $msg); 463 return false; 464 } 465 } 466 467 /** 468 * Check whether input is within a valid numeric range. 469 * 470 * @param string $form_name the name of the incoming form variable 471 * @param int $min minimum value of number, inclusive 472 * @param int $max maximum value of number, inclusive 473 * @param string $msg the message to display on error 474 * 475 * @return bool true if no errors found, false otherwise 476 */ 477 public function numericRange($form_name, $min, $max, $msg='') 478 { 479 if (Validator::numericRange(getFormData($form_name), $min, $max)) { 480 return true; 481 } else { 482 $this->addError($form_name, $msg); 483 return false; 484 } 485 } 486 487 /** 488 * Validates an email address based on the recommendations in RFC 3696. 489 * Is more loose than restrictive, to allow the many valid variants of 490 * email addresses while catching the most common mistakes. 491 * http://www.faqs.org/rfcs/rfc822.html 492 * http://www.faqs.org/rfcs/rfc2822.html 493 * http://www.faqs.org/rfcs/rfc3696.html 494 * http://www.faqs.org/rfcs/rfc1035.html 495 * 496 * @access public 497 * @param string $form_name The name of the incoming form variable. 498 * @return bool Validity of address. 499 * @author Quinn Comendant <quinn@strangecode.com> 500 */ 501 public function validateEmail($form_name, $strict=false) 315 public function isEmpty($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) 316 { 317 return $this->notEmpty($form_name, $msg, $type, $file, $line); 318 } 319 320 /** 321 * Check whether input is a string. 322 * 323 * @param string $form_name the name of the incoming form variable 324 * @param string $msg the message to display on error 325 * @param const $type A LOG_* constant (see App->logMsg()) 326 * @param const $file Filename to log (usually __FILE__) 327 * @param const $line Line number to log (usually __LINE__) 328 * @return bool true if form is a string, false otherwise. 329 */ 330 public function isString($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) 331 { 332 if (Validator::isString(getFormData($form_name), LOG_NOTICE, $file, $line)) { 333 return true; 334 } else { 335 $this->addError($form_name, $msg, $type, $file, $line); 336 return false; 337 } 338 } 339 340 /** 341 * Check whether input is a number. Allows negative numbers. 342 * 343 * @param string $form_name the name of the incoming form variable 344 * @param string $msg the message to display on error 345 * @param const $type A LOG_* constant (see App->logMsg()) 346 * @param const $file Filename to log (usually __FILE__) 347 * @param const $line Line number to log (usually __LINE__) 348 * @return bool true if no errors found, false otherwise 349 */ 350 public function isNumber($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) 351 { 352 if (Validator::isNumber(getFormData($form_name), LOG_NOTICE, $file, $line)) { 353 return true; 354 } else { 355 $this->addError($form_name, $msg, $type, $file, $line); 356 return false; 357 } 358 } 359 360 /** 361 * addError if input is NOT an integer. Don't just use is_int() because the 362 * data coming from the user is *really* a string. 363 * 364 * @param string $form_name the name of the incoming form variable 365 * @param string $msg the message to display on error 366 * @param const $type A LOG_* constant (see App->logMsg()) 367 * @param const $file Filename to log (usually __FILE__) 368 * @param const $line Line number to log (usually __LINE__) 369 * @return bool true if value is an integer 370 */ 371 public function isInteger($form_name, $msg='', $negative_ok=false, $type=MSG_ERR, $file=null, $line=null) 372 { 373 if (Validator::isInteger(getFormData($form_name), $negative_ok, LOG_NOTICE, $file, $line)) { 374 return true; 375 } else { 376 $this->addError($form_name, $msg, $type, $file, $line); 377 return false; 378 } 379 } 380 381 /** 382 * Check whether input is a float. Don't just use is_float() because the 383 * data coming from the user is *really* a string. Integers will also 384 * pass this test. 385 * 386 * @param string $form_name the name of the incoming form variable 387 * @param string $msg the message to display on error 388 * @param const $type A LOG_* constant (see App->logMsg()) 389 * @param const $file Filename to log (usually __FILE__) 390 * @param const $line Line number to log (usually __LINE__) 391 * @return bool true if value is a float 392 */ 393 public function isFloat($form_name, $msg='', $negative_ok=false, $type=MSG_ERR, $file=null, $line=null) 394 { 395 if (Validator::isFloat(getFormData($form_name), $negative_ok, LOG_NOTICE, $file, $line)) { 396 return true; 397 } else { 398 $this->addError($form_name, $msg, $type, $file, $line); 399 return false; 400 } 401 } 402 403 /** 404 * Check whether input is a Decimal or Fixed type. Use to check values to be stored in mysql decimal, numeric, num, or fixed types. 405 * The arguments $max and $dec should match M and D of the column definition "DECIMAL(M,D)". 406 * Note: some integers and floats will also pass this test. 407 * https://dev.mysql.com/doc/refman/5.5/en/fixed-point-types.html 408 * 409 * @param string $form_name The name of the incoming form variable 410 * @param int $max Total max number of digits. 411 * @param int $dec Total max number of digits after the decimal place. 412 * @param string $msg The message to display on error 413 * @param bool $negative_ok If the value can be unsigned. 414 * @param const $type A LOG_* constant (see App->logMsg()) 415 * @param const $file Filename to log (usually __FILE__) 416 * @param const $line Line number to log (usually __LINE__) 417 * @return bool True if value is a decimal, false otherwise. 418 */ 419 public function isDecimal($form_name, $max=10, $dec=2, $negative_ok=false, $msg='', $type=MSG_ERR, $file=null, $line=null) 420 { 421 if (Validator::isDecimal(getFormData($form_name), $max, $dec, $negative_ok, LOG_NOTICE, $file, $line)) { 422 return true; 423 } else { 424 // Set the example to a sequence of Ns with $max number of digits and a faction part length of $dec. 425 $msg = str_replace('{EX}', sprintf('%s.%s', str_repeat('N', $max - $dec), str_repeat('N', $dec)), $msg); 426 $this->addError($form_name, $msg, $type, $file, $line); 427 return false; 428 } 429 } 430 431 /** 432 * Check whether input is an array. 433 * 434 * @param string $form_name the name of the incoming form variable 435 * @param string $msg the message to display on error 436 * @param const $type A LOG_* constant (see App->logMsg()) 437 * @param const $file Filename to log (usually __FILE__) 438 * @param const $line Line number to log (usually __LINE__) 439 * @return bool true if value is an array 440 */ 441 public function isArray($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) 442 { 443 if (Validator::isArray(getFormData($form_name), LOG_NOTICE, $file, $line)) { 444 return true; 445 } else { 446 $this->addError($form_name, $msg, $type, $file, $line); 447 return false; 448 } 449 } 450 451 /** 452 * Check whether input matches the specified perl regular expression 453 * pattern. 454 * 455 * @param string $form_name The name of the incoming form variable 456 * @param int $regex Perl regex that the string must match 457 * @param bool $valid_on_match Set to true to be valid if match, or false to be valid if the match fails. 458 * @param string $msg The message to display on error 459 * @param const $type A LOG_* constant (see App->logMsg()) 460 * @param const $file Filename to log (usually __FILE__) 461 * @param const $line Line number to log (usually __LINE__) 462 * @return bool true if value passes regex test (or false if $valid_on_match=false) 463 */ 464 public function checkRegex($form_name, $regex, $valid_on_match=true, $msg='', $type=MSG_ERR, $file=null, $line=null) 465 { 466 if (Validator::checkRegex(getFormData($form_name), $regex, $valid_on_match, LOG_NOTICE, $file, $line)) { 467 return true; 468 } else { 469 $this->addError($form_name, $msg, $type, $file, $line); 470 return false; 471 } 472 } 473 474 /** 475 * Tests if the string length is between specified values. Whitespace excluded for min. 476 * 477 * @param string $form_name the name of the incoming form variable 478 * @param int $min minimum length of string, inclusive 479 * @param int $max maximum length of string, inclusive 480 * @param string $msg the message to display on error 481 * @param const $type A LOG_* constant (see App->logMsg()) 482 * @param const $file Filename to log (usually __FILE__) 483 * @param const $line Line number to log (usually __LINE__) 484 * @return bool true if string length is within given boundaries 485 */ 486 public function stringLength($form_name, $min, $max, $msg='', $type=MSG_ERR, $file=null, $line=null) 487 { 488 if (Validator::stringLength(getFormData($form_name), $min, $max, LOG_NOTICE, $file, $line)) { 489 return true; 490 } else { 491 $this->addError($form_name, $msg, $type, $file, $line); 492 return false; 493 } 494 } 495 496 /** 497 * Check whether input is within a valid numeric range. 498 * 499 * @param string $form_name the name of the incoming form variable 500 * @param int $min minimum value of number, inclusive 501 * @param int $max maximum value of number, inclusive 502 * @param string $msg the message to display on error 503 * @param const $type A LOG_* constant (see App->logMsg()) 504 * @param const $file Filename to log (usually __FILE__) 505 * @param const $line Line number to log (usually __LINE__) 506 * @return bool true if no errors found, false otherwise 507 */ 508 public function numericRange($form_name, $min, $max, $msg='', $type=MSG_ERR, $file=null, $line=null) 509 { 510 if (Validator::numericRange(getFormData($form_name), $min, $max, LOG_NOTICE, $file, $line)) { 511 return true; 512 } else { 513 $this->addError($form_name, $msg, $type, $file, $line); 514 return false; 515 } 516 } 517 518 /** 519 * Validates an email address based on the recommendations in RFC 3696. 520 * Is more loose than restrictive, to allow the many valid variants of 521 * email addresses while catching the most common mistakes. 522 * http://www.faqs.org/rfcs/rfc822.html 523 * http://www.faqs.org/rfcs/rfc2822.html 524 * http://www.faqs.org/rfcs/rfc3696.html 525 * http://www.faqs.org/rfcs/rfc1035.html 526 * 527 * @access public 528 * @param string $form_name The name of the incoming form variable. 529 * @param bool $strict Run strict tests (check if the domain exists and has an MX record assigned) 530 * @param const $type A LOG_* constant (see App->logMsg()) 531 * @param const $file Filename to log (usually __FILE__) 532 * @param const $line Line number to log (usually __LINE__) 533 * @return bool Validity of address. 534 * @author Quinn Comendant <quinn@strangecode.com> 535 */ 536 public function validateEmail($form_name, $strict=false, $type=MSG_ERR, $file=null, $line=null) 502 537 { 503 538 $app =& App::getInstance(); … … 511 546 512 547 // Validator::validateEmail() returns a value that relates to the Validate::EMAIL_* constants (defined in Validator.inc.php). 513 switch (Validator::validateEmail($email, $strict )) {548 switch (Validator::validateEmail($email, $strict, LOG_NOTICE, $file, $line)) { 514 549 case Validator::EMAIL_REGEX_FAIL: 515 550 // Failed regex match. 516 $this->addError($form_name, sprintf(_("The email address <em>%s</em> is formatted incorrectly."), oTxt($email)) );551 $this->addError($form_name, sprintf(_("The email address <em>%s</em> is formatted incorrectly."), oTxt($email)), $type, $file, $line); 517 552 $app->logMsg(sprintf('The email address %s is not valid.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__); 518 553 return false; 519 554 520 case Validator::EMAIL_LENGTH_FAIL 555 case Validator::EMAIL_LENGTH_FAIL: 521 556 // Failed length requirements. 522 $this->addError($form_name, sprintf(_("The email address <em>%s</em> is too long (email addresses must have fewer than 256 characters)."), oTxt($email)) );557 $this->addError($form_name, sprintf(_("The email address <em>%s</em> is too long (email addresses must have fewer than 256 characters)."), oTxt($email)), $type, $file, $line); 523 558 $app->logMsg(sprintf('The email address %s must contain less than 256 characters.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__); 524 559 return false; 525 560 526 case Validator::EMAIL_MX_FAIL 561 case Validator::EMAIL_MX_FAIL: 527 562 // Failed MX record test. 528 $this->addError($form_name, sprintf(_("The email address <em>%s</em> does not have a valid domain name"), oTxt($email)) );529 $app->logMsg(sprintf('The email address %s does not have a valid domain name.', oTxt($email)), LOG_ INFO, __FILE__, __LINE__);530 return false; 531 532 case Validator::EMAIL_SUCCESS 563 $this->addError($form_name, sprintf(_("The email address <em>%s</em> does not have a valid domain name"), oTxt($email)), $type, $file, $line); 564 $app->logMsg(sprintf('The email address %s does not have a valid domain name.', oTxt($email)), LOG_NOTICE, __FILE__, __LINE__); 565 return false; 566 567 case Validator::EMAIL_SUCCESS: 533 568 default : 534 569 return true; … … 537 572 538 573 /** 539 * Check whether input is a valid phone number. Notice: it is now set 540 * to allow characters like - or () or + so people can type in a phone 541 * number that looks like: +1 (530) 555-1212 542 * 543 * @param string $form_name the name of the incoming form variable 544 * 545 * @return bool true if no errors found, false otherwise 546 */ 547 public function validatePhone($form_name) 574 * Check whether input is a valid phone number. Notice: it is now set 575 * to allow characters like - or () or + so people can type in a phone 576 * number that looks like: +1 (530) 555-1212 577 * 578 * @param string $form_name the name of the incoming form variable 579 * @param const $type A LOG_* constant (see App->logMsg()) 580 * @param const $file Filename to log (usually __FILE__) 581 * @param const $line Line number to log (usually __LINE__) 582 * @return bool true if no errors found, false otherwise 583 */ 584 public function validatePhone($form_name, $type=MSG_ERR, $file=null, $line=null) 548 585 { 549 586 $app =& App::getInstance(); … … 552 589 553 590 // Validator::validateEmail() returns a value that relates to the Validate::PHONE_* constants (defined in Validator.inc.php). 554 switch (Validator::validatePhone($phone )) {591 switch (Validator::validatePhone($phone, LOG_NOTICE, $file, $line)) { 555 592 case Validator::PHONE_REGEX_FAIL: 556 593 // Failed regex match. 557 $this->addError($form_name, sprintf(_("The phone number <em>%s</em> is not valid."), oTxt($phone)) );594 $this->addError($form_name, sprintf(_("The phone number <em>%s</em> is not valid."), oTxt($phone)), $type, $file, $line); 558 595 $app->logMsg(sprintf('The phone number %s is not valid.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__); 559 596 return false; 560 597 561 case Validator::PHONE_LENGTH_FAIL 598 case Validator::PHONE_LENGTH_FAIL: 562 599 // Failed length requirements. 563 $this->addError($form_name, sprintf(_("The phone number <em>%s</em> is too long (phone number must have fewer than 25 characters)."), oTxt($phone)) );600 $this->addError($form_name, sprintf(_("The phone number <em>%s</em> is too long (phone number must have fewer than 25 characters)."), oTxt($phone)), $type, $file, $line); 564 601 $app->logMsg(sprintf('The phone number %s must contain less than 256 characters.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__); 565 602 return false; 566 603 567 case Validator::PHONE_SUCCESS 604 case Validator::PHONE_SUCCESS: 568 605 default : 569 606 return true; … … 572 609 573 610 /** 574 * Verifies that date can be processed by the strtotime function. 575 * 576 * @param string $form_name the name of the incoming form variable 577 * @param string $msg the message to display on error 578 * 579 * @return bool true if no errors found, false otherwise 580 */ 581 public function validateStrDate($form_name, $msg='') 611 * Verifies that date can be processed by the strtotime function. 612 * 613 * @param string $form_name the name of the incoming form variable 614 * @param string $msg the message to display on error 615 * @param const $type A LOG_* constant (see App->logMsg()) 616 * @param const $file Filename to log (usually __FILE__) 617 * @param const $line Line number to log (usually __LINE__) 618 * @return bool true if no errors found, false otherwise 619 */ 620 public function validateStrDate($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) 582 621 { 583 622 $app =& App::getInstance(); 584 623 585 if (Validator::validateStrDate(getFormData($form_name, '') )) {586 return true; 587 } else { 588 $this->addError($form_name, $msg );624 if (Validator::validateStrDate(getFormData($form_name, ''), LOG_NOTICE, $file, $line)) { 625 return true; 626 } else { 627 $this->addError($form_name, $msg, $type, $file, $line); 589 628 $app->logMsg(sprintf('The string date %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 590 629 return false; … … 594 633 595 634 /** 596 * Verifies credit card number using the Luhn (mod 10) algorithm. 597 * http://en.wikipedia.org/wiki/Luhn_algorithm 598 * 599 * @param string $form_name The name of the incoming form variable. 600 * @param string $cc_type Optional, card type to do specific checks. One of the Validator::CC_TYPE_* constants. 601 * 602 * @return bool true if no errors found, false otherwise 603 */ 604 public function validateCCNumber($form_name, $cc_type=null) 635 * Verifies credit card number using the Luhn (mod 10) algorithm. 636 * http://en.wikipedia.org/wiki/Luhn_algorithm 637 * 638 * @param string $form_name The name of the incoming form variable. 639 * @param string $cc_type Optional, card type to do specific checks. One of the Validator::CC_TYPE_* constants. 640 * @param const $type A LOG_* constant (see App->logMsg()) 641 * @param const $file Filename to log (usually __FILE__) 642 * @param const $line Line number to log (usually __LINE__) 643 * @return bool true if no errors found, false otherwise 644 */ 645 public function validateCCNumber($form_name, $cc_type=null, $type=MSG_ERR, $file=null, $line=null) 605 646 { 606 647 $cc_num = getFormData($form_name); 607 648 608 if (Validator::validateCCNumber($cc_num, $cc_type)) { 609 return true; 610 } else { 611 $this->addError($form_name, sprintf(_("The credit card number you entered is not valid. Please check the number and try again."), $cc_num)); 612 return false; 613 } 614 } 615 616 /** 617 * Check whether a file was selected for uploading. If file is missing, it's an error. 618 * 619 * @param string $form_name the name of the incoming form variable 620 * @param string $msg the message to display on error 621 * 622 * @return bool true if no errors found, false otherwise 623 */ 624 public function fileUploaded($form_name, $msg='') 625 { 626 if (Validator::fileUploaded($form_name)) { 627 return true; 628 } else { 629 $this->addError($form_name, $msg); 630 return false; 631 } 632 } 633 /** 634 * Check whether a file was selected for uploading. If file is missing, it's an error. 635 * 636 * @param string $form_name the name of the incoming form variable 637 * @param string $msg the message to display on error 638 * 639 * @return bool true if no errors found, false otherwise 640 */ 641 public function fileUploadSize($form_name, $msg='') 642 { 643 if (Validator::fileUploadSize($form_name)) { 649 if (Validator::validateCCNumber($cc_num, $cc_type, LOG_NOTICE, $file, $line)) { 650 return true; 651 } else { 652 $this->addError($form_name, sprintf(_("The credit card number you entered is not valid. Please check the number and try again."), $cc_num), $type, $file, $line); 653 return false; 654 } 655 } 656 657 /** 658 * Check whether a file was selected for uploading. If file is missing, it's an error. 659 * 660 * @param string $form_name the name of the incoming form variable 661 * @param string $msg the message to display on error 662 * @param const $type A LOG_* constant (see App->logMsg()) 663 * @param const $file Filename to log (usually __FILE__) 664 * @param const $line Line number to log (usually __LINE__) 665 * @return bool true if no errors found, false otherwise 666 */ 667 public function fileUploaded($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) 668 { 669 if (Validator::fileUploaded($form_name, LOG_NOTICE, $file, $line)) { 670 return true; 671 } else { 672 $this->addError($form_name, $msg, $type, $file, $line); 673 return false; 674 } 675 } 676 /** 677 * Check whether a file was selected for uploading. If file is missing, it's an error. 678 * 679 * @param string $form_name the name of the incoming form variable 680 * @param string $msg the message to display on error 681 * @param const $type A LOG_* constant (see App->logMsg()) 682 * @param const $file Filename to log (usually __FILE__) 683 * @param const $line Line number to log (usually __LINE__) 684 * @return bool true if no errors found, false otherwise 685 */ 686 public function fileUploadSize($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) 687 { 688 if (Validator::fileUploadSize($form_name, LOG_NOTICE, $file, $line)) { 644 689 return true; 645 690 } else { 646 691 $msg = '' == $msg ? sprintf(_("Maximum filesize exceeded. Got %s, but limit is %s."), humanFileSize($_SERVER['CONTENT_LENGTH']), humanFileSize(phpIniGetBytes('upload_max_filesize'))) : $msg; 647 $this->addError($form_name, $msg );692 $this->addError($form_name, $msg, $type, $file, $line); 648 693 return false; 649 694 }
Note: See TracChangeset
for help on using the changeset viewer.