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