|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by: Jens |
|
4
|
|
|
* Date: 12-10-2017 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CloudControl\Cms\cc\application; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
use CloudControl\Cms\cc\Application; |
|
11
|
|
|
use CloudControl\Cms\cc\Request; |
|
12
|
|
|
use CloudControl\Cms\storage\Storage; |
|
13
|
|
|
|
|
14
|
|
|
class UrlMatcher |
|
15
|
|
|
{ |
|
16
|
|
|
private $application; |
|
17
|
|
|
private $storage; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* UrlMatcher constructor. |
|
21
|
|
|
* @param Application $application |
|
22
|
|
|
* @param Storage $storage |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(Application $application, Storage $storage) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->application = $application; |
|
27
|
|
|
$this->storage = $storage; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Loop through the redirects and see if a redirect needs to take place |
|
32
|
|
|
* |
|
33
|
|
|
* @param Request $request |
|
34
|
|
|
* @throws \Exception |
|
35
|
|
|
*/ |
|
36
|
|
|
public function redirectMatching(Request $request) |
|
37
|
|
|
{ |
|
38
|
|
|
$redirects = $this->storage->getRedirects()->getRedirects(); |
|
39
|
|
|
$relativeUri = '/' . $request::$relativeUri; |
|
40
|
|
|
|
|
41
|
|
|
foreach ($redirects as $redirect) { |
|
42
|
|
|
if (preg_match_all($redirect->fromUrl, $relativeUri, $matches)) { |
|
43
|
|
|
$toUrl = preg_replace($redirect->fromUrl, $redirect->toUrl, $relativeUri); |
|
44
|
|
|
if (substr($toUrl, 0, 1) == '/') { |
|
45
|
|
|
$toUrl = substr($toUrl, 1); |
|
46
|
|
|
} |
|
47
|
|
|
if ($redirect->type == '301') { |
|
48
|
|
|
header('HTTP/1.1 301 Moved Permanently'); |
|
49
|
|
|
header('Location: ' . $request::$subfolders . $toUrl); |
|
50
|
|
|
exit; |
|
51
|
|
|
} elseif ($redirect->type == '302') { |
|
52
|
|
|
header('Location: ' . $request::$subfolders . $toUrl, true, 302); |
|
53
|
|
|
exit; |
|
54
|
|
|
} else { |
|
55
|
|
|
throw new \Exception('Invalid redirect type.'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Loop through sitemap items and see if one matches the requestUri. |
|
63
|
|
|
* If it does, add it tot the matchedSitemapItems array |
|
64
|
|
|
* |
|
65
|
|
|
* @param Request $request |
|
66
|
|
|
*/ |
|
67
|
|
|
public function sitemapMatching($request) |
|
68
|
|
|
{ |
|
69
|
|
|
$sitemap = $this->storage->getSitemap()->getSitemap(); |
|
70
|
|
|
$relativeUri = '/' . $request::$relativeUri; |
|
71
|
|
|
|
|
72
|
|
|
foreach ($sitemap as $sitemapItem) { |
|
73
|
|
|
if ($sitemapItem->regex) { |
|
74
|
|
|
$matches = array(); |
|
75
|
|
|
if (preg_match_all($sitemapItem->url, $relativeUri, $matches)) { |
|
76
|
|
|
// Make a clone, so it doesnt add the matches to the original |
|
77
|
|
|
$matchedClone = clone $sitemapItem; |
|
78
|
|
|
$matchedClone->matches = $matches; |
|
79
|
|
|
$this->application->addMatchedSitemapItem($matchedClone); |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
} else { |
|
83
|
|
|
if ($sitemapItem->url == $relativeUri) { |
|
84
|
|
|
$this->application->addMatchedSitemapItem($sitemapItem); |
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |