1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZoiloMora\ElasticAPM\Events\Common\Request; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Socket |
7
|
|
|
*/ |
8
|
|
|
final class Socket implements \JsonSerializable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Indicates whether request was sent as SSL/HTTPS request. |
12
|
|
|
* |
13
|
|
|
* @var bool|null |
14
|
|
|
*/ |
15
|
|
|
private $encrypted; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The network address sending the request. |
19
|
|
|
* Should be obtained through standard APIs and not parsed from any headers like 'Forwarded'. |
20
|
|
|
* |
21
|
|
|
* @var string|null |
22
|
|
|
*/ |
23
|
|
|
private $remoteAddress; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param bool|null $encrypted |
27
|
|
|
* @param string|null $remoteAddress |
28
|
|
|
*/ |
29
|
12 |
|
public function __construct($encrypted = null, $remoteAddress = null) |
30
|
|
|
{ |
31
|
12 |
|
$this->encrypted = $encrypted; |
32
|
12 |
|
$this->remoteAddress = $remoteAddress; |
33
|
12 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return bool|null |
37
|
|
|
*/ |
38
|
3 |
|
public function encrypted() |
39
|
|
|
{ |
40
|
3 |
|
return $this->encrypted; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string|null |
45
|
|
|
*/ |
46
|
4 |
|
public function remoteAddress() |
47
|
|
|
{ |
48
|
4 |
|
return $this->remoteAddress; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Socket |
53
|
|
|
*/ |
54
|
9 |
|
public static function discover() |
55
|
|
|
{ |
56
|
9 |
|
return new self( |
57
|
9 |
|
self::isEncrypted(), |
58
|
9 |
|
self::getRemoteAddress() |
59
|
9 |
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
9 |
|
private static function isEncrypted() |
66
|
|
|
{ |
67
|
9 |
|
if (false === array_key_exists('HTTPS', $_SERVER)) { |
68
|
8 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return 'off' !== $_SERVER['HTTPS']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string|null |
76
|
|
|
*/ |
77
|
9 |
|
private static function getRemoteAddress() |
78
|
|
|
{ |
79
|
9 |
|
$remoteAddress = null; |
80
|
|
|
|
81
|
9 |
|
if (true === array_key_exists('REMOTE_ADDR', $_SERVER)) { |
82
|
2 |
|
$remoteAddress = $_SERVER['REMOTE_ADDR']; |
83
|
2 |
|
} |
84
|
|
|
|
85
|
9 |
|
if (true === array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) { |
86
|
1 |
|
$remoteAddress = $_SERVER['HTTP_X_FORWARDED_FOR']; |
87
|
1 |
|
} |
88
|
|
|
|
89
|
9 |
|
return null === $remoteAddress |
90
|
9 |
|
? null |
91
|
9 |
|
: (string) $remoteAddress; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
1 |
|
public function jsonSerialize() |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
1 |
|
'encrypted' => $this->encrypted, |
101
|
1 |
|
'remote_address' => $this->remoteAddress, |
102
|
1 |
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|