diff options
author | Andrey Andreev <narf@bofh.bg> | 2012-11-26 21:16:12 +0100 |
---|---|---|
committer | Andrey Andreev <narf@bofh.bg> | 2012-11-26 21:16:12 +0100 |
commit | daaca882e48e26e60bf2eea9f4fad108a845fb38 (patch) | |
tree | 4ae9ccaa8315a9fdbe23295b0d0163a153bee1bb /system/libraries/Form_validation.php | |
parent | 61797f67a9bcb357ae7e1be9dadffd58eaa3e540 (diff) |
Add 'valid_url' rule to Form Validation (issue #1966)
Diffstat (limited to 'system/libraries/Form_validation.php')
-rw-r--r-- | system/libraries/Form_validation.php | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 04bb81db7..b7bd280ee 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1084,6 +1084,48 @@ class CI_Form_validation { // -------------------------------------------------------------------- /** + * Valid URL + * + * @param string $str + * @return bool + */ + public function valid_url($str) + { + if (empty($str)) + { + return FALSE; + } + elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches)) + { + if (empty($matches[2])) + { + return FALSE; + } + elseif ( ! in_array($matches[1], array('http', 'https'), TRUE)) + { + return FALSE; + } + + $str = $matches[2]; + } + + $str = 'http://'.$str; + + // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the + // underscore to be a valid hostname character instead of a dash. + // Reference: https://bugs.php.net/bug.php?id=51192 + if (version_compare(PHP_VERSION, '5.2.13', '==') === 0 OR version_compare(PHP_VERSION, '5.3.2', '==') === 0) + { + sscanf($str, 'http://%[^/]', $host); + $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host)); + } + + return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE); + } + + // -------------------------------------------------------------------- + + /** * Valid Email * * @param string |