From 94edad43d9667d42883e5ff7087d09ed970af099 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 25 Apr 2021 21:17:01 +0000 Subject: [PATCH] [UTIL] Add method to validate url --- src/Util/Common.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Util/Common.php b/src/Util/Common.php index 4d34453350..6c52b86375 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -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)); + } }