You are here

function domain_invalid_domain_requested in Domain Access 7.3

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

Redirect a request to an invalid domain.

We were sent here from domain_init() because the user cannot view the requested domain.

Take the user to the best valid match, which is usually the primary domain. In the case of nodes, try to find another match.

Return value

No return. This function issues a drupal_goto();

1 call to domain_invalid_domain_requested()
domain_init in ./domain.module
Implements hook_init().

File

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

Code

function domain_invalid_domain_requested() {
  global $_domain, $user;

  // Some users are allowed to view inactive domains.
  if (user_access('access inactive domains')) {
    return;
  }
  $path = drupal_get_normal_path($_GET['q']);
  $allow = array_filter(module_invoke_all('domain_invalid_request', $path, $_domain, $user));
  if (!empty($allow)) {
    return;
  }

  // Check to see if this is a node page. These are redirected to a visible page, if possible.
  $node = menu_get_object();
  if (empty($node->nid)) {
    $item = menu_get_item();
    $path = $item['href'];
    if (drupal_is_front_page($item['href'])) {
      $path = '';
    }
    $default = domain_default();

    // Log the access attempt.
    watchdog('domain', 'Invalid domain requested by %user on %domain; redirected to %default.', array(
      '%user' => isset($user->name) ? $user->name : variable_get('anonymous', t('Anonymous')),
      '%domain' => $_domain['sitename'],
      '%default' => $default['sitename'],
    ), WATCHDOG_WARNING);
    drupal_goto($default['path'] . drupal_get_path_alias($path));
  }

  // Try to find the proper redirect for a node.
  $path = "node/{$node->nid}";
  $domain = domain_get_node_match($node->nid);
  if ($domain['valid']) {
    $redirect = $domain;
  }
  elseif (!empty($node->domains)) {
    foreach ($node->domains as $domain_id) {
      $domain = domain_lookup($domain_id);
      if ($domain['valid']) {
        $redirect = $domain;
        break;
      }
    }
  }

  // If we found no node matches, just go to the home page.
  $extra = ' ' . t('node page.');
  if (empty($redirect)) {
    $redirect = domain_default();
    $path = '';
    $extra = '.';
  }

  // Log the access attempt.
  watchdog('domain', 'Invalid domain requested by %user on %domain, redirected to %redirect', array(
    '%user' => isset($user->name) ? $user->name : variable_get('anonymous', t('Anonymous')),
    '%domain' => $_domain['sitename'],
    '%redirect' => $redirect['sitename'] . $extra,
  ), WATCHDOG_WARNING);
  drupal_goto($redirect['path'] . drupal_get_path_alias($path));
}