Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.840 diff -u -u -p -r1.840 common.inc --- includes/common.inc 26 Dec 2008 21:01:57 -0000 1.840 +++ includes/common.inc 26 Dec 2008 23:01:54 -0000 @@ -2189,6 +2189,11 @@ function drupal_build_css_cache($types, // Create the CSS file. file_unmanaged_save_data($data, $csspath . '/' . $filename, FILE_EXISTS_REPLACE); + if (function_exists('gzencode')) { + drupal_write_gz_htaccess($csspath, 'css', 'text/css'); + // Create the gzip compressed version of the CSS file + file_unmanaged_save_data(gzencode($data, 9), $csspath . '/' . $filename . '.gz', FILE_EXISTS_REPLACE); + } } return $csspath . '/' . $filename; } @@ -2739,6 +2744,11 @@ function drupal_build_js_cache($files, $ // Create the JS file. file_unmanaged_save_data($contents, $jspath . '/' . $filename, FILE_EXISTS_REPLACE); + if (function_exists('gzencode')) { + drupal_write_gz_htaccess($jspath, 'js', 'application/x-javascript'); + // Create the gzip compressed version of the JS file + file_unmanaged_save_data(gzencode($contents, 9), $jspath . '/' . $filename . '.gz', FILE_EXISTS_REPLACE); + } } return $jspath . '/' . $filename; @@ -2753,6 +2763,42 @@ function drupal_clear_js_cache() { } /** + * Writes a .htaccess file in a directory enabling Apache to serve gzipped + * versions of files in that directory if they exists and the browser accepts + * gzip encoded files. + * + * @param $directory + * Directory the .htaccess file should be placed in. + * @param $extension + * File extension that encoding should be set for and gzip versions + * transparently rewritten to. + * @param $mimetype + * The MIME type that Apache should send with the files. + */ +function drupal_write_gz_htaccess($directory, $extension, $mimetype) { + if (!is_file("$directory/.htaccess")) { + $htaccess_lines = "\n"; + $htaccess_lines .= " \n"; + $htaccess_lines .= " # If serving an appropriate file set the encoding and MIME type correctly.\n"; + $htaccess_lines .= " AddEncoding gzip .$extension\n"; + $htaccess_lines .= " ForceType $mimetype\n"; + $htaccess_lines .= " \n"; + $htaccess_lines .= " RewriteEngine on\n"; + $htaccess_lines .= " # If the browser accepts gzip, and a .gz version of the requested file exists\n"; + $htaccess_lines .= " # then we seamlessly serve that version of the file instead.\n"; + $htaccess_lines .= " RewriteCond %{HTTP:Accept-encoding} gzip\n"; + $htaccess_lines .= " RewriteCond %{REQUEST_FILENAME}.gz -f\n"; + $htaccess_lines .= " RewriteRule ^(.*)\.$extension $1.$extension.gz [L,QSA]\n"; + $htaccess_lines .= ""; + + if (($fp = fopen("$directory/.htaccess", 'w')) && fputs($fp, $htaccess_lines)) { + fclose($fp); + chmod($directory . '/.htaccess', 0664); + } + } +} + +/** * Converts a PHP variable into its Javascript equivalent. * * We use HTML-safe strings, i.e. with <, > and & escaped.