You are here

function domain_valid_domain in Domain Access 7.3

Same name and namespace in other branches
  1. 5 domain.module \domain_valid_domain()
  2. 6.2 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.

5 calls to domain_valid_domain()
domain_form_validate in ./domain.admin.inc
Implement domain_form validate hook.
domain_requirements in ./domain.install
Checks module requirements.
domain_set_primary_domain in ./domain.module
Set the primary domain properly, if necessary.
domain_validate in ./domain.module
Validates a domain string.
drush_domain_generate_domains_validate in ./domain.drush.inc
Validate the domain generation script.

File

./domain.module, line 1335
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.');
  }
  elseif (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.
  // Note that www prefix handling must be set explicitly in the UI.
  // See http://drupal.org/node/1529316 and http://drupal.org/node/1783042
  if (variable_get('domain_www', 0) && 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', array(
      'items' => $error_list,
    ));
  }
}