2012-02-12 16:20:30 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2011 Bart Visscher bartv@thisnet.nl
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class OC_Response {
|
2012-02-12 19:38:06 +00:00
|
|
|
const STATUS_FOUND = 304;
|
2012-02-12 16:20:30 +00:00
|
|
|
const STATUS_NOT_MODIFIED = 304;
|
2012-02-12 19:38:06 +00:00
|
|
|
const STATUS_TEMPORARY_REDIRECT = 307;
|
2012-02-13 22:35:33 +00:00
|
|
|
const STATUS_NOT_FOUND = 404;
|
2013-06-10 11:45:19 +00:00
|
|
|
const STATUS_INTERNAL_SERVER_ERROR = 500;
|
2012-02-12 16:20:30 +00:00
|
|
|
|
2012-02-17 20:33:39 +00:00
|
|
|
/**
|
|
|
|
* @brief Enable response caching by sending correct HTTP headers
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param integer $cache_time time to cache the response
|
2012-02-17 20:33:39 +00:00
|
|
|
* >0 cache time in seconds
|
|
|
|
* 0 and <0 enable default browser caching
|
|
|
|
* null cache indefinitly
|
|
|
|
*/
|
2012-02-13 21:37:27 +00:00
|
|
|
static public function enableCaching($cache_time = null) {
|
|
|
|
if (is_numeric($cache_time)) {
|
|
|
|
header('Pragma: public');// enable caching in IE
|
|
|
|
if ($cache_time > 0) {
|
|
|
|
self::setExpiresHeader('PT'.$cache_time.'S');
|
|
|
|
header('Cache-Control: max-age='.$cache_time.', must-revalidate');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self::setExpiresHeader(0);
|
|
|
|
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
header('Cache-Control: cache');
|
|
|
|
header('Pragma: cache');
|
|
|
|
}
|
|
|
|
|
2012-02-12 16:20:30 +00:00
|
|
|
}
|
2012-02-17 20:33:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief disable browser caching
|
|
|
|
* @see enableCaching with cache_time = 0
|
|
|
|
*/
|
2012-02-13 22:48:05 +00:00
|
|
|
static public function disableCaching() {
|
|
|
|
self::enableCaching(0);
|
|
|
|
}
|
2012-02-12 16:20:30 +00:00
|
|
|
|
2012-02-17 20:33:39 +00:00
|
|
|
/**
|
|
|
|
* @brief Set response status
|
|
|
|
* @param $status a HTTP status code, see also the STATUS constants
|
|
|
|
*/
|
2012-02-12 16:20:30 +00:00
|
|
|
static public function setStatus($status) {
|
2012-02-12 19:38:06 +00:00
|
|
|
$protocol = $_SERVER['SERVER_PROTOCOL'];
|
2012-02-12 16:20:30 +00:00
|
|
|
switch($status) {
|
|
|
|
case self::STATUS_NOT_MODIFIED:
|
|
|
|
$status = $status . ' Not Modified';
|
|
|
|
break;
|
2012-02-12 19:38:06 +00:00
|
|
|
case self::STATUS_TEMPORARY_REDIRECT:
|
|
|
|
if ($protocol == 'HTTP/1.1') {
|
|
|
|
$status = $status . ' Temporary Redirect';
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$status = self::STATUS_FOUND;
|
|
|
|
// fallthrough
|
|
|
|
}
|
|
|
|
case self::STATUS_FOUND;
|
|
|
|
$status = $status . ' Found';
|
|
|
|
break;
|
2012-02-13 22:35:33 +00:00
|
|
|
case self::STATUS_NOT_FOUND;
|
|
|
|
$status = $status . ' Not Found';
|
|
|
|
break;
|
2013-06-10 11:45:19 +00:00
|
|
|
case self::STATUS_INTERNAL_SERVER_ERROR;
|
|
|
|
$status = $status . ' Internal Server Error';
|
|
|
|
break;
|
2012-02-12 16:20:30 +00:00
|
|
|
}
|
2012-02-12 19:38:06 +00:00
|
|
|
header($protocol.' '.$status);
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:33:39 +00:00
|
|
|
/**
|
|
|
|
* @brief Send redirect response
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param string $location to redirect to
|
2012-02-17 20:33:39 +00:00
|
|
|
*/
|
2012-02-12 19:38:06 +00:00
|
|
|
static public function redirect($location) {
|
|
|
|
self::setStatus(self::STATUS_TEMPORARY_REDIRECT);
|
|
|
|
header('Location: '.$location);
|
2012-02-12 16:20:30 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 20:33:39 +00:00
|
|
|
/**
|
|
|
|
* @brief Set reponse expire time
|
|
|
|
* @param $expires date-time when the response expires
|
|
|
|
* string for DateInterval from now
|
|
|
|
* DateTime object when to expire response
|
|
|
|
*/
|
2012-02-12 19:38:28 +00:00
|
|
|
static public function setExpiresHeader($expires) {
|
|
|
|
if (is_string($expires) && $expires[0] == 'P') {
|
|
|
|
$interval = $expires;
|
|
|
|
$expires = new DateTime('now');
|
2012-02-13 21:35:48 +00:00
|
|
|
$expires->add(new DateInterval($interval));
|
2012-02-12 19:38:28 +00:00
|
|
|
}
|
|
|
|
if ($expires instanceof DateTime) {
|
2012-02-13 21:47:31 +00:00
|
|
|
$expires->setTimezone(new DateTimeZone('GMT'));
|
2012-02-12 19:38:28 +00:00
|
|
|
$expires = $expires->format(DateTime::RFC2822);
|
|
|
|
}
|
2012-02-13 21:35:48 +00:00
|
|
|
header('Expires: '.$expires);
|
2012-02-12 19:38:28 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 20:33:39 +00:00
|
|
|
/**
|
|
|
|
* Checks and set ETag header, when the request matches sends a
|
|
|
|
* 'not modified' response
|
|
|
|
* @param $etag token to use for modification check
|
|
|
|
*/
|
2012-02-12 16:20:30 +00:00
|
|
|
static public function setETagHeader($etag) {
|
|
|
|
if (empty($etag)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-07 11:40:30 +00:00
|
|
|
$etag = '"'.$etag.'"';
|
2012-02-12 16:20:30 +00:00
|
|
|
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
|
|
|
|
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
|
|
|
|
self::setStatus(self::STATUS_NOT_MODIFIED);
|
|
|
|
exit;
|
|
|
|
}
|
2012-09-07 11:40:30 +00:00
|
|
|
header('ETag: '.$etag);
|
2012-02-12 16:20:30 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 20:33:39 +00:00
|
|
|
/**
|
|
|
|
* Checks and set Last-Modified header, when the request matches sends a
|
|
|
|
* 'not modified' response
|
|
|
|
* @param $lastModified time when the reponse was last modified
|
|
|
|
*/
|
2012-02-12 16:20:30 +00:00
|
|
|
static public function setLastModifiedHeader($lastModified) {
|
|
|
|
if (empty($lastModified)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-13 22:35:33 +00:00
|
|
|
if (is_int($lastModified)) {
|
|
|
|
$lastModified = gmdate(DateTime::RFC2822, $lastModified);
|
|
|
|
}
|
2012-02-12 16:20:30 +00:00
|
|
|
if ($lastModified instanceof DateTime) {
|
|
|
|
$lastModified = $lastModified->format(DateTime::RFC2822);
|
|
|
|
}
|
|
|
|
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
|
|
|
|
trim($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) {
|
|
|
|
self::setStatus(self::STATUS_NOT_MODIFIED);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
header('Last-Modified: '.$lastModified);
|
|
|
|
}
|
2012-02-13 22:35:33 +00:00
|
|
|
|
2013-12-10 11:05:39 +00:00
|
|
|
/**
|
|
|
|
* Sets the content disposition header (with possible workarounds)
|
|
|
|
* @param string $filename file name
|
|
|
|
* @param string $type disposition type, either 'attachment' or 'inline'
|
|
|
|
*/
|
|
|
|
static public function setContentDispositionHeader( $filename, $type = 'attachment' ) {
|
2014-01-26 17:46:09 +00:00
|
|
|
if (OC_Request::isUserAgent(array(
|
|
|
|
OC_Request::USER_AGENT_IE,
|
|
|
|
OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME,
|
|
|
|
OC_Request::USER_AGENT_FREEBOX
|
|
|
|
))) {
|
2013-12-10 11:05:39 +00:00
|
|
|
header( 'Content-Disposition: ' . rawurlencode($type) . '; filename="' . rawurlencode( $filename ) . '"' );
|
|
|
|
} else {
|
|
|
|
header( 'Content-Disposition: ' . rawurlencode($type) . '; filename*=UTF-8\'\'' . rawurlencode( $filename )
|
|
|
|
. '; filename="' . rawurlencode( $filename ) . '"' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:33:39 +00:00
|
|
|
/**
|
|
|
|
* @brief Send file as response, checking and setting caching headers
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param string $filepath of file to send
|
2012-02-17 20:33:39 +00:00
|
|
|
*/
|
2012-02-17 20:35:31 +00:00
|
|
|
static public function sendFile($filepath) {
|
2012-02-13 22:35:33 +00:00
|
|
|
$fp = fopen($filepath, 'rb');
|
|
|
|
if ($fp) {
|
|
|
|
self::setLastModifiedHeader(filemtime($filepath));
|
|
|
|
self::setETagHeader(md5_file($filepath));
|
|
|
|
|
|
|
|
header('Content-Length: '.filesize($filepath));
|
|
|
|
fpassthru($fp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self::setStatus(self::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
}
|
2012-02-12 16:20:30 +00:00
|
|
|
}
|