Blame view

sources/lib/private/response.php 6.5 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
  <?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 {
31b7f2792   Kload   Upgrade to ownclo...
10
  	const STATUS_FOUND = 304;
03e52840d   Kload   Init
11
12
  	const STATUS_NOT_MODIFIED = 304;
  	const STATUS_TEMPORARY_REDIRECT = 307;
6d9380f96   Cédric Dupont   Update sources OC...
13
  	const STATUS_BAD_REQUEST = 400;
03e52840d   Kload   Init
14
  	const STATUS_NOT_FOUND = 404;
31b7f2792   Kload   Upgrade to ownclo...
15
  	const STATUS_INTERNAL_SERVER_ERROR = 500;
837968727   Kload   [enh] Upgrade to ...
16
  	const STATUS_SERVICE_UNAVAILABLE = 503;
03e52840d   Kload   Init
17
18
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
19
20
  	* Enable response caching by sending correct HTTP headers
  	* @param integer $cache_time time to cache the response
03e52840d   Kload   Init
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  	*  >0		cache time in seconds
  	*  0 and <0	enable default browser caching
  	*  null		cache indefinitly
  	*/
  	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');
  		}
  
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
45
  	* disable browser caching
03e52840d   Kload   Init
46
47
48
49
50
51
52
  	* @see enableCaching with cache_time = 0
  	*/
  	static public function disableCaching() {
  		self::enableCaching(0);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
53
54
  	* Set response status
  	* @param int $status a HTTP status code, see also the STATUS constants
03e52840d   Kload   Init
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  	*/
  	static public function setStatus($status) {
  		$protocol = $_SERVER['SERVER_PROTOCOL'];
  		switch($status) {
  			case self::STATUS_NOT_MODIFIED:
  				$status = $status . ' Not Modified';
  				break;
  			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;
  			case self::STATUS_NOT_FOUND;
  				$status = $status . ' Not Found';
  				break;
31b7f2792   Kload   Upgrade to ownclo...
76
77
78
  			case self::STATUS_INTERNAL_SERVER_ERROR;
  				$status = $status . ' Internal Server Error';
  				break;
837968727   Kload   [enh] Upgrade to ...
79
80
81
  			case self::STATUS_SERVICE_UNAVAILABLE;
  				$status = $status . ' Service Unavailable';
  				break;
03e52840d   Kload   Init
82
83
84
85
86
  		}
  		header($protocol.' '.$status);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
87
88
  	* Send redirect response
  	* @param string $location to redirect to
03e52840d   Kload   Init
89
90
91
92
93
94
95
  	*/
  	static public function redirect($location) {
  		self::setStatus(self::STATUS_TEMPORARY_REDIRECT);
  		header('Location: '.$location);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
96
97
  	* Set reponse expire time
  	* @param string|DateTime $expires date-time when the response expires
03e52840d   Kload   Init
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  	*  string for DateInterval from now
  	*  DateTime object when to expire response
  	*/
  	static public function setExpiresHeader($expires) {
  		if (is_string($expires) && $expires[0] == 'P') {
  			$interval = $expires;
  			$expires = new DateTime('now');
  			$expires->add(new DateInterval($interval));
  		}
  		if ($expires instanceof DateTime) {
  			$expires->setTimezone(new DateTimeZone('GMT'));
  			$expires = $expires->format(DateTime::RFC2822);
  		}
  		header('Expires: '.$expires);
  	}
  
  	/**
  	* Checks and set ETag header, when the request matches sends a
  	* 'not modified' response
6d9380f96   Cédric Dupont   Update sources OC...
117
  	* @param string $etag token to use for modification check
03e52840d   Kload   Init
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  	*/
  	static public function setETagHeader($etag) {
  		if (empty($etag)) {
  			return;
  		}
  		$etag = '"'.$etag.'"';
  		if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
  		    trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
  			self::setStatus(self::STATUS_NOT_MODIFIED);
  			exit;
  		}
  		header('ETag: '.$etag);
  	}
  
  	/**
  	* Checks and set Last-Modified header, when the request matches sends a
  	* 'not modified' response
6d9380f96   Cédric Dupont   Update sources OC...
135
  	* @param int|DateTime|string $lastModified time when the reponse was last modified
03e52840d   Kload   Init
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  	*/
  	static public function setLastModifiedHeader($lastModified) {
  		if (empty($lastModified)) {
  			return;
  		}
  		if (is_int($lastModified)) {
  			$lastModified = gmdate(DateTime::RFC2822, $lastModified);
  		}
  		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);
  	}
  
  	/**
a293d369c   Kload   Update sources to...
156
157
158
159
160
  	 * 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' ) {
6d9380f96   Cédric Dupont   Update sources OC...
161
162
163
164
165
  		if (OC_Request::isUserAgent(array(
  				OC_Request::USER_AGENT_IE,
  				OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  				OC_Request::USER_AGENT_FREEBOX
  			))) {
a293d369c   Kload   Update sources to...
166
167
168
169
170
171
172
173
  			header( 'Content-Disposition: ' . rawurlencode($type) . '; filename="' . rawurlencode( $filename ) . '"' );
  		} else {
  			header( 'Content-Disposition: ' . rawurlencode($type) . '; filename*=UTF-8\'\'' . rawurlencode( $filename )
  												 . '; filename="' . rawurlencode( $filename ) . '"' );
  		}
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
174
175
  	* Send file as response, checking and setting caching headers
  	* @param string $filepath of file to send
03e52840d   Kload   Init
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  	*/
  	static public function sendFile($filepath) {
  		$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);
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  
  	/*
  	 * 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() {
  		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
  
  		// iFrame Restriction Policy
  		$xFramePolicy = OC_Config::getValue('xframe_restriction', true);
  		if ($xFramePolicy) {
  			header('X-Frame-Options: Sameorigin'); // Disallow iFraming from other domains
  		}
  
  		// Content Security Policy
  		// If you change the standard policy, please also change it in config.sample.php
  		$policy = OC_Config::getValue('custom_csp_policy',
  			'default-src \'self\'; '
  			. 'script-src \'self\' \'unsafe-eval\'; '
  			. 'style-src \'self\' \'unsafe-inline\'; '
  			. 'frame-src *; '
  			. 'img-src *; '
  			. 'font-src \'self\' data:; '
  			. 'media-src *');
  		header('Content-Security-Policy:' . $policy);
  
  		// https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
  		header('X-Robots-Tag: none');
  	}
03e52840d   Kload   Init
221
  }