Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/kohana/core
Browse files Browse the repository at this point in the history
  • Loading branch information
Woody Gilk committed Jun 27, 2010
2 parents aa29985 + 22019c0 commit 7d6ed2f
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions classes/kohana/validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,55 @@ public static function email_domain($email)
*/
public static function url($url)
{
// Regex taken from http://fightingforalostcause.net/misc/2006/compare-email-regex.php
// Added the scheme and path parts to test URLs
// Based on http://www.apps.ietf.org/rfc/rfc1738.html#sec-5
if ( ! preg_match(
'~^
$scheme = '[a-z0-9+-.]+';
$host = '(([a-z0-9]{1}[a-z0-9-]+[a-z0-9]{1}|[a-z])\.?)+([a-z]{2,6})?';
$ipaddr = '(\d{1,3}.){3}\d{1,3}';
$port = '(\:\d{1,5})?';
$path = '(/.+)?';
# scheme
[-a-z0-9+.]++://
$regex = "!^{$scheme}://({$host}|{$ipaddr}){$port}{$path}$!i";
# username:password (optional)
(?:
[-a-z0-9$_.+!*\'(),;?&=%]++ # username
(?::[-a-z0-9$_.+!*\'(),;?&=%]++)? # password (optional)
@
)?
return (bool) preg_match($regex, $url);
(?:
# ip address
\d{1,3}+(?:\.\d{1,3}+){3}+
| # or
# hostname (captured)
(
(?!-)[-a-z0-9]{1,63}+(?<!-)
(?:\.(?!-)[-a-z0-9]{1,63}+(?<!-)){0,126}+
)
)
# port (optional)
(?::\d{1,5}+)?
# path (optional)
(?:/.*)?
$~iDx', $url, $matches))
return FALSE;

// We matched an IP address
if ( ! isset($matches[1]))
return TRUE;

// Check maximum length of the whole hostname
// http://en.wikipedia.org/wiki/Domain_name#cite_note-0
if (strlen($matches[1]) > 253)
return FALSE;

// An extra check for the top level domain
// It must start with a letter
$tld = ltrim(substr($matches[1], (int) strrpos($matches[1], '.')), '.');
return ctype_alpha($tld[0]);
}

/**
Expand Down

0 comments on commit 7d6ed2f

Please sign in to comment.