1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW; |
4
|
|
|
|
5
|
|
|
class Request |
6
|
|
|
{ |
7
|
|
|
protected static $instance = null; |
8
|
|
|
|
9
|
|
|
protected $ip; |
10
|
|
|
|
11
|
|
|
protected $lang; |
12
|
|
|
|
13
|
|
|
protected $referer; |
14
|
|
|
|
15
|
|
|
protected $method; |
16
|
|
|
|
17
|
|
|
protected $ssl; |
18
|
|
|
|
19
|
|
|
protected $request; |
20
|
|
|
|
21
|
|
|
protected function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->detectIp(); |
24
|
|
|
$this->detectLang(); |
25
|
|
|
$this->detectReferer(); |
26
|
|
|
$this->detectMethod(); |
27
|
|
|
$this->detectSsl(); |
28
|
|
|
$this->detectRequest(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function getInstance() |
32
|
|
|
{ |
33
|
|
|
if (self::$instance === null) { |
34
|
|
|
self::$instance = new self; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return self::$instance; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getIp() |
41
|
|
|
{ |
42
|
|
|
return $this->ip; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getLang() |
46
|
|
|
{ |
47
|
|
|
return $this->lang; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getReferer() |
51
|
|
|
{ |
52
|
|
|
return $this->referer; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getMethod() |
56
|
|
|
{ |
57
|
|
|
return $this->method; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getSsl() |
61
|
|
|
{ |
62
|
|
|
return $this->ssl; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getRequest() |
66
|
|
|
{ |
67
|
|
|
return $this->request; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getServerVar($keyName) |
71
|
|
|
{ |
72
|
|
|
if (!isset($_SERVER[$keyName])) { |
73
|
|
|
return ''; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $_SERVER[$keyName]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function detectIp() |
80
|
|
|
{ |
81
|
|
|
$this->ip = $this->getServerVar('REMOTE_ADDR'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function detectLang() |
85
|
|
|
{ |
86
|
|
|
/* |
87
|
|
|
* HTTP_ACCEPT_LANGUAGE -> fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 |
88
|
|
|
* First "fr-FR" (preference 1/1) |
89
|
|
|
* After in the order, "fr" (preference 0.8 / 1) |
90
|
|
|
* Next "en-US" (preference 0.6/1) |
91
|
|
|
* End "en" (preference 0.4/1) |
92
|
|
|
**/ |
93
|
|
|
|
94
|
|
|
$acceptLang = $this->getServerVar('HTTP_ACCEPT_LANGUAGE'); |
95
|
|
|
$acceptLangs = explode(',', $acceptLang); |
96
|
|
|
|
97
|
|
|
$firstLang = explode(';', $acceptLangs[0]); |
98
|
|
|
$lang = strtolower($firstLang[0]); |
99
|
|
|
|
100
|
|
|
if (strpos($lang, '-') !== false) { |
101
|
|
|
$minLang = explode('-', $lang); |
102
|
|
|
$lang = $minLang[0]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->lang = $lang; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function detectReferer() |
109
|
|
|
{ |
110
|
|
|
$this->referer = $this->getServerVar('HTTP_REFERER'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function detectMethod() |
114
|
|
|
{ |
115
|
|
|
$this->method = $this->getServerVar('REQUEST_METHOD'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function detectSsl() |
119
|
|
|
{ |
120
|
|
|
$serverHttps = $this->getServerVar('HTTPS'); |
121
|
|
|
$fwdProto = $this->getServerVar('HTTP_X_FORWARDED_PROTO'); |
122
|
|
|
$fwdSsl = $this->getServerVar('HTTP_X_FORWARDED_SSL'); |
123
|
|
|
|
124
|
|
|
$this->ssl = false; |
125
|
|
|
|
126
|
|
|
if (!empty($serverHttps) && $serverHttps !== 'off') { |
127
|
|
|
$this->ssl = true; |
128
|
|
|
} elseif (!empty($fwdProto) && $fwdProto === 'https') { |
129
|
|
|
$this->ssl = true; |
130
|
|
|
} elseif (!empty($fwdSsl) && $fwdSsl === 'on') { |
131
|
|
|
$this->ssl = true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function detectRequest() |
136
|
|
|
{ |
137
|
|
|
$parseUrl = parse_url($this->getServerVar('REQUEST_URI')); |
138
|
|
|
|
139
|
|
|
$this->request = [ |
140
|
|
|
'scheme' => '', |
141
|
|
|
'host' => $this->getServerVar('HTTP_HOST'), |
142
|
|
|
'port' => '', |
143
|
|
|
'user' => '', |
144
|
|
|
'pass' => '', |
145
|
|
|
'path' => '', |
146
|
|
|
'query' => '', |
147
|
|
|
'fragment' => '', |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
$this->request = (object) array_merge($parseUrl, $this->request); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|