Fixed bug #9974: Use PHP built-in gzip/deflate compression in Frontend
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_fecompression.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Dmitry Dulepov <dmitry@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 require_once(PATH_t3lib . 'interfaces/interface.t3lib_singleton.php');
26
27 /**
28 * This class contains compression functions for the TYPO3 Frontend. It can be
29 * used only in EXT:cms/tslib/index_ts.php
30 *
31 * $Id: $
32 *
33 * @author Dmitry Dulepov <dmitry@typo3.org>
34 * @package TYPO3
35 * @subpackage tx_cms
36 */
37 class tslib_fecompression implements t3lib_Singleton {
38
39 /**
40 * Accumulates content length for the compressed content. It is necessary to
41 * replace the Content-length HTTP header after compression if it was added
42 * by TYPO3 before compression.
43 *
44 * @var int
45 */
46 protected $contentLength = 0;
47
48 /**
49 * Corrects HTTP "Content-length" header if it was sent by TYPO3 and compression
50 * is enabled.
51 *
52 * @param string $outputBuffer Output buffer to compress
53 * @param int $mode One of PHP_OUTPUT_HANDLER_xxx contants
54 * @return string Compressed string
55 * @see ob_start()
56 * @see ob_gzhandler()
57 */
58 function compressionOutputHandler($outputBuffer, $mode) {
59 // Compress the content
60 $outputBuffer = ob_gzhandler($outputBuffer, $mode);
61 if ($outputBuffer !== false) {
62 // Save compressed size
63 $this->contentLength += strlen($outputBuffer);
64
65 // Check if this was the last content chunk
66 if (0 != ($mode & PHP_OUTPUT_HANDLER_END)) {
67 // Check if we have content-length header
68 foreach (headers_list() as $header) {
69 if (0 == strncasecmp('Content-length:', $header, 15)) {
70 header('Content-length: ' . $this->contentLength);
71 break;
72 }
73 }
74 }
75 }
76 return $outputBuffer;
77 }
78 }
79
80 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/cms/tslib/class.tslib_fecompression.php']) {
81 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/cms/tslib/class.tslib_fecompression.php']);
82 }
83
84 ?>