You are here

function domain_valid_domain in Domain Access 6.2

Same name and namespace in other branches
  1. 5 domain.module \domain_valid_domain()
  2. 7.3 domain.module \domain_valid_domain()
  3. 7.2 domain.module \domain_valid_domain()

Validate the domain against all correctable errors.

Note that we decided not to check for valid TLDs here.

Parameters

$subdomain: Domain string to check.

Return value

string Empty if valid, error message on invalid.

3 calls to domain_valid_domain()
domain_batch_validate in ./domain.module
Validate handler for hook_domainbatch()
domain_form_validate in ./domain.admin.inc
Implement domain_form validate hook.
domain_validate in ./domain.module
Validates a domain string.

File

./domain.module, line 1033
Core module functions for the Domain Access suite.

Code

function domain_valid_domain($subdomain) {
  $error_list = array();

  // Check for at least one dot or the use of 'localhost'.
  // Note that localhost can specify a port.
  $localhost_check = explode(':', $subdomain);
  if (substr_count($subdomain, '.') == 0 && $localhost_check[0] != 'localhost') {
    $error_list[] = t('At least one dot (.) is required, except when using <em>localhost</em>.');
  }

  // Check for one colon only.
  if (substr_count($subdomain, ':') > 1) {
    $error_list[] = t('Only one colon (:) is allowed.');
  }
  else {
    if (substr_count($subdomain, ':') == 1) {
      $parts = explode(':', $subdomain);
      $port = (int) $parts[1];
      if (strcmp($port, $parts[1])) {
        $error_list[] = t('The port protocol must be an integer.');
      }
    }
  }

  // The domain cannot begin or end with a period.
  if (substr($subdomain, 0, 1) == '.') {
    $error_list[] = t('The domain must not begin with a dot (.)');
  }

  // The domain cannot begin or end with a period.
  if (substr($subdomain, -1) == '.') {
    $error_list[] = t('The domain must not end with a dot (.)');
  }

  // Check for valid characters, unless using non-ASCII domains.
  if (!variable_get('domain_allow_non_ascii', FALSE)) {
    $pattern = '/^[a-z0-9\\.\\-:]*$/i';
    if (!preg_match($pattern, $subdomain)) {
      $error_list[] = t('Only alphanumeric characters, dashes, and a colon are allowed.');
    }
  }

  // Check for lower case.
  if ($subdomain != drupal_strtolower($subdomain)) {
    $error_list[] = t('Only lower-case characters are allowed.');
  }

  // Check for 'www' prefix if redirection / handling is enabled under global domain settings.
  if (variable_get('domain_www', 1) && substr($subdomain, 0, strpos($subdomain, '.')) == 'www') {
    $error_list[] = t('WWW prefix handling: Domains must be registered without the www. prefix.');
  }

  // Allow modules to alter this behavior.
  drupal_alter('domain_validate', $error_list, $subdomain);

  // Return the errors, if any.
  if (!empty($error_list)) {
    return t('The domain string is invalid for %subdomain:', array(
      '%subdomain' => $subdomain,
    )) . theme('item_list', $error_list);
  }
}