| 9 |
* class, you create an array of fields that you want |
* class, you create an array of fields that you want |
| 10 |
* checked in the validate() method. |
* checked in the validate() method. |
| 11 |
* |
* |
| 12 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
* @author Walter A. Boring IV <waboring@newsblob.com> |
| 13 |
* @package phpHtmlLib |
* @package phpHtmlLib |
| 14 |
* @subpackage FormProcessing |
* @subpackage FormProcessing |
| 15 |
* |
* |
| 38 |
var $_error_code = ''; |
var $_error_code = ''; |
| 39 |
|
|
| 40 |
/** |
/** |
| 41 |
* This holds the last error message |
* This holds the last error message |
| 42 |
*/ |
*/ |
| 43 |
var $_error_message = ''; |
var $_error_message = ''; |
| 44 |
|
|
| 45 |
|
/** |
| 46 |
|
* This is the FormErrors object |
| 47 |
|
*/ |
| 48 |
|
var $_FormErrors = NULL; |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
/** |
| 52 |
|
* The constructor used to set the |
| 53 |
|
* form errors object used by this class |
| 54 |
|
* to do error text lookups |
| 55 |
|
* |
| 56 |
|
* @param FormErrors object |
| 57 |
|
*/ |
| 58 |
|
function FormValidation(&$error_obj) { |
| 59 |
|
$this->_FormErrors = &$error_obj; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
/** |
| 64 |
|
* A singleton method for only |
| 65 |
|
* creating one instance of this |
| 66 |
|
* per request. |
| 67 |
|
* |
| 68 |
|
* @return FormValidation object |
| 69 |
|
*/ |
| 70 |
|
function &singleton() { |
| 71 |
|
static $obj=NULL; |
| 72 |
|
|
| 73 |
|
if (is_null($obj)) { |
| 74 |
|
$obj = new FormValidation(new FormErrors); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
return $obj; |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
/** |
/** |
| 83 |
* This method checks to make sure an array doesn't have |
* This method checks to make sure an array doesn't have |
| 84 |
* an empty element. |
* an empty element. |
| 108 |
function _error($code, $message=NULL) { |
function _error($code, $message=NULL) { |
| 109 |
$this->_error_code = $code; |
$this->_error_code = $code; |
| 110 |
if ($message == NULL) { |
if ($message == NULL) { |
| 111 |
$err = new FormErrors; |
$this->_error_message = $this->_FormErrors->get_error_msg($code); |
|
$this->_error_message = $err->get_error_msg($code); |
|
| 112 |
unset( $err ); |
unset( $err ); |
| 113 |
} else { |
} else { |
| 114 |
$this->_error_message = $message; |
$this->_error_message = $message; |
| 115 |
} |
} |
| 116 |
|
|
| 117 |
return FALSE; |
return FALSE; |
| 118 |
} |
} |
| 119 |
|
|
| 129 |
} |
} |
| 130 |
|
|
| 131 |
/** |
/** |
| 132 |
* This method returns the error |
* This method returns the error |
| 133 |
* message from the last validation |
* message from the last validation |
| 134 |
* error found. |
* error found. |
| 135 |
* |
* |
| 136 |
* @return string |
* @return string |
| 153 |
} |
} |
| 154 |
return TRUE; |
return TRUE; |
| 155 |
} |
} |
| 156 |
|
|
| 157 |
/** |
/** |
| 158 |
* This function checks if the given string contains |
* This function checks if the given string contains |
| 159 |
* alphabetical characters or numbers. |
* alphabetical characters or numbers. |
| 163 |
* @param string - list of individual characters to allow |
* @param string - list of individual characters to allow |
| 164 |
* besides a-zA-Z |
* besides a-zA-Z |
| 165 |
* |
* |
| 166 |
* @return boolean FALSE - if any other extraneous |
* @return boolean FALSE - if any other extraneous |
| 167 |
* characters are found |
* characters are found |
| 168 |
* TRUE upon succes. |
* TRUE upon succes. |
| 169 |
*/ |
*/ |
| 197 |
* @param string - list of individual characters to allow |
* @param string - list of individual characters to allow |
| 198 |
* besides a-zA-Z |
* besides a-zA-Z |
| 199 |
* |
* |
| 200 |
* @return boolean FALSE - if any other extraneous |
* @return boolean FALSE - if any other extraneous |
| 201 |
* characters are found |
* characters are found |
| 202 |
* TRUE upon succes. |
* TRUE upon succes. |
| 203 |
*/ |
*/ |
| 208 |
return FALSE; |
return FALSE; |
| 209 |
} |
} |
| 210 |
|
|
| 211 |
|
//now do another simple check |
| 212 |
|
if ($from == '' && !is_numeric($str)) { |
| 213 |
|
return FALSE; |
| 214 |
|
} |
| 215 |
|
|
| 216 |
$to = ""; |
$to = ""; |
| 217 |
$len = strlen($from); |
$len = strlen($from); |
| 218 |
for ( $i = 0; $i < $len; ++$i ) { |
for ( $i = 0; $i < $len; ++$i ) { |
| 219 |
$to .= "0"; |
$to .= "0"; |
| 220 |
} |
} |
| 221 |
$str = strtr($str, $from, $to); |
$str = strtr($str, $from, $to); |
| 222 |
$substr = eregi_replace("[0-9]+", "", $str); |
$substr = eregi_replace("[-0-9]+", "", $str); |
| 223 |
if ( !$this->array_hasempty($substr) ) { |
if ( !$this->array_hasempty($substr) ) { |
| 224 |
return FALSE; |
return FALSE; |
| 225 |
} |
} |
| 245 |
|
|
| 246 |
/** |
/** |
| 247 |
* This method makes sure a value lies within |
* This method makes sure a value lies within |
| 248 |
* a given range of values. |
* a given range of values. |
| 249 |
* The error message can be customized by passing in |
* The error message can be customized by passing in |
| 250 |
* a customer error code |
* a customer error code |
| 251 |
* |
* |
| 277 |
} |
} |
| 278 |
return TRUE; |
return TRUE; |
| 279 |
} |
} |
| 280 |
|
|
| 281 |
/** |
/** |
| 282 |
* This method validates a string as containing |
* This method validates a string as containing |
| 283 |
* only letters and numbers |
* only letters and numbers |
| 332 |
* @param string - the value to validate |
* @param string - the value to validate |
| 333 |
* @return TRUE = succes FALSE = failed |
* @return TRUE = succes FALSE = failed |
| 334 |
*/ |
*/ |
| 335 |
function is_domainname ($name) { |
function is_domainname ($name, $forbid_www=FALSE) { |
| 336 |
|
if (!ereg( "^(([a-z0-9]{1,63}|". |
| 337 |
// letters, digits and ".-" only |
"([a-z0-9][a-z0-9\-]{1,61}[a-z0-9]))\.)+". |
| 338 |
if ( !$this->is_alphanum($name, ".-") ) { |
"[a-z]{2,63}$", $name )) { |
| 339 |
return $this->_error("INVALID_HOST_NAME"); |
return $this->_error("INVALID_HOST_NAME"); |
| 340 |
} |
} else if ($forbid_www && ereg( "^www\.", $name )) { |
|
|
|
|
$len = strlen($name); |
|
|
if ( $len > VALIDATE_MAX_HOSTNAME_LENGTH ) { |
|
|
return $this->_error("INVALID_LENGTH"); |
|
|
} |
|
|
|
|
|
// Error if it doesn't start with an alphabet or digit |
|
|
if ( !eregi("^[a-z0-9]", $name) ) { |
|
|
return $this->_error("INVALID_HOST_NAME"); |
|
|
} |
|
|
|
|
|
// Error if it contains no alphabets |
|
|
if ( !eregi("[a-z]", $name) ) { |
|
|
return $this->_error("INVALID_HOST_NAME"); |
|
|
} |
|
|
|
|
|
// Cannot contain two or more consecutive dots. |
|
|
if ( ereg("\.\.+", $name) ) { |
|
| 341 |
return $this->_error("INVALID_HOST_NAME"); |
return $this->_error("INVALID_HOST_NAME"); |
| 342 |
} |
} |
| 343 |
return TRUE; |
return TRUE; |
| 593 |
} |
} |
| 594 |
|
|
| 595 |
/** |
/** |
| 596 |
* This method validates a string as a |
* This method validates a string as a |
| 597 |
* path to a file. |
* path to a file. |
| 598 |
* |
* |
| 599 |
* @param string - the value to validate |
* @param string - the value to validate |
| 622 |
} |
} |
| 623 |
return TRUE; |
return TRUE; |
| 624 |
} |
} |
| 625 |
|
|
| 626 |
|
|
| 627 |
/** |
/** |
| 628 |
* This validates a string as a valid proper name. |
* This validates a string as a valid proper name. |
| 634 |
*/ |
*/ |
| 635 |
function is_name ($name) { |
function is_name ($name) { |
| 636 |
//letters & numbers only |
//letters & numbers only |
| 637 |
if ( !$this->is_alphanum($name, ".- '") ) { |
if ( !$this->is_alphanum($name, ".- 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåçèéêëìíîïðñòóôõöùúûüýÿŠšŸ") ) { |
| 638 |
return $this->_error("INVALID_NAME_CHARACTERS"); |
return $this->_error("INVALID_NAME_CHARACTERS"); |
| 639 |
} |
} |
| 640 |
// VALIDATE_MAXSIZE |
// VALIDATE_MAXSIZE |
| 661 |
|
|
| 662 |
|
|
| 663 |
/** |
/** |
| 664 |
* This method tests to see if a string value |
* This method tests to see if a string value |
| 665 |
* is a valid 'account' name. The string can't |
* is a valid 'account' name. The string can't |
| 666 |
* be larger then VALIDATE_MAXSIZE, and can only |
* be larger then VALIDATE_MAXSIZE, and can only |
| 667 |
* contain alphanum characters |
* contain alphanum characters |
| 700 |
if ( $len > VALIDATE_MAXSIZE ) { |
if ( $len > VALIDATE_MAXSIZE ) { |
| 701 |
return $this->_error("INVALID_PASSWORD_LENGTH"); |
return $this->_error("INVALID_PASSWORD_LENGTH"); |
| 702 |
} |
} |
|
if ( !$this->is_alphanum($password) ) { |
|
|
return $this->_error("INVALID_PASSWORD_CHARACTERS"); |
|
|
} |
|
| 703 |
return TRUE; |
return TRUE; |
| 704 |
} |
} |
| 705 |
|
|
| 741 |
* jane@blah.com or "Jane Doe <jane@blah.com>" |
* jane@blah.com or "Jane Doe <jane@blah.com>" |
| 742 |
* |
* |
| 743 |
* @param string - the value to validate |
* @param string - the value to validate |
| 744 |
|
* @param bool - allows long email name format |
| 745 |
* @return TRUE = succes FALSE = failed |
* @return TRUE = succes FALSE = failed |
| 746 |
*/ |
*/ |
| 747 |
function is_email ($email) { |
function is_email ($email, $allow_name = true) { |
| 748 |
|
|
| 749 |
//no quotes allowed |
//no quotes allowed |
| 750 |
if ( strstr($email, '"') || strstr($email, "'") ) { |
if ( strstr($email, '"') || strstr($email, "'") ) { |
| 755 |
//"Foo Bar <foo@bar.com>" |
//"Foo Bar <foo@bar.com>" |
| 756 |
$name = explode(" ", $email); |
$name = explode(" ", $email); |
| 757 |
if ( count($name) > 1 ) { |
if ( count($name) > 1 ) { |
| 758 |
|
|
| 759 |
|
if (!$allow_name) { |
| 760 |
|
return $this->_error("INVALID_EMAIL"); |
| 761 |
|
} |
| 762 |
|
|
| 763 |
//it looks like they gave us an email |
//it looks like they gave us an email |
| 764 |
//with a leading name such as Foo Bar <foo@bar.com> |
//with a leading name such as Foo Bar <foo@bar.com> |
| 765 |
//find the email address inside <> |
//find the email address inside <> |
| 788 |
} |
} |
| 789 |
|
|
| 790 |
// "@.", ".@.", "_@.", .com, .net, .edu, .blah |
// "@.", ".@.", "_@.", .com, .net, .edu, .blah |
| 791 |
if ( !ereg("^( )*([a-zA-Z0-9_/:]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $email, $arr) ) { |
if ( !ereg("^( )*([a-zA-Z0-9_/:]|\\-|\\.)+@(([a-zA-Z0-9_]|\\-)+\\.)+[a-zA-Z]{2,4}$", $email, $arr) ) { |
| 792 |
return $this->_error("INVALID_EMAIL"); |
return $this->_error("INVALID_EMAIL"); |
| 793 |
} |
} |
| 794 |
|
|
| 813 |
* This function tests a string that may |
* This function tests a string that may |
| 814 |
* contain many email addresses seperated by |
* contain many email addresses seperated by |
| 815 |
* commas |
* commas |
| 816 |
* |
* |
| 817 |
* @param string - the value to validate |
* @param string - the value to validate |
| 818 |
* @return TRUE = succes FALSE = failed |
* @return TRUE = succes FALSE = failed |
| 819 |
*/ |
*/ |
| 822 |
|
|
| 823 |
$email_arr = explode(",", $emails); |
$email_arr = explode(",", $emails); |
| 824 |
foreach( $email_arr as $key => $email) { |
foreach( $email_arr as $key => $email) { |
| 825 |
$res = $this->is_email($email); |
$res = $this->is_email(trim($email)); |
| 826 |
if ( $res !== TRUE ) { |
if ( $res !== TRUE ) { |
| 827 |
return $res; |
return $res; |
| 828 |
} |
} |
| 832 |
|
|
| 833 |
|
|
| 834 |
/** |
/** |
| 835 |
* This method validates a string as a |
* This method validates a string as a |
| 836 |
* leap year. |
* leap year. |
| 837 |
* |
* |
| 838 |
* @param string - the value to validate |
* @param string - the value to validate |
| 926 |
|
|
| 927 |
|
|
| 928 |
/** |
/** |
| 929 |
* This validates an array of values as a |
* This validates an array of values as a |
| 930 |
* valid date time |
* valid date time |
| 931 |
* |
* |
| 932 |
* NOTE: array must contain |
* NOTE: array must contain |
| 933 |
* array( |
* array( |
| 934 |
* "month" => VALUE, |
* "month" => VALUE, |
| 935 |
* "day" => VALUE, |
* "day" => VALUE, |
| 936 |
* "year" => VALUE, |
* "year" => VALUE, |
| 937 |
* "hour" => VALUE, |
* "hour" => VALUE, |
| 938 |
* "minutes" => VALUE, |
* "minutes" => VALUE, |
| 939 |
* "seconds" => VALUE); |
* "seconds" => VALUE); |
| 961 |
} |
} |
| 962 |
|
|
| 963 |
/** |
/** |
| 964 |
* This validates an array of fields as a |
* This validates an array of fields as a |
| 965 |
* valid time of the day |
* valid time of the day |
| 966 |
* |
* |
| 967 |
* NOTE: array must contain |
* NOTE: array must contain |
| 1052 |
$month = $x[1]; |
$month = $x[1]; |
| 1053 |
$day = $x[2]; |
$day = $x[2]; |
| 1054 |
|
|
| 1055 |
$d = sx_date_today(); |
$d = explode(",",date("Y,m,d")); |
| 1056 |
$today = $d[0] * 10000 + $d[1]; |
$today = $d[0] * 10000 + $d[1]; |
| 1057 |
$exp = $year * 10000 + $month; |
$exp = $year * 10000 + $month; |
| 1058 |
if ( $exp < $today ) { |
if ( $exp < $today ) { |
| 1079 |
} |
} |
| 1080 |
|
|
| 1081 |
/** |
/** |
| 1082 |
* This method validates a string as a valid url |
* This method validates a string as a valid url |
| 1083 |
* It inclues the prefix, hostname/ip, port number |
* It inclues the prefix, hostname/ip, port number |
| 1084 |
* and path. |
* and path. |
| 1085 |
* |
* |
| 1198 |
} |
} |
| 1199 |
return TRUE; |
return TRUE; |
| 1200 |
} |
} |
| 1201 |
|
|
| 1202 |
|
/** |
| 1203 |
|
* Validate if the string matches a regex |
| 1204 |
|
* |
| 1205 |
|
* @param string - the regex string |
| 1206 |
|
* @param string - the value to validate |
| 1207 |
|
* @return TRUE = succes FALSE = failed |
| 1208 |
|
*/ |
| 1209 |
|
function is_regex($regex, $str) { |
| 1210 |
|
if (preg_match($regex, $str)) { |
| 1211 |
|
return TRUE; |
| 1212 |
|
} else { |
| 1213 |
|
return FALSE; |
| 1214 |
|
} |
| 1215 |
|
} |
| 1216 |
|
|
| 1217 |
} |
} |
| 1218 |
?> |
?> |