|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WebServCo\Framework\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use WebServCo\Framework\Helpers\PhpHelper; |
|
8
|
|
|
|
|
9
|
|
|
trait RequestServerTrait |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @return array<string,string> |
|
13
|
|
|
*/ |
|
14
|
|
|
public function getAcceptContentTypes(): array |
|
15
|
|
|
{ |
|
16
|
|
|
if (!isset($this->server['HTTP_ACCEPT'])) { |
|
17
|
|
|
return []; |
|
18
|
|
|
} |
|
19
|
|
|
$acceptTypes = []; |
|
20
|
|
|
$httpAccept = \strtolower(\str_replace(' ', '', $this->server['HTTP_ACCEPT'])); |
|
21
|
|
|
$parts = \explode(',', $httpAccept); |
|
22
|
|
|
|
|
23
|
|
|
foreach ($parts as $item) { |
|
24
|
|
|
$q = 1; // the default quality is 1. |
|
25
|
|
|
if (\strpos($item, ';q=')) { // check if there is a different quality |
|
26
|
|
|
// divide "mime/type;q=X" into two parts: "mime/type" i "X" |
|
27
|
|
|
[$item, $q] = \explode(';q=', $item); |
|
28
|
|
|
} |
|
29
|
|
|
// Make sure key is string, otherwise it is cast by PHP to int and possibly overwritten. |
|
30
|
|
|
// WARNING: $q == 0 means, that mime-type isn’t supported! |
|
31
|
|
|
$acceptTypes[\sprintf('q=%s', $q)] = $item; |
|
32
|
|
|
} |
|
33
|
|
|
\asort($acceptTypes); |
|
34
|
|
|
return $acceptTypes; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getAcceptLanguage(): string |
|
38
|
|
|
{ |
|
39
|
|
|
if (!isset($this->server['HTTP_ACCEPT_LANGUAGE'])) { |
|
40
|
|
|
return ''; |
|
41
|
|
|
} |
|
42
|
|
|
return \substr($this->server['HTTP_ACCEPT_LANGUAGE'], 0, 2); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getContentType(): string |
|
46
|
|
|
{ |
|
47
|
|
|
if (!isset($this->server['CONTENT_TYPE'])) { |
|
48
|
|
|
return ''; |
|
49
|
|
|
} |
|
50
|
|
|
return $this->server['CONTENT_TYPE']; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getHost(): string |
|
54
|
|
|
{ |
|
55
|
|
|
if (!empty($this->server['HTTP_HOST'])) { |
|
56
|
|
|
return $this->server['HTTP_HOST']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!empty($this->server['SERVER_NAME'])) { |
|
60
|
|
|
return $this->server['SERVER_NAME']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!empty($this->server['HOSTNAME'])) { |
|
64
|
|
|
return $this->server['HOSTNAME']; //CLI |
|
65
|
|
|
} |
|
66
|
|
|
return ''; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getHostExtension(): string |
|
70
|
|
|
{ |
|
71
|
|
|
$host = $this->getHost(); |
|
72
|
|
|
if (empty($host)) { |
|
73
|
|
|
return ''; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$parts = \explode('.', $host); |
|
77
|
|
|
return \end($parts); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getReferer(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->server['HTTP_REFERER'] ?? ''; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get remote address from current Request object. |
|
87
|
|
|
* |
|
88
|
|
|
* For a general helper method see \WebServCo\Framework\Helpers\RequestHelper.getRemoteAddress |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getRemoteAddress(): string |
|
91
|
|
|
{ |
|
92
|
|
|
if (PhpHelper::isCli()) { |
|
93
|
|
|
return \gethostbyname(\php_uname('n')); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->server['REMOTE_ADDR'] ?? ''; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getRefererHost(): string |
|
100
|
|
|
{ |
|
101
|
|
|
return (string) \parse_url($this->getReferer(), \PHP_URL_HOST); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getSchema(): string |
|
105
|
|
|
{ |
|
106
|
|
|
if (PhpHelper::isCli()) { |
|
107
|
|
|
return ''; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (isset($this->server['HTTPS']) && 'off' !== $this->server['HTTPS']) { |
|
111
|
|
|
return 'https'; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (isset($this->server['HTTP_X_FORWARDED_PROTO']) && 'https' === $this->server['HTTP_X_FORWARDED_PROTO']) { |
|
115
|
|
|
return 'https'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if (isset($this->server['HTTP_X_FORWARDED_SSL']) && 'on' === $this->server['HTTP_X_FORWARDED_SSL']) { |
|
119
|
|
|
return 'https'; |
|
120
|
|
|
} |
|
121
|
|
|
return 'http'; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getServerProtocol(): string |
|
125
|
|
|
{ |
|
126
|
|
|
if (!isset($this->server['SERVER_PROTOCOL'])) { |
|
127
|
|
|
return ''; |
|
128
|
|
|
} |
|
129
|
|
|
return $this->server['SERVER_PROTOCOL']; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function getServerVariable(string $index): string |
|
133
|
|
|
{ |
|
134
|
|
|
if (!\array_key_exists($index, $this->server)) { |
|
135
|
|
|
throw new \OutOfBoundsException('Requested key does not exist.'); |
|
136
|
|
|
} |
|
137
|
|
|
return $this->server[$index]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function getUserAgent(): string |
|
141
|
|
|
{ |
|
142
|
|
|
if (PhpHelper::isCli()) { |
|
143
|
|
|
return ''; |
|
144
|
|
|
} |
|
145
|
|
|
return $this->server['HTTP_USER_AGENT'] ?? ''; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|