Skip to content

Commit

Permalink
MDL-21241 theme CSS and JS gzip compression + some more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jan 5, 2010
1 parent 085f71b commit 7c986f0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 14 deletions.
36 changes: 36 additions & 0 deletions lib/configonlylib.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,40 @@ function min_clean_param($value, $type) {
}

return $value;
}

/**
* This method tries to enable output compression if possible.
* This function must be called before any output or headers.
*
* (IE6 is not supported at all.)
*
* @return boolean, true if compression enabled
*/
function min_enable_zlib_compression() {

if (headers_sent()) {
return false;
}

// zlib.output_compression is preferred over ob_gzhandler()
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
$agent = $_SERVER['HTTP_USER_AGENT'];
// try to detect IE6 and prevent gzip because it is extremely buggy browser
$parts = explode(';', $agent);
if (isset($parts[1])) {
$parts = explode(' ', trim($parts[1]));
if (count($parts) > 1) {
if ($parts[0] === 'MSIE' and (float)$string[1] < 7) {
@ini_set('zlib.output_compression', '0');
return false;
}
}
}
}

@ini_set('output_handler', '');
@ini_set('zlib.output_compression', 'on');

return true;
}
4 changes: 2 additions & 2 deletions theme/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ function send_cached_image($imagepath, $rev) {
header('Content-Type: '.$mimetype);
header('Content-Length: '.filesize($imagepath));

while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
// no need to gzip already compressed images ;-)

readfile($imagepath);
die;
}
Expand All @@ -170,7 +171,6 @@ function send_uncached_image($imagepath) {
header('Content-Type: '.$mimetype);
header('Content-Length: '.filesize($imagepath));

while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
readfile($imagepath);
die;
}
Expand Down
6 changes: 3 additions & 3 deletions theme/javascripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ function send_cached_js($jspath) {
header('Pragma: ');
header('Accept-Ranges: none');
header('Content-Type: application/x-javascript');
header('Content-Length: '.filesize($jspath));
if (!min_enable_zlib_compression()) {
header('Content-Length: '.filesize($jspath));
}

while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
readfile($jspath);
die;
}
Expand All @@ -105,7 +106,6 @@ function send_uncached_js($js) {
header('Content-Type: application/x-javascript');
header('Content-Length: '.strlen($js));

while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
echo $js;
die;
}
10 changes: 6 additions & 4 deletions theme/styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ function send_ie_css($themename, $rev) {
header('Pragma: ');
header('Accept-Ranges: none');
header('Content-Type: text/css');
header('Content-Length: '.strlen($css));
if (!min_enable_zlib_compression()) {
header('Content-Length: '.strlen($css));
}

while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
echo $css;
die;
}
Expand All @@ -145,9 +146,10 @@ function send_cached_css($csspath, $rev) {
header('Pragma: ');
header('Accept-Ranges: none');
header('Content-Type: text/css');
header('Content-Length: '.filesize($csspath));
if (!min_enable_zlib_compression()) {
header('Content-Length: '.filesize($csspath));
}

while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
readfile($csspath);
die;
}
5 changes: 3 additions & 2 deletions theme/styles_debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
css_not_found();
}

// no gzip compression when debugging

$candidatesheet = "$CFG->dataroot/cache/theme/$themename/designer.ser";

if (!file_exists($candidatesheet)) {
Expand Down Expand Up @@ -85,9 +87,8 @@ function send_uncached_css($css, $lifetime = THEME_DESIGNER_CACHE_LIFETIME) {
header('Pragma: ');
header('Accept-Ranges: none');
header('Content-Type: text/css');
header('Content-Length: '.strlen($css));
//header('Content-Length: '.strlen($css));

while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
echo($css);
die;
}
Expand Down
5 changes: 3 additions & 2 deletions theme/yui_combo.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ function combo_send_cached($content, $mimetype) {
header('Cache-Control: max-age=315360000');
header('Accept-Ranges: none');
header('Content-Type: '.$mimetype);
header('Content-Length: '.strlen($content));
if (!min_enable_zlib_compression()) {
header('Content-Length: '.strlen($content));
}

while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
echo $content;
die;
}
Expand Down
3 changes: 2 additions & 1 deletion theme/yui_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ function yui_image_cached($imagepath) {
header('Content-Type: '.$mimetype);
header('Content-Length: '.filesize($imagepath));

while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
// no need to gzip already compressed images ;-)

readfile($imagepath);
die;
}
Expand Down

0 comments on commit 7c986f0

Please sign in to comment.