* @since 15 Jul 2005 01:39:14
*/
public function printErrorMessages($above='', $below='', $print_gotohash_js=false, $hash='sc-msg-formvalidator')
{
$app =& App::getInstance();
if ($this->anyErrors()) {
// data-gotohash="…" is to be used by sites that refuse inline JS (via CSP) but needs to be referenced from an external JS.
?>
anyErrors($form_name))) {
if (isset($marker)) {
echo $marker;
} else {
switch ($type) {
case MSG_ERR:
default:
echo $this->getParam('error');
break;
case MSG_WARNING:
echo $this->getParam('warning');
break;
case MSG_NOTICE:
echo $this->getParam('notice');
break;
case MSG_SUCCESS:
echo $this->getParam('success');
break;
}
}
}
}
/**
* Ensure the length of string is non-zero.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if form is not empty, false otherwise.
*/
public function notEmpty($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::notEmpty(getFormData($form_name), LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/*
* We were using the isEmpty method *wrong* for years and should have been using notEmpty because it is more grammatically correct.
* Because the only use is to ensure a value is not empty, we're simply going to alias this method to notEmpty().
* @since 03 Jun 2006 22:56:46
*/
public function isEmpty($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
return $this->notEmpty($form_name, $msg, $type, $file, $line);
}
/**
* Check whether input is a string.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if form is a string, false otherwise.
*/
public function isString($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::isString(getFormData($form_name), LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/**
* Check whether input is a number. Allows negative numbers.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if no errors found, false otherwise
*/
public function isNumber($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::isNumber(getFormData($form_name), LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/**
* addError if input is NOT an integer. Don't just use is_int() because the
* data coming from the user is *really* a string.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
* @param bool $negative_ok Set to true if negative numbers will be allowed.
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if value is an integer
*/
public function isInteger($form_name, $msg='', $negative_ok=false, $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::isInteger(getFormData($form_name), $negative_ok, LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/**
* Check whether input is a float. Don't just use is_float() because the
* data coming from the user is *really* a string. Integers will also
* pass this test.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if value is a float
*/
public function isFloat($form_name, $msg='', $negative_ok=false, $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::isFloat(getFormData($form_name), $negative_ok, LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/**
* Check whether input is a Decimal or Fixed type. Use to check values to be stored in mysql decimal, numeric, num, or fixed types.
* The arguments $max and $dec should match M and D of the column definition "DECIMAL(M,D)".
* Note: some integers and floats will also pass this test.
* https://dev.mysql.com/doc/refman/5.5/en/fixed-point-types.html
*
* @param string $form_name The name of the incoming form variable
* @param int $max Total max number of digits.
* @param int $dec Total max number of digits after the decimal place.
* @param string $msg The message to display on error
* @param bool $negative_ok If the value can be unsigned.
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool True if value is a decimal, false otherwise.
*/
public function isDecimal($form_name, $max=10, $dec=2, $negative_ok=false, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::isDecimal(getFormData($form_name), $max, $dec, $negative_ok, LOG_NOTICE, $file, $line)) {
return true;
} else {
// Set the example to a sequence of Ns with $max number of digits and a faction part length of $dec.
$msg = str_replace('{EX}', sprintf('%s.%s', str_repeat('N', $max - $dec), str_repeat('N', $dec)), $msg);
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/**
* Check whether input is an array.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if value is an array
*/
public function isArray($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::isArray(getFormData($form_name), LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/**
* Check whether input matches the specified perl regular expression
* pattern.
*
* @param string $form_name The name of the incoming form variable
* @param int $regex Perl regex that the string must match
* @param bool $valid_on_match Set to true to be valid if match, or false to be valid if the match fails.
* @param string $msg The message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if value passes regex test (or false if $valid_on_match=false)
*/
public function checkRegex($form_name, $regex, $valid_on_match=true, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::checkRegex(getFormData($form_name), $regex, $valid_on_match, LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/**
* Tests if the string length is between specified values. Whitespace excluded for min.
*
* @param string $form_name the name of the incoming form variable
* @param int $min minimum length of string, inclusive
* @param int $max maximum length of string, inclusive
* @param string $msg the message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if string length is within given boundaries
*/
public function stringLength($form_name, $min, $max, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::stringLength(getFormData($form_name), $min, $max, LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/**
* Check whether input is within a valid numeric range.
*
* @param string $form_name the name of the incoming form variable
* @param int $min minimum value of number, inclusive
* @param int $max maximum value of number, inclusive
* @param string $msg the message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if no errors found, false otherwise
*/
public function numericRange($form_name, $min, $max, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::numericRange(getFormData($form_name), $min, $max, LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/**
* Validates an email address based on the recommendations in RFC 3696.
* Is more loose than restrictive, to allow the many valid variants of
* email addresses while catching the most common mistakes.
* http://www.faqs.org/rfcs/rfc822.html
* http://www.faqs.org/rfcs/rfc2822.html
* http://www.faqs.org/rfcs/rfc3696.html
* http://www.faqs.org/rfcs/rfc1035.html
*
* @access public
* @param string $form_name The name of the incoming form variable.
* @param bool $strict Run strict tests (check if the domain exists and has an MX record assigned)
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool Validity of address.
* @author Quinn Comendant
*/
public function validateEmail($form_name, $strict=false, $type=MSG_ERR, $file=null, $line=null)
{
$app =& App::getInstance();
$email = getFormData($form_name);
if ('' == trim($email)) {
// No email address provided, and that's okay.
return true;
}
// Validator::validateEmail() returns a value that relates to the Validate::EMAIL_* constants (defined in Validator.inc.php).
switch (Validator::validateEmail($email, $strict, LOG_NOTICE, $file, $line)) {
case Validator::EMAIL_REGEX_FAIL:
// Failed regex match.
$this->addError($form_name, sprintf(_("The email address %s is formatted incorrectly."), oTxt($email)), $type, $file, $line);
$app->logMsg(sprintf('The email address %s is not valid.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__);
return false;
case Validator::EMAIL_LENGTH_FAIL:
// Failed length requirements.
$this->addError($form_name, sprintf(_("The email address %s is too long (email addresses must have fewer than 256 characters)."), oTxt($email)), $type, $file, $line);
$app->logMsg(sprintf('The email address %s must contain less than 256 characters.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__);
return false;
case Validator::EMAIL_MX_FAIL:
// Failed MX record test.
$this->addError($form_name, sprintf(_("The email address %s does not have a valid domain name"), oTxt($email)), $type, $file, $line);
$app->logMsg(sprintf('The email address %s does not have a valid domain name.', oTxt($email)), LOG_NOTICE, __FILE__, __LINE__);
return false;
case Validator::EMAIL_SUCCESS:
default :
return true;
}
}
/**
* Check whether input is a valid phone number. Notice: it is now set
* to allow characters like - or () or + so people can type in a phone
* number that looks like: +1 (530) 555-1212
*
* @param string $form_name the name of the incoming form variable
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if no errors found, false otherwise
*/
public function validatePhone($form_name, $type=MSG_ERR, $file=null, $line=null)
{
$app =& App::getInstance();
$phone = getFormData($form_name);
// Validator::validateEmail() returns a value that relates to the Validate::PHONE_* constants (defined in Validator.inc.php).
switch (Validator::validatePhone($phone, LOG_NOTICE, $file, $line)) {
case Validator::PHONE_REGEX_FAIL:
// Failed regex match.
$this->addError($form_name, sprintf(_("The phone number %s is not valid."), oTxt($phone)), $type, $file, $line);
$app->logMsg(sprintf('The phone number %s is not valid.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__);
return false;
case Validator::PHONE_LENGTH_FAIL:
// Failed length requirements.
$this->addError($form_name, sprintf(_("The phone number %s is too long (phone number must have fewer than 25 characters)."), oTxt($phone)), $type, $file, $line);
$app->logMsg(sprintf('The phone number %s must contain less than 25 characters.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__);
return false;
case Validator::PHONE_SUCCESS:
default :
return true;
}
}
/**
* Verifies that date can be processed by the strtotime function.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if no errors found, false otherwise
*/
public function validateStrDate($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
$app =& App::getInstance();
if (Validator::validateStrDate(getFormData($form_name, ''), LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
$app->logMsg(sprintf('The string date %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__);
return false;
}
}
/**
* Verifies credit card number using the Luhn (mod 10) algorithm.
* http://en.wikipedia.org/wiki/Luhn_algorithm
*
* @param string $form_name The name of the incoming form variable.
* @param string $cc_type Optional, card type to do specific checks. One of the Validator::CC_TYPE_* constants.
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if no errors found, false otherwise
*/
public function validateCCNumber($form_name, $cc_type=null, $type=MSG_ERR, $file=null, $line=null)
{
$cc_num = getFormData($form_name);
if (Validator::validateCCNumber($cc_num, $cc_type, LOG_NOTICE, $file, $line)) {
return true;
} else {
$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);
return false;
}
}
/**
* Check whether a file was selected for uploading. If file is missing, it's an error.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if no errors found, false otherwise
*/
public function fileUploaded($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::fileUploaded($form_name, LOG_NOTICE, $file, $line)) {
return true;
} else {
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
/**
* Check whether a file was selected for uploading. If file is missing, it's an error.
*
* @param string $form_name the name of the incoming form variable
* @param string $msg the message to display on error
* @param const $type A LOG_* constant (see App->logMsg())
* @param const $file Filename to log (usually __FILE__)
* @param const $line Line number to log (usually __LINE__)
* @return bool true if no errors found, false otherwise
*/
public function fileUploadSize($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
{
if (Validator::fileUploadSize(LOG_NOTICE, $file, $line)) {
return true;
} else {
$msg = '' == $msg ? sprintf(_("Maximum filesize exceeded. Got %s, but limit is %s."), humanFileSize($_SERVER['CONTENT_LENGTH']), humanFileSize(phpIniGetBytes('upload_max_filesize'))) : $msg;
$this->addError($form_name, $msg, $type, $file, $line);
return false;
}
}
} // THE END