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

From: Damien Tournoud <damien@commerceguys.com>


---
 common.inc                   |   45 +++++++++++++++++++++++++++---------------
 simpletest/tests/common.test |   15 ++++++++++++++
 2 files changed, 44 insertions(+), 16 deletions(-)

diff --git includes/common.inc includes/common.inc
index bec1a27..60f11be 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_root'])) == $GLOBALS['base_root']) {
+          $css_base_url = substr($css_base_url, strlen($GLOBALS['base_root']));
+        }
+
+        _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;
   }
 
-  $contents = '';
-  if (file_exists($file)) {
-    // Load the local CSS stylesheet.
-    $contents = file_get_contents($file);
+  // 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);
 
-    // Change to the current stylesheet's directory.
-    $cwd = getcwd();
-    chdir(dirname($file));
+  // Load the CSS stylesheet, protect against invalid stylesheets by simply
+  // hiding the possible errors.
+  $contents = @file_get_contents($file);
 
+  if ($contents) {
     // 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);
diff --git modules/simpletest/tests/common.test modules/simpletest/tests/common.test
index 2c9bca8..ec8dff6 100644
--- modules/simpletest/tests/common.test
+++ modules/simpletest/tests/common.test
@@ -892,6 +892,21 @@ class CascadingStylesheetsUnitTest extends DrupalUnitTestCase {
         . 'Actual:<pre>' . $optimized_output . '</pre>'
       );
       $this->assertEqual($optimized_output, $expected, t('Optimized CSS file has expected contents (@file)', array('@file' => $file)));
+
+      // Repeat the tests by accessing the stylesheets by URL.
+      $expected = file_get_contents("$path/$file.unoptimized.css");
+      $unoptimized_output_url = drupal_load_stylesheet($GLOBALS['base_url'] . "/$path/$file.unoptimized.css", FALSE);
+      $this->verbose('Expected:<pre>' . $expected . '</pre>'
+        . 'Actual:<pre>' . $unoptimized_output . '</pre>'
+      );
+      $this->assertEqual($unoptimized_output, $expected, t('Unoptimized CSS file (loaded from an URL) has expected contents (@file)', array('@file' => $file)));
+
+      $expected = file_get_contents("$path/$file.optimized.css");
+      $optimized_output = drupal_load_stylesheet($GLOBALS['base_url'] . "/$path/$file", TRUE);
+      $this->verbose('Expected:<pre>' . $expected . '</pre>'
+        . 'Actual:<pre>' . $optimized_output . '</pre>'
+      );
+      $this->assertEqual($optimized_output, $expected, t('Optimized CSS file (loaded from an URL) has expected contents (@file)', array('@file' => $file)));
     }
   }
 }
