1: <?php
2: /**
3: * Filter to gzip content before being served
4: *
5: * @category Horde
6: * @package Controller
7: * @author James Pepin <james@bluestatedigital.com>
8: * @license http://www.horde.org/licenses/bsd BSD
9: */
10: class Horde_Controller_Filter_Gzip implements Horde_Controller_PostFilter
11: {
12: public function processResponse(Horde_Controller_Request $request, Horde_Controller_Response $response, Horde_Controller $controller)
13: {
14: $body = $response->getBody();
15: $body = gzencode($body);
16:
17: $response->setHeader('Content-Encoding', 'gzip');
18: $response->setHeader('Content-Length', $this->_byteCount($body));
19: $response->setBody($body);
20:
21: return $response;
22: }
23:
24: /**
25: * If mbstring is set to overload str* function then we could be counting
26: * multi-byte chars as single bytes so we need to treat the string like its
27: * 8-bit encoded to get an accurate byte count.
28: */
29: protected function _byteCount($string)
30: {
31: if (ini_get('mbstring.func_overload') > 0) {
32: return mb_strlen($string, '8bit');
33: }
34:
35: return strlen($string);
36: }
37: }
38: