2012-02-12 16:20:30 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Stefan Weil <sw@weilnetz.de>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2012-02-12 16:20:30 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2012-02-12 16:20:30 +00:00
|
|
|
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;
|
2014-05-04 13:51:08 +00:00
|
|
|
const STATUS_BAD_REQUEST = 400;
|
2016-10-04 12:51:54 +00:00
|
|
|
const STATUS_FORBIDDEN = 403;
|
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;
|
2014-03-14 12:58:34 +00:00
|
|
|
const STATUS_SERVICE_UNAVAILABLE = 503;
|
2012-02-12 16:20:30 +00:00
|
|
|
|
2012-02-17 20:33:39 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* 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
|
2016-04-07 17:51:27 +00:00
|
|
|
* null cache indefinitely
|
2012-02-17 20:33:39 +00:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* disable browser caching
|
2012-02-17 20:33:39 +00:00
|
|
|
* @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
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Set response status
|
2014-04-21 13:44:54 +00:00
|
|
|
* @param int $status a HTTP status code, see also the STATUS constants
|
2012-02-17 20:33:39 +00:00
|
|
|
*/
|
2012-02-12 16:20:30 +00:00
|
|
|
static public function setStatus($status) {
|
2015-10-30 17:05:30 +00:00
|
|
|
$protocol = \OC::$server->getRequest()->getHttpProtocol();
|
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;
|
2014-03-14 12:58:34 +00:00
|
|
|
case self::STATUS_SERVICE_UNAVAILABLE;
|
|
|
|
$status = $status . ' Service Unavailable';
|
|
|
|
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
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* 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
|
|
|
/**
|
2017-07-19 17:44:10 +00:00
|
|
|
* Set response expire time
|
|
|
|
* @param string|DateTime|int $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
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param string $etag token to use for modification check
|
2012-02-17 20:33:39 +00:00
|
|
|
*/
|
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
|
2016-04-07 17:51:27 +00:00
|
|
|
* @param int|DateTime|string $lastModified time when the response was last modified
|
2012-02-17 20:33:39 +00:00
|
|
|
*/
|
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' ) {
|
2015-02-10 12:02:48 +00:00
|
|
|
if (\OC::$server->getRequest()->isUserAgent(
|
|
|
|
[
|
|
|
|
\OC\AppFramework\Http\Request::USER_AGENT_IE,
|
|
|
|
\OC\AppFramework\Http\Request::USER_AGENT_ANDROID_MOBILE_CHROME,
|
|
|
|
\OC\AppFramework\Http\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 ) . '"' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:24:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the content length header (with possible workarounds)
|
|
|
|
* @param string|int|float $length Length to be sent
|
|
|
|
*/
|
|
|
|
static public function setContentLengthHeader($length) {
|
|
|
|
if (PHP_INT_SIZE === 4) {
|
|
|
|
if ($length > PHP_INT_MAX && stripos(PHP_SAPI, 'apache') === 0) {
|
|
|
|
// Apache PHP SAPI casts Content-Length headers to PHP integers.
|
|
|
|
// This enforces a limit of PHP_INT_MAX (2147483647 on 32-bit
|
|
|
|
// platforms). So, if the length is greater than PHP_INT_MAX,
|
|
|
|
// we just do not send a Content-Length header to prevent
|
|
|
|
// bodies from being received incompletely.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Convert signed integer or float to unsigned base-10 string.
|
|
|
|
$lfh = new \OC\LargeFileHelper;
|
|
|
|
$length = $lfh->formatUnsignedInteger($length);
|
|
|
|
}
|
|
|
|
header('Content-Length: '.$length);
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:33:39 +00:00
|
|
|
/**
|
2015-04-15 08:25:07 +00:00
|
|
|
* Send file as response, checking and setting caching headers
|
|
|
|
* @param string $filepath of file to send
|
2015-04-20 08:30:16 +00:00
|
|
|
* @deprecated 8.1.0 - Use \OCP\AppFramework\Http\StreamResponse or another AppFramework controller instead
|
2015-04-15 08:25:07 +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));
|
|
|
|
|
2015-03-26 15:24:15 +00:00
|
|
|
self::setContentLengthHeader(filesize($filepath));
|
2012-02-13 22:35:33 +00:00
|
|
|
fpassthru($fp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self::setStatus(self::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
}
|
2014-05-12 13:14:01 +00:00
|
|
|
|
2015-02-09 15:30:01 +00:00
|
|
|
/**
|
2014-05-12 13:14:01 +00:00
|
|
|
* This function adds some security related headers to all requests served via base.php
|
|
|
|
* The implementation of this function has to happen here to ensure that all third-party
|
|
|
|
* components (e.g. SabreDAV) also benefit from this headers.
|
|
|
|
*/
|
|
|
|
public static function addSecurityHeaders() {
|
2015-02-09 15:30:01 +00:00
|
|
|
/**
|
|
|
|
* FIXME: Content Security Policy for legacy ownCloud components. This
|
|
|
|
* can be removed once \OCP\AppFramework\Http\Response from the AppFramework
|
|
|
|
* is used everywhere.
|
|
|
|
* @see \OCP\AppFramework\Http\Response::getHeaders
|
|
|
|
*/
|
|
|
|
$policy = 'default-src \'self\'; '
|
2016-10-26 07:41:18 +00:00
|
|
|
. 'script-src \'self\' \'unsafe-eval\' \'nonce-'.\OC::$server->getContentSecurityPolicyNonceManager()->getNonce().'\'; '
|
2014-05-12 13:14:01 +00:00
|
|
|
. 'style-src \'self\' \'unsafe-inline\'; '
|
|
|
|
. 'frame-src *; '
|
2015-09-29 12:18:12 +00:00
|
|
|
. 'img-src * data: blob:; '
|
2014-05-12 13:14:01 +00:00
|
|
|
. 'font-src \'self\' data:; '
|
2014-10-29 23:00:40 +00:00
|
|
|
. 'media-src *; '
|
2017-03-28 21:55:31 +00:00
|
|
|
. 'connect-src *; '
|
|
|
|
. 'object-src \'none\'; '
|
|
|
|
. 'base-uri \'self\'; ';
|
2014-05-12 13:14:01 +00:00
|
|
|
header('Content-Security-Policy:' . $policy);
|
2017-04-12 10:37:32 +00:00
|
|
|
header('X-Frame-Options: SAMEORIGIN'); // Disallow iFraming from other domains
|
2015-03-26 14:30:00 +00:00
|
|
|
|
|
|
|
// Send fallback headers for installations that don't have the possibility to send
|
|
|
|
// custom headers on the webserver side
|
|
|
|
if(getenv('modHeadersAvailable') !== 'true') {
|
|
|
|
header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters
|
|
|
|
header('X-Content-Type-Options: nosniff'); // Disable sniffing the content type for IE
|
|
|
|
header('X-Robots-Tag: none'); // https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
|
2016-01-11 20:20:42 +00:00
|
|
|
header('X-Download-Options: noopen'); // https://msdn.microsoft.com/en-us/library/jj542450(v=vs.85).aspx
|
|
|
|
header('X-Permitted-Cross-Domain-Policies: none'); // https://www.adobe.com/devnet/adobe-media-server/articles/cross-domain-xml-for-streaming.html
|
2015-03-26 14:30:00 +00:00
|
|
|
}
|
2014-05-12 13:14:01 +00:00
|
|
|
}
|
|
|
|
|
2012-02-12 16:20:30 +00:00
|
|
|
}
|