优化:放宽UrlUrlScheme的正则校验限制 #431

This commit is contained in:
pppscn 2024-03-20 10:00:57 +08:00
parent 275c2667c1
commit b02c03a092

View File

@ -226,31 +226,19 @@ class CommonUtils private constructor() {
} }
//是否合法的url //是否合法的url
fun checkUrl(urls: String?): Boolean { fun checkUrl(url: String?, emptyResult: Boolean = false): Boolean {
return checkUrl(urls, false) if (url.isNullOrEmpty()) return emptyResult
}
//是否合法的url val regex = Regex("^https?://\\S+\$")
fun checkUrl(urls: String?, emptyResult: Boolean): Boolean { return regex.matches(url)
if (TextUtils.isEmpty(urls)) return emptyResult
val regex = """^(https?://)?(?:www\.)?(?:\[[a-fA-F0-9:]+\]|[a-zA-Z0-9-]+\.?)(?::\d{1,5})?(?:[-a-zA-Z0-9()@:%_\+.~#?&/=\[\]]*)?${'$'}"""
val pat = Pattern.compile(regex)
val mat = pat.matcher(urls?.trim() ?: "")
return mat.matches()
} }
//是否合法的URL Scheme //是否合法的URL Scheme
fun checkUrlScheme(urls: String?): Boolean { fun checkUrlScheme(url: String?, emptyResult: Boolean = false): Boolean {
return checkUrlScheme(urls, false) if (url.isNullOrEmpty()) return emptyResult
}
//是否合法的URL Scheme val regex = Regex("^[a-zA-Z\\d]+://\\S+\$")
fun checkUrlScheme(urls: String?, emptyResult: Boolean): Boolean { return regex.matches(url)
if (TextUtils.isEmpty(urls)) return emptyResult
val regex = "^[a-zA-Z\\d]+://[-a-zA-Z\\d+&@#/%?=~_|!:,.;\\[\\]]*[-a-zA-Z\\d+&@#/%=~_|\\[\\]]"
val pat = Pattern.compile(regex)
val mat = pat.matcher(urls?.trim() ?: "")
return mat.matches()
} }
//是否合法的IP地址 //是否合法的IP地址
@ -282,6 +270,11 @@ class CommonUtils private constructor() {
return pattenPort.matcher(port).matches() return pattenPort.matcher(port).matches()
} }
fun checkEmail(email: String): Boolean {
val emailRegex = Regex("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\$")
return emailRegex.matches(email)
}
//是否启用通知监听服务 //是否启用通知监听服务
fun isNotificationListenerServiceEnabled(context: Context): Boolean { fun isNotificationListenerServiceEnabled(context: Context): Boolean {
val packageNames = NotificationManagerCompat.getEnabledListenerPackages(context) val packageNames = NotificationManagerCompat.getEnabledListenerPackages(context)