Index: domain/domain.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain.admin.inc,v
retrieving revision 1.23
diff -u -r1.23 domain.admin.inc
--- domain/domain.admin.inc	25 Mar 2009 18:09:32 -0000	1.23
+++ domain/domain.admin.inc	8 Apr 2009 07:10:30 -0000
@@ -412,28 +412,51 @@
     '#description' => t('Must be set to "Active" for users to navigate to this domain.')
   );
   $form['submit'] = array('#type' => 'submit', '#value' => t('Create domain record'));
+  $form['#validate'][] = 'domain_form_validate';
   return $form;
 }
 
 /**
- * FormsAPI for domain_create_form()
+ * FormsAPI for domain_{create/edit}_form()
  */
-function domain_create_form_validate($form, &$form_state) {
-  // TODO: Make this a proper regex?
-  $subdomain = strtolower(urlencode($form_state['values']['subdomain']));
-  $check = 0;
-  $check = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = '%s'", $subdomain));
-  if ($check > 0) {
-    form_set_error('subdomain', t('The domain value must be unique.'));
+function domain_form_validate($form, &$form_state) {
+  $domain = strtolower(urlencode($form_state['values']['subdomain']));
+  $domain_changed = (bool) strcmp($form_state['values']['subdomain'], $form['domain']['subdomain']['#default_value']);
+  if (!domain_valid_domain($domain)) {
+    form_set_error('subdomain', t('The domain value must be valid.'));
   }
-  $check = 0;
-  $check = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE sitename = '%s'", $form_state['values']['sitename']));
-  if ($check > 0) {
-    form_set_error('sitename', t('The site name value must be unique.'));
+  if ($domain_changed && !domain_unique_domain($domain)) {
+    form_set_error('subdomain', t('The domain value must be unique.'));
   }
 }
 
 /**
+ * Validate the domain against all known valid TLDs.
+ *
+ * @param $domain
+ *   Domain to check.
+ * @return bool
+ *   TRUE if valid; FALSE if invalid.
+ */
+function domain_valid_domain($domain) {
+  $pattern = '/^([a-z0-9]([-a-z0-9]*[a-z0-9])?\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i';
+  return (bool) preg_match($pattern, $domain)) {
+}
+
+/**
+ * Validate the domain against existing domains.
+ *
+ * @param $domain
+ *   Domain to check
+ * @return bool
+ *   TRUE if unique; FALSE if duplicate.
+ */
+function domain_unique_domain($domain) {
+  $count = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = '%s'", $domain));
+  return !(bool) $count;
+}
+
+/**
  * FormsAPI for domain_create_form()
  */
 function domain_create_form_submit($form, &$form_state) {
@@ -529,28 +552,13 @@
     '#description' => t('Must be set to "Active" for users to navigate to this domain.')
   );
   $form['submit'] = array('#type' => 'submit', '#value' => t('Save domain record'));
+  $form['#validate'][] = 'domain_form_validate';
   return $form;
 }
 
 /**
  * FormsAPI for domain_edit_form()
  */
-function domain_edit_form_validate($form, &$form_state) {
-  // TODO: Make this a proper regex
-  $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 domain value must be unique.'));
-  }
-  $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.'));
-  }
-}
-
-/**
- * FormsAPI for domain_edit_form()
- */
 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.
