 alias/hosting_alias.drush.inc |    3 +++
 alias/hosting_alias.install   |   15 +++++++++++++++
 alias/hosting_alias.module    |   28 ++++++++++++++++++++++++++--
 3 files changed, 44 insertions(+), 2 deletions(-)

diff --git alias/hosting_alias.drush.inc alias/hosting_alias.drush.inc
index af4a473..ef6f701 100644
--- alias/hosting_alias.drush.inc
+++ alias/hosting_alias.drush.inc
@@ -6,6 +6,9 @@ function drush_hosting_alias_pre_hosting_task() {
     $task =& drush_get_context('HOSTING_TASK');
     if (($task->ref->type == 'site') && (in_array($task->task_type, array('install', 'verify', 'migrate')))) {
       $task->options['aliases'] = implode(",", hosting_alias_get_aliases($task->ref));
+      $redirection = db_result(db_query("SELECT redirection FROM {hosting_site_alias} WHERE vid=%d", $task->ref->vid));
+      
+      $task->options['redirection'] = $redirection['redirection'];
     }
   }
 }
diff --git alias/hosting_alias.install alias/hosting_alias.install
index 4ee1361..479ec6a 100644
--- alias/hosting_alias.install
+++ alias/hosting_alias.install
@@ -28,6 +28,12 @@ function hosting_alias_schema() {
         'not null' => TRUE,
         'default' => 0,
       ),
+      'redirect' => array(
+        'type' => 'int',
+        'size' => 'tiny',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
     ),
     'indexes' => array(
       'vid' => array('vid'),
@@ -58,3 +64,12 @@ function hosting_alias_update_1() {
   return $ret;
 }
 
+/**
+ * Add the redirection field
+ */
+function hosting_alias_update_2() {
+  $ret = array();
+  db_add_field($ret, 'hosting_site_alias', 'redirection', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'));
+  return $ret;
+}
+
diff --git alias/hosting_alias.module alias/hosting_alias.module
index 3fcad70..202ce9b 100644
--- alias/hosting_alias.module
+++ alias/hosting_alias.module
@@ -44,6 +44,12 @@ function hosting_alias_form_alter(&$form, $form_state, $form_id) {
         '#description' => t('Your site can also be accessed through these domain names. This field requires each domain to be alias to be on it\'s own line'),
         '#default_value' => implode("\n", (array) $form['#node']->aliases)
       );
+      $form['redirection'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Redirection'),
+        '#description' => t('Aliases will all be redirected to the main domain if this is checked. Otherwise, all aliases will be accessible transparently.'),
+        '#default_value' => isset($form['#node']->redirect) ? $form['#node']->redirect : variable_get('hosting_alias_redirection', FALSE),
+      );
     }
   }
 }
@@ -82,13 +88,13 @@ function hosting_alias_insert($node) {
     if (is_array($aliases)) {
       foreach ($aliases as $alias) {
         if (($alias = trim($alias)) && !in_array($alias, $automatic)) {
-          db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic) VALUES (%d, %d, '%s', %d)", $node->vid, $node->nid, $alias, HOSTING_ALIAS_CUSTOM);
+          db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic, redirection) VALUES (%d, %d, '%s', %d, %d)", $node->vid, $node->nid, $alias, HOSTING_ALIAS_CUSTOM, $node->redirection);
         }
       }
     }
     if (sizeof($automatic)) {
       foreach ($automatic as $alias) {
-        db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic) VALUES (%d, %d, '%s', %d)", $node->vid, $node->nid, $alias, HOSTING_ALIAS_AUTOMATIC);
+        db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic, redirection) VALUES (%d, %d, '%s', %d, %d)", $node->vid, $node->nid, $alias, HOSTING_ALIAS_AUTOMATIC, $node->redirection);
       }
     }
   }
@@ -140,6 +146,11 @@ function hosting_alias_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
         }
         break;
       case 'load':
+        // XXX: this returns only the first redirection status. it
+        // works since they are all set to the same in hook_insert(),
+        // but we should return an associative alias => redirection
+        // array instead
+        $additions = db_result(db_query("SELECT redirection FROM {hosting_site_alias} WHERE vid=%d", $node->vid));
         // Only retrieves custom aliases, as they are all that can be modified.
         $additions['aliases'] = hosting_alias_get_aliases($node, HOSTING_ALIAS_CUSTOM);
         return $additions;
@@ -157,6 +168,13 @@ function hosting_alias_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
             '#value' => implode(', ', $links), 
             '#weight' => 10,
           );
+          $redirection = db_result(db_query("SELECT redirection FROM {hosting_site_alias} WHERE vid=%d", $node->vid));
+          $node->content['info']['redirection'] = array(
+            '#type' => 'item',
+            '#title' => t('Redirection'),
+            '#value' => $redirection['redirection'] ? t('Yes') : t('No'), 
+            '#weight' => 10,
+          );
         }
         break;
     }
@@ -192,6 +210,12 @@ function hosting_alias_settings() {
     '#description' => t('If a domain name starts with www., automatically create an alias for domain.com?'),
     '#default_value' => variable_get('hosting_alias_automatic_no_www', FALSE)
   );
+  $form['hosting_alias_redirection'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Use redirects instead of aliases by default'),
+    '#description' => t('By default, the aliasing system generates aliases in the webserver configuration and creates symlinks in the sites/ directory so that all domains work as the primary one. The aliasing system can also redirect instead of aliasing, which means all aliases will redirect the user to the primary one instead of delivering the site directly. This can also be controled per site.'),
+    '#default_value' => variable_get('hosting_alias_redirection', FALSE)
+  );
   return system_settings_form($form);
 }
 
