- fixed validateUrl-function to work with buggy php-5.2.13 and php-5.3.2 (filter_var-bug)

This commit is contained in:
Michael Kaufmann (d00p)
2010-04-21 10:40:58 +00:00
parent 4cf8e2f2ea
commit 31231a5f60

View File

@@ -29,44 +29,56 @@
function validateUrl($url) function validateUrl($url)
{ {
if(strtolower(substr($url, 0, 7)) != "http://" if(strtolower(substr($url, 0, 7)) != "http://"
&& strtolower(substr($url, 0, 8)) != "https://") && strtolower(substr($url, 0, 8)) != "https://")
{ {
$url = 'http://' . $url; $url = 'http://' . $url;
} }
if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) !== false) if(version_compare("5.2.13", PHP_VERSION, "=")
|| version_compare("5.3.2", PHP_VERSION, "="))
{ {
return true; $pattern = '/^https?:\/\/([a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?\.)+[a-z]{2,6}$/i';
if(preg_match($pattern, $url))
{
return true;
}
} }
else else
{ {
if(strtolower(substr($url, 0, 7)) == "http://" if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) !== false)
|| strtolower(substr($url, 0, 8)) == "https://")
{ {
if(strtolower(substr($url, 0, 7)) == "http://") return true;
{ }
$ip = strtolower(substr($url, 7)); }
}
if(strtolower(substr($url, 0, 8)) == "https://") // not an fqdn
{ if(strtolower(substr($url, 0, 7)) == "http://"
$ip = strtolower(substr($url, 8)); || strtolower(substr($url, 0, 8)) == "https://")
} {
if(strtolower(substr($url, 0, 7)) == "http://")
{
$ip = strtolower(substr($url, 7));
}
$ip = substr($ip, 0, strpos($ip, '/')); if(strtolower(substr($url, 0, 8)) == "https://")
{
$ip = strtolower(substr($url, 8));
}
if(validate_ip($ip, true) !== false) $ip = substr($ip, 0, strpos($ip, '/'));
{
return true; if(validate_ip($ip, true) !== false)
} {
else return true;
{
return false;
}
} }
else else
{ {
return false; return false;
} }
} }
else
{
return false;
}
} }