You are here

function _commerce_sagepay_validate_input in Drupal Commerce SagePay Integration 7

Validate a string against allowed parameters in the SagePay Protocol.

Parameters

string $text: The string to validate.

int $max_length: The maximum permitted length of the string.

string $type: The type of validation.

Return value

mixed The validated string.

File

includes/commerce_sagepay_utils.inc, line 178
commerce_sagepay_utils.inc Common utilities shared by all Integration methods.

Code

function _commerce_sagepay_validate_input($text, $max_length, $type = SAGEPAY_VALIDATE_ADDRESS) {

  // Trim text to max permitted length.
  if (strlen($text) > $max_length) {
    $text = substr($text, 0, $max_length);
  }

  // Strip unwanted characters.
  $accents = '/&([A-Za-z]{1,2})(grave|acute|circ|cedil|uml|lig);/';
  $text_encoded = htmlentities($text, ENT_NOQUOTES, 'UTF-8');
  $text = preg_replace($accents, '$1', $text_encoded);
  switch ($type) {
    case SAGEPAY_VALIDATE_NAME:
      $pat = '/[^(A-Za-z& \\/\\.\'\\-)]*/';
      break;
    case SAGEPAY_VALIDATE_ADDRESS:
      $pat = '/[^(A-Za-z0-9&:, \\/\\.\'\\+\\-\\{\\})]*/';
      break;
    case SAGEPAY_VALIDATE_POSTCODE:
      $pat = '/[^(A-Za-z0-9 \\-)]*/';
      break;
    case SAGEPAY_VALIDATE_EMAIL:

      // @todo could check for a valid email here, but need to know what
      // to do if it isn't valid.
      return $text;
  }
  return preg_replace($pat, '', $text);
}