Introduced configurability for gzip compression. Fixes issue 18.
authorDan
Sun, 28 Mar 2010 21:49:26 -0400
changeset 1226 de56132c008d
parent 1225 4c6da61e613e
child 1227 bdac73ed481e
Introduced configurability for gzip compression. Fixes issue 18.
includes/clientside/jsres.php
includes/clientside/static/ajax.js
includes/clientside/static/enano-lib-basic.js
includes/functions.php
index.php
language/english/admin.json
plugins/SpecialAdmin.php
--- a/includes/clientside/jsres.php	Sun Mar 28 21:40:37 2010 -0400
+++ b/includes/clientside/jsres.php	Sun Mar 28 21:49:26 2010 -0400
@@ -105,7 +105,7 @@
 // try to gzip the output
 if ( !defined('ENANO_JSRES_SETUP_ONLY') ):
 $do_gzip = false;
-if ( isset($_SERVER['HTTP_ACCEPT_ENCODING']) )
+if ( isset($_SERVER['HTTP_ACCEPT_ENCODING']) && getConfig('gzip_output', false) == 1 )
 {
   $acceptenc = str_replace(' ', '', strtolower($_SERVER['HTTP_ACCEPT_ENCODING']));
   $acceptenc = explode(',', $acceptenc);
--- a/includes/clientside/static/ajax.js	Sun Mar 28 21:40:37 2010 -0400
+++ b/includes/clientside/static/ajax.js	Sun Mar 28 21:49:26 2010 -0400
@@ -1724,6 +1724,7 @@
         thediv.style.display = 'none';
         thediv.style.zIndex = getHighestZ() + 2;
         thediv.id = 'mdgDynamic_rDnsInfoDiv_'+Math.floor(Math.random() * 1000000);
+        // FIXME: l10n
         thediv.innerHTML = '<b>Reverse DNS:</b><br />'+ajax.responseText+' <a href="#" onclick="elem = document.getElementById(\''+thediv.id+'\'); elem.innerHTML = \'\'; elem.style.display = \'none\';return false;">Close</a>';
         var body = document.getElementsByTagName('body');
         body = body[0];
@@ -1740,3 +1741,48 @@
     });
 }
 
+window.ajaxGzipCheck = function()
+{
+  var resultdiv = document.getElementById('gzip_check_result');
+  if ( !resultdiv )
+    return false;
+  
+  resultdiv.innerHTML = '<img alt="Loading..." src="' + cdnPath + '/images/loading.gif" />';
+  ajaxPost(makeUrlNS('Admin', 'GeneralConfig'), 'act=gzip_check', function(ajax)
+    {
+      if ( ajax.readyState == 4 && ajax.status == 200 )
+      {
+        resultdiv.innerHTML = '';
+        var response = String(ajax.responseText + '');
+        if ( !check_json_response(response) )
+        {
+          handle_invalid_json(response);
+          return false;
+        }
+        response = parseJSON(response);
+        if ( response.error )
+        {
+          resultdiv.innerHTML = '<div class="error-box-mini">' + response.error + '</div>';
+        }
+        else
+        {
+          // probably success.
+          resultdiv.innerHTML += response.server_does_it ?
+            '<div class="error-box-mini">' + $lang.get('acpgc_field_gzip_check_msg_server_does_it') + '</div>' :
+            '<div class="info-box-mini">' + $lang.get('acpgc_field_gzip_check_msg_server_good') + '</div>';
+          resultdiv.innerHTML += response.php_supports_gzip ?
+            '<div class="info-box-mini">' + $lang.get('acpgc_field_gzip_check_msg_php_good') + '</div>' :
+            '<div class="error-box-mini">' + $lang.get('acpgc_field_gzip_check_msg_php_bad') + '</div>';
+            
+          if ( response.php_supports_gzip && !response.server_does_it )
+          {
+            resultdiv.innerHTML += '<div class="success-box-mini">' + $lang.get('acpgc_field_gzip_check_msg_success') + '</div>';
+          }
+          else
+          {
+            resultdiv.innerHTML += '<div class="error-box-mini">' + $lang.get('acpgc_field_gzip_check_msg_failure') + '</div>';
+          }
+        }
+      }
+    });
+}
--- a/includes/clientside/static/enano-lib-basic.js	Sun Mar 28 21:40:37 2010 -0400
+++ b/includes/clientside/static/enano-lib-basic.js	Sun Mar 28 21:49:26 2010 -0400
@@ -576,6 +576,7 @@
   ajaxCatToTag: 'ajax.js',
   ajaxCatEdit: 'ajax.js',
   ajaxReverseDNS: 'ajax.js',
+  ajaxGzipCheck: 'ajax.js',
   ajaxOpenACLManager: 'acl.js',
   ajaxOpenDirectACLRule: 'acl.js',
   ajaxAdminPage: 'login.js',
--- a/includes/functions.php	Sun Mar 28 21:40:37 2010 -0400
+++ b/includes/functions.php	Sun Mar 28 21:49:26 2010 -0400
@@ -3215,7 +3215,7 @@
   // Compress buffered output if required and send to browser
   // Sorry, doesn't work in IE. What else is new?
   //
-  if ( $do_gzip && function_exists('gzdeflate') && !strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && !headers_sent() && $gzip_supported )
+  if ( $do_gzip && getConfig('gzip_output', false) == 1 && function_exists('gzdeflate') && !strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && !headers_sent() && $gzip_supported )
   {
     $gzip_contents = ob_get_contents();
     ob_end_clean();
--- a/index.php	Sun Mar 28 21:40:37 2010 -0400
+++ b/index.php	Sun Mar 28 21:49:26 2010 -0400
@@ -24,8 +24,7 @@
 
 // Set up gzip encoding before any output is sent
 global $do_gzip;
-// FIXME: make this configurable
-$do_gzip = !defined('ENANO_DEBUG');
+$do_gzip = !defined('ENANO_DEBUG') && getConfig('gzip_output', false) == 1 && !isset($_GET['disable_builtin_gzip']);
 
 error_reporting(E_ALL);
 
--- a/language/english/admin.json	Sun Mar 28 21:40:37 2010 -0400
+++ b/language/english/admin.json	Sun Mar 28 21:49:26 2010 -0400
@@ -336,7 +336,7 @@
       
       // Section: default theme
       heading_default_theme: 'Visual defaults',
-      field_defualt_theme: 'Default theme for guests and new users:',
+      field_default_theme: 'Default theme for guests and new users:',
       
       // Section: breadcrumbs
       field_breadcrumb_mode: 'Enable breadcrumbs:',
@@ -345,11 +345,22 @@
       field_breadcrumb_mode_never: 'Never',
       
       // Section: CDN
-      
       field_cdn_path: 'URL to <acronym title="Content Delivery Network">CDN</acronym> server:',
       field_cdn_path_hint: 'A CDN, or Content Delivery Network, allows downloading of shared Enano components from a server designed to serve out only images, CSS, and script files. Since a browser can open separate connections for the page and for images and scripts, the page loads faster. Leave this blank to just use Enano\'s local files (default).',
       field_cdn_path_example: 'Example: <tt>http://cdn.mycompany.com/enano/',
       
+      // Section: gzip
+      field_gzip: 'Enable gzip compression',
+      field_gzip_hint: 'Compress pages using before sending them to the user. At the cost of a small amount of CPU, this can save a lot of bandwidth. This only enables compression for HTML and some Javascript; to gzip compress CSS and images, please consult your webserver\'s documentation.',
+      field_gzip_lbl: 'Use gzip compression',
+      field_gzip_btn_check: 'Check server for support',
+      field_gzip_check_msg_server_does_it: 'Your server performs gzip compression on its own. There is no need to enable it in Enano, and if you do, you may cause Enano to stop working.',
+      field_gzip_check_msg_server_good: 'Your server is not automatically compressing PHP-generated pages, so Enano\'s compression facilities are needed.',
+      field_gzip_check_msg_php_bad: 'PHP does not have gzip support enabled.',
+      field_gzip_check_msg_php_good: 'Found gzip compression support in PHP.',
+      field_gzip_check_msg_success: 'Success - all server checks passed. You can enable gzip support.',
+      field_gzip_check_msg_failure: 'Server check failed. Unless you think Enano is wrong, don\'t enable gzip below.',
+      
       // Main section: users and communication
       heading_users: 'Users and communication',
       
--- a/plugins/SpecialAdmin.php	Sun Mar 28 21:40:37 2010 -0400
+++ b/plugins/SpecialAdmin.php	Sun Mar 28 21:49:26 2010 -0400
@@ -82,6 +82,41 @@
     return;
   }
   
+  // FIXME: is this a bad place for this? I couldn't think of anything much better. Not helped by the fact that I hate misc scripts.
+  if ( isset($_POST['act']) && $_POST['act'] === 'gzip_check' )
+  {
+    global $is_https;
+    header('Content-type: application/json');
+    require(ENANO_ROOT . '/includes/http.php');
+    try
+    {
+      if ( !isset($_SERVER['SERVER_ADDR']) )
+        throw new Exception('No SERVER_ADDR support - can\'t test server environment');
+      
+      $server_addr = $_SERVER['SERVER_ADDR'];
+      // cheap ipv6 test
+      if ( strstr($server_addr, ":") )
+        $server_addr = "[$server_addr]";
+      
+      $req = new Request_HTTP($server_addr, makeUrlNS('System', 'GzipTest', 'disable_builtin_gzip'), 'GET', intval($_SERVER['SERVER_PORT']), $is_https);
+      $req->add_header('Accept-Encoding', 'gzip,deflate');
+      $headers = $req->get_response_headers_array();
+      $send = array(
+          'server_does_it' => ( isset($headers['Content-encoding']) && in_array($headers['Content-encoding'], array('gzip', 'deflate')) ),
+          'php_supports_gzip' => function_exists('gzdeflate')
+        );
+    }
+    catch ( Exception $e )
+    {
+      $send = array(
+        'mode' => 'error',
+        'error' => "HTTP request exception: <pre>$e</pre>"
+        );
+    }
+    echo enano_json_encode($send);
+    return;
+  }
+  
   if(isset($_POST['submit']) && !defined('ENANO_DEMO_MODE') )
   {
     
@@ -236,6 +271,7 @@
     setConfig('avatar_directory', 'files/avatars');
     
     setConfig('userpage_grant_acl', ( isset($_POST['userpage_grant_acl']) ? '1' : '0' ));
+    setConfig('gzip_output', ( isset($_POST['gzip_output']) ? '1' : '0' ));
     
     // Allow plugins to save their changes
     $code = $plugins->setHook('acp_general_save');
@@ -496,7 +532,7 @@
       
       <tr>
         <td class="row2">
-          <?php echo $lang->get('acpgc_field_defualt_theme'); ?>
+          <?php echo $lang->get('acpgc_field_default_theme'); ?>
         </td>
         <td class="row2">
           <select name="default_theme">
@@ -553,6 +589,24 @@
         </td>
       </tr>
       
+    <!-- Gzip -->
+    
+      <tr>
+        <td class="row1">
+          <b><?php echo $lang->get('acpgc_field_gzip'); ?></b><br />
+          <small><?php echo $lang->get('acpgc_field_gzip_hint'); ?></small><br />
+          <br />
+          <a href="#" onclick="ajaxGzipCheck(); return false;"><?php echo $lang->get('acpgc_field_gzip_btn_check'); ?></a>
+        </td>
+        <td class="row1">
+          <div id="gzip_check_result"></div>
+          <label>
+            <input type="checkbox" name="gzip_output" <?php if ( getConfig('gzip_output', false) == 1 ) echo 'checked="checked" '; ?>/>
+            <?php echo $lang->get('acpgc_field_gzip_lbl'); ?>
+          </label>
+        </td>
+      </tr>
+      
     <!-- Allow plugins to add code -->
       <?php
       $code = $plugins->setHook('acp_general_basic');