Blame view

sources/lib/public/appframework/http/response.php 3.99 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
  <?php
  /**
   * ownCloud - App Framework
   *
   * @author Bernhard Posselt, Thomas Tanghus, Bart Visscher
6d9380f96   Cédric Dupont   Update sources OC...
6
   * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
31b7f2792   Kload   Upgrade to ownclo...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library 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 along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  /**
   * Public interface of ownCloud for apps to use.
   * AppFramework\HTTP\Response class
   */
  
  namespace OCP\AppFramework\Http;
  
  use OCP\AppFramework\Http;
  
  /**
   * Base class for responses. Also used to just send headers.
   *
   * It handles headers, HTTP status code, last modified and ETag.
   */
  class Response {
  
  	/**
  	 * Headers - defaults to ['Cache-Control' => 'no-cache, must-revalidate']
  	 * @var array
  	 */
  	private $headers = array(
  		'Cache-Control' => 'no-cache, must-revalidate'
  	);
  
  
  	/**
  	 * HTTP status code - defaults to STATUS OK
  	 * @var string
  	 */
  	private $status = Http::STATUS_OK;
  
  
  	/**
  	 * Last modified date
  	 * @var \DateTime
  	 */
  	private $lastModified;
  
  
  	/**
  	 * ETag
  	 * @var string
  	 */
  	private $ETag;
  
  
  	/**
  	 * Caches the response
  	 * @param int $cacheSeconds the amount of seconds that should be cached
  	 * if 0 then caching will be disabled
  	 */
  	public function cacheFor($cacheSeconds) {
  
  		if($cacheSeconds > 0) {
  			$this->addHeader('Cache-Control', 'max-age=' . $cacheSeconds .
  				', must-revalidate');
  		} else {
  			$this->addHeader('Cache-Control', 'no-cache, must-revalidate');
  		}
6d9380f96   Cédric Dupont   Update sources OC...
82
  		return $this;
31b7f2792   Kload   Upgrade to ownclo...
83
84
85
86
87
88
89
90
  	}
  
  
  	/**
  	 * Adds a new header to the response that will be called before the render
  	 * function
  	 * @param string $name The name of the HTTP header
  	 * @param string $value The value, null will delete it
6d9380f96   Cédric Dupont   Update sources OC...
91
  	 * @return Response Reference to this object
31b7f2792   Kload   Upgrade to ownclo...
92
93
  	 */
  	public function addHeader($name, $value) {
6d9380f96   Cédric Dupont   Update sources OC...
94
95
96
  		$name = trim($name);  // always remove leading and trailing whitespace
  		                      // to be able to reliably check for security 
  		                      // headers
31b7f2792   Kload   Upgrade to ownclo...
97
98
99
100
101
  		if(is_null($value)) {
  			unset($this->headers[$name]);
  		} else {
  			$this->headers[$name] = $value;
  		}
6d9380f96   Cédric Dupont   Update sources OC...
102
103
  
  		return $this;
31b7f2792   Kload   Upgrade to ownclo...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  	}
  
  
  	/**
  	 * Returns the set headers
  	 * @return array the headers
  	 */
  	public function getHeaders() {
  		$mergeWith = array();
  
  		if($this->lastModified) {
  			$mergeWith['Last-Modified'] =
  				$this->lastModified->format(\DateTime::RFC2822);
  		}
  
  		if($this->ETag) {
  			$mergeWith['ETag'] = '"' . $this->ETag . '"';
  		}
  
  		return array_merge($mergeWith, $this->headers);
  	}
  
  
  	/**
  	 * By default renders no output
  	 * @return null
  	 */
  	public function render() {
  		return null;
  	}
  
  
  	/**
  	* Set response status
  	* @param int $status a HTTP status code, see also the STATUS constants
6d9380f96   Cédric Dupont   Update sources OC...
139
  	* @return Response Reference to this object
31b7f2792   Kload   Upgrade to ownclo...
140
141
142
  	*/
  	public function setStatus($status) {
  		$this->status = $status;
6d9380f96   Cédric Dupont   Update sources OC...
143
144
  
  		return $this;
31b7f2792   Kload   Upgrade to ownclo...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  	}
  
  
  	/**
  	 * Get response status
  	 */
  	public function getStatus() {
  		return $this->status;
  	}
  
  
  	/**
  	 * Get the ETag
  	 * @return string the etag
  	 */
  	public function getETag() {
  		return $this->ETag;
  	}
  
  
  	/**
  	 * Get "last modified" date
6d9380f96   Cédric Dupont   Update sources OC...
167
  	 * @return \DateTime RFC2822 formatted last modified date
31b7f2792   Kload   Upgrade to ownclo...
168
169
170
171
172
173
174
175
176
  	 */
  	public function getLastModified() {
  		return $this->lastModified;
  	}
  
  
  	/**
  	 * Set the ETag
  	 * @param string $ETag
6d9380f96   Cédric Dupont   Update sources OC...
177
  	 * @return Response Reference to this object
31b7f2792   Kload   Upgrade to ownclo...
178
179
180
  	 */
  	public function setETag($ETag) {
  		$this->ETag = $ETag;
6d9380f96   Cédric Dupont   Update sources OC...
181
182
  
  		return $this;
31b7f2792   Kload   Upgrade to ownclo...
183
184
185
186
187
188
  	}
  
  
  	/**
  	 * Set "last modified" date
  	 * @param \DateTime $lastModified
6d9380f96   Cédric Dupont   Update sources OC...
189
  	 * @return Response Reference to this object
31b7f2792   Kload   Upgrade to ownclo...
190
191
192
  	 */
  	public function setLastModified($lastModified) {
  		$this->lastModified = $lastModified;
6d9380f96   Cédric Dupont   Update sources OC...
193
194
  
  		return $this;
31b7f2792   Kload   Upgrade to ownclo...
195
196
197
198
  	}
  
  
  }