. */ /** * Validate different inputs * * @version 1.0 * @date 10.04.2010 * @author Wim van Ravesteijn * @license http://opensource.org/licenses/gpl-license.php GNU Public License */ abstract class Validator { /** * Check if the supplied e-mail address is a valid e-mail address. Both syntax and MX record for domain are checked * * @param string $email address to be checked * @return boolean true in case the e-mail address is valid, false otherwise */ public static function email($email) { $re="/(^(\w|\.|-)+@(\w|-)+(\.(\w|-)+)*\.[a-zA-Z]{2,4}$)/"; if( preg_match($re,$email) ) { //Regex matches, now check MX if( getmxrr(substr($email,strpos($email,"@")+1), $mxhosts) ) { return true; }else { return false; } }else { return false; } } /** * Check if the supplied sip number is valid. * * @param string $sipNumber address to be checked * @return boolean true in case the sip number is valid, false otherwise */ public static function sipNumber($sipNumber) { $re="/(^(\w|\.|-)+@(\w|-)+(\.(\w|-)+)*\.[a-zA-Z]{2,4}$)/"; return preg_match($re, $sipNumber); } /** * Check if the supplied url is a valid one. Syntax check only * * @param string $url url to be checked * @return boolean true in case the url is valid, false otherwise */ public static function url($url) { $re = "/^https?:\/\/(\w|-)+(\.(\w|-)+)*\.[a-zA-Z]{2,4}(\/.*)?$/"; return preg_match($re, $url); } /** * Validate an SQL-style date * * @param string date in SQL-style (yyyy-mm-dd) * @return boolean true in case date is valid, false otherwise */ public static function sqlDate($date) { if( !preg_match("/(^(\d){4}\-(\d){2}\-(\d){2}$)/", $date) ) { return false; } return checkdate(substr($date, 5, 2), substr($date, 8, 2), substr($date, 0, 4)); } /** * Validate a latitude * * @param double latitude * @return boolean true in case latitude is valid, false otherwise */ public static function latitude($latitude) { return ( is_numeric($latitude) AND $latitude >= -90 AND $latitude <= 90 ); } /** * Validate a longitude * * @param double longitude * @return boolean true in case longitude is valid, false otherwise */ public static function longitude($longitude) { return self::latitude($longitude); } /** * Validate a country, which should be a valid ISO-2 code * * @param string an ISO-2 country code * @return boolean true in case country is valid, false otherwise */ public static function countryIso2($country) { // TODO provide proper validation return ( strlen(trim($country)) == 2 ); } /** * Validate a BodyCode, which should be exactly 3 capital letters * * @param string a BodyCode * @return boolean true in case BodyCode is valid, false otherwise */ public static function BodyCode($BodyCode) { $re = "/^[A-Z]{3}$/"; return ( preg_match($re, $BodyCode )); } /** * Validate a phone number * * @param string phone number * @return boolean true in case phone number is valid, false otherwise */ public static function phone($phone) { return ( preg_match("/(^\+[0-9 -]+$)/", $phone) == 1 ); } /** * Validate a gender * * @param string gender * @return boolean true in case gender is valid, false otherwise */ public static function gender($gender) { return ( $gender == "male" OR $gender == "female" ); } /** * Validate a card serial number (CSN) * * @param string csn * @return boolean true in case csn is valid, false otherwise */ public static function csn($csn) { $csn = strtoupper($csn); $re="/(^[A-Z]\d{5}-?\d{5}\d?[0-9X]$)/"; if( !preg_match($re, $csn) ) { return false; } $expected = substr($csn, -1); if( $expected == "X" ) { $expected = 10; } $check = ""; for( $i=0; $i=65) AND (ord( substr($csn, $i, 1) )<=90) ) { $check .= ord( substr($csn, $i, 1) ); }else if( (ord(substr($csn, $i, 1))>=48) AND (ord(substr($csn, $i, 1))<=57) ) { # Dealing with 0-9 $check .= substr($csn, $i, 1); } } $result = 0; for($i=0; $i