]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-21241 theme CSS and JS gzip compression + some more fixes
authorPetr Skoda <skodak@moodle.org>
Tue, 5 Jan 2010 20:18:15 +0000 (20:18 +0000)
committerPetr Skoda <skodak@moodle.org>
Tue, 5 Jan 2010 20:18:15 +0000 (20:18 +0000)
lib/configonlylib.php
theme/image.php
theme/javascripts.php
theme/styles.php
theme/styles_debug.php
theme/yui_combo.php
theme/yui_image.php

index 11c70e917ebda948b40c6952655363036ce58a75..b02c4d8252bce0d874551c44aff9ce0eb9073369 100644 (file)
@@ -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;
 }
\ No newline at end of file
index a5823e5c314e3c0b320079466db26cebf57368d1..1324ca7210a482402059749d2e3f4b8c4f7d29bd 100644 (file)
@@ -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;
 }
@@ -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;
 }
index f347b11c023c6a4c0c199db7ebd5dc6ed4b74b40..33b7ff1117c617ab1caea5a81437a136cf025f5d 100644 (file)
@@ -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;
 }
@@ -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;
 }
index 969700e9b36c780d871e7e93ce8af52ac4e3edee..39a7b0523324d7713c8670305ba765d2df1bc9ad 100644 (file)
@@ -129,9 +129,10 @@ EOF;
     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;
 }
@@ -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;
 }
index 1fe88343e611ee107ef3a92c3d48b062b128d5d4..8e26a8fc7d68d9c65833c6ae205b67d8f7b647b1 100644 (file)
@@ -44,6 +44,8 @@ if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
     css_not_found();
 }
 
+// no gzip compression when debugging
+
 $candidatesheet = "$CFG->dataroot/cache/theme/$themename/designer.ser";
 
 if (!file_exists($candidatesheet)) {
@@ -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;
 }
index 7fadba4f9f9a0981ab2f25aaeed309e2f2d1ea23..1502c393be4a183846042a179156d0ea907fff54 100644 (file)
@@ -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;
 }
index 30fa5d64bd3b49f085407ccb596b74ec802a386b..7a625b862d03c3f1f51fac86c35a14c6f4f5ffe5 100644 (file)
@@ -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;
 }