@@ -1,18 +1,18 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | class FrmEntryValidate { |
4 | - public static function validate( $values, $exclude = false ) { |
|
5 | - FrmEntry::sanitize_entry_post( $values ); |
|
6 | - $errors = array(); |
|
4 | + public static function validate( $values, $exclude = false ) { |
|
5 | + FrmEntry::sanitize_entry_post( $values ); |
|
6 | + $errors = array(); |
|
7 | 7 | |
8 | - if ( ! isset($values['form_id']) || ! isset($values['item_meta']) ) { |
|
9 | - $errors['form'] = __( 'There was a problem with your submission. Please try again.', 'formidable' ); |
|
10 | - return $errors; |
|
11 | - } |
|
8 | + if ( ! isset($values['form_id']) || ! isset($values['item_meta']) ) { |
|
9 | + $errors['form'] = __( 'There was a problem with your submission. Please try again.', 'formidable' ); |
|
10 | + return $errors; |
|
11 | + } |
|
12 | 12 | |
13 | 13 | if ( FrmAppHelper::is_admin() && is_user_logged_in() && ( ! isset( $values[ 'frm_submit_entry_' . $values['form_id'] ] ) || ! wp_verify_nonce( $values[ 'frm_submit_entry_' . $values['form_id'] ], 'frm_submit_entry_nonce' ) ) ) { |
14 | - $errors['form'] = __( 'You do not have permission to do that', 'formidable' ); |
|
15 | - } |
|
14 | + $errors['form'] = __( 'You do not have permission to do that', 'formidable' ); |
|
15 | + } |
|
16 | 16 | |
17 | 17 | self::set_item_key( $values ); |
18 | 18 | |
@@ -69,24 +69,24 @@ discard block |
||
69 | 69 | return FrmField::getAll( $where, 'field_order' ); |
70 | 70 | } |
71 | 71 | |
72 | - public static function validate_field( $posted_field, &$errors, $values, $args = array() ) { |
|
73 | - $defaults = array( |
|
74 | - 'id' => $posted_field->id, |
|
75 | - 'parent_field_id' => '', // the id of the repeat or embed form |
|
76 | - 'key_pointer' => '', // the pointer in the posted array |
|
77 | - 'exclude' => array(), // exclude these field types from validation |
|
78 | - ); |
|
79 | - $args = wp_parse_args( $args, $defaults ); |
|
72 | + public static function validate_field( $posted_field, &$errors, $values, $args = array() ) { |
|
73 | + $defaults = array( |
|
74 | + 'id' => $posted_field->id, |
|
75 | + 'parent_field_id' => '', // the id of the repeat or embed form |
|
76 | + 'key_pointer' => '', // the pointer in the posted array |
|
77 | + 'exclude' => array(), // exclude these field types from validation |
|
78 | + ); |
|
79 | + $args = wp_parse_args( $args, $defaults ); |
|
80 | 80 | |
81 | - if ( empty($args['parent_field_id']) ) { |
|
81 | + if ( empty($args['parent_field_id']) ) { |
|
82 | 82 | $value = isset( $values['item_meta'][ $args['id'] ] ) ? $values['item_meta'][ $args['id'] ] : ''; |
83 | - } else { |
|
84 | - // value is from a nested form |
|
85 | - $value = $values; |
|
86 | - } |
|
83 | + } else { |
|
84 | + // value is from a nested form |
|
85 | + $value = $values; |
|
86 | + } |
|
87 | 87 | |
88 | - // Check for values in "Other" fields |
|
89 | - FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args ); |
|
88 | + // Check for values in "Other" fields |
|
89 | + FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args ); |
|
90 | 90 | |
91 | 91 | self::maybe_clear_value_for_default_blank_setting( $posted_field, $value ); |
92 | 92 | |
@@ -95,11 +95,11 @@ discard block |
||
95 | 95 | $value = reset($value); |
96 | 96 | } |
97 | 97 | |
98 | - if ( $posted_field->required == '1' && ! is_array( $value ) && trim( $value ) == '' ) { |
|
98 | + if ( $posted_field->required == '1' && ! is_array( $value ) && trim( $value ) == '' ) { |
|
99 | 99 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'blank' ); |
100 | - } else if ( $posted_field->type == 'text' && ! isset( $_POST['item_name'] ) ) { |
|
101 | - $_POST['item_name'] = $value; |
|
102 | - } |
|
100 | + } else if ( $posted_field->type == 'text' && ! isset( $_POST['item_name'] ) ) { |
|
101 | + $_POST['item_name'] = $value; |
|
102 | + } |
|
103 | 103 | |
104 | 104 | if ( $value != '' ) { |
105 | 105 | self::validate_url_field( $errors, $posted_field, $value, $args ); |
@@ -108,13 +108,13 @@ discard block |
||
108 | 108 | self::validate_phone_field( $errors, $posted_field, $value, $args ); |
109 | 109 | } |
110 | 110 | |
111 | - FrmEntriesHelper::set_posted_value($posted_field, $value, $args); |
|
111 | + FrmEntriesHelper::set_posted_value($posted_field, $value, $args); |
|
112 | 112 | |
113 | - self::validate_recaptcha($errors, $posted_field, $args); |
|
113 | + self::validate_recaptcha($errors, $posted_field, $args); |
|
114 | 114 | |
115 | 115 | $errors = apply_filters( 'frm_validate_' . $posted_field->type . '_field_entry', $errors, $posted_field, $value, $args ); |
116 | 116 | $errors = apply_filters( 'frm_validate_field_entry', $errors, $posted_field, $value, $args ); |
117 | - } |
|
117 | + } |
|
118 | 118 | |
119 | 119 | private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) { |
120 | 120 | if ( FrmField::is_option_true_in_object( $field, 'default_blank' ) && $value == $field->default_value ) { |
@@ -124,32 +124,32 @@ discard block |
||
124 | 124 | |
125 | 125 | public static function validate_url_field( &$errors, $field, &$value, $args ) { |
126 | 126 | if ( $value == '' || ! in_array( $field->type, array( 'website', 'url', 'image' ) ) ) { |
127 | - return; |
|
128 | - } |
|
127 | + return; |
|
128 | + } |
|
129 | 129 | |
130 | - if ( trim($value) == 'http://' ) { |
|
131 | - $value = ''; |
|
132 | - } else { |
|
133 | - $value = esc_url_raw( $value ); |
|
130 | + if ( trim($value) == 'http://' ) { |
|
131 | + $value = ''; |
|
132 | + } else { |
|
133 | + $value = esc_url_raw( $value ); |
|
134 | 134 | $value = preg_match( '/^(https?|ftps?|mailto|news|feed|telnet):/is', $value ) ? $value : 'http://' . $value; |
135 | - } |
|
135 | + } |
|
136 | 136 | |
137 | - // validate the url format |
|
137 | + // validate the url format |
|
138 | 138 | if ( ! preg_match('/^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i', $value) ) { |
139 | 139 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); |
140 | 140 | } |
141 | - } |
|
141 | + } |
|
142 | 142 | |
143 | 143 | public static function validate_email_field( &$errors, $field, $value, $args ) { |
144 | - if ( $value == '' || $field->type != 'email' ) { |
|
145 | - return; |
|
146 | - } |
|
144 | + if ( $value == '' || $field->type != 'email' ) { |
|
145 | + return; |
|
146 | + } |
|
147 | 147 | |
148 | - //validate the email format |
|
149 | - if ( ! is_email($value) ) { |
|
148 | + //validate the email format |
|
149 | + if ( ! is_email($value) ) { |
|
150 | 150 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); |
151 | - } |
|
152 | - } |
|
151 | + } |
|
152 | + } |
|
153 | 153 | |
154 | 154 | public static function validate_number_field( &$errors, $field, $value, $args ) { |
155 | 155 | //validate the number format |
@@ -243,9 +243,9 @@ discard block |
||
243 | 243 | } |
244 | 244 | |
245 | 245 | public static function validate_recaptcha( &$errors, $field, $args ) { |
246 | - if ( $field->type != 'captcha' || FrmAppHelper::is_admin() || apply_filters( 'frm_is_field_hidden', false, $field, stripslashes_deep( $_POST ) ) ) { |
|
247 | - return; |
|
248 | - } |
|
246 | + if ( $field->type != 'captcha' || FrmAppHelper::is_admin() || apply_filters( 'frm_is_field_hidden', false, $field, stripslashes_deep( $_POST ) ) ) { |
|
247 | + return; |
|
248 | + } |
|
249 | 249 | |
250 | 250 | $frm_settings = FrmAppHelper::get_settings(); |
251 | 251 | if ( empty( $frm_settings->pubkey ) ) { |
@@ -253,61 +253,61 @@ discard block |
||
253 | 253 | return; |
254 | 254 | } |
255 | 255 | |
256 | - if ( ! isset($_POST['g-recaptcha-response']) ) { |
|
257 | - // If captcha is missing, check if it was already verified |
|
256 | + if ( ! isset($_POST['g-recaptcha-response']) ) { |
|
257 | + // If captcha is missing, check if it was already verified |
|
258 | 258 | if ( ! isset( $_POST['recaptcha_checked'] ) || ! wp_verify_nonce( $_POST['recaptcha_checked'], 'frm_ajax' ) ) { |
259 | - // There was no captcha submitted |
|
259 | + // There was no captcha submitted |
|
260 | 260 | $errors[ 'field' . $args['id'] ] = __( 'The captcha is missing from this form', 'formidable' ); |
261 | - } |
|
262 | - return; |
|
263 | - } |
|
261 | + } |
|
262 | + return; |
|
263 | + } |
|
264 | 264 | |
265 | - $arg_array = array( |
|
266 | - 'body' => array( |
|
265 | + $arg_array = array( |
|
266 | + 'body' => array( |
|
267 | 267 | 'secret' => $frm_settings->privkey, |
268 | 268 | 'response' => $_POST['g-recaptcha-response'], |
269 | 269 | 'remoteip' => FrmAppHelper::get_ip_address(), |
270 | 270 | ), |
271 | 271 | ); |
272 | - $resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array ); |
|
273 | - $response = json_decode(wp_remote_retrieve_body( $resp ), true); |
|
272 | + $resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array ); |
|
273 | + $response = json_decode(wp_remote_retrieve_body( $resp ), true); |
|
274 | 274 | |
275 | - if ( isset( $response['success'] ) && ! $response['success'] ) { |
|
276 | - // What happens when the CAPTCHA was entered incorrectly |
|
275 | + if ( isset( $response['success'] ) && ! $response['success'] ) { |
|
276 | + // What happens when the CAPTCHA was entered incorrectly |
|
277 | 277 | $errors[ 'field' . $args['id'] ] = ( ! isset( $field->field_options['invalid'] ) || $field->field_options['invalid'] == '' ) ? $frm_settings->re_msg : $field->field_options['invalid']; |
278 | - } else if ( is_wp_error( $resp ) ) { |
|
278 | + } else if ( is_wp_error( $resp ) ) { |
|
279 | 279 | $error_string = $resp->get_error_message(); |
280 | 280 | $errors[ 'field' . $args['id'] ] = __( 'There was a problem verifying your recaptcha', 'formidable' ); |
281 | 281 | $errors[ 'field' . $args['id'] ] .= ' ' . $error_string; |
282 | - } |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * check for spam |
|
287 | - * @param boolean $exclude |
|
288 | - * @param array $values |
|
289 | - * @param array $errors by reference |
|
290 | - */ |
|
291 | - public static function spam_check( $exclude, $values, &$errors ) { |
|
292 | - if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { |
|
293 | - // only check spam if there are no other errors |
|
294 | - return; |
|
295 | - } |
|
282 | + } |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * check for spam |
|
287 | + * @param boolean $exclude |
|
288 | + * @param array $values |
|
289 | + * @param array $errors by reference |
|
290 | + */ |
|
291 | + public static function spam_check( $exclude, $values, &$errors ) { |
|
292 | + if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { |
|
293 | + // only check spam if there are no other errors |
|
294 | + return; |
|
295 | + } |
|
296 | 296 | |
297 | 297 | if ( self::is_honeypot_spam() || self::is_spam_bot() ) { |
298 | 298 | $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' ); |
299 | 299 | } |
300 | 300 | |
301 | - if ( self::blacklist_check( $values ) ) { |
|
302 | - $errors['spam'] = __( 'Your entry appears to be blacklist spam!', 'formidable' ); |
|
303 | - } |
|
301 | + if ( self::blacklist_check( $values ) ) { |
|
302 | + $errors['spam'] = __( 'Your entry appears to be blacklist spam!', 'formidable' ); |
|
303 | + } |
|
304 | 304 | |
305 | - if ( self::is_akismet_spam( $values ) ) { |
|
305 | + if ( self::is_akismet_spam( $values ) ) { |
|
306 | 306 | if ( self::is_akismet_enabled_for_user( $values['form_id'] ) ) { |
307 | 307 | $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' ); |
308 | 308 | } |
309 | - } |
|
310 | - } |
|
309 | + } |
|
310 | + } |
|
311 | 311 | |
312 | 312 | private static function is_honeypot_spam() { |
313 | 313 | $honeypot_value = FrmAppHelper::get_param( 'frm_verify', '', 'get', 'sanitize_text_field' ); |
@@ -329,15 +329,15 @@ discard block |
||
329 | 329 | return ( isset( $form->options['akismet'] ) && ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] != 'logged' || ! is_user_logged_in() ) ); |
330 | 330 | } |
331 | 331 | |
332 | - public static function blacklist_check( $values ) { |
|
333 | - if ( ! apply_filters('frm_check_blacklist', true, $values) ) { |
|
334 | - return false; |
|
335 | - } |
|
332 | + public static function blacklist_check( $values ) { |
|
333 | + if ( ! apply_filters('frm_check_blacklist', true, $values) ) { |
|
334 | + return false; |
|
335 | + } |
|
336 | 336 | |
337 | - $mod_keys = trim( get_option( 'blacklist_keys' ) ); |
|
338 | - if ( empty( $mod_keys ) ) { |
|
339 | - return false; |
|
340 | - } |
|
337 | + $mod_keys = trim( get_option( 'blacklist_keys' ) ); |
|
338 | + if ( empty( $mod_keys ) ) { |
|
339 | + return false; |
|
340 | + } |
|
341 | 341 | |
342 | 342 | $content = FrmEntriesHelper::entry_array_to_string( $values ); |
343 | 343 | if ( empty( $content ) ) { |
@@ -349,7 +349,7 @@ discard block |
||
349 | 349 | $user_info = self::get_spam_check_user_info( $values ); |
350 | 350 | |
351 | 351 | return wp_blacklist_check( $user_info['comment_author'], $user_info['comment_author_email'], $user_info['comment_author_url'], $content, $ip, $user_agent ); |
352 | - } |
|
352 | + } |
|
353 | 353 | |
354 | 354 | /** |
355 | 355 | * Check entries for Akismet spam |