2013-08-17 13:16:48 +04:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-02-22 00:35:31 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author b108@volgograd "b108@volgograd"
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author coderkun <olli@coderkun.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Mitar <mitar.git@tnode.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Oliver Wegner <void1976@gmail.com>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Thomas Tanghus <thomas@tanghus.net>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2013-08-17 13:16:48 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
namespace OC\AppFramework\Http;
|
|
|
|
|
2016-01-25 19:15:54 +03:00
|
|
|
use OC\Security\CSRF\CsrfToken;
|
|
|
|
use OC\Security\CSRF\CsrfTokenManager;
|
2015-02-10 15:02:48 +03:00
|
|
|
use OC\Security\TrustedDomainHelper;
|
|
|
|
use OCP\IConfig;
|
2013-08-31 23:34:29 +04:00
|
|
|
use OCP\IRequest;
|
2015-08-13 08:36:42 +03:00
|
|
|
use OCP\Security\ICrypto;
|
2015-02-09 13:41:48 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2013-08-20 19:53:58 +04:00
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
/**
|
|
|
|
* Class for accessing variables in the request.
|
|
|
|
* This class provides an immutable object with request variables.
|
2016-03-11 13:50:19 +03:00
|
|
|
*
|
|
|
|
* @property mixed[] cookies
|
|
|
|
* @property mixed[] env
|
|
|
|
* @property mixed[] files
|
|
|
|
* @property string method
|
|
|
|
* @property mixed[] parameters
|
|
|
|
* @property mixed[] server
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
2013-08-20 19:53:58 +04:00
|
|
|
class Request implements \ArrayAccess, \Countable, IRequest {
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2015-11-19 19:33:17 +03:00
|
|
|
const USER_AGENT_IE = '/(MSIE)|(Trident)/';
|
2016-03-24 16:59:47 +03:00
|
|
|
// Microsoft Edge User Agent from https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
|
|
|
|
const USER_AGENT_MS_EDGE = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+ Edge\/[0-9.]+$/';
|
|
|
|
// Firefox User Agent from https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference
|
|
|
|
const USER_AGENT_FIREFOX = '/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/[0-9.]+$/';
|
|
|
|
// Chrome User Agent from https://developer.chrome.com/multidevice/user-agent
|
2016-10-26 13:07:10 +03:00
|
|
|
const USER_AGENT_CHROME = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\)( Ubuntu Chromium\/[0-9.]+|) Chrome\/[0-9.]+ (Mobile Safari|Safari)\/[0-9.]+$/';
|
2016-03-24 16:59:47 +03:00
|
|
|
// Safari User Agent from http://www.useragentstring.com/pages/Safari/
|
|
|
|
const USER_AGENT_SAFARI = '/^Mozilla\/5\.0 \([^)]+\) AppleWebKit\/[0-9.]+ \(KHTML, like Gecko\) Version\/[0-9.]+ Safari\/[0-9.A-Z]+$/';
|
2015-02-10 15:02:48 +03:00
|
|
|
// Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent
|
|
|
|
const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#';
|
|
|
|
const USER_AGENT_FREEBOX = '#^Mozilla/5\.0$#';
|
2017-05-14 22:22:13 +03:00
|
|
|
const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost|::1)$/';
|
2016-07-18 16:11:44 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_IOS instead
|
|
|
|
*/
|
2017-02-10 12:05:24 +03:00
|
|
|
const USER_AGENT_OWNCLOUD_IOS = '/^Mozilla\/5\.0 \(iOS\) (ownCloud|Nextcloud)\-iOS.*$/';
|
2016-07-18 16:11:44 +03:00
|
|
|
/**
|
|
|
|
* @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_ANDROID instead
|
|
|
|
*/
|
2016-03-23 21:31:17 +03:00
|
|
|
const USER_AGENT_OWNCLOUD_ANDROID = '/^Mozilla\/5\.0 \(Android\) ownCloud\-android.*$/';
|
2016-07-18 16:11:44 +03:00
|
|
|
/**
|
|
|
|
* @deprecated use \OCP\IRequest::USER_AGENT_CLIENT_DESKTOP instead
|
|
|
|
*/
|
2016-03-23 21:31:17 +03:00
|
|
|
const USER_AGENT_OWNCLOUD_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/';
|
2015-02-10 15:02:48 +03:00
|
|
|
|
2013-10-03 05:56:37 +04:00
|
|
|
protected $inputStream;
|
2013-09-28 03:35:24 +04:00
|
|
|
protected $content;
|
2018-02-22 00:35:31 +03:00
|
|
|
protected $items = [];
|
|
|
|
protected $allowedKeys = [
|
2013-09-17 20:38:18 +04:00
|
|
|
'get',
|
|
|
|
'post',
|
|
|
|
'files',
|
|
|
|
'server',
|
|
|
|
'env',
|
|
|
|
'cookies',
|
|
|
|
'urlParams',
|
|
|
|
'parameters',
|
2013-09-27 17:16:34 +04:00
|
|
|
'method',
|
|
|
|
'requesttoken',
|
2018-02-22 00:35:31 +03:00
|
|
|
];
|
2015-02-09 13:41:48 +03:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
protected $secureRandom;
|
2015-02-10 15:02:48 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
2015-02-09 13:41:48 +03:00
|
|
|
/** @var string */
|
|
|
|
protected $requestId = '';
|
2015-08-13 08:36:42 +03:00
|
|
|
/** @var ICrypto */
|
|
|
|
protected $crypto;
|
2016-01-25 19:15:54 +03:00
|
|
|
/** @var CsrfTokenManager|null */
|
|
|
|
protected $csrfTokenManager;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2015-08-29 17:46:10 +03:00
|
|
|
/** @var bool */
|
|
|
|
protected $contentDecoded = false;
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
/**
|
|
|
|
* @param array $vars An associative array with the following optional values:
|
2015-02-09 13:41:48 +03: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
|
2015-02-10 15:02:48 +03:00
|
|
|
* @param IConfig $config
|
2016-01-25 19:15:54 +03:00
|
|
|
* @param CsrfTokenManager|null $csrfTokenManager
|
2015-02-09 13:41:48 +03:00
|
|
|
* @param string $stream
|
2013-08-17 13:16:48 +04:00
|
|
|
* @see http://www.php.net/manual/en/reserved.variables.php
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function __construct(array $vars= [],
|
2015-02-16 18:26:02 +03:00
|
|
|
ISecureRandom $secureRandom = null,
|
2015-02-10 15:02:48 +03:00
|
|
|
IConfig $config,
|
2016-01-25 19:15:54 +03:00
|
|
|
CsrfTokenManager $csrfTokenManager = null,
|
2018-02-22 00:35:31 +03:00
|
|
|
string $stream = 'php://input') {
|
2014-04-12 17:02:19 +04:00
|
|
|
$this->inputStream = $stream;
|
2018-02-22 00:35:31 +03:00
|
|
|
$this->items['params'] = [];
|
2015-02-09 13:41:48 +03:00
|
|
|
$this->secureRandom = $secureRandom;
|
2015-02-10 15:02:48 +03:00
|
|
|
$this->config = $config;
|
2016-01-25 19:15:54 +03:00
|
|
|
$this->csrfTokenManager = $csrfTokenManager;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2014-04-12 18:17:49 +04:00
|
|
|
if(!array_key_exists('method', $vars)) {
|
2015-01-15 16:19:56 +03:00
|
|
|
$vars['method'] = 'GET';
|
2014-04-12 18:17:49 +04:00
|
|
|
}
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
foreach($this->allowedKeys as $name) {
|
2013-09-28 03:35:24 +04:00
|
|
|
$this->items[$name] = isset($vars[$name])
|
2015-01-15 16:19:56 +03:00
|
|
|
? $vars[$name]
|
2018-02-22 00:35:31 +03:00
|
|
|
: [];
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->items['parameters'] = array_merge(
|
|
|
|
$this->items['get'],
|
|
|
|
$this->items['post'],
|
2014-04-12 17:02:19 +04:00
|
|
|
$this->items['urlParams'],
|
|
|
|
$this->items['params']
|
2013-08-17 13:16:48 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2015-02-10 15:02:48 +03:00
|
|
|
/**
|
2015-02-17 00:12:47 +03:00
|
|
|
* @param array $parameters
|
2015-02-10 15:02:48 +03:00
|
|
|
*/
|
2015-02-17 00:12:47 +03:00
|
|
|
public function setUrlParameters(array $parameters) {
|
2015-01-15 16:19:56 +03:00
|
|
|
$this->items['urlParams'] = $parameters;
|
|
|
|
$this->items['parameters'] = array_merge(
|
|
|
|
$this->items['parameters'],
|
|
|
|
$this->items['urlParams']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-10 15:02:48 +03:00
|
|
|
/**
|
|
|
|
* Countable method
|
|
|
|
* @return int
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function count(): int {
|
2018-02-22 17:51:09 +03:00
|
|
|
return \count($this->items['parameters']);
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 19:30:58 +04:00
|
|
|
* @return boolean
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function offsetExists($offset): bool {
|
2013-08-17 13:16:48 +04:00
|
|
|
return isset($this->items['parameters'][$offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-22 00:35:31 +03:00
|
|
|
* @see offsetExists
|
|
|
|
* @param string $offset
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-08-17 13:16:48 +04:00
|
|
|
public function offsetGet($offset) {
|
|
|
|
return isset($this->items['parameters'][$offset])
|
|
|
|
? $this->items['parameters'][$offset]
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-22 00:35:31 +03:00
|
|
|
* @see offsetExists
|
|
|
|
* @param string $offset
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2013-08-17 13:16:48 +04:00
|
|
|
public function offsetSet($offset, $value) {
|
|
|
|
throw new \RuntimeException('You cannot change the contents of the request object');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-22 00:35:31 +03:00
|
|
|
* @see offsetExists
|
|
|
|
* @param string $offset
|
|
|
|
*/
|
2013-08-17 13:16:48 +04:00
|
|
|
public function offsetUnset($offset) {
|
|
|
|
throw new \RuntimeException('You cannot change the contents of the request object');
|
|
|
|
}
|
|
|
|
|
2015-02-10 15:02:48 +03:00
|
|
|
/**
|
|
|
|
* Magic property accessors
|
2015-02-17 00:12:47 +03:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
2015-02-10 15:02:48 +03:00
|
|
|
*/
|
2013-08-17 13:16:48 +04:00
|
|
|
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-28 03:35:24 +04:00
|
|
|
* If you access e.g. ->post but the current HTTP request method
|
|
|
|
* is GET a \LogicException will be thrown.
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
|
|
|
* @param string $name The key to look for.
|
2013-09-28 03:35:24 +04:00
|
|
|
* @throws \LogicException
|
2013-08-17 13:16:48 +04:00
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
public function __get($name) {
|
|
|
|
switch($name) {
|
2013-09-28 03:35:24 +04:00
|
|
|
case 'put':
|
|
|
|
case 'patch':
|
2013-08-17 13:16:48 +04:00
|
|
|
case 'get':
|
|
|
|
case 'post':
|
2013-09-27 16:36:19 +04:00
|
|
|
if($this->method !== strtoupper($name)) {
|
2013-09-28 03:35:24 +04:00
|
|
|
throw new \LogicException(sprintf('%s cannot be accessed in a %s request.', $name, $this->method));
|
2013-09-27 16:36:19 +04:00
|
|
|
}
|
2015-08-29 17:46:10 +03:00
|
|
|
return $this->getContent();
|
2013-08-17 13:16:48 +04:00
|
|
|
case 'files':
|
|
|
|
case 'server':
|
|
|
|
case 'env':
|
|
|
|
case 'cookies':
|
|
|
|
case 'urlParams':
|
|
|
|
case 'method':
|
2015-08-29 17:46:10 +03:00
|
|
|
return isset($this->items[$name])
|
|
|
|
? $this->items[$name]
|
|
|
|
: null;
|
|
|
|
case 'parameters':
|
|
|
|
case 'params':
|
|
|
|
return $this->getContent();
|
2013-08-17 13:16:48 +04:00
|
|
|
default;
|
2015-01-15 16:19:56 +03:00
|
|
|
return isset($this[$name])
|
|
|
|
? $this[$name]
|
2013-08-17 13:16:48 +04:00
|
|
|
: null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 15:02:48 +03:00
|
|
|
/**
|
2015-02-17 00:12:47 +03:00
|
|
|
* @param string $name
|
2015-02-10 15:02:48 +03:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-08-17 13:16:48 +04:00
|
|
|
public function __isset($name) {
|
2018-02-22 00:35:31 +03:00
|
|
|
if (\in_array($name, $this->allowedKeys, true)) {
|
2016-04-28 13:23:17 +03:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-17 13:16:48 +04:00
|
|
|
return isset($this->items['parameters'][$name]);
|
|
|
|
}
|
|
|
|
|
2015-02-10 15:02:48 +03:00
|
|
|
/**
|
2015-02-17 00:12:47 +03:00
|
|
|
* @param string $id
|
2015-02-10 15:02:48 +03:00
|
|
|
*/
|
2013-08-17 13:16:48 +04:00
|
|
|
public function __unset($id) {
|
2016-03-11 13:50:19 +03:00
|
|
|
throw new \RuntimeException('You cannot change the contents of the request object');
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value for a specific http header.
|
|
|
|
*
|
|
|
|
* This method returns null if the header did not exist.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getHeader(string $name): string {
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2018-02-22 17:51:09 +03:00
|
|
|
$name = strtoupper(str_replace('-', '_',$name));
|
2013-08-17 13:16:48 +04:00
|
|
|
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.
|
2018-10-25 23:26:49 +03:00
|
|
|
switch ($name) {
|
2013-08-17 13:16:48 +04:00
|
|
|
case 'CONTENT_TYPE' :
|
|
|
|
case 'CONTENT_LENGTH' :
|
2019-10-08 01:46:50 +03:00
|
|
|
case 'REMOTE_ADDR':
|
2018-10-25 23:26:49 +03:00
|
|
|
if (isset($this->server[$name])) {
|
|
|
|
return $this->server[$name];
|
|
|
|
}
|
|
|
|
break;
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:15:12 +03:00
|
|
|
return '';
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
2013-08-20 19:53:58 +04: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
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getParam(string $key, $default = null) {
|
2013-08-20 19:53:58 +04: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
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getParams(): array {
|
2018-08-28 19:11:42 +03:00
|
|
|
return is_array($this->parameters) ? $this->parameters : [];
|
2013-08-20 19:53:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the method of the request
|
|
|
|
* @return string the method of the request (POST, GET, etc)
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getMethod(): string {
|
2013-08-20 19:53:58 +04: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
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getUploadedFile(string $key) {
|
2013-08-20 19:53:58 +04: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
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getEnv(string $key) {
|
2013-08-20 19:53:58 +04: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
|
2016-04-18 13:14:07 +03:00
|
|
|
* @return string the value in the $_COOKIE element
|
2013-08-20 19:53:58 +04:00
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getCookie(string $key) {
|
2013-08-20 19:53:58 +04:00
|
|
|
return isset($this->cookies[$key]) ? $this->cookies[$key] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the request body content.
|
|
|
|
*
|
2013-10-03 00:13:40 +04: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 19:53:58 +04:00
|
|
|
*
|
2013-09-28 03:35:24 +04:00
|
|
|
* @return array|string|resource The request body content or a resource to read the body stream.
|
2013-08-20 19:53:58 +04:00
|
|
|
*
|
|
|
|
* @throws \LogicException
|
|
|
|
*/
|
2013-09-28 03:35:24 +04:00
|
|
|
protected function getContent() {
|
2013-10-03 00:13:40 +04:00
|
|
|
// If the content can't be parsed into an array then return a stream resource.
|
|
|
|
if ($this->method === 'PUT'
|
2018-01-12 16:15:12 +03:00
|
|
|
&& $this->getHeader('Content-Length') !== '0'
|
2017-12-14 23:48:59 +03:00
|
|
|
&& $this->getHeader('Content-Length') !== ''
|
2013-10-03 00:13:40 +04:00
|
|
|
&& strpos($this->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false
|
|
|
|
&& strpos($this->getHeader('Content-Type'), 'application/json') === false
|
|
|
|
) {
|
2014-04-12 17:02:19 +04: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-28 03:35:24 +04:00
|
|
|
$this->content = false;
|
2013-10-03 05:56:37 +04:00
|
|
|
return fopen($this->inputStream, 'rb');
|
2014-04-12 17:02:19 +04:00
|
|
|
} else {
|
2015-08-29 17:46:10 +03:00
|
|
|
$this->decodeContent();
|
|
|
|
return $this->items['parameters'];
|
2013-09-28 03:35:24 +04:00
|
|
|
}
|
2013-08-20 19:53:58 +04:00
|
|
|
}
|
2013-09-27 17:16:34 +04:00
|
|
|
|
2015-08-29 17:46:10 +03:00
|
|
|
/**
|
|
|
|
* Attempt to decode the content and populate parameters
|
|
|
|
*/
|
|
|
|
protected function decodeContent() {
|
|
|
|
if ($this->contentDecoded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$params = [];
|
|
|
|
|
|
|
|
// 'application/json' must be decoded manually.
|
|
|
|
if (strpos($this->getHeader('Content-Type'), 'application/json') !== false) {
|
|
|
|
$params = json_decode(file_get_contents($this->inputStream), true);
|
2018-02-22 00:35:31 +03:00
|
|
|
if($params !== null && \count($params) > 0) {
|
2015-08-29 17:46:10 +03:00
|
|
|
$this->items['params'] = $params;
|
|
|
|
if($this->method === 'POST') {
|
|
|
|
$this->items['post'] = $params;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle application/x-www-form-urlencoded for methods other than GET
|
|
|
|
// or post correctly
|
|
|
|
} elseif($this->method !== 'GET'
|
|
|
|
&& $this->method !== 'POST'
|
|
|
|
&& strpos($this->getHeader('Content-Type'), 'application/x-www-form-urlencoded') !== false) {
|
|
|
|
|
|
|
|
parse_str(file_get_contents($this->inputStream), $params);
|
2018-02-22 00:35:31 +03:00
|
|
|
if(\is_array($params)) {
|
2015-08-29 17:46:10 +03:00
|
|
|
$this->items['params'] = $params;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 00:35:31 +03:00
|
|
|
if (\is_array($params)) {
|
2015-09-15 00:36:40 +03:00
|
|
|
$this->items['parameters'] = array_merge($this->items['parameters'], $params);
|
|
|
|
}
|
2015-08-29 17:46:10 +03:00
|
|
|
$this->contentDecoded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-27 17:16:34 +04:00
|
|
|
/**
|
|
|
|
* Checks if the CSRF check was correct
|
|
|
|
* @return bool true if CSRF check passed
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function passesCSRFCheck(): bool {
|
2016-01-25 19:15:54 +03:00
|
|
|
if($this->csrfTokenManager === null) {
|
2013-09-27 17:16:34 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-20 18:37:30 +03:00
|
|
|
if(!$this->passesStrictCookieCheck()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-27 17:16:34 +04:00
|
|
|
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;
|
|
|
|
}
|
2016-01-25 19:15:54 +03:00
|
|
|
$token = new CsrfToken($token);
|
2013-09-27 17:16:34 +04:00
|
|
|
|
2016-01-25 19:15:54 +03:00
|
|
|
return $this->csrfTokenManager->isTokenValid($token);
|
2015-02-09 13:41:48 +03:00
|
|
|
}
|
|
|
|
|
2016-08-09 20:09:53 +03:00
|
|
|
/**
|
|
|
|
* Whether the cookie checks are required
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
private function cookieCheckRequired(): bool {
|
2017-03-09 18:48:48 +03:00
|
|
|
if ($this->getHeader('OCS-APIREQUEST')) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-02 23:56:44 +03:00
|
|
|
if($this->getCookie(session_name()) === null && $this->getCookie('nc_token') === null) {
|
2016-08-09 20:09:53 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:53:44 +03:00
|
|
|
/**
|
|
|
|
* Wrapper around session_get_cookie_params
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getCookieParams(): array {
|
2016-11-23 14:53:44 +03:00
|
|
|
return session_get_cookie_params();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends the __Host- prefix to the cookie if applicable
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
protected function getProtectedCookieName(string $name): string {
|
2016-11-23 14:53:44 +03:00
|
|
|
$cookieParams = $this->getCookieParams();
|
|
|
|
$prefix = '';
|
|
|
|
if($cookieParams['secure'] === true && $cookieParams['path'] === '/') {
|
|
|
|
$prefix = '__Host-';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $prefix.$name;
|
|
|
|
}
|
|
|
|
|
2016-07-20 18:37:30 +03:00
|
|
|
/**
|
|
|
|
* Checks if the strict cookie has been sent with the request if the request
|
|
|
|
* is including any cookies.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @since 9.1.0
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function passesStrictCookieCheck(): bool {
|
2016-08-09 20:09:53 +03:00
|
|
|
if(!$this->cookieCheckRequired()) {
|
2016-07-20 18:37:30 +03:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-23 14:53:44 +03:00
|
|
|
|
|
|
|
$cookieName = $this->getProtectedCookieName('nc_sameSiteCookiestrict');
|
|
|
|
if($this->getCookie($cookieName) === 'true'
|
2016-07-20 18:37:30 +03:00
|
|
|
&& $this->passesLaxCookieCheck()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the lax cookie has been sent with the request if the request
|
|
|
|
* is including any cookies.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @since 9.1.0
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function passesLaxCookieCheck(): bool {
|
2016-08-09 20:09:53 +03:00
|
|
|
if(!$this->cookieCheckRequired()) {
|
2016-07-20 18:37:30 +03:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-23 14:53:44 +03:00
|
|
|
|
|
|
|
$cookieName = $this->getProtectedCookieName('nc_sameSiteCookielax');
|
|
|
|
if($this->getCookie($cookieName) === 'true') {
|
2016-07-20 18:37:30 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-09 13:41:48 +03: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
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getId(): string {
|
2015-02-09 13:41:48 +03:00
|
|
|
if(isset($this->server['UNIQUE_ID'])) {
|
|
|
|
return $this->server['UNIQUE_ID'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(empty($this->requestId)) {
|
2017-04-12 14:04:28 +03:00
|
|
|
$validChars = ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS;
|
2017-04-04 11:12:41 +03:00
|
|
|
$this->requestId = $this->secureRandom->generate(20, $validChars);
|
2015-02-09 13:41:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->requestId;
|
|
|
|
}
|
|
|
|
|
2018-10-25 12:36:52 +03:00
|
|
|
/**
|
|
|
|
* Checks if given $remoteAddress matches given $trustedProxy.
|
|
|
|
* If $trustedProxy is an IPv4 IP range given in CIDR notation, true will be returned if
|
|
|
|
* $remoteAddress is an IPv4 address within that IP range.
|
|
|
|
* Otherwise $remoteAddress will be compared to $trustedProxy literally and the result
|
|
|
|
* will be returned.
|
|
|
|
* @return boolean true if $remoteAddress matches $trustedProxy, false otherwise
|
|
|
|
*/
|
|
|
|
protected function matchesTrustedProxy($trustedProxy, $remoteAddress) {
|
|
|
|
$cidrre = '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\/([0-9]{1,2})$/';
|
|
|
|
|
|
|
|
if (preg_match($cidrre, $trustedProxy, $match)) {
|
|
|
|
$net = $match[1];
|
|
|
|
$shiftbits = min(32, max(0, 32 - intval($match[2])));
|
|
|
|
$netnum = ip2long($net) >> $shiftbits;
|
|
|
|
$ipnum = ip2long($remoteAddress) >> $shiftbits;
|
|
|
|
|
|
|
|
return $ipnum === $netnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $trustedProxy === $remoteAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if given $remoteAddress matches any entry in the given array $trustedProxies.
|
|
|
|
* For details regarding what "match" means, refer to `matchesTrustedProxy`.
|
|
|
|
* @return boolean true if $remoteAddress matches any entry in $trustedProxies, false otherwise
|
|
|
|
*/
|
|
|
|
protected function isTrustedProxy($trustedProxies, $remoteAddress) {
|
|
|
|
foreach ($trustedProxies as $tp) {
|
|
|
|
if ($this->matchesTrustedProxy($tp, $remoteAddress)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-10 15:02:48 +03:00
|
|
|
/**
|
|
|
|
* Returns the remote address, if the connection came from a trusted proxy
|
|
|
|
* and `forwarded_for_headers` has been configured then the IP address
|
|
|
|
* specified in this header will be returned instead.
|
|
|
|
* Do always use this instead of $_SERVER['REMOTE_ADDR']
|
|
|
|
* @return string IP address
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getRemoteAddress(): string {
|
2015-02-10 15:02:48 +03:00
|
|
|
$remoteAddress = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : '';
|
|
|
|
$trustedProxies = $this->config->getSystemValue('trusted_proxies', []);
|
|
|
|
|
2018-10-25 12:36:52 +03:00
|
|
|
if(\is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress)) {
|
2015-07-25 20:10:21 +03:00
|
|
|
$forwardedForHeaders = $this->config->getSystemValue('forwarded_for_headers', [
|
|
|
|
'HTTP_X_FORWARDED_FOR'
|
|
|
|
// only have one default, so we cannot ship an insecure product out of the box
|
|
|
|
]);
|
2015-02-10 15:02:48 +03:00
|
|
|
|
|
|
|
foreach($forwardedForHeaders as $header) {
|
|
|
|
if(isset($this->server[$header])) {
|
|
|
|
foreach(explode(',', $this->server[$header]) as $IP) {
|
|
|
|
$IP = trim($IP);
|
|
|
|
if (filter_var($IP, FILTER_VALIDATE_IP) !== false) {
|
|
|
|
return $IP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $remoteAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check overwrite condition
|
|
|
|
* @param string $type
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
private function isOverwriteCondition(string $type = ''): bool {
|
2015-02-10 15:02:48 +03:00
|
|
|
$regex = '/' . $this->config->getSystemValue('overwritecondaddr', '') . '/';
|
2015-07-16 17:40:57 +03:00
|
|
|
$remoteAddr = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : '';
|
|
|
|
return $regex === '//' || preg_match($regex, $remoteAddr) === 1
|
2015-01-19 13:56:04 +03:00
|
|
|
|| $type !== 'protocol';
|
2015-02-10 15:02:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-24 14:24:55 +03:00
|
|
|
* Returns the server protocol. It respects one or more reverse proxies servers
|
|
|
|
* and load balancers
|
2015-02-10 15:02:48 +03:00
|
|
|
* @return string Server protocol (http or https)
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getServerProtocol(): string {
|
2015-02-10 15:02:48 +03:00
|
|
|
if($this->config->getSystemValue('overwriteprotocol') !== ''
|
|
|
|
&& $this->isOverwriteCondition('protocol')) {
|
|
|
|
return $this->config->getSystemValue('overwriteprotocol');
|
|
|
|
}
|
|
|
|
|
2018-12-17 14:44:23 +03:00
|
|
|
if ($this->fromTrustedProxy() && isset($this->server['HTTP_X_FORWARDED_PROTO'])) {
|
2015-02-24 14:24:55 +03:00
|
|
|
if (strpos($this->server['HTTP_X_FORWARDED_PROTO'], ',') !== false) {
|
|
|
|
$parts = explode(',', $this->server['HTTP_X_FORWARDED_PROTO']);
|
2015-02-24 15:37:34 +03:00
|
|
|
$proto = strtolower(trim($parts[0]));
|
2015-02-24 14:24:55 +03:00
|
|
|
} else {
|
|
|
|
$proto = strtolower($this->server['HTTP_X_FORWARDED_PROTO']);
|
|
|
|
}
|
|
|
|
|
2015-02-10 15:02:48 +03:00
|
|
|
// Verify that the protocol is always HTTP or HTTPS
|
|
|
|
// default to http if an invalid value is provided
|
|
|
|
return $proto === 'https' ? 'https' : 'http';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->server['HTTPS'])
|
|
|
|
&& $this->server['HTTPS'] !== null
|
2015-11-27 12:01:56 +03:00
|
|
|
&& $this->server['HTTPS'] !== 'off'
|
|
|
|
&& $this->server['HTTPS'] !== '') {
|
2015-02-10 15:02:48 +03:00
|
|
|
return 'https';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'http';
|
|
|
|
}
|
|
|
|
|
2015-10-06 15:18:46 +03:00
|
|
|
/**
|
|
|
|
* Returns the used HTTP protocol.
|
|
|
|
*
|
|
|
|
* @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0.
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getHttpProtocol(): string {
|
2018-02-22 12:22:11 +03:00
|
|
|
$claimedProtocol = $this->server['SERVER_PROTOCOL'];
|
|
|
|
|
|
|
|
if (\is_string($claimedProtocol)) {
|
|
|
|
$claimedProtocol = strtoupper($claimedProtocol);
|
|
|
|
}
|
2015-10-06 15:18:46 +03:00
|
|
|
|
|
|
|
$validProtocols = [
|
|
|
|
'HTTP/1.0',
|
|
|
|
'HTTP/1.1',
|
|
|
|
'HTTP/2',
|
|
|
|
];
|
|
|
|
|
2018-02-22 00:35:31 +03:00
|
|
|
if(\in_array($claimedProtocol, $validProtocols, true)) {
|
2015-10-06 15:18:46 +03:00
|
|
|
return $claimedProtocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'HTTP/1.1';
|
|
|
|
}
|
|
|
|
|
2015-02-10 15:02:48 +03:00
|
|
|
/**
|
|
|
|
* Returns the request uri, even if the website uses one or more
|
|
|
|
* reverse proxies
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getRequestUri(): string {
|
2015-02-10 15:02:48 +03:00
|
|
|
$uri = isset($this->server['REQUEST_URI']) ? $this->server['REQUEST_URI'] : '';
|
|
|
|
if($this->config->getSystemValue('overwritewebroot') !== '' && $this->isOverwriteCondition()) {
|
2018-02-22 00:35:31 +03:00
|
|
|
$uri = $this->getScriptName() . substr($uri, \strlen($this->server['SCRIPT_NAME']));
|
2015-02-10 15:02:48 +03:00
|
|
|
}
|
|
|
|
return $uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get raw PathInfo from request (not urldecoded)
|
|
|
|
* @throws \Exception
|
2015-02-16 16:01:15 +03:00
|
|
|
* @return string Path info
|
2015-02-10 15:02:48 +03:00
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getRawPathInfo(): string {
|
2015-02-10 15:02:48 +03:00
|
|
|
$requestUri = isset($this->server['REQUEST_URI']) ? $this->server['REQUEST_URI'] : '';
|
2019-01-20 12:29:58 +03:00
|
|
|
// remove too many slashes - can be caused by reverse proxy configuration
|
2015-02-10 15:02:48 +03:00
|
|
|
$requestUri = preg_replace('%/{2,}%', '/', $requestUri);
|
|
|
|
|
|
|
|
// Remove the query string from REQUEST_URI
|
|
|
|
if ($pos = strpos($requestUri, '?')) {
|
|
|
|
$requestUri = substr($requestUri, 0, $pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
$scriptName = $this->server['SCRIPT_NAME'];
|
|
|
|
$pathInfo = $requestUri;
|
|
|
|
|
|
|
|
// strip off the script name's dir and file name
|
|
|
|
// FIXME: Sabre does not really belong here
|
2017-07-24 12:36:20 +03:00
|
|
|
list($path, $name) = \Sabre\Uri\split($scriptName);
|
2015-02-10 15:02:48 +03:00
|
|
|
if (!empty($path)) {
|
|
|
|
if($path === $pathInfo || strpos($pathInfo, $path.'/') === 0) {
|
2018-02-22 00:35:31 +03:00
|
|
|
$pathInfo = substr($pathInfo, \strlen($path));
|
2015-02-10 15:02:48 +03:00
|
|
|
} else {
|
|
|
|
throw new \Exception("The requested uri($requestUri) cannot be processed by the script '$scriptName')");
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 12:22:11 +03:00
|
|
|
if ($name === null) {
|
|
|
|
$name = '';
|
|
|
|
}
|
|
|
|
|
2015-02-10 15:02:48 +03:00
|
|
|
if (strpos($pathInfo, '/'.$name) === 0) {
|
2018-02-22 00:35:31 +03:00
|
|
|
$pathInfo = substr($pathInfo, \strlen($name) + 1);
|
2015-02-10 15:02:48 +03:00
|
|
|
}
|
2018-02-22 12:22:11 +03:00
|
|
|
if ($name !== '' && strpos($pathInfo, $name) === 0) {
|
2018-02-22 00:35:31 +03:00
|
|
|
$pathInfo = substr($pathInfo, \strlen($name));
|
2015-02-10 15:02:48 +03:00
|
|
|
}
|
2015-09-23 13:32:49 +03:00
|
|
|
if($pathInfo === false || $pathInfo === '/'){
|
2015-02-10 15:02:48 +03:00
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return $pathInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get PathInfo from request
|
|
|
|
* @throws \Exception
|
|
|
|
* @return string|false Path info or false when not found
|
|
|
|
*/
|
|
|
|
public function getPathInfo() {
|
|
|
|
$pathInfo = $this->getRawPathInfo();
|
2015-02-12 14:29:01 +03:00
|
|
|
// following is taken from \Sabre\HTTP\URLUtil::decodePathSegment
|
2015-02-10 15:02:48 +03:00
|
|
|
$pathInfo = rawurldecode($pathInfo);
|
|
|
|
$encoding = mb_detect_encoding($pathInfo, ['UTF-8', 'ISO-8859-1']);
|
|
|
|
|
|
|
|
switch($encoding) {
|
|
|
|
case 'ISO-8859-1' :
|
|
|
|
$pathInfo = utf8_encode($pathInfo);
|
|
|
|
}
|
|
|
|
// end copy
|
|
|
|
|
|
|
|
return $pathInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the script name, even if the website uses one or more
|
|
|
|
* reverse proxies
|
|
|
|
* @return string the script name
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getScriptName(): string {
|
2015-02-10 15:02:48 +03:00
|
|
|
$name = $this->server['SCRIPT_NAME'];
|
|
|
|
$overwriteWebRoot = $this->config->getSystemValue('overwritewebroot');
|
|
|
|
if ($overwriteWebRoot !== '' && $this->isOverwriteCondition()) {
|
2015-02-16 18:26:02 +03:00
|
|
|
// FIXME: This code is untestable due to __DIR__, also that hardcoded path is really dangerous
|
2018-02-22 00:35:31 +03:00
|
|
|
$serverRoot = str_replace('\\', '/', substr(__DIR__, 0, -\strlen('lib/private/appframework/http/')));
|
|
|
|
$suburi = str_replace('\\', '/', substr(realpath($this->server['SCRIPT_FILENAME']), \strlen($serverRoot)));
|
2015-02-10 15:02:48 +03:00
|
|
|
$name = '/' . ltrim($overwriteWebRoot . $suburi, '/');
|
|
|
|
}
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the user agent matches a given regex
|
|
|
|
* @param array $agent array of agent names
|
|
|
|
* @return bool true if at least one of the given agent matches, false otherwise
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function isUserAgent(array $agent): bool {
|
2015-11-18 17:27:17 +03:00
|
|
|
if (!isset($this->server['HTTP_USER_AGENT'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-10 15:02:48 +03:00
|
|
|
foreach ($agent as $regex) {
|
|
|
|
if (preg_match($regex, $this->server['HTTP_USER_AGENT'])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the unverified server host from the headers without checking
|
|
|
|
* whether it is a trusted domain
|
|
|
|
* @return string Server host
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getInsecureServerHost(): string {
|
2019-11-28 16:51:54 +03:00
|
|
|
if ($this->fromTrustedProxy() && $this->getOverwriteHost() !== null) {
|
|
|
|
return $this->getOverwriteHost();
|
|
|
|
}
|
|
|
|
|
2015-02-23 23:49:35 +03:00
|
|
|
$host = 'localhost';
|
2018-12-17 14:44:23 +03:00
|
|
|
if ($this->fromTrustedProxy() && isset($this->server['HTTP_X_FORWARDED_HOST'])) {
|
2015-02-10 15:02:48 +03:00
|
|
|
if (strpos($this->server['HTTP_X_FORWARDED_HOST'], ',') !== false) {
|
|
|
|
$parts = explode(',', $this->server['HTTP_X_FORWARDED_HOST']);
|
|
|
|
$host = trim(current($parts));
|
|
|
|
} else {
|
|
|
|
$host = $this->server['HTTP_X_FORWARDED_HOST'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isset($this->server['HTTP_HOST'])) {
|
|
|
|
$host = $this->server['HTTP_HOST'];
|
|
|
|
} else if (isset($this->server['SERVER_NAME'])) {
|
|
|
|
$host = $this->server['SERVER_NAME'];
|
|
|
|
}
|
|
|
|
}
|
2019-11-28 16:51:54 +03:00
|
|
|
|
2015-02-10 15:02:48 +03:00
|
|
|
return $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the server host from the headers, or the first configured
|
|
|
|
* trusted domain if the host isn't in the trusted list
|
|
|
|
* @return string Server host
|
|
|
|
*/
|
2018-02-22 00:35:31 +03:00
|
|
|
public function getServerHost(): string {
|
2015-02-10 15:02:48 +03:00
|
|
|
// overwritehost is always trusted
|
|
|
|
$host = $this->getOverwriteHost();
|
|
|
|
if ($host !== null) {
|
|
|
|
return $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the host from the headers
|
|
|
|
$host = $this->getInsecureServerHost();
|
|
|
|
|
|
|
|
// Verify that the host is a trusted domain if the trusted domains
|
|
|
|
// are defined
|
|
|
|
// If no trusted domain is provided the first trusted domain is returned
|
|
|
|
$trustedDomainHelper = new TrustedDomainHelper($this->config);
|
|
|
|
if ($trustedDomainHelper->isTrustedDomain($host)) {
|
|
|
|
return $host;
|
|
|
|
} else {
|
|
|
|
$trustedList = $this->config->getSystemValue('trusted_domains', []);
|
2015-06-22 13:28:07 +03:00
|
|
|
if(!empty($trustedList)) {
|
|
|
|
return $trustedList[0];
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2015-02-10 15:02:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the overwritehost setting from the config if set and
|
|
|
|
* if the overwrite condition is met
|
|
|
|
* @return string|null overwritehost value or null if not defined or the defined condition
|
|
|
|
* isn't met
|
|
|
|
*/
|
|
|
|
private function getOverwriteHost() {
|
|
|
|
if($this->config->getSystemValue('overwritehost') !== '' && $this->isOverwriteCondition()) {
|
|
|
|
return $this->config->getSystemValue('overwritehost');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-17 14:44:23 +03:00
|
|
|
private function fromTrustedProxy(): bool {
|
|
|
|
$remoteAddress = isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : '';
|
|
|
|
$trustedProxies = $this->config->getSystemValue('trusted_proxies', []);
|
|
|
|
|
|
|
|
return \is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress);
|
|
|
|
}
|
2015-02-09 13:41:48 +03:00
|
|
|
}
|