update Piwik to version 2.16 (fixes #91)

This commit is contained in:
oliver 2016-04-10 18:55:57 +02:00
commit d885a4baa9
5833 changed files with 418860 additions and 226988 deletions

View file

@ -1,6 +1,6 @@
<?php
/**
* Piwik - Open source web analytics
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
@ -16,6 +16,9 @@ namespace Piwik;
*/
class ProxyHttp
{
const DEFLATE_ENCODING_REGEX = '/(?:^|, ?)(deflate)(?:,|$)/';
const GZIP_ENCODING_REGEX = '/(?:^|, ?)((x-)?gzip)(?:,|$)/';
/**
* Returns true if the current request appears to be a secure HTTPS connection
*
@ -53,104 +56,109 @@ class ProxyHttp
* @param string $contentType The content type of the static file.
* @param bool $expireFarFuture Day in the far future to set the Expires header to.
* Should be set to false for files that should not be cached.
* @param int|false $byteStart The starting byte in the file to serve. If false, the data from the beginning
* of the file will be served.
* @param int|false $byteEnd The ending byte in the file to serve. If false, the data from $byteStart to the
* end of the file will be served.
*/
public static function serverStaticFile($file, $contentType, $expireFarFutureDays = 100)
public static function serverStaticFile($file, $contentType, $expireFarFutureDays = 100, $byteStart = false,
$byteEnd = false)
{
if (file_exists($file)) {
// conditional GET
$modifiedSince = '';
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$modifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
// if the file cannot be found return HTTP status code '404'
if (!file_exists($file)) {
Common::sendResponseCode(404);
return;
}
// strip any trailing data appended to header
if (false !== ($semicolon = strpos($modifiedSince, ';'))) {
$modifiedSince = substr($modifiedSince, 0, $semicolon);
}
}
$modifiedSince = Http::getModifiedSinceHeader();
$fileModifiedTime = @filemtime($file);
$lastModified = gmdate('D, d M Y H:i:s', $fileModifiedTime) . ' GMT';
$fileModifiedTime = @filemtime($file);
$lastModified = gmdate('D, d M Y H:i:s', $fileModifiedTime) . ' GMT';
// set HTTP response headers
self::overrideCacheControlHeaders('public');
@header('Vary: Accept-Encoding');
@header('Content-Disposition: inline; filename=' . basename($file));
// set some HTTP response headers
self::overrideCacheControlHeaders('public');
Common::sendHeader('Vary: Accept-Encoding');
Common::sendHeader('Content-Disposition: inline; filename=' . basename($file));
if ($expireFarFutureDays) {
// Required by proxy caches potentially in between the browser and server to cache the request indeed
@header("Expires: " . gmdate('D, d M Y H:i:s', time() + 86400 * (int)$expireFarFutureDays) . ' GMT');
}
if ($expireFarFutureDays) {
// Required by proxy caches potentially in between the browser and server to cache the request indeed
Common::sendHeader(self::getExpiresHeaderForFutureDay($expireFarFutureDays));
}
// Returns 304 if not modified since
if ($modifiedSince === $lastModified) {
self::setHttpStatus('304 Not Modified');
} else {
// optional compression
$compressed = false;
$encoding = '';
$compressedFileLocation = AssetManager::getInstance()->getAssetDirectory() . '/' . basename($file);
// Return 304 if the file has not modified since
if ($modifiedSince === $lastModified) {
Common::sendResponseCode(304);
return;
}
$phpOutputCompressionEnabled = ProxyHttp::isPhpOutputCompressed();
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && !$phpOutputCompressionEnabled) {
$acceptEncoding = $_SERVER['HTTP_ACCEPT_ENCODING'];
// if we have to serve the file, serve it now, either in the clear or compressed
if ($byteStart === false) {
$byteStart = 0;
}
if (extension_loaded('zlib') && function_exists('file_get_contents') && function_exists('file_put_contents')) {
if (preg_match('/(?:^|, ?)(deflate)(?:,|$)/', $acceptEncoding, $matches)) {
$encoding = 'deflate';
$filegz = $compressedFileLocation . '.deflate';
} else if (preg_match('/(?:^|, ?)((x-)?gzip)(?:,|$)/', $acceptEncoding, $matches)) {
$encoding = $matches[1];
$filegz = $compressedFileLocation . '.gz';
}
if ($byteEnd === false) {
$byteEnd = filesize($file);
}
if (!empty($encoding)) {
// compress-on-demand and use cache
if (!file_exists($filegz) || ($fileModifiedTime > @filemtime($filegz))) {
$data = file_get_contents($file);
$compressed = false;
$encoding = '';
$compressedFileLocation = AssetManager::getInstance()->getAssetDirectory() . '/' . basename($file);
if ($encoding == 'deflate') {
$data = gzdeflate($data, 9);
} else if ($encoding == 'gzip' || $encoding == 'x-gzip') {
$data = gzencode($data, 9);
}
if (!($byteStart == 0
&& $byteEnd == filesize($file))
) {
$compressedFileLocation .= ".$byteStart.$byteEnd";
}
file_put_contents($filegz, $data);
}
$phpOutputCompressionEnabled = self::isPhpOutputCompressed();
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && !$phpOutputCompressionEnabled) {
list($encoding, $extension) = self::getCompressionEncodingAcceptedByClient();
$filegz = $compressedFileLocation . $extension;
$compressed = true;
$file = $filegz;
}
} else {
// manually compressed
$filegz = $compressedFileLocation . '.gz';
if (preg_match('/(?:^|, ?)((x-)?gzip)(?:,|$)/', $acceptEncoding, $matches) && file_exists($filegz) && ($fileModifiedTime < @filemtime($filegz))) {
$encoding = $matches[1];
$compressed = true;
$file = $filegz;
}
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;
@header('Last-Modified: ' . $lastModified);
if (!$phpOutputCompressionEnabled) {
@header('Content-Length: ' . filesize($file));
}
if (!empty($contentType)) {
@header('Content-Type: ' . $contentType);
}
if ($compressed) {
@header('Content-Encoding: ' . $encoding);
}
if (!_readfile($file)) {
self::setHttpStatus('505 Internal server error');
$byteStart = 0;
$byteEnd = filesize($file);
}
}
} else {
self::setHttpStatus('404 Not Found');
}
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);
}
}
@ -185,7 +193,6 @@ class ProxyHttp
!empty($autoAppendFile);
}
/**
* Workaround IE bug when downloading certain document types over SSL and
* cache control headers are present, e.g.,
@ -202,31 +209,63 @@ class ProxyHttp
public static function overrideCacheControlHeaders($override = null)
{
if ($override || self::isHttps()) {
@header('Pragma: ');
@header('Expires: ');
Common::sendHeader('Pragma: ');
Common::sendHeader('Expires: ');
if (in_array($override, array('public', 'private', 'no-cache', 'no-store'))) {
@header("Cache-Control: $override, must-revalidate");
Common::sendHeader("Cache-Control: $override, must-revalidate");
} else {
@header('Cache-Control: must-revalidate');
Common::sendHeader('Cache-Control: must-revalidate');
}
}
}
/**
* Set response header, e.g., HTTP/1.0 200 Ok
*
* @param string $status Status
* @return bool
* 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()`.
*/
protected static function setHttpStatus($status)
private static function getExpiresHeaderForFutureDay($expireFarFutureDays)
{
if (substr_compare(PHP_SAPI, '-fcgi', -5)) {
@header($_SERVER['SERVER_PROTOCOL'] . ' ' . $status);
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');
} elseif (preg_match(self::GZIP_ENCODING_REGEX, $acceptEncoding, $matches)) {
return array('gzip', '.gz');
} else {
// FastCGI
@header('Status: ' . $status);
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);
} elseif ($compressionEncoding == 'gzip' || $compressionEncoding == 'x-gzip') {
$data = gzencode($data, 9);
}
file_put_contents($compressedFilePath, $data);
}
}