You are here

function themekey_validator_ip_address in ThemeKey 7

Same name and namespace in other branches
  1. 7.3 themekey_validators.inc \themekey_validator_ip_address()
  2. 7.2 themekey_validators.inc \themekey_validator_ip_address()

Validates a Theme Switching Rule. Allowed Operators: any Allowed values:

  • valid regular expression if operator is "~"
  • a valid IPv4 address like "123.123.123.123" if operator is "=" or "!"
  • fragment of an IPv4 address which contains at least one digit for different operators

Parameters

$rule: A Theme Switching Rule as associative array:

  • property: ThemeKey property as string (e.g., "drupal:path")
  • wildcard: optional string, only used if property is "drupal:path:wildcard"
  • operator: ThemeKey operator as string ("=", "!", "*", "!*", "<", "<=", ">", ">=", "~", "!~")
  • value: ThemeKey property value as string

Return value

An associative array of errors:

  • property: translated error message as string describing a problem with the property
  • wildcard: translated error message as string describing a problem with the wildcard
  • operator: translated error message as string describing a problem with the operator
  • value: translated error message as string describing a problem with the value

If no errors detected the array is empty.

1 string reference to 'themekey_validator_ip_address'
themekey_system_themekey_properties in modules/themekey.system.inc
Implements hook_themekey_properties().

File

./themekey_validators.inc, line 690
Non-displayable characters.

Code

function themekey_validator_ip_address($rule) {
  $errors = array();
  switch ($rule['operator']) {
    case '=':
    case '!':

      // TODO add for support IPv6
      // TODO improve regex for IPv4
      if (!preg_match("/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\$/", $rule['value'])) {
        $errors['value'] = t('Not a valid IPv4 address. Format should look like "123.123.123.123"');
      }
      break;
    case '~':
    case '!~':
      $errors = themekey_validator_regex($rule);
      break;
    default:

      // TODO add for support IPv6
      // TODO improve regex for IPv4
      if (!preg_match("/^[1-2][0-9\\.]*\$/", $rule['value'])) {
        $errors['value'] = t("Value isn't suitable for checks against dates formatted like \"123.123.123.123\"");
      }
      break;
  }
  return $errors;
}