2012-02-12 20:20:30 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Andreas Fischer <bantu@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Sergio Bertolín <sbertolin@solidgear.es>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Stefan Weil <sw@weilnetz.de>
|
2015-03-26 13:44:34 +03: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 20:20:30 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2012-02-12 20:20:30 +04:00
|
|
|
class OC_Response {
|
2012-02-12 23:38:06 +04:00
|
|
|
const STATUS_FOUND = 304;
|
2012-02-12 20:20:30 +04:00
|
|
|
const STATUS_NOT_MODIFIED = 304;
|
2012-02-12 23:38:06 +04:00
|
|
|
const STATUS_TEMPORARY_REDIRECT = 307;
|
2014-05-04 17:51:08 +04:00
|
|
|
const STATUS_BAD_REQUEST = 400;
|
2016-10-04 15:51:54 +03:00
|
|
|
const STATUS_FORBIDDEN = 403;
|
2012-02-14 02:35:33 +04:00
|
|
|
const STATUS_NOT_FOUND = 404;
|
2013-06-10 15:45:19 +04:00
|
|
|
const STATUS_INTERNAL_SERVER_ERROR = 500;
|
2014-03-14 16:58:34 +04:00
|
|
|
const STATUS_SERVICE_UNAVAILABLE = 503;
|
2012-02-12 20:20:30 +04:00
|
|
|
|
2012-02-18 00:33:39 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Set response status
|
2014-04-21 17:44:54 +04:00
|
|
|
* @param int $status a HTTP status code, see also the STATUS constants
|
2012-02-18 00:33:39 +04:00
|
|
|
*/
|
2012-02-12 20:20:30 +04:00
|
|
|
static public function setStatus($status) {
|
2015-10-30 20:05:30 +03:00
|
|
|
$protocol = \OC::$server->getRequest()->getHttpProtocol();
|
2012-02-12 20:20:30 +04:00
|
|
|
switch($status) {
|
|
|
|
case self::STATUS_NOT_MODIFIED:
|
|
|
|
$status = $status . ' Not Modified';
|
|
|
|
break;
|
2012-02-12 23:38:06 +04: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-14 02:35:33 +04:00
|
|
|
case self::STATUS_NOT_FOUND;
|
|
|
|
$status = $status . ' Not Found';
|
|
|
|
break;
|
2013-06-10 15:45:19 +04:00
|
|
|
case self::STATUS_INTERNAL_SERVER_ERROR;
|
|
|
|
$status = $status . ' Internal Server Error';
|
|
|
|
break;
|
2014-03-14 16:58:34 +04:00
|
|
|
case self::STATUS_SERVICE_UNAVAILABLE;
|
|
|
|
$status = $status . ' Service Unavailable';
|
|
|
|
break;
|
2012-02-12 20:20:30 +04:00
|
|
|
}
|
2012-02-12 23:38:06 +04:00
|
|
|
header($protocol.' '.$status);
|
|
|
|
}
|
|
|
|
|
2013-12-10 15:05:39 +04: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 15:02:48 +03: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 15:05:39 +04: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 18:24:15 +03: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);
|
|
|
|
}
|
|
|
|
|
2015-02-09 18:30:01 +03:00
|
|
|
/**
|
2014-05-12 17:14:01 +04: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 18:30:01 +03: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 10:41:18 +03:00
|
|
|
. 'script-src \'self\' \'unsafe-eval\' \'nonce-'.\OC::$server->getContentSecurityPolicyNonceManager()->getNonce().'\'; '
|
2014-05-12 17:14:01 +04:00
|
|
|
. 'style-src \'self\' \'unsafe-inline\'; '
|
|
|
|
. 'frame-src *; '
|
2015-09-29 15:18:12 +03:00
|
|
|
. 'img-src * data: blob:; '
|
2014-05-12 17:14:01 +04:00
|
|
|
. 'font-src \'self\' data:; '
|
2014-10-30 02:00:40 +03:00
|
|
|
. 'media-src *; '
|
2017-03-29 00:55:31 +03:00
|
|
|
. 'connect-src *; '
|
|
|
|
. 'object-src \'none\'; '
|
|
|
|
. 'base-uri \'self\'; ';
|
2014-05-12 17:14:01 +04:00
|
|
|
header('Content-Security-Policy:' . $policy);
|
2017-04-12 13:37:32 +03:00
|
|
|
header('X-Frame-Options: SAMEORIGIN'); // Disallow iFraming from other domains
|
2015-03-26 17:30:00 +03: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 23:20:42 +03: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 17:30:00 +03:00
|
|
|
}
|
2014-05-12 17:14:01 +04:00
|
|
|
}
|
|
|
|
|
2012-02-12 20:20:30 +04:00
|
|
|
}
|