2013-08-17 13:16:48 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - App Framework
|
|
|
|
*
|
|
|
|
* @author Bernhard Posselt, Thomas Tanghus, Bart Visscher
|
2014-05-07 00:25:05 +04:00
|
|
|
* @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-11-25 19:28:24 +04:00
|
|
|
/**
|
|
|
|
* Public interface of ownCloud for apps to use.
|
|
|
|
* AppFramework\HTTP\Response class
|
|
|
|
*/
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2013-08-21 03:00:26 +04:00
|
|
|
namespace OCP\AppFramework\Http;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2013-10-23 07:57:34 +04:00
|
|
|
use OCP\AppFramework\Http;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
/**
|
2013-10-17 02:07:29 +04:00
|
|
|
* Base class for responses. Also used to just send headers.
|
|
|
|
*
|
|
|
|
* It handles headers, HTTP status code, last modified and ETag.
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
|
|
|
class Response {
|
|
|
|
|
|
|
|
/**
|
2013-10-17 02:07:29 +04:00
|
|
|
* Headers - defaults to ['Cache-Control' => 'no-cache, must-revalidate']
|
|
|
|
* @var array
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
|
|
|
private $headers = array(
|
|
|
|
'Cache-Control' => 'no-cache, must-revalidate'
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2014-11-27 16:19:00 +03:00
|
|
|
/**
|
|
|
|
* Cookies that will be need to be constructed as header
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $cookies = array();
|
|
|
|
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
/**
|
2013-10-17 02:07:29 +04:00
|
|
|
* HTTP status code - defaults to STATUS OK
|
2014-11-27 16:19:00 +03:00
|
|
|
* @var int
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
|
|
|
private $status = Http::STATUS_OK;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-17 02:07:29 +04:00
|
|
|
* Last modified date
|
2013-08-17 13:16:48 +04:00
|
|
|
* @var \DateTime
|
|
|
|
*/
|
|
|
|
private $lastModified;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-17 02:07:29 +04:00
|
|
|
* ETag
|
2013-08-17 13:16:48 +04:00
|
|
|
* @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
|
2014-11-27 16:19:00 +03:00
|
|
|
* @return $this
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
|
|
|
public function cacheFor($cacheSeconds) {
|
|
|
|
|
|
|
|
if($cacheSeconds > 0) {
|
2013-11-03 16:51:39 +04:00
|
|
|
$this->addHeader('Cache-Control', 'max-age=' . $cacheSeconds .
|
2013-08-17 13:16:48 +04:00
|
|
|
', must-revalidate');
|
|
|
|
} else {
|
|
|
|
$this->addHeader('Cache-Control', 'no-cache, must-revalidate');
|
|
|
|
}
|
|
|
|
|
2014-03-10 02:01:16 +04:00
|
|
|
return $this;
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
2014-11-27 16:19:00 +03:00
|
|
|
/**
|
|
|
|
* Adds a new cookie to the response
|
|
|
|
* @param string $name The name of the cookie
|
|
|
|
* @param string $value The value of the cookie
|
|
|
|
* @param \DateTime|null $expireDate Date on that the cookie should expire, if set
|
|
|
|
* to null cookie will be considered as session
|
|
|
|
* cookie.
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function addCookie($name, $value, \DateTime $expireDate = null) {
|
|
|
|
$this->cookies[$name] = array('value' => $value, 'expireDate' => $expireDate);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the specified cookies
|
|
|
|
* @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null))
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setCookies(array $cookies) {
|
|
|
|
$this->cookies = $cookies;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates the specified cookie
|
|
|
|
* @param string $name
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function invalidateCookie($name) {
|
|
|
|
$this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00'));
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates the specified cookies
|
|
|
|
* @param array $cookieNames array('foo', 'bar')
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function invalidateCookies(array $cookieNames) {
|
|
|
|
foreach($cookieNames as $cookieName) {
|
|
|
|
$this->invalidateCookie($cookieName);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the cookies
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCookies() {
|
|
|
|
return $this->cookies;
|
|
|
|
}
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2014-11-27 16:19:00 +03:00
|
|
|
* @return $this
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
|
|
|
public function addHeader($name, $value) {
|
2014-05-08 13:47:18 +04:00
|
|
|
$name = trim($name); // always remove leading and trailing whitespace
|
2014-10-28 18:34:04 +03:00
|
|
|
// to be able to reliably check for security
|
2014-05-08 13:47:18 +04:00
|
|
|
// headers
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
if(is_null($value)) {
|
|
|
|
unset($this->headers[$name]);
|
|
|
|
} else {
|
|
|
|
$this->headers[$name] = $value;
|
|
|
|
}
|
2014-03-10 02:01:16 +04:00
|
|
|
|
|
|
|
return $this;
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-28 18:34:04 +03:00
|
|
|
/**
|
|
|
|
* Set the headers
|
2014-11-27 16:19:00 +03:00
|
|
|
* @param array $headers value header pairs
|
|
|
|
* @return $this
|
2014-10-28 18:34:04 +03:00
|
|
|
*/
|
2014-11-27 16:19:00 +03:00
|
|
|
public function setHeaders(array $headers) {
|
2014-10-28 18:34:04 +03:00
|
|
|
$this->headers = $headers;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
/**
|
|
|
|
* Returns the set headers
|
|
|
|
* @return array the headers
|
|
|
|
*/
|
|
|
|
public function getHeaders() {
|
|
|
|
$mergeWith = array();
|
2013-11-03 16:51:39 +04:00
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
if($this->lastModified) {
|
2013-11-03 16:51:39 +04:00
|
|
|
$mergeWith['Last-Modified'] =
|
2013-08-17 13:16:48 +04:00
|
|
|
$this->lastModified->format(\DateTime::RFC2822);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->ETag) {
|
|
|
|
$mergeWith['ETag'] = '"' . $this->ETag . '"';
|
|
|
|
}
|
2013-11-03 16:51:39 +04:00
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
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
|
2014-03-10 12:31:30 +04:00
|
|
|
* @return Response Reference to this object
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
|
|
|
public function setStatus($status) {
|
|
|
|
$this->status = $status;
|
2014-03-10 02:01:16 +04:00
|
|
|
|
|
|
|
return $this;
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get response status
|
|
|
|
*/
|
|
|
|
public function getStatus() {
|
|
|
|
return $this->status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-17 02:07:29 +04:00
|
|
|
* Get the ETag
|
2013-08-17 13:16:48 +04:00
|
|
|
* @return string the etag
|
|
|
|
*/
|
|
|
|
public function getETag() {
|
|
|
|
return $this->ETag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-17 02:07:29 +04:00
|
|
|
* Get "last modified" date
|
2014-02-06 19:30:58 +04:00
|
|
|
* @return \DateTime RFC2822 formatted last modified date
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
|
|
|
public function getLastModified() {
|
|
|
|
return $this->lastModified;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-17 02:07:29 +04:00
|
|
|
* Set the ETag
|
2013-08-17 13:16:48 +04:00
|
|
|
* @param string $ETag
|
2014-03-10 12:31:30 +04:00
|
|
|
* @return Response Reference to this object
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
|
|
|
public function setETag($ETag) {
|
|
|
|
$this->ETag = $ETag;
|
2014-03-10 02:01:16 +04:00
|
|
|
|
|
|
|
return $this;
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-17 02:07:29 +04:00
|
|
|
* Set "last modified" date
|
2013-08-17 13:16:48 +04:00
|
|
|
* @param \DateTime $lastModified
|
2014-03-10 12:31:30 +04:00
|
|
|
* @return Response Reference to this object
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
|
|
|
public function setLastModified($lastModified) {
|
|
|
|
$this->lastModified = $lastModified;
|
2014-03-10 02:01:16 +04:00
|
|
|
|
|
|
|
return $this;
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|