ServerRequest.php 65 KB

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