1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Http package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Http; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Iqbal Maulana <[email protected]> |
15
|
|
|
* @created 8/5/15 |
16
|
|
|
*/ |
17
|
|
|
class SchemaHost |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ServerBag |
21
|
|
|
*/ |
22
|
|
|
private $server; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor. |
26
|
|
|
* |
27
|
|
|
* @param ServerBag $server |
28
|
|
|
*/ |
29
|
|
|
public function __construct(ServerBag $server) |
30
|
|
|
{ |
31
|
|
|
$this->server = $server; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get schema and http host. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function getSchemaAndHttpHost() |
40
|
|
|
{ |
41
|
|
|
return sprintf('%s://%s', $this->getSchema(), $this->getHttpHost()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get http host and port if not standard. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getHttpHost() |
50
|
|
|
{ |
51
|
|
|
$schema = $this->getSchema(); |
52
|
|
|
$port = $this->getPort(); |
53
|
|
|
|
54
|
|
|
if (('http' === $schema && 80 === $port) || ('https' === $schema && 443 === $port)) { |
55
|
|
|
return $this->getHost(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return sprintf('%s:%s', $this->getHost(), $this->getPort()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get server host. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getHost() |
67
|
|
|
{ |
68
|
|
|
$host = $this->server->get('SERVER_NAME', ''); |
69
|
|
|
|
70
|
|
|
return false === empty($host) ? $host : $this->server->get('SERVER_ADDR'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get server port. |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
public function getPort() |
79
|
|
|
{ |
80
|
|
|
return (int) $this->server->get('SERVER_PORT', 80); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get http schema. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getSchema() |
89
|
|
|
{ |
90
|
|
|
return $this->isSecure() ? 'https' : 'http'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check if server request is secure (https) or not. |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function isSecure() |
99
|
|
|
{ |
100
|
|
|
$https = $this->server->get('HTTPS'); |
101
|
|
|
|
102
|
|
|
return false === empty($https) && 'off' !== strtolower($https); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|