=== modified file 'includes/database.inc'
--- includes/database.inc	2008-01-04 09:31:48 +0000
+++ includes/database.inc	2008-01-07 16:03:34 +0000
@@ -315,34 +315,43 @@ function db_rewrite_sql($query, $primary
   }
 
   if (!empty($where) || !empty($join)) {
-    if (!empty($where)) {
-      $new = "WHERE $where ";
-    }
-    $new = " $join $new";
-    if (strpos($query, 'WHERE')) {
-      $query = str_replace('WHERE', $new .'AND (', $query);
-      $insert = ') ';
-    }
-    else {
-      $insert = $new;
-    }
-    if (strpos($query, 'GROUP')) {
-      $replace = 'GROUP';
-    }
-    elseif (strpos($query, 'HAVING')) {
-      $replace = 'HAVING';
-    }
-    elseif (strpos($query, 'ORDER')) {
-      $replace = 'ORDER';
-    }
-    elseif (strpos($query, 'LIMIT')) {
-      $replace = 'LIMIT';
+    $pattern = '{
+      # Beginning of the string
+      ^
+      ((?P<anonymous_view>
+        # Everything within this set of parentheses is named "anonymous view"
+        (?:
+          [^()]++                   # anything not parentheses
+        |
+          \( (?P>anonymous_view) \)          # an open parenthesis, more "anonymous view" and finally a close parenthesis.
+        )*
+      )[^()]+WHERE)
+    }x';
+    preg_match($pattern, $query, $matches);
+    if ($where) {
+      $n = strlen($matches[1]);
+      $second_part = substr($query, $n);
+      $first_part = substr($matches[1], 0, $n - 5) ." $join WHERE $where AND ( ";
+      $haystack_reverse = strrev($second_part);
+      // No need to use strrev on the needle, we supply GROUP, ORDER, LIMIT
+      // reversed.
+      foreach (array('PUORG', 'REDRO', 'TIMIL') as $needle_reverse) {
+        $pos = strpos($haystack_reverse, $needle_reverse);
+        if ($pos !== FALSE) {
+          // All needles are five characters long.
+          $pos += 5;
+          break;
+        }
+      }
+      if ($pos === FALSE) {
+        $query = $first_part . $second_part .')';
+      }
+      else {
+        $query = $first_part . substr($second_part, 0, -$pos) .')'. substr($second_part, -$pos);
+      }
     }
     else {
-      $query .= $insert;
-    }
-    if (isset($replace)) {
-      $query = str_replace($replace, $insert . $replace, $query);
+      $query = $matches[1] ." $join ". substr($query, strlen($matches[1]));
     }
   }
 

