-
Notifications
You must be signed in to change notification settings - Fork 0
/
UrlClass.php
339 lines (315 loc) · 10.6 KB
/
UrlClass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* [URL] - This class is responsible to retrieve data and set data using the friendly URL best practice.
*
* 2020-04-16 -> Readme updated
* 2021-01-16 -> Methods updated - __construct accepts URLs with or without https://
* 2021-01-15 -> getId (best practice within SEO and URL best practices);
* 2021-01-16 -> Renamed method to create a new link using URL class
* 2021-01-21 -> Improved overall class performance - created URLencode method
* 2021-07-31 -> Changed README.md to English
* 2021-10-27 -> Renamed almost ALL methods to english and parameters as well
* 2021-10-27 -> Finished documentation
* 2022-03-25 -> Fixed $get definition and added __get magic function
* 2022-09-25 -> Renamed to be able to use PHP Unit
* 2023-06-10 -> Switched REQUEST_SCHEME to user HTTPS var key only
*/
class URL
{
private $site = null; // website name
private $url = null; // website URL
private $url_now = null; // current URL
private $parts = null; // URL parts
private $regras = array(); // Custom user rules
private $get = false; // You can decide if the $_GET will be considered or ignored
/**
* __construct Creates the URL within the new class instance
*/
public function __construct($url = false, $get = false)
{
$this->setGet($get);
$this->site = $_SERVER['HTTP_HOST'];
$request_scheme = "https";
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') {
$request_scheme = "http";
}
$this->url = ($url ? $url : $request_scheme . "://" . $this->site . $_SERVER['REQUEST_URI']);
if (substr($this->url, -1) !== "/") {
$this->url = $this->url . "/";
}
$this->url_now = $request_scheme . "://" . $this->site . $_SERVER["REQUEST_URI"];
if (substr($this->url_now, -1) !== "/") {
$this->url_now = $this->url_now . "/";
}
$parts = str_replace(preg_replace("/http(s)?\:\/\//", "", $this->url), '', preg_replace("/http(s)?\:\/\//", "", explode("?", $this->url_now)[0]));
$parts = explode("/", $parts);
if ($this->get) {
$get = explode("?", $this->url_now);
$get = explode("&", $get[1]);
}
if ($parts[count($parts) - 1] == "") {
unset($parts[count($parts) - 1]);
}
if (is_array($parts)) {
if ($this->get) {
$parts = array_merge($parts, $get);
}
$this->parts = $parts;
}
}
public function __set($key, $value)
{
$this->$key = $value;
return $this;
}
public function __get($key)
{
return $this->$key;
}
/**
* @param int $part Receives the desired URL part
* @return string $position Returns the informed URL part text
*/
public function get($part)
{
if (array_key_exists($part, $this->parts)) {
return "/" . $this->parts[$part];
}
if (array_key_exists($part, $this->regras)) {
return "/" . $this->parts[$this->regras[$part]];
}
return "/";
}
/**
* @return string $string Returns website's HOST
*/
public function getSite()
{
return $this->site;
}
/**
* @return string $string Returns website's HOST. If $ip switches 'localhost' by SERVER IP
*/
public function getURL($ip = false)
{
$url = $this->url;
$serverIp = $_SERVER["SERVER_ADDR"] == "::1" ? "127.0.0.1" : $_SERVER["SERVER_ADDR"];
if ($ip) {
$url = str_replace("localhost", $serverIp, $this->url);
}
return $url;
}
/**
* @return array $array Returns all URL parts in an array
*/
public function getParts()
{
return array_map(function ($part) {
return "/" . $part;
}, $this->parts);
}
/**
* @return string Returns the current URL
*/
public function now($ip = false)
{
$url = $this->url_now;
$serverIp = $_SERVER["SERVER_ADDR"] == "::1" ? "127.0.0.1" : $_SERVER["SERVER_ADDR"];
if ($ip) {
$url = str_replace("localhost", $serverIp, $url);
}
return $url;
}
/**
* @param string $word Receives a word to search within the URL parts
* @return bool If the word exists on URL, returns true
*/
public function has($word)
{
if ($word !== "" && (in_array($word, $this->parts) || (in_array($this->URLencode($word), $this->parts)))) {
return true;
}
$word = str_replace("/", "\/", $word);
if ($word !== "" && (preg_match("/" . $word . "/", $this->now()))) {
return true;
}
return false;
}
/**
*
* @param bool $get Defines if $_GET will be considered or ignored
*/
public function setGet($get = false)
{
$this->get = ($get);
}
/**
*
* @param string $name Receives the rule name
* @param int $position It is the rule itself
*/
public function addRule($name, $position)
{
$this->regras[$name] = $position;
}
/**
* @param string $position = null Searches for an ID on the URL
* @return int $id Returns the ID if it is on the URL
*/
public function getId($position = false)
{
$id = 0;
if ($position === false) {
$last = (count($this->parts)) - 1;
$aux = array_reverse(explode("-", $this->parts[$last]));
$id = (int) $aux[0];
} else {
if ((is_int($position) || is_string($position)) and $this->get($position) !== "") {
$id = (int) array_reverse(explode('-', $this->get($position)))[0];
}
}
return $id;
}
/**
* @param string $word This acts as a break point for the current URL
* @return string Returns all URL parts after the given word
*/
public function getAfter($word, $onlyKey = false)
{
if ($this->has($word)) {
$wordKey = null;
foreach ($this->parts as $key => $value) {
if ($value == $word) {
$wordKey = $key;
break;
}
}
return ($onlyKey ? (isset($this->parts[$wordKey + 1]) ? $wordKey + 1 : false) : (isset($this->parts[$wordKey + 1]) ? $this->parts[$wordKey + 1] : false));
}
return false;
}
/**
* @param string $word This acts as a break point for the current URL
* @return string Returns all URL parts before the given word
*/
public function getURLAfter($word)
{
if ($this->has($word)) {
$wordKey = $this->getAfter($word, true);
$contar = false;
$url = array();
foreach ($this->parts as $key => $value) {
if ($key == $wordKey) {
$contar = true;
}
if ($contar) {
$url[] = $value;
}
}
return implode("/", $url);
}
return false;
}
/**
* @param string $word Receives a word to search for an ID after the word
* @return int $id Returns the ID if found
*/
public function getIdAfter($word)
{
return $this->getId($this->getAfter($this->URLencode($word), true));
}
/**
* @param string $word This acts as a break point for the current URL
* @return string Returns all the URL parts before the given word
*/
public function getBefore($word, $onlyKey = false)
{
if ($this->has($word)) {
$wordKey = null;
foreach ($this->parts as $key => $value) {
if ($value == $word) {
$wordKey = $key;
break;
}
}
return ($onlyKey ? (isset($this->parts[$wordKey - 1]) ? $wordKey - 1 : false) : (isset($this->parts[$wordKey - 1]) ? $this->parts[$wordKey - 1] : false));
}
return false;
}
/**
* @param string $word This acts as a break point for the current URL
* @return string Returns all URL parts before the given word
*/
public function getURLBefore($word)
{
if ($this->has($word)) {
$wordKey = $this->getBefore($word, true);
if ($wordKey !== "") {
$quebrar = null;
$url = array();
$url[] = $this->site;
foreach ($this->parts as $key => $value) {
if ($quebrar !== null) {
break;
}
if ($key == $wordKey) {
$quebrar = true;
}
$url[] = $value;
}
return implode("/", $url);
} else {
return false;
}
}
return false;
}
/**
* @param string $word Receives a word to search for an ID before the word
* @return int $id Returns the ID if given word and ID is found
*/
public function getIdBefore($word)
{
return $this->getId($this->getBefore($this->URLencode($word), true));
}
/**
* @param string $string Receives some text to normalize for URL format
* @return string $novaUrl This function will returns the same given string, but in "URL format"
*/
public function URLencode($string)
{
$string = preg_replace('/[áàãâä]/ui', 'a', $string);
$string = preg_replace('/[éèêë]/ui', 'e', $string);
$string = preg_replace('/[íìîï]/ui', 'i', $string);
$string = preg_replace('/[óòõôö]/ui', 'o', $string);
$string = preg_replace('/[úùûü]/ui', 'u', $string);
$string = preg_replace('/[ç]/ui', 'c', $string);
$string = preg_replace('/[^a-z0-9]/i', '_', $string);
$string = preg_replace('/_+/', '-', $string);
return $string;
}
/**
*
* @param string $string Receives a text to render as link
* @param int $id Receievs an ID to append on the link
* @return string Returns generated link
*/
public function makeLink($string, $id = false)
{
$string = $this->URLencode($string);
if (strlen($string) > 50) {
$string = substr($string, 0, 50);
}
return $string . ($id ? "-" . $id : "");
}
/**
*
* @param string $string Receives some text to render as your own website link
* @param int $id Receives an ID to append on the generated link
* @return string Returns the generated link as if it is on your own website
*/
public function makeHostLink($string, $id = false)
{
return $this->site . $this->url . $this->makeLink($string, $id);
}
}