| 1 |
<?php |
| 2 |
/** |
| 3 |
* Validation related functions |
| 4 |
* |
| 5 |
* All UsersWP form validation handled here. |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @author GeoDirectory Team <info@wpgeodirectory.com> |
| 9 |
*/ |
| 10 |
class UsersWP_Validation { |
| 11 |
|
| 12 |
/** |
| 13 |
* Validates the submitted form data. |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* @package userswp |
| 17 |
* |
| 18 |
* @param array $data Submitted form data |
| 19 |
* @param string $type Form type. |
| 20 |
* @param array|bool $fields Fields applicable for validation. |
| 21 |
* |
| 22 |
* @return array|mixed|void|WP_Error Validated form data. |
| 23 |
*/ |
| 24 |
public function validate_fields($data, $type, $fields = false) { |
| 25 |
|
| 26 |
$errors = new WP_Error(); |
| 27 |
|
| 28 |
$errors = apply_filters('uwp_validate_fields_before', $errors, $data, $type); |
| 29 |
|
| 30 |
$error_code = $errors->get_error_code(); |
| 31 |
if (!empty($error_code)) { |
| 32 |
return $errors; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
if (!$fields) { |
| 37 |
global $wpdb; |
| 38 |
$table_name = uwp_get_table_prefix() . 'uwp_form_fields'; |
| 39 |
if ($type == 'register') { |
| 40 |
if (isset($data["uwp_role_id"])) { |
| 41 |
$role_id = (int) strip_tags(esc_sql($data["uwp_role_id"])); |
| 42 |
} else { |
| 43 |
$role_id = 0; |
| 44 |
} |
| 45 |
$fields = get_register_validate_form_fields($role_id); |
| 46 |
} elseif ($type == 'change') { |
| 47 |
$fields = get_change_validate_form_fields(); |
| 48 |
} elseif ($type == 'account') { |
| 49 |
$fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = %s AND field_type != 'fieldset' AND field_type != 'file' AND is_active = '1' AND is_register_only_field = '0' ORDER BY sort_order ASC", array('account'))); |
| 50 |
} else { |
| 51 |
$fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = %s AND field_type != 'fieldset' AND field_type != 'file' AND is_active = '1' ORDER BY sort_order ASC", array($type))); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
$validated_data = array(); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
$email_field = uwp_get_custom_field_info('uwp_account_email'); |
| 61 |
$email_extra = array(); |
| 62 |
if (isset($email_field->extra_fields) && $email_field->extra_fields != '') { |
| 63 |
$email_extra = unserialize($email_field->extra_fields); |
| 64 |
} |
| 65 |
$enable_confirm_email_field = isset($email_extra['confirm_email']) ? $email_extra['confirm_email'] : '0'; |
| 66 |
|
| 67 |
$password_field = uwp_get_custom_field_info('uwp_account_password'); |
| 68 |
$enable_password = $password_field->is_active; |
| 69 |
$password_extra = array(); |
| 70 |
if (isset($password_field->extra_fields) && $password_field->extra_fields != '') { |
| 71 |
$password_extra = unserialize($password_field->extra_fields); |
| 72 |
} |
| 73 |
$enable_confirm_password_field = isset($password_extra['confirm_password']) ? $password_extra['confirm_password'] : '0'; |
| 74 |
|
| 75 |
$enable_old_password = uwp_get_option('change_enable_old_password', false); |
| 76 |
|
| 77 |
if ($type == 'account' || $type == 'change') { |
| 78 |
if (!is_user_logged_in()) { |
| 79 |
$errors->add('not_logged_in', __('<strong>Error</strong>: Permission denied.', 'userswp')); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if (!empty($fields)) { |
| 84 |
foreach ($fields as $field) { |
| 85 |
|
| 86 |
if (!isset($data[$field->htmlvar_name]) && $field->is_required != 1) { |
| 87 |
continue; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
if ($type == 'register') { |
| 92 |
|
| 93 |
if ($enable_password != '1') { |
| 94 |
if ( ($field->htmlvar_name == 'uwp_account_password') OR ($field->htmlvar_name == 'uwp_account_confirm_password') ) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if ($enable_confirm_email_field != '1') { |
| 100 |
if ( $field->htmlvar_name == 'uwp_account_confirm_email' ) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
if (!isset($data[$field->htmlvar_name]) && $field->is_required == 1) { |
| 108 |
if (is_admin()) { |
| 109 |
//do nothing since admin edit fields can be empty |
| 110 |
} else { |
| 111 |
if ($field->required_msg) { |
| 112 |
$errors->add('empty_'.$field->htmlvar_name, __('<strong>Error</strong>: '.$field->site_title.' '.$field->required_msg, 'userswp')); |
| 113 |
} else { |
| 114 |
$errors->add('empty_'.$field->htmlvar_name, __('<strong>Error</strong>: '.$field->site_title.' cannot be empty.', 'userswp')); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
$error_code = $errors->get_error_code(); |
| 120 |
if (!empty($error_code)) { |
| 121 |
return $errors; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
$value = isset($data[$field->htmlvar_name]) ? $data[$field->htmlvar_name] : ''; |
| 126 |
$sanitized_value = $value; |
| 127 |
|
| 128 |
if ($field->field_type == 'password') { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
$sanitized = false; |
| 133 |
|
| 134 |
// sanitize our default fields |
| 135 |
switch($field->htmlvar_name) { |
| 136 |
|
| 137 |
case 'uwp_register_username': |
| 138 |
case 'uwp_account_username': |
| 139 |
case 'uwp_login_username': |
| 140 |
case 'uwp_reset_username': |
| 141 |
$sanitized_value = sanitize_user($value); |
| 142 |
$sanitized = true; |
| 143 |
break; |
| 144 |
|
| 145 |
case 'uwp_register_first_name': |
| 146 |
case 'uwp_register_last_name': |
| 147 |
case 'uwp_account_first_name': |
| 148 |
case 'uwp_account_last_name': |
| 149 |
$sanitized_value = sanitize_text_field($value); |
| 150 |
$sanitized = true; |
| 151 |
break; |
| 152 |
|
| 153 |
case 'uwp_register_email': |
| 154 |
case 'uwp_forgot_email': |
| 155 |
case 'uwp_account_email': |
| 156 |
$sanitized_value = sanitize_email($value); |
| 157 |
$sanitized = true; |
| 158 |
break; |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
if (!$sanitized && !empty($value)) { |
| 163 |
// sanitize by field type |
| 164 |
switch($field->field_type) { |
| 165 |
|
| 166 |
case 'text': |
| 167 |
$sanitized_value = sanitize_text_field($value); |
| 168 |
break; |
| 169 |
|
| 170 |
case 'checkbox': |
| 171 |
$sanitized_value = sanitize_text_field($value); |
| 172 |
break; |
| 173 |
|
| 174 |
case 'email': |
| 175 |
$sanitized_value = sanitize_email($value); |
| 176 |
break; |
| 177 |
|
| 178 |
case 'multiselect': |
| 179 |
$sanitized_value = array_map( 'sanitize_text_field', $value ); |
| 180 |
break; |
| 181 |
|
| 182 |
case 'datepicker': |
| 183 |
$sanitized_value = sanitize_text_field($value); |
| 184 |
$extra_fields = unserialize($field->extra_fields); |
| 185 |
|
| 186 |
if ($extra_fields['date_format'] == '') |
| 187 |
$extra_fields['date_format'] = 'yy-mm-dd'; |
| 188 |
|
| 189 |
$date_format = $extra_fields['date_format']; |
| 190 |
|
| 191 |
if (!empty($sanitized_value)) { |
| 192 |
$date_value = uwp_date($sanitized_value, 'Y-m-d', $date_format); |
| 193 |
$sanitized_value = strtotime($date_value); |
| 194 |
} |
| 195 |
break; |
| 196 |
|
| 197 |
default: |
| 198 |
$sanitized_value = sanitize_text_field($value); |
| 199 |
|
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
if ($field->is_required == 1 && $sanitized_value == '') { |
| 205 |
if (is_admin()) { |
| 206 |
//do nothing since admin edit fields can be empty |
| 207 |
} else { |
| 208 |
if ($field->required_msg) { |
| 209 |
$errors->add('empty_'.$field->htmlvar_name, __('<strong>Error</strong>: '.$field->site_title.' '.$field->required_msg, 'userswp')); |
| 210 |
} else { |
| 211 |
$errors->add('empty_'.$field->htmlvar_name, __('<strong>Error</strong>: '.$field->site_title.' cannot be empty.', 'userswp')); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
if ($field->field_type == 'email' && !empty($sanitized_value) && !is_email($sanitized_value)) { |
| 217 |
$incorrect_email_error_msg = apply_filters('uwp_incorrect_email_error_msg', __('<strong>Error</strong>: The email address isn’t correct.', 'userswp')); |
| 218 |
$errors->add('invalid_email', $incorrect_email_error_msg); |
| 219 |
} |
| 220 |
|
| 221 |
//register email |
| 222 |
if ($type == 'register' && $field->htmlvar_name == 'uwp_account_email' && email_exists($sanitized_value)) { |
| 223 |
$errors->add('email_exists', __('<strong>Error</strong>: This email is already registered, please choose another one.', 'userswp')); |
| 224 |
} |
| 225 |
|
| 226 |
//forgot email |
| 227 |
if ($field->htmlvar_name == 'uwp_forgot_email' && !email_exists($sanitized_value)) { |
| 228 |
$errors->add('email_exists', __('<strong>Error</strong>: This email doesn\'t exists.', 'userswp')); |
| 229 |
} |
| 230 |
|
| 231 |
$incorrect_username_error_msg = apply_filters('uwp_incorrect_username_error_msg', __('<strong>Error</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.', 'userswp')); |
| 232 |
|
| 233 |
// Check the username for register |
| 234 |
if ($field->htmlvar_name == 'uwp_account_username') { |
| 235 |
if (!is_admin()) { |
| 236 |
if (!validate_username($sanitized_value)) { |
| 237 |
$errors->add('invalid_username', $incorrect_username_error_msg); |
| 238 |
} |
| 239 |
if (username_exists($sanitized_value)) { |
| 240 |
$errors->add('username_exists', __('<strong>Error</strong>: This username is already registered. Please choose another one.', 'userswp')); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// Check the username for login |
| 246 |
if ($field->htmlvar_name == 'uwp_login_username') { |
| 247 |
if (!validate_username($sanitized_value)) { |
| 248 |
$errors->add('invalid_username', $incorrect_username_error_msg); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
$validated_data[$field->htmlvar_name] = $sanitized_value; |
| 254 |
|
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
$error_code = $errors->get_error_code(); |
| 259 |
if (!empty($error_code)) { |
| 260 |
return $errors; |
| 261 |
} |
| 262 |
|
| 263 |
if ($type == 'login') { |
| 264 |
$password_type = 'login'; |
| 265 |
} elseif ($type == 'reset') { |
| 266 |
$password_type = 'reset'; |
| 267 |
} elseif ($type == 'change') { |
| 268 |
$password_type = 'change'; |
| 269 |
} else { |
| 270 |
$password_type = 'account'; |
| 271 |
} |
| 272 |
|
| 273 |
if (($type == 'change' && $enable_old_password == '1')) { |
| 274 |
//check old password |
| 275 |
if( empty( $data['uwp_'.$password_type.'_old_password'] ) ) { |
| 276 |
$errors->add( 'empty_password', __( '<strong>Error</strong>: Please enter your old password', 'userswp' ) ); |
| 277 |
} |
| 278 |
|
| 279 |
$error_code = $errors->get_error_code(); |
| 280 |
if (!empty($error_code)) { |
| 281 |
return $errors; |
| 282 |
} |
| 283 |
|
| 284 |
$pass = $data['uwp_'.$password_type.'_old_password']; |
| 285 |
$user = get_user_by( 'id', get_current_user_id() ); |
| 286 |
if ( !wp_check_password( $pass, $user->data->user_pass, $user->ID) ) { |
| 287 |
$errors->add( 'invalid_password', __( '<strong>Error</strong>: Incorrect old password', 'userswp' ) ); |
| 288 |
} |
| 289 |
|
| 290 |
$error_code = $errors->get_error_code(); |
| 291 |
if (!empty($error_code)) { |
| 292 |
return $errors; |
| 293 |
} |
| 294 |
|
| 295 |
if( $data['uwp_'.$password_type.'_old_password'] == $data['uwp_'.$password_type.'_password'] ) { |
| 296 |
$errors->add( 'invalid_password', __( '<strong>Error</strong>: Old password and new password are same', 'userswp' ) ); |
| 297 |
} |
| 298 |
|
| 299 |
$error_code = $errors->get_error_code(); |
| 300 |
if (!empty($error_code)) { |
| 301 |
return $errors; |
| 302 |
} |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
if (($type == 'register' && $enable_confirm_email_field == '1')) { |
| 307 |
//check confirm email |
| 308 |
if( empty( $data['uwp_account_email'] ) ) { |
| 309 |
$errors->add( 'empty_email', __( '<strong>Error</strong>: Please enter your Email', 'userswp' ) ); |
| 310 |
} |
| 311 |
|
| 312 |
$error_code = $errors->get_error_code(); |
| 313 |
if (!empty($error_code)) { |
| 314 |
return $errors; |
| 315 |
} |
| 316 |
|
| 317 |
if( !isset($data['uwp_account_confirm_email']) || empty( $data['uwp_account_confirm_email'] ) ) { |
| 318 |
$errors->add( 'empty_confirm_email', __( '<strong>Error</strong>: Please fill Confirm Email field', 'userswp' ) ); |
| 319 |
} |
| 320 |
|
| 321 |
$error_code = $errors->get_error_code(); |
| 322 |
if (!empty($error_code)) { |
| 323 |
return $errors; |
| 324 |
} |
| 325 |
|
| 326 |
if( $data['uwp_account_email'] != $data['uwp_account_confirm_email'] ) { |
| 327 |
$errors->add( 'email_mismatch', __( '<strong>Error</strong>: Email and Confirm email not match', 'userswp' ) ); |
| 328 |
} |
| 329 |
|
| 330 |
$error_code = $errors->get_error_code(); |
| 331 |
if (!empty($error_code)) { |
| 332 |
return $errors; |
| 333 |
} |
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
if ($type == 'change' || $type == 'reset' || $type == 'login' || ($type == 'register' && $enable_password == '1')) { |
| 338 |
//check password |
| 339 |
if( empty( $data['uwp_'.$password_type.'_password'] ) ) { |
| 340 |
$errors->add( 'empty_password', __( 'Please enter a password', 'userswp' ) ); |
| 341 |
} |
| 342 |
|
| 343 |
if ($type != 'login' && strlen($data['uwp_'.$password_type.'_password']) < 7) { |
| 344 |
$errors->add('pass_match', __('ERROR: Password must be 7 characters or more.', 'userswp')); |
| 345 |
} |
| 346 |
|
| 347 |
$validated_data['password'] = $data['uwp_'.$password_type.'_password']; |
| 348 |
} |
| 349 |
|
| 350 |
$error_code = $errors->get_error_code(); |
| 351 |
if (!empty($error_code)) { |
| 352 |
return $errors; |
| 353 |
} |
| 354 |
|
| 355 |
if (($type == 'register' && $enable_password == '1') || $type == 'reset' || $type == 'change') { |
| 356 |
|
| 357 |
if (($type == 'register' && $enable_confirm_password_field != '1')) { |
| 358 |
$validated_data['password'] = $data['uwp_'.$password_type.'_password']; |
| 359 |
} else { |
| 360 |
//check password |
| 361 |
if ($data['uwp_'.$password_type.'_password'] != $data['uwp_'.$password_type.'_confirm_password']) { |
| 362 |
$errors->add('pass_match', __('ERROR: Passwords do not match.', 'userswp')); |
| 363 |
} |
| 364 |
|
| 365 |
$validated_data['password'] = $data['uwp_'.$password_type.'_password']; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
$error_code = $errors->get_error_code(); |
| 371 |
if (!empty($error_code)) { |
| 372 |
return $errors; |
| 373 |
} |
| 374 |
|
| 375 |
return $validated_data; |
| 376 |
} |
| 377 |
|
| 378 |
} |