#927406: fix handling of URLs in the CSS aggregator.

From: Damien Tournoud <damien@commerceguys.com>


---
 common.inc |   39 ++++++++++++++++++++++++++-------------
 1 files changed, 26 insertions(+), 13 deletions(-)

diff --git includes/common.inc includes/common.inc
index 935e9ea..c323b41 100644
--- includes/common.inc
+++ includes/common.inc
@@ -3327,10 +3327,19 @@ function drupal_build_css_cache($css) {
       // Only 'file' stylesheets can be aggregated.
       if ($stylesheet['type'] == 'file') {
         $contents = drupal_load_stylesheet($stylesheet['data'], TRUE);
-        // Return the path to where this CSS file originated from.
-        $base = base_path() . dirname($stylesheet['data']) . '/';
-        _drupal_build_css_path(NULL, $base);
-        // Prefix all paths within this CSS file, ignoring external and absolute paths.
+
+        // Build the base URL of this CSS file: start with the full URL.
+        $css_base_url = file_create_url($stylesheet['data']);
+        // Move to the parent.
+        $css_base_url = substr($css_base_url, 0, strrpos($css_base_url, '/'));
+        // Simplify to a relative URL if the stylesheet URL starts with the
+        // base URL of the website.
+        if (substr($css_base_url, 0, strlen($GLOBALS['base_url'])) == $GLOBALS['base_url']) {
+          $css_base_url = substr($css_base_url, strlen($GLOBALS['base_url']));
+        }
+
+        _drupal_build_css_path(NULL, $css_base_url);
+        // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
         $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents);
       }
     }
@@ -3409,7 +3418,7 @@ function _drupal_build_css_path($matches, $base = NULL) {
  * @return
  *   Contents of the stylesheet, including any resolved @import commands.
  */
-function drupal_load_stylesheet($file, $optimize = NULL) {
+function drupal_load_stylesheet($file, $optimize = NULL, $basepath = '') {
   // $_optimize does not use drupal_static as it is set by $optimize.
   static $_optimize;
   // Store optimization parameter for preg_replace_callback with nested @import loops.
@@ -3417,20 +3426,24 @@ function drupal_load_stylesheet($file, $optimize = NULL) {
     $_optimize = $optimize;
   }
 
+  // Stylesheets are relative one to each other. Start by adding a base path
+  // prefix provided by the parent stylesheet if necessary.
+  static $current_basepath;
+  if (isset($basepath)) {
+    $current_basepath = $basepath;
+  }
+  if ($current_basepath && !file_uri_scheme($file)) {
+    $file = $current_basepath . '/' . $file;
+    $current_basepath = dirname($file);
+  }
+
   $contents = '';
   if (file_exists($file)) {
     // Load the local CSS stylesheet.
     $contents = file_get_contents($file);
 
-    // Change to the current stylesheet's directory.
-    $cwd = getcwd();
-    chdir(dirname($file));
-
     // Process the stylesheet.
     $contents = drupal_load_stylesheet_content($contents, $_optimize);
-
-    // Change back directory.
-    chdir($cwd);
   }
 
   return $contents;
@@ -3506,7 +3519,7 @@ function drupal_load_stylesheet_content($contents, $optimize = FALSE) {
 function _drupal_load_stylesheet($matches) {
   $filename = $matches[1];
   // Load the imported stylesheet and replace @import commands in there as well.
-  $file = drupal_load_stylesheet($filename);
+  $file = drupal_load_stylesheet($filename, NULL, NULL);
 
   // Determine the file's directory.
   $directory = dirname($filename);
