=== modified file 'includes/database.mysql-common.inc'
--- includes/database.mysql-common.inc	2007-10-02 16:15:56 +0000
+++ includes/database.mysql-common.inc	2007-10-10 17:00:51 +0000
@@ -530,3 +530,27 @@ function db_change_field(&$ret, $table, 
 function db_last_insert_id($table, $field) {
   return db_result(db_query('SELECT LAST_INSERT_ID()'));
 }
+
+/**
+ * Prepare two strings for comparing case insensitively by the database.
+ *
+ * @param $str1
+ *   The first string.
+ * @param $operator
+ *   Typically = or LIKE.
+ * @param $str2
+ *   The second string.
+ * @return
+ *   Example: db_stricmp('foo', '=', "'%s'") returns foo = '%s' as mysql is
+ *   case insensitive.
+ */
+function db_stricmp($str1, $operator = '=', $str2 = "'%s'") {
+  $mysql_case_sensitive = variable_get('mysql_case_sensitive', NULL);
+  if (!isset($mysql_case_sensitive)) {
+    $fields = db_fetch_array(db_query('SHOW FULL COLUMNS FROM {system}'));
+    $collation = $fields['Collation'];
+    $mysql_case_sensitive = $collation[strlen($collation) - 1] == 's';
+    variable_set('mysql_case_sensitive', $mysql_case_sensitive);
+  }
+  return $mysql_case_sensitive ? "LOWER($str1) $operator LOWER($str2)" : "$str1 $operator $str2";
+}

=== modified file 'includes/database.pgsql.inc'
--- includes/database.pgsql.inc	2007-10-02 16:15:56 +0000
+++ includes/database.pgsql.inc	2007-10-10 16:52:25 +0000
@@ -424,6 +424,22 @@ function db_check_setup() {
 }
 
 /**
+ * Prepare two strings for comparing case insensitively by the database.
+ *
+ * @param $str1
+ *   The first string.
+ * @param $operator
+ *   Typically = or LIKE.
+ * @param $str2
+ *   The second string.
+ * @return
+ *   Example: db_stricmp('foo', '=', "'%s'") returns LOWER(foo) = LOWER('%s')
+ */
+function db_stricmp($str1, $operator = '=', $str2 = "'%s'") {
+  return $operator == 'LIKE' ? "$str1 ILIKE $str2" : "LOWER($str1) $operator LOWER($str2)";
+}
+
+/**
  * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to
  * the SELECT list entry of the given query and the resulting query is returned.
  * This function only applies the wrapper if a DISTINCT doesn't already exist in

=== modified file 'modules/comment/comment.module'
--- modules/comment/comment.module	2007-10-08 14:22:31 +0000
+++ modules/comment/comment.module	2007-10-10 16:42:17 +0000
@@ -1441,9 +1441,9 @@ function comment_validate($edit) {
   // Check validity of name, mail and homepage (if given)
   if (!$user->uid || isset($edit['is_anonymous'])) {
     $node = node_load($edit['nid']);
-    if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) > COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
+    if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
       if ($edit['name']) {
-        $taken = db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE LOWER(name) = '%s'", $edit['name']));
+        $taken = db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE ". db_stricmp('name'), $edit['name']));
 
         if ($taken != 0) {
           form_set_error('name', t('The name you used belongs to a registered user.'));

=== modified file 'modules/profile/profile.module'
--- modules/profile/profile.module	2007-10-05 13:26:53 +0000
+++ modules/profile/profile.module	2007-10-10 16:42:17 +0000
@@ -765,7 +765,7 @@ function profile_form_profile($edit, $us
 function profile_autocomplete($field, $string) {
   $matches = array();
   if (db_result(db_query("SELECT COUNT(*) FROM {profile_fields} WHERE fid = %d AND autocomplete = 1", $field))) {
-    $result = db_query_range("SELECT value FROM {profile_values} WHERE fid = %d AND LOWER(value) LIKE LOWER('%s%%') GROUP BY value ORDER BY value ASC", $field, $string, 0, 10);
+    $result = db_query_range('SELECT value FROM {profile_values} WHERE fid = %d AND '. db_stricmp('value', 'LIKE', "'%s%%'") .' GROUP BY value ORDER BY value ASC', $field, $string, 0, 10);
     while ($data = db_fetch_object($result)) {
       $matches[$data->value] = check_plain($data->value);
     }
@@ -901,7 +901,7 @@ function _profile_get_fields($category, 
   }
   else {
     // Use LOWER('%s') instead of PHP's strtolower() to avoid UTF-8 conversion issues.
-    $filters[] = "LOWER(category) = LOWER('%s')";
+    $filters[] = db_stricmp('category');
     $args[] = $category;
   }
   if (!user_access('administer users')) {
@@ -918,7 +918,7 @@ function _profile_get_fields($category, 
  */
 function profile_admin_settings_autocomplete($string) {
   $matches = array();
-  $result = db_query_range("SELECT category FROM {profile_fields} WHERE LOWER(category) LIKE LOWER('%s%%')", $string, 0, 10);
+  $result = db_query_range('SELECT category FROM {profile_fields} WHERE '. db_stricmp('category', 'LIKE', "'%s%%'"), $string, 0, 10);
   while ($data = db_fetch_object($result)) {
     $matches[$data->category] = check_plain($data->category);
   }

=== modified file 'modules/statistics/statistics.admin.inc'
--- modules/statistics/statistics.admin.inc	2007-08-23 16:34:44 +0000
+++ modules/statistics/statistics.admin.inc	2007-10-10 16:42:17 +0000
@@ -81,7 +81,7 @@ function statistics_top_visitors() {
     array('data' => t('Operations'))
   );
 
-  $sql = "SELECT COUNT(a.uid) AS hits, a.uid, u.name, a.hostname, SUM(a.timer) AS total, ac.aid FROM {accesslog} a LEFT JOIN {access} ac ON ac.type = 'host' AND LOWER(a.hostname) LIKE (ac.mask) LEFT JOIN {users} u ON a.uid = u.uid GROUP BY a.hostname, a.uid, u.name, ac.aid". tablesort_sql($header);
+  $sql = "SELECT COUNT(a.uid) AS hits, a.uid, u.name, a.hostname, SUM(a.timer) AS total, ac.aid FROM {accesslog} a LEFT JOIN {access} ac ON ac.type = 'host' AND ". db_stricmp('a.hostname', 'LIKE', 'ac.mask') .' LEFT JOIN {users} u ON a.uid = u.uid GROUP BY a.hostname, a.uid, u.name, ac.aid'. tablesort_sql($header);
   $sql_cnt = "SELECT COUNT(DISTINCT(CONCAT(uid, hostname))) FROM {accesslog}";
   $result = pager_query($sql, 30, 0, $sql_cnt);
 

=== modified file 'modules/taxonomy/taxonomy.module'
--- modules/taxonomy/taxonomy.module	2007-10-02 16:15:56 +0000
+++ modules/taxonomy/taxonomy.module	2007-10-10 16:42:17 +0000
@@ -830,7 +830,7 @@ function _taxonomy_term_children($tid) {
  *   An array of matching term objects.
  */
 function taxonomy_get_term_by_name($name) {
-  $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.* FROM {term_data} t WHERE LOWER(t.name) LIKE LOWER('%s')", 't', 'tid'), trim($name));
+  $db_result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t WHERE '. db_stricmp('t.name', 'LIKE'), 't', 'tid'), trim($name));
   $result = array();
   while ($term = db_fetch_object($db_result)) {
     $result[] = $term;

=== modified file 'modules/taxonomy/taxonomy.pages.inc'
--- modules/taxonomy/taxonomy.pages.inc	2007-09-27 16:52:00 +0000
+++ modules/taxonomy/taxonomy.pages.inc	2007-10-10 16:42:17 +0000
@@ -80,7 +80,7 @@ function taxonomy_autocomplete($vid, $st
   $last_string = trim(array_pop($array));
   $matches = array();
   if ($last_string != '') {
-    $result = db_query_range(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE t.vid = %d AND LOWER(t.name) LIKE LOWER('%%%s%%')", 't', 'tid'), $vid, $last_string, 0, 10);
+    $result = db_query_range(db_rewrite_sql('SELECT t.tid, t.name FROM {term_data} t WHERE t.vid = %d AND '. db_stricmp('t.name', 'LIKE', "'%%%s%%'"), 't', 'tid'), $vid, $last_string, 0, 10);
 
     $prefix = count($array) ? implode(', ', $array) .', ' : '';
 

=== modified file 'modules/user/user.module'
--- modules/user/user.module	2007-10-07 19:27:40 +0000
+++ modules/user/user.module	2007-10-10 16:42:17 +0000
@@ -153,7 +153,7 @@ function user_load($array = array()) {
       $params[] = md5($value);
     }
     else {
-      $query[]= "LOWER($key) = LOWER('%s')";
+      $query[]= db_stricmp($key);
       $params[] = $value;
     }
   }
@@ -471,7 +471,7 @@ function user_access($string, $account =
  * @return boolean TRUE for blocked users, FALSE for active
  */
 function user_is_blocked($name) {
-  $deny  = db_fetch_object(db_query("SELECT name FROM {users} WHERE status = 0 AND name = LOWER('%s')", $name));
+  $deny  = db_fetch_object(db_query('SELECT name FROM {users} WHERE status = 0 AND '. db_stricmp('name'), $name));
 
   return $deny;
 }
@@ -528,13 +528,13 @@ function user_search($op = 'search', $ke
         $keys = preg_replace('!\*+!', '%', $keys);
         if (user_access('administer users')) {
           // Administrators can also search in the otherwise private email field.
-          $result = pager_query("SELECT name, uid, mail FROM {users} WHERE LOWER(name) LIKE LOWER('%%%s%%') OR LOWER(mail) LIKE LOWER('%%%s%%')", 15, 0, NULL, $keys, $keys);
+          $result = pager_query('SELECT name, uid, mail FROM {users} WHERE '. db_stricmp('name') .' LIKE '. db_stricmp("'%%%s%%'") .' OR '. db_stricmp('mail', 'LIKE', "'%%%s%%'"), 15, 0, NULL, $keys, $keys);
           while ($account = db_fetch_object($result)) {
             $find[] = array('title' => $account->name .' ('. $account->mail .')', 'link' => url('user/'. $account->uid, array('absolute' => TRUE)));
           }
         }
         else {
-          $result = pager_query("SELECT name, uid FROM {users} WHERE LOWER(name) LIKE LOWER('%%%s%%')", 15, 0, NULL, $keys);
+          $result = pager_query('SELECT name, uid FROM {users} WHERE '. db_stricmp('name', 'LIKE', "'%%%s%%'"), 15, 0, NULL, $keys);
           while ($account = db_fetch_object($result)) {
             $find[] = array('title' => $account->name, 'link' => url('user/'. $account->uid, array('absolute' => TRUE)));
           }
@@ -1361,7 +1361,7 @@ function _user_edit_validate($uid, &$edi
     if ($error = user_validate_name($edit['name'])) {
       form_set_error('name', $error);
     }
-    else if (db_result(db_query("SELECT COUNT(*) FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $uid, $edit['name'])) > 0) {
+    else if (db_result(db_query('SELECT COUNT(*) FROM {users} WHERE uid != %d AND '. db_stricmp('name'), $uid, $edit['name'])) > 0) {
       form_set_error('name', t('The name %name is already taken.', array('%name' => $edit['name'])));
     }
     else if (drupal_is_denied('user', $edit['name'])) {
@@ -1373,7 +1373,7 @@ function _user_edit_validate($uid, &$edi
   if ($error = user_validate_mail($edit['mail'])) {
     form_set_error('mail', $error);
   }
-  else if (db_result(db_query("SELECT COUNT(*) FROM {users} WHERE uid != %d AND LOWER(mail) = LOWER('%s')", $uid, $edit['mail'])) > 0) {
+  else if (db_result(db_query('SELECT COUNT(*) FROM {users} WHERE uid != %d AND '. db_stricmp('mail'), $uid, $edit['mail'])) > 0) {
     form_set_error('mail', t('The e-mail address %email is already registered. <a href="@password">Have you forgotten your password?</a>', array('%email' => $edit['mail'], '@password' => url('user/password'))));
   }
   else if (drupal_is_denied('mail', $edit['mail'])) {

=== modified file 'modules/user/user.pages.inc'
--- modules/user/user.pages.inc	2007-09-10 13:14:38 +0000
+++ modules/user/user.pages.inc	2007-10-10 16:42:17 +0000
@@ -12,7 +12,7 @@
 function user_autocomplete($string = '') {
   $matches = array();
   if ($string) {
-    $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $string, 0, 10);
+    $result = db_query_range('SELECT name FROM {users} WHERE '. db_stricmp('name', 'LIKE', "'%s%%'"), $string, 0, 10);
     while ($user = db_fetch_object($result)) {
       $matches[$user->name] = check_plain($user->name);
     }

