[UTIL] Add method to validate url

This commit is contained in:
Hugo Sales 2021-04-25 21:17:01 +00:00
parent 5c78def973
commit 94edad43d9
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 15 additions and 0 deletions

View File

@ -235,4 +235,19 @@ abstract class Common
{
return min(max($value, $min), $max);
}
/**
* If $secure is true, only allow https URLs to pass
*/
public function isValidHttpUrl(string $url, bool $ensure_secure = false)
{
if (empty($url)) {
return false;
}
// (if false, we use '?' in 'https?' to say the 's' is optional)
$regex = $secure ? '/^https$/' : '/^https?$/';
return filter_var($url, FILTER_VALIDATE_URL)
&& preg_match($regex, parse_url($url, PHP_URL_SCHEME));
}
}