? css_gzip-478506.patch
Index: css_gzip.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/css_gzip/css_gzip.module,v
retrieving revision 1.2
diff -u -p -r1.2 css_gzip.module
--- css_gzip.module	2 May 2009 21:44:47 -0000	1.2
+++ css_gzip.module	2 Jun 2009 06:05:18 -0000
@@ -40,56 +40,63 @@ function css_gzip_form_alter(&$form, $fo
  * Minify the aggregated CSS file if CSS Optimization is turned on.
  */
 function css_gzip_preprocess_page(&$variables) {
-  // Only do this for pages that have CSS on them.
-  if (!empty($variables['styles'])) {
-    // Only process it if CSS Optimization is enabled.
-    if (variable_get('preprocess_css', FALSE)) {
-      // GZip the CSS file.
-      $htaccess = file_directory_path() .'/css/.htaccess';
-      if (variable_get('css_aggregator_gzip', FALSE)) {
-        //Get list of CSS files
-        $path = base_path() . file_directory_path();
-        $css_files=explode($path, $variables['styles']);
-        array_shift($css_files);
-        for ($i = 0; $i < count($css_files); $i++) {
-          $css_files[$i]=array_shift(explode('"', $css_files[$i]));
-        }
+  $htaccess = file_directory_path() .'/css/.htaccess';
+  if (!variable_get('css_aggregator_gzip', FALSE) || variable_get('css_aggregator_gzip_no_htaccess', FALSE)) {
+      css_gzip_remove_htaccess($htaccess);
+    }
+  if (!empty($variables['styles']) && variable_get('preprocess_css', FALSE) && variable_get('css_aggregator_gzip', FALSE)) {
+    if (!variable_get('css_aggregator_gzip_no_htaccess', FALSE) && (file_exists($htaccess)==FALSE || variable_get('css_aggregator_gzip_htaccess_size', NULL)!=filesize($htaccess))) {
+      css_gzip_create_htaccess($htaccess);
+    }
+
+    $css_files = css_gzip_file_list($variables);
+    // Create the GZip file if it doesn't already exist.
+    foreach ($css_files as $css_file) {
+      if (!file_exists(file_directory_path() . $css_file .'.gz')) {
+        file_save_data(gzencode(file_get_contents(file_directory_path() . $css_file), 9), file_directory_path() . $css_file .'.gz', FILE_EXISTS_REPLACE);
+      }
+    }
+  }
+}
 
-        // Create the GZip file if it doesn't already exist.
-        foreach ($css_files as $css_file) {
-          if (!file_exists($css_file .'.gz')) {
-            file_save_data(gzencode(file_get_contents(file_directory_path() . $css_file), 9), $css_file .'.gz', FILE_EXISTS_REPLACE);
-          }
-        }
+/**
+ * Get list of css files for this page
+ */
+function css_gzip_file_list($variables) {
+  $path = base_path() . file_directory_path();
+  $css_files=explode($path, $variables['styles']);
+  array_shift($css_files);
+  for ($i = 0; $i < count($css_files); $i++) {
+    $css_files[$i]=array_shift(explode('"', $css_files[$i]));
+  }
+  return $css_files;
+}
 
-        // Make sure the .htaccess file is active to handle GZipped CSS files.
-        if (variable_get('css_aggregator_gzip_no_htaccess', FALSE)==FALSE) {
-          $htaccess_contents = <<<EOT
+/**
+ * Generate & write the .htaccess file inside the files/css dir.
+ */
+function css_gzip_create_htaccess($htaccess) {
+  $htaccess_contents = <<<EOT
 <FilesMatch "\.(css.gz)$">
   AddEncoding x-gzip .gz
   ForceType text/css
 </FilesMatch>
 <IfModule mod_rewrite.c>
   RewriteEngine on
-  RewriteCond %{HTTP_USER_AGENT} !".*Safari.*"
   RewriteCond %{HTTP:Accept-encoding} gzip
   RewriteCond %{REQUEST_FILENAME}.gz -f
   RewriteRule ^(.*)\.css $1.css.gz [L,QSA]
 </IfModule>
 EOT;
-          if (file_exists($htaccess)==FALSE || variable_get('css_aggregator_gzip_htaccess_size', NULL)!=filesize($htaccess)) {
-            file_save_data($htaccess_contents, $htaccess, FILE_EXISTS_REPLACE);
-            variable_set('css_aggregator_gzip_htaccess_size', filesize($htaccess));
-          }
-        }
-      }
-      
-      if (variable_get('css_aggregator_gzip', FALSE)==FALSE || variable_get('css_aggregator_gzip_no_htaccess', FALSE)==TRUE) {
-        //delete .htaccess file so *.gz files do not get served.
-        if (file_exists($htaccess)) {
-          file_delete($htaccess);
-        }
-      }
-    }
+  file_save_data($htaccess_contents, $htaccess, FILE_EXISTS_REPLACE);
+  variable_set('css_aggregator_gzip_htaccess_size', filesize($htaccess));
+}
+
+/**
+ * Delete the .htaccess file inside the files/css dir.
+ */
+function css_gzip_remove_htaccess($htaccess) {
+  if (file_exists($htaccess)) {
+    file_delete($htaccess);
   }
 }
