From: Tyler A. <fi...@us...> - 2001-02-22 13:24:49
|
Update of /cvsroot/squirrelmail/squirrelmail/plugins/gzip In directory usw-pr-cvs1:/tmp/cvs-serv26402/gzip Added Files: Tag: sm-1_0 INSTALL README setup.php Log Message: * Added all the plugins to the 1.0 branch. Hope that this is still the correct thing to do -- don't want to make people mad. * Will add them to the 1.1 branch shortly. * Are we going to make a new policy for plugin people to include their plugin in the CVS for whatever SquirrelMail versions the plugin works with? Just the newest stable branch and the experimental branch? * If these should NOT be here, let me know and I'll remove all plugins from 1.0 and the soon-to-have-them 1.1 branches. --- NEW FILE --- Installing Plugins ================== Simply untar the file in the plugins directory, and make sure it is in its own directory, and that the name of the directory is the name of the plugin. Example below uses "plug_demo" as the name of the plugin: $ cd plugins $ tar -zxvf /usr/archives/plug_demo.tar.gz Then go to your config directory and run conf.pl. Choose option 8 and add the plugin. Save and exit, then that should be all if the plugin was made correctly. :) $ cd ../config $ ./conf.pl --- NEW FILE --- GZip -- Version 1.1 This compresses output if it matches certain criteria and if your browser supports gzip'd web pages. This will save bandwidth, but could use a bit more processor time. Features ======== * Less data transferred to client, so faster on low-bandwidth connections. * Browser detection -- compresses only if the browser allows it * Uses zlib or gzip (whatever is available -- see below) * Compresses only when bigger than size specified Description =========== This plugin, when used with new hooks that I inserted, will compress all output to the user, if it is bigger than the limit and if the user so desires. The advantage is that bandwidth is saved and that people using modems will be much happier. The disadvantage is that it does suck a little bit more CPU time, and that a temporary file needs to be created. Output will be compressed only if it is bigger than the size specified by the user. This will not compress excessively small documents, and thus save server load. As it turns out, I use gzcompress() if you have zlib compiled into PHP. The downfall is that it doesn't produce the gzip header nor the proper gzip CRC, both of which I have corrected in the code. This might get changed post-4.0.2. If you have zlib compiled into PHP, this plugin takes advantage of the library and will not spawn separate gzip processes. If PHP is not compiled with zlib, don't worry. This plugin will use your gzip binary, if you have gzip installed, but you might need to specify it in setup.php near the top. If you are using a strange gzip, make sure that it can have information sent to it (via STDIN) and that the output (STDOUT) can be redirected to a file. A good test would be echo This Is A Test | gzip > output_file In order for this plugin to work, you need to have two hooks set up on whatever pages you want compressed. The first is 'html_top', and the other is 'html_bottom'. html_top must be before the HTML output starts, usually before the DisplayPageHeader() function. The html_bottom hook should be placed at the very end of the PHP script. These new hooks are temporary, until offical hooks are placed at the very beginning and end of each script. Future Work =========== * Work around the temporary file * Use normal hooks or get the html_top and html_bottom in the standard distribution * Different compression techniques (deflate, bzip, etc) * Maybe use a different directory for temporary files than $data_dir Installation ============ As with other plugins, just uncompress the archive in the plugins directory, go back to the main directory, run configure and add the plugin. Questions/comments/flames/etc can be sent to Tyler Akins <fi...@ti...> Changes ======= 1.1 -> 1.2 * If you have zlib installed, it no longer requires temporary files 1.0 -> 1.1 * Fixed documentation to say GZip * Enforced minimum size requirement when compressing --- NEW FILE --- <?php /* Compress all output sent to the client, if they want it and if the client supports it. */ // This plugin requires that you either have the 'zlib' library compiled with // PHP (with --with-zlib), or that you specify where the 'gzip' program is. // * If you have zlib, you can have this be '' or the path to gzip. It // doesn't matter -- the zlib will be used automatically. // * If you don't have zlib, this plugin must use this variable. // * If you don't have zlib and you set $gzip_binary to '', then the // plugin will not attach itself to any hooks and will not be loaded and // it won't produce errors either. $gzip_binary = '/bin/gzip'; /* Initialize the plugin */ function squirrelmail_plugin_init_gzip() { global $squirrelmail_plugin_hooks; if (! extension_loaded('zlib') && $gzip_binary = '') return; $squirrelmail_plugin_hooks['options_display_inside']['gzip'] = 'gzip_options'; $squirrelmail_plugin_hooks['options_display_save']['gzip'] = 'gzip_save'; $squirrelmail_plugin_hooks['loading_prefs']['gzip'] = 'gzip_load'; // I would like to see standard hooks... // Maybe "[script]_html_top" (like 'download_html_top', // 'read_body_html_top', and others). $squirrelmail_plugin_hooks['html_top']['gzip'] = 'gzip_start'; $squirrelmail_plugin_hooks['html_bottom']['gzip'] = 'gzip_end'; } function gzip_save() { global $username,$data_dir; global $gzip_gzip_disable; global $gzip_gzip_size; if (isset($gzip_gzip_enable)) { setPref($data_dir, $username, 'gzip_disable', '1'); } else { setPref($data_dir, $username, 'gzip_disable', ''); } setPref($data_dir, $username, 'gzip_size', $gzip_gzip_size); } function gzip_load() { global $username, $data_dir; global $gzip_disable, $gzip_size; $gzip_enable = getPref($data_dir, $username, 'gzip_disable'); $gzip_size = getPref($data_dir, $username, 'gzip_size'); if ($gzip_size <= 0) { $gzip_size = 15; } } function gzip_options() { global $gzip_size, $gzip_disable; echo "<tr><td align=right nowrap valign=top>Compressed Output:</td>\n"; echo "<td><input name=\"gzip_gzip_disable\" type=CHECKBOX"; if ($gzip_disable) echo " CHECKED"; echo "> Disable compression<br>Compress if bigger than <input type=text name=\"gzip_gzip_size\" "; echo "size=4 value=\"$gzip_size\"> kilobytes</td></tr>\n"; } function gzip_start() { global $gzip_disable, $gzip_supported; global $HTTP_SERVER_VARS, $username; $gzip_supported = ''; if ($gzip_disable) { return; } $methods = explode(', ', $HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING']); foreach ($methods as $val) { if ($val == 'x-gzip') { $gzip_supported = 'x-gzip'; } else if ($val == 'gzip' && $gzip_supported == '') { $gzip_supported = 'gzip'; } } if ($gzip_supported == '') return; ob_start(); ob_implicit_flush(0); } function gzip_end() { global $gzip_supported, $data_dir, $gzip_size; if ($gzip_supported == '') return; if (ob_get_length() < $gzip_size * 1024) { ob_end_flush(); return; } // If you want to make sure that the page is being compressed, // just add this line: // echo "<P>COMPRESSED</p>\n"; $contents = ob_get_contents(); ob_end_clean(); // Maybe support other compression techniques in the future if ($gzip_supported == 'x-gzip' || $gzip_supported == 'gzip') { header("Content-Encoding: $gzip_supported"); $TempFile = tempnam($data_dir, 'sm-gz'); if (extension_loaded('zlib')) { echo "\x1f\x8b\x08\x00\x00\x00\x00\x00"; $Size = strlen($contents); $Crc = crc32($contents); $contents = gzcompress($contents, 9); $contents = substr($contents, 0, strlen($contents) - 4); echo $contents; echo pack('V', $Crc); echo pack('V', $Size); $TempFile = 0; } else { $fp = popen("$gzip_binary > $TempFile", "wb"); fwrite($fp, $contents); pclose($fp); } } // This check may seem silly here, but if we decide to add other // compression methods, they might not need a messed-up temporary // file if ($TempFile) { $fp = fopen($TempFile, "rb"); while (!feof($fp)) { echo fread($fp, 1024); } fclose($fp); unlink($TempFile); } } ?> |