|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace System; |
|
4
|
|
|
|
|
5
|
|
|
class Url |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Protocol |
|
9
|
|
|
* |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
private $protocol; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Host |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $host; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Parameters |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $parameters; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Base Url |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $baseUrl; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Application Object |
|
37
|
|
|
* |
|
38
|
|
|
* @var \System\Application |
|
39
|
|
|
*/ |
|
40
|
|
|
private $app; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructor |
|
44
|
|
|
* |
|
45
|
|
|
* @param \System\Application $app |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(Application $app) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->app = $app; |
|
50
|
|
|
|
|
51
|
|
|
$this->prepare(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Prepare url |
|
56
|
|
|
* |
|
57
|
|
|
* @property object $request |
|
58
|
|
|
* @property object $http |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
|
private function prepare() |
|
62
|
|
|
{ |
|
63
|
|
|
$script = dirname($this->app->request->server('SCRIPT_NAME')); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$requestUri = $this->app->request->server('REQUEST_URI'); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
if (strpos($requestUri, '?')) { |
|
68
|
|
|
list($requestUri) = explode('?', $requestUri); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->protocol = $this->app->http->requestProtocol(); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$this->rootDomain = $this->app->request->server('HTTP_HOST'); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$this->host = $this->protocol . '://' . $this->rootDomain; |
|
76
|
|
|
|
|
77
|
|
|
$this->parameters = cleanUrl($script, $requestUri); |
|
78
|
|
|
|
|
79
|
|
|
$this->baseUrl = $this->host . $this->parameters; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get Protocol |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function protocol() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->protocol; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get Root domain |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function rootDomain() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->rootDomain; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get parameters |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function parameters() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->parameters; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get only host |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function host() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->host; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get full url of the script |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function baseUrl() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->baseUrl; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Generate full link for the given path |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $path |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public function link(string $path) |
|
139
|
|
|
{ |
|
140
|
|
|
$link = $this->host(); |
|
141
|
|
|
|
|
142
|
|
|
$path = rtrim($path, '/'); |
|
143
|
|
|
$path = ltrim($path, '/'); |
|
144
|
|
|
|
|
145
|
|
|
return $link . '/' . $path; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Redirect to the given path |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $path |
|
152
|
|
|
* @param int $num |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
|
|
public function redirectTo(string $path, int $num = 0) |
|
156
|
|
|
{ |
|
157
|
|
|
header('Refresh: ' . $num . '; URL=' . $this->link($path)); |
|
158
|
|
|
exit; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Redirect to the previous page |
|
163
|
|
|
* |
|
164
|
|
|
* @return void |
|
165
|
|
|
*/ |
|
166
|
|
|
public function redirectToPreviousPage() |
|
167
|
|
|
{ |
|
168
|
|
|
header('Refresh: ' . $this->request->referer()); |
|
|
|
|
|
|
169
|
|
|
exit; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Redirect to the 404 page |
|
174
|
|
|
* |
|
175
|
|
|
* @property object $request |
|
176
|
|
|
* @property object $load |
|
177
|
|
|
* @param string $path |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
|
|
public function notfound(string $path = '') |
|
181
|
|
|
{ |
|
182
|
|
|
if (!$path) { |
|
183
|
|
|
$path = '/404'; |
|
184
|
|
|
|
|
185
|
|
|
if ($this->app->request->isRequestToAdminManagement() && $this->app->load->model('User')->isAdmin()) { |
|
|
|
|
|
|
186
|
|
|
$path = 'admin/404'; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
header('Refresh: 0; URL=' . $this->link($path)); |
|
190
|
|
|
exit; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.