1 | <?php |
||
10 | trait Server { |
||
11 | /** |
||
12 | * Uppercase method, GET by default |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | public $method; |
||
17 | /** |
||
18 | * The best guessed host |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | public $host; |
||
23 | /** |
||
24 | * Schema `http` or `https` |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | public $scheme; |
||
29 | /** |
||
30 | * Is requested with HTTPS |
||
31 | * |
||
32 | * @var bool |
||
33 | */ |
||
34 | public $secure; |
||
35 | /** |
||
36 | * Protocol, for instance: `HTTP/1.0`, `HTTP/1.1` (default), HTTP/2.0 |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | public $protocol; |
||
41 | /** |
||
42 | * Path |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | public $path; |
||
47 | /** |
||
48 | * URI, basically `$path?$query_string` (without `?` is query string is empty), `/` by default |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | public $uri; |
||
53 | /** |
||
54 | * Query string |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | public $query_string; |
||
59 | /** |
||
60 | * Where request came from, not necessary real IP of client, `127.0.0.1` by default |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | public $remote_addr; |
||
65 | /** |
||
66 | * The best guessed IP of client (based on all known headers), `$this->remote_addr` by default |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | public $ip; |
||
71 | /** |
||
72 | * Headers are normalized to lowercase keys with hyphen as separator, for instance: `connection`, `referer`, `content-type`, `accept-language` |
||
73 | * |
||
74 | * @var string[] |
||
75 | */ |
||
76 | public $headers; |
||
77 | /** |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected $cli; |
||
81 | /** |
||
82 | * @param string[] $server Typically `$_SERVER` |
||
83 | */ |
||
84 | 16 | function init_server ($server = []) { |
|
98 | /** |
||
99 | * @param string[] $server |
||
100 | */ |
||
101 | 16 | protected function fill_server_properties ($server) { |
|
102 | 16 | $this->cli = @$server['CLI'] === true; |
|
103 | 16 | $this->method = strtoupper($server['REQUEST_METHOD']); |
|
104 | 16 | $this->host = $this->host($server); |
|
105 | 16 | $this->secure = $this->secure($server); |
|
106 | 16 | $this->scheme = $this->secure ? 'https' : 'http'; |
|
107 | 16 | $this->protocol = $server['SERVER_PROTOCOL']; |
|
108 | 16 | $this->query_string = $server['QUERY_STRING']; |
|
109 | 16 | $this->uri = null_byte_filter(urldecode($server['REQUEST_URI'])) ?: '/'; |
|
110 | 16 | if (strpos($this->uri, '/index.php') === 0) { |
|
111 | $this->uri = substr($this->uri, 10); |
||
112 | } |
||
113 | 16 | $this->path = explode('?', $this->uri, 2)[0]; |
|
114 | 16 | $this->remote_addr = $server['REMOTE_ADDR']; |
|
115 | 16 | $this->ip = $this->ip($server); |
|
116 | 16 | } |
|
117 | /** |
||
118 | * @param string[] $server |
||
119 | */ |
||
120 | 16 | protected function fill_headers ($server) { |
|
133 | /** |
||
134 | * The best guessed IP of client (based on all known headers), `127.0.0.1` by default |
||
135 | * |
||
136 | * @param string[] $server |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 16 | protected function ip ($server) { |
|
159 | /** |
||
160 | * The best guessed host |
||
161 | * |
||
162 | * @param string[] $server |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | 16 | protected function host ($server) { |
|
194 | /** |
||
195 | * Secure protocol detection |
||
196 | * |
||
197 | * @param array $server |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | 16 | protected function secure ($server) { |
|
207 | /** |
||
208 | * Get header by name |
||
209 | * |
||
210 | * @param string $name Case-insensitive |
||
211 | * |
||
212 | * @return string Header content if exists or empty string otherwise |
||
213 | */ |
||
214 | 12 | function header ($name) { |
|
218 | } |
||
219 |