Completed
Pull Request — master (#8792)
by Morris
18:52
created

OC_Response   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 121
rs 10
wmc 16
lcom 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
C setStatus() 0 29 8
A setContentDispositionHeader() 0 13 2
A setContentLengthHeader() 0 16 4
B addSecurityHeaders() 0 30 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Andreas Fischer <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Jörn Friedrich Dreyer <[email protected]>
9
 * @author Lukas Reschke <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Robin McCorkell <[email protected]>
12
 * @author Sergio Bertolín <[email protected]>
13
 * @author Stefan Weil <[email protected]>
14
 * @author Thomas Müller <[email protected]>
15
 * @author Vincent Petry <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
33
class OC_Response {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
34
	const STATUS_FOUND = 302;
35
	const STATUS_NOT_MODIFIED = 304;
36
	const STATUS_TEMPORARY_REDIRECT = 307;
37
	const STATUS_BAD_REQUEST = 400;
38
	const STATUS_FORBIDDEN = 403;
39
	const STATUS_NOT_FOUND = 404;
40
	const STATUS_INTERNAL_SERVER_ERROR = 500;
41
	const STATUS_SERVICE_UNAVAILABLE = 503;
42
43
	/**
44
	* Set response status
45
	* @param int $status a HTTP status code, see also the STATUS constants
46
	*/
47
	static public function setStatus($status) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
48
		$protocol = \OC::$server->getRequest()->getHttpProtocol();
49
		switch($status) {
50
			case self::STATUS_NOT_MODIFIED:
51
				$status = $status . ' Not Modified';
52
				break;
53
			case self::STATUS_TEMPORARY_REDIRECT:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
54
				if ($protocol == 'HTTP/1.0') {
55
					$status = self::STATUS_FOUND;
56
					// fallthrough
57
				} else {
58
					$status = $status . ' Temporary Redirect';
59
					break;
60
				}
61
			case self::STATUS_FOUND;
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
62
				$status = $status . ' Found';
63
				break;
64
			case self::STATUS_NOT_FOUND;
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
65
				$status = $status . ' Not Found';
66
				break;
67
			case self::STATUS_INTERNAL_SERVER_ERROR;
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
68
				$status = $status . ' Internal Server Error';
69
				break;
70
			case self::STATUS_SERVICE_UNAVAILABLE;
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
71
				$status = $status . ' Service Unavailable';
72
				break;
73
		}
74
		header($protocol.' '.$status);
75
	}
76
77
	/**
78
	 * Sets the content disposition header (with possible workarounds)
79
	 * @param string $filename file name
80
	 * @param string $type disposition type, either 'attachment' or 'inline'
81
	 */
82
	static public function setContentDispositionHeader( $filename, $type = 'attachment' ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
83
		if (\OC::$server->getRequest()->isUserAgent(
84
			[
85
				\OC\AppFramework\Http\Request::USER_AGENT_IE,
86
				\OC\AppFramework\Http\Request::USER_AGENT_ANDROID_MOBILE_CHROME,
87
				\OC\AppFramework\Http\Request::USER_AGENT_FREEBOX,
88
			])) {
89
			header( 'Content-Disposition: ' . rawurlencode($type) . '; filename="' . rawurlencode( $filename ) . '"' );
90
		} else {
91
			header( 'Content-Disposition: ' . rawurlencode($type) . '; filename*=UTF-8\'\'' . rawurlencode( $filename )
92
												 . '; filename="' . rawurlencode( $filename ) . '"' );
93
		}
94
	}
95
96
	/**
97
	 * Sets the content length header (with possible workarounds)
98
	 * @param string|int|float $length Length to be sent
99
	 */
100
	static public function setContentLengthHeader($length) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
101
		if (PHP_INT_SIZE === 4) {
102
			if ($length > PHP_INT_MAX && stripos(PHP_SAPI, 'apache') === 0) {
103
				// Apache PHP SAPI casts Content-Length headers to PHP integers.
104
				// This enforces a limit of PHP_INT_MAX (2147483647 on 32-bit
105
				// platforms). So, if the length is greater than PHP_INT_MAX,
106
				// we just do not send a Content-Length header to prevent
107
				// bodies from being received incompletely.
108
				return;
109
			}
110
			// Convert signed integer or float to unsigned base-10 string.
111
			$lfh = new \OC\LargeFileHelper;
112
			$length = $lfh->formatUnsignedInteger($length);
113
		}
114
		header('Content-Length: '.$length);
115
	}
116
117
	/**
118
	 * This function adds some security related headers to all requests served via base.php
119
	 * The implementation of this function has to happen here to ensure that all third-party
120
	 * components (e.g. SabreDAV) also benefit from this headers.
121
	 */
122
	public static function addSecurityHeaders() {
123
		/**
124
		 * FIXME: Content Security Policy for legacy ownCloud components. This
125
		 * can be removed once \OCP\AppFramework\Http\Response from the AppFramework
126
		 * is used everywhere.
127
		 * @see \OCP\AppFramework\Http\Response::getHeaders
128
		 */
129
		$policy = 'default-src \'self\'; '
130
			. 'script-src \'self\' \'unsafe-eval\' \'nonce-'.\OC::$server->getContentSecurityPolicyNonceManager()->getNonce().'\'; '
131
			. 'style-src \'self\' \'unsafe-inline\'; '
132
			. 'frame-src *; '
133
			. 'img-src * data: blob:; '
134
			. 'font-src \'self\' data:; '
135
			. 'media-src *; ' 
136
			. 'connect-src *; '
137
			. 'object-src \'none\'; '
138
			. 'base-uri \'self\'; ';
139
		header('Content-Security-Policy:' . $policy);
140
		header('X-Frame-Options: SAMEORIGIN'); // Disallow iFraming from other domains
141
142
		// Send fallback headers for installations that don't have the possibility to send
143
		// custom headers on the webserver side
144
		if(getenv('modHeadersAvailable') !== 'true') {
145
			header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters
146
			header('X-Content-Type-Options: nosniff'); // Disable sniffing the content type for IE
147
			header('X-Robots-Tag: none'); // https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
148
			header('X-Download-Options: noopen'); // https://msdn.microsoft.com/en-us/library/jj542450(v=vs.85).aspx
149
			header('X-Permitted-Cross-Domain-Policies: none'); // https://www.adobe.com/devnet/adobe-media-server/articles/cross-domain-xml-for-streaming.html
150
		}
151
	}
152
153
}
154