00001 <?php
00011
00012
00013
00014
00015 define('VALID_NOT_EMPTY', '/.+/');
00016 define('VALID_NUMBER', '/^[0-9]+$/');
00017 define('VALID_FLOAT', '/^[0-9]*\.?[0-9]+$/');
00018 define('VALID_EMAIL', '/\\A(?:^([a-z0-9][a-z0-9_\\-\\.\\+]*)@([a-z0-9][a-z0-9\\.\\-]{0,63}\\.(com|org|net|biz|info|name|net|pro|aero|coop|museum|[a-z]{2,4}))$)\\z/i');
00019 define('VALID_URL', '@((ht|f)tps?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@');
00020 define('VALID_DATE', '/^[12][0-9]{3}-[01][0-9]-[0123][0-9]$/');
00021 define('VALID_TIME', '/^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/');
00022 define('VALID_YEAR', '/^[12][0-9]{3}$/');
00023
00024 class Validator
00025 {
00036 function validate(&$errors, $var, $regex, $errorstr, $data=false)
00037 {
00038 $req = is_array($data) ? $data : Registry::get('pronto:request_args');
00039 $val = $this->prepare_input($req[$var]);
00040 if(!$this->is_valid($val, $regex)) {
00041 if(is_array($errors)) $errors[$var] = $errorstr;
00042 return false;
00043 }
00044 return true;
00045 }
00046
00055 function is_valid($value, $regex)
00056 {
00057 return !!preg_match($regex, $value);
00058 }
00059
00068 function required(&$errors, $vars, $data=false)
00069 {
00070 foreach($vars as $v) {
00071 $errstr = __('%s is required', ucwords(str_replace('_', ' ', $v)));
00072 $this->validate($errors, $v, VALID_NOT_EMPTY, $errstr, $data);
00073 }
00074 }
00075
00082 function prepare_input($val)
00083 {
00084 if(is_array($val)) {
00085 foreach($val as $k=>$v) {
00086 $val[$k] = $this->prepare_input($v);
00087 }
00088 return $val;
00089 }
00090 return get_magic_quotes_gpc() ? stripslashes($val) : $val;
00091 }
00092 }
00093
00094 ?>