MediaWiki fundraising/REL1_35
DataValidator Class Reference

DataValidator This class is responsible for performing all kinds of data validation, wherever we may need it. More...

Static Public Member Functions

static cc_number_exists_in_str (string $str)
 Analyzes a string to see if any credit card numbers are hiding out in it.
 
static expandIPBlockToArray ( $ip)
 Takes either an IP address, or an IP address with a CIDR block, and expands it to an array containing all the relevant addresses so we can do things like save the expanded list to memcache, and use in_array().
 
static getCardType ( $card_num)
 Calculates and returns the card type for a given credit card number.
 
static getEmptyErrorArray ()
 getEmptyErrorArray
 
static getError ( $field, $type)
 getError - returns the error object appropriate for a validation error on the specified field, of the specified type.
 
static getErrorToken ( $field)
 getErrorToken, intended to be used by classes that exist relatively close to the form classes, returns the error token (defined on the forms) that specifies where the error will appear within the form output.
 
static getZeroPaddedValue ( $value, $total_length)
 Okay, so this isn't all validation, but there's a validation component in there so I'm calling it close enough.
 
static guessLanguage ( $data)
 Returns a valid mediawiki language code to use for all the DonationInterface translations.
 
static ip_is_listed ( $ip, $ip_list)
 Check whether IP matches a block list.
 
static luhn_check ( $str)
 Performs a Luhn algorithm check on a string.
 
static obviousXssInString ( $value)
 
static special_characters_in_wrong_locations ( $str)
 Analyzes a string to see if there are special characters at the beginning/end of a string or right before the @symbol.
 
static validate (GatewayType $gateway, $data, $check_not_empty=[])
 validate Run all the validation rules we have defined against a (hopefully normalized) DonationInterface data set.
 
static validate_address ( $value)
 Gets rid of numbers that pass luhn in address fields -.
 
static validate_name ( $value)
 Some people are silly and enter their CC numbers as their name.
 
static validate_not_just_punctuation ( $value)
 Validates that somebody didn't just punch in a bunch of punctuation, and nothing else.
 
static value_appears_in ( $needle, $haystack)
 Test to determine if a value appears in a haystack.
 

Static Protected Member Functions

static checkValidationPassed ( $fields, $results)
 checkValidationPassed is a validate helper function.
 
static validate_alphanumeric ( $value)
 validate_alphanumeric Checks to make sure the value is populated with an alphanumeric value... ...which would be great, if it made sense at all.
 
static validate_boolean ( $value)
 validate_boolean Determines if the $value passed in is a valid boolean.
 
static validate_credit_card ( $value)
 validate_credit_card Determines if the $value passed in is (possibly) a valid credit card number.
 
static validate_currency_code ( $value, $acceptedCurrencies)
 
static validate_email ( $value)
 validate_email Determines if the $value passed in is a valid email address.
 
static validate_gateway ( $value)
 validate_gateway Checks to make sure the gateway is populated with a valid and enabled gateway.
 
static validate_not_empty ( $value)
 validate_not_empty Checks to make sure that the $value is present in the $data array, and not null or an empty string.
 
static validate_numeric ( $value)
 validate_numeric Determines if the $value passed in is numeric.
 

Detailed Description

DataValidator This class is responsible for performing all kinds of data validation, wherever we may need it.

All functions should be static, so we don't have to construct anything in order to use it any/everywhere.

Author
khorn
awight

Definition at line 16 of file DataValidator.php.

Member Function Documentation

◆ cc_number_exists_in_str()

static DataValidator::cc_number_exists_in_str ( string $str)
static

Analyzes a string to see if any credit card numbers are hiding out in it.

Parameters
string$str
Returns
bool True if a CC number was found sneaking about in the shadows

Definition at line 450 of file DataValidator.php.

Referenced by EmployerFieldValidation\validate(), validate_address(), validate_email(), validate_name(), and WmfFramework_Drupal\validateEmail().

◆ checkValidationPassed()

static DataValidator::checkValidationPassed ( $fields,
$results )
staticprotected

checkValidationPassed is a validate helper function.

In order to determine that we are ready to do the third stage of data validation (calculated) for any given field, we need to determine that all fields required to validate the original have, themselves, passed validation.

Parameters
array$fieldsAn array of field names to check.
array$resultsIntermediate result of validation.
Returns
bool true if all fields specified in $fields passed their not_empty and valid_type validation. Otherwise, false.

Definition at line 255 of file DataValidator.php.

◆ expandIPBlockToArray()

static DataValidator::expandIPBlockToArray ( $ip)
static

Takes either an IP address, or an IP address with a CIDR block, and expands it to an array containing all the relevant addresses so we can do things like save the expanded list to memcache, and use in_array().

Parameters
string$ipEither a single address, or a block.
Returns
array An expanded list of IP addresses denoted by $ip.

Definition at line 579 of file DataValidator.php.

◆ getCardType()

static DataValidator::getCardType ( $card_num)
static

Calculates and returns the card type for a given credit card number.

Parameters
int$card_numA credit card number.
Returns
string|bool 'amex', 'mc', 'visa', 'discover', or false.

Definition at line 539 of file DataValidator.php.

Referenced by validate_credit_card().

◆ getEmptyErrorArray()

static DataValidator::getEmptyErrorArray ( )
static

getEmptyErrorArray

Deprecated
This only exists anymore, to make badly-coded forms happy when they start pulling keys all over the place without checking to see if they're set or not.
Returns
array All the possible error tokens as keys, with blank errors.

Definition at line 60 of file DataValidator.php.

◆ getError()

static DataValidator::getError ( $field,
$type )
static

getError - returns the error object appropriate for a validation error on the specified field, of the specified type.

Parameters
string$field- The common name of the field containing the data that is causing the error.
string$type- The type of error being caused, from a set. Possible values are: 'not_empty' - the value is required and not currently present 'valid_type' - in general, the wrong format 'calculated' - fields that failed some kind of multiple-field data integrity check.
Returns
ValidationError

Definition at line 93 of file DataValidator.php.

References $type, and getErrorToken().

Referenced by FiscalNumber\getError(), Amount\validate(), validate(), and EmployerFieldValidation\validate().

◆ getErrorToken()

static DataValidator::getErrorToken ( $field)
static

getErrorToken, intended to be used by classes that exist relatively close to the form classes, returns the error token (defined on the forms) that specifies where the error will appear within the form output.

Parameters
string$fieldThe field that ostensibly has an error that needs to be displayed to the user.
Returns
string The error token corresponding to a field

Definition at line 26 of file DataValidator.php.

Referenced by getError(), and validate().

◆ getZeroPaddedValue()

static DataValidator::getZeroPaddedValue ( $value,
$total_length )
static

Okay, so this isn't all validation, but there's a validation component in there so I'm calling it close enough.

Parameters
string$valuethe value that should be zero-padded out to $total_length
int$total_lengthThe fixed number of characters that $value should be padded out to
Returns
string The zero-padded value, or false if it was too long to work with.

Definition at line 676 of file DataValidator.php.

Referenced by IngenicoFinancialNumber\stage().

◆ guessLanguage()

static DataValidator::guessLanguage ( $data)
static

Returns a valid mediawiki language code to use for all the DonationInterface translations.

Will only look at the currently configured language if the 'language' key doesn't exist in the data set: Users may not have a language preference set if we're bouncing between mediawiki instances for payments.

Parameters
array$dataA normalized DonationInterface data set.
Returns
string A valid mediawiki language code.

Definition at line 563 of file DataValidator.php.

◆ ip_is_listed()

static DataValidator::ip_is_listed ( $ip,
$ip_list )
static

Check whether IP matches a block list.

TODO: We might want to store the expanded list in memcache.

Parameters
string$ipThe IP addx we want to check
array$ip_listIP list to check against
Returns
bool

Definition at line 606 of file DataValidator.php.

Referenced by Gateway_Extras_CustomFilters_IP_Velocity\filter(), and Gateway_Extras_SessionVelocityFilter\filter().

◆ luhn_check()

static DataValidator::luhn_check ( $str)
static

Performs a Luhn algorithm check on a string.

Parameters
string$str
Returns
bool True if the number was valid according to the algorithm

Definition at line 513 of file DataValidator.php.

◆ obviousXssInString()

static DataValidator::obviousXssInString ( $value)
static

Definition at line 414 of file DataValidator.php.

Referenced by validate_address(), and validate_name().

◆ special_characters_in_wrong_locations()

static DataValidator::special_characters_in_wrong_locations ( $str)
static

Analyzes a string to see if there are special characters at the beginning/end of a string or right before the @symbol.

Parameters
string$str
Returns
bool True if special character is found at the beginning/end of a string or right before the @symbol

Definition at line 427 of file DataValidator.php.

Referenced by validate_email().

◆ validate()

static DataValidator::validate ( GatewayType $gateway,
$data,
$check_not_empty = [] )
static

validate Run all the validation rules we have defined against a (hopefully normalized) DonationInterface data set.

Parameters
GatewayType$gateway
array$dataNormalized donation data.
array$check_not_emptyAn array of fields to do empty validation on. If this is not populated, no fields will throw errors for being empty, UNLESS they are required for a field that uses them for more complex validation (the 'calculated' phase).
Exceptions
BadMethodCallException
Returns
array A list of ValidationError objects, or empty on successful validation.

Definition at line 129 of file DataValidator.php.

References GatewayType\getCurrencies(), getError(), and getErrorToken().

Referenced by GatewayAdapter\validate().

◆ validate_address()

static DataValidator::validate_address ( $value)
static

Gets rid of numbers that pass luhn in address fields -.

See also
validate_name
Parameters
string$value
Returns
bool True if suspiciously like a CC number

Definition at line 409 of file DataValidator.php.

References cc_number_exists_in_str(), and obviousXssInString().

◆ validate_alphanumeric()

static DataValidator::validate_alphanumeric ( $value)
staticprotected

validate_alphanumeric Checks to make sure the value is populated with an alphanumeric value... ...which would be great, if it made sense at all.

TODO: This is duuuuumb. Make it do something good, or get rid of it. If we can think of a way to make this useful, we should do something here.

Parameters
string$valueThe value that is meant to be alphanumeric
Returns
bool True if $value is ANYTHING. Or not. :[

Definition at line 371 of file DataValidator.php.

◆ validate_boolean()

static DataValidator::validate_boolean ( $value)
staticprotected

validate_boolean Determines if the $value passed in is a valid boolean.

Parameters
string$valueThe piece of data that is supposed to be a boolean.
Returns
bool True if $value is a valid boolean, otherwise false.

Definition at line 309 of file DataValidator.php.

◆ validate_credit_card()

static DataValidator::validate_credit_card ( $value)
staticprotected

validate_credit_card Determines if the $value passed in is (possibly) a valid credit card number.

Parameters
string$valueThe piece of data that is supposed to be a credit card number.
Returns
bool True if $value is a reasonable credit card number, otherwise false.

Definition at line 295 of file DataValidator.php.

References getCardType().

◆ validate_currency_code()

static DataValidator::validate_currency_code ( $value,
$acceptedCurrencies )
staticprotected

Definition at line 281 of file DataValidator.php.

◆ validate_email()

static DataValidator::validate_email ( $value)
staticprotected

validate_email Determines if the $value passed in is a valid email address.

Parameters
string$valueThe piece of data that is supposed to be an email address.
Returns
bool True if $value is a valid email address, otherwise false.

Definition at line 275 of file DataValidator.php.

References cc_number_exists_in_str(), and special_characters_in_wrong_locations().

◆ validate_gateway()

static DataValidator::validate_gateway ( $value)
staticprotected

validate_gateway Checks to make sure the gateway is populated with a valid and enabled gateway.

Parameters
string$valueThe value that is meant to be a gateway.
Returns
bool True if $value is a valid gateway, otherwise false

Definition at line 344 of file DataValidator.php.

◆ validate_name()

static DataValidator::validate_name ( $value)
static

Some people are silly and enter their CC numbers as their name.

This performs a luhn check on the name to make sure it's not actually a potentially valid CC number.

Parameters
string$valueZe name!
Returns
bool True if the name is not suspiciously like a CC number

Definition at line 399 of file DataValidator.php.

References cc_number_exists_in_str(), and obviousXssInString().

◆ validate_not_empty()

static DataValidator::validate_not_empty ( $value)
staticprotected

validate_not_empty Checks to make sure that the $value is present in the $data array, and not null or an empty string.

Anything else that is 'falseish' is still perfectly valid to have as a data point. TODO: Consider doing this in a batch.

Parameters
string$valueThe value to check for non-emptiness.
Returns
bool True if the $value is not missing or empty, otherwise false.

Definition at line 358 of file DataValidator.php.

◆ validate_not_just_punctuation()

static DataValidator::validate_not_just_punctuation ( $value)
static

Validates that somebody didn't just punch in a bunch of punctuation, and nothing else.

Doing so for certain fields can short-circuit AVS checking at some banks, and so we want to treat data like this as empty in the adapter staging phase.

Parameters
string$valueThe value to check
Returns
bool true if it's more than just punctuation, false if it is.

Definition at line 383 of file DataValidator.php.

Referenced by StreetAddress\stage_street().

◆ validate_numeric()

static DataValidator::validate_numeric ( $value)
staticprotected

validate_numeric Determines if the $value passed in is numeric.

Parameters
string$valueThe piece of data that is supposed to be numeric.
Returns
bool True if $value is numeric, otherwise false.

Definition at line 329 of file DataValidator.php.

◆ value_appears_in()

static DataValidator::value_appears_in ( $needle,
$haystack )
static

Test to determine if a value appears in a haystack.

The haystack may have explicit +/- rules (a - will take precedence over a +; if there is no

  • rule, but there is a - rule everything is implicitly accepted); and may also have an 'ALL' condition.
Parameters
mixed$needleValue, or array of values, to match
mixed$haystackValue, or array of values, that are acceptable
Returns
bool

Definition at line 628 of file DataValidator.php.


The documentation for this class was generated from the following file: