Show
Ignore:
Timestamp:
02/11/2008 03:59:45 PM (11 months ago)
Author:
Shadowhand
Message:

Using the Filter extension for valid::, now that we require PHP 5.2+

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/valid.php

    r1865 r2032  
    2020        public static function email($email) 
    2121        { 
    22                 return (bool) preg_match('/^(?!\.)[-+_a-z0-9.]++(?<!\.)@(?![-.])[-a-z0-9.]+(?<!\.)\.[a-z]{2,6}$/iD', $email); 
     22                return (bool) filter_var($email, FILTER_VALIDATE_EMAIL); 
    2323        } 
    2424 
     
    5656         * 
    5757         * @param   string   URL 
    58          * @param   string   protocol 
    59          * @return  boolean 
    60          */ 
    61         public static function url($url, $scheme = 'http') 
    62         { 
    63                 // Scheme is always lowercase 
    64                 $scheme = strtolower($scheme); 
    65  
    66                 // Disable error reporting 
    67                 $ER = error_reporting(0); 
    68  
    69                 // Use parse_url to validate the URL 
    70                 $url = parse_url($url); 
    71  
    72                 // Restore error reporting 
    73                 error_reporting($ER); 
    74  
    75                 // If the boolean check returns TRUE, return FALSE, and vice versa 
    76                 return ! (empty($url['host']) OR empty($url['scheme']) OR $url['scheme'] !== $scheme); 
     58         * @return  boolean 
     59         */ 
     60        public static function url($url) 
     61        { 
     62                return (bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED); 
    7763        } 
    7864 
     
    8167         * 
    8268         * @param   string   IP address 
    83          * @return  boolean 
    84          */ 
    85         public static function ip($ip) 
    86         { 
    87                 if ( ! preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/D', $ip)) 
    88                         return FALSE; 
    89  
    90                 $octets = explode('.', $ip); 
    91  
    92                 for ($i = 1; $i < 5; $i++) 
    93                 { 
    94                         $octet = (int) $octets[($i-1)]; 
    95                         if ($i === 1) 
    96                         { 
    97                                 if ($octet > 223 OR $octet < 1) 
    98                                         return FALSE; 
    99                         } 
    100                         elseif ($i === 4) 
    101                         { 
    102                                 if ($octet < 1) 
    103                                         return FALSE; 
    104                         } 
    105                         else 
    106                         { 
    107                                 if ($octet > 254) 
    108                                         return FALSE; 
    109                         } 
    110                 } 
    111  
    112                 return TRUE; 
     69         * @param   boolean  allow IPv6 addresses 
     70         * @return  boolean 
     71         */ 
     72        public static function ip($ip, $ipv6 = FALSE) 
     73        { 
     74                // Do not allow private and reserved range IPs 
     75                $flags = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE; 
     76 
     77                if ($ipv6 === TRUE) 
     78                { 
     79                        return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags); 
     80                } 
     81                else 
     82                { 
     83                        return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags | FILTER_FLAG_IPV4); 
     84                } 
    11385        } 
    11486