getAssetDirectory() . '/' . basename($file); if (!($byteStart == 0 && $byteEnd == filesize($file)) ) { $compressedFileLocation .= ".$byteStart.$byteEnd"; } $phpOutputCompressionEnabled = self::isPhpOutputCompressed(); if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && !$phpOutputCompressionEnabled) { list($encoding, $extension) = self::getCompressionEncodingAcceptedByClient(); $filegz = $compressedFileLocation . $extension; if (self::canCompressInPhp()) { if (!empty($encoding)) { // compress the file if it doesn't exist or is newer than the existing cached file, and cache // the compressed result if (self::shouldCompressFile($file, $filegz)) { self::compressFile($file, $filegz, $encoding, $byteStart, $byteEnd); } $compressed = true; $file = $filegz; $byteStart = 0; $byteEnd = filesize($file); } } else { // if a compressed file exists, the file was manually compressed so we just serve that if ($extension == '.gz' && !self::shouldCompressFile($file, $filegz) ) { $compressed = true; $file = $filegz; $byteStart = 0; $byteEnd = filesize($file); } } } Common::sendHeader('Last-Modified: ' . $lastModified); if (!$phpOutputCompressionEnabled) { Common::sendHeader('Content-Length: ' . ($byteEnd - $byteStart)); } if (!empty($contentType)) { Common::sendHeader('Content-Type: ' . $contentType); } if ($compressed) { Common::sendHeader('Content-Encoding: ' . $encoding); } if (!_readfile($file, $byteStart, $byteEnd)) { Common::sendResponseCode(500); } } /** * Test if php output is compressed * * @return bool True if php output is (or suspected/likely) to be compressed */ public static function isPhpOutputCompressed() { // Off = ''; On = '1'; otherwise, it's a buffer size $zlibOutputCompression = ini_get('zlib.output_compression'); // could be ob_gzhandler, ob_deflatehandler, etc $outputHandler = ini_get('output_handler'); // output handlers can be stacked $obHandlers = array_filter(ob_list_handlers(), function ($var) { return $var !== "default output handler"; }); // user defined handler via wrapper if (!defined('PIWIK_TEST_MODE')) { $autoPrependFile = ini_get('auto_prepend_file'); $autoAppendFile = ini_get('auto_append_file'); } return !empty($zlibOutputCompression) || !empty($outputHandler) || !empty($obHandlers) || !empty($autoPrependFile) || !empty($autoAppendFile); } /** * Workaround IE bug when downloading certain document types over SSL and * cache control headers are present, e.g., * * Cache-Control: no-cache * Cache-Control: no-store,max-age=0,must-revalidate * Pragma: no-cache * * @see http://support.microsoft.com/kb/316431/ * @see RFC2616 * * @param string $override One of "public", "private", "no-cache", or "no-store". (optional) */ public static function overrideCacheControlHeaders($override = null) { if ($override || self::isHttps()) { Common::sendHeader('Pragma: '); Common::sendHeader('Expires: '); if (in_array($override, array('public', 'private', 'no-cache', 'no-store'))) { Common::sendHeader("Cache-Control: $override, must-revalidate"); } else { Common::sendHeader('Cache-Control: must-revalidate'); } } } /** * Returns a formatted Expires HTTP header for a certain number of days in the future. The result * can be used in a call to `header()`. */ private static function getExpiresHeaderForFutureDay($expireFarFutureDays) { return "Expires: " . gmdate('D, d M Y H:i:s', time() + 86400 * (int)$expireFarFutureDays) . ' GMT'; } private static function getCompressionEncodingAcceptedByClient() { $acceptEncoding = $_SERVER['HTTP_ACCEPT_ENCODING']; if (preg_match(self::DEFLATE_ENCODING_REGEX, $acceptEncoding, $matches)) { return array('deflate', '.deflate'); } else if (preg_match(self::GZIP_ENCODING_REGEX, $acceptEncoding, $matches)) { return array('gzip', '.gz'); } else { return array(false, false); } } private static function canCompressInPhp() { return extension_loaded('zlib') && function_exists('file_get_contents') && function_exists('file_put_contents'); } private static function shouldCompressFile($fileToCompress, $compressedFilePath) { $toCompressLastModified = @filemtime($fileToCompress); $compressedLastModified = @filemtime($compressedFilePath); return !file_exists($compressedFilePath) || ($toCompressLastModified > $compressedLastModified); } private static function compressFile($fileToCompress, $compressedFilePath, $compressionEncoding, $byteStart, $byteEnd) { $data = file_get_contents($fileToCompress); $data = substr($data, $byteStart, $byteEnd - $byteStart); if ($compressionEncoding == 'deflate') { $data = gzdeflate($data, 9); } else if ($compressionEncoding == 'gzip' || $compressionEncoding == 'x-gzip') { $data = gzencode($data, 9); } file_put_contents($compressedFilePath, $data); } }