ServerRequest.php 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Http;
  16. use ArrayAccess;
  17. use BadMethodCallException;
  18. use Cake\Core\Configure;
  19. use Cake\Http\Cookie\CookieCollection;
  20. use Cake\Http\Exception\MethodNotAllowedException;
  21. use Cake\Http\Session;
  22. use Cake\Utility\Hash;
  23. use InvalidArgumentException;
  24. use Psr\Http\Message\ServerRequestInterface;
  25. use Psr\Http\Message\StreamInterface;
  26. use Psr\Http\Message\UploadedFileInterface;
  27. use Psr\Http\Message\UriInterface;
  28. use Zend\Diactoros\PhpInputStream;
  29. use Zend\Diactoros\Stream;
  30. use Zend\Diactoros\UploadedFile;
  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 ArrayAccess, ServerRequestInterface
  36. {
  37. /**
  38. * Array of parameters parsed from the URL.
  39. *
  40. * @var array
  41. * @deprecated 3.4.0 This public property will be removed in 4.0.0. Use getParam() instead.
  42. */
  43. protected $params = [
  44. 'plugin' => null,
  45. 'controller' => null,
  46. 'action' => null,
  47. '_ext' => null,
  48. 'pass' => []
  49. ];
  50. /**
  51. * Array of POST data. Will contain form data as well as uploaded files.
  52. * In PUT/PATCH/DELETE requests this property will contain the form-urlencoded
  53. * data.
  54. *
  55. * @var null|array|object
  56. * @deprecated 3.4.0 This public property will be removed in 4.0.0. Use getData() instead.
  57. */
  58. protected $data = [];
  59. /**
  60. * Array of query string arguments
  61. *
  62. * @var array
  63. * @deprecated 3.4.0 This public property will be removed in 4.0.0. Use getQuery() or getQueryParams() instead.
  64. */
  65. protected $query = [];
  66. /**
  67. * Array of cookie data.
  68. *
  69. * @var array
  70. * @deprecated 3.4.0 This public property will be removed in 4.0.0. Use getCookie() instead.
  71. */
  72. protected $cookies = [];
  73. /**
  74. * Array of environment data.
  75. *
  76. * @var array
  77. */
  78. protected $_environment = [];
  79. /**
  80. * The URL string used for the request.
  81. *
  82. * @var string
  83. * @deprecated 3.6.0 This public property will be removed in 4.0.0. Use getRequestTarget() instead.
  84. */
  85. protected $url;
  86. /**
  87. * Base URL path.
  88. *
  89. * @var string
  90. * @deprecated 3.4.0 This public property will be removed in 4.0.0. Use getAttribute('base') instead.
  91. */
  92. protected $base;
  93. /**
  94. * webroot path segment for the request.
  95. *
  96. * @var string
  97. * @deprecated 3.4.0 This public property will be removed in 4.0.0. Use getAttribute('webroot') instead.
  98. */
  99. protected $webroot = '/';
  100. /**
  101. * The full address to the current request
  102. *
  103. * @var string
  104. * @deprecated 3.4.0 This public property will be removed in 4.0.0. Use getAttribute('here') or getUri()->getPath() instead.
  105. */
  106. protected $here;
  107. /**
  108. * Whether or not to trust HTTP_X headers set by most load balancers.
  109. * Only set to true if your application runs behind load balancers/proxies
  110. * that you control.
  111. *
  112. * @var bool
  113. */
  114. public $trustProxy = false;
  115. /**
  116. * Contents of php://input
  117. *
  118. * @var string
  119. */
  120. protected $_input;
  121. /**
  122. * The built in detectors used with `is()` can be modified with `addDetector()`.
  123. *
  124. * There are several ways to specify a detector, see \Cake\Http\ServerRequest::addDetector() for the
  125. * various formats and ways to define detectors.
  126. *
  127. * @var array
  128. */
  129. protected static $_detectors = [
  130. 'get' => ['env' => 'REQUEST_METHOD', 'value' => 'GET'],
  131. 'post' => ['env' => 'REQUEST_METHOD', 'value' => 'POST'],
  132. 'put' => ['env' => 'REQUEST_METHOD', 'value' => 'PUT'],
  133. 'patch' => ['env' => 'REQUEST_METHOD', 'value' => 'PATCH'],
  134. 'delete' => ['env' => 'REQUEST_METHOD', 'value' => 'DELETE'],
  135. 'head' => ['env' => 'REQUEST_METHOD', 'value' => 'HEAD'],
  136. 'options' => ['env' => 'REQUEST_METHOD', 'value' => 'OPTIONS'],
  137. 'ssl' => ['env' => 'HTTPS', 'options' => [1, 'on']],
  138. 'ajax' => ['env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'],
  139. 'flash' => ['env' => 'HTTP_USER_AGENT', 'pattern' => '/^(Shockwave|Adobe) Flash/'],
  140. 'requested' => ['param' => 'requested', 'value' => 1],
  141. 'json' => ['accept' => ['application/json'], 'param' => '_ext', 'value' => 'json'],
  142. 'xml' => ['accept' => ['application/xml', 'text/xml'], 'param' => '_ext', 'value' => 'xml'],
  143. ];
  144. /**
  145. * Instance cache for results of is(something) calls
  146. *
  147. * @var array
  148. */
  149. protected $_detectorCache = [];
  150. /**
  151. * Request body stream. Contains php://input unless `input` constructor option is used.
  152. *
  153. * @var \Psr\Http\Message\StreamInterface
  154. */
  155. protected $stream;
  156. /**
  157. * Uri instance
  158. *
  159. * @var \Psr\Http\Message\UriInterface
  160. */
  161. protected $uri;
  162. /**
  163. * Instance of a Session object relative to this request
  164. *
  165. * @var \Cake\Http\Session
  166. */
  167. protected $session;
  168. /**
  169. * Store the additional attributes attached to the request.
  170. *
  171. * @var array
  172. */
  173. protected $attributes = [];
  174. /**
  175. * A list of propertes that emulated by the PSR7 attribute methods.
  176. *
  177. * @var array
  178. */
  179. protected $emulatedAttributes = ['session', 'webroot', 'base', 'params', 'here'];
  180. /**
  181. * Array of Psr\Http\Message\UploadedFileInterface objects.
  182. *
  183. * @var array
  184. */
  185. protected $uploadedFiles = [];
  186. /**
  187. * The HTTP protocol version used.
  188. *
  189. * @var string|null
  190. */
  191. protected $protocol;
  192. /**
  193. * The request target if overridden
  194. *
  195. * @var string|null
  196. */
  197. protected $requestTarget;
  198. /**
  199. * List of deprecated properties that have backwards
  200. * compatibility offered through magic methods.
  201. *
  202. * @var array
  203. */
  204. private $deprecatedProperties = [
  205. 'data' => ['get' => 'getData()', 'set' => 'withData()'],
  206. 'query' => ['get' => 'getQuery()', 'set' => 'withQueryParams()'],
  207. 'params' => ['get' => 'getParam()', 'set' => 'withParam()'],
  208. 'cookies' => ['get' => 'getCookie()', 'set' => 'withCookieParams()'],
  209. 'url' => ['get' => 'getPath()', 'set' => 'withRequestTarget()'],
  210. 'base' => ['get' => 'getAttribute("base")', 'set' => 'withAttribute("base")'],
  211. 'webroot' => ['get' => 'getAttribute("webroot")', 'set' => 'withAttribute("webroot")'],
  212. 'here' => ['get' => 'getAttribute("here")', 'set' => 'withAttribute("here")'],
  213. ];
  214. /**
  215. * Wrapper method to create a new request from PHP superglobals.
  216. *
  217. * Uses the $_GET, $_POST, $_FILES, $_COOKIE, $_SERVER, $_ENV and php://input data to construct
  218. * the request.
  219. *
  220. * @return self
  221. * @deprecated 3.4.0 Use `Cake\Http\ServerRequestFactory` instead.
  222. */
  223. public static function createFromGlobals()
  224. {
  225. deprecationWarning(
  226. 'ServerRequest::createFromGlobals() is deprecated. ' .
  227. 'Use `Cake\Http\ServerRequestFactory` instead.'
  228. );
  229. return ServerRequestFactory::fromGlobals();
  230. }
  231. /**
  232. * Create a new request object.
  233. *
  234. * You can supply the data as either an array or as a string. If you use
  235. * a string you can only supply the URL for the request. Using an array will
  236. * let you provide the following keys:
  237. *
  238. * - `post` POST data or non query string data
  239. * - `query` Additional data from the query string.
  240. * - `files` Uploaded file data formatted like $_FILES.
  241. * - `cookies` Cookies for this request.
  242. * - `environment` $_SERVER and $_ENV data.
  243. * - ~~`url`~~ The URL without the base path for the request. This option is deprecated and will be removed in 4.0.0
  244. * - `uri` The PSR7 UriInterface object. If null, one will be created.
  245. * - `base` The base URL for the request.
  246. * - `webroot` The webroot directory for the request.
  247. * - `input` The data that would come from php://input this is useful for simulating
  248. * requests with put, patch or delete data.
  249. * - `session` An instance of a Session object
  250. *
  251. * @param string|array $config An array of request data to create a request with.
  252. * The string version of this argument is *deprecated* and will be removed in 4.0.0
  253. */
  254. public function __construct($config = [])
  255. {
  256. if (is_string($config)) {
  257. $config = ['url' => $config];
  258. }
  259. $config += [
  260. 'params' => $this->params,
  261. 'query' => [],
  262. 'post' => [],
  263. 'files' => [],
  264. 'cookies' => [],
  265. 'environment' => [],
  266. 'url' => '',
  267. 'uri' => null,
  268. 'base' => '',
  269. 'webroot' => '',
  270. 'input' => null,
  271. ];
  272. $this->_setConfig($config);
  273. }
  274. /**
  275. * Process the config/settings data into properties.
  276. *
  277. * @param array $config The config data to use.
  278. * @return void
  279. */
  280. protected function _setConfig($config)
  281. {
  282. if (!empty($config['url']) && $config['url'][0] === '/') {
  283. $config['url'] = substr($config['url'], 1);
  284. }
  285. if (empty($config['session'])) {
  286. $config['session'] = new Session([
  287. 'cookiePath' => $config['base']
  288. ]);
  289. }
  290. $this->_environment = $config['environment'];
  291. $this->cookies = $config['cookies'];
  292. if (isset($config['uri']) && $config['uri'] instanceof UriInterface) {
  293. $uri = $config['uri'];
  294. } else {
  295. $uri = ServerRequestFactory::createUri($config['environment']);
  296. }
  297. // Extract a query string from config[url] if present.
  298. // This is required for backwards compatibility and keeping
  299. // UriInterface implementations happy.
  300. $querystr = '';
  301. if (strpos($config['url'], '?') !== false) {
  302. list($config['url'], $querystr) = explode('?', $config['url']);
  303. }
  304. if (strlen($config['url'])) {
  305. $uri = $uri->withPath('/' . $config['url']);
  306. }
  307. if (strlen($querystr)) {
  308. $uri = $uri->withQuery($querystr);
  309. }
  310. $this->uri = $uri;
  311. $this->base = $config['base'];
  312. $this->webroot = $config['webroot'];
  313. $this->url = substr($uri->getPath(), 1);
  314. $this->here = $this->base . '/' . $this->url;
  315. if (isset($config['input'])) {
  316. $stream = new Stream('php://memory', 'rw');
  317. $stream->write($config['input']);
  318. $stream->rewind();
  319. } else {
  320. $stream = new PhpInputStream();
  321. }
  322. $this->stream = $stream;
  323. $config['post'] = $this->_processPost($config['post']);
  324. $this->data = $this->_processFiles($config['post'], $config['files']);
  325. $this->query = $this->_processGet($config['query'], $querystr);
  326. $this->params = $config['params'];
  327. $this->session = $config['session'];
  328. }
  329. /**
  330. * Sets the REQUEST_METHOD environment variable based on the simulated _method
  331. * HTTP override value. The 'ORIGINAL_REQUEST_METHOD' is also preserved, if you
  332. * want the read the non-simulated HTTP method the client used.
  333. *
  334. * @param array $data Array of post data.
  335. * @return array
  336. */
  337. protected function _processPost($data)
  338. {
  339. $method = $this->getEnv('REQUEST_METHOD');
  340. $override = false;
  341. if (in_array($method, ['PUT', 'DELETE', 'PATCH']) &&
  342. strpos($this->contentType(), 'application/x-www-form-urlencoded') === 0
  343. ) {
  344. $data = $this->input();
  345. parse_str($data, $data);
  346. }
  347. if ($this->hasHeader('X-Http-Method-Override')) {
  348. $data['_method'] = $this->getHeaderLine('X-Http-Method-Override');
  349. $override = true;
  350. }
  351. $this->_environment['ORIGINAL_REQUEST_METHOD'] = $method;
  352. if (isset($data['_method'])) {
  353. $this->_environment['REQUEST_METHOD'] = $data['_method'];
  354. unset($data['_method']);
  355. $override = true;
  356. }
  357. if ($override && !in_array($this->_environment['REQUEST_METHOD'], ['PUT', 'POST', 'DELETE', 'PATCH'])) {
  358. $data = [];
  359. }
  360. return $data;
  361. }
  362. /**
  363. * Process the GET parameters and move things into the object.
  364. *
  365. * @param array $query The array to which the parsed keys/values are being added.
  366. * @param string $queryString A query string from the URL if provided
  367. * @return array An array containing the parsed query string as keys/values.
  368. */
  369. protected function _processGet($query, $queryString = '')
  370. {
  371. $unsetUrl = '/' . str_replace(['.', ' '], '_', urldecode($this->url));
  372. unset($query[$unsetUrl], $query[$this->base . $unsetUrl]);
  373. if (strlen($queryString)) {
  374. parse_str($queryString, $queryArgs);
  375. $query += $queryArgs;
  376. }
  377. return $query;
  378. }
  379. /**
  380. * Process uploaded files and move things onto the post data.
  381. *
  382. * @param array $post Post data to merge files onto.
  383. * @param array $files Uploaded files to merge in.
  384. * @return array merged post + file data.
  385. */
  386. protected function _processFiles($post, $files)
  387. {
  388. if (!is_array($files)) {
  389. return $post;
  390. }
  391. $fileData = [];
  392. foreach ($files as $key => $value) {
  393. if ($value instanceof UploadedFileInterface) {
  394. $fileData[$key] = $value;
  395. continue;
  396. }
  397. if (is_array($value) && isset($value['tmp_name'])) {
  398. $fileData[$key] = $this->_createUploadedFile($value);
  399. continue;
  400. }
  401. throw new InvalidArgumentException(sprintf(
  402. 'Invalid value in FILES "%s"',
  403. json_encode($value)
  404. ));
  405. }
  406. $this->uploadedFiles = $fileData;
  407. // Make a flat map that can be inserted into $post for BC.
  408. $fileMap = Hash::flatten($fileData);
  409. foreach ($fileMap as $key => $file) {
  410. $error = $file->getError();
  411. $tmpName = '';
  412. if ($error === UPLOAD_ERR_OK) {
  413. $tmpName = $file->getStream()->getMetadata('uri');
  414. }
  415. $post = Hash::insert($post, $key, [
  416. 'tmp_name' => $tmpName,
  417. 'error' => $error,
  418. 'name' => $file->getClientFilename(),
  419. 'type' => $file->getClientMediaType(),
  420. 'size' => $file->getSize(),
  421. ]);
  422. }
  423. return $post;
  424. }
  425. /**
  426. * Create an UploadedFile instance from a $_FILES array.
  427. *
  428. * If the value represents an array of values, this method will
  429. * recursively process the data.
  430. *
  431. * @param array $value $_FILES struct
  432. * @return array|UploadedFileInterface
  433. */
  434. protected function _createUploadedFile(array $value)
  435. {
  436. if (is_array($value['tmp_name'])) {
  437. return $this->_normalizeNestedFiles($value);
  438. }
  439. return new UploadedFile(
  440. $value['tmp_name'],
  441. $value['size'],
  442. $value['error'],
  443. $value['name'],
  444. $value['type']
  445. );
  446. }
  447. /**
  448. * Normalize an array of file specifications.
  449. *
  450. * Loops through all nested files and returns a normalized array of
  451. * UploadedFileInterface instances.
  452. *
  453. * @param array $files The file data to normalize & convert.
  454. * @return array An array of UploadedFileInterface objects.
  455. */
  456. protected function _normalizeNestedFiles(array $files = [])
  457. {
  458. $normalizedFiles = [];
  459. foreach (array_keys($files['tmp_name']) as $key) {
  460. $spec = [
  461. 'tmp_name' => $files['tmp_name'][$key],
  462. 'size' => $files['size'][$key],
  463. 'error' => $files['error'][$key],
  464. 'name' => $files['name'][$key],
  465. 'type' => $files['type'][$key],
  466. ];
  467. $normalizedFiles[$key] = $this->_createUploadedFile($spec);
  468. }
  469. return $normalizedFiles;
  470. }
  471. /**
  472. * Get the content type used in this request.
  473. *
  474. * @return string
  475. */
  476. public function contentType()
  477. {
  478. $type = $this->getEnv('CONTENT_TYPE');
  479. if ($type) {
  480. return $type;
  481. }
  482. return $this->getEnv('HTTP_CONTENT_TYPE');
  483. }
  484. /**
  485. * Returns the instance of the Session object for this request
  486. *
  487. * @return \Cake\Http\Session
  488. */
  489. public function getSession()
  490. {
  491. return $this->session;
  492. }
  493. /**
  494. * Returns the instance of the Session object for this request
  495. *
  496. * If a session object is passed as first argument it will be set as
  497. * the session to use for this request
  498. *
  499. * @deprecated 3.5.0 Use getSession() instead. The setter part will be removed.
  500. * @param \Cake\Http\Session|null $session the session object to use
  501. * @return \Cake\Http\Session
  502. */
  503. public function session(Session $session = null)
  504. {
  505. deprecationWarning(
  506. 'ServerRequest::session() is deprecated. ' .
  507. 'Use getSession() instead. The setter part will be removed.'
  508. );
  509. if ($session === null) {
  510. return $this->session;
  511. }
  512. return $this->session = $session;
  513. }
  514. /**
  515. * Get the IP the client is using, or says they are using.
  516. *
  517. * @return string The client IP.
  518. */
  519. public function clientIp()
  520. {
  521. if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_FOR')) {
  522. $addresses = explode(',', $this->getEnv('HTTP_X_FORWARDED_FOR'));
  523. $ipaddr = end($addresses);
  524. } elseif ($this->trustProxy && $this->getEnv('HTTP_CLIENT_IP')) {
  525. $ipaddr = $this->getEnv('HTTP_CLIENT_IP');
  526. } else {
  527. $ipaddr = $this->getEnv('REMOTE_ADDR');
  528. }
  529. return trim($ipaddr);
  530. }
  531. /**
  532. * Returns the referer that referred this request.
  533. *
  534. * @param bool $local Attempt to return a local address.
  535. * Local addresses do not contain hostnames.
  536. * @return string The referring address for this request.
  537. */
  538. public function referer($local = false)
  539. {
  540. $ref = $this->getEnv('HTTP_REFERER');
  541. $base = Configure::read('App.fullBaseUrl') . $this->webroot;
  542. if (!empty($ref) && !empty($base)) {
  543. if ($local && strpos($ref, $base) === 0) {
  544. $ref = substr($ref, strlen($base));
  545. if (!strlen($ref) || strpos($ref, '//') === 0) {
  546. $ref = '/';
  547. }
  548. if ($ref[0] !== '/') {
  549. $ref = '/' . $ref;
  550. }
  551. return $ref;
  552. }
  553. if (!$local) {
  554. return $ref;
  555. }
  556. }
  557. return '/';
  558. }
  559. /**
  560. * Missing method handler, handles wrapping older style isAjax() type methods
  561. *
  562. * @param string $name The method called
  563. * @param array $params Array of parameters for the method call
  564. * @return mixed
  565. * @throws \BadMethodCallException when an invalid method is called.
  566. */
  567. public function __call($name, $params)
  568. {
  569. if (strpos($name, 'is') === 0) {
  570. $type = strtolower(substr($name, 2));
  571. array_unshift($params, $type);
  572. return $this->is(...$params);
  573. }
  574. throw new BadMethodCallException(sprintf('Method %s does not exist', $name));
  575. }
  576. /**
  577. * Magic set method allows backward compatibility for former public properties
  578. *
  579. *
  580. * @param string $name The property being accessed.
  581. * @param mixed $value The property value.
  582. * @return mixed Either the value of the parameter or null.
  583. * @deprecated 3.6.0 Public properties will be removed in 4.0.0.
  584. * Use appropriate setters instead.
  585. */
  586. public function __set($name, $value)
  587. {
  588. if (isset($this->deprecatedProperties[$name])) {
  589. $method = $this->deprecatedProperties[$name]['set'];
  590. deprecationWarning(
  591. "Setting {$name} as a property will be removed in 4.0.0. " .
  592. "Use {$method} instead."
  593. );
  594. return $this->{$name} = $value;
  595. }
  596. throw new BadMethodCallException("Cannot set {$name} it is not a known property.");
  597. }
  598. /**
  599. * Magic get method allows access to parsed routing parameters directly on the object.
  600. *
  601. * Allows access to `$this->params['controller']` via `$this->controller`
  602. *
  603. * @param string $name The property being accessed.
  604. * @return mixed Either the value of the parameter or null.
  605. * @deprecated 3.4.0 Accessing routing parameters through __get will removed in 4.0.0.
  606. * Use getParam() instead.
  607. */
  608. public function &__get($name)
  609. {
  610. if (isset($this->deprecatedProperties[$name])) {
  611. $method = $this->deprecatedProperties[$name]['get'];
  612. deprecationWarning(
  613. "Accessing `{$name}` as a property will be removed in 4.0.0. " .
  614. "Use request->{$method} instead."
  615. );
  616. return $this->{$name};
  617. }
  618. deprecationWarning(sprintf(
  619. 'Accessing routing parameters through `%s` will removed in 4.0.0. ' .
  620. 'Use `getParam()` instead.',
  621. $name
  622. ));
  623. if (isset($this->params[$name])) {
  624. return $this->params[$name];
  625. }
  626. $value = null;
  627. return $value;
  628. }
  629. /**
  630. * Magic isset method allows isset/empty checks
  631. * on routing parameters.
  632. *
  633. * @param string $name The property being accessed.
  634. * @return bool Existence
  635. * @deprecated 3.4.0 Accessing routing parameters through __isset will removed in 4.0.0.
  636. * Use getParam() instead.
  637. */
  638. public function __isset($name)
  639. {
  640. if (isset($this->deprecatedProperties[$name])) {
  641. $method = $this->deprecatedProperties[$name]['get'];
  642. deprecationWarning(
  643. "Accessing {$name} as a property will be removed in 4.0.0. " .
  644. "Use {$method} instead."
  645. );
  646. return isset($this->{$name});
  647. }
  648. deprecationWarning(
  649. 'Accessing routing parameters through __isset will removed in 4.0.0. ' .
  650. 'Use getParam() instead.'
  651. );
  652. return isset($this->params[$name]);
  653. }
  654. /**
  655. * Check whether or not a Request is a certain type.
  656. *
  657. * Uses the built in detection rules as well as additional rules
  658. * defined with Cake\Http\ServerRequest::addDetector(). Any detector can be called
  659. * as `is($type)` or `is$Type()`.
  660. *
  661. * @param string|array $type The type of request you want to check. If an array
  662. * this method will return true if the request matches any type.
  663. * @param array ...$args List of arguments
  664. * @return bool Whether or not the request is the type you are checking.
  665. */
  666. public function is($type, ...$args)
  667. {
  668. if (is_array($type)) {
  669. $result = array_map([$this, 'is'], $type);
  670. return count(array_filter($result)) > 0;
  671. }
  672. $type = strtolower($type);
  673. if (!isset(static::$_detectors[$type])) {
  674. return false;
  675. }
  676. if ($args) {
  677. return $this->_is($type, $args);
  678. }
  679. if (!isset($this->_detectorCache[$type])) {
  680. $this->_detectorCache[$type] = $this->_is($type, $args);
  681. }
  682. return $this->_detectorCache[$type];
  683. }
  684. /**
  685. * Clears the instance detector cache, used by the is() function
  686. *
  687. * @return void
  688. */
  689. public function clearDetectorCache()
  690. {
  691. $this->_detectorCache = [];
  692. }
  693. /**
  694. * Worker for the public is() function
  695. *
  696. * @param string $type The type of request you want to check.
  697. * @param array $args Array of custom detector arguments.
  698. * @return bool Whether or not the request is the type you are checking.
  699. */
  700. protected function _is($type, $args)
  701. {
  702. $detect = static::$_detectors[$type];
  703. if (is_callable($detect)) {
  704. array_unshift($args, $this);
  705. return $detect(...$args);
  706. }
  707. if (isset($detect['env']) && $this->_environmentDetector($detect)) {
  708. return true;
  709. }
  710. if (isset($detect['header']) && $this->_headerDetector($detect)) {
  711. return true;
  712. }
  713. if (isset($detect['accept']) && $this->_acceptHeaderDetector($detect)) {
  714. return true;
  715. }
  716. if (isset($detect['param']) && $this->_paramDetector($detect)) {
  717. return true;
  718. }
  719. return false;
  720. }
  721. /**
  722. * Detects if a specific accept header is present.
  723. *
  724. * @param array $detect Detector options array.
  725. * @return bool Whether or not the request is the type you are checking.
  726. */
  727. protected function _acceptHeaderDetector($detect)
  728. {
  729. $acceptHeaders = explode(',', $this->getEnv('HTTP_ACCEPT'));
  730. foreach ($detect['accept'] as $header) {
  731. if (in_array($header, $acceptHeaders)) {
  732. return true;
  733. }
  734. }
  735. return false;
  736. }
  737. /**
  738. * Detects if a specific header is present.
  739. *
  740. * @param array $detect Detector options array.
  741. * @return bool Whether or not the request is the type you are checking.
  742. */
  743. protected function _headerDetector($detect)
  744. {
  745. foreach ($detect['header'] as $header => $value) {
  746. $header = $this->getEnv('http_' . $header);
  747. if ($header !== null) {
  748. if (!is_string($value) && !is_bool($value) && is_callable($value)) {
  749. return call_user_func($value, $header);
  750. }
  751. return ($header === $value);
  752. }
  753. }
  754. return false;
  755. }
  756. /**
  757. * Detects if a specific request parameter is present.
  758. *
  759. * @param array $detect Detector options array.
  760. * @return bool Whether or not the request is the type you are checking.
  761. */
  762. protected function _paramDetector($detect)
  763. {
  764. $key = $detect['param'];
  765. if (isset($detect['value'])) {
  766. $value = $detect['value'];
  767. return isset($this->params[$key]) ? $this->params[$key] == $value : false;
  768. }
  769. if (isset($detect['options'])) {
  770. return isset($this->params[$key]) ? in_array($this->params[$key], $detect['options']) : false;
  771. }
  772. return false;
  773. }
  774. /**
  775. * Detects if a specific environment variable is present.
  776. *
  777. * @param array $detect Detector options array.
  778. * @return bool Whether or not the request is the type you are checking.
  779. */
  780. protected function _environmentDetector($detect)
  781. {
  782. if (isset($detect['env'])) {
  783. if (isset($detect['value'])) {
  784. return $this->getEnv($detect['env']) == $detect['value'];
  785. }
  786. if (isset($detect['pattern'])) {
  787. return (bool)preg_match($detect['pattern'], $this->getEnv($detect['env']));
  788. }
  789. if (isset($detect['options'])) {
  790. $pattern = '/' . implode('|', $detect['options']) . '/i';
  791. return (bool)preg_match($pattern, $this->getEnv($detect['env']));
  792. }
  793. }
  794. return false;
  795. }
  796. /**
  797. * Check that a request matches all the given types.
  798. *
  799. * Allows you to test multiple types and union the results.
  800. * See Request::is() for how to add additional types and the
  801. * built-in types.
  802. *
  803. * @param array $types The types to check.
  804. * @return bool Success.
  805. * @see \Cake\Http\ServerRequest::is()
  806. */
  807. public function isAll(array $types)
  808. {
  809. $result = array_filter(array_map([$this, 'is'], $types));
  810. return count($result) === count($types);
  811. }
  812. /**
  813. * Add a new detector to the list of detectors that a request can use.
  814. * There are several different formats and types of detectors that can be set.
  815. *
  816. * ### Callback detectors
  817. *
  818. * Callback detectors allow you to provide a callable to handle the check.
  819. * The callback will receive the request object as its only parameter.
  820. *
  821. * ```
  822. * addDetector('custom', function ($request) { //Return a boolean });
  823. * addDetector('custom', ['SomeClass', 'somemethod']);
  824. * ```
  825. *
  826. * ### Environment value comparison
  827. *
  828. * An environment value comparison, compares a value fetched from `env()` to a known value
  829. * the environment value is equality checked against the provided value.
  830. *
  831. * e.g `addDetector('post', ['env' => 'REQUEST_METHOD', 'value' => 'POST'])`
  832. *
  833. * ### Pattern value comparison
  834. *
  835. * Pattern value comparison allows you to compare a value fetched from `env()` to a regular expression.
  836. *
  837. * ```
  838. * addDetector('iphone', ['env' => 'HTTP_USER_AGENT', 'pattern' => '/iPhone/i']);
  839. * ```
  840. *
  841. * ### Option based comparison
  842. *
  843. * Option based comparisons use a list of options to create a regular expression. Subsequent calls
  844. * to add an already defined options detector will merge the options.
  845. *
  846. * ```
  847. * addDetector('mobile', ['env' => 'HTTP_USER_AGENT', 'options' => ['Fennec']]);
  848. * ```
  849. *
  850. * ### Request parameter detectors
  851. *
  852. * Allows for custom detectors on the request parameters.
  853. *
  854. * e.g `addDetector('requested', ['param' => 'requested', 'value' => 1]`
  855. *
  856. * You can also make parameter detectors that accept multiple values
  857. * using the `options` key. This is useful when you want to check
  858. * if a request parameter is in a list of options.
  859. *
  860. * `addDetector('extension', ['param' => 'ext', 'options' => ['pdf', 'csv']]`
  861. *
  862. * @param string $name The name of the detector.
  863. * @param callable|array $callable A callable or options array for the detector definition.
  864. * @return void
  865. */
  866. public static function addDetector($name, $callable)
  867. {
  868. $name = strtolower($name);
  869. if (is_callable($callable)) {
  870. static::$_detectors[$name] = $callable;
  871. return;
  872. }
  873. if (isset(static::$_detectors[$name], $callable['options'])) {
  874. $callable = Hash::merge(static::$_detectors[$name], $callable);
  875. }
  876. static::$_detectors[$name] = $callable;
  877. }
  878. /**
  879. * Add parameters to the request's parsed parameter set. This will overwrite any existing parameters.
  880. * This modifies the parameters available through `$request->getParam()`.
  881. *
  882. * @param array $params Array of parameters to merge in
  883. * @return $this The current object, you can chain this method.
  884. * @deprecated 3.6.0 ServerRequest::addParams() is deprecated. Use `withParam()` or
  885. * `withAttribute('params')` instead.
  886. */
  887. public function addParams(array $params)
  888. {
  889. deprecationWarning(
  890. 'ServerRequest::addParams() is deprecated. ' .
  891. 'Use `withParam()` or `withAttribute("params", $params)` instead.'
  892. );
  893. $this->params = array_merge($this->params, $params);
  894. return $this;
  895. }
  896. /**
  897. * Add paths to the requests' paths vars. This will overwrite any existing paths.
  898. * Provides an easy way to modify, here, webroot and base.
  899. *
  900. * @param array $paths Array of paths to merge in
  901. * @return $this The current object, you can chain this method.
  902. * @deprecated 3.6.0 Mutating a request in place is deprecated. Use `withAttribute()` to modify paths instead.
  903. */
  904. public function addPaths(array $paths)
  905. {
  906. deprecationWarning(
  907. 'ServerRequest::addPaths() is deprecated. ' .
  908. 'Use `withAttribute($key, $value)` instead.'
  909. );
  910. foreach (['webroot', 'here', 'base'] as $element) {
  911. if (isset($paths[$element])) {
  912. $this->{$element} = $paths[$element];
  913. }
  914. }
  915. return $this;
  916. }
  917. /**
  918. * Get the value of the current requests URL. Will include the query string arguments.
  919. *
  920. * @param bool $base Include the base path, set to false to trim the base path off.
  921. * @return string The current request URL including query string args.
  922. * @deprecated 3.4.0 This method will be removed in 4.0.0. You should use getRequestTarget() instead.
  923. */
  924. public function here($base = true)
  925. {
  926. deprecationWarning(
  927. 'ServerRequest::here() will be removed in 4.0.0. You should use getRequestTarget() instead.'
  928. );
  929. $url = $this->here;
  930. if (!empty($this->query)) {
  931. $url .= '?' . http_build_query($this->query, null, '&');
  932. }
  933. if (!$base) {
  934. $url = preg_replace('/^' . preg_quote($this->base, '/') . '/', '', $url, 1);
  935. }
  936. return $url;
  937. }
  938. /**
  939. * Normalize a header name into the SERVER version.
  940. *
  941. * @param string $name The header name.
  942. * @return string The normalized header name.
  943. */
  944. protected function normalizeHeaderName($name)
  945. {
  946. $name = str_replace('-', '_', strtoupper($name));
  947. if (!in_array($name, ['CONTENT_LENGTH', 'CONTENT_TYPE'])) {
  948. $name = 'HTTP_' . $name;
  949. }
  950. return $name;
  951. }
  952. /**
  953. * Read an HTTP header from the Request information.
  954. *
  955. * If the header is not defined in the request, this method
  956. * will fallback to reading data from $_SERVER and $_ENV.
  957. * This fallback behavior is deprecated, and will be removed in 4.0.0
  958. *
  959. * @param string $name Name of the header you want.
  960. * @return string|null Either null on no header being set or the value of the header.
  961. * @deprecated 4.0.0 The automatic fallback to env() will be removed in 4.0.0, see getHeader()
  962. */
  963. public function header($name)
  964. {
  965. deprecationWarning(
  966. 'ServerRequest::header() is deprecated. ' .
  967. 'The automatic fallback to env() will be removed in 4.0.0, see getHeader()'
  968. );
  969. $name = $this->normalizeHeaderName($name);
  970. return $this->getEnv($name);
  971. }
  972. /**
  973. * Get all headers in the request.
  974. *
  975. * Returns an associative array where the header names are
  976. * the keys and the values are a list of header values.
  977. *
  978. * While header names are not case-sensitive, getHeaders() will normalize
  979. * the headers.
  980. *
  981. * @return array An associative array of headers and their values.
  982. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  983. */
  984. public function getHeaders()
  985. {
  986. $headers = [];
  987. foreach ($this->_environment as $key => $value) {
  988. $name = null;
  989. if (strpos($key, 'HTTP_') === 0) {
  990. $name = substr($key, 5);
  991. }
  992. if (strpos($key, 'CONTENT_') === 0) {
  993. $name = $key;
  994. }
  995. if ($name !== null) {
  996. $name = str_replace('_', ' ', strtolower($name));
  997. $name = str_replace(' ', '-', ucwords($name));
  998. $headers[$name] = (array)$value;
  999. }
  1000. }
  1001. return $headers;
  1002. }
  1003. /**
  1004. * Check if a header is set in the request.
  1005. *
  1006. * @param string $name The header you want to get (case-insensitive)
  1007. * @return bool Whether or not the header is defined.
  1008. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1009. */
  1010. public function hasHeader($name)
  1011. {
  1012. $name = $this->normalizeHeaderName($name);
  1013. return isset($this->_environment[$name]);
  1014. }
  1015. /**
  1016. * Get a single header from the request.
  1017. *
  1018. * Return the header value as an array. If the header
  1019. * is not present an empty array will be returned.
  1020. *
  1021. * @param string $name The header you want to get (case-insensitive)
  1022. * @return array An associative array of headers and their values.
  1023. * If the header doesn't exist, an empty array will be returned.
  1024. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1025. */
  1026. public function getHeader($name)
  1027. {
  1028. $name = $this->normalizeHeaderName($name);
  1029. if (isset($this->_environment[$name])) {
  1030. return (array)$this->_environment[$name];
  1031. }
  1032. return [];
  1033. }
  1034. /**
  1035. * Get a single header as a string from the request.
  1036. *
  1037. * @param string $name The header you want to get (case-insensitive)
  1038. * @return string Header values collapsed into a comma separated string.
  1039. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1040. */
  1041. public function getHeaderLine($name)
  1042. {
  1043. $value = $this->getHeader($name);
  1044. return implode(', ', $value);
  1045. }
  1046. /**
  1047. * Get a modified request with the provided header.
  1048. *
  1049. * @param string $name The header name.
  1050. * @param string|array $value The header value
  1051. * @return static
  1052. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1053. */
  1054. public function withHeader($name, $value)
  1055. {
  1056. $new = clone $this;
  1057. $name = $this->normalizeHeaderName($name);
  1058. $new->_environment[$name] = $value;
  1059. return $new;
  1060. }
  1061. /**
  1062. * Get a modified request with the provided header.
  1063. *
  1064. * Existing header values will be retained. The provided value
  1065. * will be appended into the existing values.
  1066. *
  1067. * @param string $name The header name.
  1068. * @param string|array $value The header value
  1069. * @return static
  1070. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1071. */
  1072. public function withAddedHeader($name, $value)
  1073. {
  1074. $new = clone $this;
  1075. $name = $this->normalizeHeaderName($name);
  1076. $existing = [];
  1077. if (isset($new->_environment[$name])) {
  1078. $existing = (array)$new->_environment[$name];
  1079. }
  1080. $existing = array_merge($existing, (array)$value);
  1081. $new->_environment[$name] = $existing;
  1082. return $new;
  1083. }
  1084. /**
  1085. * Get a modified request without a provided header.
  1086. *
  1087. * @param string $name The header name to remove.
  1088. * @return static
  1089. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1090. */
  1091. public function withoutHeader($name)
  1092. {
  1093. $new = clone $this;
  1094. $name = $this->normalizeHeaderName($name);
  1095. unset($new->_environment[$name]);
  1096. return $new;
  1097. }
  1098. /**
  1099. * Get the HTTP method used for this request.
  1100. *
  1101. * @return string The name of the HTTP method used.
  1102. * @deprecated 3.4.0 This method will be removed in 4.0.0. Use getMethod() instead.
  1103. */
  1104. public function method()
  1105. {
  1106. deprecationWarning(
  1107. 'ServerRequest::method() is deprecated. ' .
  1108. 'This method will be removed in 4.0.0. Use getMethod() instead.'
  1109. );
  1110. return $this->getEnv('REQUEST_METHOD');
  1111. }
  1112. /**
  1113. * Get the HTTP method used for this request.
  1114. * There are a few ways to specify a method.
  1115. *
  1116. * - If your client supports it you can use native HTTP methods.
  1117. * - You can set the HTTP-X-Method-Override header.
  1118. * - You can submit an input with the name `_method`
  1119. *
  1120. * Any of these 3 approaches can be used to set the HTTP method used
  1121. * by CakePHP internally, and will effect the result of this method.
  1122. *
  1123. * @return string The name of the HTTP method used.
  1124. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1125. */
  1126. public function getMethod()
  1127. {
  1128. return $this->getEnv('REQUEST_METHOD');
  1129. }
  1130. /**
  1131. * Update the request method and get a new instance.
  1132. *
  1133. * @param string $method The HTTP method to use.
  1134. * @return static A new instance with the updated method.
  1135. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1136. */
  1137. public function withMethod($method)
  1138. {
  1139. $new = clone $this;
  1140. if (!is_string($method) ||
  1141. !preg_match('/^[!#$%&\'*+.^_`\|~0-9a-z-]+$/i', $method)
  1142. ) {
  1143. throw new InvalidArgumentException(sprintf(
  1144. 'Unsupported HTTP method "%s" provided',
  1145. $method
  1146. ));
  1147. }
  1148. $new->_environment['REQUEST_METHOD'] = $method;
  1149. return $new;
  1150. }
  1151. /**
  1152. * Get all the server environment parameters.
  1153. *
  1154. * Read all of the 'environment' or 'server' data that was
  1155. * used to create this request.
  1156. *
  1157. * @return array
  1158. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1159. */
  1160. public function getServerParams()
  1161. {
  1162. return $this->_environment;
  1163. }
  1164. /**
  1165. * Get all the query parameters in accordance to the PSR-7 specifications. To read specific query values
  1166. * use the alternative getQuery() method.
  1167. *
  1168. * @return array
  1169. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1170. */
  1171. public function getQueryParams()
  1172. {
  1173. return $this->query;
  1174. }
  1175. /**
  1176. * Update the query string data and get a new instance.
  1177. *
  1178. * @param array $query The query string data to use
  1179. * @return static A new instance with the updated query string data.
  1180. * @link http://www.php-fig.org/psr/psr-7/ This method is part of the PSR-7 server request interface.
  1181. */
  1182. public function withQueryParams(array $query)
  1183. {
  1184. $new = clone $this;
  1185. $new->query = $query;
  1186. return $new;
  1187. }
  1188. /**
  1189. * Get the host that the request was handled on.
  1190. *
  1191. * @return string
  1192. */
  1193. public function host()
  1194. {
  1195. if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_HOST')) {
  1196. return $this->getEnv('HTTP_X_FORWARDED_HOST');
  1197. }
  1198. return $this->getEnv('HTTP_HOST');
  1199. }
  1200. /**
  1201. * Get the port the request was handled on.
  1202. *
  1203. * @return string
  1204. */
  1205. public function port()
  1206. {
  1207. if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_PORT')) {
  1208. return $this->getEnv('HTTP_X_FORWARDED_PORT');
  1209. }
  1210. return $this->getEnv('SERVER_PORT');
  1211. }
  1212. /**
  1213. * Get the current url scheme used for the request.
  1214. *
  1215. * e.g. 'http', or 'https'
  1216. *
  1217. * @return string The scheme used for the request.
  1218. */
  1219. public function scheme()
  1220. {
  1221. if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_PROTO')) {
  1222. return $this->getEnv('HTTP_X_FORWARDED_PROTO');
  1223. }
  1224. return $this->getEnv('HTTPS') ? 'https' : 'http';
  1225. }
  1226. /**
  1227. * Get the domain name and include $tldLength segments of the tld.
  1228. *
  1229. * @param int $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld.
  1230. * While `example.co.uk` contains 2.
  1231. * @return string Domain name without subdomains.
  1232. */
  1233. public function domain($tldLength = 1)
  1234. {
  1235. $segments = explode('.', $this->host());
  1236. $domain = array_slice($segments, -1 * ($tldLength + 1));
  1237. return implode('.', $domain);
  1238. }
  1239. /**
  1240. * Get the subdomains for a host.
  1241. *
  1242. * @param int $tldLength Number of segments your tld contains. For example: `example.com` contains 1 tld.
  1243. * While `example.co.uk` contains 2.
  1244. * @return array An array of subdomains.
  1245. */
  1246. public function subdomains($tldLength = 1)
  1247. {
  1248. $segments = explode('.', $this->host());
  1249. return array_slice($segments, 0, -1 * ($tldLength + 1));
  1250. }
  1251. /**
  1252. * Find out which content types the client accepts or check if they accept a
  1253. * particular type of content.
  1254. *
  1255. * #### Get all types:
  1256. *
  1257. * ```
  1258. * $this->request->accepts();
  1259. * ```
  1260. *
  1261. * #### Check for a single type:
  1262. *
  1263. * ```
  1264. * $this->request->accepts('application/json');
  1265. * ```
  1266. *
  1267. * This method will order the returned content types by the preference values indicated
  1268. * by the client.
  1269. *
  1270. * @param string|null $type The content type to check for. Leave null to get all types a client accepts.
  1271. * @return array|bool Either an array of all the types the client accepts or a boolean if they accept the
  1272. * provided type.
  1273. */
  1274. public function accepts($type = null)
  1275. {
  1276. $raw = $this->parseAccept();
  1277. $accept = [];
  1278. foreach ($raw as $types) {
  1279. $accept = array_merge($accept, $types);
  1280. }
  1281. if ($type === null) {
  1282. return $accept;
  1283. }
  1284. return in_array($type, $accept);
  1285. }
  1286. /**
  1287. * Parse the HTTP_ACCEPT header and return a sorted array with content types
  1288. * as the keys, and pref values as the values.
  1289. *
  1290. * Generally you want to use Cake\Http\ServerRequest::accept() to get a simple list
  1291. * of the accepted content types.
  1292. *
  1293. * @return array An array of prefValue => [content/types]
  1294. */
  1295. public function parseAccept()
  1296. {
  1297. return $this->_parseAcceptWithQualifier($this->getHeaderLine('Accept'));
  1298. }
  1299. /**
  1300. * Get the languages accepted by the client, or check if a specific language is accepted.
  1301. *
  1302. * Get the list of accepted languages:
  1303. *
  1304. * ``` \Cake\Http\ServerRequest::acceptLanguage(); ```
  1305. *
  1306. * Check if a specific language is accepted:
  1307. *
  1308. * ``` \Cake\Http\ServerRequest::acceptLanguage('es-es'); ```
  1309. *
  1310. * @param string|null $language The language to test.
  1311. * @return array|bool If a $language is provided, a boolean. Otherwise the array of accepted languages.
  1312. */
  1313. public function acceptLanguage($language = null)
  1314. {
  1315. $raw = $this->_parseAcceptWithQualifier($this->getHeaderLine('Accept-Language'));
  1316. $accept = [];
  1317. foreach ($raw as $languages) {
  1318. foreach ($languages as &$lang) {
  1319. if (strpos($lang, '_')) {
  1320. $lang = str_replace('_', '-', $lang);
  1321. }
  1322. $lang = strtolower($lang);
  1323. }
  1324. $accept = array_merge($accept, $languages);
  1325. }
  1326. if ($language === null) {
  1327. return $accept;
  1328. }
  1329. return in_array(strtolower($language), $accept);
  1330. }
  1331. /**
  1332. * Parse Accept* headers with qualifier options.
  1333. *
  1334. * Only qualifiers will be extracted, any other accept extensions will be
  1335. * discarded as they are not frequently used.
  1336. *
  1337. * @param string $header Header to parse.
  1338. * @return array
  1339. */
  1340. protected function _parseAcceptWithQualifier($header)
  1341. {
  1342. $accept = [];
  1343. $header = explode(',', $header);
  1344. foreach (array_filter($header) as $value) {
  1345. $prefValue = '1.0';
  1346. $value = trim($value);
  1347. $semiPos = strpos($value, ';');
  1348. if ($semiPos !== false) {
  1349. $params = explode(';', $value);
  1350. $value = trim($params[0]);
  1351. foreach ($params as $param) {
  1352. $qPos = strpos($param, 'q=');
  1353. if ($qPos !== false) {
  1354. $prefValue = substr($param, $qPos + 2);
  1355. }
  1356. }
  1357. }
  1358. if (!isset($accept[$prefValue])) {
  1359. $accept[$prefValue] = [];
  1360. }
  1361. if ($prefValue) {
  1362. $accept[$prefValue][] = $value;
  1363. }
  1364. }
  1365. krsort($accept);
  1366. return $accept;
  1367. }
  1368. /**
  1369. * Provides a read accessor for `$this->query`.
  1370. * Allows you to use a `Hash::get()` compatible syntax for reading post data.
  1371. *
  1372. * @param string|null $name Query string variable name or null to read all.
  1373. * @return string|array|null The value being read
  1374. * @deprecated 3.4.0 Use getQuery() or the PSR-7 getQueryParams() and withQueryParams() methods instead.
  1375. */
  1376. public function query($name = null)
  1377. {
  1378. deprecationWarning(
  1379. 'ServerRequest::query() is deprecated. ' .
  1380. 'Use getQuery() or the PSR-7 getQueryParams() and withQueryParams() methods instead.'
  1381. );
  1382. if ($name === null) {
  1383. return $this->query;
  1384. }
  1385. return $this->getQuery($name);
  1386. }
  1387. /**
  1388. * Read a specific query value or dotted path.
  1389. *
  1390. * Developers are encouraged to use getQueryParams() when possible as it is PSR-7 compliant, and this method
  1391. * is not.
  1392. *
  1393. * ### PSR-7 Alternative
  1394. *
  1395. * ```
  1396. * $value = Hash::get($request->getQueryParams(), 'Post.id', null);
  1397. * ```
  1398. *
  1399. * @param string|null $name The name or dotted path to the query param or null to read all.
  1400. * @param mixed $default The default value if the named parameter is not set, and $name is not null.
  1401. * @return null|string|array Query data.
  1402. * @see ServerRequest::getQueryParams()
  1403. */
  1404. public function getQuery($name = null, $default = null)
  1405. {
  1406. if ($name === null) {
  1407. return $this->query;
  1408. }
  1409. return Hash::get($this->query, $name, $default);
  1410. }
  1411. /**
  1412. * Provides a read/write accessor for `$this->data`.
  1413. * Allows you to use a `Hash::get()` compatible syntax for reading post data.
  1414. *
  1415. * ### Reading values.
  1416. *
  1417. * ```
  1418. * $request->data('Post.title');
  1419. * ```
  1420. *
  1421. * When reading values you will get `null` for keys/values that do not exist.
  1422. *
  1423. * ### Writing values
  1424. *
  1425. * ```
  1426. * $request->data('Post.title', 'New post!');
  1427. * ```
  1428. *
  1429. * You can write to any value, even paths/keys that do not exist, and the arrays
  1430. * will be created for you.
  1431. *
  1432. * @param string|null $name Dot separated name of the value to read/write
  1433. * @param mixed ...$args The data to set (deprecated)
  1434. * @return mixed|$this Either the value being read, or this so you can chain consecutive writes.
  1435. * @deprecated 3.4.0 Use withData() and getData() or getParsedBody() instead.
  1436. */
  1437. public function data($name = null, ...$args)
  1438. {
  1439. deprecationWarning(
  1440. 'ServerRequest::data() is deprecated. ' .
  1441. 'Use withData() and getData() or getParsedBody() instead.'
  1442. );
  1443. if (count($args) === 1) {
  1444. $this->data = Hash::insert($this->data, $name, $args[0]);
  1445. return $this;
  1446. }
  1447. if ($name !== null) {
  1448. return Hash::get($this->data, $name);
  1449. }
  1450. return $this->data;
  1451. }
  1452. /**
  1453. * Provides a safe accessor for request data. Allows
  1454. * you to use Hash::get() compatible paths.
  1455. *
  1456. * ### Reading values.
  1457. *
  1458. * ```
  1459. * // get all data
  1460. * $request->getData();
  1461. *
  1462. * // Read a specific field.
  1463. * $request->getData('Post.title');
  1464. *
  1465. * // With a default value.
  1466. * $request->getData('Post.not there', 'default value');
  1467. * ```
  1468. *
  1469. * When reading values you will get `null` for keys/values that do not exist.
  1470. *
  1471. * @param string|null $name Dot separated name of the value to read. Or null to read all data.
  1472. * @param mixed $default The default data.
  1473. * @return null|string|array The value being read.
  1474. */
  1475. public function getData($name = null, $default = null)
  1476. {
  1477. if ($name === null) {
  1478. return $this->data;
  1479. }
  1480. if (!is_array($this->data) && $name) {
  1481. return $default;
  1482. }
  1483. return Hash::get($this->data, $name, $default);
  1484. }
  1485. /**
  1486. * Safely access the values in $this->params.
  1487. *
  1488. * @param string $name The name of the parameter to get.
  1489. * @param mixed ...$args Value to set (deprecated).
  1490. * @return mixed|$this The value of the provided parameter. Will
  1491. * return false if the parameter doesn't exist or is falsey.
  1492. * @deprecated 3.4.0 Use getParam() and withParam() instead.
  1493. */
  1494. public function param($name, ...$args)
  1495. {
  1496. deprecationWarning(
  1497. 'ServerRequest::param() is deprecated. ' .
  1498. 'Use getParam() and withParam() instead.'
  1499. );
  1500. if (count($args) === 1) {
  1501. $this->params = Hash::insert($this->params, $name, $args[0]);
  1502. return $this;
  1503. }
  1504. return $this->getParam($name);
  1505. }
  1506. /**
  1507. * Read data from `php://input`. Useful when interacting with XML or JSON
  1508. * request body content.
  1509. *
  1510. * Getting input with a decoding function:
  1511. *
  1512. * ```
  1513. * $this->request->input('json_decode');
  1514. * ```
  1515. *
  1516. * Getting input using a decoding function, and additional params:
  1517. *
  1518. * ```
  1519. * $this->request->input('Xml::build', ['return' => 'DOMDocument']);
  1520. * ```
  1521. *
  1522. * Any additional parameters are applied to the callback in the order they are given.
  1523. *
  1524. * @param string|null $callback A decoding callback that will convert the string data to another
  1525. * representation. Leave empty to access the raw input data. You can also
  1526. * supply additional parameters for the decoding callback using var args, see above.
  1527. * @param array ...$args The additional arguments
  1528. * @return string The decoded/processed request data.
  1529. */
  1530. public function input($callback = null, ...$args)
  1531. {
  1532. $this->stream->rewind();
  1533. $input = $this->stream->getContents();
  1534. if ($callback) {
  1535. array_unshift($args, $input);
  1536. return call_user_func_array($callback, $args);
  1537. }
  1538. return $input;
  1539. }
  1540. /**
  1541. * Read cookie data from the request's cookie data.
  1542. *
  1543. * @param string $key The key you want to read.
  1544. * @return null|string Either the cookie value, or null if the value doesn't exist.
  1545. * @deprecated 3.4.0 Use getCookie() instead.
  1546. */
  1547. public function cookie($key)
  1548. {
  1549. deprecationWarning(
  1550. 'ServerRequest::cookie() is deprecated. ' .
  1551. 'Use getCookie() instead.'
  1552. );
  1553. if (isset($this->cookies[$key])) {
  1554. return $this->cookies[$key];
  1555. }
  1556. return null;
  1557. }
  1558. /**
  1559. * Read cookie data from the request's cookie data.
  1560. *
  1561. * @param string $key The key or dotted path you want to read.
  1562. * @param string $default The default value if the cookie is not set.
  1563. * @return null|array|string Either the cookie value, or null if the value doesn't exist.
  1564. */
  1565. public function getCookie($key, $default = null)
  1566. {
  1567. return Hash::get($this->cookies, $key, $default);
  1568. }
  1569. /**
  1570. * Get a cookie collection based on the request's cookies
  1571. *
  1572. * The CookieCollection lets you interact with request cookies using
  1573. * `\Cake\Http\Cookie\Cookie` objects and can make converting request cookies
  1574. * into response cookies easier.
  1575. *
  1576. * This method will create a new cookie collection each time it is called.
  1577. * This is an optimization that allows fewer objects to be allocated until
  1578. * the more complex CookieCollection is needed. In general you should prefer
  1579. * `getCookie()` and `getCookieParams()` over this method. Using a CookieCollection
  1580. * is ideal if your cookies contain complex JSON encoded data.
  1581. *
  1582. * @return \Cake\Http\Cookie\CookieCollection
  1583. */
  1584. public function getCookieCollection()
  1585. {
  1586. return CookieCollection::createFromServerRequest($this);
  1587. }
  1588. /**
  1589. * Replace the cookies in the request with those contained in
  1590. * the provided CookieCollection.
  1591. *
  1592. * @param \Cake\Http\Cookie\CookieCollection $cookies The cookie collection
  1593. * @return static
  1594. */
  1595. public function withCookieCollection(CookieCollection $cookies)
  1596. {
  1597. $new = clone $this;
  1598. $values = [];
  1599. foreach ($cookies as $cookie) {
  1600. $values[$cookie->getName()] = $cookie->getValue();
  1601. }
  1602. $new->cookies = $values;
  1603. return $new;
  1604. }
  1605. /**
  1606. * Get all the cookie data from the request.
  1607. *
  1608. * @return array An array of cookie data.
  1609. */
  1610. public function getCookieParams()
  1611. {
  1612. return $this->cookies;
  1613. }
  1614. /**
  1615. * Replace the cookies and get a new request instance.
  1616. *
  1617. * @param array $cookies The new cookie data to use.
  1618. * @return static
  1619. */
  1620. public function withCookieParams(array $cookies)
  1621. {
  1622. $new = clone $this;
  1623. $new->cookies = $cookies;
  1624. return $new;
  1625. }
  1626. /**
  1627. * Get the parsed request body data.
  1628. *
  1629. * If the request Content-Type is either application/x-www-form-urlencoded
  1630. * or multipart/form-data, nd the request method is POST, this will be the
  1631. * post data. For other content types, it may be the deserialized request
  1632. * body.
  1633. *
  1634. * @return null|array|object The deserialized body parameters, if any.
  1635. * These will typically be an array or object.
  1636. */
  1637. public function getParsedBody()
  1638. {
  1639. return $this->data;
  1640. }
  1641. /**
  1642. * Update the parsed body and get a new instance.
  1643. *
  1644. * @param null|array|object $data The deserialized body data. This will
  1645. * typically be in an array or object.
  1646. * @return static
  1647. */
  1648. public function withParsedBody($data)
  1649. {
  1650. $new = clone $this;
  1651. $new->data = $data;
  1652. return $new;
  1653. }
  1654. /**
  1655. * Retrieves the HTTP protocol version as a string.
  1656. *
  1657. * @return string HTTP protocol version.
  1658. */
  1659. public function getProtocolVersion()
  1660. {
  1661. if ($this->protocol) {
  1662. return $this->protocol;
  1663. }
  1664. // Lazily populate this data as it is generally not used.
  1665. preg_match('/^HTTP\/([\d.]+)$/', $this->getEnv('SERVER_PROTOCOL'), $match);
  1666. $protocol = '1.1';
  1667. if (isset($match[1])) {
  1668. $protocol = $match[1];
  1669. }
  1670. $this->protocol = $protocol;
  1671. return $this->protocol;
  1672. }
  1673. /**
  1674. * Return an instance with the specified HTTP protocol version.
  1675. *
  1676. * The version string MUST contain only the HTTP version number (e.g.,
  1677. * "1.1", "1.0").
  1678. *
  1679. * @param string $version HTTP protocol version
  1680. * @return static
  1681. */
  1682. public function withProtocolVersion($version)
  1683. {
  1684. if (!preg_match('/^(1\.[01]|2)$/', $version)) {
  1685. throw new InvalidArgumentException("Unsupported protocol version '{$version}' provided");
  1686. }
  1687. $new = clone $this;
  1688. $new->protocol = $version;
  1689. return $new;
  1690. }
  1691. /**
  1692. * Get a value from the request's environment data.
  1693. * Fallback to using env() if the key is not set in the $environment property.
  1694. *
  1695. * @param string $key The key you want to read from.
  1696. * @param string|null $default Default value when trying to retrieve an environment
  1697. * variable's value that does not exist.
  1698. * @return string|null Either the environment value, or null if the value doesn't exist.
  1699. */
  1700. public function getEnv($key, $default = null)
  1701. {
  1702. $key = strtoupper($key);
  1703. if (!array_key_exists($key, $this->_environment)) {
  1704. $this->_environment[$key] = env($key);
  1705. }
  1706. return $this->_environment[$key] !== null ? $this->_environment[$key] : $default;
  1707. }
  1708. /**
  1709. * Update the request with a new environment data element.
  1710. *
  1711. * Returns an updated request object. This method returns
  1712. * a *new* request object and does not mutate the request in-place.
  1713. *
  1714. * @param string $key The key you want to write to.
  1715. * @param string $value Value to set
  1716. * @return static
  1717. */
  1718. public function withEnv($key, $value)
  1719. {
  1720. $new = clone $this;
  1721. $new->_environment[$key] = $value;
  1722. $new->clearDetectorCache();
  1723. return $new;
  1724. }
  1725. /**
  1726. * Get/Set value from the request's environment data.
  1727. * Fallback to using env() if key not set in $environment property.
  1728. *
  1729. * @deprecated 3.5.0 Use getEnv()/withEnv() instead.
  1730. * @param string $key The key you want to read/write from/to.
  1731. * @param string|null $value Value to set. Default null.
  1732. * @param string|null $default Default value when trying to retrieve an environment
  1733. * variable's value that does not exist. The value parameter must be null.
  1734. * @return $this|string|null This instance if used as setter,
  1735. * if used as getter either the environment value, or null if the value doesn't exist.
  1736. */
  1737. public function env($key, $value = null, $default = null)
  1738. {
  1739. deprecationWarning(
  1740. 'ServerRequest::env() is deprecated. ' .
  1741. 'Use getEnv()/withEnv() instead.'
  1742. );
  1743. if ($value !== null) {
  1744. $this->_environment[$key] = $value;
  1745. $this->clearDetectorCache();
  1746. return $this;
  1747. }
  1748. $key = strtoupper($key);
  1749. if (!array_key_exists($key, $this->_environment)) {
  1750. $this->_environment[$key] = env($key);
  1751. }
  1752. return $this->_environment[$key] !== null ? $this->_environment[$key] : $default;
  1753. }
  1754. /**
  1755. * Allow only certain HTTP request methods, if the request method does not match
  1756. * a 405 error will be shown and the required "Allow" response header will be set.
  1757. *
  1758. * Example:
  1759. *
  1760. * $this->request->allowMethod('post');
  1761. * or
  1762. * $this->request->allowMethod(['post', 'delete']);
  1763. *
  1764. * If the request would be GET, response header "Allow: POST, DELETE" will be set
  1765. * and a 405 error will be returned.
  1766. *
  1767. * @param string|array $methods Allowed HTTP request methods.
  1768. * @return bool true
  1769. * @throws \Cake\Http\Exception\MethodNotAllowedException
  1770. */
  1771. public function allowMethod($methods)
  1772. {
  1773. $methods = (array)$methods;
  1774. foreach ($methods as $method) {
  1775. if ($this->is($method)) {
  1776. return true;
  1777. }
  1778. }
  1779. $allowed = strtoupper(implode(', ', $methods));
  1780. $e = new MethodNotAllowedException();
  1781. $e->responseHeader('Allow', $allowed);
  1782. throw $e;
  1783. }
  1784. /**
  1785. * Read data from php://input, mocked in tests.
  1786. *
  1787. * @return string contents of php://input
  1788. */
  1789. protected function _readInput()
  1790. {
  1791. if (empty($this->_input)) {
  1792. $fh = fopen('php://input', 'rb');
  1793. $content = stream_get_contents($fh);
  1794. fclose($fh);
  1795. $this->_input = $content;
  1796. }
  1797. return $this->_input;
  1798. }
  1799. /**
  1800. * Modify data originally from `php://input`. Useful for altering json/xml data
  1801. * in middleware or DispatcherFilters before it gets to RequestHandlerComponent
  1802. *
  1803. * @param string $input A string to replace original parsed data from input()
  1804. * @return void
  1805. * @deprecated 3.4.0 This method will be removed in 4.0.0. Use withBody() instead.
  1806. */
  1807. public function setInput($input)
  1808. {
  1809. deprecationWarning(
  1810. 'This method will be removed in 4.0.0.' .
  1811. 'Use withBody() instead.'
  1812. );
  1813. $stream = new Stream('php://memory', 'rw');
  1814. $stream->write($input);
  1815. $stream->rewind();
  1816. $this->stream = $stream;
  1817. }
  1818. /**
  1819. * Update the request with a new request data element.
  1820. *
  1821. * Returns an updated request object. This method returns
  1822. * a *new* request object and does not mutate the request in-place.
  1823. *
  1824. * Use `withParsedBody()` if you need to replace the all request data.
  1825. *
  1826. * @param string $name The dot separated path to insert $value at.
  1827. * @param mixed $value The value to insert into the request data.
  1828. * @return static
  1829. */
  1830. public function withData($name, $value)
  1831. {
  1832. $copy = clone $this;
  1833. $copy->data = Hash::insert($copy->data, $name, $value);
  1834. return $copy;
  1835. }
  1836. /**
  1837. * Update the request removing a data element.
  1838. *
  1839. * Returns an updated request object. This method returns
  1840. * a *new* request object and does not mutate the request in-place.
  1841. *
  1842. * @param string $name The dot separated path to remove.
  1843. * @return static
  1844. */
  1845. public function withoutData($name)
  1846. {
  1847. $copy = clone $this;
  1848. $copy->data = Hash::remove($copy->data, $name);
  1849. return $copy;
  1850. }
  1851. /**
  1852. * Update the request with a new routing parameter
  1853. *
  1854. * Returns an updated request object. This method returns
  1855. * a *new* request object and does not mutate the request in-place.
  1856. *
  1857. * @param string $name The dot separated path to insert $value at.
  1858. * @param mixed $value The value to insert into the the request parameters.
  1859. * @return static
  1860. */
  1861. public function withParam($name, $value)
  1862. {
  1863. $copy = clone $this;
  1864. $copy->params = Hash::insert($copy->params, $name, $value);
  1865. return $copy;
  1866. }
  1867. /**
  1868. * Safely access the values in $this->params.
  1869. *
  1870. * @param string $name The name or dotted path to parameter.
  1871. * @param mixed $default The default value if `$name` is not set. Default `false`.
  1872. * @return mixed
  1873. */
  1874. public function getParam($name, $default = false)
  1875. {
  1876. return Hash::get($this->params, $name, $default);
  1877. }
  1878. /**
  1879. * Return an instance with the specified request attribute.
  1880. *
  1881. * @param string $name The attribute name.
  1882. * @param mixed $value The value of the attribute.
  1883. * @return static
  1884. */
  1885. public function withAttribute($name, $value)
  1886. {
  1887. $new = clone $this;
  1888. if (in_array($name, $this->emulatedAttributes, true)) {
  1889. $new->{$name} = $value;
  1890. } else {
  1891. $new->attributes[$name] = $value;
  1892. }
  1893. return $new;
  1894. }
  1895. /**
  1896. * Return an instance without the specified request attribute.
  1897. *
  1898. * @param string $name The attribute name.
  1899. * @return static
  1900. * @throws \InvalidArgumentException
  1901. */
  1902. public function withoutAttribute($name)
  1903. {
  1904. $new = clone $this;
  1905. if (in_array($name, $this->emulatedAttributes, true)) {
  1906. throw new InvalidArgumentException(
  1907. "You cannot unset '$name'. It is a required CakePHP attribute."
  1908. );
  1909. }
  1910. unset($new->attributes[$name]);
  1911. return $new;
  1912. }
  1913. /**
  1914. * Read an attribute from the request, or get the default
  1915. *
  1916. * @param string $name The attribute name.
  1917. * @param mixed|null $default The default value if the attribute has not been set.
  1918. * @return mixed
  1919. */
  1920. public function getAttribute($name, $default = null)
  1921. {
  1922. if (in_array($name, $this->emulatedAttributes, true)) {
  1923. return $this->{$name};
  1924. }
  1925. if (array_key_exists($name, $this->attributes)) {
  1926. return $this->attributes[$name];
  1927. }
  1928. return $default;
  1929. }
  1930. /**
  1931. * Get all the attributes in the request.
  1932. *
  1933. * This will include the params, webroot, base, and here attributes that CakePHP
  1934. * provides.
  1935. *
  1936. * @return array
  1937. */
  1938. public function getAttributes()
  1939. {
  1940. $emulated = [
  1941. 'params' => $this->params,
  1942. 'webroot' => $this->webroot,
  1943. 'base' => $this->base,
  1944. 'here' => $this->here
  1945. ];
  1946. return $this->attributes + $emulated;
  1947. }
  1948. /**
  1949. * Get the uploaded file from a dotted path.
  1950. *
  1951. * @param string $path The dot separated path to the file you want.
  1952. * @return null|\Psr\Http\Message\UploadedFileInterface
  1953. */
  1954. public function getUploadedFile($path)
  1955. {
  1956. $file = Hash::get($this->uploadedFiles, $path);
  1957. if (!$file instanceof UploadedFile) {
  1958. return null;
  1959. }
  1960. return $file;
  1961. }
  1962. /**
  1963. * Get the array of uploaded files from the request.
  1964. *
  1965. * @return array
  1966. */
  1967. public function getUploadedFiles()
  1968. {
  1969. return $this->uploadedFiles;
  1970. }
  1971. /**
  1972. * Update the request replacing the files, and creating a new instance.
  1973. *
  1974. * @param array $files An array of uploaded file objects.
  1975. * @return static
  1976. * @throws \InvalidArgumentException when $files contains an invalid object.
  1977. */
  1978. public function withUploadedFiles(array $files)
  1979. {
  1980. $this->validateUploadedFiles($files, '');
  1981. $new = clone $this;
  1982. $new->uploadedFiles = $files;
  1983. return $new;
  1984. }
  1985. /**
  1986. * Recursively validate uploaded file data.
  1987. *
  1988. * @param array $uploadedFiles The new files array to validate.
  1989. * @param string $path The path thus far.
  1990. * @return void
  1991. * @throws \InvalidArgumentException If any leaf elements are not valid files.
  1992. */
  1993. protected function validateUploadedFiles(array $uploadedFiles, $path)
  1994. {
  1995. foreach ($uploadedFiles as $key => $file) {
  1996. if (is_array($file)) {
  1997. $this->validateUploadedFiles($file, $key . '.');
  1998. continue;
  1999. }
  2000. if (!$file instanceof UploadedFileInterface) {
  2001. throw new InvalidArgumentException("Invalid file at '{$path}{$key}'");
  2002. }
  2003. }
  2004. }
  2005. /**
  2006. * Gets the body of the message.
  2007. *
  2008. * @return \Psr\Http\Message\StreamInterface Returns the body as a stream.
  2009. */
  2010. public function getBody()
  2011. {
  2012. return $this->stream;
  2013. }
  2014. /**
  2015. * Return an instance with the specified message body.
  2016. *
  2017. * @param \Psr\Http\Message\StreamInterface $body The new request body
  2018. * @return static
  2019. */
  2020. public function withBody(StreamInterface $body)
  2021. {
  2022. $new = clone $this;
  2023. $new->stream = $body;
  2024. return $new;
  2025. }
  2026. /**
  2027. * Retrieves the URI instance.
  2028. *
  2029. * @return \Psr\Http\Message\UriInterface Returns a UriInterface instance
  2030. * representing the URI of the request.
  2031. */
  2032. public function getUri()
  2033. {
  2034. return $this->uri;
  2035. }
  2036. /**
  2037. * Return an instance with the specified uri
  2038. *
  2039. * *Warning* Replacing the Uri will not update the `base`, `webroot`,
  2040. * and `url` attributes.
  2041. *
  2042. * @param \Psr\Http\Message\UriInterface $uri The new request uri
  2043. * @param bool $preserveHost Whether or not the host should be retained.
  2044. * @return static
  2045. */
  2046. public function withUri(UriInterface $uri, $preserveHost = false)
  2047. {
  2048. $new = clone $this;
  2049. $new->uri = $uri;
  2050. if ($preserveHost && $this->hasHeader('Host')) {
  2051. return $new;
  2052. }
  2053. $host = $uri->getHost();
  2054. if (!$host) {
  2055. return $new;
  2056. }
  2057. if ($uri->getPort()) {
  2058. $host .= ':' . $uri->getPort();
  2059. }
  2060. $new->_environment['HTTP_HOST'] = $host;
  2061. return $new;
  2062. }
  2063. /**
  2064. * Create a new instance with a specific request-target.
  2065. *
  2066. * You can use this method to overwrite the request target that is
  2067. * inferred from the request's Uri. This also lets you change the request
  2068. * target's form to an absolute-form, authority-form or asterisk-form
  2069. *
  2070. * @link https://tools.ietf.org/html/rfc7230#section-2.7 (for the various
  2071. * request-target forms allowed in request messages)
  2072. * @param string $target The request target.
  2073. * @return static
  2074. */
  2075. public function withRequestTarget($target)
  2076. {
  2077. $new = clone $this;
  2078. $new->requestTarget = $target;
  2079. return $new;
  2080. }
  2081. /**
  2082. * Retrieves the request's target.
  2083. *
  2084. * Retrieves the message's request-target either as it was requested,
  2085. * or as set with `withRequestTarget()`. By default this will return the
  2086. * application relative path without base directory, and the query string
  2087. * defined in the SERVER environment.
  2088. *
  2089. * @return string
  2090. */
  2091. public function getRequestTarget()
  2092. {
  2093. if ($this->requestTarget !== null) {
  2094. return $this->requestTarget;
  2095. }
  2096. $target = $this->uri->getPath();
  2097. if ($this->uri->getQuery()) {
  2098. $target .= '?' . $this->uri->getQuery();
  2099. }
  2100. if (empty($target)) {
  2101. $target = '/';
  2102. }
  2103. return $target;
  2104. }
  2105. /**
  2106. * Get the path of current request.
  2107. *
  2108. * @return string
  2109. * @since 3.6.1
  2110. */
  2111. public function getPath()
  2112. {
  2113. if ($this->requestTarget === null) {
  2114. return $this->uri->getPath();
  2115. }
  2116. list($path) = explode('?', $this->requestTarget);
  2117. return $path;
  2118. }
  2119. /**
  2120. * Array access read implementation
  2121. *
  2122. * @param string $name Name of the key being accessed.
  2123. * @return mixed
  2124. * @deprecated 3.4.0 The ArrayAccess methods will be removed in 4.0.0. Use getParam(), getData() and getQuery() instead.
  2125. */
  2126. public function offsetGet($name)
  2127. {
  2128. deprecationWarning(
  2129. 'The ArrayAccess methods will be removed in 4.0.0.' .
  2130. 'Use getParam(), getData() and getQuery() instead.'
  2131. );
  2132. if (isset($this->params[$name])) {
  2133. return $this->params[$name];
  2134. }
  2135. if ($name === 'url') {
  2136. return $this->query;
  2137. }
  2138. if ($name === 'data') {
  2139. return $this->data;
  2140. }
  2141. return null;
  2142. }
  2143. /**
  2144. * Array access write implementation
  2145. *
  2146. * @param string $name Name of the key being written
  2147. * @param mixed $value The value being written.
  2148. * @return void
  2149. * @deprecated 3.4.0 The ArrayAccess methods will be removed in 4.0.0. Use withParam() instead.
  2150. */
  2151. public function offsetSet($name, $value)
  2152. {
  2153. deprecationWarning(
  2154. 'The ArrayAccess methods will be removed in 4.0.0.' .
  2155. 'Use withParam() instead.'
  2156. );
  2157. $this->params[$name] = $value;
  2158. }
  2159. /**
  2160. * Array access isset() implementation
  2161. *
  2162. * @param string $name thing to check.
  2163. * @return bool
  2164. * @deprecated 3.4.0 The ArrayAccess methods will be removed in 4.0.0. Use getParam() instead.
  2165. */
  2166. public function offsetExists($name)
  2167. {
  2168. deprecationWarning(
  2169. 'The ArrayAccess methods will be removed in 4.0.0.' .
  2170. 'Use getParam() instead.'
  2171. );
  2172. if ($name === 'url' || $name === 'data') {
  2173. return true;
  2174. }
  2175. return isset($this->params[$name]);
  2176. }
  2177. /**
  2178. * Array access unset() implementation
  2179. *
  2180. * @param string $name Name to unset.
  2181. * @return void
  2182. * @deprecated 3.4.0 The ArrayAccess methods will be removed in 4.0.0. Use withParam() instead.
  2183. */
  2184. public function offsetUnset($name)
  2185. {
  2186. deprecationWarning(
  2187. 'The ArrayAccess methods will be removed in 4.0.0.' .
  2188. 'Use withParam() instead.'
  2189. );
  2190. unset($this->params[$name]);
  2191. }
  2192. }
  2193. // @deprecated Add backwards compat alias.
  2194. class_alias('Cake\Http\ServerRequest', 'Cake\Network\Request');