? 540584-domain-source.patch
? 561282-views-filtering.patch
? big.redirect.patch
Index: domain.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain.module,v
retrieving revision 1.122
diff -u -p -r1.122 domain.module
--- domain.module	11 Oct 2009 17:44:21 -0000	1.122
+++ domain.module	11 Oct 2009 18:31:17 -0000
@@ -58,6 +58,7 @@ function domain_init() {
   if (!is_array($_domain)) {
     $_domain = array();
   }
+
   // Error handling in case the module is not installed correctly.
   if (empty($_domain)) {
     $_domain['error'] = '-1';
@@ -94,11 +95,10 @@ function domain_init() {
   }
 
   // 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']);
+    domain_invalid_domain_requested();
   }
+
   // Set the site name to the domain-specific name.
   $conf['site_name'] = $_domain['sitename'];
 
@@ -1757,6 +1757,70 @@ function domain_request_name() {
 }
 
 /**
+ * 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
+ *   No return. This function issues a drupal_goto();
+ */
+function domain_invalid_domain_requested() {
+  global $_domain, $user;
+
+  // Check to see if this is a node page. These are redirected to a visible page, if possible.
+  $item = menu_get_item();
+  $nid = NULL;
+  if ($item['path'] == 'node/%') {
+    $node = node_load(arg(1));
+  }
+  if (empty($node->nid)) {
+    $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' => $user->name, '%domain' => $_domain['sitename'], '%default' => $default['sitename']), WATCHDOG_WARNING);
+    drupal_goto($default['path'] . $path);
+  }
+  // Try to find the proper redirect for a node.
+  $path = "node/$node->nid";
+  if (isset($node->domain_source)) {
+    $domain = domain_lookup($node->domain_source);
+    if ($domain['valid']) {
+      $redirect = $domain;
+    }
+    else if (!empty($node->domains)) {
+      foreach ($node->domains as $domain_id) {
+        if ($domain_id == -1) {
+          $domain_id = 0;
+        }
+        $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' => $user->name, '%domain' => $_domain['sitename'], '%redirect' => $redirect['sitename'] . $extra), WATCHDOG_WARNING);
+  drupal_goto($redirect['path'] . $path);
+}
+
+
+/**
  * Determines a domain_id matching given $_name.
  *
  * This function runs a lookup against the {domain} table matching the
