1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Router; |
4
|
|
|
|
5
|
|
|
class Server |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
protected const PROTOCOL_HTTPS = 'https'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $input = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param string $path |
17
|
|
|
* @param null|string $host |
18
|
|
|
* @param null|string $protocol |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
7 |
|
public static function url(string $path, ?string $host = null, ?string $protocol = null): string |
22
|
|
|
{ |
23
|
7 |
|
$scheme = $protocol ?: static::sharedInstance()->protocol(); |
24
|
7 |
|
$host = $host ?? static::sharedInstance()->host(); |
25
|
7 |
|
return $scheme . '://' . $host . $path; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $default |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
5 |
|
public function protocol(string $default = self::PROTOCOL_HTTPS): string |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* PHP https check with flexible ssl (CloudFlare), how to do? |
36
|
|
|
* |
37
|
|
|
* @see https://stackoverflow.com/a/42387790 |
38
|
|
|
*/ |
39
|
5 |
|
$visitor = $this->get('HTTP_CF_VISITOR'); |
40
|
5 |
|
if ($visitor) { |
41
|
|
|
$data = \json_decode($visitor, true); |
42
|
|
|
return $data['scheme'] ?? $default; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* PHP Group guidelines |
47
|
|
|
*/ |
48
|
5 |
|
$https = $this->get('HTTPS', 'off'); |
49
|
5 |
|
$port = (int)$this->get('SERVER_PORT', 80); |
50
|
5 |
|
if ($https === 'on' || $port === 443) { |
51
|
|
|
return static::PROTOCOL_HTTPS; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check other popular keys |
56
|
|
|
*/ |
57
|
5 |
|
return $this->get( |
58
|
5 |
|
'HTTP_X_FORWARDED_PROTO', |
59
|
5 |
|
$this->get('REQUEST_SCHEME', $default) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Server |
65
|
|
|
*/ |
66
|
5 |
|
public static function sharedInstance(): self |
67
|
|
|
{ |
68
|
5 |
|
static $self; |
69
|
5 |
|
if (!$self) { |
70
|
1 |
|
$self = new static(); |
71
|
|
|
} |
72
|
5 |
|
return $self; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
5 |
|
public function host(): string |
79
|
|
|
{ |
80
|
5 |
|
return $this->get('HTTP_HOST', PHP_SAPI); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $default |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
5 |
|
public function method(string $default = 'GET'): string |
89
|
|
|
{ |
90
|
5 |
|
return $this->get('REQUEST_METHOD', $default); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $name |
95
|
|
|
* @param null|string $default |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
5 |
|
public function get(string $name, ?string $default = null): ?string |
99
|
|
|
{ |
100
|
5 |
|
if (empty($this->input[$name])) { |
101
|
5 |
|
$this->input[$name] = \filter_input(INPUT_SERVER, $name, \FILTER_DEFAULT, [ |
102
|
|
|
'options' => [ |
103
|
5 |
|
'default' => $default |
104
|
|
|
] |
105
|
|
|
]); |
106
|
|
|
} |
107
|
5 |
|
return $this->input[$name]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function path(): string |
114
|
|
|
{ |
115
|
|
|
return \parse_url($this->get('REQUEST_URI'), PHP_URL_PATH); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|