Index: multidomain.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/multidomain/multidomain.info,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 multidomain.info
--- multidomain.info	7 Mar 2007 23:20:58 -0000	1.1.2.1
+++ multidomain.info	20 Apr 2007 17:18:50 -0000
@@ -1,4 +1,4 @@
 ; $Id: multidomain.info,v 1.1.2.1 2007/03/07 23:20:58 robroy Exp $
 name = Multiple domains 
 description = Allows site administrator to configure which domains content appears on.
-dependencies = singlesignon
+dependencies = singlesignon taxonomy
Index: multidomain.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/multidomain/multidomain.module,v
retrieving revision 1.1.2.4
diff -u -r1.1.2.4 multidomain.module
--- multidomain.module	7 Mar 2007 23:20:58 -0000	1.1.2.4
+++ multidomain.module	25 Apr 2007 23:01:14 -0000
@@ -107,7 +107,56 @@
     '#description' => t("Always use this domain if no matching domains were found? This option is recommended, but it might be necessary to disable this for certain configurations of shared tables.")
   );
 
-  return system_settings_form($form);
+  $vid = variable_get('multidomain_vocabulary', '');
+  $vocabulary = taxonomy_get_vocabulary($vid);
+
+  $form['multidomain_nodes'] = array('#type' => 'checkboxes',
+    '#title' => t('Domain vocabulary node types'),
+    '#default_value' => $vocabulary->nodes,
+    '#options' => node_get_types('names'),
+    '#description' => t('A list of node types you want to be associated with domains.'),
+    '#required' => TRUE,
+  );
+
+  $form = system_settings_form($form);
+  // Don't use the default form handling.
+  unset($form['#base']);
+  return $form;
+}
+
+/**
+ * Form submit callback. Save additional data not saved by default.
+ */
+function multidomain_default_domain_settings_submit($form_id, $form_values) {
+  $op = isset($form_values['op']) ? $form_values['op'] : '';
+  if ($op != t('Reset to defaults')) {
+    $vid = variable_get('multidomain_vocabulary', '');
+    $vocabulary = (array) taxonomy_get_vocabulary($vid);
+    $vocabulary['nodes'] = array_filter($form_values['multidomain_nodes']);
+    taxonomy_save_vocabulary($vocabulary);
+    // Create or update a term associated with this domain.
+    if ($form_values['singlesignon_master_url'] != variable_get('singlesignon_master_url', 'http://')) {     
+      if ($tid = variable_get('multidomain_master_domain_tid', 0)) {
+        $term = (array) taxonomy_get_term($tid);
+      }
+      else {
+        $term = array(
+          'description' => '',
+          'vid' => $vid,
+          // Set a low weigth as this is the primary domain.
+          'weight' => -10,
+        );
+      }
+      // In either case, set the new name.
+      $term['name'] = $form_values['singlesignon_master_url'];
+      taxonomy_save_term($term);
+      variable_set('multidomain_master_domain_tid', $term['tid']);
+    }
+  }
+  // We don't want this saved as a variable.
+  unset($form_values['multidomain_nodes']);
+  // Save the remaining settings.
+  system_settings_form_submit($form_id, $form_values);
 }
 
 function multidomain_domain_delete($url) {
@@ -116,6 +165,11 @@
   return confirm_form($form, t('Do you want to delete the @domain domain?', array('@domain' => $url)), 'admin/settings/multidomain');
 }
 
+/**
+ * Menu callback. Delete a domain.
+ *
+ * %todo Delete the taxonomy term associated with this domain?
+ */
 function multidomain_domain_delete_submit($form_id, $values) {
   $domains = variable_get('multidomain_sites', array());
   unset($domains[$values['url']]);
@@ -133,6 +187,7 @@
   $edit = $domains[$url];
 
   $form = array();
+  $form['tid'] = array('#type' => 'value', '#value' => isset($edit['tid']) ? $edit['tid'] : '');
   $form['url'] = array('#type' => 'hidden', '#value' => $url);
   $form['domain'] = array(
     '#type' => 'textfield',
@@ -212,9 +267,20 @@
 
 function multidomain_domain_settings_submit($form_id, $form_values) {
   $domains = variable_get('multidomain_sites', array());
+  // If domain or weight has changed, update the term tied to this domain.
+  if (($form_values['domain'] != $form_values['url']) || ($form_values['weight'] != $domains[$form_values['url']]['weight'])) {
+    // Load the existing term.
+    $term = (array) taxonomy_get_term($form_values['tid']);
+    $term['name'] = $form_values['domain'];
+    $term['weight'] = $form_values['weight'];
+    taxonomy_save_term($term);
+    drupal_set_message(t('Domain term updated.'));
+  }
+  // If the domain has been changed, unset the previous data.
   if ($form_values['domain'] != $form_values['url']) {
     unset($domains[$form_values['url']]);
   }
+
   $domains[$form_values['domain']] = $form_values;
   uasort($domains, '_multidomain_domain_sort');
   variable_set('multidomain_sites', $domains);
@@ -275,7 +341,15 @@
 
 function multidomain_settings_submit($form_id, $form_values) {
   $domains = variable_get('multidomain_sites', array());
-  $domains[$form_values['new_domain']] = array();
+  // Create a term for the new domain.
+  $term = array(
+    'name' => $form_values['new_domain'],
+    'description' => '',
+    'vid' => variable_get('multidomain_vocabulary', ''),
+    'weight' => 0,
+  );
+  taxonomy_save_term($term);
+  $domains[$form_values['new_domain']] = array('tid' => $term['tid']);
   variable_set('multidomain_sites', $domains);
 }
 
@@ -319,6 +393,30 @@
   }
 }
 
+/**
+ * Implementation of hook_db_rewrite_sql().
+ *
+ * Limit nodes returned by domain term.
+ */
+function multidomain_db_rewrite_sql($query, $primary_table, $primary_field) {
+  static $domains;
+  $return = array();
+  if ($primary_field == 'nid') {
+    if ($domains === NULL) {
+      $domains = variable_get('multidomain_sites', array());
+    }
+    $protocol = ( $_SERVER['HTTPS'] ) ? 'https://' : 'http://' ;
+    // Determine if we are on a registered domain.
+    if ($domain = $domains[$protocol . $_SERVER['HTTP_HOST']]) {
+      if (isset($domain['tid'])) {
+        $return['join'] = 'INNER JOIN {term_node} tn ON tn.nid = '. $primary_table .'.nid';
+        $return['where'] = 'tn.tid = '. $domain['tid'];
+      }
+    }
+  }
+  return $return;
+}
+
 if (!function_exists('custom_url_rewrite')) {
   function custom_url_rewrite($type, $alias, $real_path) {
     static $default = null;
@@ -339,6 +437,13 @@
     }
     $base = $force_default ? $default : NULL;
     foreach ($domains as $domain => $info) {
+      // Match path by taxonomy.
+      // Determine if this is a node.
+      $source = $type == 'alias' ? $path : drupal_lookup_path($path);
+      $arg = explode('/', $source);
+      if ($arg[0] == 'node' && is_numeric($arg[1])) {
+        $page_match = db_num_rows(db_query('SELECT tn.* FROM {term_node} tn WHERE tn.nid = %d AND tn.tid = %d', $arg[1], $domain['tid']));
+      }
       // Match path if necessary
       if ($info['pages']) {
         if ($info['visibility'] < 2) {
@@ -382,7 +487,7 @@
       // Permanent redirect on old pages.
       #if ($protocol . $_SERVER['HTTP_HOST'] != $base) {
       #  header("HTTP/1.1 301 Moved Permanently");
-      #  header("Location: " . $base .'/'. $path);
+      #  header("Location: " . $base .'/'. (!$clean_url ? '?q=' : '') . $path;);
       #  exit();
       #}
     }
--- .svn/README.txt
+++ .svn/README.txt
@@ -0,0 +1,2 @@
+This is a Subversion working copy administrative directory.
+Visit http://subversion.tigris.org/ for more information.


--- .svn/text-base/README.TXT.svn-base+++ .svn/text-base/README.TXT.svn-base
@@ -0,0 +1,21 @@+Module : Multi Domain
+Author : Adrian Rossouw
+Email  : adrian@bryght.com
+
+This is a module which allows you to configure which domains to use for different
+parts of your site. It is also useful for switching to different protocols (ie: http / https)
+for different parts of your site (ie: put all the ecommerce or user account stuff behind https)
+
+This module was primarily developed for www.oasismag.com, because I want to host
+different types of content, on different sites, while using the same database, so that I can 
+post information between the different sites, without having to worry about syndication
+and the like.
+
+This module requires the single sign on module, to allow you to be logged into multiple domains
+at the same time.
+
+This module will be extended in the future by userdomains.module, which will allow each user
+to have their own sub domain on the site.
+
+A small patch to Drupal core is required for this module to work. It is included in the package.
+


--- .svn/text-base/multidomain.info.svn-base+++ .svn/text-base/multidomain.info.svn-base
@@ -0,0 +1,4 @@+; $Id: multidomain.info,v 1.1.2.1 2007/03/07 23:20:58 robroy Exp $
+name = Multiple domains 
+description = Allows site administrator to configure which domains content appears on.
+dependencies = singlesignon taxonomy


--- .svn/text-base/multidomain.module.svn-base+++ .svn/text-base/multidomain.module.svn-base
@@ -0,0 +1,504 @@+<?php
+// $Id: multidomain.module,v 1.1.2.4 2007/03/07 23:20:58 robroy Exp $
+
+/**
+ * Implementation of hook_help().
+ */
+function multidomain_help($section) {
+  switch ($section) {
+    case 'admin/settings/multidomain':
+      return t('The multi-domain module allows you to configure multiple domains for your content to be hosted on. You can configure several different domains, and the rules for matching them.');
+      break;
+  }
+}
+
+/**
+ * Implementation of hook_perm().
+ */
+function multidomain_perm() {
+  return array('administer multidomain');
+}
+
+/**
+ * Implementation of hook_menu().
+ */
+function multidomain_menu($may_cache = true) {
+  global $user;
+  $items = array();
+  if ($may_cache) {
+  } else {
+    $domains = variable_get('multidomain_sites', array());
+
+    $items[] = array(
+      'path' => 'admin/settings/multidomain', 'title' => t('Multiple domains'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('multidomain_settings'),
+      'access' => user_access('administer multidomain'),
+      'description' => t('Configure which domains your site will be hosted on.'),
+    );
+
+    $items[] = array(
+      'path' => 'admin/settings/multidomain/list',
+      'title' => t('List'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('multidomain_settings'),
+      'access' => user_access('administer multidomain'),
+      'description' => t('Configure which domains your site will be hosted on.'),
+      'type' => MENU_DEFAULT_LOCAL_TASK,
+      'weight' => -10
+    );
+    $items[] = array(
+      'path' => 'admin/settings/multidomain/settings',
+      'title' => t('Configure'),
+      'description' => t('The description of the menu item. It is used as title attribute and on the administration overview page.'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('multidomain_default_domain_settings'),
+      'access' => user_access('administer multidomain'),
+      'type' => MENU_LOCAL_TASK,
+    );
+    $items[] = array(
+      'path' => 'admin/settings/multidomain/settings/default',
+      'title' => t('Default domain'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('multidomain_default_domain_settings'),
+      'access' => user_access('administer multidomain'),
+      'type' => MENU_DEFAULT_LOCAL_TASK,
+    );
+
+    foreach ($domains as $url => $data) {
+      $items[] = array(
+        'path' => 'admin/settings/multidomain/settings/'. str_replace('://', '_', $url),
+        'title' => t('@domain', array('@domain' => $url)),
+        'callback' => 'drupal_get_form',
+        'callback arguments' => array('multidomain_domain_settings', $url),
+        'access' => user_access('administer multidomain'),
+        'type' => MENU_LOCAL_TASK,
+      );
+      $items[] = array(
+        'path' => 'admin/settings/multidomain/delete/'. str_replace('://', '_', $url),
+        'title' => t('@domain', array('@domain' => $url)),
+        'callback' => 'drupal_get_form',
+        'callback arguments' => array('multidomain_domain_delete', $url),
+        'access' => user_access('administer multidomain'),
+        'type' => MENU_LOCAL_TASK,
+      );
+    }
+  }
+  return $items;
+}
+
+function multidomain_default_domain_settings() {
+  $form = array();
+  $form['singlesignon_master_url'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Primary domain name'),
+    '#description' => t('The domain name for content to be hosted on, provided no other settings are found. This is the same value as the single signon primary domain.'),
+    '#default_value' => variable_get('singlesignon_master_url', 'http://'),
+    '#validate' => array('multidomain_valid_domain' => array()),
+    '#weight' => -10,
+    '#size' => 40,
+    '#maxlength' => 255,
+  );
+
+  $form['multidomain_force_default_domain'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Force default domain'),
+    '#default_value' => variable_get('multidomain_force_default_domain', true),
+    '#description' => t("Always use this domain if no matching domains were found? This option is recommended, but it might be necessary to disable this for certain configurations of shared tables.")
+  );
+
+  $vid = variable_get('multidomain_vocabulary', '');
+  $vocabulary = taxonomy_get_vocabulary($vid);
+
+  $form['multidomain_nodes'] = array('#type' => 'checkboxes',
+    '#title' => t('Domain vocabulary node types'),
+    '#default_value' => $vocabulary->nodes,
+    '#options' => node_get_types('names'),
+    '#description' => t('A list of node types you want to be associated with domains.'),
+    '#required' => TRUE,
+  );
+
+  $form = system_settings_form($form);
+  // Don't use the default form handling.
+  unset($form['#base']);
+  return $form;
+}
+
+/**
+ * Form submit callback. Save additional data not saved by default.
+ */
+function multidomain_default_domain_settings_submit($form_id, $form_values) {
+  $op = isset($form_values['op']) ? $form_values['op'] : '';
+  if ($op != t('Reset to defaults')) {
+    $vid = variable_get('multidomain_vocabulary', '');
+    $vocabulary = (array) taxonomy_get_vocabulary($vid);
+    $vocabulary['nodes'] = array_filter($form_values['multidomain_nodes']);
+    taxonomy_save_vocabulary($vocabulary);
+    // Create or update a term associated with this domain.
+    if ($form_values['singlesignon_master_url'] != variable_get('singlesignon_master_url', 'http://')) {     
+      if ($tid = variable_get('multidomain_master_domain_tid', 0)) {
+        $term = (array) taxonomy_get_term($tid);
+      }
+      else {
+        $term = array(
+          'description' => '',
+          'vid' => $vid,
+          // Set a low weigth as this is the primary domain.
+          'weight' => -10,
+        );
+      }
+      // In either case, set the new name.
+      $term['name'] = $form_values['singlesignon_master_url'];
+      taxonomy_save_term($term);
+      variable_set('multidomain_master_domain_tid', $term['tid']);
+    }
+  }
+  // We don't want this saved as a variable.
+  unset($form_values['multidomain_nodes']);
+  // Save the remaining settings.
+  system_settings_form_submit($form_id, $form_values);
+}
+
+function multidomain_domain_delete($url) {
+  $form = array();
+  $form['url'] = array('#type' => 'hidden', '#value' => $url);
+  return confirm_form($form, t('Do you want to delete the @domain domain?', array('@domain' => $url)), 'admin/settings/multidomain');
+}
+
+/**
+ * Menu callback. Delete a domain.
+ *
+ * %todo Delete the taxonomy term associated with this domain?
+ */
+function multidomain_domain_delete_submit($form_id, $values) {
+  $domains = variable_get('multidomain_sites', array());
+  unset($domains[$values['url']]);
+  variable_set('multidomain_sites', $domains);
+}
+
+function multidomain_valid_domain($element) {
+  if (!(preg_match('/^https:\/\//i', $element['#value']) || preg_match('/^http:\/\//i', $element['#value']))) {
+    form_error($element, t('Domains must be preceded by http:// or https://.'));
+  }
+}
+
+function multidomain_domain_settings($url = 'default') {
+  $domains = variable_get('multidomain_sites', array());
+  $edit = $domains[$url];
+
+  $form = array();
+  $form['tid'] = array('#type' => 'value', '#value' => isset($edit['tid']) ? $edit['tid'] : '');
+  $form['url'] = array('#type' => 'hidden', '#value' => $url);
+  $form['domain'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Domain name'),
+    '#description' => t('The domain name of this url. The http:// is required.'),
+    '#default_value' =>  $url,
+    '#size' => 40,
+    '#maxlength' => 255,
+    '#validate' => array('multidomain_valid_domain' => array()),
+  );
+
+  $form['weight'] = array(
+    '#type' => 'weight',
+    '#title' => t('Weight'),
+    '#description' => t('The weight defines which domain will get used. Domains with lower weights get matched first.'),
+    '#delta' => 10,
+    '#default_value' => $edit['weight'],
+  );
+
+  $types = node_get_types();
+  $form['page_vis_settings']['types'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Use this domain for the following types of posts'),
+    '#options' => drupal_map_assoc(array_keys($types)),
+    '#default_value' => $edit['types'],
+  );
+
+  $access = user_access('use PHP for block visibility');
+  if ($edit['visibility'] == 2 && !$access) {
+    $form['page_vis_settings'] = array();
+    $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
+    $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
+  }
+  else {
+    $options = array(t('Match every page except the listed pages.'), t('Match only the listed pages.'));
+    $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
+
+    if ($access) {
+      $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
+      $description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
+    }
+    $form['page_vis_settings']['visibility'] = array(
+      '#type' => 'radios',
+      '#title' => t('Use this domain for specific pages'),
+      '#options' => $options,
+      '#default_value' => $edit['visibility'],
+    );
+    $form['page_vis_settings']['pages'] = array(
+      '#type' => 'textarea',
+      '#title' => t('Pages'),
+      '#default_value' => $edit['pages'],
+      '#description' => $description,
+    );
+  }
+
+  $form['init'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Initialization code'),
+    '#description' => t('Code to run during initialization of pages on this domain. Remember the <em>&lt;?php</em> and <em>?></em> tags. A common task here is overriding variables using the global $conf variable.'),
+    '#default_value' => $edit['init'],
+  );
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Submit'),
+  );
+
+  return $form;
+}
+
+function multidomain_domain_settings_validate($form_id, $form_values) {
+  $domains = variable_get('multidomain_sites', array());
+  if (isset($domains[$form_values['domain']]) && ($form_values['url'] != $form_values['domain'])) {
+    form_set_error('domain', t('This domain has already been created.'));
+  }
+}
+
+function multidomain_domain_settings_submit($form_id, $form_values) {
+  $domains = variable_get('multidomain_sites', array());
+  // If domain or weight has changed, update the term tied to this domain.
+  if (($form_values['domain'] != $form_values['url']) || ($form_values['weight'] != $domains[$form_values['url']]['weight'])) {
+    // Load the existing term.
+    $term = (array) taxonomy_get_term($form_values['tid']);
+    $term['name'] = $form_values['domain'];
+    $term['weight'] = $form_values['weight'];
+    taxonomy_save_term($term);
+    drupal_set_message(t('Domain term updated.'));
+  }
+  // If the domain has been changed, unset the previous data.
+  if ($form_values['domain'] != $form_values['url']) {
+    unset($domains[$form_values['url']]);
+  }
+
+  $domains[$form_values['domain']] = $form_values;
+  uasort($domains, '_multidomain_domain_sort');
+  variable_set('multidomain_sites', $domains);
+  return 'admin/settings/multidomain';
+}
+
+/**
+ * Function used by uasort to sort domains by weight.
+ */
+function _multidomain_domain_sort($a, $b) {
+  $a_weight = (is_array($a) && isset($a['weight'])) ? $a['weight'] : 0;
+  $b_weight = (is_array($b) && isset($b['weight'])) ? $b['weight'] : 0;
+  if ($a_weight == $b_weight) {
+    return 0;
+  }
+  return ($a_weight < $b_weight) ? -1 : 1;
+}
+
+function multidomain_settings() {
+  $sites = variable_get('multidomain_sites', array());
+  $form['sites']['default']['name'] = array(
+    '#value' => t('@default <strong>(default)</strong>', array( '@default' =>  variable_get('singlesignon_master_url', t('Not defined')))),
+  );
+  $form['sites']['default']['settings'] = array(
+    '#value' => l(t('Configure'), 'admin/settings/multidomain/settings'),
+  );
+  foreach ($sites as $url => $site) {
+    $form['sites'][$url]['name'] = array(
+      '#value' => $url,
+    );
+    $links = array(
+      l('Configure', 'admin/settings/multidomain/settings/'. str_replace('://', '_', $url)),
+      l('Delete', 'admin/settings/multidomain/delete/'. str_replace('://', '_', $url)),
+    );
+    $form['sites'][$url]['settings'] = array(
+      '#value' => implode(' | ', $links),
+    );
+  }
+  $form['new']['new_domain'] = array(
+    '#type' => 'textfield',
+    '#size' => 40,
+    '#maxlength' => 255,
+    '#validate' => array('multidomain_valid_domain' => array()),
+  );
+  $form['new']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Add domain'),
+  );
+  return $form;
+}
+
+function multidomain_settings_validate($form_id, $form_values) {
+  $domains = variable_get('multidomain_sites', array());
+  if (isset($domains[$form_values['new_domain']])) {
+    form_set_error('new][new_domain', t('This domain has already been created.'));
+  }
+}
+
+function multidomain_settings_submit($form_id, $form_values) {
+  $domains = variable_get('multidomain_sites', array());
+  // Create a term for the new domain.
+  $term = array(
+    'name' => $form_values['new_domain'],
+    'description' => '',
+    'vid' => variable_get('multidomain_vocabulary', ''),
+    'weight' => 0,
+  );
+  taxonomy_save_term($term);
+  $domains[$form_values['new_domain']] = array('tid' => $term['tid']);
+  variable_set('multidomain_sites', $domains);
+}
+
+function theme_multidomain_settings($form) {
+  $rows = array();
+
+  if (sizeof(element_children($form['sites']))) {
+    foreach ($form['sites'] as $url => $elements) {
+      if (!element_child($url)) {
+        continue;
+      }
+
+      $row = array();
+      $row[] = drupal_render($form['sites'][$url]['name']);
+      $row[] = drupal_render($form['sites'][$url]['settings']);
+      $rows[] = $row;
+    }
+  }
+
+  $rows[] = array(
+    drupal_render($form['new']['new_domain']),
+    drupal_render($form['new']['submit']),
+  );
+  $form['table'] = array(
+    '#value' => theme('table', array(t('Domain'),t('Operations')), $rows),
+  );
+
+  return drupal_render($form);
+}
+
+/**
+ * Implementation of hook_init().
+ */
+function multidomain_init() {
+  $domains = variable_get('multidomain_sites', array());
+  $protocol = ( $_SERVER['HTTPS'] ) ? 'https://' : 'http://' ;
+  if ($domain = $domains[$protocol . $_SERVER['HTTP_HOST']]) {
+    if ($domain['init']) {
+      drupal_eval($domain['init']);
+    }
+  }
+}
+
+/**
+ * Implementation of hook_db_rewrite_sql().
+ *
+ * Limit nodes returned by domain term.
+ */
+function multidomain_db_rewrite_sql($query, $primary_table, $primary_field) {
+  static $domains;
+  $return = array();
+  if ($primary_field == 'nid') {
+    if ($domains === NULL) {
+      $domains = variable_get('multidomain_sites', array());
+    }
+    $protocol = ( $_SERVER['HTTPS'] ) ? 'https://' : 'http://' ;
+    // Determine if we are on a registered domain.
+    if ($domain = $domains[$protocol . $_SERVER['HTTP_HOST']]) {
+      if (isset($domain['tid'])) {
+        $return['join'] = 'INNER JOIN {term_node} tn ON tn.nid = '. $primary_table .'.nid';
+        $return['where'] = 'tn.tid = '. $domain['tid'];
+      }
+    }
+  }
+  return $return;
+}
+
+if (!function_exists('custom_url_rewrite')) {
+  function custom_url_rewrite($type, $alias, $real_path) {
+    global $clean_url;
+    // Cache the clean_url variable to improve performance.
+    if (!isset($clean_url)) {
+      $clean_url = (bool)variable_get('clean_url', '0');
+    }
+    static $default = null;
+    static $force_default = null;
+    static $domains = null;
+
+    $path = ( $alias ) ? $alias : $real_path;
+    if (strpos($path, '://') !== FALSE) {
+      return $path;
+    }
+
+    if (is_null($default)) {
+      // initialise defaults
+      $default = variable_get('singlesignon_master_url','');
+      $force_default = variable_get('multidomain_force_default_domain', false);
+      $domains = variable_get('multidomain_sites', array());
+      $protocol = $_SERVER['HTTPS'] ? 'https://' : 'http://' ;
+    }
+    $base = $force_default ? $default : NULL;
+    foreach ($domains as $domain => $info) {
+      // Match path by taxonomy.
+      // Determine if this is a node.
+      $source = $type == 'alias' ? $path : drupal_lookup_path($path);
+      $arg = explode('/', $source);
+      if ($arg[0] == 'node' && is_numeric($arg[1])) {
+        $page_match = db_num_rows(db_query('SELECT tn.* FROM {term_node} tn WHERE tn.nid = %d AND tn.tid = %d', $arg[1], $domain['tid']));
+      }
+      // Match path if necessary
+      if ($info['pages']) {
+        if ($info['visibility'] < 2) {
+          $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($info['pages'], '/')) .')$/';
+          // Compare with the internal and path alias (if any).
+          $page_match = preg_match($regexp, $real_path);
+          if ($path != $real_path) {
+            $page_match = $page_match || preg_match($regexp, $real_path);
+          }
+          // When $block->visibility has a value of 0, the block is displayed on
+          // all pages except those listed in $block->pages. When set to 1, it
+          // is displayed only on those pages listed in $block->pages.
+          $page_match = !($info['visibility'] xor $page_match);
+        }
+        else {
+          $page_match = drupal_eval($info['pages']);
+        }
+      }
+      else {
+        $page_match = FALSE;
+      }
+
+      if (sizeof($info['types'])) {
+        $parts = explode('/', $real_path);
+        if ($parts[0] == 'node' && is_numeric($parts[1])) {
+          $node = node_load($parts[1]);
+          $page_match = $page_match || in_array($node->type, array_keys($info['types']));
+        }
+      }
+      if ($page_match) {
+        $base = $domain;
+        break;
+      }
+    }
+    if ($type == 'alias') {
+      if (($protocol . $_SERVER['HTTP_HOST'] != $base) && !is_null($base) && (strpos($path, '://') === FALSE)) {
+        $path = $base .'/'. (!$clean_url ? '?q=' : '') . $path;
+      }
+    }
+    else {
+      // Permanent redirect on old pages.
+      #if ($protocol . $_SERVER['HTTP_HOST'] != $base) {
+      #  header("HTTP/1.1 301 Moved Permanently");
+      #  header("Location: " . $base .'/'. (!$clean_url ? '?q=' : '') . $path;);
+      #  exit();
+      #}
+    }
+    return $path;
+  }
+}
+else {
+  drupal_set_message(t('The multi domain module has found a custom_url_rewrite function that is already defined. This conflicts with the module\'s operations, and as such the multi domain module will not work.'), 'error');
+}


--- .svn/text-base/multidomain_alias.patch.svn-base+++ .svn/text-base/multidomain_alias.patch.svn-base
@@ -0,0 +1,25 @@+Index: includes/common.inc
+===================================================================
+RCS file: /cvs/drupal/drupal/includes/common.inc,v
+retrieving revision 1.602
+diff -u -r1.602 common.inc
+--- includes/common.inc	15 Dec 2006 07:47:08 -0000	1.602
++++ includes/common.inc	19 Dec 2006 02:22:41 -0000
+@@ -1174,11 +1174,15 @@
+   }
+ 
+   $base = ($absolute ? $base_url . '/' : base_path());
+-
+   // The special path '<front>' links to the default front page.
+   if (!empty($path) && $path != '<front>') {
+     $path = drupal_get_path_alias($path);
+-    $path = drupal_urlencode($path);
++    if (strpos($path, '://') !== FALSE) {
++      $base = '';
++    }
++    else {
++      $path = drupal_urlencode($path);
++    }
+     if (!$clean_url) {
+       if (isset($query)) {
+         return $base . $script .'?q='. $path .'&'. $query . $fragment;


--- multidomain.install+++ multidomain.install
@@ -0,0 +1,51 @@+<?php
+// $Id: $
+
+/** 
+ * Implementation of hook_install(). 
+ */
+function multidomain_install() {
+  $vid = variable_get('multidomain_vocabulary', '');
+  if (empty($vid)) {
+    // Check to see if a forum vocabulary exists
+    $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", 'multidomain'));
+    if (!$vid) {
+      // Create the forum vocabulary. Assign the vocabulary a low weight so
+      // it will appear first in node create and edit forms.
+      // Assign it all existing node types.
+      $nodes = array();
+      foreach (array_keys(node_get_types('names')) as $type) {
+        $nodes[$type] = 1;
+      }
+      $vocabulary = array('name' => 'Domain', 'multiple' => 0, 'required' => 1, 'hierarchy' => 1, 'relations' => 0, 'module' => 'forum', 'weight' => -10, 'nodes' => $nodes);
+      taxonomy_save_vocabulary($vocabulary);
+      drupal_set_message('A Domain vocabulary was created. To assign content to a domain, add it to this vocabulary.');
+      $vid = $vocabulary['vid'];
+    }
+    variable_set('multidomain_vocabulary', $vid);
+  } 
+}
+
+/** 
+ * Create and populate a vocabulary. 
+ */
+function multidomain_update_1() {
+  // Initialize.
+  multidomain_install();
+  // Create a term for each existing domain.
+  $vid = variable_get('multidomain_vocabulary', '');
+  $domains = variable_get('multidomain_sites', array());
+  foreach ($domains as $domain => $settings) {
+    $term = array(
+      'name' => $domain,
+      'description' => '',
+      'vid' => $vid,
+      'weight' => $settings['weight'],
+    );
+    taxonomy_save_term($term);
+    // Set the term id for this domain.
+    $domains[$domain]['tid'] = $term['tid'];
+  }
+  variable_set('multidomain_sites', $domains);
+  return array();
+}


--- multidomain.install+++ multidomain.install
@@ -0,0 +1,51 @@+<?php
+// $Id: $
+
+/** 
+ * Implementation of hook_install(). 
+ */
+function multidomain_install() {
+  $vid = variable_get('multidomain_vocabulary', '');
+  if (empty($vid)) {
+    // Check to see if a forum vocabulary exists
+    $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", 'multidomain'));
+    if (!$vid) {
+      // Create the forum vocabulary. Assign the vocabulary a low weight so
+      // it will appear first in node create and edit forms.
+      // Assign it all existing node types.
+      $nodes = array();
+      foreach (array_keys(node_get_types('names')) as $type) {
+        $nodes[$type] = 1;
+      }
+      $vocabulary = array('name' => 'Domain', 'multiple' => 0, 'required' => 1, 'hierarchy' => 1, 'relations' => 0, 'module' => 'forum', 'weight' => -10, 'nodes' => $nodes);
+      taxonomy_save_vocabulary($vocabulary);
+      drupal_set_message('A Domain vocabulary was created. To assign content to a domain, add it to this vocabulary.');
+      $vid = $vocabulary['vid'];
+    }
+    variable_set('multidomain_vocabulary', $vid);
+  } 
+}
+
+/** 
+ * Create and populate a vocabulary. 
+ */
+function multidomain_update_1() {
+  // Initialize.
+  multidomain_install();
+  // Create a term for each existing domain.
+  $vid = variable_get('multidomain_vocabulary', '');
+  $domains = variable_get('multidomain_sites', array());
+  foreach ($domains as $domain => $settings) {
+    $term = array(
+      'name' => $domain,
+      'description' => '',
+      'vid' => $vid,
+      'weight' => $settings['weight'],
+    );
+    taxonomy_save_term($term);
+    // Set the term id for this domain.
+    $domains[$domain]['tid'] = $term['tid'];
+  }
+  variable_set('multidomain_sites', $domains);
+  return array();
+}
