ServerRequest.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Http;
  17. use BadMethodCallException;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Exception\CakeException;
  20. use Cake\Http\Cookie\CookieCollection;
  21. use Cake\Http\Exception\MethodNotAllowedException;
  22. use Cake\Utility\Hash;
  23. use InvalidArgumentException;
  24. use Laminas\Diactoros\PhpInputStream;
  25. use Laminas\Diactoros\Stream;
  26. use Laminas\Diactoros\UploadedFile;
  27. use Psr\Http\Message\ServerRequestInterface;
  28. use Psr\Http\Message\StreamInterface;
  29. use Psr\Http\Message\UploadedFileInterface;
  30. use Psr\Http\Message\UriInterface;
  31. /**
  32. * A class that helps wrap Request information and particulars about a single request.
  33. * Provides methods commonly used to introspect on the request headers and request body.
  34. */
  35. class ServerRequest implements ServerRequestInterface
  36. {
  37. /**
  38. * Array of parameters parsed from the URL.
  39. *
  40. * @var array
  41. */
  42. protected $params = [
  43. 'plugin' => null,
  44. 'controller' => null,
  45. 'action' => null,
  46. '_ext' => null,
  47. 'pass' => [],
  48. ];
  49. /**
  50. * Array of POST data. Will contain form data as well as uploaded files.
  51. * In PUT/PATCH/DELETE requests this property will contain the form-urlencoded
  52. * data.
  53. *
  54. * @var object|array|null
  55. */
  56. protected $data = [];
  57. /**
  58. * Array of query string arguments
  59. *
  60. * @var array
  61. */
  62. protected $query = [];
  63. /**
  64. * Array of cookie data.
  65. *
  66. * @var array
  67. */
  68. protected $cookies = [];
  69. /**
  70. * Array of environment data.
  71. *
  72. * @var array
  73. */
  74. protected $_environment = [];
  75. /**
  76. * Base URL path.
  77. *
  78. * @var string
  79. */
  80. protected $base;
  81. /**
  82. * webroot path segment for the request.
  83. *
  84. * @var string
  85. */
  86. protected $webroot = '/';
  87. /**
  88. * Whether to trust HTTP_X headers set by most load balancers.
  89. * Only set to true if your application runs behind load balancers/proxies
  90. * that you control.
  91. *
  92. * @var bool
  93. */
  94. public $trustProxy = false;
  95. /**
  96. * Trusted proxies list
  97. *
  98. * @var array<string>
  99. */
  100. protected $trustedProxies = [];
  101. /**
  102. * The built in detectors used with `is()` can be modified with `addDetector()`.
  103. *
  104. * There are several ways to specify a detector, see \Cake\Http\ServerRequest::addDetector() for the
  105. * various formats and ways to define detectors.
  106. *
  107. * @var array<callable|array>
  108. */
  109. protected static $_detectors = [
  110. 'get' => ['env' => 'REQUEST_METHOD', 'value' => 'GET'],
  111. 'post' => ['env' => 'REQUEST_METHOD', 'value' => 'POST'],
  112. 'put' => ['env' => 'REQUEST_METHOD', 'value' => 'PUT'],
  113. 'patch' => ['env' => 'REQUEST_METHOD', 'value' => 'PATCH'],
  114. 'delete' => ['env' => 'REQUEST_METHOD', 'value' => 'DELETE'],
  115. 'head' => ['env' => 'REQUEST_METHOD', 'value' => 'HEAD'],
  116. 'options' => ['env' => 'REQUEST_METHOD', 'value' => 'OPTIONS'],
  117. 'ssl' => ['env' => 'HTTPS', 'options' => [1, 'on']],
  118. 'https' => ['env' => 'HTTPS', 'options' => [1, 'on']],
  119. 'ajax' => ['env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'],
  120. 'json' => ['accept' => ['application/json'], 'param' => '_ext', 'value' => 'json'],
  121. 'xml' => [
  122. 'accept' => ['application/xml', 'text/xml'],
  123. 'exclude' => ['text/html'],
  124. 'param' => '_ext',
  125. 'value' => 'xml',
  126. ],
  127. ];
  128. /**
  129. * Instance cache for results of is(something) calls
  130. *
  131. * @var array
  132. */
  133. protected $_detectorCache = [];
  134. /**
  135. * Request body stream. Contains php://input unless `input` constructor option is used.
  136. *
  137. * @var \Psr\Http\Message\StreamInterface
  138. */
  139. protected $stream;
  140. /**
  141. * Uri instance
  142. *
  143. * @var \Psr\Http\Message\UriInterface
  144. */
  145. protected $uri;
  146. /**
  147. * Instance of a Session object relative to this request
  148. *
  149. * @var \Cake\Http\Session
  150. */
  151. protected $session;
  152. /**
  153. * Instance of a FlashMessage object relative to this request
  154. *
  155. * @var \Cake\Http\FlashMessage
  156. */
  157. protected $flash;
  158. /**
  159. * Store the additional attributes attached to the request.
  160. *
  161. * @var array
  162. */
  163. protected $attributes = [];
  164. /**
  165. * A list of properties that emulated by the PSR7 attribute methods.
  166. *
  167. * @var array<string>
  168. */
  169. protected $emulatedAttributes = ['session', 'flash', 'webroot', 'base', 'params', 'here'];
  170. /**
  171. * Array of Psr\Http\Message\UploadedFileInterface objects.
  172. *
  173. * @var array
  174. */
  175. protected $uploadedFiles = [];
  176. /**
  177. * The HTTP protocol version used.
  178. *
  179. * @var string|null
  180. */
  181. protected $protocol;
  182. /**
  183. * The request target if overridden
  184. *
  185. * @var string|null
  186. */
  187. protected $requestTarget;
  188. /**
  189. * Create a new request object.
  190. *
  191. * You can supply the data as either an array or as a string. If you use
  192. * a string you can only supply the URL for the request. Using an array will
  193. * let you provide the following keys:
  194. *
  195. * - `post` POST data or non query string data
  196. * - `query` Additional data from the query string.
  197. * - `files` Uploaded files in a normalized structure, with each leaf an instance of UploadedFileInterface.
  198. * - `cookies` Cookies for this request.
  199. * - `environment` $_SERVER and $_ENV data.
  200. * - `url` The URL without the base path for the request.
  201. * - `uri` The PSR7 UriInterface object. If null, one will be created from `url` or `environment`.
  202. * - `base` The base URL for the request.
  203. * - `webroot` The webroot directory for the request.
  204. * - `input` The data that would come from php://input this is useful for simulating
  205. * requests with put, patch or delete data.
  206. * - `session` An instance of a Session object
  207. *
  208. * @param array<string, mixed> $config An array of request data to create a request with.
  209. */
  210. public function __construct(array $config = [])
  211. {
  212. $config += [
  213. 'params' => $this->params,
  214. 'query' => [],
  215. 'post' => [],
  216. 'files' => [],
  217. 'cookies' => [],
  218. 'environment' => [],
  219. 'url' => '',
  220. 'uri' => null,
  221. 'base' => '',
  222. 'webroot' => '',
  223. 'input' => null,
  224. ];
  225. $this->_setConfig($config);
  226. }
  227. /**
  228. * Process the config/settings data into properties.
  229. *
  230. * @param array<string, mixed> $config The config data to use.
  231. * @return void
  232. */
  233. protected function _setConfig(array $config): void
  234. {
  235. if (empty($config['session'])) {
  236. $config['session'] = new Session([
  237. 'cookiePath' => $config['base'],
  238. ]);
  239. }
  240. if (empty($config['environment']['REQUEST_METHOD'])) {
  241. $config['environment']['REQUEST_METHOD'] = 'GET';
  242. }
  243. $this->cookies = $config['cookies'];
  244. if (isset($config['uri'])) {
  245. if (!$config['uri'] instanceof UriInterface) {
  246. throw new CakeException('The `uri` key must be an instance of ' . UriInterface::class);
  247. }
  248. $uri = $config['uri'];
  249. } else {
  250. if ($config['url'] !== '') {
  251. $config = $this->processUrlOption($config);
  252. }
  253. $uri = ServerRequestFactory::createUri($config['environment']);
  254. }
  255. $this->_environment = $config['environment'];
  256. $this->uri = $uri;
  257. $this->base = $config['base'];
  258. $this->webroot = $config['webroot'];
  259. if (isset($config['input'])) {
  260. $stream = new Stream('php://memory', 'rw');
  261. $stream->write($config['input']);
  262. $stream->rewind();
  263. } else {
  264. $stream = new PhpInputStream();
  265. }
  266. $this->stream = $stream;
  267. $this->data = $config['post'];
  268. $this->uploadedFiles = $config['files'];
  269. $this->query = $config['query'];
  270. $this->params = $config['params'];
  271. $this->session = $config['session'];
  272. $this->flash = new FlashMessage($this->session);
  273. }
  274. /**
  275. * Set environment vars based on `url` option to facilitate UriInterface instance generation.
  276. *
  277. * `query` option is also updated based on URL's querystring.
  278. *
  279. * @param array<string, mixed> $config Config array.
  280. * @return array<string, mixed> Update config.
  281. */
  282. protected function processUrlOption(array $config): array
  283. {
  284. if ($config['url'][0] !== '/') {
  285. $config['url'] = '/' . $config['url'];
  286. }
  287. if (strpos($config['url'], '?') !== false) {
  288. [$config['url'], $config['environment']['QUERY_STRING']] = explode('?', $config['url']);
  289. parse_str($config['environment']['QUERY_STRING'], $queryArgs);
  290. $config['query'] += $queryArgs;
  291. }
  292. $config['environment']['REQUEST_URI'] = $config['url'];
  293. return $config;
  294. }
  295. /**
  296. * Get the content type used in this request.
  297. *
  298. * @return string|null
  299. */
  300. public function contentType(): ?string
  301. {
  302. $type = $this->getEnv('CONTENT_TYPE');
  303. if ($type) {
  304. return $type;
  305. }
  306. return $this->getEnv('HTTP_CONTENT_TYPE');
  307. }
  308. /**
  309. * Returns the instance of the Session object for this request
  310. *
  311. * @return \Cake\Http\Session
  312. */
  313. public function getSession(): Session
  314. {
  315. return $this->session;
  316. }
  317. /**
  318. * Returns the instance of the FlashMessage object for this request
  319. *
  320. * @return \Cake\Http\FlashMessage
  321. */
  322. public function getFlash(): FlashMessage
  323. {
  324. return $this->flash;
  325. }
  326. /**
  327. * Get the IP the client is using, or says they are using.
  328. *
  329. * @return string The client IP.
  330. */
  331. public function clientIp(): string
  332. {
  333. if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_FOR')) {
  334. $addresses = array_map('trim', explode(',', (string)$this->getEnv('HTTP_X_FORWARDED_FOR')));
  335. $trusted = (count($this->trustedProxies) > 0);
  336. $n = count($addresses);
  337. if ($trusted) {
  338. $trusted = array_diff($addresses, $this->trustedProxies);
  339. $trusted = (count($trusted) === 1);
  340. }
  341. if ($trusted) {
  342. return $addresses[0];
  343. }
  344. return $addresses[$n - 1];
  345. }
  346. if ($this->trustProxy && $this->getEnv('HTTP_X_REAL_IP')) {
  347. $ipaddr = $this->getEnv('HTTP_X_REAL_IP');
  348. } elseif ($this->trustProxy && $this->getEnv('HTTP_CLIENT_IP')) {
  349. $ipaddr = $this->getEnv('HTTP_CLIENT_IP');
  350. } else {
  351. $ipaddr = $this->getEnv('REMOTE_ADDR');
  352. }
  353. return trim((string)$ipaddr);
  354. }
  355. /**
  356. * register trusted proxies
  357. *
  358. * @param array<string> $proxies ips list of trusted proxies
  359. * @return void
  360. */
  361. public function setTrustedProxies(array $proxies): void
  362. {
  363. $this->trustedProxies = $proxies;
  364. $this->trustProxy = true;
  365. }
  366. /**
  367. * Get trusted proxies
  368. *
  369. * @return array<string>
  370. */
  371. public function getTrustedProxies(): array
  372. {
  373. return $this->trustedProxies;
  374. }
  375. /**
  376. * Returns the referer that referred this request.
  377. *
  378. * @param bool $local Attempt to return a local address.
  379. * Local addresses do not contain hostnames.
  380. * @return string|null The referring address for this request or null.
  381. */
  382. public function referer(bool $local = true): ?string
  383. {
  384. $ref = $this->getEnv('HTTP_REFERER');
  385. $base = Configure::read('App.fullBaseUrl') . $this->webroot;
  386. if (!empty($ref) && !empty($base)) {
  387. if ($local && strpos($ref, $base) === 0) {
  388. $ref = substr($ref, strlen($base));
  389. if ($ref === '' || strpos($ref, '//') === 0) {
  390. $ref = '/';
  391. }
  392. if ($ref[0] !== '/') {
  393. $ref = '/' . $ref;
  394. }
  395. return $ref;
  396. }
  397. if (!$local) {
  398. return $ref;
  399. }
  400. }
  401. return null;
  402. }
  403. /**
  404. * Missing method handler, handles wrapping older style isAjax() type methods
  405. *
  406. * @param string $name The method called
  407. * @param array $params Array of parameters for the method call
  408. * @return bool
  409. * @throws \BadMethodCallException when an invalid method is called.
  410. */
  411. public function __call(string $name, array $params)
  412. {
  413. if (strpos($name, 'is') === 0) {
  414. $type = strtolower(substr($name, 2));
  415. array_unshift($params, $type);
  416. return $this->is(...$params);
  417. }
  418. throw new BadMethodCallException(sprintf('Method "%s()" does not exist', $name));
  419. }
  420. /**
  421. * Check whether a Request is a certain type.
  422. *
  423. * Uses the built-in detection rules as well as additional rules
  424. * defined with {@link \Cake\Http\ServerRequest::addDetector()}. Any detector can be called
  425. * as `is($type)` or `is$Type()`.
  426. *
  427. * @param array<string>|string $type The type of request you want to check. If an array
  428. * this method will return true if the request matches any type.
  429. * @param mixed ...$args List of arguments
  430. * @return bool Whether the request is the type you are checking.
  431. * @throws \InvalidArgumentException If no detector has been set for the provided type.
  432. */
  433. public function is($type, ...$args): bool
  434. {
  435. if (is_array($type)) {
  436. foreach ($type as $_type) {
  437. if ($this->is($_type)) {
  438. return true;
  439. }
  440. }
  441. return false;
  442. }
  443. $type = strtolower($type);
  444. if (!isset(static::$_detectors[$type])) {
  445. throw new InvalidArgumentException("No detector set for type `{$type}`");
  446. }
  447. if ($args) {
  448. return $this->_is($type, $args);
  449. }
  450. return $this->_detectorCache[$type] = $this->_detectorCache[$type] ?? $this->_is($type, $args);
  451. }
  452. /**
  453. * Clears the instance detector cache, used by the is() function
  454. *
  455. * @return void
  456. */
  457. public function clearDetectorCache(): void
  458. {
  459. $this->_detectorCache = [];
  460. }
  461. /**
  462. * Worker for the public is() function
  463. *
  464. * @param string $type The type of request you want to check.
  465. * @param array $args Array of custom detector arguments.
  466. * @return bool Whether the request is the type you are checking.
  467. */
  468. protected function _is(string $type, array $args): bool
  469. {
  470. $detect = static::$_detectors[$type];
  471. if (is_callable($detect)) {
  472. array_unshift($args, $this);
  473. return $detect(...$args);
  474. }
  475. if (isset($detect['env']) && $this->_environmentDetector($detect)) {
  476. return true;
  477. }
  478. if (isset($detect['header']) && $this->_headerDetector($detect)) {
  479. return true;
  480. }
  481. if (isset($detect['accept']) && $this->_acceptHeaderDetector($detect)) {
  482. return true;
  483. }
  484. if (isset($detect['param']) && $this->_paramDetector($detect)) {
  485. return true;
  486. }
  487. return false;
  488. }
  489. /**
  490. * Detects if a specific accept header is present.
  491. *
  492. * @param array $detect Detector options array.
  493. * @return bool Whether the request is the type you are checking.
  494. */
  495. protected function _acceptHeaderDetector(array $detect): bool
  496. {
  497. $content = new ContentTypeNegotiation();
  498. $options = $detect['accept'];
  499. // Some detectors overlap with the default browser Accept header
  500. // For these types we use an exclude list to refine our content type
  501. // detection.
  502. $exclude = $detect['exclude'] ?? null;
  503. if ($exclude) {
  504. $options = array_merge($options, $exclude);
  505. }
  506. $accepted = $content->preferredType($this, $options);
  507. if ($accepted === null) {
  508. return false;
  509. }
  510. if ($exclude && in_array($accepted, $exclude, true)) {
  511. return false;
  512. }
  513. return true;
  514. }
  515. /**
  516. * Detects if a specific header is present.
  517. *
  518. * @param array $detect Detector options array.
  519. * @return bool Whether the request is the type you are checking.
  520. */
  521. protected function _headerDetector(array $detect): bool
  522. {
  523. foreach ($detect['header'] as $header => $value) {
  524. $header = $this->getEnv('http_' . $header);
  525. if ($header !== null) {
  526. if (!is_string($value) && !is_bool($value) && is_callable($value)) {
  527. return $value($header);
  528. }
  529. return $header === $value;
  530. }
  531. }
  532. return false;
  533. }
  534. /**
  535. * Detects if a specific request parameter is present.
  536. *
  537. * @param array $detect Detector options array.
  538. * @return bool Whether the request is the type you are checking.
  539. */
  540. protected function _paramDetector(array $detect): bool
  541. {
  542. $key = $detect['param'];
  543. if (isset($detect['value'])) {
  544. $value = $detect['value'];
  545. return isset($this->params[$key]) ? $this->params[$key] == $value : false;
  546. }
  547. if (isset($detect['options'])) {
  548. return isset($this->params[$key]) ? in_array($this->params[$key], $detect['options']) : false;
  549. }
  550. return false;
  551. }
  552. /**
  553. * Detects if a specific environment variable is present.
  554. *
  555. * @param array $detect Detector options array.
  556. * @return bool Whether the request is the type you are checking.
  557. */
  558. protected function _environmentDetector(array $detect): bool
  559. {
  560. if (isset($detect['env'])) {
  561. if (isset($detect['value'])) {
  562. return $this->getEnv($detect['env']) == $detect['value'];
  563. }
  564. if (isset($detect['pattern'])) {
  565. return (bool)preg_match($detect['pattern'], (string)$this->getEnv($detect['env']));
  566. }
  567. if (isset($detect['options'])) {
  568. $pattern = '/' . implode('|', $detect['options']) . '/i';
  569. return (bool)preg_match($pattern, (string)$this->getEnv($detect['env']));
  570. }
  571. }
  572. return false;
  573. }
  574. /**
  575. * Check that a request matches all the given types.
  576. *
  577. * Allows you to test multiple types and union the results.
  578. * See Request::is() for how to add additional types and the
  579. * built-in types.
  580. *
  581. * @param array<string> $types The types to check.
  582. * @return bool Success.
  583. * @see \Cake\Http\ServerRequest::is()
  584. */
  585. public function isAll(array $types): bool
  586. {
  587. foreach ($types as $type) {
  588. if (!$this->is($type)) {
  589. return false;
  590. }
  591. }
  592. return true;
  593. }
  594. /**
  595. * Add a new detector to the list of detectors that a request can use.
  596. * There are several different types of detectors that can be set.
  597. *
  598. * ### Callback comparison
  599. *
  600. * Callback detectors allow you to provide a callable to handle the check.
  601. * The callback will receive the request object as its only parameter.
  602. *
  603. * ```
  604. * addDetector('custom', function ($request) { //Return a boolean });
  605. * ```
  606. *
  607. * ### Environment value comparison
  608. *
  609. * An environment value comparison, compares a value fetched from `env()` to a known value
  610. * the environment value is equality checked against the provided value.
  611. *
  612. * ```
  613. * addDetector('post', ['env' => 'REQUEST_METHOD', 'value' => 'POST']);
  614. * ```
  615. *
  616. * ### Request parameter comparison
  617. *
  618. * Allows for custom detectors on the request parameters.
  619. *
  620. * ```
  621. * addDetector('admin', ['param' => 'prefix', 'value' => 'admin']);
  622. * ```
  623. *
  624. * ### Accept comparison
  625. *
  626. * Allows for detector to compare against Accept header value.
  627. *
  628. * ```
  629. * addDetector('csv', ['accept' => 'text/csv']);
  630. * ```
  631. *
  632. * ### Header comparison
  633. *
  634. * Allows for one or more headers to be compared.
  635. *
  636. * ```
  637. * addDetector('fancy', ['header' => ['X-Fancy' => 1]);
  638. * ```
  639. *
  640. * The `param`, `env` and comparison types allow the following
  641. * value comparison options:
  642. *
  643. * ### Pattern value comparison
  644. *
  645. * Pattern value comparison allows you to compare a value fetched from `env()` to a regular expression.
  646. *
  647. * ```
  648. * addDetector('iphone', ['env' => 'HTTP_USER_AGENT', 'pattern' => '/iPhone/i']);
  649. * ```
  650. *
  651. * ### Option based comparison
  652. *
  653. * Option based comparisons use a list of options to create a regular expression. Subsequent calls
  654. * to add an already defined options detector will merge the options.
  655. *
  656. * ```
  657. * addDetector('mobile', ['env' => 'HTTP_USER_AGENT', 'options' => ['Fennec']]);
  658. * ```
  659. *
  660. * You can also make compare against multiple values
  661. * using the `options` key. This is useful when you want to check
  662. * if a request value is in a list of options.
  663. *
  664. * `addDetector('extension', ['param' => '_ext', 'options' => ['pdf', 'csv']]`
  665. *
  666. * @param string $name The name of the detector.
  667. * @param callable|array $detector A callable or options array for the detector definition.
  668. * @return void
  669. */
  670. public static function addDetector(string $name, $detector): void
  671. {
  672. $name = strtolower($name);
  673. if (is_callable($detector)) {
  674. static::$_detectors[$name] = $detector;
  675. return;
  676. }
  677. if (isset(static::$_detectors[$name], $detector['options'])) {
  678. /** @psalm-suppress PossiblyInvalidArgument */
  679. $detector = Hash::merge(static::$_detectors[$name], $detector);
  680. }
  681. static::$_detectors[$name] = $detector;
  682. }
  683. /**
  684. * Normalize a header name into the SERVER version.
  685. *
  686. * @param string $name The header name.
  687. * @return string The normalized header name.
  688. */
  689. protected function normalizeHeaderName(string $name): string
  690. {
  691. $name = str_replace('-', '_', strtoupper($name));
  692. if (!in_array($name, ['CONTENT_LENGTH', 'CONTENT_TYPE'], true)) {
  693. $name = 'HTTP_' . $name;
  694. }
  695. return $name;
  696. }
  697. /**
  698. * Get all headers in the request.
  699. *
  700. * Returns an associative array where the header names are
  701. * the keys and the values are a list of header values.
  702. *
  703. * While header names are not case-sensitive, getHeaders() will normalize
  704. * the headers.
  705. *
  706. * @return array<string[]> An associative array of headers and their values.
  707. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  708. */
  709. public function getHeaders(): array
  710. {
  711. $headers = [];
  712. foreach ($this->_environment as $key => $value) {
  713. $name = null;
  714. if (strpos($key, 'HTTP_') === 0) {
  715. $name = substr($key, 5);
  716. }
  717. if (strpos($key, 'CONTENT_') === 0) {
  718. $name = $key;
  719. }
  720. if ($name !== null) {
  721. $name = str_replace('_', ' ', strtolower($name));
  722. $name = str_replace(' ', '-', ucwords($name));
  723. $headers[$name] = (array)$value;
  724. }
  725. }
  726. return $headers;
  727. }
  728. /**
  729. * Check if a header is set in the request.
  730. *
  731. * @param string $name The header you want to get (case-insensitive)
  732. * @return bool Whether the header is defined.
  733. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  734. */
  735. public function hasHeader($name): bool
  736. {
  737. $name = $this->normalizeHeaderName($name);
  738. return isset($this->_environment[$name]);
  739. }
  740. /**
  741. * Get a single header from the request.
  742. *
  743. * Return the header value as an array. If the header
  744. * is not present an empty array will be returned.
  745. *
  746. * @param string $name The header you want to get (case-insensitive)
  747. * @return array<string> An associative array of headers and their values.
  748. * If the header doesn't exist, an empty array will be returned.
  749. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  750. */
  751. public function getHeader($name): array
  752. {
  753. $name = $this->normalizeHeaderName($name);
  754. if (isset($this->_environment[$name])) {
  755. return (array)$this->_environment[$name];
  756. }
  757. return [];
  758. }
  759. /**
  760. * Get a single header as a string from the request.
  761. *
  762. * @param string $name The header you want to get (case-insensitive)
  763. * @return string Header values collapsed into a comma separated string.
  764. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  765. */
  766. public function getHeaderLine($name): string
  767. {
  768. $value = $this->getHeader($name);
  769. return implode(', ', $value);
  770. }
  771. /**
  772. * Get a modified request with the provided header.
  773. *
  774. * @param string $name The header name.
  775. * @param array|string $value The header value
  776. * @return static
  777. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  778. */
  779. public function withHeader($name, $value)
  780. {
  781. $new = clone $this;
  782. $name = $this->normalizeHeaderName($name);
  783. $new->_environment[$name] = $value;
  784. return $new;
  785. }
  786. /**
  787. * Get a modified request with the provided header.
  788. *
  789. * Existing header values will be retained. The provided value
  790. * will be appended into the existing values.
  791. *
  792. * @param string $name The header name.
  793. * @param array|string $value The header value
  794. * @return static
  795. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  796. */
  797. public function withAddedHeader($name, $value)
  798. {
  799. $new = clone $this;
  800. $name = $this->normalizeHeaderName($name);
  801. $existing = [];
  802. if (isset($new->_environment[$name])) {
  803. $existing = (array)$new->_environment[$name];
  804. }
  805. $existing = array_merge($existing, (array)$value);
  806. $new->_environment[$name] = $existing;
  807. return $new;
  808. }
  809. /**
  810. * Get a modified request without a provided header.
  811. *
  812. * @param string $name The header name to remove.
  813. * @return static
  814. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  815. */
  816. public function withoutHeader($name)
  817. {
  818. $new = clone $this;
  819. $name = $this->normalizeHeaderName($name);
  820. unset($new->_environment[$name]);
  821. return $new;
  822. }
  823. /**
  824. * Get the HTTP method used for this request.
  825. * There are a few ways to specify a method.
  826. *
  827. * - If your client supports it you can use native HTTP methods.
  828. * - You can set the HTTP-X-Method-Override header.
  829. * - You can submit an input with the name `_method`
  830. *
  831. * Any of these 3 approaches can be used to set the HTTP method used
  832. * by CakePHP internally, and will effect the result of this method.
  833. *
  834. * @return string The name of the HTTP method used.
  835. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  836. */
  837. public function getMethod(): string
  838. {
  839. return (string)$this->getEnv('REQUEST_METHOD');
  840. }
  841. /**
  842. * Update the request method and get a new instance.
  843. *
  844. * @param string $method The HTTP method to use.
  845. * @return static A new instance with the updated method.
  846. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  847. */
  848. public function withMethod($method)
  849. {
  850. $new = clone $this;
  851. if (
  852. !is_string($method) ||
  853. !preg_match('/^[!#$%&\'*+.^_`\|~0-9a-z-]+$/i', $method)
  854. ) {
  855. throw new InvalidArgumentException(sprintf(
  856. 'Unsupported HTTP method "%s" provided',
  857. $method
  858. ));
  859. }
  860. $new->_environment['REQUEST_METHOD'] = $method;
  861. return $new;
  862. }
  863. /**
  864. * Get all the server environment parameters.
  865. *
  866. * Read all of the 'environment' or 'server' data that was
  867. * used to create this request.
  868. *
  869. * @return array
  870. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  871. */
  872. public function getServerParams(): array
  873. {
  874. return $this->_environment;
  875. }
  876. /**
  877. * Get all the query parameters in accordance to the PSR-7 specifications. To read specific query values
  878. * use the alternative getQuery() method.
  879. *
  880. * @return array
  881. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  882. */
  883. public function getQueryParams(): array
  884. {
  885. return $this->query;
  886. }
  887. /**
  888. * Update the query string data and get a new instance.
  889. *
  890. * @param array $query The query string data to use
  891. * @return static A new instance with the updated query string data.
  892. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  893. */
  894. public function withQueryParams(array $query)
  895. {
  896. $new = clone $this;
  897. $new->query = $query;
  898. return $new;
  899. }
  900. /**
  901. * Get the host that the request was handled on.
  902. *
  903. * @return string|null
  904. */
  905. public function host(): ?string
  906. {
  907. if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_HOST')) {
  908. return $this->getEnv('HTTP_X_FORWARDED_HOST');
  909. }
  910. return $this->getEnv('HTTP_HOST');
  911. }
  912. /**
  913. * Get the port the request was handled on.
  914. *
  915. * @return string|null
  916. */
  917. public function port(): ?string
  918. {
  919. if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_PORT')) {
  920. return $this->getEnv('HTTP_X_FORWARDED_PORT');
  921. }
  922. return $this->getEnv('SERVER_PORT');
  923. }
  924. /**
  925. * Get the current url scheme used for the request.
  926. *
  927. * e.g. 'http', or 'https'
  928. *
  929. * @return string|null The scheme used for the request.
  930. */
  931. public function scheme(): ?string
  932. {
  933. if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_PROTO')) {
  934. return $this->getEnv('HTTP_X_FORWARDED_PROTO');
  935. }
  936. return $this->getEnv('HTTPS') ? 'https' : 'http';
  937. }
  938. /**
  939. * Get the domain name and include $tldLength segments of the tld.
  940. *
  941. * @param int $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld.
  942. * While `example.co.uk` contains 2.
  943. * @return string Domain name without subdomains.
  944. */
  945. public function domain(int $tldLength = 1): string
  946. {
  947. $host = $this->host();
  948. if (empty($host)) {
  949. return '';
  950. }
  951. $segments = explode('.', $host);
  952. $domain = array_slice($segments, -1 * ($tldLength + 1));
  953. return implode('.', $domain);
  954. }
  955. /**
  956. * Get the subdomains for a host.
  957. *
  958. * @param int $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld.
  959. * While `example.co.uk` contains 2.
  960. * @return array<string> An array of subdomains.
  961. */
  962. public function subdomains(int $tldLength = 1): array
  963. {
  964. $host = $this->host();
  965. if (empty($host)) {
  966. return [];
  967. }
  968. $segments = explode('.', $host);
  969. return array_slice($segments, 0, -1 * ($tldLength + 1));
  970. }
  971. /**
  972. * Find out which content types the client accepts or check if they accept a
  973. * particular type of content.
  974. *
  975. * #### Get all types:
  976. *
  977. * ```
  978. * $this->request->accepts();
  979. * ```
  980. *
  981. * #### Check for a single type:
  982. *
  983. * ```
  984. * $this->request->accepts('application/json');
  985. * ```
  986. *
  987. * This method will order the returned content types by the preference values indicated
  988. * by the client.
  989. *
  990. * @param string|null $type The content type to check for. Leave null to get all types a client accepts.
  991. * @return array<string>|bool Either an array of all the types the client accepts or a boolean if they accept the
  992. * provided type.
  993. */
  994. public function accepts(?string $type = null)
  995. {
  996. $content = new ContentTypeNegotiation();
  997. if ($type) {
  998. return $content->preferredType($this, [$type]) !== null;
  999. }
  1000. $accept = [];
  1001. foreach ($content->parseAccept($this) as $types) {
  1002. $accept = array_merge($accept, $types);
  1003. }
  1004. return $accept;
  1005. }
  1006. /**
  1007. * Parse the HTTP_ACCEPT header and return a sorted array with content types
  1008. * as the keys, and pref values as the values.
  1009. *
  1010. * Generally you want to use {@link \Cake\Http\ServerRequest::accepts()} to get a simple list
  1011. * of the accepted content types.
  1012. *
  1013. * @return array An array of `prefValue => [content/types]`
  1014. * @deprecated 4.4.0 Use `accepts()` or `ContentTypeNegotiation` class instead.
  1015. */
  1016. public function parseAccept(): array
  1017. {
  1018. return (new ContentTypeNegotiation())->parseAccept($this);
  1019. }
  1020. /**
  1021. * Get the languages accepted by the client, or check if a specific language is accepted.
  1022. *
  1023. * Get the list of accepted languages:
  1024. *
  1025. * ```$request->acceptLanguage();```
  1026. *
  1027. * Check if a specific language is accepted:
  1028. *
  1029. * ```$request->acceptLanguage('es-es');```
  1030. *
  1031. * @param string|null $language The language to test.
  1032. * @return array|bool If a $language is provided, a boolean. Otherwise the array of accepted languages.
  1033. */
  1034. public function acceptLanguage(?string $language = null)
  1035. {
  1036. $content = new ContentTypeNegotiation();
  1037. if ($language !== null) {
  1038. return $content->acceptLanguage($this, $language);
  1039. }
  1040. return $content->acceptedLanguages($this);
  1041. }
  1042. /**
  1043. * Read a specific query value or dotted path.
  1044. *
  1045. * Developers are encouraged to use getQueryParams() if they need the whole query array,
  1046. * as it is PSR-7 compliant, and this method is not. Using Hash::get() you can also get single params.
  1047. *
  1048. * ### PSR-7 Alternative
  1049. *
  1050. * ```
  1051. * $value = Hash::get($request->getQueryParams(), 'Post.id');
  1052. * ```
  1053. *
  1054. * @param string|null $name The name or dotted path to the query param or null to read all.
  1055. * @param mixed $default The default value if the named parameter is not set, and $name is not null.
  1056. * @return array|string|null Query data.
  1057. * @see ServerRequest::getQueryParams()
  1058. */
  1059. public function getQuery(?string $name = null, $default = null)
  1060. {
  1061. if ($name === null) {
  1062. return $this->query;
  1063. }
  1064. return Hash::get($this->query, $name, $default);
  1065. }
  1066. /**
  1067. * Provides a safe accessor for request data. Allows
  1068. * you to use Hash::get() compatible paths.
  1069. *
  1070. * ### Reading values.
  1071. *
  1072. * ```
  1073. * // get all data
  1074. * $request->getData();
  1075. *
  1076. * // Read a specific field.
  1077. * $request->getData('Post.title');
  1078. *
  1079. * // With a default value.
  1080. * $request->getData('Post.not there', 'default value');
  1081. * ```
  1082. *
  1083. * When reading values you will get `null` for keys/values that do not exist.
  1084. *
  1085. * Developers are encouraged to use getParsedBody() if they need the whole data array,
  1086. * as it is PSR-7 compliant, and this method is not. Using Hash::get() you can also get single params.
  1087. *
  1088. * ### PSR-7 Alternative
  1089. *
  1090. * ```
  1091. * $value = Hash::get($request->getParsedBody(), 'Post.id');
  1092. * ```
  1093. *
  1094. * @param string|null $name Dot separated name of the value to read. Or null to read all data.
  1095. * @param mixed $default The default data.
  1096. * @return mixed The value being read.
  1097. */
  1098. public function getData(?string $name = null, $default = null)
  1099. {
  1100. if ($name === null) {
  1101. return $this->data;
  1102. }
  1103. if (!is_array($this->data) && $name) {
  1104. return $default;
  1105. }
  1106. /** @psalm-suppress PossiblyNullArgument */
  1107. return Hash::get($this->data, $name, $default);
  1108. }
  1109. /**
  1110. * Read data from `php://input`. Useful when interacting with XML or JSON
  1111. * request body content.
  1112. *
  1113. * Getting input with a decoding function:
  1114. *
  1115. * ```
  1116. * $this->request->input('json_decode');
  1117. * ```
  1118. *
  1119. * Getting input using a decoding function, and additional params:
  1120. *
  1121. * ```
  1122. * $this->request->input('Xml::build', ['return' => 'DOMDocument']);
  1123. * ```
  1124. *
  1125. * Any additional parameters are applied to the callback in the order they are given.
  1126. *
  1127. * @deprecated 4.1.0 Use `(string)$request->getBody()` to get the raw PHP input
  1128. * as string; use `BodyParserMiddleware` to parse the request body so that it's
  1129. * available as array/object through `$request->getParsedBody()`.
  1130. * @param callable|null $callback A decoding callback that will convert the string data to another
  1131. * representation. Leave empty to access the raw input data. You can also
  1132. * supply additional parameters for the decoding callback using var args, see above.
  1133. * @param mixed ...$args The additional arguments
  1134. * @return mixed The decoded/processed request data.
  1135. */
  1136. public function input(?callable $callback = null, ...$args)
  1137. {
  1138. deprecationWarning(
  1139. 'Use `(string)$request->getBody()` to get the raw PHP input as string; '
  1140. . 'use `BodyParserMiddleware` to parse the request body so that it\'s available as array/object '
  1141. . 'through $request->getParsedBody()'
  1142. );
  1143. $this->stream->rewind();
  1144. $input = $this->stream->getContents();
  1145. if ($callback) {
  1146. array_unshift($args, $input);
  1147. return $callback(...$args);
  1148. }
  1149. return $input;
  1150. }
  1151. /**
  1152. * Read cookie data from the request's cookie data.
  1153. *
  1154. * @param string $key The key or dotted path you want to read.
  1155. * @param array|string|null $default The default value if the cookie is not set.
  1156. * @return array|string|null Either the cookie value, or null if the value doesn't exist.
  1157. */
  1158. public function getCookie(string $key, $default = null)
  1159. {
  1160. return Hash::get($this->cookies, $key, $default);
  1161. }
  1162. /**
  1163. * Get a cookie collection based on the request's cookies
  1164. *
  1165. * The CookieCollection lets you interact with request cookies using
  1166. * `\Cake\Http\Cookie\Cookie` objects and can make converting request cookies
  1167. * into response cookies easier.
  1168. *
  1169. * This method will create a new cookie collection each time it is called.
  1170. * This is an optimization that allows fewer objects to be allocated until
  1171. * the more complex CookieCollection is needed. In general you should prefer
  1172. * `getCookie()` and `getCookieParams()` over this method. Using a CookieCollection
  1173. * is ideal if your cookies contain complex JSON encoded data.
  1174. *
  1175. * @return \Cake\Http\Cookie\CookieCollection
  1176. */
  1177. public function getCookieCollection(): CookieCollection
  1178. {
  1179. return CookieCollection::createFromServerRequest($this);
  1180. }
  1181. /**
  1182. * Replace the cookies in the request with those contained in
  1183. * the provided CookieCollection.
  1184. *
  1185. * @param \Cake\Http\Cookie\CookieCollection $cookies The cookie collection
  1186. * @return static
  1187. */
  1188. public function withCookieCollection(CookieCollection $cookies)
  1189. {
  1190. $new = clone $this;
  1191. $values = [];
  1192. foreach ($cookies as $cookie) {
  1193. $values[$cookie->getName()] = $cookie->getValue();
  1194. }
  1195. $new->cookies = $values;
  1196. return $new;
  1197. }
  1198. /**
  1199. * Get all the cookie data from the request.
  1200. *
  1201. * @return array An array of cookie data.
  1202. */
  1203. public function getCookieParams(): array
  1204. {
  1205. return $this->cookies;
  1206. }
  1207. /**
  1208. * Replace the cookies and get a new request instance.
  1209. *
  1210. * @param array $cookies The new cookie data to use.
  1211. * @return static
  1212. */
  1213. public function withCookieParams(array $cookies)
  1214. {
  1215. $new = clone $this;
  1216. $new->cookies = $cookies;
  1217. return $new;
  1218. }
  1219. /**
  1220. * Get the parsed request body data.
  1221. *
  1222. * If the request Content-Type is either application/x-www-form-urlencoded
  1223. * or multipart/form-data, and the request method is POST, this will be the
  1224. * post data. For other content types, it may be the deserialized request
  1225. * body.
  1226. *
  1227. * @return object|array|null The deserialized body parameters, if any.
  1228. * These will typically be an array.
  1229. */
  1230. public function getParsedBody()
  1231. {
  1232. return $this->data;
  1233. }
  1234. /**
  1235. * Update the parsed body and get a new instance.
  1236. *
  1237. * @param object|array|null $data The deserialized body data. This will
  1238. * typically be in an array or object.
  1239. * @return static
  1240. */
  1241. public function withParsedBody($data)
  1242. {
  1243. $new = clone $this;
  1244. $new->data = $data;
  1245. return $new;
  1246. }
  1247. /**
  1248. * Retrieves the HTTP protocol version as a string.
  1249. *
  1250. * @return string HTTP protocol version.
  1251. */
  1252. public function getProtocolVersion(): string
  1253. {
  1254. if ($this->protocol) {
  1255. return $this->protocol;
  1256. }
  1257. // Lazily populate this data as it is generally not used.
  1258. preg_match('/^HTTP\/([\d.]+)$/', (string)$this->getEnv('SERVER_PROTOCOL'), $match);
  1259. $protocol = '1.1';
  1260. if (isset($match[1])) {
  1261. $protocol = $match[1];
  1262. }
  1263. $this->protocol = $protocol;
  1264. return $this->protocol;
  1265. }
  1266. /**
  1267. * Return an instance with the specified HTTP protocol version.
  1268. *
  1269. * The version string MUST contain only the HTTP version number (e.g.,
  1270. * "1.1", "1.0").
  1271. *
  1272. * @param string $version HTTP protocol version
  1273. * @return static
  1274. */
  1275. public function withProtocolVersion($version)
  1276. {
  1277. if (!preg_match('/^(1\.[01]|2)$/', $version)) {
  1278. throw new InvalidArgumentException("Unsupported protocol version '{$version}' provided");
  1279. }
  1280. $new = clone $this;
  1281. $new->protocol = $version;
  1282. return $new;
  1283. }
  1284. /**
  1285. * Get a value from the request's environment data.
  1286. * Fallback to using env() if the key is not set in the $environment property.
  1287. *
  1288. * @param string $key The key you want to read from.
  1289. * @param string|null $default Default value when trying to retrieve an environment
  1290. * variable's value that does not exist.
  1291. * @return string|null Either the environment value, or null if the value doesn't exist.
  1292. */
  1293. public function getEnv(string $key, ?string $default = null): ?string
  1294. {
  1295. $key = strtoupper($key);
  1296. if (!array_key_exists($key, $this->_environment)) {
  1297. $this->_environment[$key] = env($key);
  1298. }
  1299. return $this->_environment[$key] !== null ? (string)$this->_environment[$key] : $default;
  1300. }
  1301. /**
  1302. * Update the request with a new environment data element.
  1303. *
  1304. * Returns an updated request object. This method returns
  1305. * a *new* request object and does not mutate the request in-place.
  1306. *
  1307. * @param string $key The key you want to write to.
  1308. * @param string $value Value to set
  1309. * @return static
  1310. */
  1311. public function withEnv(string $key, string $value)
  1312. {
  1313. $new = clone $this;
  1314. $new->_environment[$key] = $value;
  1315. $new->clearDetectorCache();
  1316. return $new;
  1317. }
  1318. /**
  1319. * Allow only certain HTTP request methods, if the request method does not match
  1320. * a 405 error will be shown and the required "Allow" response header will be set.
  1321. *
  1322. * Example:
  1323. *
  1324. * $this->request->allowMethod('post');
  1325. * or
  1326. * $this->request->allowMethod(['post', 'delete']);
  1327. *
  1328. * If the request would be GET, response header "Allow: POST, DELETE" will be set
  1329. * and a 405 error will be returned.
  1330. *
  1331. * @param array<string>|string $methods Allowed HTTP request methods.
  1332. * @return true
  1333. * @throws \Cake\Http\Exception\MethodNotAllowedException
  1334. */
  1335. public function allowMethod($methods): bool
  1336. {
  1337. $methods = (array)$methods;
  1338. foreach ($methods as $method) {
  1339. if ($this->is($method)) {
  1340. return true;
  1341. }
  1342. }
  1343. $allowed = strtoupper(implode(', ', $methods));
  1344. $e = new MethodNotAllowedException();
  1345. $e->setHeader('Allow', $allowed);
  1346. throw $e;
  1347. }
  1348. /**
  1349. * Update the request with a new request data element.
  1350. *
  1351. * Returns an updated request object. This method returns
  1352. * a *new* request object and does not mutate the request in-place.
  1353. *
  1354. * Use `withParsedBody()` if you need to replace the all request data.
  1355. *
  1356. * @param string $name The dot separated path to insert $value at.
  1357. * @param mixed $value The value to insert into the request data.
  1358. * @return static
  1359. */
  1360. public function withData(string $name, $value)
  1361. {
  1362. $copy = clone $this;
  1363. if (is_array($copy->data)) {
  1364. $copy->data = Hash::insert($copy->data, $name, $value);
  1365. }
  1366. return $copy;
  1367. }
  1368. /**
  1369. * Update the request removing a data element.
  1370. *
  1371. * Returns an updated request object. This method returns
  1372. * a *new* request object and does not mutate the request in-place.
  1373. *
  1374. * @param string $name The dot separated path to remove.
  1375. * @return static
  1376. */
  1377. public function withoutData(string $name)
  1378. {
  1379. $copy = clone $this;
  1380. if (is_array($copy->data)) {
  1381. $copy->data = Hash::remove($copy->data, $name);
  1382. }
  1383. return $copy;
  1384. }
  1385. /**
  1386. * Update the request with a new routing parameter
  1387. *
  1388. * Returns an updated request object. This method returns
  1389. * a *new* request object and does not mutate the request in-place.
  1390. *
  1391. * @param string $name The dot separated path to insert $value at.
  1392. * @param mixed $value The value to insert into the the request parameters.
  1393. * @return static
  1394. */
  1395. public function withParam(string $name, $value)
  1396. {
  1397. $copy = clone $this;
  1398. $copy->params = Hash::insert($copy->params, $name, $value);
  1399. return $copy;
  1400. }
  1401. /**
  1402. * Safely access the values in $this->params.
  1403. *
  1404. * @param string $name The name or dotted path to parameter.
  1405. * @param mixed $default The default value if `$name` is not set. Default `null`.
  1406. * @return mixed
  1407. */
  1408. public function getParam(string $name, $default = null)
  1409. {
  1410. return Hash::get($this->params, $name, $default);
  1411. }
  1412. /**
  1413. * Return an instance with the specified request attribute.
  1414. *
  1415. * @param string $name The attribute name.
  1416. * @param mixed $value The value of the attribute.
  1417. * @return static
  1418. */
  1419. public function withAttribute($name, $value)
  1420. {
  1421. $new = clone $this;
  1422. if (in_array($name, $this->emulatedAttributes, true)) {
  1423. $new->{$name} = $value;
  1424. } else {
  1425. $new->attributes[$name] = $value;
  1426. }
  1427. return $new;
  1428. }
  1429. /**
  1430. * Return an instance without the specified request attribute.
  1431. *
  1432. * @param string $name The attribute name.
  1433. * @return static
  1434. * @throws \InvalidArgumentException
  1435. */
  1436. public function withoutAttribute($name)
  1437. {
  1438. $new = clone $this;
  1439. if (in_array($name, $this->emulatedAttributes, true)) {
  1440. throw new InvalidArgumentException(
  1441. "You cannot unset '$name'. It is a required CakePHP attribute."
  1442. );
  1443. }
  1444. unset($new->attributes[$name]);
  1445. return $new;
  1446. }
  1447. /**
  1448. * Read an attribute from the request, or get the default
  1449. *
  1450. * @param string $name The attribute name.
  1451. * @param mixed|null $default The default value if the attribute has not been set.
  1452. * @return mixed
  1453. */
  1454. public function getAttribute($name, $default = null)
  1455. {
  1456. if (in_array($name, $this->emulatedAttributes, true)) {
  1457. if ($name === 'here') {
  1458. return $this->base . $this->uri->getPath();
  1459. }
  1460. return $this->{$name};
  1461. }
  1462. if (array_key_exists($name, $this->attributes)) {
  1463. return $this->attributes[$name];
  1464. }
  1465. return $default;
  1466. }
  1467. /**
  1468. * Get all the attributes in the request.
  1469. *
  1470. * This will include the params, webroot, base, and here attributes that CakePHP
  1471. * provides.
  1472. *
  1473. * @return array
  1474. */
  1475. public function getAttributes(): array
  1476. {
  1477. $emulated = [
  1478. 'params' => $this->params,
  1479. 'webroot' => $this->webroot,
  1480. 'base' => $this->base,
  1481. 'here' => $this->base . $this->uri->getPath(),
  1482. ];
  1483. return $this->attributes + $emulated;
  1484. }
  1485. /**
  1486. * Get the uploaded file from a dotted path.
  1487. *
  1488. * @param string $path The dot separated path to the file you want.
  1489. * @return \Psr\Http\Message\UploadedFileInterface|null
  1490. */
  1491. public function getUploadedFile(string $path): ?UploadedFileInterface
  1492. {
  1493. $file = Hash::get($this->uploadedFiles, $path);
  1494. if (!$file instanceof UploadedFile) {
  1495. return null;
  1496. }
  1497. return $file;
  1498. }
  1499. /**
  1500. * Get the array of uploaded files from the request.
  1501. *
  1502. * @return array
  1503. */
  1504. public function getUploadedFiles(): array
  1505. {
  1506. return $this->uploadedFiles;
  1507. }
  1508. /**
  1509. * Update the request replacing the files, and creating a new instance.
  1510. *
  1511. * @param array $uploadedFiles An array of uploaded file objects.
  1512. * @return static
  1513. * @throws \InvalidArgumentException when $files contains an invalid object.
  1514. */
  1515. public function withUploadedFiles(array $uploadedFiles)
  1516. {
  1517. $this->validateUploadedFiles($uploadedFiles, '');
  1518. $new = clone $this;
  1519. $new->uploadedFiles = $uploadedFiles;
  1520. return $new;
  1521. }
  1522. /**
  1523. * Recursively validate uploaded file data.
  1524. *
  1525. * @param array $uploadedFiles The new files array to validate.
  1526. * @param string $path The path thus far.
  1527. * @return void
  1528. * @throws \InvalidArgumentException If any leaf elements are not valid files.
  1529. */
  1530. protected function validateUploadedFiles(array $uploadedFiles, string $path): void
  1531. {
  1532. foreach ($uploadedFiles as $key => $file) {
  1533. if (is_array($file)) {
  1534. $this->validateUploadedFiles($file, $key . '.');
  1535. continue;
  1536. }
  1537. if (!$file instanceof UploadedFileInterface) {
  1538. throw new InvalidArgumentException("Invalid file at '{$path}{$key}'");
  1539. }
  1540. }
  1541. }
  1542. /**
  1543. * Gets the body of the message.
  1544. *
  1545. * @return \Psr\Http\Message\StreamInterface Returns the body as a stream.
  1546. */
  1547. public function getBody(): StreamInterface
  1548. {
  1549. return $this->stream;
  1550. }
  1551. /**
  1552. * Return an instance with the specified message body.
  1553. *
  1554. * @param \Psr\Http\Message\StreamInterface $body The new request body
  1555. * @return static
  1556. */
  1557. public function withBody(StreamInterface $body)
  1558. {
  1559. $new = clone $this;
  1560. $new->stream = $body;
  1561. return $new;
  1562. }
  1563. /**
  1564. * Retrieves the URI instance.
  1565. *
  1566. * @return \Psr\Http\Message\UriInterface Returns a UriInterface instance
  1567. * representing the URI of the request.
  1568. */
  1569. public function getUri(): UriInterface
  1570. {
  1571. return $this->uri;
  1572. }
  1573. /**
  1574. * Return an instance with the specified uri
  1575. *
  1576. * *Warning* Replacing the Uri will not update the `base`, `webroot`,
  1577. * and `url` attributes.
  1578. *
  1579. * @param \Psr\Http\Message\UriInterface $uri The new request uri
  1580. * @param bool $preserveHost Whether the host should be retained.
  1581. * @return static
  1582. */
  1583. public function withUri(UriInterface $uri, $preserveHost = false)
  1584. {
  1585. $new = clone $this;
  1586. $new->uri = $uri;
  1587. if ($preserveHost && $this->hasHeader('Host')) {
  1588. return $new;
  1589. }
  1590. $host = $uri->getHost();
  1591. if (!$host) {
  1592. return $new;
  1593. }
  1594. $port = $uri->getPort();
  1595. if ($port) {
  1596. $host .= ':' . $port;
  1597. }
  1598. $new->_environment['HTTP_HOST'] = $host;
  1599. return $new;
  1600. }
  1601. /**
  1602. * Create a new instance with a specific request-target.
  1603. *
  1604. * You can use this method to overwrite the request target that is
  1605. * inferred from the request's Uri. This also lets you change the request
  1606. * target's form to an absolute-form, authority-form or asterisk-form
  1607. *
  1608. * @link https://tools.ietf.org/html/rfc7230#section-2.7 (for the various
  1609. * request-target forms allowed in request messages)
  1610. * @param string $requestTarget The request target.
  1611. * @return static
  1612. * @psalm-suppress MoreSpecificImplementedParamType
  1613. */
  1614. public function withRequestTarget($requestTarget)
  1615. {
  1616. $new = clone $this;
  1617. $new->requestTarget = $requestTarget;
  1618. return $new;
  1619. }
  1620. /**
  1621. * Retrieves the request's target.
  1622. *
  1623. * Retrieves the message's request-target either as it was requested,
  1624. * or as set with `withRequestTarget()`. By default this will return the
  1625. * application relative path without base directory, and the query string
  1626. * defined in the SERVER environment.
  1627. *
  1628. * @return string
  1629. */
  1630. public function getRequestTarget(): string
  1631. {
  1632. if ($this->requestTarget !== null) {
  1633. return $this->requestTarget;
  1634. }
  1635. $target = $this->uri->getPath();
  1636. if ($this->uri->getQuery()) {
  1637. $target .= '?' . $this->uri->getQuery();
  1638. }
  1639. if (empty($target)) {
  1640. $target = '/';
  1641. }
  1642. return $target;
  1643. }
  1644. /**
  1645. * Get the path of current request.
  1646. *
  1647. * @return string
  1648. * @since 3.6.1
  1649. */
  1650. public function getPath(): string
  1651. {
  1652. if ($this->requestTarget === null) {
  1653. return $this->uri->getPath();
  1654. }
  1655. [$path] = explode('?', $this->requestTarget);
  1656. return $path;
  1657. }
  1658. }