Changeset 568 for branches/1.1dev/lib
- Timestamp:
- Oct 29, 2016 2:15:08 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1.1dev/lib/FormValidator.inc.php
r409 r568 1 1 <?php 2 2 /** 3 * FormValidator.inc.php 3 * FormValidator.inc.php 4 4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information 5 5 */ 6 6 7 7 // Examples of use: 8 8 // … … 35 35 class FormValidator 36 36 { 37 37 38 38 /** 39 39 * Array filling with errors. The key will be the name of the form where … … 41 41 */ 42 42 var $errors = array(); 43 43 44 44 /** 45 45 * Return the current list of errors. … … 53 53 return $this->errors; 54 54 } 55 55 56 56 /** 57 57 * Add an error to the errors stack. … … 74 74 ); 75 75 } 76 76 77 77 /** 78 78 * Check whether any errors have been triggered. … … 80 80 * @param string $form_name the name of the incoming form variable 81 81 * 82 * @return bool true if any errors were found, or if found for 82 * @return bool true if any errors were found, or if found for 83 83 * a variable of $form_name, false otherwise 84 84 */ … … 88 88 foreach ($this->errors as $err) { 89 89 if ($err['name'] == $form_name) { 90 return true; 90 return true; 91 91 } 92 92 } … … 134 134 function notEmpty($form_name, $msg='') 135 135 { 136 136 137 137 $val = getFormData($form_name); 138 138 if (is_array($val)) { … … 143 143 return false; 144 144 } 145 } else { 145 } else { 146 146 if (trim($val) != '') { 147 147 $this->addError($form_name, $msg); … … 171 171 return false; 172 172 } 173 } else { 173 } else { 174 174 if (trim($val) == '') { 175 175 $this->addError($form_name, $msg); … … 242 242 /** 243 243 * Check whether input is a float. Don't just use is_float() because the 244 * data coming from the user is *really* a string. Integers will also 244 * data coming from the user is *really* a string. Integers will also 245 245 * pass this test. 246 246 * … … 280 280 } 281 281 } 282 282 283 283 /** 284 284 * Check whether input matches the specified perl regular expression 285 * pattern. 285 * pattern. 286 286 * 287 287 * @param string $form_name the name of the incoming form variable … … 312 312 } 313 313 } 314 314 315 315 /** 316 316 * Tests if the string length is between specified values. Whitespace excluded for min. … … 326 326 { 327 327 $val = getFormData($form_name); 328 328 329 329 if (strlen(trim($val)) < $min || strlen($val) > $max) { 330 330 $this->addError($form_name, $msg); … … 361 361 362 362 /** 363 * Validates email address length, domain name exist ance, format.363 * Validates email address length, domain name existence, format. 364 364 * 365 365 * @param string $form_name The name of the incoming form variable 366 * 366 * @param bool $strict Run strict tests (check if the domain exists and has an MX record assigned) 367 367 * @return bool true if no errors found, false otherwise 368 368 */ 369 function validateEmail($form_name )370 { 369 function validateEmail($form_name, $strict=false) 370 { 371 371 $email = getFormData($form_name); 372 372 if ('' == trim($email)) { 373 373 return false; 374 374 } 375 375 376 376 $regex = '/^(?:[^,@]*\s+|[^,@]*(<)|)' // Display name 377 377 . '((?:[^.<>\s@\",\[\]]+[^<>\s@\",\[\]])*[^.<>\s@\",\[\]]+)' // Local-part … … 386 386 . '|' 387 387 . '(?:|\s*|\s+\([^,@]+\)\s*))$/i'; 388 388 389 389 // Test email address format. 390 390 if (!preg_match($regex, getFormData($form_name), $e_parts)) { … … 393 393 return false; 394 394 } 395 395 396 396 // We have a match! Here are the captured subpatterns, on which further tests are run. 397 // The part before the @. 397 // The part before the @. 398 398 $local = $e_parts[2]; 399 399 400 // The part after the @. 400 // The part after the @. 401 401 // If domain is an IP [XXX.XXX.XXX.XXX] strip off the brackets. 402 402 $domain = $e_parts[3]{0} == '[' ? mb_substr($e_parts[3], 1, -1) : $e_parts[3]; 403 403 404 404 // Test length. 405 405 if (mb_strlen($local) > 64 || mb_strlen($domain) > 191) { … … 408 408 return false; 409 409 } 410 411 // Check domain exists: It's a domain if ip2long fails; Checkdnsrr ensures a MX record exists; Gethostbyname() ensures the domain exists. 412 // Compare ip2long twice for php4 backwards compat. 413 if ((ip2long($domain) == '-1' || ip2long($domain) === false) && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) { 414 $this->addError($form_name, sprintf(_("The email address <em>%s</em> does not have a valid domain name."), oTxt(getFormData($form_name))), MSG_ERR, __FILE__, __LINE__); 415 logMsg(sprintf('The email address %s does not have a valid domain name.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 416 return false; 417 } 418 410 411 if ($strict) { 412 // Strict tests. 413 if (ip2long($domain) === false && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) { 414 // Check domain exists: It's a domain if ip2long fails; checkdnsrr ensures a MX record exists; gethostbyname() ensures the domain exists. 415 $this->addError($form_name, sprintf(_("The email address <em>%s</em> does not have a valid domain name."), oTxt(getFormData($form_name))), MSG_ERR, __FILE__, __LINE__); 416 $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val))); 417 return false; 418 } 419 } 420 419 421 return true; 420 422 } … … 432 434 { 433 435 $phone = getFormData($form_name); 434 436 435 437 $this->checkRegex($form_name, '/^[0-9 +().-]*$/', true, sprintf(_("The phone number <strong>%s</strong> is not valid."), $phone)); 436 438 $this->stringLength($form_name, 0, 25, sprintf(_("The phone number <strong>%s</strong> is too long"), $phone)); … … 455 457 } 456 458 } 457 458 459 460 459 461 /** 460 462 * Verifies credit card number. … … 471 473 $cc_num = getFormData($form_name); 472 474 } 473 475 474 476 if ('' == $cc_num) { 475 477 return false; 476 478 } 477 479 478 480 // Innocent until proven guilty 479 481 $card_is_valid = true; 480 482 481 483 // Get rid of any non-digits 482 484 $cc_num = preg_replace('/[^\d]/', '', $cc_num); 483 485 484 486 // Perform card-specific checks, if applicable 485 487 switch (strtolower($cc_type)) { … … 514 516 break; 515 517 } 516 518 517 519 // The Luhn formula works right to left, so reverse the number. 518 520 $cc_num = strrev($cc_num); 519 521 520 522 $luhn_total = 0; 521 523 … … 529 531 $digit *= 2; 530 532 } 531 533 532 534 // If the result is two digits, add them. 533 535 if (strlen($digit) == 2) { 534 536 $digit = substr($digit,0,1) + substr($digit,1,1); 535 537 } 536 538 537 539 // Add the current digit to the $luhn_total. 538 540 $luhn_total += $digit; 539 541 } 540 542 541 543 // If it passed (or bypassed) the card-specific check and the Total is evenly divisible by 10, it's cool! 542 544 if ($card_is_valid && $luhn_total % 10 == 0) { … … 565 567 } 566 568 } 567 569 568 570 } // THE END 569 571
Note: See TracChangeset
for help on using the changeset viewer.