You are here

function domain_valid_domain in Domain Access 5

Same name and namespace in other branches
  1. 6.2 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.

1 call to domain_valid_domain()
domain_validate in ./domain.module
Validates a domain string.

File

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

Code

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

  // Check for at least one dot.
  if (substr_count($subdomain, '.') == 0) {
    $error_list[] = t('At least one dot (.) is required.');
  }

  // 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
  $pattern = '/^[a-z0-9\\.\\-:]*$/i';
  if (!preg_match($pattern, $subdomain)) {
    $error_list[] = t('Only alphanumeric characters, dashes, and a colon are allowed.');
  }
  if (!empty($error_list)) {
    return t('The domain string is invalid:') . theme('item_list', $error_list);
  }
}