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-12 16:20:30 +00:00
|
|
|
|
|
|
|
static public function enableCaching() {
|
|
|
|
header('Cache-Control: cache');
|
|
|
|
header('Pragma: cache');
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12 16:20:30 +00:00
|
|
|
}
|
2012-02-12 19:38:06 +00:00
|
|
|
header($protocol.' '.$status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function redirect($location) {
|
|
|
|
self::setStatus(self::STATUS_TEMPORARY_REDIRECT);
|
|
|
|
header('Location: '.$location);
|
2012-02-12 16:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static public function setETagHeader($etag) {
|
|
|
|
if (empty($etag)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self::enableCaching();
|
|
|
|
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
|
|
|
|
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
|
|
|
|
self::setStatus(self::STATUS_NOT_MODIFIED);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
header('ETag: '.$etag);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function setLastModifiedHeader($lastModified) {
|
|
|
|
if (empty($lastModified)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($lastModified instanceof DateTime) {
|
|
|
|
$lastModified = $lastModified->format(DateTime::RFC2822);
|
|
|
|
}
|
|
|
|
self::enableCaching();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|