checked = false; $this->last_check = false; $this->Langs = &$packages->getModule('langs'); $this->add($packages,$form_name, $group_name, $type, $def, $required, $add_info1, $add_info2, $add_info3); } function add(&$packages, $form_name, $group_name, $type, $def, $required, $add_info1, $add_info2, $add_info3) { $this->add_info1 = $add_info1; $this->add_info2 = $add_info2; $this->add_info3 = $add_info3; $this->form_name = $form_name; if (is_null($group_name)) $this->group_name = $form_name; else $this->group_name = $group_name; $this->display_name = $this->Langs->getGroupString('_validator', $this->group_name); $this->type = $type; $this->default_value = $def; $this->required = $required; switch ($this->type) { case VRT_REGEXP: // { $this->pattern = $add_info1; break; } // end of VRT_REGEXP case VRT_IMAGE_FILE: { break; } // end of VRT_IMAGE_FILE case VRT_CUSTOM_FILE: { break; } // end of VRT_CUSTOM_FILE case VRT_EMAIL: { $this->pattern = '/^ *([a-z0-9_-]+\.)*[a-z0-9_-]+@(([a-z0-9-]+\.)+(com|net|org|mil|edu|gov|arpa|info|biz|inc|name|[a-z]{2})|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) *$/i'; $this->min_length = 5; $this->max_length = 64; break; } // end of VRT_EMAIL case VRT_NUMBER: { $this->pattern = '/^[\-\+]?[0-9]+$/'; if (is_numeric($add_info1)) $this->min_numeric_value = $add_info1; if (is_numeric($add_info2)) $this->max_numeric_value = $add_info2; break; } // end of VRT_NUMBER case VRT_FLOAT: { $this->pattern = '/^[\-\+]{0,1}[0-9]+[\.]{0,1}[0-9]*$/'; if (is_numeric($add_info1)) $this->min_numeric_value = $add_info1; if (is_numeric($add_info2)) { $this->max_numeric_value = $add_info2; $this->max_length = strlen(''.$add_info2) + 3; } break; } //end of VRT_FLOAT case VRT_TEXT: { if (!is_null($add_info1)) $this->min_length = $add_info1; if (!is_null($add_info2)) $this->max_length = $add_info2; break; } // end of VRT_TEXT } } function validate_input(&$errors, &$infos) { if ($this->checked) return $this->last_check; $this->checked = true; $this->last_check = true; $value = $this->default_value; if ( ($this->type == VRT_IMAGE_FILE) || ($this->type == VRT_CUSTOM_FILE) ) { $value = ''; if (isset($_FILES[$this->form_name])) { if ( (intval($_FILES[$this->form_name]['size']) > 0) && (intval($_FILES[$this->form_name]['error']) == 0) ) { $value = $_FILES[$this->form_name]['name']; } } } else { $value = InPostGet($this->form_name, $this->default_value); } if (is_array($value)) foreach ($value as $k => $v) $value[$k] = trim($value[$k]); else { $value = trim($value); if ( (strlen($this->default_value)>0) && (strlen($value) == 0) ) $value = $this->default_value; } if ( (!$this->required) && ($value == '') ) { $this->last_check = true; return true; } if ( (!is_array($value)) && (strlen($value)==0) ) { $errors[$this->group_name] = $this->Langs->getGroupString('_validator_message', 'validator_empty_string', $this->display_name); $this->last_check = false; return false; } if ($this->min_length > 0) if (strlen($value) < $this->min_length) { $errors[$this->group_name] = $this->Langs->getGroupString('_validator_message', 'validator_min_length', $this->display_name, $this->min_length); $this->last_check = false; return false; } if ($this->max_length > 0) if (strlen($value) > $this->max_length) { $errors[$this->group_name] = $this->Langs->getGroupString('_validator_message', 'validator_max_length', $this->display_name, $this->max_length); $this->last_check = false; return false; } if (strlen($this->min_numeric_value) != 0) { $tmp = ((!is_array($value))?(array($value)):($value) ); foreach ($tmp as $vlv) if (doubleval($vlv) < doubleval($this->min_numeric_value)) { $errors[$this->group_name] = $this->Langs->getGroupString('_validator_message', 'validator_min_value', $this->display_name, $this->min_numeric_value); $this->last_check = false; return false; } } if (strlen($this->max_numeric_value)) { $tmp = ((!is_array($value))?(array($value)):($value) ); foreach ($tmp as $vlv) if (doubleval($vlv) > doubleval($this->max_numeric_value)) { $errors[$this->group_name] = $this->Langs->getGroupString('_validator_message', 'validator_max_value', $this->display_name, $this->max_numeric_value); $this->last_check = false; return false; } } if (strlen($this->pattern)) { $tmp = ((!is_array($value))?(array($value)):($value) ); foreach ($tmp as $vlv) { //print $this->pattern; if (!preg_match($this->pattern, $vlv)) { $errors[$this->group_name] = $this->Langs->getGroupString('_validator_message', 'validator_valid_value', $this->display_name); $this->last_check = false; return false; } } } if ( ($this->type == VRT_CUSTOM_FILE) && (!is_null($this->add_info1)) ) { if (!in_array($_FILES[$this->form_name]['type'], $this->add_info1)) { $errors[$this->group_name] = $this->Langs->getGroupString('_validator_message', 'validator_invalid_file', $this->display_name); $this->last_check = false; return false; } } if ($this->type == VRT_IMAGE_FILE) { $image_types = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png'); if (!in_array($_FILES[$this->form_name]['type'], $image_types)) { $errors[$this->group_name] = $this->Langs->getGroupString('_validator_message', 'validator_not_image_file', $this->display_name); $this->last_check = false; return false; } } if ($this->type == VRT_CALLBACK) { if (!call_user_func($this->add_info1, $this->group_name, $value)) { $errors[$this->group_name] = $this->Langs->getString('_validator_message', 'validator_valid_value', $this->display_name); $this->last_check = false; return false; } } if ($this->last_check) $infos[$this->group_name] = $this->form_name.' is valid'; return $this->last_check; } function get_js($current_running_form, $current_form_fields) { return ''; $out_js = ''; if (in_array($this->form_name, $current_form_fields)) if ($this->required) { $out_js .= 'if (f.elements["'.$this->form_name.'"]) {'; $out_js .= 'var el_val = GetValue(f.elements["'.$this->form_name.'"]);'; $out_js .= 'if (trim(el_val).length == 0) return ShowAlert(\''.$app->langs->get_gstring('_validator_message', 'validator_empty_string', js_escape_string($this->display_name)).'\', f, \''.$this->form_name.'\');'; if (strlen($this->min_numeric_value)) { $out_js .= 'if (isNaN(el_val)) return ShowAlert(\''.$app->langs->get_gstring('_validator_message', 'validator_need_number', js_escape_string($this->display_name), $this->min_numeric_value).'\', f, \''.$this->form_name.'\');'; $out_js .= 'if (parse_int(el_val) < '.$this->min_numeric_value.') return ShowAlert(\''.$app->langs->get_gstring('_validator_message', 'validator_min_value', js_escape_string($this->display_name), $this->min_numeric_value).'\', f, \''.$this->form_name.'\');'; } if (strlen($this->max_numeric_value)) { $out_js .= 'if (isNaN(el_val)) return ShowAlert(\''.$app->langs->get_gstring('_validator_message', 'validator_need_number', js_escape_string($this->display_name), $this->min_numeric_value).'\', f, \''.$this->form_name.'\');'; $out_js .= 'if (parse_int(el_val) > '.$this->max_numeric_value.') return ShowAlert(\''.$app->langs->get_gstring('_validator_message', 'validator_max_value', js_escape_string($this->display_name), $this->max_numeric_value).'\', f, \''.$this->form_name.'\');'; } if ($this->min_length > 0) { $out_js .= 'if (trim(el_val).length < '.$this->min_length.') return ShowAlert(\''.$app->langs->get_gstring('_validator_message', 'validator_min_length', js_escape_string($this->display_name), $this->min_length).'\', f, \''.$this->form_name.'\');'; } if ($this->type == VRT_EMAIL) $out_js .= 'if (!isValidEmail(el_val)) return ShowAlert(\''.$app->langs->get_gstring('_validator_message', 'validator_valid_value', js_escape_string($this->display_name), $this->min_length).'\', f, \''.$this->form_name.'\');'; $out_js .= '}'; } return $out_js; } } $internal_metas = array(); $input_fields = array(); $internal_errors = array(); $internal_infos = array(); class CValidator { function add(&$packages, $name, $type, $add_info1 = null, $add_info2 = null, $add_info3 = null) { global $internal_metas; $internal_metas[$name][] = new CValidatorMeta($packages,$name, null, $type, '', true, $add_info1, $add_info2, $add_info3); } function add_nr(&$packages, $name, $type, $def, $add_info1 = null, $add_info2 = null, $add_info3 = null) { global $internal_metas; $internal_metas[$name][] = new CValidatorMeta($packages, $name, null, $type, $def, false, $add_info1, $add_info2, $add_info3); } function add_in_group(&$packages, $name, $group_name, $type, $add_info1 = null, $add_info2 = null, $add_info3 = null) { global $internal_metas; $internal_metas[$name][] = new CValidatorMeta($packages,$name, $group_name, $type, '', true, $add_info1, $add_info2, $add_info3); } function add_in_group_nr(&$packages,$name, $group_name, $type, $def, $add_info1 = null, $add_info2 = null, $add_info3 = null) { global $internal_metas; $internal_metas[$name][] = new CValidatorMeta($packages,$name, $group_name, $type, $def, false, $add_info1, $add_info2, $add_info3); } function validate_input( $it_fs ) { global $internal_metas; global $input_fields; global $internal_errors, $internal_infos; $input_fields = $it_fs; $valid = true; $mts = array_keys($internal_metas); foreach ($mts as $k) { $mts2 = array_keys($internal_metas[$k]); foreach ($mts2 as $idx) $valid &= $internal_metas[$k][$idx]->validate_input($internal_errors, $internal_infos); } return $valid; } function get_infos() { return $GLOBALS['internal_infos']; } function get_errors() { return $GLOBALS['internal_errors']; } function validity($name) { global $internal_errors; global $internal_infos; if (isset($internal_errors[$name])) return V_INVALID; if (isset($internal_infos[$name])) return V_VALID; return V_NOT_CHECKED; } function get_js($current_running_form, $current_form_fields) { global $internal_metas; $out = ''; $out = ''; return $out; } function clear() { global $internal_metas; $internal_metas = array(); } } ?> can't set cookie