1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\HTTP\PhpEnvironment;
use Magento\Framework\Stdlib\Cookie\CookieReaderInterface;
use Magento\Framework\Stdlib\StringUtils;
use Zend\Http\Header\HeaderInterface;
use Zend\Stdlib\Parameters;
use Zend\Stdlib\ParametersInterface;
use Zend\Uri\UriFactory;
use Zend\Uri\UriInterface;
/**
* HTTP Request for current PHP environment.
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class Request extends \Zend\Http\PhpEnvironment\Request
{
/**#@+
* Protocols
*/
const SCHEME_HTTP = 'http';
const SCHEME_HTTPS = 'https';
/**#@-*/
// Configuration path for SSL Offload http header
const XML_PATH_OFFLOADER_HEADER = 'web/secure/offloader_header';
/**
* @var string
*/
protected $module;
/**
* @var string
*/
protected $controller;
/**
* @var string
*/
protected $action;
/**
* PATH_INFO
*
* @var string
*/
protected $pathInfo = '';
/**
* @var string
*/
protected $requestString = '';
/**
* Request parameters
*
* @var array
*/
protected $params = [];
/**
* @var array
*/
protected $aliases = [];
/**
* Has the action been dispatched?
*
* @var boolean
*/
protected $dispatched = false;
/**
* Flag for whether the request is forwarded or not
*
* @var bool
*/
protected $forwarded;
/**
* @var CookieReaderInterface
*/
protected $cookieReader;
/**
* @var StringUtils
*/
protected $converter;
/**
* @var \Magento\Framework\App\Config
*/
protected $appConfig;
/**
* Name of http header to check for ssl offloading default value is X-Forwarded-Proto
*
* @var string
*/
protected $sslOffloadHeader;
/**
* @param CookieReaderInterface $cookieReader
* @param StringUtils $converter
* @param UriInterface|string|null $uri
*/
public function __construct(
CookieReaderInterface $cookieReader,
StringUtils $converter,
$uri = null
) {
$this->cookieReader = $cookieReader;
if (null !== $uri) {
if (!$uri instanceof UriInterface) {
$uri = UriFactory::factory($uri);
}
if ($uri->isValid()) {
$path = $uri->getPath();
$query = $uri->getQuery();
if (!empty($query)) {
$path .= '?' . $query;
}
$this->setRequestUri($path);
} else {
throw new \InvalidArgumentException('Invalid URI provided to constructor');
}
}
$this->converter = $converter;
parent::__construct();
}
/**
* Retrieve the module name
*
* @return string
*/
public function getModuleName()
{
return $this->module;
}
/**
* Set the module name to use
*
* @param string $value
* @return $this
*/
public function setModuleName($value)
{
$this->module = $value;
return $this;
}
/**
* Retrieve the controller name
*
* @return string
*/
public function getControllerName()
{
return $this->controller;
}
/**
* Set the controller name to use
*
* @param string $value
* @return $this
*/
public function setControllerName($value)
{
$this->controller = $value;
return $this;
}
/**
* Retrieve the action name
*
* @return string
*/
public function getActionName()
{
return $this->action;
}
/**
* Set the action name
*
* @param string $value
* @return $this
*/
public function setActionName($value)
{
$this->action = $value;
return $this;
}
/**
* Returns everything between the BaseUrl and QueryString.
* This value is calculated instead of reading PATH_INFO
* directly from $_SERVER due to cross-platform differences.
*
* @return string
*/
public function getPathInfo()
{
if (empty($this->pathInfo)) {
$this->setPathInfo();
}
return $this->pathInfo;
}
/**
* Set the PATH_INFO string
*
* @param string|null $pathInfo
* @return $this
*/
public function setPathInfo($pathInfo = null)
{
if ($pathInfo === null) {
$requestUri = $this->getRequestUri();
if ('/' == $requestUri) {
return $this;
}
// Remove the query string from REQUEST_URI
$pos = strpos($requestUri, '?');
if ($pos) {
$requestUri = substr($requestUri, 0, $pos);
}
$baseUrl = $this->getBaseUrl();
$pathInfo = substr($requestUri, strlen($baseUrl));
if (!empty($baseUrl) && '/' === $pathInfo) {
$pathInfo = '';
} elseif (null === $baseUrl) {
$pathInfo = $requestUri;
}
$this->requestString = $pathInfo . ($pos !== false ? substr($requestUri, $pos) : '');
}
$this->pathInfo = (string)$pathInfo;
return $this;
}
/**
* Get request string
*
* @return string
*/
public function getRequestString()
{
return $this->requestString;
}
/**
* Retrieve an alias
*
* Retrieve the actual key represented by the alias $name.
*
* @param string $name
* @return string|null Returns null when no alias exists
*/
public function getAlias($name)
{
if (isset($this->aliases[$name])) {
return $this->aliases[$name];
}
return null;
}
/**
* Set a key alias
*
* Set an alias used for key lookups. $name specifies the alias, $target
* specifies the actual key to use.
*
* @param string $name
* @param string $target
* @return $this
*/
public function setAlias($name, $target)
{
$this->aliases[$name] = $target;
return $this;
}
/**
* Get an action parameter
*
* @param string $key
* @param mixed $default Default value to use if key not found
* @return mixed
*/
public function getParam($key, $default = null)
{
$key = (string) $key;
$keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
if (isset($this->params[$keyName])) {
return $this->params[$keyName];
} elseif (isset($this->queryParams[$keyName])) {
return $this->queryParams[$keyName];
} elseif (isset($this->postParams[$keyName])) {
return $this->postParams[$keyName];
}
return $default;
}
/**
* Set an action parameter
*
* A $value of null will unset the $key if it exists
*
* @param string $key
* @param mixed $value
* @return $this
*/
public function setParam($key, $value)
{
$key = (string) $key;
$keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
if ((null === $value) && isset($this->params[$keyName])) {
unset($this->params[$keyName]);
} elseif (null !== $value) {
$this->params[$keyName] = $value;
}
return $this;
}
/**
* Get all action parameters
*
* @return array
*/
public function getParams()
{
$params = $this->params;
if ($value = (array)$this->getQuery()) {
$params += $value;
}
if ($value = (array)$this->getPost()) {
$params += $value;
}
return $params;
}
/**
* Set action parameters en masse; does not overwrite
*
* Null values will unset the associated key.
*
* @param array $array
* @return $this
*/
public function setParams(array $array)
{
foreach ($array as $key => $value) {
$this->setParam($key, $value);
}
return $this;
}
/**
* Unset all user parameters
*
* @return $this
*/
public function clearParams()
{
$this->params = [];
return $this;
}
/**
* Get the request URI scheme
*
* @return string
*/
public function getScheme()
{
return $this->isSecure() ? self::SCHEME_HTTPS : self::SCHEME_HTTP;
}
/**
* Set flag indicating whether or not request has been dispatched
*
* @param boolean $flag
* @return $this
*/
public function setDispatched($flag = true)
{
$this->dispatched = $flag ? true : false;
return $this;
}
/**
* Determine if the request has been dispatched
*
* @return boolean
*/
public function isDispatched()
{
return $this->dispatched;
}
/**
* Is https secure request
*
* @return bool
*/
public function isSecure()
{
if ($this->immediateRequestSecure()) {
return true;
}
return $this->initialRequestSecure($this->getSslOffloadHeader());
}
/**
* Get value of SSL offload http header from configuration - defaults to X-Forwarded-Proto
*
* @return string
*/
private function getSslOffloadHeader()
{
// Lets read from db only one time okay.
if ($this->sslOffloadHeader === null) {
// @todo: Untangle Config dependence on Scope, so that this class can be instantiated even if app is not
// installed MAGETWO-31756
// Check if a proxy sent a header indicating an initial secure request
$this->sslOffloadHeader = trim(
(string)$this->getAppConfig()->getValue(
self::XML_PATH_OFFLOADER_HEADER,
\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT
)
);
}
return $this->sslOffloadHeader;
}
/**
* Create an instance of Magento\Framework\App\Config
*
* @return \Magento\Framework\App\Config
* @deprecated 100.1.0
*/
private function getAppConfig()
{
if ($this->appConfig == null) {
$this->appConfig =
\Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\App\Config::class);
}
return $this->appConfig;
}
/**
* Checks if the immediate request is delivered over HTTPS
*
* @return bool
*/
protected function immediateRequestSecure()
{
$https = $this->getServer('HTTPS');
$headerServerPort = $this->getServer('SERVER_PORT');
return (!empty($https) && $https != 'off') || $headerServerPort == 443;
}
/**
* In case there is a proxy server, checks if the initial request to the proxy was delivered over HTTPS
*
* @param string $offLoaderHeader
* @return bool
*/
protected function initialRequestSecure($offLoaderHeader)
{
// Transform http header to $_SERVER format ie X-Forwarded-Proto becomes $_SERVER['HTTP_X_FORWARDED_PROTO']
$offLoaderHeader = str_replace('-', '_', strtoupper($offLoaderHeader));
// Some webservers do not append HTTP_
$header = $this->getServer($offLoaderHeader);
// Apache appends HTTP_
$httpHeader = $this->getServer('HTTP_' . $offLoaderHeader);
return !empty($offLoaderHeader) && ($header === 'https' || $httpHeader === 'https');
}
/**
* Retrieve a value from a cookie.
*
* @param string|null $name
* @param string|null $default The default value to return if no value could be found for the given $name.
* @return string|null
*/
public function getCookie($name = null, $default = null)
{
return $this->cookieReader->getCookie($name, $default);
}
/**
* Retrieve SERVER parameters
*
* @param string $name
* @param mixed $default
* @return mixed|ParametersInterface
*/
public function getServerValue($name = null, $default = null)
{
$server = $this->getServer($name, $default);
if ($server instanceof ParametersInterface) {
return $server->toArray();
}
return $server;
}
/**
* Retrieve GET parameters
*
* @param string $name
* @param mixed $default
* @return mixed|ParametersInterface
*/
public function getQueryValue($name = null, $default = null)
{
$query = $this->getQuery($name, $default);
if ($query instanceof ParametersInterface) {
return $query->toArray();
}
return $query;
}
/**
* Set GET parameters
*
* @param string $name
* @param mixed $value
* @return $this
*/
public function setQueryValue($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $key => $value) {
$this->getQuery()->set($key, $value);
}
return $this;
}
$this->getQuery()->set($name, $value);
return $this;
}
/**
* Retrieve POST parameters
*
* @param string $name
* @param mixed $default
* @return mixed|ParametersInterface
*/
public function getPostValue($name = null, $default = null)
{
$post = $this->getPost($name, $default);
if ($post instanceof ParametersInterface) {
return $post->toArray();
}
return $post;
}
/**
* Set POST parameters
*
* @param string|array $name
* @param mixed $value
* @return $this
*/
public function setPostValue($name, $value = null)
{
if (is_array($name)) {
$this->setPost(new Parameters($name));
return $this;
}
$this->getPost()->set($name, $value);
return $this;
}
/**
* Access values contained in the superglobals as public members
*
* Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV
*
* @see http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx
* @param string $key
* @return mixed
*/
public function __get($key)
{
switch (true) {
case isset($this->params[$key]):
return $this->params[$key];
case isset($this->queryParams[$key]):
return $this->queryParams[$key];
case isset($this->postParams[$key]):
return $this->postParams[$key];
case isset($_COOKIE[$key]):
return $_COOKIE[$key];
case ($key == 'REQUEST_URI'):
return $this->getRequestUri();
case ($key == 'PATH_INFO'):
return $this->getPathInfo();
case isset($this->serverParams[$key]):
return $this->serverParams[$key];
case isset($this->envParams[$key]):
return $this->envParams[$key];
default:
return null;
}
}
/**
* Alias to __get
*
* @param string $key
* @return mixed
*/
public function get($key)
{
return $this->__get($key);
}
/**
* Check to see if a property is set
*
* @param string $key
* @return boolean
*/
public function __isset($key)
{
switch (true) {
case isset($this->params[$key]):
return true;
case isset($this->queryParams[$key]):
return true;
case isset($this->postParams[$key]):
return true;
case isset($_COOKIE[$key]):
return true;
case isset($this->serverParams[$key]):
return true;
case isset($this->envParams[$key]):
return true;
default:
return false;
}
}
/**
* Alias to __isset()
*
* @param string $key
* @return boolean
*/
public function has($key)
{
return $this->__isset($key);
}
/**
* Get all headers of a certain name/type.
*
* @param string $name Header name to retrieve.
* @param mixed|null $default Default value to use when the requested header is missing.
* @return bool|string
*/
public function getHeader($name, $default = false)
{
$header = parent::getHeader($name, $default);
if ($header instanceof HeaderInterface) {
return $header->getFieldValue();
}
return false;
}
/**
* Retrieve HTTP HOST
*
* @param bool $trimPort
* @return string
*
* @todo getHttpHost should return only string (currently method return boolean value too)
*/
public function getHttpHost($trimPort = true)
{
$httpHost = $this->getServer('HTTP_HOST');
$httpHost = $this->converter->cleanString($httpHost);
if (empty($httpHost)) {
return false;
}
if ($trimPort) {
$host = explode(':', $httpHost);
return $host[0];
}
return $httpHost;
}
/**
* Get the client's IP addres
*
* @param boolean $checkProxy
* @return string
*/
public function getClientIp($checkProxy = true)
{
if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
$ip = $this->getServer('HTTP_CLIENT_IP');
} elseif ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
$ip = $this->getServer('HTTP_X_FORWARDED_FOR');
} else {
$ip = $this->getServer('REMOTE_ADDR');
}
return $ip;
}
/**
* Retrieve only user params
*
* @return array
*/
public function getUserParams()
{
return $this->params;
}
/**
* Retrieve a single user param
*
* @param string $key
* @param string $default Default value to use if key not found
* @return mixed
*/
public function getUserParam($key, $default = null)
{
if (isset($this->params[$key])) {
return $this->params[$key];
}
return $default;
}
/**
* Set the REQUEST_URI
*
* @param string $requestUri
* @return $this
*/
public function setRequestUri($requestUri = null)
{
if ($requestUri === null) {
$requestUri = $this->detectRequestUri();
} elseif (!is_string($requestUri)) {
return $this;
} else {
if (false !== ($pos = strpos($requestUri, '?'))) {
$query = substr($requestUri, $pos + 1);
parse_str($query, $vars);
$this->setQueryValue($vars);
}
}
$this->requestUri = $requestUri;
return $this;
}
/**
* Get base url
*
* @return string
*/
public function getBaseUrl()
{
$url = urldecode(parent::getBaseUrl());
$url = str_replace('\\', '/', $url);
return $url;
}
/**
* Get flag value for whether the request is forwarded or not.
*
* @return bool
* @codeCoverageIgnore
*/
public function isForwarded()
{
return $this->forwarded;
}
/**
* Set flag value for whether the request is forwarded or not.
*
* @param bool $forwarded
* @return $this
* @codeCoverageIgnore
*/
public function setForwarded($forwarded)
{
$this->forwarded = $forwarded;
return $this;
}
}