You are here

function _domain_alias_validate in Domain Access 7.2

Same name and namespace in other branches
  1. 6.2 domain_alias/domain_alias.admin.inc \_domain_alias_validate()
  2. 7.3 domain_alias/domain_alias.admin.inc \_domain_alias_validate()

Helper function to validate alias entries.

Parameters

$alias: The form element defining the alias.

$id: The form array id, either a number or an alias_id.

$aliases: The array of currently processed alias strings.

$type: The type of alias, used to determine form errors.

Return value

A valid alias string, or an error.

1 call to _domain_alias_validate()
domain_alias_form_validate in domain_alias/domain_alias.admin.inc
FAPI for domain_alias_form()

File

domain_alias/domain_alias.admin.inc, line 162
Administration functions for the domain_alias module.

Code

function _domain_alias_validate($form, $alias, $id, $aliases, $type) {
  $alias = $alias['pattern'];

  // 1) Check that the same alias is not entered twice.
  if (in_array($alias, $aliases)) {
    form_error($form[$type][$id]['pattern'], t('%name is already defined. You must use unique values.', array(
      '%name' => $alias,
    )));
    return;
  }

  // 2) Check that the alias only has one wildcard.
  $count = substr_count($alias, '*') + substr_count($alias, '?');
  if ($count > 1) {
    form_error($form[$type][$id]['pattern'], t('You may only have one wildcard character in each alias.'));
    return;
  }

  // 3) Only one colon allowed, and it must be followed by numbers only.
  $count = substr_count($alias, ':');
  if ($count > 1) {
    form_error($form[$type][$id]['pattern'], t('You may only have one colon ":" character in each alias.'));
    return;
  }
  elseif ($count == 1) {
    $int = substr($alias, strpos($alias, ':') + 1);
    if (!is_numeric($int)) {
      form_error($form[$type][$id]['pattern'], t('A colon may only be followed by an integer indicating the proper port.'));
      return;
    }
  }

  // 4) Check that the alias doesn't contain any invalid characters.
  $check = preg_match('/^[a-z0-9\\.\\+\\-\\*\\?:]*$/', $alias);
  if ($check == 0) {
    form_error($form[$type][$id]['pattern'], t('%name contains invalid characters.', array(
      '%name' => $alias,
    )));
  }

  // 5) Check that the alias is not a direct match for a registered domain.
  $check = preg_match('/[a-z0-9\\.\\+\\-:]*$/', $alias);
  if ($check == 1) {
    $test = db_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = :alias", array(
      ':alias' => $alias,
    ))
      ->fetchField();
    if ($test > 0) {
      form_error($form[$type][$id]['pattern'], t('%name matches an existing domain record.', array(
        '%name' => $alias,
      )));
    }
  }

  // 6) Check that the alias doesn't match another alias defined for this domain.
  $replace = array(
    '.' => '\\.',
    '+' => '\\+',
    '-' => '\\-',
    '*' => '[a-z0-9\\.\\+\\-]*',
    '?' => '[a-z0-9\\.\\+\\-]?',
  );
  $_pattern = str_replace(array_keys($replace), $replace, $alias);
  foreach ($aliases as $_alias) {
    $_alias_pattern = str_replace(array_keys($replace), $replace, $_alias);
    if (preg_match('@' . $_pattern . '@i', $_alias) || preg_match('@' . $_alias_pattern . '@i', $alias)) {
      form_error($form[$type][$id]['pattern'], t('%name matches %existing. You must use unique values.', array(
        '%name' => $alias,
        '%existing' => $_alias,
      )));
      break;
    }
  }

  // 7) Check that the alias or a pattern matching the same domain name does not exist.
  $_pattern = _domain_alias_placeholders_to_sql($alias);
  if (isset($form_state['values']['domain_id']) && $_pattern != $form_state['values']['domain_id']) {
    $_alias = db_query("SELECT alias_id, domain_id, pattern FROM {domain_alias} WHERE pattern = :pattern1 OR pattern  = :pattern2", array(
      ':pattern1' => $_pattern,
      ':pattern2' => $form_state['values']['domain_id'],
    ))
      ->fetchAssoc();
  }
  else {
    $_alias = db_query("SELECT alias_id, domain_id, pattern FROM {domain_alias} WHERE pattern = :pattern", array(
      ':pattern' => $_pattern,
    ))
      ->fetchAssoc();
  }
  if (!empty($_alias)) {
    form_error($form[$type][$id]['pattern'], t('%name matches <a href="!url" title="Edit aliases for domain !id">alias #!aid</a> (%existing). You must use unique values. ', array(
      '%name' => $alias,
      '%existing' => _domain_alias_placeholders_from_sql($_alias['pattern']),
      '!url' => url('admin/build/domain/alias/' . $_alias['domain_id']),
      '!id' => $_alias['domain_id'],
      '!aid' => $_alias['alias_id'],
    )));
  }
  return $alias;
}