Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Request.php
Go to the documentation of this file.
1 <?php
7 
10 use Zend\Http\Header\HeaderInterface;
11 use Zend\Stdlib\Parameters;
12 use Zend\Stdlib\ParametersInterface;
13 use Zend\Uri\UriFactory;
14 use Zend\Uri\UriInterface;
15 
19 class Request extends \Zend\Http\PhpEnvironment\Request
20 {
24  const SCHEME_HTTP = 'http';
25  const SCHEME_HTTPS = 'https';
28  // Configuration path for SSL Offload http header
29  const XML_PATH_OFFLOADER_HEADER = 'web/secure/offloader_header';
30 
34  protected $module;
35 
39  protected $controller;
40 
44  protected $action;
45 
51  protected $pathInfo = '';
52 
56  protected $requestString = '';
57 
63  protected $params = [];
64 
68  protected $aliases = [];
69 
75  protected $dispatched = false;
76 
82  protected $forwarded;
83 
87  protected $cookieReader;
88 
92  protected $converter;
93 
97  protected $appConfig;
98 
104  protected $sslOffloadHeader;
105 
111  public function __construct(
114  $uri = null
115  ) {
116  $this->cookieReader = $cookieReader;
117  if (null !== $uri) {
118  if (!$uri instanceof UriInterface) {
119  $uri = UriFactory::factory($uri);
120  }
121  if ($uri->isValid()) {
122  $path = $uri->getPath();
123  $query = $uri->getQuery();
124  if (!empty($query)) {
125  $path .= '?' . $query;
126  }
127  $this->setRequestUri($path);
128  } else {
129  throw new \InvalidArgumentException('Invalid URI provided to constructor');
130  }
131  }
132  $this->converter = $converter;
133  parent::__construct();
134  }
135 
141  public function getModuleName()
142  {
143  return $this->module;
144  }
145 
152  public function setModuleName($value)
153  {
154  $this->module = $value;
155  return $this;
156  }
157 
163  public function getControllerName()
164  {
165  return $this->controller;
166  }
167 
174  public function setControllerName($value)
175  {
176  $this->controller = $value;
177  return $this;
178  }
179 
185  public function getActionName()
186  {
187  return $this->action;
188  }
189 
196  public function setActionName($value)
197  {
198  $this->action = $value;
199  return $this;
200  }
201 
209  public function getPathInfo()
210  {
211  if (empty($this->pathInfo)) {
212  $this->setPathInfo();
213  }
214  return $this->pathInfo;
215  }
216 
223  public function setPathInfo($pathInfo = null)
224  {
225  if ($pathInfo === null) {
226  $requestUri = $this->getRequestUri();
227  if ('/' == $requestUri) {
228  return $this;
229  }
230 
231  // Remove the query string from REQUEST_URI
232  $pos = strpos($requestUri, '?');
233  if ($pos) {
234  $requestUri = substr($requestUri, 0, $pos);
235  }
236 
237  $baseUrl = $this->getBaseUrl();
238  $pathInfo = substr($requestUri, strlen($baseUrl));
239  if (!empty($baseUrl) && '/' === $pathInfo) {
240  $pathInfo = '';
241  } elseif (null === $baseUrl) {
242  $pathInfo = $requestUri;
243  }
244  $this->requestString = $pathInfo . ($pos !== false ? substr($requestUri, $pos) : '');
245  }
246  $this->pathInfo = (string)$pathInfo;
247  return $this;
248  }
249 
255  public function getRequestString()
256  {
257  return $this->requestString;
258  }
259 
268  public function getAlias($name)
269  {
270  if (isset($this->aliases[$name])) {
271  return $this->aliases[$name];
272  }
273  return null;
274  }
275 
286  public function setAlias($name, $target)
287  {
288  $this->aliases[$name] = $target;
289  return $this;
290  }
291 
299  public function getParam($key, $default = null)
300  {
301  $key = (string) $key;
302  $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
303  if (isset($this->params[$keyName])) {
304  return $this->params[$keyName];
305  } elseif (isset($this->queryParams[$keyName])) {
306  return $this->queryParams[$keyName];
307  } elseif (isset($this->postParams[$keyName])) {
308  return $this->postParams[$keyName];
309  }
310  return $default;
311  }
312 
322  public function setParam($key, $value)
323  {
324  $key = (string) $key;
325  $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
326  if ((null === $value) && isset($this->params[$keyName])) {
327  unset($this->params[$keyName]);
328  } elseif (null !== $value) {
329  $this->params[$keyName] = $value;
330  }
331  return $this;
332  }
333 
339  public function getParams()
340  {
342  if ($value = (array)$this->getQuery()) {
343  $params += $value;
344  }
345  if ($value = (array)$this->getPost()) {
346  $params += $value;
347  }
348  return $params;
349  }
350 
359  public function setParams(array $array)
360  {
361  foreach ($array as $key => $value) {
362  $this->setParam($key, $value);
363  }
364  return $this;
365  }
366 
372  public function clearParams()
373  {
374  $this->params = [];
375  return $this;
376  }
377 
383  public function getScheme()
384  {
385  return $this->isSecure() ? self::SCHEME_HTTPS : self::SCHEME_HTTP;
386  }
387 
394  public function setDispatched($flag = true)
395  {
396  $this->dispatched = $flag ? true : false;
397  return $this;
398  }
399 
405  public function isDispatched()
406  {
407  return $this->dispatched;
408  }
409 
415  public function isSecure()
416  {
417  if ($this->immediateRequestSecure()) {
418  return true;
419  }
420 
421  return $this->initialRequestSecure($this->getSslOffloadHeader());
422  }
423 
429  private function getSslOffloadHeader()
430  {
431  // Lets read from db only one time okay.
432  if ($this->sslOffloadHeader === null) {
433  // @todo: Untangle Config dependence on Scope, so that this class can be instantiated even if app is not
434  // installed MAGETWO-31756
435  // Check if a proxy sent a header indicating an initial secure request
436  $this->sslOffloadHeader = trim(
437  (string)$this->getAppConfig()->getValue(
438  self::XML_PATH_OFFLOADER_HEADER,
439  \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT
440  )
441  );
442  }
443 
445  }
446 
453  private function getAppConfig()
454  {
455  if ($this->appConfig == null) {
456  $this->appConfig =
457  \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\App\Config::class);
458  }
459  return $this->appConfig;
460  }
461 
467  protected function immediateRequestSecure()
468  {
469  $https = $this->getServer('HTTPS');
470  $headerServerPort = $this->getServer('SERVER_PORT');
471  return (!empty($https) && $https != 'off') || $headerServerPort == 443;
472  }
473 
480  protected function initialRequestSecure($offLoaderHeader)
481  {
482  // Transform http header to $_SERVER format ie X-Forwarded-Proto becomes $_SERVER['HTTP_X_FORWARDED_PROTO']
483  $offLoaderHeader = str_replace('-', '_', strtoupper($offLoaderHeader));
484  // Some webservers do not append HTTP_
485  $header = $this->getServer($offLoaderHeader);
486  // Apache appends HTTP_
487  $httpHeader = $this->getServer('HTTP_' . $offLoaderHeader);
488  return !empty($offLoaderHeader) && ($header === 'https' || $httpHeader === 'https');
489  }
490 
498  public function getCookie($name = null, $default = null)
499  {
500  return $this->cookieReader->getCookie($name, $default);
501  }
502 
510  public function getServerValue($name = null, $default = null)
511  {
512  $server = $this->getServer($name, $default);
513  if ($server instanceof ParametersInterface) {
514  return $server->toArray();
515  }
516  return $server;
517  }
518 
526  public function getQueryValue($name = null, $default = null)
527  {
528  $query = $this->getQuery($name, $default);
529  if ($query instanceof ParametersInterface) {
530  return $query->toArray();
531  }
532  return $query;
533  }
534 
542  public function setQueryValue($name, $value = null)
543  {
544  if (is_array($name)) {
545  foreach ($name as $key => $value) {
546  $this->getQuery()->set($key, $value);
547  }
548  return $this;
549  }
550  $this->getQuery()->set($name, $value);
551  return $this;
552  }
553 
561  public function getPostValue($name = null, $default = null)
562  {
563  $post = $this->getPost($name, $default);
564  if ($post instanceof ParametersInterface) {
565  return $post->toArray();
566  }
567  return $post;
568  }
569 
577  public function setPostValue($name, $value = null)
578  {
579  if (is_array($name)) {
580  $this->setPost(new Parameters($name));
581  return $this;
582  }
583  $this->getPost()->set($name, $value);
584  return $this;
585  }
586 
595  public function __get($key)
596  {
597  switch (true) {
598  case isset($this->params[$key]):
599  return $this->params[$key];
600 
601  case isset($this->queryParams[$key]):
602  return $this->queryParams[$key];
603 
604  case isset($this->postParams[$key]):
605  return $this->postParams[$key];
606 
607  case isset($_COOKIE[$key]):
608  return $_COOKIE[$key];
609 
610  case ($key == 'REQUEST_URI'):
611  return $this->getRequestUri();
612 
613  case ($key == 'PATH_INFO'):
614  return $this->getPathInfo();
615 
616  case isset($this->serverParams[$key]):
617  return $this->serverParams[$key];
618 
619  case isset($this->envParams[$key]):
620  return $this->envParams[$key];
621 
622  default:
623  return null;
624  }
625  }
626 
633  public function get($key)
634  {
635  return $this->__get($key);
636  }
637 
644  public function __isset($key)
645  {
646  switch (true) {
647  case isset($this->params[$key]):
648  return true;
649 
650  case isset($this->queryParams[$key]):
651  return true;
652 
653  case isset($this->postParams[$key]):
654  return true;
655 
656  case isset($_COOKIE[$key]):
657  return true;
658 
659  case isset($this->serverParams[$key]):
660  return true;
661 
662  case isset($this->envParams[$key]):
663  return true;
664 
665  default:
666  return false;
667  }
668  }
669 
676  public function has($key)
677  {
678  return $this->__isset($key);
679  }
680 
688  public function getHeader($name, $default = false)
689  {
690  $header = parent::getHeader($name, $default);
691  if ($header instanceof HeaderInterface) {
692  return $header->getFieldValue();
693  }
694  return false;
695  }
696 
705  public function getHttpHost($trimPort = true)
706  {
707  $httpHost = $this->getServer('HTTP_HOST');
708  $httpHost = $this->converter->cleanString($httpHost);
709  if (empty($httpHost)) {
710  return false;
711  }
712  if ($trimPort) {
713  $host = explode(':', $httpHost);
714  return $host[0];
715  }
716  return $httpHost;
717  }
718 
725  public function getClientIp($checkProxy = true)
726  {
727  if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
728  $ip = $this->getServer('HTTP_CLIENT_IP');
729  } elseif ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
730  $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
731  } else {
732  $ip = $this->getServer('REMOTE_ADDR');
733  }
734  return $ip;
735  }
736 
742  public function getUserParams()
743  {
744  return $this->params;
745  }
746 
754  public function getUserParam($key, $default = null)
755  {
756  if (isset($this->params[$key])) {
757  return $this->params[$key];
758  }
759  return $default;
760  }
761 
768  public function setRequestUri($requestUri = null)
769  {
770  if ($requestUri === null) {
771  $requestUri = $this->detectRequestUri();
772  } elseif (!is_string($requestUri)) {
773  return $this;
774  } else {
775  if (false !== ($pos = strpos($requestUri, '?'))) {
776  $query = substr($requestUri, $pos + 1);
777  parse_str($query, $vars);
778  $this->setQueryValue($vars);
779  }
780  }
781  $this->requestUri = $requestUri;
782  return $this;
783  }
784 
790  public function getBaseUrl()
791  {
792  $url = urldecode(parent::getBaseUrl());
793  $url = str_replace('\\', '/', $url);
794  return $url;
795  }
796 
801  public function isForwarded()
802  {
803  return $this->forwarded;
804  }
805 
811  public function setForwarded($forwarded)
812  {
813  $this->forwarded = $forwarded;
814  return $this;
815  }
816 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getQueryValue($name=null, $default=null)
Definition: Request.php:526
$target
Definition: skip.phtml:8
getServerValue($name=null, $default=null)
Definition: Request.php:510
getPostValue($name=null, $default=null)
Definition: Request.php:561
$value
Definition: gender.phtml:16
__construct(CookieReaderInterface $cookieReader, StringUtils $converter, $uri=null)
Definition: Request.php:111
$pos
Definition: list.phtml:42
getCookie($name=null, $default=null)
Definition: Request.php:498
if(!trim($html)) $alias
Definition: details.phtml:20
if(!isset($_GET['name'])) $name
Definition: log.php:14