Critics have remarked that those four worthies were truly peerless throughout the ages; yet the present falls short of the past—the ancients favored unadorned simplicity, whereas the moderns prefer refined elegance. Styles of substance and ornament rise and fall in succession, shifting with the changing mores of the times; such evolution is simply the natural order of things. The ideal lies in honoring antiquity without clashing with the present, and embracing modernity without succumbing to its flaws—embodying that perfect balance of substance and refinement that defines the true gentleman. There is surely no need to abandon a carved palace in favor of a cave dwelling, or to trade a jade carriage for a primitive cart with solid wooden wheels.
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
use Symfony\Component\HttpFoundation\Exception\JsonException;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
// Help opcache.preload discover always-needed symbols
class_exists(AcceptHeader::class);
class_exists(FileBag::class);
class_exists(HeaderBag::class);
class_exists(HeaderUtils::class);
class_exists(InputBag::class);
class_exists(ParameterBag::class);
class_exists(ServerBag::class);
/**
* Request represents an HTTP request.
*
* The methods dealing with URL accept / return a raw path (% encoded):
* * getBasePath
* * getBaseUrl
* * getPathInfo
* * getRequestUri
* * getUri
* * getUriForPath
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Request
{
public const HEADER_FORWARDED = 0b000001; // When using RFC 7239
public const HEADER_X_FORWARDED_FOR = 0b000010;
public const HEADER_X_FORWARDED_HOST = 0b000100;
public const HEADER_X_FORWARDED_PROTO = 0b001000;
public const HEADER_X_FORWARDED_PORT = 0b010000;
public const HEADER_X_FORWARDED_PREFIX = 0b100000;
public const HEADER_X_FORWARDED_AWS_ELB = 0b0011010; // AWS ELB doesn't send X-Forwarded-Host
public const HEADER_X_FORWARDED_TRAEFIK = 0b0111110; // All "X-Forwarded-*" headers sent by Traefik reverse proxy
public const METHOD_HEAD = 'HEAD';
public const METHOD_GET = 'GET';
public const METHOD_POST = 'POST';
public const METHOD_PUT = 'PUT';
public const METHOD_PATCH = 'PATCH';
public const METHOD_DELETE = 'DELETE';
public const METHOD_PURGE = 'PURGE';
public const METHOD_OPTIONS = 'OPTIONS';
public const METHOD_TRACE = 'TRACE';
public const METHOD_CONNECT = 'CONNECT';
/**
* @var string[]
*/
protected static $trustedProxies = [];
/**
* @var string[]
*/
protected static $trustedHostPatterns = [];
/**
* @var string[]
*/
protected static $trustedHosts = [];
protected static $httpMethodParameterOverride = false;
/**
* Custom parameters.
*
* @var ParameterBag
*/
public $attributes;
/**
* Request body parameters ($_POST).
*
* @see getPayload() for portability between content types
*
* @var InputBag
*/
public $request;
/**
* Query string parameters ($_GET).
*
* @var InputBag
*/
public $query;
/**
* Server and execution environment parameters ($_SERVER).
*
* @var ServerBag
*/
public $server;
/**
* Uploaded files ($_FILES).
*
* @var FileBag
*/
public $files;
/**
* Cookies ($_COOKIE).
*
* @var InputBag
*/
public $cookies;
/**
* Headers (taken from the $_SERVER).
*
* @var HeaderBag
*/
public $headers;
/**
* @var string|resource|false|null
*/
protected $content;
/**
* @var string[]
*/
protected $languages;
/**
* @var string[]
*/
protected $charsets;
/**
* @var string[]
*/
protected $encodings;
/**
* @var string[]
*/
protected $acceptableContentTypes;
/**
* @var string
*/
protected $pathInfo;
/**
* @var string
*/
protected $requestUri;
/**
* @var string
*/
protected $baseUrl;
/**
* @var string
*/
protected $basePath;
/**
* @var string
*/
protected $method;
/**
* @var string
*/
protected $format;
/**
* @var SessionInterface|callable(): SessionInterface
*/
protected $session;
/**
* @var string|null
*/
protected $locale;
/**
* @var string
*/
protected $defaultLocale = 'en';
/**
* @var array<string, string[]>
*/
protected static $formats;
protected static $requestFactory;
private ?string $preferredFormat = null;
private bool $isHostValid = true;
private bool $isForwardedValid = true;
private bool $isSafeContentPreferred;
private static int $trustedHeaderSet = -1;
private const FORWARDED_PARAMS = [
self::HEADER_X_FORWARDED_FOR => 'for',
self::HEADER_X_FORWARDED_HOST => 'host',
self::HEADER_X_FORWARDED_PROTO => 'proto',
self::HEADER_X_FORWARDED_PORT => 'host',
];
/**
* Names for headers that can be trusted when
* using trusted proxies.
*
* The FORWARDED header is the standard as of rfc7239.
*
* The other headers are non-standard, but widely used
* by popular reverse proxies (like Apache mod_proxy or Amazon EC2).
*/
private const TRUSTED_HEADERS = [
self::HEADER_FORWARDED => 'FORWARDED',
self::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
self::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
self::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
self::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
self::HEADER_X_FORWARDED_PREFIX => 'X_FORWARDED_PREFIX',
];
/**
* @param array $query The GET parameters
* @param array $request The POST parameters
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
* @param array $cookies The COOKIE parameters
* @param array $files The FILES parameters
* @param array $server The SERVER parameters
* @param string|resource|null $content The raw body data
*/
public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
{
$this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
}
/**
* Sets the parameters for this request.
*
* This method also re-initializes all properties.
*
* @param array $query The GET parameters
* @param array $request The POST parameters
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
* @param array $cookies The COOKIE parameters
* @param array $files The FILES parameters
* @param array $server The SERVER parameters
* @param string|resource|null $content The raw body data
*
* @return void
*/
public function initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
{
$this->request = new InputBag($request);
$this->query = new InputBag($query);
$this->attributes = new ParameterBag($attributes);
$this->cookies = new InputBag($cookies);
$this->files = new FileBag($files);
$this->server = new ServerBag($server);
$this->headers = new HeaderBag($this->server->getHeaders());
$this->content = $content;
$this->languages = null;
$this->charsets = null;
$this->encodings = null;
$this->acceptableContentTypes = null;
$this->pathInfo = null;
$this->requestUri = null;
$this->baseUrl = null;
$this->basePath = null;
$this->method = null;
$this->format = null;
}
/**
* Creates a new request with values from PHP's super globals.
*/
public static function createFromGlobals(): static
{
$request = self::createRequestFromFactory($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER);
if (str_starts_with($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded')
&& \in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])
) {
parse_str($request->getContent(), $data);
$request->request = new InputBag($data);
}
return $request;
}
/**
* Creates a Request based on a given URI and configuration.
*
* The information contained in the URI always take precedence
* over the other information (server and parameters).
*
* @param string $uri The URI
* @param string $method The HTTP method
* @param array $parameters The query (GET) or request (POST) parameters
* @param array $cookies The request cookies ($_COOKIE)
* @param array $files The request files ($_FILES)
* @param array $server The server parameters ($_SERVER)
* @param string|resource|null $content The raw body data
*/
public static function create(string $uri, string $method = 'GET', array $parameters = [], array $cookies = [], array $files = [], array $server = [], $content = null): static
{
$server = array_replace([
'SERVER_NAME' => 'localhost',
'SERVER_PORT' => 80,
'HTTP_HOST' => 'localhost',
'HTTP_USER_AGENT' => 'Symfony',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'REMOTE_ADDR' => '127.0.0.1',
'SCRIPT_NAME' => '',
'SCRIPT_FILENAME' => '',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_TIME' => time(),
'REQUEST_TIME_FLOAT' => microtime(true),
], $server);
$server['PATH_INFO'] = '';
$server['REQUEST_METHOD'] = strtoupper($method);
$components = parse_url($uri);
if (false === $components) {
trigger_deprecation('symfony/http-foundation', '6.3', 'Calling "%s()" with an invalid URI is deprecated.', __METHOD__);
$components = [];
}
if (isset($components['host'])) {
$server['SERVER_NAME'] = $components['host'];
$server['HTTP_HOST'] = $components['host'];
}
if (isset($components['scheme'])) {
if ('https' === $components['scheme']) {
$server['HTTPS'] = 'on';
$server['SERVER_PORT'] = 443;
} else {
unset($server['HTTPS']);
$server['SERVER_PORT'] = 80;
}
}
if (isset($components['port'])) {
$server['SERVER_PORT'] = $components['port'];
$server['HTTP_HOST'] .= ':'.$components['port'];
}
if (isset($components['user'])) {
$server['PHP_AUTH_USER'] = $components['user'];
}
if (isset($components['pass'])) {
$server['PHP_AUTH_PW'] = $components['pass'];
}
if (!isset($components['path'])) {
$components['path'] = '/';
}
switch (strtoupper($method)) {
case 'POST':
case 'PUT':
case 'DELETE':
if (!isset($server['CONTENT_TYPE'])) {
$server['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
}
// no break
case 'PATCH':
$request = $parameters;
$query = [];
break;
default:
$request = [];
$query = $parameters;
break;
}
$queryString = '';
if (isset($components['query'])) {
parse_str(html_entity_decode($components['query']), $qs);
if ($query) {
$query = array_replace($qs, $query);
$queryString = http_build_query($query, '', '&');
} else {
$query = $qs;
$queryString = $components['query'];
}
} elseif ($query) {
$queryString = http_build_query($query, '', '&');
}
$server['REQUEST_URI'] = $components['path'].('' !== $queryString ? '?'.$queryString : '');
$server['QUERY_STRING'] = $queryString;
return self::createRequestFromFactory($query, $request, [], $cookies, $files, $server, $content);
}
/**
* Sets a callable able to create a Request instance.
*
* This is mainly useful when you need to override the Request class
* to keep BC with an existing system. It should not be used for any
* other purpose.
*
* @return void
*/
public static function setFactory(?callable $callable)
{
self::$requestFactory = $callable;
}
/**
* Clones a request and overrides some of its parameters.
*
* @param array|null $query The GET parameters
* @param array|null $request The POST parameters
* @param array|null $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
* @param array|null $cookies The COOKIE parameters
* @param array|null $files The FILES parameters
* @param array|null $server The SERVER parameters
*/
public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null): static
{
$dup = clone $this;
if (null !== $query) {
$dup->query = new InputBag($query);
}
if (null !== $request) {
$dup->request = new InputBag($request);
}
if (null !== $attributes) {
$dup->attributes = new ParameterBag($attributes);
}
if (null !== $cookies) {
$dup->cookies = new InputBag($cookies);
}
if (null !== $files) {
$dup->files = new FileBag($files);
}
if (null !== $server) {
$dup->server = new ServerBag($server);
$dup->headers = new HeaderBag($dup->server->getHeaders());
}
$dup->languages = null;
$dup->charsets = null;
$dup->encodings = null;
$dup->acceptableContentTypes = null;
$dup->pathInfo = null;
$dup->requestUri = null;
$dup->baseUrl = null;
$dup->basePath = null;
$dup->method = null;
$dup->format = null;
if (!$dup->get('_format') && $this->get('_format')) {
$dup->attributes->set('_format', $this->get('_format'));
}
if (!$dup->getRequestFormat(null)) {
$dup->setRequestFormat($this->getRequestFormat(null));
}
return $dup;
}
/**
* Clones the current request.
*
* Note that the session is not cloned as duplicated requests
* are most of the time sub-requests of the main one.
*/
public function __clone()
{
$this->query = clone $this->query;
$this->request = clone $this->request;
$this->attributes = clone $this->attributes;
$this->cookies = clone $this->cookies;
$this->files = clone $this->files;
$this->server = clone $this->server;
$this->headers = clone $this->headers;
}
public function __toString(): string
{
$content = $this->getContent();
$cookieHeader = '';
$cookies = [];
foreach ($this->cookies as $k => $v) {
$cookies[] = \is_array($v) ? http_build_query([$k => $v], '', '; ', \PHP_QUERY_RFC3986) : "$k=$v";
}
if ($cookies) {
$cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n";
}
return
sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n".
$this->headers.
$cookieHeader."\r\n".
$content;
}
/**
* Overrides the PHP global variables according to this request instance.
*
* It overrides $_GET, $_POST, $_REQUEST, $_SERVER, $_COOKIE.
* $_FILES is never overridden, see rfc1867
*
* @return void
*/
public function overrideGlobals()
{
$this->server->set('QUERY_STRING', static::normalizeQueryString(http_build_query($this->query->all(), '', '&')));
$_GET = $this->query->all();
$_POST = $this->request->all();
$_SERVER = $this->server->all();
$_COOKIE = $this->cookies->all();
foreach ($this->headers->all() as $key => $value) {
$key = strtoupper(str_replace('-', '_', $key));
if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true)) {
$_SERVER[$key] = implode(', ', $value);
} else {
$_SERVER['HTTP_'.$key] = implode(', ', $value);
}
}
$request = ['g' => $_GET, 'p' => $_POST, 'c' => $_COOKIE];
$requestOrder = \ini_get('request_order') ?: \ini_get('variables_order');
$requestOrder = preg_replace('#[^cgp]#', '', strtolower($requestOrder)) ?: 'gp';
$_REQUEST = [[]];
foreach (str_split($requestOrder) as $order) {
$_REQUEST[] = $request[$order];
}
$_REQUEST = array_merge(...$_REQUEST);
}
/**
* Sets a list of trusted proxies.
*
* You should only list the reverse proxies that you manage directly.
*
* @param array $proxies A list of trusted proxies, the string 'REMOTE_ADDR' will be replaced with $_SERVER['REMOTE_ADDR']
* @param int $trustedHeaderSet A bit field of Request::HEADER_*, to set which headers to trust from your proxies
*
* @return void
*/
public static function setTrustedProxies(array $proxies, int $trustedHeaderSet)
{
self::$trustedProxies = array_reduce($proxies, function ($proxies, $proxy) {
if ('REMOTE_ADDR' !== $proxy) {
$proxies[] = $proxy;
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$proxies[] = $_SERVER['REMOTE_ADDR'];
}
return $proxies;
}, []);
self::$trustedHeaderSet = $trustedHeaderSet;
}
/**
* Gets the list of trusted proxies.
*
* @return string[]
*/
public static function getTrustedProxies(): array
{
return self::$trustedProxies;
}
/**
* Gets the set of trusted headers from trusted proxies.
*
* @return int A bit field of Request::HEADER_* that defines which headers are trusted from your proxies
*/
public static function getTrustedHeaderSet(): int
{
return self::$trustedHeaderSet;
}
/**
* Sets a list of trusted host patterns.
*
* You should only list the hosts you manage using regexs.
*
* @param array $hostPatterns A list of trusted host patterns
*
* @return void
*/
public static function setTrustedHosts(array $hostPatterns)
{
self::$trustedHostPatterns = array_map(fn ($hostPattern) => sprintf('{%s}i', $hostPattern), $hostPatterns);
// we need to reset trusted hosts on trusted host patterns change
self::$trustedHosts = [];
}
/**
* Gets the list of trusted host patterns.
*
* @return string[]
*/
public static function getTrustedHosts(): array
{
return self::$trustedHostPatterns;
}
/**
* Normalizes a query string.
*
* It builds a normalized query string, where keys/value pairs are alphabetized,
* have consistent escaping and unneeded delimiters are removed.
*/
public static function normalizeQueryString(?string $qs): string
{
if ('' === ($qs ?? '')) {
return '';
}
$qs = HeaderUtils::parseQuery($qs);
ksort($qs);
return http_build_query($qs, '', '&', \PHP_QUERY_RFC3986);
}
/**
* Enables support for the _method request parameter to determine the intended HTTP method.
*
* Be warned that enabling this feature might lead to CSRF issues in your code.
* Check that you are using CSRF tokens when required.
* If the HTTP method parameter override is enabled, an html-form with method "POST" can be altered
* and used to send a "PUT" or "DELETE" request via the _method request parameter.
* If these methods are not protected against CSRF, this presents a possible vulnerability.
*
* The HTTP method can only be overridden when the real HTTP method is POST.
*
* @return void
*/
public static function enableHttpMethodParameterOverride()
{
self::$httpMethodParameterOverride = true;
}
/**
* Checks whether support for the _method request parameter is enabled.
*/
public static function getHttpMethodParameterOverride(): bool
{
return self::$httpMethodParameterOverride;
}
/**
* Gets a "parameter" value from any bag.
*
* This method is mainly useful for libraries that want to provide some flexibility. If you don't need the
* flexibility in controllers, it is better to explicitly get request parameters from the appropriate
* public property instead (attributes, query, request).
*
* Order of precedence: PATH (routing placeholders or custom attributes), GET, POST
*
* @internal use explicit input sources instead
*/
public function get(string $key, mixed $default = null): mixed
{
if ($this !== $result = $this->attributes->get($key, $this)) {
return $result;
}
if ($this->query->has($key)) {
return $this->query->all()[$key];
}
if ($this->request->has($key)) {
return $this->request->all()[$key];
}
return $default;
}
/**
* Gets the Session.
*
* @throws SessionNotFoundException When session is not set properly
*/
public function getSession(): SessionInterface
{
$session = $this->session;
if (!$session instanceof SessionInterface && null !== $session) {
$this->setSession($session = $session());
}
if (null === $session) {
throw new SessionNotFoundException('Session has not been set.');
}
return $session;
}
/**
* Whether the request contains a Session which was started in one of the
* previous requests.
*/
public function hasPreviousSession(): bool
{
// the check for $this->session avoids malicious users trying to fake a session cookie with proper name
return $this->hasSession() && $this->cookies->has($this->getSession()->getName());
}
/**
* Whether the request contains a Session object.
*
* This method does not give any information about the state of the session object,
* like whether the session is started or not. It is just a way to check if this Request
* is associated with a Session instance.
*
* @param bool $skipIfUninitialized When true, ignores factories injected by `setSessionFactory`
*/
public function hasSession(bool $skipIfUninitialized = false): bool
{
return null !== $this->session && (!$skipIfUninitialized || $this->session instanceof SessionInterface);
}
/**
* @return void
*/
public function setSession(SessionInterface $session)
{
$this->session = $session;
}
/**
* @internal
*
* @param callable(): SessionInterface $factory
*/
public function setSessionFactory(callable $factory): void
{
$this->session = $factory;
}
/**
* Returns the client IP addresses.
*
* In the returned array the most trusted IP address is first, and the
* least trusted one last. The "real" client IP address is the last one,
* but this is also the least trusted one. Trusted proxies are stripped.
*
* Use this method carefully; you should use getClientIp() instead.
*
* @see getClientIp()
*/
public function getClientIps(): array
{
$ip = $this->server->get('REMOTE_ADDR');
if (!$this->isFromTrustedProxy()) {
return [$ip];
}
return $this->getTrustedValues(self::HEADER_X_FORWARDED_FOR, $ip) ?: [$ip];
}
/**
* Returns the client IP address.
*
* This method can read the client IP address from the "X-Forwarded-For" header
* when trusted proxies were set via "setTrustedProxies()". The "X-Forwarded-For"
* header value is a comma+space separated list of IP addresses, the left-most
* being the original client, and each successive proxy that passed the request
* adding the IP address where it received the request from.
*
* If your reverse proxy uses a different header name than "X-Forwarded-For",
* ("Client-Ip" for instance), configure it via the $trustedHeaderSet
* argument of the Request::setTrustedProxies() method instead.
*
* @see getClientIps()
* @see https://wikipedia.org/wiki/X-Forwarded-For
*/
public function getClientIp(): ?string
{
$ipAddresses = $this->getClientIps();
return $ipAddresses[0];
}
/**
* Returns current script name.
*/
public function getScriptName(): string
{
return $this->server->get('SCRIPT_NAME', $this->server->get('ORIG_SCRIPT_NAME', ''));
}
/**
* Returns the path being requested relative to the executed script.
*
* The path info always starts with a /.
*
* Suppose this request is instantiated from /mysite on localhost:
*
* * http://localhost/mysite returns an empty string
* * http://localhost/mysite/about returns '/about'
* * http://localhost/mysite/enco%20ded returns '/enco%20ded'
* * http://localhost/mysite/about?var=1 returns '/about'
*
* @return string The raw path (i.e. not urldecoded)
*/
public function getPathInfo(): string
{
return $this->pathInfo ??= $this->preparePathInfo();
}
/**
* Returns the root path from which this request is executed.
*
* Suppose that an index.php file instantiates this request object:
*
* * http://localhost/index.php returns an empty string
* * http://localhost/index.php/page returns an empty string
* * http://localhost/web/index.php returns '/web'
* * http://localhost/we%20b/index.php returns '/we%20b'
*
* @return string The raw path (i.e. not urldecoded)
*/
public function getBasePath(): string
{
return $this->basePath ??= $this->prepareBasePath();
}
/**
* Returns the root URL from which this request is executed.
*
* The base URL never ends with a /.
*
* This is similar to getBasePath(), except that it also includes the
* script filename (e.g. index.php) if one exists.
*
* @return string The raw URL (i.e. not urldecoded)
*/
public function getBaseUrl(): string
{
$trustedPrefix = '';
// the proxy prefix must be prepended to any prefix being needed at the webserver level
if ($this->isFromTrustedProxy() && $trustedPrefixValues = $this->getTrustedValues(self::HEADER_X_FORWARDED_PREFIX)) {
$trustedPrefix = rtrim($trustedPrefixValues[0], '/');
}
return $trustedPrefix.$this->getBaseUrlReal();
}
/**
* Returns the real base URL received by the webserver from which this request is executed.
* The URL does not include trusted reverse proxy prefix.
*
* @return string The raw URL (i.e. not urldecoded)
*/
private function getBaseUrlReal(): string
{
return $this->baseUrl ??= $this->prepareBaseUrl();
}
/**
* Gets the request's scheme.
*/
public function getScheme(): string
{
return $this->isSecure() ? 'https' : 'http';
}
/**
* Returns the port on which the request is made.
*
* This method can read the client port from the "X-Forwarded-Port" header
* when trusted proxies were set via "setTrustedProxies()".
*
* The "X-Forwarded-Port" header must contain the client port.
*
* @return int|string|null Can be a string if fetched from the server bag
*/
public function getPort(): int|string|null
{
if ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_X_FORWARDED_PORT)) {
$host = $host[0];
} elseif ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_X_FORWARDED_HOST)) {
$host = $host[0];
} elseif (!$host = $this->headers->get('HOST')) {
return $this->server->get('SERVER_PORT');
}
if ('[' === $host[0]) {
$pos = strpos($host, ':', strrpos($host, ']'));
} else {
$pos = strrpos($host, ':');
}
if (false !== $pos && $port = substr($host, $pos + 1)) {
return (int) $port;
}
return 'https' === $this->getScheme() ? 443 : 80;
}
/**
* Returns the user.
*/
public function getUser(): ?string
{
return $this->headers->get('PHP_AUTH_USER');
}
/**
* Returns the password.
*/
public function getPassword(): ?string
{
return $this->headers->get('PHP_AUTH_PW');
}
/**
* Gets the user info.
*
* @return string|null A user name if any and, optionally, scheme-specific information about how to gain authorization to access the server
*/
public function getUserInfo(): ?string
{
$userinfo = $this->getUser();
$pass = $this->getPassword();
if ('' != $pass) {
$userinfo .= ":$pass";
}
return $userinfo;
}
/**
* Returns the HTTP host being requested.
*
* The port name will be appended to the host if it's non-standard.
*/
public function getHttpHost(): string
{
$scheme = $this->getScheme();
$port = $this->getPort();
if (('http' === $scheme && 80 == $port) || ('https' === $scheme && 443 == $port)) {
return $this->getHost();
}
return $this->getHost().':'.$port;
}
/**
* Returns the requested URI (path and query string).
*
* @return string The raw URI (i.e. not URI decoded)
*/
public function getRequestUri(): string
{
return $this->requestUri ??= $this->prepareRequestUri();
}
/**
* Gets the scheme and HTTP host.
*
* If the URL was called with basic authentication, the user
* and the password are not added to the generated string.
*/
public function getSchemeAndHttpHost(): string
{
return $this->getScheme().'://'.$this->getHttpHost();
}
/**
* Generates a normalized URI (URL) for the Request.
*
* @see getQueryString()
*/
public function getUri(): string
{
if (null !== $qs = $this->getQueryString()) {
$qs = '?'.$qs;
}
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
}
/**
* Generates a normalized URI for the given path.
*
* @param string $path A path to use instead of the current one
*/
public function getUriForPath(string $path): string
{
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$path;
}
/**
* Returns the path as relative reference from the current Request path.
*
* Only the URIs path component (no schema, host etc.) is relevant and must be given.
* Both paths must be absolute and not contain relative parts.
* Relative URLs from one resource to another are useful when generating self-contained downloadable document archives.
* Furthermore, they can be used to reduce the link size in documents.
*
* Example target paths, given a base path of "/a/b/c/d":
* - "/a/b/c/d" -> ""
* - "/a/b/c/" -> "./"
* - "/a/b/" -> "../"
* - "/a/b/c/other" -> "other"
* - "/a/x/y" -> "../../x/y"
*/
public function getRelativeUriForPath(string $path): string
{
// be sure that we are dealing with an absolute path
if (!isset($path[0]) || '/' !== $path[0]) {
return $path;
}
if ($path === $basePath = $this->getPathInfo()) {
return '';
}
$sourceDirs = explode('/', isset($basePath[0]) && '/' === $basePath[0] ? substr($basePath, 1) : $basePath);
$targetDirs = explode('/', substr($path, 1));
array_pop($sourceDirs);
$targetFile = array_pop($targetDirs);
foreach ($sourceDirs as $i => $dir) {
if (isset($targetDirs[$i]) && $dir === $targetDirs[$i]) {
unset($sourceDirs[$i], $targetDirs[$i]);
} else {
break;
}
}
$targetDirs[] = $targetFile;
$path = str_repeat('../', \count($sourceDirs)).implode('/', $targetDirs);
// A reference to the same base directory or an empty subdirectory must be prefixed with "./".
// This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
// as the first segment of a relative-path reference, as it would be mistaken for a scheme name
// (see https://tools.ietf.org/html/rfc3986#section-4.2).
return !isset($path[0]) || '/' === $path[0]
|| false !== ($colonPos = strpos($path, ':')) && ($colonPos < ($slashPos = strpos($path, '/')) || false === $slashPos)
? "./$path" : $path;
}
/**
* Generates the normalized query string for the Request.
*
* It builds a normalized query string, where keys/value pairs are alphabetized
* and have consistent escaping.
*/
public function getQueryString(): ?string
{
$qs = static::normalizeQueryString($this->server->get('QUERY_STRING'));
return '' === $qs ? null : $qs;
}
/**
* Checks whether the request is secure or not.
*
* This method can read the client protocol from the "X-Forwarded-Proto" header
* when trusted proxies were set via "setTrustedProxies()".
*
* The "X-Forwarded-Proto" header must contain the protocol: "https" or "http".
*/
public function isSecure(): bool
{
if ($this->isFromTrustedProxy() && $proto = $this->getTrustedValues(self::HEADER_X_FORWARDED_PROTO)) {
return \in_array(strtolower($proto[0]), ['https', 'on', 'ssl', '1'], true);
}
$https = $this->server->get('HTTPS');
return !empty($https) && 'off' !== strtolower($https);
}
/**
* Returns the host name.
*
* This method can read the client host name from the "X-Forwarded-Host" header
* when trusted proxies were set via "setTrustedProxies()".
*
* The "X-Forwarded-Host" header must contain the client host name.
*
* @throws SuspiciousOperationException when the host name is invalid or not trusted
*/
public function getHost(): string
{
if ($this->isFromTrustedProxy() && $host = $this->getTrustedValues(self::HEADER_X_FORWARDED_HOST)) {
$host = $host[0];
} elseif (!$host = $this->headers->get('HOST')) {
if (!$host = $this->server->get('SERVER_NAME')) {
$host = $this->server->get('SERVER_ADDR', '');
}
}
// trim and remove port number from host
// host is lowercase as per RFC 952/2181
$host = strtolower(preg_replace('/:\d+$/', '', trim($host)));
// as the host can come from the user (HTTP_HOST and depending on the configuration, SERVER_NAME too can come from the user)
// check that it does not contain forbidden characters (see RFC 952 and RFC 2181)
// use preg_replace() instead of preg_match() to prevent DoS attacks with long host names
if ($host && '' !== preg_replace('/(?:^\[)?[a-zA-Z0-9-:\]_]+\.?/', '', $host)) {
if (!$this->isHostValid) {
return '';
}
$this->isHostValid = false;
throw new SuspiciousOperationException(sprintf('Invalid Host "%s".', $host));
}
if (\count(self::$trustedHostPatterns) > 0) {
// to avoid host header injection attacks, you should provide a list of trusted host patterns
if (\in_array($host, self::$trustedHosts)) {
return $host;
}
foreach (self::$trustedHostPatterns as $pattern) {
if (preg_match($pattern, $host)) {
self::$trustedHosts[] = $host;
return $host;
}
}
if (!$this->isHostValid) {
return '';
}
$this->isHostValid = false;
throw new SuspiciousOperationException(sprintf('Untrusted Host "%s".', $host));
}
return $host;
}
/**
* Sets the request method.
*
* @return void
*/
public function setMethod(string $method)
{
$this->method = null;
$this->server->set('REQUEST_METHOD', $method);
}
/**
* Gets the request "intended" method.
*
* If the X-HTTP-Method-Override header is set, and if the method is a POST,
* then it is used to determine the "real" intended HTTP method.
*
* The _method request parameter can also be used to determine the HTTP method,
* but only if enableHttpMethodParameterOverride() has been called.
*
* The method is always an uppercased string.
*
* @see getRealMethod()
*/
public function getMethod(): string
{
if (null !== $this->method) {
return $this->method;
}
$this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
if ('POST' !== $this->method) {
return $this->method;
}
$method = $this->headers->get('X-HTTP-METHOD-OVERRIDE');
if (!$method && self::$httpMethodParameterOverride) {
$method = $this->request->get('_method', $this->query->get('_method', 'POST'));
}
if (!\is_string($method)) {
return $this->method;
}
$method = strtoupper($method);
if (\in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) {
return $this->method = $method;
}
if (!preg_match('/^[A-Z]++$/D', $method)) {
throw new SuspiciousOperationException(sprintf('Invalid method override "%s".', $method));
}
return $this->method = $method;
}
/**
* Gets the "real" request method.
*
* @see getMethod()
*/
public function getRealMethod(): string
{
return strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
}
/**
* Gets the mime type associated with the format.
*/
public function getMimeType(string $format): ?string
{
if (null === static::$formats) {
static::initializeFormats();
}
return isset(static::$formats[$format]) ? static::$formats[$format][0] : null;
}
/**
* Gets the mime types associated with the format.
*
* @return string[]
*/
public static function getMimeTypes(string $format): array
{
if (null === static::$formats) {
static::initializeFormats();
}
return static::$formats[$format] ?? [];
}
/**
* Gets the format associated with the mime type.
*/
public function getFormat(?string $mimeType): ?string
{
$canonicalMimeType = null;
if ($mimeType && false !== $pos = strpos($mimeType, ';')) {
$canonicalMimeType = trim(substr($mimeType, 0, $pos));
}
if (null === static::$formats) {
static::initializeFormats();
}
foreach (static::$formats as $format => $mimeTypes) {
if (\in_array($mimeType, (array) $mimeTypes)) {
return $format;
}
if (null !== $canonicalMimeType && \in_array($canonicalMimeType, (array) $mimeTypes)) {
return $format;
}
}
return null;
}
/**
* Associates a format with mime types.
*
* @param string|string[] $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type)
*
* @return void
*/
public function setFormat(?string $format, string|array $mimeTypes)
{
if (null === static::$formats) {
static::initializeFormats();
}
static::$formats[$format] = \is_array($mimeTypes) ? $mimeTypes : [$mimeTypes];
}
/**
* Gets the request format.
*
* Here is the process to determine the format:
*
* * format defined by the user (with setRequestFormat())
* * _format request attribute
* * $default
*
* @see getPreferredFormat
*/
public function getRequestFormat(?string $default = 'html'): ?string
{
$this->format ??= $this->attributes->get('_format');
return $this->format ?? $default;
}
/**
* Sets the request format.
*
* @return void
*/
public function setRequestFormat(?string $format)
{
$this->format = $format;
}
/**
* Gets the usual name of the format associated with the request's media type (provided in the Content-Type header).
*
* @deprecated since Symfony 6.2, use getContentTypeFormat() instead
*/
public function getContentType(): ?string
{
trigger_deprecation('symfony/http-foundation', '6.2', 'The "%s()" method is deprecated, use "getContentTypeFormat()" instead.', __METHOD__);
return $this->getContentTypeFormat();
}
/**
* Gets the usual name of the format associated with the request's media type (provided in the Content-Type header).
*
* @see Request::$formats
*/
public function getContentTypeFormat(): ?string
{
return $this->getFormat($this->headers->get('CONTENT_TYPE', ''));
}
/**
* Sets the default locale.
*
* @return void
*/
public function setDefaultLocale(string $locale)
{
$this->defaultLocale = $locale;
if (null === $this->locale) {
$this->setPhpDefaultLocale($locale);
}
}
/**
* Get the default locale.
*/
public function getDefaultLocale(): string
{
return $this->defaultLocale;
}
/**
* Sets the locale.
*
* @return void
*/
public function setLocale(string $locale)
{
$this->setPhpDefaultLocale($this->locale = $locale);
}
/**
* Get the locale.
*/
public function getLocale(): string
{
return $this->locale ?? $this->defaultLocale;
}
/**
* Checks if the request method is of specified type.
*
* @param string $method Uppercase request method (GET, POST etc)
*/
public function isMethod(string $method): bool
{
return $this->getMethod() === strtoupper($method);
}
/**
* Checks whether or not the method is safe.
*
* @see https://tools.ietf.org/html/rfc7231#section-4.2.1
*/
public function isMethodSafe(): bool
{
return \in_array($this->getMethod(), ['GET', 'HEAD', 'OPTIONS', 'TRACE']);
}
/**
* Checks whether or not the method is idempotent.
*/
public function isMethodIdempotent(): bool
{
return \in_array($this->getMethod(), ['HEAD', 'GET', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', 'PURGE']);
}
/**
* Checks whether the method is cacheable or not.
*
* @see https://tools.ietf.org/html/rfc7231#section-4.2.3
*/
public function isMethodCacheable(): bool
{
return \in_array($this->getMethod(), ['GET', 'HEAD']);
}
/**
* Returns the protocol version.
*
* If the application is behind a proxy, the protocol version used in the
* requests between the client and the proxy and between the proxy and the
* server might be different. This returns the former (from the "Via" header)
* if the proxy is trusted (see "setTrustedProxies()"), otherwise it returns
* the latter (from the "SERVER_PROTOCOL" server parameter).
*/
public function getProtocolVersion(): ?string
{
if ($this->isFromTrustedProxy()) {
preg_match('~^(HTTP/)?([1-9]\.[0-9]) ~', $this->headers->get('Via') ?? '', $matches);
if ($matches) {
return 'HTTP/'.$matches[2];
}
}
return $this->server->get('SERVER_PROTOCOL');
}
/**
* Returns the request body content.
*
* @param bool $asResource If true, a resource will be returned
*
* @return string|resource
*
* @psalm-return ($asResource is true ? resource : string)
*/
public function getContent(bool $asResource = false)
{
$currentContentIsResource = \is_resource($this->content);
if (true === $asResource) {
if ($currentContentIsResource) {
rewind($this->content);
return $this->content;
}
// Content passed in parameter (test)
if (\is_string($this->content)) {
$resource = fopen('php://temp', 'r+');
fwrite($resource, $this->content);
rewind($resource);
return $resource;
}
$this->content = false;
return fopen('php://input', 'r');
}
if ($currentContentIsResource) {
rewind($this->content);
return stream_get_contents($this->content);
}
if (null === $this->content || false === $this->content) {
$this->content = file_get_contents('php://input');
}
return $this->content;
}
/**
* Gets the decoded form or json request body.
*
* @throws JsonException When the body cannot be decoded to an array
*/
public function getPayload(): InputBag
{
if ($this->request->count()) {
return clone $this->request;
}
if ('' === $content = $this->getContent()) {
return new InputBag([]);
}
try {
$content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new JsonException('Could not decode request body.', $e->getCode(), $e);
}
if (!\is_array($content)) {
throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
}
return new InputBag($content);
}
/**
* Gets the request body decoded as array, typically from a JSON payload.
*
* @see getPayload() for portability between content types
*
* @throws JsonException When the body cannot be decoded to an array
*/
public function toArray(): array
{
if ('' === $content = $this->getContent()) {
throw new JsonException('Request body is empty.');
}
try {
$content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new JsonException('Could not decode request body.', $e->getCode(), $e);
}
if (!\is_array($content)) {
throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
}
return $content;
}
/**
* Gets the Etags.
*/
public function getETags(): array
{
return preg_split('/\s*,\s*/', $this->headers->get('If-None-Match', ''), -1, \PREG_SPLIT_NO_EMPTY);
}
public function isNoCache(): bool
{
return $this->headers->hasCacheControlDirective('no-cache') || 'no-cache' == $this->headers->get('Pragma');
}
/**
* Gets the preferred format for the response by inspecting, in the following order:
* * the request format set using setRequestFormat;
* * the values of the Accept HTTP header.
*
* Note that if you use this method, you should send the "Vary: Accept" header
* in the response to prevent any issues with intermediary HTTP caches.
*/
public function getPreferredFormat(?string $default = 'html'): ?string
{
if (null !== $this->preferredFormat || null !== $this->preferredFormat = $this->getRequestFormat(null)) {
return $this->preferredFormat;
}
foreach ($this->getAcceptableContentTypes() as $mimeType) {
if ($this->preferredFormat = $this->getFormat($mimeType)) {
return $this->preferredFormat;
}
}
return $default;
}
/**
* Returns the preferred language.
*
* @param string[] $locales An array of ordered available locales
*/
public function getPreferredLanguage(array $locales = null): ?string
{
$preferredLanguages = $this->getLanguages();
if (empty($locales)) {
return $preferredLanguages[0] ?? null;
}
if (!$preferredLanguages) {
return $locales[0];
}
$extendedPreferredLanguages = [];
foreach ($preferredLanguages as $language) {
$extendedPreferredLanguages[] = $language;
if (false !== $position = strpos($language, '_')) {
$superLanguage = substr($language, 0, $position);
if (!\in_array($superLanguage, $preferredLanguages)) {
$extendedPreferredLanguages[] = $superLanguage;
}
}
}
$preferredLanguages = array_values(array_intersect($extendedPreferredLanguages, $locales));
return $preferredLanguages[0] ?? $locales[0];
}
/**
* Gets a list of languages acceptable by the client browser ordered in the user browser preferences.
*
* @return string[]
*/
public function getLanguages(): array
{
if (null !== $this->languages) {
return $this->languages;
}
$languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all();
$this->languages = [];
foreach ($languages as $acceptHeaderItem) {
$lang = $acceptHeaderItem->getValue();
if (str_contains($lang, '-')) {
$codes = explode('-', $lang);
if ('i' === $codes[0]) {
// Language not listed in ISO 639 that are not variants
// of any listed language, which can be registered with the
// i-prefix, such as i-cherokee
if (\count($codes) > 1) {
$lang = $codes[1];
}
} else {
for ($i = 0, $max = \count($codes); $i < $max; ++$i) {
if (0 === $i) {
$lang = strtolower($codes[0]);
} else {
$lang .= '_'.strtoupper($codes[$i]);
}
}
}
}
$this->languages[] = $lang;
}
return $this->languages;
}
/**
* Gets a list of charsets acceptable by the client browser in preferable order.
*
* @return string[]
*/
public function getCharsets(): array
{
if (null !== $this->charsets) {
return $this->charsets;
}
return $this->charsets = array_map('strval', array_keys(AcceptHeader::fromString($this->headers->get('Accept-Charset'))->all()));
}
/**
* Gets a list of encodings acceptable by the client browser in preferable order.
*
* @return string[]
*/
public function getEncodings(): array
{
if (null !== $this->encodings) {
return $this->encodings;
}
return $this->encodings = array_map('strval', array_keys(AcceptHeader::fromString($this->headers->get('Accept-Encoding'))->all()));
}
/**
* Gets a list of content types acceptable by the client browser in preferable order.
*
* @return string[]
*/
public function getAcceptableContentTypes(): array
{
if (null !== $this->acceptableContentTypes) {
return $this->acceptableContentTypes;
}
return $this->acceptableContentTypes = array_map('strval', array_keys(AcceptHeader::fromString($this->headers->get('Accept'))->all()));
}
/**
* Returns true if the request is an XMLHttpRequest.
*
* It works if your JavaScript library sets an X-Requested-With HTTP header.
* It is known to work with common JavaScript frameworks:
*
* @see https://wikipedia.org/wiki/List_of_Ajax_frameworks#JavaScript
*/
public function isXmlHttpRequest(): bool
{
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
}
/**
* Checks whether the client browser prefers safe content or not according to RFC8674.
*
* @see https://tools.ietf.org/html/rfc8674
*/
public function preferSafeContent(): bool
{
if (isset($this->isSafeContentPreferred)) {
return $this->isSafeContentPreferred;
}
if (!$this->isSecure()) {
// see https://tools.ietf.org/html/rfc8674#section-3
return $this->isSafeContentPreferred = false;
}
return $this->isSafeContentPreferred = AcceptHeader::fromString($this->headers->get('Prefer'))->has('safe');
}
/*
* The following methods are derived from code of the Zend Framework (1.10dev - 2010-01-24)
*
* Code subject to the new BSD license (https://framework.zend.com/license).
*
* Copyright (c) 2005-2010 Zend Technologies USA Inc. (https://www.zend.com/)
*/
/**
* @return string
*/
protected function prepareRequestUri()
{
$requestUri = '';
if ('1' == $this->server->get('IIS_WasUrlRewritten') && '' != $this->server->get('UNENCODED_URL')) {
// IIS7 with URL Rewrite: make sure we get the unencoded URL (double slash problem)
$requestUri = $this->server->get('UNENCODED_URL');
$this->server->remove('UNENCODED_URL');
$this->server->remove('IIS_WasUrlRewritten');
} elseif ($this->server->has('REQUEST_URI')) {
$requestUri = $this->server->get('REQUEST_URI');
if ('' !== $requestUri && '/' === $requestUri[0]) {
// To only use path and query remove the fragment.
if (false !== $pos = strpos($requestUri, '#')) {
$requestUri = substr($requestUri, 0, $pos);
}
} else {
// HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path,
// only use URL path.
$uriComponents = parse_url($requestUri);
if (isset($uriComponents['path'])) {
$requestUri = $uriComponents['path'];
}
if (isset($uriComponents['query'])) {
$requestUri .= '?'.$uriComponents['query'];
}
}
} elseif ($this->server->has('ORIG_PATH_INFO')) {
// IIS 5.0, PHP as CGI
$requestUri = $this->server->get('ORIG_PATH_INFO');
if ('' != $this->server->get('QUERY_STRING')) {
$requestUri .= '?'.$this->server->get('QUERY_STRING');
}
$this->server->remove('ORIG_PATH_INFO');
}
// normalize the request URI to ease creating sub-requests from this request
$this->server->set('REQUEST_URI', $requestUri);
return $requestUri;
}
/**
* Prepares the base URL.
*/
protected function prepareBaseUrl(): string
{
$filename = basename($this->server->get('SCRIPT_FILENAME', ''));
if (basename($this->server->get('SCRIPT_NAME', '')) === $filename) {
$baseUrl = $this->server->get('SCRIPT_NAME');
} elseif (basename($this->server->get('PHP_SELF', '')) === $filename) {
$baseUrl = $this->server->get('PHP_SELF');
} elseif (basename($this->server->get('ORIG_SCRIPT_NAME', '')) === $filename) {
$baseUrl = $this->server->get('ORIG_SCRIPT_NAME'); // 1and1 shared hosting compatibility
} else {
// Backtrack up the script_filename to find the portion matching
// php_self
$path = $this->server->get('PHP_SELF', '');
$file = $this->server->get('SCRIPT_FILENAME', '');
$segs = explode('/', trim($file, '/'));
$segs = array_reverse($segs);
$index = 0;
$last = \count($segs);
$baseUrl = '';
do {
$seg = $segs[$index];
$baseUrl = '/'.$seg.$baseUrl;
++$index;
} while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos);
}
// Does the baseUrl have anything in common with the request_uri?
$requestUri = $this->getRequestUri();
if ('' !== $requestUri && '/' !== $requestUri[0]) {
$requestUri = '/'.$requestUri;
}
if ($baseUrl && null !== $prefix = $this->getUrlencodedPrefix($requestUri, $baseUrl)) {
// full $baseUrl matches
return $prefix;
}
if ($baseUrl && null !== $prefix = $this->getUrlencodedPrefix($requestUri, rtrim(\dirname($baseUrl), '/'.\DIRECTORY_SEPARATOR).'/')) {
// directory portion of $baseUrl matches
return rtrim($prefix, '/'.\DIRECTORY_SEPARATOR);
}
$truncatedRequestUri = $requestUri;
if (false !== $pos = strpos($requestUri, '?')) {
$truncatedRequestUri = substr($requestUri, 0, $pos);
}
$basename = basename($baseUrl ?? '');
if (empty($basename) || !strpos(rawurldecode($truncatedRequestUri), $basename)) {
// no match whatsoever; set it blank
return '';
}
// If using mod_rewrite or ISAPI_Rewrite strip the script filename
// out of baseUrl. $pos !== 0 makes sure it is not matching a value
// from PATH_INFO or QUERY_STRING
if (\strlen($requestUri) >= \strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) {
$baseUrl = substr($requestUri, 0, $pos + \strlen($baseUrl));
}
return rtrim($baseUrl, '/'.\DIRECTORY_SEPARATOR);
}
/**
* Prepares the base path.
*/
protected function prepareBasePath(): string
{
$baseUrl = $this->getBaseUrl();
if (empty($baseUrl)) {
return '';
}
$filename = basename($this->server->get('SCRIPT_FILENAME'));
if (basename($baseUrl) === $filename) {
$basePath = \dirname($baseUrl);
} else {
$basePath = $baseUrl;
}
if ('\\' === \DIRECTORY_SEPARATOR) {
$basePath = str_replace('\\', '/', $basePath);
}
return rtrim($basePath, '/');
}
/**
* Prepares the path info.
*/
protected function preparePathInfo(): string
{
if (null === ($requestUri = $this->getRequestUri())) {
return '/';
}
// Remove the query string from REQUEST_URI
if (false !== $pos = strpos($requestUri, '?')) {
$requestUri = substr($requestUri, 0, $pos);
}
if ('' !== $requestUri && '/' !== $requestUri[0]) {
$requestUri = '/'.$requestUri;
}
if (null === ($baseUrl = $this->getBaseUrlReal())) {
return $requestUri;
}
$pathInfo = substr($requestUri, \strlen($baseUrl));
if (false === $pathInfo || '' === $pathInfo) {
// If substr() returns false then PATH_INFO is set to an empty string
return '/';
}
return $pathInfo;
}
/**
* Initializes HTTP request formats.
*
* @return void
*/
protected static function initializeFormats()
{
static::$formats = [
'html' => ['text/html', 'application/xhtml+xml'],
'txt' => ['text/plain'],
'js' => ['application/javascript', 'application/x-javascript', 'text/javascript'],
'css' => ['text/css'],
'json' => ['application/json', 'application/x-json'],
'jsonld' => ['application/ld+json'],
'xml' => ['text/xml', 'application/xml', 'application/x-xml'],
'rdf' => ['application/rdf+xml'],
'atom' => ['application/atom+xml'],
'rss' => ['application/rss+xml'],
'form' => ['application/x-www-form-urlencoded', 'multipart/form-data'],
];
}
private function setPhpDefaultLocale(string $locale): void
{
// if either the class Locale doesn't exist, or an exception is thrown when
// setting the default locale, the intl module is not installed, and
// the call can be ignored:
try {
if (class_exists(\Locale::class, false)) {
\Locale::setDefault($locale);
}
} catch (\Exception) {
}
}
/**
* Returns the prefix as encoded in the string when the string starts with
* the given prefix, null otherwise.
*/
private function getUrlencodedPrefix(string $string, string $prefix): ?string
{
if (!str_starts_with(rawurldecode($string), $prefix)) {
return null;
}
$len = \strlen($prefix);
if (preg_match(sprintf('#^(%%[[:xdigit:]]{2}|.){%d}#', $len), $string, $match)) {
return $match[0];
}
return null;
}
private static function createRequestFromFactory(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null): static
{
if (self::$requestFactory) {
$request = (self::$requestFactory)($query, $request, $attributes, $cookies, $files, $server, $content);
if (!$request instanceof self) {
throw new \LogicException('The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.');
}
return $request;
}
return new static($query, $request, $attributes, $cookies, $files, $server, $content);
}
/**
* Indicates whether this request originated from a trusted proxy.
*
* This can be useful to determine whether or not to trust the
* contents of a proxy-specific header.
*/
public function isFromTrustedProxy(): bool
{
return self::$trustedProxies && IpUtils::checkIp($this->server->get('REMOTE_ADDR', ''), self::$trustedProxies);
}
private function getTrustedValues(int $type, string $ip = null): array
{
$clientValues = [];
$forwardedValues = [];
if ((self::$trustedHeaderSet & $type) && $this->headers->has(self::TRUSTED_HEADERS[$type])) {
foreach (explode(',', $this->headers->get(self::TRUSTED_HEADERS[$type])) as $v) {
$clientValues[] = (self::HEADER_X_FORWARDED_PORT === $type ? '0.0.0.0:' : '').trim($v);
}
}
if ((self::$trustedHeaderSet & self::HEADER_FORWARDED) && (isset(self::FORWARDED_PARAMS[$type])) && $this->headers->has(self::TRUSTED_HEADERS[self::HEADER_FORWARDED])) {
$forwarded = $this->headers->get(self::TRUSTED_HEADERS[self::HEADER_FORWARDED]);
$parts = HeaderUtils::split($forwarded, ',;=');
$forwardedValues = [];
$param = self::FORWARDED_PARAMS[$type];
foreach ($parts as $subParts) {
if (null === $v = HeaderUtils::combine($subParts)[$param] ?? null) {
continue;
}
if (self::HEADER_X_FORWARDED_PORT === $type) {
if (str_ends_with($v, ']') || false === $v = strrchr($v, ':')) {
$v = $this->isSecure() ? ':443' : ':80';
}
$v = '0.0.0.0'.$v;
}
$forwardedValues[] = $v;
}
}
if (null !== $ip) {
$clientValues = $this->normalizeAndFilterClientIps($clientValues, $ip);
$forwardedValues = $this->normalizeAndFilterClientIps($forwardedValues, $ip);
}
if ($forwardedValues === $clientValues || !$clientValues) {
return $forwardedValues;
}
if (!$forwardedValues) {
return $clientValues;
}
if (!$this->isForwardedValid) {
return null !== $ip ? ['0.0.0.0', $ip] : [];
}
$this->isForwardedValid = false;
throw new ConflictingHeadersException(sprintf('The request has both a trusted "%s" header and a trusted "%s" header, conflicting with each other. You should either configure your proxy to remove one of them, or configure your project to distrust the offending one.', self::TRUSTED_HEADERS[self::HEADER_FORWARDED], self::TRUSTED_HEADERS[$type]));
}
private function normalizeAndFilterClientIps(array $clientIps, string $ip): array
{
if (!$clientIps) {
return [];
}
$clientIps[] = $ip; // Complete the IP chain with the IP the request actually came from
$firstTrustedIp = null;
foreach ($clientIps as $key => $clientIp) {
if (strpos($clientIp, '.')) {
// Strip :port from IPv4 addresses. This is allowed in Forwarded
// and may occur in X-Forwarded-For.
$i = strpos($clientIp, ':');
if ($i) {
$clientIps[$key] = $clientIp = substr($clientIp, 0, $i);
}
} elseif (str_starts_with($clientIp, '[')) {
// Strip brackets and :port from IPv6 addresses.
$i = strpos($clientIp, ']', 1);
$clientIps[$key] = $clientIp = substr($clientIp, 1, $i - 1);
}
if (!filter_var($clientIp, \FILTER_VALIDATE_IP)) {
unset($clientIps[$key]);
continue;
}
if (IpUtils::checkIp($clientIp, self::$trustedProxies)) {
unset($clientIps[$key]);
// Fallback to this when the client IP falls into the range of trusted proxies
$firstTrustedIp ??= $clientIp;
}
}
// Now the IP chain contains only untrusted proxies and the client IP
return $clientIps ? array_reverse($clientIps) : [$firstTrustedIp];
}
}
We have trained our AI Chat Bots with the knowledge of industry experts and conversion experts so you can be sure it knows how to do its job and answer all your questions instantly and provide requested information
AI Content Generation
Create amazing content 10X faster
Wordfairy can help you with a variety of writing tasks, from writing blog post, creating better resumes and job descriptions to composing emails and social media content, and many more. With 70+ templates, we can save you time and improve your writing skills.
AI Image Creation
Use AI to create any art or image
Are you looking for a tool to help you create unique beautiful artwork and images quickly and easily? Look no further! Our AI-powered software makes it simple to generate high-quality art and images with just a few clicks. With our intuitive interface and powerful technology, you can create stunning visuals in minutes instead of hours.
AI Voiceover Synthesize
Make studio-quality voiceovers in minutes
Truly human emotions in every voice over generated, breathing life into your voice overs. Our AI voices have elements that make a voice sound NATURAL and have all the expressions and tone inflections that are needed to make people more engaged in your content
AI Speech to Text Transcribe
Transcribe accurately your audio
Accurately transcribe audio content in various formats. Enable transcription of your audio files in multiple languages, as well as translation from those languages into English.
AI Code Generation
Write code like a Pro
Generate complex algorithms simply by using natural language to explain what you are after, we will take care rest for you. Write code like Pro in Python, Flutter, PHP, JavaScript, Ruby and other programming languages.
Think Big, Task Easy
Customers Using Incus Have Experienced:
90%
Time Reduced In Task
4x
Increase In Visibility
60%
Increase In Revenue
Select a Task
Choose from a variety of writing skills trained on industry best-practices
Ads
Create ads much faster and be more creative
Clickbait Titles
Create a creative clickbait titles for your products
Pro
Ad Headlines
Write an attention grabbing ad headlines
Blog Posts
Content for the generating articles, blog post
Blog Titles
Nobody wants to read boring blog titles, generate catchy blog titles with this tool
Blog Section
Write a full blog section (few paragraphs) about a subheading of your article
Blog Ideas
The perfect tool to start writing great articles. Generate creative ideas for your next post
Blog Intros
Write an intro that will entice your visitors to read more about your article
Blog Conclusion
End your blog articles with an engaging conclusion paragraph
Contents
Tools for writing creatives for different moods and tasks
Article Generator
Turn a title and outline text into a fully complete high quality article within seconds
Content Rewriter
Take a piece of content and rewrite it to make it more interesting, creative, and engaging
Paragraph Generator
Generate paragraphs about any topic including a keyword and in a specific tone of voice
Talking Points
Write short, simple and informative points for the subheadings of your article
Pros & Cons
Write the pros and cons of a product, service or website for your blog article
Summarize Text
Summarize any text in a short and easy to understand concise way
Product Description
Write the description about your product and why it worth it
Startup Name Generator
Generate cool, creative, and catchy names for your startup in seconds
Product Name Generator
Create creative product names from examples words
Academic Essay
Create creative academic essays for various subjects just in a second
Creative Stories
Allow AI to generate creative stories for you based on input text
Grammar Checker
Make sure that there are no errors in your content
Summarize for 2nd Grader
Summarize any complex content for a 2nd grader child
Text Extender
Extend your sentences with more description and additional information
Rewrite with Keywords
Rewrite your existing content with including specific keywords
Business Ideas
Generate business ideas based on your keywords and description
Tone Changer
Change the tone of your writing to match your audience
Dictionary
Use a dictionary to find all details of your word
Privacy Policy
Develop a privacy policy information for your organization
Terms and Conditions
Develop a terms and conditions information for your organization
Ecommerce
Powerful tools for e-commerce, listings of your products
Great AI tool to use for academic essay writing and summarising texts!
Josh P
UK
5
Absolutely blown away with the quality of the writing that the AI provides and all the different features, one of the best i've used by far.
Laura S
UK
5
Using this for university and its made my life so much easier!
Tom W
UK
5
Really impressed with everything thats offered by the price!
Sarah
UK
5
Used this when I had a writers block and wow! one of the best features is being able to change the writing tone and style of each text.
Peter Scott
UK
5
An absolute dream for an assignment that I had due. I was skeptical at first, but after seeing my grades go up I use wordfairy for all of my coursework.
John Cross
UK
5
Woohoo! Word Fairy AI just sprinkled some serious writing magic in my life! It's like having a personal writing assistant that never sleeps.
Tim
UK
5
I'm not exaggerating when I say Word Fairy AI is the Dumbledore of writing tools. It's like having a magical professor guiding me through the writing process. I was skeptical at first—it's AI after all, right? But boy, was I proven wrong!
Kira Brown
UK
5
Finally met my writing soulmate! Word Fairy AI has become my brainstorm buddy, my grammar guru, and my secret weapon in the writing world. Its artificial intelligence beams with brilliance, offering word choices I'd never even think of!
Rob
UK
5
Writing just got a major upgrade, thanks to Word Fairy AI! It's like having a pocket-sized Shakespeare with impeccable grammar skills! This clever tool turns my writing foes into friends, fixing those sneaky typos and suggesting ways to improve clarity.
Bill
UK
5
Word Fairy AI is a total game-changer! It's like having a writing superhero by my side, guiding me through every paragraph. It spins my ideas into engaging content effortlessly.
Carol K
UK
5
Wow, Word Fairy AI has turned me into a wordsmith extraordinaire! I used to struggle with writer's block, but with this incredible writing tool, ideas flow like a river.
Rafique
UK
5
Prepare to be mind-blown by Word Fairy AI! This brilliant tool is my secret weapon in the battle against dull content. It sprinkles captivating phrases and catchy headlines all over my writing. Breath-taking AI-powered magic at its best.
Bella
UK
5
Wowza! Word Fairy AI is the writing genie I never knew I needed! With just a few clicks, it conjures up impeccable sentences and helps structure my thoughts flawlessly. Writing, editing, and proofreading have become a breeze.
Antonio
UK
5
This brilliant tool sprinkles dazzling vocabulary and grammar skills into your work, making it shine like a supernova. No more worrying about errors or boring content.
Jenny
UK
5
This baby corrects my clumsy wording, suggests better sentence structures, and even helps me find the perfect synonyms to spice things up. Seriously, if you've ever struggled with writing, let the Word Fairy AI work its magic for you!"
Iris
UK
5
t's like having a personal coach, pushing me towards better writing. Whether I'm composing a professional email or a creative masterpiece, this tool sprinkles linguistic fairy dust on every word. If you're tired of mediocre writing, make Word Fairy AI your new BFF!
Chen
UK
5
My productivity and creativity have hit the stratosphere thanks to this incredible AI. Prepare yourself for word wizardry!
Sumaya
UK
5
Seriously, where has this little AI genius been all my life? Writing projects have become a breeze, and my work is so much more polished now. Word Fairy AI, you've earned your wings in my heart!
Ellie
UK
5
Thanks to this genius AI, my writing has soared to new heights! I can't wait to see what magical stories we'll create together next!
Adam G
USA
5
Great AI tool to use for academic essay writing and summarising texts!
Josh P
UK
5
Absolutely blown away with the quality of the writing that the AI provides and all the different features, one of the best i've used by far.
Laura S
UK
5
Using this for university and its made my life so much easier!
Tom W
UK
5
Really impressed with everything thats offered by the price!
Sarah
UK
5
Used this when I had a writers block and wow! one of the best features is being able to change the writing tone and style of each text.
Peter Scott
UK
5
An absolute dream for an assignment that I had due. I was skeptical at first, but after seeing my grades go up I use wordfairy for all of my coursework.
John Cross
UK
5
Woohoo! Word Fairy AI just sprinkled some serious writing magic in my life! It's like having a personal writing assistant that never sleeps.
Tim
UK
5
I'm not exaggerating when I say Word Fairy AI is the Dumbledore of writing tools. It's like having a magical professor guiding me through the writing process. I was skeptical at first—it's AI after all, right? But boy, was I proven wrong!
Kira Brown
UK
5
Finally met my writing soulmate! Word Fairy AI has become my brainstorm buddy, my grammar guru, and my secret weapon in the writing world. Its artificial intelligence beams with brilliance, offering word choices I'd never even think of!
Rob
UK
5
Writing just got a major upgrade, thanks to Word Fairy AI! It's like having a pocket-sized Shakespeare with impeccable grammar skills! This clever tool turns my writing foes into friends, fixing those sneaky typos and suggesting ways to improve clarity.
Bill
UK
5
Word Fairy AI is a total game-changer! It's like having a writing superhero by my side, guiding me through every paragraph. It spins my ideas into engaging content effortlessly.
Carol K
UK
5
Wow, Word Fairy AI has turned me into a wordsmith extraordinaire! I used to struggle with writer's block, but with this incredible writing tool, ideas flow like a river.
Rafique
UK
5
Prepare to be mind-blown by Word Fairy AI! This brilliant tool is my secret weapon in the battle against dull content. It sprinkles captivating phrases and catchy headlines all over my writing. Breath-taking AI-powered magic at its best.
Bella
UK
5
Wowza! Word Fairy AI is the writing genie I never knew I needed! With just a few clicks, it conjures up impeccable sentences and helps structure my thoughts flawlessly. Writing, editing, and proofreading have become a breeze.
Antonio
UK
5
This brilliant tool sprinkles dazzling vocabulary and grammar skills into your work, making it shine like a supernova. No more worrying about errors or boring content.
Jenny
UK
5
This baby corrects my clumsy wording, suggests better sentence structures, and even helps me find the perfect synonyms to spice things up. Seriously, if you've ever struggled with writing, let the Word Fairy AI work its magic for you!"
Iris
UK
5
t's like having a personal coach, pushing me towards better writing. Whether I'm composing a professional email or a creative masterpiece, this tool sprinkles linguistic fairy dust on every word. If you're tired of mediocre writing, make Word Fairy AI your new BFF!
Chen
UK
5
My productivity and creativity have hit the stratosphere thanks to this incredible AI. Prepare yourself for word wizardry!
Sumaya
UK
5
Seriously, where has this little AI genius been all my life? Writing projects have become a breeze, and my work is so much more polished now. Word Fairy AI, you've earned your wings in my heart!
Ellie
UK
5
Thanks to this genius AI, my writing has soared to new heights! I can't wait to see what magical stories we'll create together next!
Frequently Asked Questions
Got Questions? We have you covered
We are always here to provide full support and clear any doubts that you might have
What Is WordFairy AI?
Word Fairy AI is an advanced artificial intelligence writing tool designed to assist writers in creating high-quality and engaging content. Equipped with state-of-the-art algorithms, this innovative tool offers unparalleled writing guidance and support.
With Word Fairy AI, users can expect an intelligent and efficient writing partner that can generate ideas, provide grammar and spelling suggestions, and help structure essays, articles, or any written work. By leveraging the power of AI, this tool harnesses vast amounts of data and knowledge to deliver accurate and contextually relevant recommendations, saving writers valuable time and effort.
Gone are the days of struggling with writer's block or spending hours on end trying to perfect a sentence or paragraph. Word Fairy AI acts as a virtual mentor, offering creative inspiration and guiding users with well-crafted suggestions tailored to their specific writing needs. Whether you're an aspiring writer, a student, or a seasoned professional, this writing tool is your ultimate companion in the journey of crafting impeccable content.
Does Wordfairy AI Offer A Free Trial?
Absolutely! Word Fairy AI offers a free sign up and trail of 1000 words for you to see all of our different features and test out our custom AI software. We're certain that you will be blown away with how advanced and customised our software is.
What Is The Word Fairy AI Referral Scheme
Do you know anyone who can benefit from Word Fairy AI? Refer a friend and we will give you 50% of the value of the first month of their subscription in the form of a digital amazon voucher. Get in touch with us at admin@wordfairy.ai for more details!
How Does Word Fairy AI Work?
Equipped with cutting-edge algorithms, Word Fairy AI assists users in generating engaging and persuasive content across various genres and formats. From blog posts and marketing copy to academic papers and creative writing, this tool adapts to the specific writing style and requirements of each user, providing invaluable suggestions to enhance coherence, clarity, and overall impact.
What sets Word Fairy AI apart is its ability to learn and adapt to individual preferences and writing goals. As users interact with the tool, it gradually tailors its suggestions based on their writing patterns and preferences. This personalized approach ensures that each user receives the most relevant and helpful recommendations throughout their writing journey.
By leveraging the vast knowledge and deep understanding of language encoded within its algorithms, Word Fairy AI enables writers to express their ideas with precision and eloquence. From grammar and spelling corrections to insightful vocabulary suggestions, this tool acts as a virtual writing tutor, providing continuous feedback and support to help users reach their full potential.
How Are Texts Generated For The Same Answer Different?
What sets Word Fairy AI apart is its ability to analyze the context and purpose of the writing, generating tailored suggestions that resonate with the intended audience. By considering factors such as genre, tone, and target audience, this tool guides users toward crafting content that captivates and engages readers, making every written piece a masterpiece.
Furthermore, Word Fairy AI acts as a platform for continuous growth and improvement. By analyzing the writing patterns and preferences of each user, this tool learns and adapts, delivering increasingly accurate and contextually relevant suggestions as users interact with it more frequently. This personalized approach guarantees that each individual's unique style and voice shine through, fostering a sense of authenticity in every written work.
With its vast knowledge database and intricate understanding of language nuances, Word Fairy AI offers comprehensive writing support at users' fingertips. From instantaneous grammar checks to insightful vocabulary suggestions, this tool inspires users to push the boundaries of their writing abilities and enables them to create compelling and impactful content that resonates with their intended audience.
This means that even if the same question for the same topic is asked 100 times, there will be 100 unique and completely different answers with no similarities.
How Is Word Fairy AI Different From Chat GPT?
Word Fairy AI is distinct from Chat GPT in terms of its primary function and purpose. While Chat GPT is mainly designed to engage in conversational interactions and provide responses like a chatbot, Word Fairy AI focuses predominantly on enhancing the writing capabilities of its users. With a range of advanced language models and text generation algorithms, Word Fairy AI aims to assist individuals in crafting high-quality written content for various purposes, such as articles, reports, blog posts, or creative writing. By leveraging it's powerful AI technology, Word Fairy AI can provide users with intelligent suggestions, grammar corrections, and insightful recommendations to refine their writing skills and create compelling textual compositions.
Can I Use Word Fairy For My University Assignments?
Yes, you can definitely use Word Fairy for your university assignments. Word Fairy AI is an advanced artificial intelligence writing tool designed to assist users in creating well-written and professional documents. Its powerful features, such as grammar and spell-check, suggest improvements, and content enhancement, make it an ideal tool for academic writing.
Word Fairy AI ensures that your assignments are free from grammatical errors, typos, and awkward phrasing. This tool not only helps you proofread your work but also offers suggestions for improving sentence structure, vocabulary usage, and overall coherence. It acts as a virtual writing assistant, providing valuable feedback to enhance the quality of your assignments.
Moreover, Word Fairy AI saves you time and effort by automating tedious tasks such as citations and bibliography formatting. With its comprehensive database of citation styles, including APA, MLA, and Chicago, you can be confident that your references are accurately formatted. This allows you to focus more on the content of your assignments, ensuring it meets the high academic standards set by your university.
It is important to note that while Word Fairy AI assists and streamlines the writing process, it is still your responsibility as a student to ensure the integrity of your work. The tool's primary purpose is to enhance your writing skills and provide suggestions, but ultimately, you must ensure that the content and ideas presented in your assignments are original and properly cited.
In summary, Word Fairy AI is an invaluable tool for your university assignments. It provides a range of features designed to improve your writing, enhance the quality of your work, and save you time. By using Word Fairy AI, you can confidently submit well-written and professional assignments that meet the academic standards of your university.
I Still Have Questions
No Problem at all! Please feel free to get in touch with us at admin@wordfairy.ai and we would be more than happy to help
Facebook Ads
Write Facebook ads that engage your audience and deliver a high conversion rate
Instagram Captions
Grab attention with catchy captions for your Instagram posts
Instagram Hashtags Generator
Find the best hashtags to use for your Instagram posts
Social Media Post (Personal)
Write a social media post for yourself to be published on any platform
Social Media Post (Business)
Write a post for your business to be published on any social media platform
Facebook Headlines
Write catchy and convincing headlines to make your Facebook Ads stand out
Google Ads Headlines
Write catchy 30-character headlines to promote your product with Google Ads
Google Ads Description
Write a Google Ads description that makes your ad stand out and generates leads
LinkedIn Posts
Create an interesting linkedin post with the help of AI
Twitter Tweets
Generate an interesting twitter tweets with AI
LinkedIn Ad Headlines
Attention-grabbing, click-inducing and high-converting ad headlines for LinkedIn
LinkedIn Ad Descriptions
Professional and eye-catching ad descriptions that will make your product shine