You are here

function domain_init in Domain Access 7.3

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

Implements hook_init().

Initializes a global $_domain variable if necessary (usually that's done in domain_bootstrap.inc) and loads information on current domain.

Also handles www stripping, checks the validity of user domains and updates $conf['site_name'].

File

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

Code

function domain_init() {
  global $_domain, $conf;
  if (!is_array($_domain)) {
    $_domain = array();
  }

  // Error handling in case the module is not installed correctly.
  if (!isset($_domain['domain_id'])) {
    $_domain = domain_default(TRUE);
    $_domain['error'] = 'bootstrap include';
  }

  // If $_domain['error'] is present, then set a message and stop.
  if (!isset($error) && isset($_domain['error'])) {
    $error = 'Domain access failed to load during phase: ' . $_domain['error'] . '. Please check your settings.php file and site configuration.';

    // Do not show on form submissions, when enabling the module.
    if (empty($_POST)) {

      // Show a warning to admin users, if enabled.
      // You may disable this warning by adding:
      // $conf['domain_hide_errors'] = TRUE;
      // to the bottom of settings.php.
      $hide = variable_get('domain_hide_errors', FALSE);
      if (user_access('administer domains') && empty($hide)) {
        drupal_set_message($error, 'error');
      }
      if (empty($hide)) {
        watchdog('domain', $error, NULL, WATCHDOG_ERROR);
      }
    }
  }

  // End of the error handling routine.
  // If coming from a node save, make sure we are on an accessible domain.
  domain_node_save_redirect();

  // Strip the www. off the domain, if required by the module settings.
  $www_replaced = FALSE;
  if (variable_get('domain_www', 0) && strpos($_domain['subdomain'], 'www.') !== FALSE) {
    $_domain['subdomain'] = str_replace('www.', '', $_domain['subdomain']);
    $www_replaced = TRUE;
  }

  // Add information from domain_lookup but keep existing values (domain_id and subdomain)
  $domain = domain_lookup($_domain['domain_id'], NULL, TRUE);
  if ($domain != -1) {
    $_domain = array_merge($domain, $_domain);
  }

  // Set the initial domain record, for later reference. See http://drupal.org/node/706490.
  domain_initial_domain($_domain);

  // If we have replaced 'www.' in the url, redirect to the clean domain.
  if ($www_replaced) {
    drupal_goto(domain_get_uri($_domain));
  }

  // For Domain User, we check the validity of accounts, so the 'valid' flag must be TRUE.
  if (empty($_domain['valid'])) {
    domain_invalid_domain_requested();
  }

  // Set the site name to the domain-specific name.
  if (variable_get('domain_sitename_override', 1)) {
    $conf['site_name'] = $_domain['sitename'];
  }
}