Index: domain.info
===================================================================
--- domain.info	(revision 3076)
+++ domain.info	(working copy)
@@ -1,4 +1,5 @@
 ;  $Id: domain.info,v 1.2 2007/10/04 01:35:28 agentken Exp $
+core = 6.x
 name = Domain Access
 description = A subdomain-based access control system
 package = Domain Access
\ No newline at end of file
Index: domain.install
===================================================================
--- domain.install	(revision 3076)
+++ domain.install	(working copy)
@@ -10,35 +10,56 @@
  * Implements hook_install()
  */
 function domain_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysqli':
-    case 'mysql':
-      db_query("CREATE TABLE {domain} (
-        domain_id int(11) NOT NULL AUTO_INCREMENT,
-        subdomain varchar(40) NOT NULL default '',
-        sitename varchar(80) NOT NULL default '',
-        scheme varchar(8) NOT NULL default 'http',
-        valid varchar(1) NOT NULL default 1,        
-        PRIMARY KEY  (domain_id),
-        KEY subdomain (subdomain)
-        ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-      break;
-    case 'pgsql':
-      db_query("CREATE TABLE {domain} (
-        domain_id serial,
-        subdomain varchar(40) NOT NULL default '',
-        sitename varchar(80) NOT NULL default '',
-        scheme varchar(8) NOT NULL default 'http',   
-        valid varchar(1) NOT NULL default 1,                
-        PRIMARY KEY (domain_id)
-        )");   
-      db_query("CREATE INDEX {domain}_subdomain_idx ON {domain} (subdomain)");
-      break;
-  } 
   // Create the 'all' realm for Domain Access.
+  drupal_install_schema('domain'); 
   db_query("INSERT INTO {node_access} VALUES (0, 0, 'domain_all', 1, 0, 0)");    
 }
 
+function domain_schema () {
+  $schema['domain'] = array(
+    'description' => t('The table defining domains'),
+    'fields' => array(
+      'domain_id' => array(
+	    'description' => t('the domain id'),
+	    'type' => 'serial',
+	    'not null' => TRUE,
+      ),
+      'subdomain' => array(
+	    'description' => t('the name of the subdomain'),
+	    'type' => 'varchar',
+	    'length' => 40,
+	    'not null' => TRUE,
+      ),
+      'sitename' => array(
+	    'description' => t('the name of the site'),
+	    'type' => 'varchar',
+	    'length' => 80,
+	    'not null' => TRUE,
+      ),
+      'scheme' => array(
+	    'description' => t('????'),
+	    'type' => 'varchar',
+	    'length' => 8,
+	    'not null' => TRUE,
+	    'default' => 'http',
+      ),
+      'valid' => array(
+	    'description' => t('????'),
+	    'type' => 'varchar',
+	    'length' => 1,
+	    'not null' => TRUE,
+	    'default' => '1',
+      ),
+    ),
+    'primary key' => array('domain_id'),
+    'indexes' => array(
+	  'subdomain' => array('subdomain'),
+    ),
+  );
+  
+  return $schema;
+}
+
 /**
  * Implements hook_uninstall()
  */
Index: domain.module
===================================================================
--- domain.module	(revision 3118)
+++ domain.module	(working copy)
@@ -65,73 +65,60 @@
  *
  * @ingroup drupal
  */
-function domain_menu($may_cache) {
+function domain_menu() {
   $items = array();
   $admin = user_access('administer domains');
-  if ($may_cache) {
-    $items[] = array(
-      'title' => t('Domains'),
-      'path' => 'admin/build/domain',
-      'access' => $admin,
-      'callback' => 'domain_admin',
-      'callback arguments' => array('configure'),
-      'description' => t('Settings for the Domain Access module.')
-    );    
-    $items[] = array(
-      'title' => t('Settings'),
-      'path' => 'admin/build/domain/settings',
-      'access' => $admin,
-      'type' => MENU_DEFAULT_LOCAL_TASK,
-      'callback' => 'domain_admin',
-      'callback arguments' => array('configure'),
-      'weight' => -10
-    );     
-    $items[] = array(
-      'title' => t('Domain list'),
-      'path' => 'admin/build/domain/view',
-      'access' => $admin,
-      'type' => MENU_LOCAL_TASK,      
-      'callback' => 'domain_admin',
-      'callback arguments' => array('view'),
-      'weight' => -8
-    );    
-    $items[] = array(
-      'title' => t('Create domain record'),
-      'path' => 'admin/build/domain/create',
-      'access' => $admin,
-      'type' => MENU_LOCAL_TASK,
-      'callback' => 'domain_admin',
-      'callback arguments' => array('create'),
-      'weight' => -4      
-    );    
-    $items[] = array(
-      'title' => t('Node settings'),
-      'path' => 'admin/build/domain/advanced',
-      'access' => $admin,
-      'type' => MENU_LOCAL_TASK,
-      'callback' => 'domain_admin',
-      'callback arguments' => array('advanced'),
-      'weight' => -2
-    );         
-  }  
-  else {    
-    $items[] = array(
-      'title' => t('Edit domain record'),
-      'path' => 'admin/build/domain/edit',
-      'access' => $admin,
-      'type' => MENU_CALLBACK,
-      'callback' => 'domain_admin',
-      'callback arguments' => array('edit', arg(4))      
-    );  
-    $items[] = array(
-      'title' => t('Delete domain record'),
-      'path' => 'admin/build/domain/delete',
-      'access' => $admin,
-      'type' => MENU_CALLBACK,
-      'callback' => 'domain_admin',
-      'callback arguments' => array('delete', arg(4))      
-    );    
-  }
+  $items['admin/build/domain'] = array(
+    'title' => t('Domains'),
+    'access argumemts' => array('administer domains'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('domain_configure_form'),
+    'description' => t('Settings for the Domain Access module.'),
+    'file' => 'domain_admin.inc',
+  );    
+  $items['admin/build/domain/settings'] = array(
+    'title' => t('Settings'),
+    'type' => MENU_DEFAULT_LOCAL_TASK,
+    'file' => 'domain_admin.inc',
+    'weight' => -10
+  );     
+  $items['admin/build/domain/view'] = array(
+    'title' => t('Domain list'),
+    'type' => MENU_LOCAL_TASK,      
+    'page callback' => 'domain_view',
+    'file' => 'domain_admin.inc',
+    'weight' => -8,
+  );    
+  $items['admin/build/domain/create'] = array(
+    'title' => t('Create domain record'),
+    'type' => MENU_LOCAL_TASK,
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('domain_create_form'),
+    'file' => 'domain_admin.inc',
+    'weight' => -4      
+  );    
+  $items['admin/build/domain/advanced'] = array(
+    'title' => t('Node settings'),
+    'type' => MENU_LOCAL_TASK,
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('domain_advanced_form'),
+    'file' => 'domain_admin.inc',
+    'weight' => -2
+  );             
+  $items['admin/build/domain/edit'] = array(
+    'title' => t('Edit domain record'),
+    'type' => MENU_CALLBACK,
+    'page callback' => 'drupal_get_form',
+    'file' => 'domain_admin.inc',
+    'page arguments' => array('domain_edit_form',4),      
+  );  
+  $items['admin/build/domain/delete'] = array(
+    'title' => t('Delete domain record'),
+    'type' => MENU_CALLBACK,
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('domain_delete_form',4),
+    'file' => 'domain_admin.inc',      
+  );    
   return $items;
 }
 
@@ -311,11 +298,11 @@
  *
  * @ingroup domain
  */
-function domain_admin($action, $id = NULL) {
+/*function domain_admin($action, $id = NULL) {
   include_once('domain_admin.inc');
   $func = 'domain_'. $action;
   return $func($id);
-}
+}*/
 
 /**
  * Runs a lookup against the {domain} table.  One of the two values must be present
@@ -495,10 +482,11 @@
   global $base_url;
   $_url = parse_url($base_url);
   // We need a trailing slash at the end of the path
-  if (substr($_url['path'], -1) != '/') {
-    $_url['path'] .= '/';
+  if (isset($_url['path']) && substr($_url['path'], -1) != '/') {
+    $_url['path'] .= $_url['path'].'/';
   }
-  $path = $domain['scheme'] .'://'. $domain['subdomain'] . $_url['path'];
+  $url = isset($_url['path'])?$_url['path']:'';
+  $path = $domain['scheme'] .'://'. $domain['subdomain'].$url ;
   return $path;
 }
 
@@ -550,7 +538,13 @@
       $node->editors = array();
       $node->domain_site = FALSE;
       $node->url = 'test';
-      $result = db_query("SELECT gid, realm, grant_view, grant_update, grant_delete FROM {node_access} WHERE nid = %d AND (realm = '%s' OR realm = '%s' OR realm = '%s')", $node->nid, 'domain_id', 'domain_site', 'domain_editor');
+      if(isset($node->nid)){
+      	$nid = $node->nid;
+      }
+      else{
+      	$nid = -1;
+      }
+      $result = db_query("SELECT gid, realm, grant_view, grant_update, grant_delete FROM {node_access} WHERE nid = %d AND (realm = '%s' OR realm = '%s' OR realm = '%s')", $nid, 'domain_id', 'domain_site', 'domain_editor');
       while ($data = db_fetch_object($result)) {
         // Transform the 0 to -1, since {node_access} is unsigned.
         ($data->gid == 0) ? $gid = -1 : $gid = $data->gid;
@@ -720,7 +714,8 @@
   // If the form is hidden, we are passed the 'domains_raw' variable.
   // We need to append unique values from this variable to the existing
   // stored values.  See the logic for 'view domain publshing' in domain_form_alter().
-  if (is_array($node->domains_raw)) {
+ 
+  if (isset($node->domains_raw)&&is_array($node->domains_raw)) {
     if (!isset($node->domains)) {
       $node->domains = array();
     }  
@@ -894,7 +889,7 @@
  *
  * @ingroup node
  */
-function domain_form_alter($form_id, &$form) {
+function domain_form_alter(&$form, $form_state, $form_id) {
   // If SEO is turned on, then form actions need to be absolute paths
   // to the currently active domain.  See http://drupal.org/node/196217.
   $seo = variable_get('domain_seo', 0);
@@ -908,7 +903,7 @@
   }
 
   // Apply to all node editing forms, but make sure we are not on the CCK field configuration form.
-  if ($form['#id'] == 'node-form' && !$form['#node']->cck_dummy_node_form) {
+  if ($form['#id'] == 'node-form' && !isset($form['#node']->cck_dummy_node_form)) {
     global $_domain, $user;
     // By default, the requesting domain is assigned.
     $default = array($_domain['domain_id']);
@@ -919,7 +914,7 @@
     }
 
     // Some options will be passed as hidden values, we need to run some checks on those.
-    if ($form['#node']->nid) {
+    if (isset($form['#node']->nid)) {
       $raw = $form['#node']->domains;
     }  
     else {
@@ -950,7 +945,7 @@
         '#title' => t('Send to all affiliates'),
         '#required' => FALSE,
         '#description' => t('Select if this content can be shown to all affiliates.  This setting will override the options below.'),
-        '#default_value' => ($form['#node']->nid) ? $form['#node']->domain_site : variable_get('domain_node_'. $form['#node']->type, $behavior),
+        '#default_value' => isset($form['#node']->nid) ? $form['#node']->domain_site : variable_get('domain_node_'. $form['#node']->type, $behavior),
       );    
       $form['domain']['domains'] = array(
         '#type' => 'checkboxes',
@@ -958,7 +953,7 @@
         '#options' => $options,
         '#required' => TRUE,
         '#description' => t('Select which affiliates can access this content.'),
-        '#default_value' => ($form['#node']->nid) ? $form['#node']->domains : $default,
+        '#default_value' => isset($form['#node']->nid) ? $form['#node']->domains : $default,
       );
     }
     // If the user has limited permissions, show that form or obey the settings.
@@ -1212,5 +1207,9 @@
         cache_clear_all();
       }  
       break;
-  }  
-}  
\ No newline at end of file
+  }
+  menu_rebuild();
+  cache_clear_all('*', 'cache_page', TRUE);
+  // Also clear the menu cache.
+  cache_clear_all('*','cache_menu', TRUE);
+}  
Index: domain_admin.inc
===================================================================
--- domain_admin.inc	(revision 3117)
+++ domain_admin.inc	(working copy)
@@ -89,6 +89,7 @@
     $row[] = $actions;
     $rows[] = $row;
   }
+  
   if (!empty($rows)) {
     $output .= theme_table($header, $rows);
     $output .= theme('pager', NULL, 24, 0); 
@@ -100,24 +101,14 @@
 }
 
 /**
- * Module settings and behaviors.
- *
- * @ingroup domain 
- */
-function domain_configure() {
-  // Is the module installed correctly?
-  module_invoke_all('domaininstall');
-  // Return the configuration form.
-  return drupal_get_form('domain_configure_form');
-}
-
-/**
  * FormsAPI for configuring the domain module.
  *
  * @ingroup domain
  */
-function domain_configure_form($user_submitted = FALSE) {
-  $form = array();
+function domain_configure_form() {
+  // Is the module installed correctly?
+  module_invoke_all('domaininstall');
+  
   $form['domain'] = array(
     '#type' => 'fieldset',
     '#title' => t('Default domain settings'),
@@ -221,10 +212,10 @@
     '#description' => t('If set, users with the <em>edit domain nodes</em> permission will be able to edit all nodes assigned to their domain.')
   );    
   // When turning on and off editing rules, we rebuild part of the node_access table.
-  $form['#submit']['domain_admin_editor_form'] = array();  
+  $form['#submit'][] = 'domain_configure_form_submit';  
   $form['domain_editors_status'] = array('#type' => 'value', '#value' => variable_get('domain_editors', 0));
   // Adding this submit behavior disabled the default, so we add that explictly.
-  $form['#submit']['system_settings_form_submit'] = array();
+  $form['#submit'][] = 'system_settings_form_submit';
   
   // Check to see if the node_access patch is enabled.  If it is, then
   // we need to know if more than one node_access module is running.
@@ -327,9 +318,9 @@
  *
  * @ingroup domain
  */
-function domain_admin_editor_form($form_id, $form_values) {
+function domain_configure_form_submit($form, &$form_state) {
   $clear = FALSE;
-  if ($form_values['domain_editors_status'] == 0 && $form_values['domain_editors'] == 1) {
+  if ($form_state['values']['domain_editors_status'] == 0 && $form_state['values']['domain_editors'] == 1) {
     // Apply rules
     $result = db_query("SELECT nid, gid FROM {node_access} WHERE realm = 'domain_site'");
     while ($data = db_fetch_array($result)) {
@@ -337,7 +328,7 @@
     }
     $clear = TRUE;
   }
-  else if ($form_values['domain_editors_status'] == 1 && $form_values['domain_editor'] == 0) {
+  else if ($form_state['values']['domain_editors_status'] == 1 && $form_state['values']['domain_editor'] == 0) {
     // Remove rules
     db_query("DELETE FROM {node_access} WHERE realm = 'domain_editor'");
     $clear = TRUE;
@@ -370,8 +361,8 @@
  *
  * @ingroup domain
  */
-function domain_create_form($arguments = array()) { 
-  $form = array();
+function domain_create_form($edit,$arguments = array()) { 
+ 
   // The $arguments arrray allows other modules to pass values to change the bahavior
   // of submit and validate functions.
   if (!empty($arguments)) {
@@ -423,15 +414,15 @@
  *
  * @ingroup domain
  */
-function domain_create_form_validate($form_id, $form_values) {
+function domain_create_form_validate($form, &$form_state) {
   // TODO: Make this a proper regex?
-  $subdomain = strtolower(urlencode($form_values['subdomain']));
+  $subdomain = strtolower(urlencode($form_state['values']['subdomain']));
   $check = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = '%s'", $subdomain));
   if ($check) {
     form_set_error('subdomain', t('The subdomain value must be unique.'));
   }
   $check = NULL;
-  $check = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE sitename = '%s'", $form_values['sitename']));
+  $check = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE sitename = '%s'", $form_state['values']['sitename']));
   if ($check) {
     form_set_error('sitename', t('The site name value must be unique.'));
   }  
@@ -442,35 +433,19 @@
  *
  * @ingroup domain 
  */
-function domain_create_form_submit($form_id, $form_values) {
-  db_query("INSERT INTO {domain} (subdomain, sitename, scheme, valid) VALUES ('%s', '%s', '%s', %d)", $form_values['subdomain'], $form_values['sitename'], $form_values['scheme'], $form_values['valid']);
+function domain_create_form_submit($form, &$form_state) {
+  db_query("INSERT INTO {domain} (subdomain, sitename, scheme, valid) VALUES ('%s', '%s', '%s', %d)", $form_state['values']['subdomain'], $form_state['values']['sitename'], $form_state['values']['scheme'], $form_state['values']['valid']);
   // Let other modules act.
-  $domain = domain_lookup(NULL, $form_values['subdomain']);
-  module_invoke_all('domainupdate', 'create', $domain, $form_values);
+  $domain = domain_lookup(NULL, $form_state['values']['subdomain']);
+  module_invoke_all('domainupdate', 'create', $domain, $form_state['values']);
   // The user_submitted flag is needed for Domain User.
-  if (!$form_values['domain_arguments']['user_submitted']) { 
+  if (! isset($form_state['values']['domain_arguments']['user_submitted'])) { 
     drupal_set_message(t('Domain record created.'));
     drupal_goto('admin/build/domain/view');
   }  
 }
 
 /**
- * Edit an existing domain record
- * 
- * @param $domain_id
- *  The unique id for this domain, taken from {domain}.
- *
- * @ingroup domain
- */
-function domain_edit($domain_id) {
-  $domain = domain_lookup($domain_id);
-  // We must do this from the default domain, in case the URL does not resolve.
-  $default = domain_default();
-  domain_goto($default);  
-  return drupal_get_form('domain_edit_form', $domain);
-}
-
-/**
  * FormsAPI for editing a domain record
  *
  * @param $domain
@@ -478,8 +453,13 @@
  *
  * @ingroup domain 
  */
-function domain_edit_form($domain) { 
-  $form = array();
+function domain_edit_form($edit,$domain_id) { 
+  
+  $domain = domain_lookup($domain_id);
+  // We must do this from the default domain, in case the URL does not resolve.
+  $default = domain_default();
+  domain_goto($default);  
+  
   $form['domain'] = array(
     '#type' => 'fieldset',
     '#title' => t('Edit domain record'),
@@ -529,14 +509,14 @@
  *
  * @ingroup domain 
  */
-function domain_edit_form_validate($form_id, $form_values) {
+function domain_edit_form_validate($form, &$form_state) {
   // TODO: Make this a proper regex
-  $subdomain = strtolower(urlencode($form_values['subdomain']));
-  $check = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = '%s' AND domain_id != %d", $subdomain, $form_values['domain_id']));
+  $subdomain = strtolower(urlencode($form_state['values']['subdomain']));
+  $check = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = '%s' AND domain_id != %d", $subdomain, $form_state['values']['domain_id']));
   if ($check) {
     form_set_error('subdomain', t('The subdomain value must be unique.'));
   }
-  $check2 = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE sitename = '%s' AND domain_id != %d", $form_values['sitename'], $form_values['domain_id']));
+  $check2 = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE sitename = '%s' AND domain_id != %d", $form_state['values']['sitename'], $form_state['values']['domain_id']));
   if ($check2) {
     form_set_error('sitename', t('The site name value must be unique.'));
   }  
@@ -547,29 +527,16 @@
  *
  * @ingroup domain 
  */
-function domain_edit_form_submit($form_id, $form_values) {
-  db_query("UPDATE {domain} SET subdomain = '%s', sitename = '%s', scheme = '%s', valid = %d WHERE domain_id = %d", $form_values['subdomain'], $form_values['sitename'], $form_values['scheme'], $form_values['valid'], $form_values['domain_id']);
+function domain_edit_form_submit($form, &$form_state) {
+  db_query("UPDATE {domain} SET subdomain = '%s', sitename = '%s', scheme = '%s', valid = %d WHERE domain_id = %d", $form_state['values']['subdomain'], $form_state['values']['sitename'], $form_state['values']['scheme'], $form_state['values']['valid'], $form_state['values']['domain_id']);
   // Let other modules act.
-  $domain = domain_lookup($form_values['domain_id']);
-  module_invoke_all('domainupdate', 'update', $domain, $form_values);
+  $domain = domain_lookup($form_state['values']['domain_id']);
+  module_invoke_all('domainupdate', 'update', $domain, $form_state['values']);
   drupal_set_message(t('Domain record updated.'));
   drupal_goto('admin/build/domain/view');  
 }
 
 /**
- * Delete a domain record.
- * 
- * @param $domain_id
- *  The unique id for this domain, taken from {domain}. 
- *
- * @ingroup domain 
- */
-function domain_delete($domain_id) {
-  $domain = domain_lookup($domain_id);
-  return drupal_get_form('domain_delete_form', $domain);
-}
-
-/**
  * FormsAPI
  *
  * @param $domain
@@ -577,8 +544,8 @@
  *
  * @ingroup domain 
  */
-function domain_delete_form($domain) { 
-  $form = array(); 
+function domain_delete_form($edit,$domain_id) { 
+  $domain = domain_lookup($domain_id);
   $form['domain_id'] = array('#type' => 'value', '#value' => $domain['domain_id']);  
   $form['domain'] = array('#value' => t('<p>Are you sure you wish to delete the domain record for <b>%domain</b>?</p>', array('%domain' => $domain['subdomain'])));
   $form['cancel'] = array('#value' => '<p>'. l(t('Cancel action'), 'admin/build/domain') .'<br />');
@@ -591,12 +558,12 @@
  *
  * @ingroup domain 
  */
-function domain_delete_form_submit($form_id, $form_values) {
+function domain_delete_form_submit($form, &$form_state) {
   // Run the lookup before we delete the row!
-  $domain = domain_lookup($form_values['domain_id']);
-  db_query("DELETE FROM {domain} WHERE domain_id = %d", $form_values['domain_id']);
+  $domain = domain_lookup($form_state['values']['domain_id']);
+  db_query("DELETE FROM {domain} WHERE domain_id = %d", $form_state['values']['domain_id']);
   // Let other modules act.
-  module_invoke_all('domainupdate', 'delete', $domain, $form_values);
+  module_invoke_all('domainupdate', 'delete', $domain, $form_state['values']);
   drupal_set_message(t('Domain record deleted.'));
   // Some tortured logic to return to the root domain and the domain list.
   $default = domain_default();
@@ -609,16 +576,6 @@
 }
 
 /**
- * Advanced node-type settings
- *
- * @ingroup domain 
- */
-function domain_advanced() {
-  $node_types = node_get_types('names');
-  return drupal_get_form('domain_advanced_form', $node_types);  
-}
-
-/**
  * FormsAPI
  *
  * @param $node_types
@@ -626,8 +583,8 @@
  *
  * @ingroup domain 
  */
-function domain_advanced_form($node_types) {
-  $form = array();
+function domain_advanced_form() {
+  $node_types = node_get_types('names');
   // Some editors will not have full node editing permissions.  This allows us
   // to give selected permissions for nodes within the editor's domain.
   if (variable_get('domain_editors', DOMAIN_EDITOR_RULE) == 1) {
