[FEATURE] Compare backend usergroups
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Utility / CompressionUtility.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * This class contains compression functions for the TYPO3 Frontend. It can be
19 * used only in EXT:frontend/Classes/Http/RequestHandler.php
20 *
21 * @internal this is an internal TYPO3 implementation and solely used for EXT:frontend and not part of TYPO3's Core API.
22 */
23 class CompressionUtility implements \TYPO3\CMS\Core\SingletonInterface
24 {
25 /**
26 * Accumulates content length for the compressed content. It is necessary to
27 * replace the Content-length HTTP header after compression if it was added
28 * by TYPO3 before compression.
29 *
30 * @var int
31 */
32 protected $contentLength = 0;
33
34 /**
35 * Corrects HTTP "Content-length" header if it was sent by TYPO3 and compression
36 * is enabled.
37 *
38 * @param string $outputBuffer Output buffer to compress
39 * @param int $mode One of PHP_OUTPUT_HANDLER_xxx constants
40 * @return string Compressed string
41 * @see ob_start()
42 * @see ob_gzhandler()
43 */
44 public function compressionOutputHandler($outputBuffer, $mode)
45 {
46 // Compress the content
47 $outputBuffer = ob_gzhandler($outputBuffer, $mode);
48 if ($outputBuffer !== false) {
49 // Save compressed size
50 $this->contentLength += strlen($outputBuffer);
51 // Check if this was the last content chunk
52 if (0 != ($mode & PHP_OUTPUT_HANDLER_END)) {
53 // Check if we have content-length header
54 foreach (headers_list() as $header) {
55 if (0 == strncasecmp('Content-length:', $header, 15)) {
56 header('Content-length: ' . $this->contentLength);
57 break;
58 }
59 }
60 }
61 }
62 return $outputBuffer;
63 }
64 }