1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Retour plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Retour allows you to intelligently redirect legacy URLs, so that you don't |
6
|
|
|
* lose SEO value when rebuilding & restructuring a website |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2020 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\retour\helpers; |
13
|
|
|
|
14
|
|
|
use craft\helpers\UrlHelper as CraftUrlHelper; |
15
|
|
|
|
16
|
|
|
/** |
|
|
|
|
17
|
|
|
* @author nystudio107 |
|
|
|
|
18
|
|
|
* @package Retour |
|
|
|
|
19
|
|
|
* @since 3.1.34 |
|
|
|
|
20
|
|
|
*/ |
|
|
|
|
21
|
|
|
class UrlHelper extends CraftUrlHelper |
22
|
|
|
{ |
23
|
|
|
// Public Static Methods |
24
|
|
|
// ========================================================================= |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns a query string that is a combination of al of the query strings from |
28
|
|
|
* the passed in $urls |
29
|
|
|
* |
30
|
|
|
* @param ...$urls |
|
|
|
|
31
|
|
|
* @return string |
|
|
|
|
32
|
|
|
*/ |
33
|
|
|
public static function combineQueryStringsFromUrls(...$urls): string |
34
|
|
|
{ |
35
|
|
|
$queryParams = []; |
36
|
|
|
foreach ($urls as $url) { |
37
|
|
|
$parsedUrl = parse_url($url); |
38
|
|
|
$params = []; |
39
|
|
|
parse_str($parsedUrl['query'] ?? '', $params); |
40
|
|
|
$queryParams[] = $params; |
41
|
|
|
} |
42
|
|
|
$queryParams = array_unique(array_merge([], ...$queryParams), SORT_REGULAR); |
43
|
|
|
|
44
|
|
|
return http_build_query($queryParams); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Merge the $url and $path together, combining any overlapping path segments |
49
|
|
|
* |
50
|
|
|
* @param string $url |
|
|
|
|
51
|
|
|
* @param string $path |
|
|
|
|
52
|
|
|
* @return string |
|
|
|
|
53
|
|
|
*/ |
54
|
|
|
public static function mergeUrlWithPath(string $url, string $path): string |
55
|
|
|
{ |
56
|
|
|
$overlap = 0; |
57
|
|
|
$urlOffset = strlen($url); |
58
|
|
|
$pathLength = strlen($path); |
59
|
|
|
$pathOffset = 0; |
60
|
|
|
while ($urlOffset > 0 && $pathOffset < $pathLength) { |
61
|
|
|
$urlOffset--; |
62
|
|
|
$pathOffset++; |
63
|
|
|
if (str_starts_with($path, substr($url, $urlOffset, $pathOffset))) { |
64
|
|
|
$overlap = $pathOffset; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return rtrim($url, '/') . '/' . ltrim(substr($path, $overlap), '/'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return a sanitized URL |
73
|
|
|
* |
74
|
|
|
* @param string $url |
|
|
|
|
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
1 |
|
public static function sanitizeUrl(string $url): string |
79
|
|
|
{ |
80
|
|
|
// HTML decode the entities, then strip out any tags |
81
|
1 |
|
$url = html_entity_decode($url, ENT_NOQUOTES, 'UTF-8'); |
82
|
1 |
|
$url = urldecode($url); |
83
|
1 |
|
$url = strip_tags($url); |
84
|
|
|
// Remove any Twig tags that somehow are present in the incoming URL |
85
|
|
|
/** @noinspection CallableParameterUseCaseInTypeContextInspection */ |
|
|
|
|
86
|
1 |
|
$url = preg_replace('/{.*}/', '', $url); |
87
|
|
|
// Remove any linebreaks that may be errantly in the URL |
88
|
1 |
|
$url = (string)str_replace([ |
|
|
|
|
89
|
1 |
|
PHP_EOL, |
90
|
1 |
|
"\r", |
91
|
1 |
|
"\n", |
92
|
1 |
|
] |
93
|
1 |
|
, '', $url |
|
|
|
|
94
|
1 |
|
); |
95
|
|
|
|
96
|
1 |
|
return $url; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|