You are here

function domain_init in Domain Access 5

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

Implement hook_init()

Inititalizes a global $_domain variable and set it based on the third-level domain used to access the page.

File

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

Code

function domain_init() {
  global $_domain, $conf;
  $_domain = array();

  // We lower case this, since EXAMPLE.com == example.com.
  $_subdomain = strtolower(rtrim($_SERVER['HTTP_HOST']));

  // Strip the www. off the subdomain, if required by the module settings.
  $raw_domain = $_subdomain;
  if (variable_get('domain_www', 0)) {
    $_subdomain = str_replace('www.', '', $_subdomain);
  }

  // Lookup the active domain against our allowed hosts record.
  $data = db_fetch_array(db_query("SELECT domain_id FROM {domain} WHERE subdomain = '%s'", $_subdomain));

  // Get the domain data.
  $_domain = domain_lookup($data['domain_id']);

  // If return is -1, then the DNS didn't match anything, so use defaults.
  if ($_domain == -1) {
    $_domain = domain_default();

    // If the request was not for the primary domain, send the user there.
    // See http://drupal.org/node/293453 and http://drupal.org/node/321071.
    if (variable_get('domain_redirect_wildcards', TRUE) && !empty($_domain['subdomain']) && $_subdomain != $_domain['subdomain']) {
      $request = domain_get_uri($_domain);
      if (variable_get('domain_redirect_alert', TRUE)) {
        drupal_set_message(t('You have followed an incorrect link to this website.  Please update your links and bookmarks to <a href="!url">!url</a>.', array(
          '!url' => $request,
        )));
      }
      drupal_goto($request);
    }
  }

  // If we stripped the www. send the user to the proper domain.  This should only
  // happen once, on an inbound link or typed URL, so the overhead is acceptable.
  if ($raw_domain != $_subdomain) {
    drupal_goto(domain_get_uri($_domain));
  }

  // For Domain User, we check the validity of accounts, so the 'valid' flag must be TRUE.
  // If this check fails, we send users to the default site homepage.
  if (!$_domain['valid'] && !user_access('administer domains')) {
    $_domain = domain_default();
    drupal_goto($_domain['path']);
  }

  // Set the site name to the domain-specific name.
  $conf['site_name'] = $_domain['sitename'];
}