? domain_bootstrap.inc
? domain_bootstrap.patch
? settings.inc
Index: API.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/API.php,v
retrieving revision 1.36
diff -u -p -r1.36 API.php
--- API.php	25 Jul 2008 13:53:51 -0000	1.36
+++ API.php	12 Sep 2008 16:04:30 -0000
@@ -476,3 +476,51 @@ function hook_domainignore() {
   // User login should always be from the current domain.
   return array('user_login');
 }
+
+/**
+ * Hook domain_bootstrap_lookup allows modules to modify the domain record used on the
+ * current page on bootstrap level, that is, before it is used anywhere else.
+ *
+ * This for example allows to change the domain_id matched to the current
+ * domain name before related information is retrieved during domain_init().
+ *
+ * Note: Because this function is usually called VERY early, many Drupal
+ * functions or modules won't be loaded yet.
+ *
+ * In order for this hook to work your module needs to be registered in
+ * domain/settings.inc.
+ *
+ * @param $domain
+ * An array containing current domain (host) name (used during bootstrap) and
+ * the results of lookup against {domain} table.
+ * @return
+ * An array containing at least a valid domain_id.
+ */
+function hook_domain_bootstrap_lookup($domain) {
+  // match en.example.org to default domain (id:0)
+  if ($domain['subdomain'] == 'en.example.org') {
+    $domain['domain_id'] = 0;
+  }
+
+  return $domain;
+}
+
+/**
+ * Hook hook_domain_bootstrap_full allows modules to execute code after the domain
+ * bootstrap phases which (usually) is even before drupal's hook_boot().
+ *
+ * This can be used to modify drupal's variables system or prefix database
+ * tables, as used in modules domain_conf and domain_prefix.
+ *
+ * Note: Because this function is usually called VERY early, many Drupal
+ * functions or modules won't be loaded yet.
+ *
+ * In order for this hook to work your module needs to be registered in
+ * domain/settings.inc.
+ *
+ * @param $domain
+ * An array containing current subdomain and domain_id and any other values
+ * added during domain bootstrap phase 2 (DOMAIN_BOOTSTRAP_DOMAINNAME_RESOLVE).
+ */
+function hook_domain_bootstrap_full($domain) {
+}
Index: domain.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain.module,v
retrieving revision 1.66
diff -u -p -r1.66 domain.module
--- domain.module	9 Sep 2008 15:35:22 -0000	1.66
+++ domain.module	12 Sep 2008 16:04:33 -0000
@@ -32,41 +32,34 @@ define('DOMAIN_EDITOR_RULE', FALSE);
 define('DOMAIN_SITE_GRANT', TRUE);
 
 /**
- * Implements hook_init()
+ * Implementation of hook_init().
  *
- * Inititalizes a global $_domain variable and set it based on the
- * third-level domain used to access the page.
- */
+ * Inititalizes 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'].
+*/
 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 $_domain is empty start domain bootstrap (by including settings.inc)
+  if (!isset($_domain['domain_id'])) {
+    include drupal_get_path('module', 'domain') .'/settings.inc';
+  }
 
-  // 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.
-    if (!empty($_domain['subdomain']) && $_subdomain != $_domain['subdomain']) {
-      $request = domain_get_uri($_domain);
-      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);
-    }
+  // Strip the www. off the subdomain, if required by the module settings.
+  if (variable_get('domain_www', 0) && strpos($_domain['subdomain'], 'www.') !== FALSE) {
+    $_domain['subdomain'] = str_replace('www.', '', $_domain['subdomain']);
+    $www_replaced = TRUE;
   }
 
-  // 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) {
+  // add information from domain_lookup but keep existing values (domain_id and subdomain)
+  $domain = domain_lookup($_domain['domain_id']);
+  $_domain = array_merge($domain, $_domain);
+
+  // If we have replaced 'www.' in the url, redirect to the clean domain.
+  if ($www_replaced) {
     drupal_goto(domain_get_uri($_domain));
   }
 
@@ -1222,6 +1215,9 @@ function domain_grant_all($reset= FALSE)
  * Implements hook_domaininstall()
  */
 function domain_domaininstall() {
+  // Cleanup list of bootstrap modules (remove disabled ones)
+  domain_bootstrap_modules_cleanup();
+
   // Check to see if the hook_url_alter() patch is installed.
   if (url('domain_access_test_path') != url('domain_access_path_test')) {
     drupal_set_message(t('The <em>custom_url_rewrite_outbound()</em> function is not installed.  Some features are not available. See the <em>custom_url_rewrite_outbound()</em> section of <a href="!url">INSTALL.txt</a>', array('!url' => base_path() . drupal_get_path('module', 'domain') .'/INSTALL.txt')));
Index: settings_custom_url.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/settings_custom_url.inc,v
retrieving revision 1.7
diff -u -p -r1.7 settings_custom_url.inc
--- settings_custom_url.inc	6 Jul 2008 21:16:39 -0000	1.7
+++ settings_custom_url.inc	12 Sep 2008 16:04:33 -0000
@@ -10,99 +10,3 @@
  * @ingroup domain
  */
 
-/**
- * Implements custom_url_rewrite_outbound().
- * Forces absolute paths for domains when needed.
- */
-function custom_url_rewrite_outbound(&$path, &$options, $original_path) {
-  global $_domain;
-
-  // If the domain_id is not set, then the Domain module is not active, and we cannot run this function.
-  if (isset($_domain['domain_id'])) {
-    // Set static variables for the node lookups, to remove redundant queries.
-    static $domain_site, $domain, $nodepaths;
-
-    // Check to see that this function is installed.
-    $skip = FALSE;
-    $arg = arg(0);
-    if ($arg == 'admin' && ($path == 'domain_access_test_path' || $path == 'domain_access_path_test')) {
-      $path = 'yes';
-      $skip = TRUE;
-    }
-
-    // This routine only needs to be run from certain urls or if we want to
-    // force all links to go to a single domain for SEO.
-    // See http://drupal.org/node/195366 for the background.
-    $check = domain_grant_all();
-    $seo = variable_get('domain_seo', 0);
-    // If using Domain Source, we force links to a specific domain.
-    $use_source =  module_exists('domain_source');
-
-    if (!$skip && ($check || $seo || $use_source)) {
-      // Check to see if this is a node or comment link and set $nid accordingly.
-      // We static the $nid results to make this more efficient.
-      $pattern = explode('/', $original_path);
-
-      // Advanced pattern matching, we find the node id based on token %n in the path string.
-      if (!isset($nodepaths)) {
-        $pathdata = variable_get('domain_paths', "node/%n\r\nnode/%n/edit\r\ncomment/reply/%n\r\nnode/add/book/parent/%n\r\nbook/export/html/%n\r\nnode\%n\outline");
-        $path_match = preg_replace('/(\r\n?|\n)/', '|', $pathdata);
-        $nodepaths = explode("|", $path_match);
-      }
-      $nid = FALSE;
-      foreach ($nodepaths as $match) {
-        $match_array = explode('/', $match);
-        $placeholder = array_search('%n', $match_array);
-        if (isset($pattern[$placeholder])) {
-          $match_array[$placeholder] = $pattern[$placeholder];
-          if (is_numeric($pattern[$placeholder]) && $match_array == $pattern) {
-            $nid = (int) $pattern[$placeholder];
-            break;
-          }
-        }
-      }
-      // This path has matched a node id, so it may need to be rewritten.
-      if ($nid) {
-        $root = domain_lookup(variable_get('domain_default_source', 0));
-        // Remove redundancy from the domain_site check.
-        if (!isset($domain_site[$nid])) {
-          // If this check works, we don't need to rewrite the path unless SEO rules demand it.
-          $domain_site[$nid] = db_result(db_query("SELECT grant_view FROM {node_access} WHERE nid = %d AND gid = 0 AND realm = '%s'", $nid, 'domain_site'));
-        }
-        if (!$domain_site[$nid] || $use_source) {
-          // Remove rendundancy from the domain_id check.
-          if (!isset($domain[$nid])) {
-            // The Domain Source module is optional, and allows nodes to be assigned to specific domains for the
-            // purpose of this check.
-            if ($use_source) {
-              $source = db_result(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $nid));
-              $domain[$nid] = domain_lookup($source);
-            }
-            else {
-              // Load the domain data for this node -- but only take the first match.
-              $id = db_result(db_query_range("SELECT gid FROM {node_access} WHERE nid = %d AND realm = '%s' AND grant_view = 1 ORDER BY gid", $nid, 'domain_id', 0, 1));
-              $domain[$nid] = domain_lookup($id);
-            }
-          }
-          // Can we and do we need to rewrite this path?
-          if ($domain[$nid] != -1 && $domain[$nid]['domain_id'] != $_domain['domain_id']) {
-            $options['absolute'] = TRUE;
-            // In this case, the $base_url cannot have a trailing slash
-            $options['base_url'] = rtrim($domain[$nid]['path'], '/');
-            // Domain Source trumps the seo rules below.
-            if (isset($source)) {
-              $seo = FALSE;
-            }
-          }
-        }
-        // If strict SEO rules are enabled, we set "all affiliate" links to the root domain.
-        // Only needed if we are not on the default source domain.
-        else if ($root != -1 && $seo && $_domain['domain_id'] != $root['domain_id']) {
-          $options['absolute'] = TRUE;
-          // In this case, the $base_url cannot have a trailing slash
-          $options['base_url'] = rtrim($root['path'], '/');
-        }
-      }
-    }
-  }
-}
Index: domain_conf/domain_conf.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_conf/domain_conf.install,v
retrieving revision 1.4
diff -u -p -r1.4 domain_conf.install
--- domain_conf/domain_conf.install	26 Mar 2008 02:47:31 -0000	1.4
+++ domain_conf/domain_conf.install	12 Sep 2008 16:04:33 -0000
@@ -32,3 +32,21 @@ function domain_conf_schema() {
 function domain_conf_uninstall() {
   drupal_uninstall_schema('domain_conf');
 }
+
+/**
+ * Implementation of hook_enable().
+ *
+ * Register the domain_conf with the domain module so it's loaded during domain
+ * bootstrap and can implement domain_bootstrap hooks.
+ */
+function domain_conf_enable() {
+  domain_bootstrap_register('domain_conf');
+}
+
+/**
+ * Implementation of hook_disable().
+ */
+function domain_conf_disable() {
+  domain_bootstrap_unregister('domain_conf');
+}
+
Index: domain_conf/domain_conf.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_conf/domain_conf.module,v
retrieving revision 1.25
diff -u -p -r1.25 domain_conf.module
--- domain_conf/domain_conf.module	6 Jul 2008 21:16:39 -0000	1.25
+++ domain_conf/domain_conf.module	12 Sep 2008 16:04:34 -0000
@@ -17,6 +17,51 @@
  */
 
 /**
+ * Implementation of hook_domain_bootstrap_full().
+ *
+ * Dynamic domain settings loading: Loads the settings for the current domain.
+ *
+ * This routine was in hook_init(), but there are cases where
+ * the $conf array needs to be loaded in early phases of bootstrap.
+ * In particular, these variables need to be available during variable_init().
+ *
+ * Hook hook_domain_bootstrap_full allows to execute code at domain bootstrap
+ * time which is before drupal's hook_boot() and before variable_init().
+ *
+ * In order for this to work correctly the settings.inc needs to be included
+ * in settings.php (see readme)
+ *
+ * @param $domain
+ * Array containing domain_id for current hostname
+ *
+ * @return void
+ */
+function domain_conf_domain_bootstrap_full($domain) {
+  // To work properly this function needs to be loaded before variable_init(),
+  // therefore we check that domain bootstrap was setup correctly.
+  if (!domain_settings_setup_ok()) {
+    drupal_set_message(t('The Domain module is not installed correctly. Please edit your settings.php file as described in <a href="!url">INSTALL.txt</a>', array('!url' => base_path() . drupal_get_path('module', 'domain') .'/INSTALL.txt')), 'error', FALSE);
+    return;
+  }
+  if (!is_numeric($domain['domain_id'])) {
+    drupal_set_message('Domain Configuration: domain_conf_domain_bootstrap_full, no valid id given. ', 'error');
+    return;
+  }
+  else {
+    $data = array();
+    $data = db_fetch_array(db_query("SELECT settings FROM {domain_conf} WHERE domain_id = %d", $domain['domain_id']));
+    if (!empty($data)) {
+      global $conf;
+      $settings = unserialize($data['settings']);
+      // Overwrite the $conf variables.
+      foreach ($settings as $key => $value) {
+        $conf[$key] = $value;
+      }
+    }
+  }
+}
+
+/**
  * Implements hook_init()
  */
 function domain_conf_init() {
@@ -108,16 +153,6 @@ function domain_conf_domainwarnings() {
 }
 
 /**
- * Implements hook_domaininstall()
- */
-function domain_conf_domaininstall() {
-  // If Domain Conf is being used, check to see that it is installed correctly.
-  if (module_exists('domain_conf') && !function_exists('_domain_conf_load')) {
-    drupal_set_message(t('The Domain Configuration module is not installed correctly.  Please edit your settings.php file as described in <a href="!url">INSTALL.txt</a>', array('!url' => base_path() . drupal_get_path('module', 'domain_conf') .'/INSTALL.txt')));
-  }
-}
-
-/**
  * Implements hook_domainbatch()
  */
 function domain_conf_domainbatch() {
Index: domain_prefix/domain_prefix.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_prefix/domain_prefix.install,v
retrieving revision 1.6
diff -u -p -r1.6 domain_prefix.install
--- domain_prefix/domain_prefix.install	30 Mar 2008 17:51:46 -0000	1.6
+++ domain_prefix/domain_prefix.install	12 Sep 2008 16:04:34 -0000
@@ -60,3 +60,21 @@ function domain_prefix_uninstall() {
  *
  * Developer note: the next update will be update 2.
  */
+
+/**
+ * Implementation of hook_enable().
+ *
+ * Register the domain_prefix with the domain module so it's loaded during domain
+ * bootstrap and can implement domain_bootstrap hooks.
+ */
+function domain_prefix_enable() {
+  domain_bootstrap_register('domain_prefix');
+}
+
+/**
+ * Implementation of hook_disable().
+ */
+function domain_prefix_disable() {
+  domain_bootstrap_unregister('domain_prefix');
+}
+
Index: domain_prefix/domain_prefix.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_prefix/domain_prefix.module,v
retrieving revision 1.20
diff -u -p -r1.20 domain_prefix.module
--- domain_prefix/domain_prefix.module	16 Aug 2008 16:28:26 -0000	1.20
+++ domain_prefix/domain_prefix.module	12 Sep 2008 16:04:34 -0000
@@ -26,6 +26,60 @@ define('DOMAIN_PREFIX_DROP', 8);
 define('DOMAIN_PREFIX_UPDATE', 16);
 
 /**
+ * Implementation of hook_domain_bootstrap_full().
+ *
+ * Dynamic domain settings loading: Loads the settings for the current domain.
+ *
+ * This routine was in hook_init(), but there are cases where
+ * the $conf array needs to be loaded in early phases of bootstrap.
+ * In particular, these variables need to be available during variable_init().
+ *
+ * Hook hook_domain_bootstrap_full allows to execute code at domain bootstrap
+ * time which is before drupal's hook_boot() and before variable_init().
+ *
+ * In order for this to work correctly settings.inc needs to be included
+ * in settings.php.
+ *
+ * @param $domain
+ * Array containing domain_id for current hostname
+ *
+ * @return void
+ */
+function domain_prefix_domain_bootstrap_full($domain) {
+  // To work properly this function needs to be loaded before variable_init(),
+  // therefore we check that domain bootstrap was setup correctly.
+  if (!domain_settings_setup_ok()) {
+    drupal_set_message(t('The Domain module is not installed correctly. Please edit your settings.php file as described in <a href="!url">INSTALL.txt</a>', array('!url' => base_path() . drupal_get_path('module', 'domain') .'/INSTALL.txt')), 'error', FALSE);
+    return;
+  }
+  else if (!is_numeric($domain['domain_id'])) {
+    drupal_set_message('Domain Prefix: domain_prefix_domain_bootstrap_full, no valid id given. ', 'error');
+    return;
+  }
+  else {
+    $tables = array();
+    $prefix = 'domain_'. $domain['domain_id'] .'_';
+    $result = db_query("SELECT tablename FROM {domain_prefix} WHERE domain_id = %d AND status > %d", $domain['domain_id'], 1);
+    while ($data = db_fetch_array($result)) {
+      $tables[] = $data['tablename'];
+    }
+    if (!empty($tables)) {
+      global $db_prefix;
+      $new_prefix = array();
+      // There might be global prefixing; if so, prepend the global.
+      if (is_string($db_prefix)) {
+        $new_prefix['default'] = $db_prefix;
+        $prefix = $db_prefix . $prefix;
+      }
+      foreach ($tables as $table) {
+        $new_prefix[$table] = $prefix;
+      }
+      $db_prefix = $new_prefix;
+    }
+  }
+}
+
+/**
  * Implements hook_menu()
  */
 function domain_prefix_menu() {
@@ -76,16 +130,6 @@ function domain_prefix_theme() {
 }
 
 /**
- * Implements hook_domaininstall()
- */
-function domain_prefix_domaininstall() {
-  // If Domain Prefix is being used, check to see that it is installed correctly.
-  if (module_exists('domain_prefix') && !function_exists('_domain_prefix_load')) {
-    drupal_set_message(t('The Domain Prefix module is not installed correctly.  Please edit your settings.php file as described in <a href="!url">INSTALL.txt</a>', array('!url' => base_path() . drupal_get_path('module', 'domain_prefix') .'/INSTALL.txt')));
-  }
-}
-
-/**
  * Implements hook_domainlinks()
  *
  * @param $domain
