-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathUrlHelper.php
331 lines (306 loc) · 11 KB
/
UrlHelper.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
<?php
/**
* SEOmatic plugin for Craft CMS 3.x
*
* A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
* and flexible
*
* @link https://nystudio107.com
* @copyright Copyright (c) 2017 nystudio107
*/
namespace nystudio107\seomatic\helpers;
use Craft;
use craft\errors\SiteNotFoundException;
use craft\helpers\UrlHelper as CraftUrlHelper;
use nystudio107\seomatic\Seomatic;
use Throwable;
use yii\base\Exception;
use function is_string;
/**
* @author nystudio107
* @package Seomatic
* @since 3.0.0
*/
class UrlHelper extends CraftUrlHelper
{
// Public Static Properties
// =========================================================================
// Public Static Methods
// =========================================================================
/**
* @inheritDoc
*/
public static function siteUrl(string $path = '', $params = null, string $scheme = null, int $siteId = null): string
{
try {
$siteUrl = self::getSiteUrlOverrideSetting($siteId);
} catch (Throwable $e) {
// That's okay
}
if (!empty($siteUrl)) {
$siteUrl = MetaValue::parseString($siteUrl);
// Extract out just the path part
$parts = self::decomposeUrl($path);
$path = $parts['path'] . $parts['suffix'];
$url = self::mergeUrlWithPath($siteUrl, $path);
// Handle trailing slashes properly for generated URLs
$generalConfig = Craft::$app->getConfig()->getGeneral();
if ($generalConfig->addTrailingSlashesToUrls && !preg_match('/(.+\?.*)|(\.[^\/]+$)/', $url)) {
$url = rtrim($url, '/') . '/';
}
if (!$generalConfig->addTrailingSlashesToUrls) {
$url = rtrim($url, '/');
}
return DynamicMeta::sanitizeUrl(parent::urlWithParams($url, $params ?? []), false, false);
}
return DynamicMeta::sanitizeUrl(parent::siteUrl($path, $params, $scheme, $siteId), false, false);
}
/**
* Merge the $url and $path together, combining any overlapping path segments
*
* @param string $url
* @param string $path
* @return string
*/
public static function mergeUrlWithPath(string $url, string $path): string
{
$overlap = 0;
$url = rtrim($url, '/') . '/';
$path = '/' . ltrim($path, '/');
$urlOffset = strlen($url);
$pathLength = strlen($path);
$pathOffset = 0;
while ($urlOffset > 0 && $pathOffset < $pathLength) {
$urlOffset--;
$pathOffset++;
if (str_starts_with($path, substr($url, $urlOffset, $pathOffset))) {
$overlap = $pathOffset;
}
}
return rtrim($url, '/') . '/' . ltrim(substr($path, $overlap), '/');
}
/**
* Return the page trigger and the value of the page trigger (null if it doesn't exist)
*
* @return array
*/
public static function pageTriggerValue(): array
{
$pageTrigger = Craft::$app->getConfig()->getGeneral()->pageTrigger;
if (!is_string($pageTrigger) || $pageTrigger === '') {
$pageTrigger = 'p';
}
// Is this query string-based pagination?
if ($pageTrigger[0] === '?') {
$pageTrigger = trim($pageTrigger, '?=');
}
// Avoid conflict with the path param
$pathParam = Craft::$app->getConfig()->getGeneral()->pathParam;
if ($pageTrigger === $pathParam) {
$pageTrigger = $pathParam === 'p' ? 'pg' : 'p';
}
$pageTriggerValue = Craft::$app->getRequest()->getParam($pageTrigger);
return [$pageTrigger, $pageTriggerValue];
}
/**
* Return an absolute URL with protocol that curl will be happy with
*
* @param string $url
*
* @return string
*/
public static function absoluteUrlWithProtocol($url): string
{
// Make this a full URL
if (!self::isAbsoluteUrl($url)) {
$protocol = 'http';
if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)
|| isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0
) {
$protocol = 'https';
}
if (self::isProtocolRelativeUrl($url)) {
try {
$url = self::urlWithScheme($url, $protocol);
} catch (SiteNotFoundException $e) {
Craft::error($e->getMessage(), __METHOD__);
}
} else {
try {
$url = self::siteUrl($url, null, $protocol);
if (self::isProtocolRelativeUrl($url)) {
$url = self::urlWithScheme($url, $protocol);
}
} catch (Exception $e) {
Craft::error($e->getMessage(), __METHOD__);
}
}
}
// Ensure that any spaces in the URL are encoded
$url = str_replace(' ', '%20', $url);
// If the incoming URL has a trailing slash, respect it by preserving it
$preserveTrailingSlash = false;
if (str_ends_with($url, '/')) {
$preserveTrailingSlash = true;
}
// Handle trailing slashes properly for generated URLs
$generalConfig = Craft::$app->getConfig()->getGeneral();
if ($generalConfig->addTrailingSlashesToUrls && !preg_match('/(.+\?.*)|(\.[^\/]+$)/', $url)) {
$url = rtrim($url, '/') . '/';
}
if (!$generalConfig->addTrailingSlashesToUrls && (!$preserveTrailingSlash || self::urlIsSiteIndex($url))) {
$url = rtrim($url, '/');
}
return DynamicMeta::sanitizeUrl($url, false, false);
}
/**
* urlencode() just the query parameters in the URL
*
* @param string $url
* @return string
*/
public static function encodeUrlQueryParams(string $url): string
{
$urlParts = parse_url($url);
$encodedUrl = "";
if (isset($urlParts['scheme'])) {
$encodedUrl .= $urlParts['scheme'] . '://';
}
if (isset($urlParts['host'])) {
$encodedUrl .= $urlParts['host'];
}
if (isset($urlParts['port'])) {
$encodedUrl .= ':' . $urlParts['port'];
}
if (isset($urlParts['path'])) {
$encodedUrl .= $urlParts['path'];
}
if (isset($urlParts['query'])) {
$query = explode('&', $urlParts['query']);
foreach ($query as $j => $value) {
$value = explode('=', $value, 2);
if (count($value) === 2) {
$query[$j] = urlencode($value[0]) . '=' . urlencode($value[1]);
} else {
$query[$j] = urlencode($value[0]);
}
}
$encodedUrl .= '?' . implode('&', $query);
}
if (isset($urlParts['fragment'])) {
$encodedUrl .= '#' . $urlParts['fragment'];
}
return $encodedUrl;
}
/**
* Return whether this URL has a sub-directory as part of it
*
* @param string $url
* @return bool
*/
public static function urlHasSubDir(string $url): bool
{
return !empty(parse_url(trim($url, '/'), PHP_URL_PATH));
}
/**
* See if the url is a site index, and if so, strip the trailing slash
* ref: https://github.com/craftcms/cms/issues/5675
*
* @param string $url
* @return bool
*/
public static function urlIsSiteIndex(string $url): bool
{
$sites = Craft::$app->getSites()->getAllSites();
$result = false;
foreach ($sites as $site) {
$sitePath = parse_url(self::siteUrl('/', null, null, $site->id), PHP_URL_PATH);
if (!empty($sitePath)) {
// Normalizes a URI path by trimming leading/ trailing slashes and removing double slashes
$sitePath = '/' . preg_replace('/\/\/+/', '/', trim($sitePath, '/'));
}
// Normalizes a URI path by trimming leading/ trailing slashes and removing double slashes
$url = '/' . preg_replace('/\/\/+/', '/', trim($url, '/'));
// See if this url ends with a site prefix, and thus is a site index
if (str_ends_with($url, $sitePath)) {
$result = true;
}
}
return $result;
}
/**
* Return the siteUrlOverride setting, which can be a string or an array of site URLs
* indexed by the site handle
*
* @param int|null $siteId
* @return string
* @throws Exception
* @throws SiteNotFoundException
*/
public static function getSiteUrlOverrideSetting(?int $siteId = null): string
{
// If the override is a string, just return it
$siteUrlOverride = Seomatic::$settings->siteUrlOverride;
if (is_string($siteUrlOverride)) {
return $siteUrlOverride;
}
// If the override is an array, pluck the appropriate one by handle
if (is_array($siteUrlOverride)) {
$sites = Craft::$app->getSites();
$site = $sites->getCurrentSite();
if ($siteId !== null) {
$site = $sites->getSiteById($siteId, true);
if (!$site) {
throw new Exception('Invalid site ID: ' . $siteId);
}
}
return $siteUrlOverride[$site->handle] ?? '';
}
}
/**
* Encodes non-alphanumeric characters in a URL, except reserved characters and already-encoded characters.
*
* @param string $url
* @return string
* @since 4.13.0
*/
public static function encodeUrl(string $url): string
{
$parts = preg_split('/([:\/?#\[\]@!$&\'()*+,;=%])/', $url, flags: PREG_SPLIT_DELIM_CAPTURE);
$url = '';
foreach ($parts as $i => $part) {
if ($i % 2 === 0) {
$url .= urlencode($part);
} else {
$url .= $part;
}
}
return $url;
}
// Protected Methods
// =========================================================================
/**
* Decompose a url into a prefix, path, and suffix
*
* @param $pathOrUrl
*
* @return array
*/
protected static function decomposeUrl($pathOrUrl): array
{
$result = array();
if (filter_var($pathOrUrl, FILTER_VALIDATE_URL)) {
$url_parts = parse_url($pathOrUrl);
$result['prefix'] = $url_parts['scheme'] . '://' . $url_parts['host'];
$result['path'] = $url_parts['path'] ?? '';
$result['suffix'] = '';
$result['suffix'] .= empty($url_parts['query']) ? '' : '?' . $url_parts['query'];
$result['suffix'] .= empty($url_parts['fragment']) ? '' : '#' . $url_parts['fragment'];
} else {
$result['prefix'] = '';
$result['path'] = $pathOrUrl;
$result['suffix'] = '';
}
return $result;
}
}