ServerRequest.php 65 KB

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