-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathurl_map.php
More file actions
58 lines (54 loc) · 1.41 KB
/
url_map.php
File metadata and controls
58 lines (54 loc) · 1.41 KB
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
<?php
namespace KVSun;
/**
* Checks if URL matches URL pattern of old site
*
* @param shgysk8zer0\Core\URL $url URL object
* @return bool Whether or not it matches the patten
*/
function is_old_url(\shgysk8zer0\Core\URL $url)
{
return preg_match(OLD_URL_PATTERN, $url->path);
}
/**
* Redirect from old site URL to new one
* The new URL may or may not exist.
*
* @param shgysk8zer0\Core\URL $url URL object
* @return void
* @todo Check before redirect if new URL exists
*/
function redirect_old_url(\shgysk8zer0\Core\URL $url)
{
$headers = \shgysk8zer0\Core\Headers::getInstance();
$url->path = preg_replace(OLD_URL_PATTERN, null, $url->path);
$headers->Location = "$url";
http_response_code(\shgysk8zer0\Core_API\Abstracts\HTTPStatusCodes::MOVED_PERMANENTLY);
exit();
}
/**
* Takes a URL object from old website and returns a new one with updated path
*
* @param shgysk8zer0\Core\URL $url URL object
* @return shgysk8zer0\Core\URL $url Modified URL object / clone
*/
function get_new_url(\shgysk8zer0\Core\URL $url)
{
$new = clone($url);
$new->path = trim(preg_replace(OLD_URL_PATTERN, null, $new->path), '/');
return $new;
}
/**
* Checks if a redirect is needed to convert old site URL to new site URL
* Redirects if necessary
*
* @param void
* @return void
*/
function redirect_check()
{
$url = \shgysk8zer0\Core\URL::getInstance();
if (is_old_url($url)) {
redirect_old_url($url);
}
}