public static function serverStaticFile($file, $contentType, $expireFarFutureDays = 100, $byteStart = false, $byteEnd = false, $filename = false)
{
// if the file cannot be found return HTTP status code '404'
if (!file_exists($file)) {
Common::sendResponseCode(404);
return;
}
$modifiedSince = Http::getModifiedSinceHeader();
$fileModifiedTime = @filemtime($file);
$lastModified = gmdate('D, d M Y H:i:s', $fileModifiedTime) . ' GMT';
// set some HTTP response headers
self::overrideCacheControlHeaders('public');
Common::sendHeader('Vary: Accept-Encoding');
if (false === $filename) {
$filename = basename($file);
}
Common::sendHeader('Content-Disposition: inline; filename=' . $filename);
if ($expireFarFutureDays) {
// Required by proxy caches potentially in between the browser and server to cache the request indeed
Common::sendHeader(self::getExpiresHeaderForFutureDay($expireFarFutureDays));
}
// Return 304 if the file has not modified since
if ($modifiedSince === $lastModified) {
Common::sendResponseCode(304);
return;
}
// if we have to serve the file, serve it now, either in the clear or compressed
if ($byteStart === false) {
$byteStart = 0;
}
if ($byteEnd === false) {
$byteEnd = filesize($file);
}
$compressed = false;
$encoding = '';
$compressedFileLocation = AssetManager::getInstance()->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);
}
}