* $frm_login = new form('login_form.htm', 'login.php', 'loginfrm', 'POST'); * * * @param string $skin_filename * @param string $action * @param string $method * @return form */ function form($skin_filename, $action = '', $name = '', $method = 'POST') { if (!defined('FORM_ERROR_INVALID_CALLBACK')) { /** * Error code sent when an invalid callback function is used. * * @ignore */ define('FORM_ERROR_INVALID_CALLBACK', 0); /** * Error code sent when a field is assigned a non-existant validator * * @ignore */ define('FORM_ERROR_INVALID_VALIDATOR', 1); /** * Error code sent when a field is assigned a non-existant field type * * @ignore */ define('FORM_ERROR_INVALID_FIELD_TYPE', 2); /** * Error code sent when an error occurs in an eval() * * @ignore */ define('FORM_ERROR_INVALID_EVAL', 3); } $this->_skin_filename = $skin_filename; $this->_skin_object = new template($skin_filename); $this->_skin_field = new template(); $this->_form_action = $action; $this->_form_method = $method; $this->_form_name = $name; $this->add_field_type('hidden', '$this->_field_type_hidden'); $this->add_field_type('text', '$this->_field_type_text'); $this->add_field_type('textarea', '$this->_field_type_textarea'); $this->add_field_type('fcktextarea', '$this->_field_type_fcktextarea'); $this->add_field_type('password', '$this->_field_type_password'); $this->add_field_type('submit', '$this->_field_type_submit'); $this->add_field_type('button', '$this->_field_type_button'); $this->add_field_type('phone', '$this->_field_type_phone_number'); $this->add_field_type('yesno', '$this->_field_type_yesno'); $this->add_field_type('checkbox', '$this->_field_type_checkbox'); $this->add_field_type('yesnolabel', '$this->_field_type_yesnolabel'); $this->add_field_type('country', '$this->_field_type_country'); $this->add_field_type('state', '$this->_field_type_state'); $this->add_field_type('malefemale', '$this->_field_type_malefemale'); $this->add_field_type('label', '$this->_field_type_label'); $this->add_field_type('select', '$this->_field_type_selectbox'); $this->add_field_type('file', '$this->_field_type_file'); $this->add_field_type('date', '$this->_field_type_date'); $this->add_validator('date', '$this->_date_validator'); $this->add_validator('email', '$this->_email_validator'); $this->add_validator('required', '$this->_required_validator'); $this->add_validator('username_exists', '$this->_username_validator'); $this->add_validator('select', '$this->_selectbox_validator'); } /** * Assign a callback function to a validator. * * * $frm_login = new form('login_form.htm', 'login.php', 'POST'); * $frm_login->add_validator('username_validator', 'username_check'); * $frm_login->add_field('Username', 'username', 'text', 'username_validator'); * * * Sample callback function: * * * function _required_validator($name, $value) * { * if ($value != '') * { * return(true); * } * else * { * return(array('error' => 'true', 'error_message' => 'Field is Empty')); * } * } * * * @param string $name * @param string $callback */ function add_validator($name, $callback) { if (substr($callback, 0, 7) == '$this->') { if (!method_exists($this, substr($callback, 7))) { $this->_report_error(FORM_ERROR_INVALID_CALLBACK, '$this->'.substr($callback, 7)); } } elseif (!function_exists($callback)) { $this->_report_error(FORM_ERROR_INVALID_CALLBACK, $callback); } $this->_validators[$name] = $callback; } /** * Assign a callback function to a field type. * * This function is used to create custom fields. * * Sample callback function: * * * function _field_type_text($title, $name, $value, $error, $error_message) * { * $field_html = $this->_skin_object->get_block('input text'); * $this->_skin_field->load_raw($field_html); * $this->_skin_field->assign('title', $title); * $this->_skin_field->assign('name', $name); * $this->_skin_field->assign('value', $value); * $this->_skin_field->assign('error', $error); * $this->_skin_field->assign('error_message', $error_message); * return($this->_skin_field->output(true)); * } * * * @param string $field_type * @param callback $callback */ function add_field_type($field_type, $callback) { if (substr($callback, 0, 7) == '$this->') { if (!method_exists($this, substr($callback, 7))) { $this->_report_error(FORM_ERROR_INVALID_CALLBACK, '$this->'.substr($callback, 7)); } } elseif (!function_exists($callback)) { $this->_report_error(FORM_ERROR_INVALID_CALLBACK, $callback); } $this->_field_types[$field_type] = $callback; } /** * This function is used to add a field to the form. * * The $title parameter is used as a title for the field. * The $validator and $field_value parameters are optional. * * Sample code: * * * $frm_login = new form('login_form.htm', 'login.php', 'POST'); * $frm_login->add_validator('username_validator', 'username_check'); * $frm_login->add_field('Username:', 'username', 'text', 'username_validator'); * * * @param string $title * @param string $field_name * @param string $field_type * @param string $validator * @param string $field_default_value */ function form_add_field($title, $field_name, $field_type, $validator = '', $field_default_value = '') { if (is_array($validator)) { foreach($validator as $validator_name) { if (!isset($this->_validators[$validator_name])) { $this->_report_error(FORM_ERROR_INVALID_VALIDATOR, $validator_name); } } } elseif (!isset($this->_validators[$validator]) && $validator != '') { $this->_report_error(FORM_ERROR_INVALID_VALIDATOR, $validator); } if (!isset($this->_field_types[$field_type])) { $this->_report_error(FORM_ERROR_INVALID_FIELD_TYPE, $field_type); } $this->_fields[$field_name] = array( 'title' => $title, 'name' => $field_name, 'type' => $field_type, 'validator' => $validator, 'default_value' => $field_default_value ); } /** * Used to validate the results of a form. * * Sample code: * * $form = new form('form_skin.htm'); * $form->form_add_field('Email', 'emailtest', 'text', array('required', 'email')); * $form->form_add_field('Field Test', 'field2', 'text', 'required'); * $form->form_add_field('', 'submitbutton', 'submit'); * * $validate_result = $form->form_validate(); * * if ($validate_result) * { * echo "form validated!"; * } * else * { * if (empty($_POST)) * { * $form_html = $form->form_display(false); * } * else * { * $form_html = $form->form_display(); * } * * $t = new template('test.htm'); * $t->assign('form', $form_html); * $t->output(); * } * * * @param string $method * @return bool */ function form_validate($method = "POST") { if (sizeof($this->_fields) == 0) { return(true); } $form_success = true; foreach($this->_fields as $key => $field) { switch($method) { case 'POST': $value = post($field['name'], $field['default_value'], false); break; case 'GET': $value = get($field['name'], $field['default_value'], false); break; } $this->_fields[$key]['value'] = $value; if (is_array($field['validator'])) { foreach($field['validator'] as $validator) { if (is_array($value)) { $value = serialize($value); $validate_code = sprintf('$validate_result = %s("%s", "%s", true);', $this->_validators[$validator], addslashes($field['name']), addslashes($value)); } else { $validate_code = sprintf('$validate_result = %s("%s", "%s");', $this->_validators[$validator], addslashes($field['name']), addslashes($value)); } eval($validate_code); if ($validate_result !== true) { break; } } } elseif ($field['validator'] != '') { if (is_array($value)) { $value = serialize($value); $validate_code = sprintf('$validate_result = %s("%s", "%s", true);', $this->_validators[$field['validator']], addslashes($field['name']), addslashes($value)); } else { $validate_code = sprintf('$validate_result = %s("%s", "%s");', $this->_validators[$field['validator']], addslashes($field['name']), addslashes($value)); } eval($validate_code); } else { $validate_result = true; } if ($validate_result === true) { unset($this->_fields[$key]['error']); unset($this->_fields[$key]['error_message']); } else { $form_success = false; $this->_fields[$key]['error'] = true; $this->_fields[$key]['error_message'] = $validate_result['error_message']; } } return($form_success); } /** * Manually set an error message for a specific field. * * @param string $field * @param string $error_message */ function form_set_error($field, $error_message) { if (isset($this->_fields[$field])) { $this->_fields[$field]['error'] = true; $this->_fields[$field]['error_message'] = $error_message; } } /** * Used to display the generated form. This method is used when the form layout is too complex * to generate using a standard skin. All replaces are assigned into the $template. * * $show_errors is used to determine whether or not error messages will be shown. * * @param template $template * @param bool $show_errors * @return bool */ function form_manual_display(&$template, $show_errors = true) { if (sizeof($this->_fields) > 0) { foreach($this->_fields as $key => $field) { $error = 0; $error_message = ''; $value = ''; if ($show_errors) { if (isset($field['error'])) { $error = $field['error']; } if (isset($field['error_message'])) { $error_message = $field['error_message']; } } if (isset($field['value'])) { $value = $field['value']; } if (is_array($value)) { $value = serialize($value); } $value = str_replace(array("\\'", "'"), array("'", "\'"), $value); $field_html_code = sprintf('$field_html = %s(\'%s\', \'%s\', \'%s\', \'%s\', \'%s\');', $this->_field_types[$field['type']], addslashes($field['title']), addslashes($field['name']), $value, $error, addslashes($error_message)); eval($field_html_code); $template->assign('form_field_'.$field['name'], $field_html); } } $template->assign('form_method', $this->_form_method); $template->assign('form_action', $this->_form_action); $template->assign('form_name', $this->_form_name); return(true); } /** * Used to display the generated form. * * $show_errors is used to determine whether or not error messages will be shown. * * @param bool $show_errors * @return string */ function form_display($show_errors = true) { $form_html = $this->_skin_object->get_block('form'); $form_fields_html = ''; if (sizeof($this->_fields) > 0) { foreach($this->_fields as $key => $field) { $error = 0; $error_message = ''; $value = ''; if ($show_errors) { if (isset($field['error'])) { $error = $field['error']; } if (isset($field['error_message'])) { $error_message = $field['error_message']; } } if (isset($field['value'])) { $value = $field['value']; } if (is_array($value)) { $value = serialize($value); } $field_html_code = sprintf('$field_html = %s(\'%s\', \'%s\', \'%s\', \'%s\', \'%s\');', $this->_field_types[$field['type']], addslashes($field['title']), addslashes($field['name']), $value, $error, addslashes($error_message)); eval($field_html_code); $this->_skin_object->assign('form_field_'.$field['name'], $field_html); $form_fields_html .= $field_html; } } $this->_skin_object->load_raw($form_html); $this->_skin_object->assign('form_content', $form_fields_html); $this->_skin_object->assign('form_method', $this->_form_method); $this->_skin_object->assign('form_action', $this->_form_action); $this->_skin_object->assign('form_name', $this->_form_name); return($this->_skin_object->output(true)); } /** * Field Type: Hidden * * @ignore */ function _field_type_hidden($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input hidden'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', $value); $this->_skin_field->assign('error_message', $error_message); return($this->_skin_field->output(true)); } /** * Field Type: Input Text * * @ignore */ function _field_type_text($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input text'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', htmlentities($value)); if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: Text Area * * @ignore */ function _field_type_textarea($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input textarea'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', htmlentities($value)); if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: FCK Text Area * * @ignore */ function _field_type_fcktextarea($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input fcktextarea'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', htmlentities($value)); if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: Input Password * * @ignore */ function _field_type_password($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input password'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', htmlentities($value)); if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: Submit Button * * @ignore */ function _field_type_submit($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input submit'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); return($this->_skin_field->output(true)); } /** * Field Type: Button * * @ignore */ function _field_type_button($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input button'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); return($this->_skin_field->output(true)); } /** * Field Type: Label * * @ignore */ function _field_type_label($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input label'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('value', $value); return($this->_skin_field->output(true)); } /** * Field Type: Country Select Box * * @ignore */ function _field_type_country($title, $name, $value, $error, $error_message) { global $mysql; global $CONFIG; $lang = $CONFIG['current_lang']; $field_html = $this->_skin_object->get_block('input country'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', $value); $query_countries = " SELECT countries_index.global_country_id value, name FROM global_countries_index countries_index, global_countries_content countries_content WHERE countries_content.global_country_id = countries_index.global_country_id AND lang = '{$lang}' ORDER BY name ASC; "; $rset_countries = $mysql->query($query_countries); $this->_skin_field->loop('countries', $rset_countries); if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: State Select Box * * @ignore */ function _field_type_state($title, $name, $value, $error, $error_message) { global $mysql; global $CONFIG; $lang = $CONFIG['current_lang']; $field_html = $this->_skin_object->get_block('input state'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', $value); $query_states = " SELECT states_index.global_state_id value, name FROM global_states_index states_index, global_states_content states_content WHERE states_content.global_state_id = states_index.global_state_id AND lang = '{$lang}' ORDER BY name ASC; "; $rset_states = $mysql->query($query_states); $this->_skin_field->loop('states', $rset_states); if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Validator: Select Box * * @ignore */ function _selectbox_validator($name, $value) { $options = $this->_fields[$name]['default_value']['options']; if (sizeof($options) > 0) { foreach($options as $option) { if ($option['value'] == $value) { return(true); } } } return(array('error' => 'true', 'error_message' => 'Field is Empty')); } /** * Field Type: Select Box * * @ignore */ function _field_type_selectbox($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input selectbox'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $select = @unserialize($value); if (!is_array($select)) { $loop_options = $this->_fields[$name]['default_value']['options']; } else { $loop_options = $select['options']; $value = $select['value']; } $this->_skin_field->loop('selectbox', $loop_options); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', $value); if ($error_message != '' || (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: Yes/No Selection * * @ignore */ function _field_type_yesno($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input yesno'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', $value); if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: Checkbox * * @ignore */ function _field_type_checkbox($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input checkbox'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', $value); if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: Yes/No Selection * * @ignore */ function _field_type_yesnolabel($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input yesnolabel'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); if ($value) { $this->_skin_field->assign('value', TRANSLATE('yes')); } else { $this->_skin_field->assign('value', TRANSLATE('no')); } if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: Male/Female Selection * * @ignore */ function _field_type_malefemale($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input malefemale'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', $value); if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: File * * @ignore */ function _field_type_file($title, $name, $value, $error, $error_message) { $field_html = $this->_skin_object->get_block('input file'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); $this->_skin_field->assign('value', $value); if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Field Type: Careers Date Selector * * @ignore */ function _field_type_date($title, $name, $value, $error, $error_message) { global $mysql; global $CONFIG; if (is_numeric($value) && $value != '') { $value = date('j-n-Y', $value); } $lang = $CONFIG['current_lang']; $field_html = $this->_skin_object->get_block('input date'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); if ((int)$value != '') { $this->_skin_field->assign('value', $value); } else { $this->_skin_field->assign('value', ''); } if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Validator: Date * * @ignore */ function _date_validator($name, $value) { $timestamp = date_to_timestamp($value); if ($timestamp > 0) { return(true); } else { return(array('error' => 'true', 'error_message' => 'Field is Empty')); } } /** * Field Type: Phone Number * * @ignore */ function _field_type_phone_number($title, $name, $value, $error, $error_message) { $value = @unserialize($value); $field_html = $this->_skin_object->get_block('input phone'); $this->_skin_field->load_raw($field_html); $this->_skin_field->reset(); $this->_skin_field->assign('title', $title); $this->_skin_field->assign('name', $name); if (sizeof($value) == 3) { $this->_skin_field->assign('value_1', $value[0]); $this->_skin_field->assign('value_2', $value[1]); $this->_skin_field->assign('value_3', $value[2]); } else { $this->_skin_field->assign('value_1', ''); $this->_skin_field->assign('value_2', ''); $this->_skin_field->assign('value_3', ''); } if ($error_message != '' && (bool)$error) { $this->_skin_field->assign('error', $error); $this->_skin_field->assign('error_message', $error_message); } return($this->_skin_field->output(true)); } /** * Called by the class when an error occurs. * * @ignore */ function _report_error($number) { if (!isset($this->_errors[$number])) { return(false); } if (func_num_args() > 1) { $func_args = ''; for($argId = 1; $argId < func_num_args(); $argId++) { $argument = func_get_arg($argId); $func_args .= "'".addslashes($argument)."',"; } $func_args = substr($func_args, 0, -1); $eval_code = '$message = sprintf("'.addslashes($this->_errors[$number]).'",'.$func_args.');'; eval($eval_code); } else { $message = $this->_errors[$number]; } trigger_error($message, E_USER_ERROR); } /** * Validator: Email * * @ignore */ function _email_validator($name, $value) { if(ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)", $value)) { return(true); } else { return(array('error' => 'true', 'error_message' => 'Invalid E-mail Address')); } } /** * Validator: Username Exist * * @ignore */ function _username_validator($name, $value) { global $mysql; $query_username = " SELECT count(user_id) amount FROM admin_users WHERE username = '{$value}'; "; $user = $mysql->fetch_record($mysql->query($query_username)); if ($user['amount'] == '0') { return(true); } else { return(array('error' => 'true', 'error_message' => 'Username Already in Use!')); } } /** * Validator: Required Field * * @ignore */ function _required_validator($name, $value, $is_array = false) { if ($is_array) { $value = @unserialize($value); } if (!is_array($value) && $value != '') { return(true); } elseif (is_array($value) && sizeof($value) > 0) { foreach($value as $key => $single_value) { if ($single_value == '') { return(array('error' => 'true', 'error_message' => 'Field is Empty')); } } return(true); } else { return(array('error' => 'true', 'error_message' => 'Field is Empty')); } } /** * Set $object to the form skin object * * @param object $object */ function get_skin_object(&$object) { $object &= $this->_skin_object; } /** * Set $object to the form skin field object * * @param object $object */ function get_skin_field_object(&$object) { $object &= $this->_skin_field; } } ?>