PageRenderTime 42ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Http/Request.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 912 lines | 398 code | 124 blank | 390 comment | 37 complexity | f13b3820f83c708ebbaa54f9de56bf1d MD5 | raw file
  1. <?php
  2. namespace Illuminate\Http;
  3. use Closure;
  4. use ArrayAccess;
  5. use SplFileInfo;
  6. use RuntimeException;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Str;
  9. use Symfony\Component\HttpFoundation\ParameterBag;
  10. use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
  11. class Request extends SymfonyRequest implements ArrayAccess
  12. {
  13. /**
  14. * The decoded JSON content for the request.
  15. *
  16. * @var string
  17. */
  18. protected $json;
  19. /**
  20. * The user resolver callback.
  21. *
  22. * @var \Closure
  23. */
  24. protected $userResolver;
  25. /**
  26. * The route resolver callback.
  27. *
  28. * @var \Closure
  29. */
  30. protected $routeResolver;
  31. /**
  32. * Create a new Illuminate HTTP request from server variables.
  33. *
  34. * @return static
  35. */
  36. public static function capture()
  37. {
  38. static::enableHttpMethodParameterOverride();
  39. return static::createFromBase(SymfonyRequest::createFromGlobals());
  40. }
  41. /**
  42. * Return the Request instance.
  43. *
  44. * @return $this
  45. */
  46. public function instance()
  47. {
  48. return $this;
  49. }
  50. /**
  51. * Get the request method.
  52. *
  53. * @return string
  54. */
  55. public function method()
  56. {
  57. return $this->getMethod();
  58. }
  59. /**
  60. * Get the root URL for the application.
  61. *
  62. * @return string
  63. */
  64. public function root()
  65. {
  66. return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/');
  67. }
  68. /**
  69. * Get the URL (no query string) for the request.
  70. *
  71. * @return string
  72. */
  73. public function url()
  74. {
  75. return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/');
  76. }
  77. /**
  78. * Get the full URL for the request.
  79. *
  80. * @return string
  81. */
  82. public function fullUrl()
  83. {
  84. $query = $this->getQueryString();
  85. return $query ? $this->url().'?'.$query : $this->url();
  86. }
  87. /**
  88. * Get the current path info for the request.
  89. *
  90. * @return string
  91. */
  92. public function path()
  93. {
  94. $pattern = trim($this->getPathInfo(), '/');
  95. return $pattern == '' ? '/' : $pattern;
  96. }
  97. /**
  98. * Get the current encoded path info for the request.
  99. *
  100. * @return string
  101. */
  102. public function decodedPath()
  103. {
  104. return rawurldecode($this->path());
  105. }
  106. /**
  107. * Get a segment from the URI (1 based index).
  108. *
  109. * @param int $index
  110. * @param string|null $default
  111. * @return string|null
  112. */
  113. public function segment($index, $default = null)
  114. {
  115. return Arr::get($this->segments(), $index - 1, $default);
  116. }
  117. /**
  118. * Get all of the segments for the request path.
  119. *
  120. * @return array
  121. */
  122. public function segments()
  123. {
  124. $segments = explode('/', $this->path());
  125. return array_values(array_filter($segments, function ($v) {
  126. return $v != '';
  127. }));
  128. }
  129. /**
  130. * Determine if the current request URI matches a pattern.
  131. *
  132. * @param mixed string
  133. * @return bool
  134. */
  135. public function is()
  136. {
  137. foreach (func_get_args() as $pattern) {
  138. if (Str::is($pattern, urldecode($this->path()))) {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. /**
  145. * Determine if the request is the result of an AJAX call.
  146. *
  147. * @return bool
  148. */
  149. public function ajax()
  150. {
  151. return $this->isXmlHttpRequest();
  152. }
  153. /**
  154. * Determine if the request is the result of an PJAX call.
  155. *
  156. * @return bool
  157. */
  158. public function pjax()
  159. {
  160. return $this->headers->get('X-PJAX') == true;
  161. }
  162. /**
  163. * Determine if the request is over HTTPS.
  164. *
  165. * @return bool
  166. */
  167. public function secure()
  168. {
  169. return $this->isSecure();
  170. }
  171. /**
  172. * Returns the client IP address.
  173. *
  174. * @return string
  175. */
  176. public function ip()
  177. {
  178. return $this->getClientIp();
  179. }
  180. /**
  181. * Returns the client IP addresses.
  182. *
  183. * @return array
  184. */
  185. public function ips()
  186. {
  187. return $this->getClientIps();
  188. }
  189. /**
  190. * Determine if the request contains a given input item key.
  191. *
  192. * @param string|array $key
  193. * @return bool
  194. */
  195. public function exists($key)
  196. {
  197. $keys = is_array($key) ? $key : func_get_args();
  198. $input = $this->all();
  199. foreach ($keys as $value) {
  200. if (! array_key_exists($value, $input)) {
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. /**
  207. * Determine if the request contains a non-empty value for an input item.
  208. *
  209. * @param string|array $key
  210. * @return bool
  211. */
  212. public function has($key)
  213. {
  214. $keys = is_array($key) ? $key : func_get_args();
  215. foreach ($keys as $value) {
  216. if ($this->isEmptyString($value)) {
  217. return false;
  218. }
  219. }
  220. return true;
  221. }
  222. /**
  223. * Determine if the given input key is an empty string for "has".
  224. *
  225. * @param string $key
  226. * @return bool
  227. */
  228. protected function isEmptyString($key)
  229. {
  230. $value = $this->input($key);
  231. $boolOrArray = is_bool($value) || is_array($value);
  232. return ! $boolOrArray && trim((string) $value) === '';
  233. }
  234. /**
  235. * Get all of the input and files for the request.
  236. *
  237. * @return array
  238. */
  239. public function all()
  240. {
  241. return array_replace_recursive($this->input(), $this->files->all());
  242. }
  243. /**
  244. * Retrieve an input item from the request.
  245. *
  246. * @param string $key
  247. * @param string|array|null $default
  248. * @return string|array
  249. */
  250. public function input($key = null, $default = null)
  251. {
  252. $input = $this->getInputSource()->all() + $this->query->all();
  253. return Arr::get($input, $key, $default);
  254. }
  255. /**
  256. * Get a subset of the items from the input data.
  257. *
  258. * @param array $keys
  259. * @return array
  260. */
  261. public function only($keys)
  262. {
  263. $keys = is_array($keys) ? $keys : func_get_args();
  264. $results = [];
  265. $input = $this->all();
  266. foreach ($keys as $key) {
  267. Arr::set($results, $key, Arr::get($input, $key));
  268. }
  269. return $results;
  270. }
  271. /**
  272. * Get all of the input except for a specified array of items.
  273. *
  274. * @param array|mixed $keys
  275. * @return array
  276. */
  277. public function except($keys)
  278. {
  279. $keys = is_array($keys) ? $keys : func_get_args();
  280. $results = $this->all();
  281. Arr::forget($results, $keys);
  282. return $results;
  283. }
  284. /**
  285. * Retrieve a query string item from the request.
  286. *
  287. * @param string $key
  288. * @param string|array|null $default
  289. * @return string|array
  290. */
  291. public function query($key = null, $default = null)
  292. {
  293. return $this->retrieveItem('query', $key, $default);
  294. }
  295. /**
  296. * Determine if a cookie is set on the request.
  297. *
  298. * @param string $key
  299. * @return bool
  300. */
  301. public function hasCookie($key)
  302. {
  303. return ! is_null($this->cookie($key));
  304. }
  305. /**
  306. * Retrieve a cookie from the request.
  307. *
  308. * @param string $key
  309. * @param string|array|null $default
  310. * @return string|array
  311. */
  312. public function cookie($key = null, $default = null)
  313. {
  314. return $this->retrieveItem('cookies', $key, $default);
  315. }
  316. /**
  317. * Retrieve a file from the request.
  318. *
  319. * @param string $key
  320. * @param mixed $default
  321. * @return \Symfony\Component\HttpFoundation\File\UploadedFile|array|null
  322. */
  323. public function file($key = null, $default = null)
  324. {
  325. return Arr::get($this->files->all(), $key, $default);
  326. }
  327. /**
  328. * Determine if the uploaded data contains a file.
  329. *
  330. * @param string $key
  331. * @return bool
  332. */
  333. public function hasFile($key)
  334. {
  335. if (! is_array($files = $this->file($key))) {
  336. $files = [$files];
  337. }
  338. foreach ($files as $file) {
  339. if ($this->isValidFile($file)) {
  340. return true;
  341. }
  342. }
  343. return false;
  344. }
  345. /**
  346. * Check that the given file is a valid file instance.
  347. *
  348. * @param mixed $file
  349. * @return bool
  350. */
  351. protected function isValidFile($file)
  352. {
  353. return $file instanceof SplFileInfo && $file->getPath() != '';
  354. }
  355. /**
  356. * Retrieve a header from the request.
  357. *
  358. * @param string $key
  359. * @param string|array|null $default
  360. * @return string|array
  361. */
  362. public function header($key = null, $default = null)
  363. {
  364. return $this->retrieveItem('headers', $key, $default);
  365. }
  366. /**
  367. * Retrieve a server variable from the request.
  368. *
  369. * @param string $key
  370. * @param string|array|null $default
  371. * @return string|array
  372. */
  373. public function server($key = null, $default = null)
  374. {
  375. return $this->retrieveItem('server', $key, $default);
  376. }
  377. /**
  378. * Retrieve an old input item.
  379. *
  380. * @param string $key
  381. * @param string|array|null $default
  382. * @return string|array
  383. */
  384. public function old($key = null, $default = null)
  385. {
  386. return $this->session()->getOldInput($key, $default);
  387. }
  388. /**
  389. * Flash the input for the current request to the session.
  390. *
  391. * @param string $filter
  392. * @param array $keys
  393. * @return void
  394. */
  395. public function flash($filter = null, $keys = [])
  396. {
  397. $flash = (! is_null($filter)) ? $this->$filter($keys) : $this->input();
  398. $this->session()->flashInput($flash);
  399. }
  400. /**
  401. * Flash only some of the input to the session.
  402. *
  403. * @param array|mixed $keys
  404. * @return void
  405. */
  406. public function flashOnly($keys)
  407. {
  408. $keys = is_array($keys) ? $keys : func_get_args();
  409. return $this->flash('only', $keys);
  410. }
  411. /**
  412. * Flash only some of the input to the session.
  413. *
  414. * @param array|mixed $keys
  415. * @return void
  416. */
  417. public function flashExcept($keys)
  418. {
  419. $keys = is_array($keys) ? $keys : func_get_args();
  420. return $this->flash('except', $keys);
  421. }
  422. /**
  423. * Flush all of the old input from the session.
  424. *
  425. * @return void
  426. */
  427. public function flush()
  428. {
  429. $this->session()->flashInput([]);
  430. }
  431. /**
  432. * Retrieve a parameter item from a given source.
  433. *
  434. * @param string $source
  435. * @param string $key
  436. * @param string|array|null $default
  437. * @return string|array
  438. */
  439. protected function retrieveItem($source, $key, $default)
  440. {
  441. if (is_null($key)) {
  442. return $this->$source->all();
  443. }
  444. return $this->$source->get($key, $default, true);
  445. }
  446. /**
  447. * Merge new input into the current request's input array.
  448. *
  449. * @param array $input
  450. * @return void
  451. */
  452. public function merge(array $input)
  453. {
  454. $this->getInputSource()->add($input);
  455. }
  456. /**
  457. * Replace the input for the current request.
  458. *
  459. * @param array $input
  460. * @return void
  461. */
  462. public function replace(array $input)
  463. {
  464. $this->getInputSource()->replace($input);
  465. }
  466. /**
  467. * Get the JSON payload for the request.
  468. *
  469. * @param string $key
  470. * @param mixed $default
  471. * @return mixed
  472. */
  473. public function json($key = null, $default = null)
  474. {
  475. if (! isset($this->json)) {
  476. $this->json = new ParameterBag((array) json_decode($this->getContent(), true));
  477. }
  478. if (is_null($key)) {
  479. return $this->json;
  480. }
  481. return Arr::get($this->json->all(), $key, $default);
  482. }
  483. /**
  484. * Get the input source for the request.
  485. *
  486. * @return \Symfony\Component\HttpFoundation\ParameterBag
  487. */
  488. protected function getInputSource()
  489. {
  490. if ($this->isJson()) {
  491. return $this->json();
  492. }
  493. return $this->getMethod() == 'GET' ? $this->query : $this->request;
  494. }
  495. /**
  496. * Determine if the given content types match.
  497. *
  498. * @param string $actual
  499. * @param string $type
  500. * @return bool
  501. */
  502. public static function matchesType($actual, $type)
  503. {
  504. if ($actual === $type) {
  505. return true;
  506. }
  507. $split = explode('/', $actual);
  508. return isset($split[1]) && preg_match('#'.preg_quote($split[0], '#').'/.+\+'.preg_quote($split[1], '#').'#', $type);
  509. }
  510. /**
  511. * Determine if the request is sending JSON.
  512. *
  513. * @return bool
  514. */
  515. public function isJson()
  516. {
  517. return Str::contains($this->header('CONTENT_TYPE'), ['/json', '+json']);
  518. }
  519. /**
  520. * Determine if the current request is asking for JSON in return.
  521. *
  522. * @return bool
  523. */
  524. public function wantsJson()
  525. {
  526. $acceptable = $this->getAcceptableContentTypes();
  527. return isset($acceptable[0]) && Str::contains($acceptable[0], ['/json', '+json']);
  528. }
  529. /**
  530. * Determines whether the current requests accepts a given content type.
  531. *
  532. * @param string|array $contentTypes
  533. * @return bool
  534. */
  535. public function accepts($contentTypes)
  536. {
  537. $accepts = $this->getAcceptableContentTypes();
  538. if (count($accepts) === 0) {
  539. return true;
  540. }
  541. $types = (array) $contentTypes;
  542. foreach ($accepts as $accept) {
  543. if ($accept === '*/*' || $accept === '*') {
  544. return true;
  545. }
  546. foreach ($types as $type) {
  547. if ($this->matchesType($accept, $type) || $accept === strtok($type, '/').'/*') {
  548. return true;
  549. }
  550. }
  551. }
  552. return false;
  553. }
  554. /**
  555. * Return the most suitable content type from the given array based on content negotiation.
  556. *
  557. * @param string|array $contentTypes
  558. * @return string|null
  559. */
  560. public function prefers($contentTypes)
  561. {
  562. $accepts = $this->getAcceptableContentTypes();
  563. $contentTypes = (array) $contentTypes;
  564. foreach ($accepts as $accept) {
  565. if (in_array($accept, ['*/*', '*'])) {
  566. return $contentTypes[0];
  567. }
  568. foreach ($contentTypes as $contentType) {
  569. $type = $contentType;
  570. if (! is_null($mimeType = $this->getMimeType($contentType))) {
  571. $type = $mimeType;
  572. }
  573. if ($this->matchesType($type, $accept) || $accept === strtok($type, '/').'/*') {
  574. return $contentType;
  575. }
  576. }
  577. }
  578. }
  579. /**
  580. * Determines whether a request accepts JSON.
  581. *
  582. * @return bool
  583. */
  584. public function acceptsJson()
  585. {
  586. return $this->accepts('application/json');
  587. }
  588. /**
  589. * Determines whether a request accepts HTML.
  590. *
  591. * @return bool
  592. */
  593. public function acceptsHtml()
  594. {
  595. return $this->accepts('text/html');
  596. }
  597. /**
  598. * Get the data format expected in the response.
  599. *
  600. * @param string $default
  601. * @return string
  602. */
  603. public function format($default = 'html')
  604. {
  605. foreach ($this->getAcceptableContentTypes() as $type) {
  606. if ($format = $this->getFormat($type)) {
  607. return $format;
  608. }
  609. }
  610. return $default;
  611. }
  612. /**
  613. * Create an Illuminate request from a Symfony instance.
  614. *
  615. * @param \Symfony\Component\HttpFoundation\Request $request
  616. * @return \Illuminate\Http\Request
  617. */
  618. public static function createFromBase(SymfonyRequest $request)
  619. {
  620. if ($request instanceof static) {
  621. return $request;
  622. }
  623. $content = $request->content;
  624. $request = (new static)->duplicate(
  625. $request->query->all(), $request->request->all(), $request->attributes->all(),
  626. $request->cookies->all(), $request->files->all(), $request->server->all()
  627. );
  628. $request->content = $content;
  629. $request->request = $request->getInputSource();
  630. return $request;
  631. }
  632. /**
  633. * {@inheritdoc}
  634. */
  635. public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
  636. {
  637. return parent::duplicate($query, $request, $attributes, $cookies, array_filter((array) $files), $server);
  638. }
  639. /**
  640. * Get the session associated with the request.
  641. *
  642. * @return \Illuminate\Session\Store
  643. *
  644. * @throws \RuntimeException
  645. */
  646. public function session()
  647. {
  648. if (! $this->hasSession()) {
  649. throw new RuntimeException('Session store not set on request.');
  650. }
  651. return $this->getSession();
  652. }
  653. /**
  654. * Get the user making the request.
  655. *
  656. * @return mixed
  657. */
  658. public function user()
  659. {
  660. return call_user_func($this->getUserResolver());
  661. }
  662. /**
  663. * Get the route handling the request.
  664. *
  665. * @param string|null $param
  666. *
  667. * @return \Illuminate\Routing\Route|object|string
  668. */
  669. public function route($param = null)
  670. {
  671. $route = call_user_func($this->getRouteResolver());
  672. if (is_null($route) || is_null($param)) {
  673. return $route;
  674. } else {
  675. return $route->parameter($param);
  676. }
  677. }
  678. /**
  679. * Get the user resolver callback.
  680. *
  681. * @return \Closure
  682. */
  683. public function getUserResolver()
  684. {
  685. return $this->userResolver ?: function () {
  686. //
  687. };
  688. }
  689. /**
  690. * Set the user resolver callback.
  691. *
  692. * @param \Closure $callback
  693. * @return $this
  694. */
  695. public function setUserResolver(Closure $callback)
  696. {
  697. $this->userResolver = $callback;
  698. return $this;
  699. }
  700. /**
  701. * Get the route resolver callback.
  702. *
  703. * @return \Closure
  704. */
  705. public function getRouteResolver()
  706. {
  707. return $this->routeResolver ?: function () {
  708. //
  709. };
  710. }
  711. /**
  712. * Set the route resolver callback.
  713. *
  714. * @param \Closure $callback
  715. * @return $this
  716. */
  717. public function setRouteResolver(Closure $callback)
  718. {
  719. $this->routeResolver = $callback;
  720. return $this;
  721. }
  722. /**
  723. * Determine if the given offset exists.
  724. *
  725. * @param string $offset
  726. * @return bool
  727. */
  728. public function offsetExists($offset)
  729. {
  730. return array_key_exists($offset, $this->all());
  731. }
  732. /**
  733. * Get the value at the given offset.
  734. *
  735. * @param string $offset
  736. * @return mixed
  737. */
  738. public function offsetGet($offset)
  739. {
  740. return Arr::get($this->all(), $offset);
  741. }
  742. /**
  743. * Set the value at the given offset.
  744. *
  745. * @param string $offset
  746. * @param mixed $value
  747. * @return void
  748. */
  749. public function offsetSet($offset, $value)
  750. {
  751. return $this->getInputSource()->set($offset, $value);
  752. }
  753. /**
  754. * Remove the value at the given offset.
  755. *
  756. * @param string $offset
  757. * @return void
  758. */
  759. public function offsetUnset($offset)
  760. {
  761. return $this->getInputSource()->remove($offset);
  762. }
  763. /**
  764. * Check if an input element is set on the request.
  765. *
  766. * @param string $key
  767. * @return bool
  768. */
  769. public function __isset($key)
  770. {
  771. return ! is_null($this->__get($key));
  772. }
  773. /**
  774. * Get an input element from the request.
  775. *
  776. * @param string $key
  777. * @return mixed
  778. */
  779. public function __get($key)
  780. {
  781. $all = $this->all();
  782. if (array_key_exists($key, $all)) {
  783. return $all[$key];
  784. } else {
  785. return $this->route($key);
  786. }
  787. }
  788. }