? custom_url_rewrite_outbound.patch
Index: common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.611.2.24
diff -u -p -r1.611.2.24 common.inc
--- common.inc	13 May 2009 17:10:36 -0000	1.611.2.24
+++ common.inc	16 Jun 2009 01:57:56 -0000
@@ -1307,35 +1307,48 @@ function url($path = NULL, $query = NULL
     $clean_url = (bool)variable_get('clean_url', '0');
   }
 
-  $base = ($absolute ? $base_url . '/' : base_path());
+  // Preserve the original path before aliasing.
+  $original_path = $path;
+  // Prevent the global $base_url from being altered.
+  $current_base_url = $base_url;
 
   // The special path '<front>' links to the default front page.
-  if (!empty($path) && $path != '<front>') {
+  if ($path == '<front>') {
+    $path = '';
+  }
+  elseif (!empty($path)) {
     $path = drupal_get_path_alias($path);
-    $path = drupal_urlencode($path);
-    if (!$clean_url) {
-      if (isset($query)) {
-        return $base . $script .'?q='. $path .'&'. $query . $fragment;
-      }
-      else {
-        return $base . $script .'?q='. $path . $fragment;
-      }
+  }
+
+  if (function_exists('custom_url_rewrite_outbound')) {
+    // Modules may alter outbound links by reference.
+    custom_url_rewrite_outbound($path, $query, $fragment, $absolute, $current_base_url, $original_path);
+  }
+
+  $base = ($absolute ? $current_base_url . '/' : base_path());
+  $path = drupal_urlencode($path);
+  
+  if (!$clean_url) {
+    $variables = array();
+    if (!empty($path)) {
+      $variables[] = 'q='. $path;
+    }
+    if (isset($query)) {
+      $variables[] = $query;
+    }
+    if ($query = join('&', $variables)) {
+      return $base . $script .'?'. $query . $fragment;
     }
     else {
-      if (isset($query)) {
-        return $base . $path .'?'. $query . $fragment;
-      }
-      else {
-        return $base . $path . $fragment;
-      }
+      return $base . $fragment;
     }
   }
   else {
     if (isset($query)) {
-      return $base . $script .'?'. $query . $fragment;
+      return $base . $path .'?'. $query . $fragment;
     }
     else {
-      return $base . $fragment;
+      return $base . $path . $fragment;
     }
   }
 }
