2013-08-17 09:16:48 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - Request
|
|
|
|
*
|
|
|
|
* @author Thomas Tanghus
|
2014-05-06 20:25:05 +00:00
|
|
|
* @author Bernhard Posselt
|
2013-08-17 09:16:48 +00:00
|
|
|
* @copyright 2013 Thomas Tanghus (thomas@tanghus.net)
|
2014-05-06 20:25:05 +00:00
|
|
|
* @copyright 2014 Bernhard Posselt <dev@bernhard-posselt.com>
|
2013-08-17 09:16:48 +00: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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\AppFramework\Http;
|
|
|
|
|
2013-08-31 19:34:29 +00:00
|
|
|
use OCP\IRequest;
|
2015-02-09 10:41:48 +00:00
|
|
|
use OCP\Security\ISecureRandom;
|
2013-08-20 15:53:58 +00:00
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
/**
|
|
|
|
* Class for accessing variables in the request.
|
|
|
|
* This class provides an immutable object with request variables.
|
|
|
|
*/
|
|
|
|
|
2013-08-20 15:53:58 +00:00
|
|
|
class Request implements \ArrayAccess, \Countable, IRequest {
|
2013-08-17 09:16:48 +00:00
|
|
|
|
2013-10-03 01:56:37 +00:00
|
|
|
protected $inputStream;
|
2013-09-27 23:35:24 +00:00
|
|
|
protected $content;
|
2013-08-17 09:16:48 +00:00
|
|
|
protected $items = array();
|
|
|
|
protected $allowedKeys = array(
|
2013-09-17 16:38:18 +00:00
|
|
|
'get',
|
|
|
|
'post',
|
|
|
|
'files',
|
|
|
|
'server',
|
|
|
|
'env',
|
|
|
|
'cookies',
|
|
|
|
'urlParams',
|
|
|
|
'parameters',
|
2013-09-27 13:16:34 +00:00
|
|
|
'method',
|
|
|
|
'requesttoken',
|
2013-08-17 09:16:48 +00:00
|
|
|
);
|
2015-02-09 10:41:48 +00:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
protected $secureRandom;
|
|
|
|
/** @var string */
|
|
|
|
protected $requestId = '';
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $vars An associative array with the following optional values:
|
2015-02-09 10:41:48 +00:00
|
|
|
* - array 'urlParams' the parameters which were matched from the URL
|
|
|
|
* - array 'get' the $_GET array
|
|
|
|
* - array|string 'post' the $_POST array or JSON string
|
|
|
|
* - array 'files' the $_FILES array
|
|
|
|
* - array 'server' the $_SERVER array
|
|
|
|
* - array 'env' the $_ENV array
|
|
|
|
* - array 'cookies' the $_COOKIE array
|
|
|
|
* - string 'method' the request method (GET, POST etc)
|
|
|
|
* - string|false 'requesttoken' the requesttoken or false when not available
|
|
|
|
* @param ISecureRandom $secureRandom
|
|
|
|
* @param string $stream
|
2013-08-17 09:16:48 +00:00
|
|
|
* @see http://www.php.net/manual/en/reserved.variables.php
|
|
|
|
*/
|
2015-02-09 10:41:48 +00:00
|
|
|
public function __construct(array $vars=array(),
|
|
|
|
ISecureRandom $secureRandom,
|
|
|
|
$stream='php://input') {
|
2014-04-12 13:02:19 +00:00
|
|
|
$this->inputStream = $stream;
|
|
|
|
$this->items['params'] = array();
|
2015-02-09 10:41:48 +00:00
|
|
|
$this->secureRandom = $secureRandom;
|
2013-08-17 09:16:48 +00:00
|
|
|
|
2014-04-12 14:17:49 +00:00
|
|
|
if(!array_key_exists('method', $vars)) {
|
2015-01-15 13:19:56 +00:00
|
|
|
$vars['method'] = 'GET';
|
2014-04-12 14:17:49 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
foreach($this->allowedKeys as $name) {
|
2013-09-27 23:35:24 +00:00
|
|
|
$this->items[$name] = isset($vars[$name])
|
2015-01-15 13:19:56 +00:00
|
|
|
? $vars[$name]
|
2013-08-17 09:16:48 +00:00
|
|
|
: array();
|
|
|
|
}
|
|
|
|
|
2014-04-12 13:02:19 +00:00
|
|
|
// 'application/json' must be decoded manually.
|
|
|
|
if (strpos($this->getHeader('Content-Type'), 'application/json') !== false) {
|
|
|
|
$params = json_decode(file_get_contents($this->inputStream), true);
|
|
|
|
if(count($params) > 0) {
|
|
|
|
$this->items['params'] = $params;
|
2014-04-12 14:17:49 +00:00
|
|
|
if($vars['method'] === 'POST') {
|
2015-01-15 13:19:56 +00:00
|
|
|
$this->items['post'] = $params;
|
2014-04-12 14:17:49 +00:00
|
|
|
}
|
2014-04-12 13:02:19 +00:00
|
|
|
}
|
|
|
|
// Handle application/x-www-form-urlencoded for methods other than GET
|
|
|
|
// or post correctly
|
|
|
|
} elseif($vars['method'] !== 'GET'
|
|
|
|
&& $vars['method'] !== 'POST'
|
|
|
|
&& strpos($this->getHeader('Content-Type'), 'application/x-www-form-urlencoded') !== false) {
|
2015-01-15 13:19:56 +00:00
|
|
|
|
2014-04-12 13:02:19 +00:00
|
|
|
parse_str(file_get_contents($this->inputStream), $params);
|
|
|
|
if(is_array($params)) {
|
|
|
|
$this->items['params'] = $params;
|
|
|
|
}
|
2015-01-15 13:19:56 +00:00
|
|
|
}
|
2013-09-27 23:35:24 +00:00
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
$this->items['parameters'] = array_merge(
|
|
|
|
$this->items['get'],
|
|
|
|
$this->items['post'],
|
2014-04-12 13:02:19 +00:00
|
|
|
$this->items['urlParams'],
|
|
|
|
$this->items['params']
|
2013-08-17 09:16:48 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-15 13:19:56 +00:00
|
|
|
public function setUrlParameters($parameters) {
|
|
|
|
$this->items['urlParams'] = $parameters;
|
|
|
|
$this->items['parameters'] = array_merge(
|
|
|
|
$this->items['parameters'],
|
|
|
|
$this->items['urlParams']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
// Countable method.
|
|
|
|
public function count() {
|
|
|
|
return count(array_keys($this->items['parameters']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ArrayAccess methods
|
|
|
|
*
|
|
|
|
* Gives access to the combined GET, POST and urlParams arrays
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* $var = $request['myvar'];
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* if(!isset($request['myvar']) {
|
|
|
|
* // Do something
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* $request['myvar'] = 'something'; // This throws an exception.
|
|
|
|
*
|
|
|
|
* @param string $offset The key to lookup
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return boolean
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
public function offsetExists($offset) {
|
|
|
|
return isset($this->items['parameters'][$offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see offsetExists
|
|
|
|
*/
|
|
|
|
public function offsetGet($offset) {
|
|
|
|
return isset($this->items['parameters'][$offset])
|
|
|
|
? $this->items['parameters'][$offset]
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see offsetExists
|
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value) {
|
|
|
|
throw new \RuntimeException('You cannot change the contents of the request object');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see offsetExists
|
|
|
|
*/
|
|
|
|
public function offsetUnset($offset) {
|
|
|
|
throw new \RuntimeException('You cannot change the contents of the request object');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Magic property accessors
|
|
|
|
public function __set($name, $value) {
|
|
|
|
throw new \RuntimeException('You cannot change the contents of the request object');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access request variables by method and name.
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* $request->post['myvar']; // Only look for POST variables
|
|
|
|
* $request->myvar; or $request->{'myvar'}; or $request->{$myvar}
|
|
|
|
* Looks in the combined GET, POST and urlParams array.
|
|
|
|
*
|
2013-09-27 23:35:24 +00:00
|
|
|
* If you access e.g. ->post but the current HTTP request method
|
|
|
|
* is GET a \LogicException will be thrown.
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
|
|
|
* @param string $name The key to look for.
|
2013-09-27 23:35:24 +00:00
|
|
|
* @throws \LogicException
|
2013-08-17 09:16:48 +00:00
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
public function __get($name) {
|
|
|
|
switch($name) {
|
2013-09-27 23:35:24 +00:00
|
|
|
case 'put':
|
|
|
|
case 'patch':
|
2013-08-17 09:16:48 +00:00
|
|
|
case 'get':
|
|
|
|
case 'post':
|
2013-09-27 12:36:19 +00:00
|
|
|
if($this->method !== strtoupper($name)) {
|
2013-09-27 23:35:24 +00:00
|
|
|
throw new \LogicException(sprintf('%s cannot be accessed in a %s request.', $name, $this->method));
|
2013-09-27 12:36:19 +00:00
|
|
|
}
|
2013-08-17 09:16:48 +00:00
|
|
|
case 'files':
|
|
|
|
case 'server':
|
|
|
|
case 'env':
|
|
|
|
case 'cookies':
|
|
|
|
case 'parameters':
|
|
|
|
case 'params':
|
|
|
|
case 'urlParams':
|
2013-09-27 23:35:24 +00:00
|
|
|
if(in_array($name, array('put', 'patch'))) {
|
|
|
|
return $this->getContent($name);
|
|
|
|
} else {
|
|
|
|
return isset($this->items[$name])
|
|
|
|
? $this->items[$name]
|
|
|
|
: null;
|
|
|
|
}
|
2013-08-17 09:16:48 +00:00
|
|
|
break;
|
|
|
|
case 'method':
|
|
|
|
return $this->items['method'];
|
|
|
|
break;
|
|
|
|
default;
|
2015-01-15 13:19:56 +00:00
|
|
|
return isset($this[$name])
|
|
|
|
? $this[$name]
|
2013-08-17 09:16:48 +00:00
|
|
|
: null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function __isset($name) {
|
|
|
|
return isset($this->items['parameters'][$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function __unset($id) {
|
|
|
|
throw new \RunTimeException('You cannot change the contents of the request object');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value for a specific http header.
|
|
|
|
*
|
|
|
|
* This method returns null if the header did not exist.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getHeader($name) {
|
|
|
|
|
|
|
|
$name = strtoupper(str_replace(array('-'),array('_'),$name));
|
|
|
|
if (isset($this->server['HTTP_' . $name])) {
|
|
|
|
return $this->server['HTTP_' . $name];
|
|
|
|
}
|
|
|
|
|
|
|
|
// There's a few headers that seem to end up in the top-level
|
|
|
|
// server array.
|
|
|
|
switch($name) {
|
|
|
|
case 'CONTENT_TYPE' :
|
|
|
|
case 'CONTENT_LENGTH' :
|
|
|
|
if (isset($this->server[$name])) {
|
|
|
|
return $this->server[$name];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-08-20 15:53:58 +00:00
|
|
|
/**
|
|
|
|
* Lets you access post and get parameters by the index
|
|
|
|
* In case of json requests the encoded json body is accessed
|
|
|
|
*
|
|
|
|
* @param string $key the key which you want to access in the URL Parameter
|
|
|
|
* placeholder, $_POST or $_GET array.
|
|
|
|
* The priority how they're returned is the following:
|
|
|
|
* 1. URL parameters
|
|
|
|
* 2. POST parameters
|
|
|
|
* 3. GET parameters
|
|
|
|
* @param mixed $default If the key is not found, this value will be returned
|
|
|
|
* @return mixed the content of the array
|
|
|
|
*/
|
2013-09-17 16:38:18 +00:00
|
|
|
public function getParam($key, $default = null) {
|
2013-08-20 15:53:58 +00:00
|
|
|
return isset($this->parameters[$key])
|
|
|
|
? $this->parameters[$key]
|
|
|
|
: $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all params that were received, be it from the request
|
|
|
|
* (as GET or POST) or throuh the URL by the route
|
|
|
|
* @return array the array with all parameters
|
|
|
|
*/
|
2013-09-17 16:38:18 +00:00
|
|
|
public function getParams() {
|
2013-08-20 15:53:58 +00:00
|
|
|
return $this->parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the method of the request
|
|
|
|
* @return string the method of the request (POST, GET, etc)
|
|
|
|
*/
|
2013-09-17 16:38:18 +00:00
|
|
|
public function getMethod() {
|
2013-08-20 15:53:58 +00:00
|
|
|
return $this->method;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for accessing an uploaded file through the $_FILES array
|
|
|
|
* @param string $key the key that will be taken from the $_FILES array
|
|
|
|
* @return array the file in the $_FILES element
|
|
|
|
*/
|
2013-09-17 16:38:18 +00:00
|
|
|
public function getUploadedFile($key) {
|
2013-08-20 15:53:58 +00:00
|
|
|
return isset($this->files[$key]) ? $this->files[$key] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for getting env variables
|
|
|
|
* @param string $key the key that will be taken from the $_ENV array
|
|
|
|
* @return array the value in the $_ENV element
|
|
|
|
*/
|
2013-09-17 16:38:18 +00:00
|
|
|
public function getEnv($key) {
|
2013-08-20 15:53:58 +00:00
|
|
|
return isset($this->env[$key]) ? $this->env[$key] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for getting cookie variables
|
|
|
|
* @param string $key the key that will be taken from the $_COOKIE array
|
|
|
|
* @return array the value in the $_COOKIE element
|
|
|
|
*/
|
2013-09-17 16:38:18 +00:00
|
|
|
function getCookie($key) {
|
2013-08-20 15:53:58 +00:00
|
|
|
return isset($this->cookies[$key]) ? $this->cookies[$key] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the request body content.
|
|
|
|
*
|
2013-10-02 20:13:40 +00:00
|
|
|
* If the HTTP request method is PUT and the body
|
|
|
|
* not application/x-www-form-urlencoded or application/json a stream
|
|
|
|
* resource is returned, otherwise an array.
|
2013-08-20 15:53:58 +00:00
|
|
|
*
|
2013-09-27 23:35:24 +00:00
|
|
|
* @return array|string|resource The request body content or a resource to read the body stream.
|
2013-08-20 15:53:58 +00:00
|
|
|
*
|
|
|
|
* @throws \LogicException
|
|
|
|
*/
|
2013-09-27 23:35:24 +00:00
|
|
|
protected function getContent() {
|
2013-10-02 20:13:40 +00:00
|
|
|
// If the content can't be parsed into an array then return a stream resource.
|
|
|
|
if ($this->method === 'PUT'
|
|
|
|
&& strpos($this->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false
|
|
|
|
&& strpos($this->getHeader('Content-Type'), 'application/json') === false
|
|
|
|
) {
|
2014-04-12 13:02:19 +00:00
|
|
|
if ($this->content === false) {
|
|
|
|
throw new \LogicException(
|
|
|
|
'"put" can only be accessed once if not '
|
|
|
|
. 'application/x-www-form-urlencoded or application/json.'
|
|
|
|
);
|
|
|
|
}
|
2013-09-27 23:35:24 +00:00
|
|
|
$this->content = false;
|
2013-10-03 01:56:37 +00:00
|
|
|
return fopen($this->inputStream, 'rb');
|
2014-04-12 13:02:19 +00:00
|
|
|
} else {
|
|
|
|
return $this->parameters;
|
2013-09-27 23:35:24 +00:00
|
|
|
}
|
2013-08-20 15:53:58 +00:00
|
|
|
}
|
2013-09-27 13:16:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the CSRF check was correct
|
|
|
|
* @return bool true if CSRF check passed
|
|
|
|
* @see OC_Util::callRegister()
|
|
|
|
*/
|
|
|
|
public function passesCSRFCheck() {
|
|
|
|
if($this->items['requesttoken'] === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->items['get']['requesttoken'])) {
|
|
|
|
$token = $this->items['get']['requesttoken'];
|
|
|
|
} elseif (isset($this->items['post']['requesttoken'])) {
|
|
|
|
$token = $this->items['post']['requesttoken'];
|
|
|
|
} elseif (isset($this->items['server']['HTTP_REQUESTTOKEN'])) {
|
|
|
|
$token = $this->items['server']['HTTP_REQUESTTOKEN'];
|
|
|
|
} else {
|
|
|
|
//no token found.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the token is valid
|
|
|
|
if($token !== $this->items['requesttoken']) {
|
|
|
|
// Not valid
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Valid token
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-09 10:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging
|
|
|
|
* If `mod_unique_id` is installed this value will be taken.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getId() {
|
|
|
|
if(isset($this->server['UNIQUE_ID'])) {
|
|
|
|
return $this->server['UNIQUE_ID'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(empty($this->requestId)) {
|
|
|
|
$this->requestId = $this->secureRandom->getLowStrengthGenerator()->generate(20);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->requestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|