Sanitize links before storing them

This commit is contained in:
Michael 2024-02-12 05:21:13 +00:00
parent fe00a3893d
commit 061f43788c
4 changed files with 31 additions and 28 deletions

View file

@ -659,6 +659,29 @@ class Network
return !empty($scheme) && in_array($scheme, ['http', 'https']) && parse_url($url, PHP_URL_HOST);
}
/**
* Remove invalid parts from an URL
*
* @param string $url
* @return string sanitized URL
*/
public static function sanitizeUrl(string $url): string
{
$sanitized = $url = trim($url);
foreach (['"', ' '] as $character) {
$pos = strpos($sanitized, $character);
if ($pos !== false) {
$sanitized = trim(substr($sanitized, 0, $pos));
}
}
if ($sanitized != $url) {
Logger::debug('Link got sanitized', ['url' => $url, 'sanitzed' => $sanitized]);
}
return $sanitized;
}
/**
* Creates an Uri object out of a given Uri string
*