root/trunk/gui/include/spGzip.php

Revision 1327, 7.3 kB (checked in by rats, 4 months ago)

* Fixed: --scan-knownbad-files and --check-deleted are no longer supported by rkhunter
* Fixed #1471: chkrootkit should be in lenny / hardy
* Updated Chinese (simplified)
* Updated German
* Fixed #1475: typo on installation (ispcp-setup)
* Fixed: default user for rkhunter.log

Line 
1 <?php
2 /**
3  * ispCP ω (OMEGA) a Virtual Hosting Control System
4  *
5  * @copyright     2001-2006 by moleSoftware GmbH
6  * @copyright     2006-2008 by ispCP | http://isp-control.net
7  * @version     SVN: $Id$
8  * @link         http://isp-control.net
9  * @author         ispCP Team
10  *
11  * @license
12  *   This program is free software; you can redistribute it and/or modify it under
13  *   the terms of the MPL General Public License as published by the Free Software
14  *   Foundation; either version 1.1 of the License, or (at your option) any later
15  *   version.
16  *   You should have received a copy of the MPL Mozilla Public License along with
17  *   this program; if not, write to the Open Source Initiative (OSI)
18  *   http://opensource.org | osi@opensource.org
19  */
20
21 /**
22  * This class checks the output buffer
23  * zips the input if necessary
24  * @param: int $MaxServerload the maximum serverload
25  * @param: int $MinCompression the minimum compression level
26  * @param: int $MaxCompression the maximum compression level
27  * @input: mixed $level the compression level
28  * @input: bool $debug use debug mode (no compression)
29  * @input: bool $showSize show the compression in html
30  * @return: mixed the output
31  */
32 class spOutput {
33    /**
34     * Public vars
35     */
36    var $MaxServerload = '2';
37    var $MinCompression = '2';
38    var $MaxCompression = '8';
39    /**
40     * Private vars
41     */
42    var $contents;
43    var $gzdata;
44    var $encoding;
45    var $crc;
46    var $size;
47    var $gzsize;
48    var $serverload;
49    var $level;
50    var $debug;
51    var $showSize;
52
53    /**
54     * Constructor
55     */
56    function spOutput($level='3', $debug=false, $showSize=true) {
57       $this->level = $level;
58       $this->debug = $debug;
59       $this->showSize = $showSize;
60    }
61
62    /**
63     * Let's work a bit with the buffer
64     */
65    function output($buffer) {
66       $this->contents = $buffer;
67
68       /* Find out which encoding to use */
69       $this->encoding = false;
70
71       /**
72        * Check the best compress version for the browser
73        * Use the @ to prevent bots from saving an error
74        */
75       if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
76          if(@strpos(' ' . $_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false) {
77             $this->encoding = 'x-gzip';
78          }
79          if(@strpos(' ' . $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
80             $this->encoding = 'gzip';
81          }
82       }
83
84       /* Find out more about the file which should be compressed */
85       $filetype = substr($this->contents, 0, 4);
86
87       if(substr($filetype, 0, 2) === '^_') {
88          /* gzip data */
89          $this->encoding = false;
90       }
91       elseif(substr($filetype, 0, 3) === 'GIF') {
92          /* gif images */
93          $this->encoding = false;
94       }
95       elseif(substr($filetype, 0, 2) === "\xFF\xD8") {
96          /* jpeg images */
97          $this->encoding = false;
98       }
99       elseif(substr($filetype, 0, 4) === "\x89PNG") {
100          /* png images */
101          $this->encoding = false;
102       }
103       elseif(substr($filetype, 0, 3) === 'FWS') {
104          /* Shockwave Flash */
105          $this->encoding = false;
106       }
107       elseif(substr($filetype, 0, 2) === 'PK') {
108          /* pk zip file */
109          $this->encoding = false;
110       }
111       elseif($filetype == '%PDF') {
112          /* PDF File */
113          $this->encoding = false;
114       }
115
116       /* There might be some Problems */
117       if(headers_sent() || connection_status() != 0 || !$this->encoding || $this->contents === false || !extension_loaded('zlib') || @ini_get('output_handler') == 'ob_gzhandler' || @ini_get('zlib.output_compression') || (isset($GLOBALS['data']['error']) && !empty($GLOBALS['data']['error'])))
118          return $this->contents;
119
120       /* We need a level to compress the output */
121       if($this->level == 'auto') {
122          /* Let's find out, which compression level is ok for the system */
123          if(@file_exists('/proc/loadavg')) {
124             $fh = @fopen('/proc/loadavg', 'r');
125             $load_averages = @fread($fh, 64);
126             @fclose($fh);
127
128             $load_averages = @explode(' ', $load_averages);
129             $this->serverload = ((float)$load_averages[0]+(float)$load_averages[1]+(float)$load_averages[2])/3;
130          }
131          elseif(preg_match('/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/i', @exec('uptime'), $load_averages)) {
132             $this->serverload = ((float)$load_averages[1]+(float)$load_averages[2]+(float)$load_averages[3])/3;
133          }
134          else {
135              $this->serverload = '1';
136          }
137
138          $this->level = (1-($this->serverload/$this->MaxServerload))*10;
139
140          /* Ok, that looks terrible, but its faster than a min/max construction */
141          $this->level = ($this->level > $this->MinCompression) ? (($this->level < $this->MaxCompression) ? intval($this->level) : $this->MaxCompression) : $this->MinCompression;
142       }
143
144       /* The introduction for the compressed data */
145       $this->gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
146
147       /**
148        * Maybe you want some extra information
149        * This is not the ideal because you need to
150        * compress the output twice
151        */
152       if($this->showSize) {
153          /* We need some vars for the information */
154          $uncompressed   = round(strlen($this->contents)/1024, 2);
155          $start      = $this->getMicrotime();
156          $compressed   = round(strlen(gzcompress($this->contents, $this->level))/1024, 2);
157          $time      = round(($this->getMicrotime()-$start)*1000, 2);
158          $savingkb   = $uncompressed-$compressed;
159          $saving      = $uncompressed > '0' ? @round($savingkb/$uncompressed*100, 0) : '0';
160          $showlevel   = ($this->level == 'auto') ? ' (auto)' : '';
161
162          /* Shows some informations */
163          $this->contents .= "\n".'<!--'."\n\t".'Compression level: '.$this->level.$showlevel."\n\t".'Original size: '.$uncompressed.' kb'."\n\t".'New size: '.$compressed.' kb'."\n\t".'Saving: '.$savingkb.' kb ('.$saving.' %)'."\n\t".'Time: '.$time.' ms'."\n\t".'Serverload: '.round($this->serverload, 2)."\n".'-->';
164       }
165
166       /* Compress the output */
167       $this->size   = strlen($this->contents);
168       $this->gzdata   .= substr(gzcompress($this->contents, $this->level), 0, - 4);
169       $this->gzdata   .= pack('V', crc32($this->contents));
170       $this->gzdata   .= pack('V', $this->size);
171       $this->gzsize   = strlen($this->gzdata);
172
173         /* This prevents stupid IEs from displaying blank pages */
174         if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/MSIE/i', $_SERVER['HTTP_USER_AGENT']) && $this->gzsize < 4096) {
175             /* Returns the uncompressed content */
176             return $this->contents;
177         }
178
179       /* Maybe you just want to see the result of all this */
180       if($this->debug) {
181          $this->contents = $this->contents."\n".'<!--'."\n\t".'spGzip is in debug mode. The shown output is uncompressed'."\n".'-->';
182          return $this->contents;
183       }
184
185       /* Send the special header */
186       header('Content-Encoding: '.$this->encoding);
187       header('Content-Length: '.$this->gzsize);
188
189       /* Exit the class and send all data to the browser */
190       return $this->gzdata;
191    }
192
193    /**
194     * Returns the actual microtime
195     * @return: int the actual microtime
196     */
197    function getMicrotime() {
198       return array_sum(explode(' ', microtime()));
199    }
200 }
201 /**
202  * construct the object
203  */
204 $GLOBALS['class']['output'] = new spOutput('auto', false, true);
205
206 /**
207  * Start the output buffering
208  */
209 ob_start(array(&$GLOBALS['class']['output'], 'output'));
210
211 ?>
Note: See TracBrowser for help on using the browser.