Completed
Push — master ( 0e8edf...7a828e )
by Jamie
03:33
created
classes/models/FrmEntryValidate.php 1 patch
Indentation   +153 added lines, -153 removed lines patch added patch discarded remove patch
@@ -1,26 +1,26 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 class FrmEntryValidate {
4
-    public static function validate( $values, $exclude = false ) {
5
-        global $wpdb;
4
+	public static function validate( $values, $exclude = false ) {
5
+		global $wpdb;
6 6
 
7
-        FrmEntry::sanitize_entry_post( $values );
8
-        $errors = array();
7
+		FrmEntry::sanitize_entry_post( $values );
8
+		$errors = array();
9 9
 
10
-        if ( ! isset($values['form_id']) || ! isset($values['item_meta']) ) {
11
-            $errors['form'] = __( 'There was a problem with your submission. Please try again.', 'formidable' );
12
-            return $errors;
13
-        }
10
+		if ( ! isset($values['form_id']) || ! isset($values['item_meta']) ) {
11
+			$errors['form'] = __( 'There was a problem with your submission. Please try again.', 'formidable' );
12
+			return $errors;
13
+		}
14 14
 
15 15
 		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' ) ) ) {
16
-            $errors['form'] = __( 'You do not have permission to do that', 'formidable' );
17
-        }
16
+			$errors['form'] = __( 'You do not have permission to do that', 'formidable' );
17
+		}
18 18
 
19
-        if ( ! isset($values['item_key']) || $values['item_key'] == '' ) {
19
+		if ( ! isset($values['item_key']) || $values['item_key'] == '' ) {
20 20
 			$_POST['item_key'] = $values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' );
21
-        }
21
+		}
22 22
 
23
-        $where = apply_filters('frm_posted_field_ids', array( 'fi.form_id' => $values['form_id'] ) );
23
+		$where = apply_filters('frm_posted_field_ids', array( 'fi.form_id' => $values['form_id'] ) );
24 24
 		// Don't get subfields
25 25
 		$where['fr.parent_form_id'] = array( null, 0 );
26 26
 		// Don't get excluded fields (like file upload fields in the ajax validation)
@@ -28,42 +28,42 @@  discard block
 block discarded – undo
28 28
 			$where['fi.type not'] = $exclude;
29 29
 		}
30 30
 
31
-        $posted_fields = FrmField::getAll($where, 'field_order');
31
+		$posted_fields = FrmField::getAll($where, 'field_order');
32 32
 
33
-        // Pass exclude value to validate_field function so it can be used for repeating sections
34
-        $args = array( 'exclude' => $exclude );
33
+		// Pass exclude value to validate_field function so it can be used for repeating sections
34
+		$args = array( 'exclude' => $exclude );
35 35
 
36
-        foreach ( $posted_fields as $posted_field ) {
37
-            self::validate_field($posted_field, $errors, $values, $args);
38
-            unset($posted_field);
39
-        }
36
+		foreach ( $posted_fields as $posted_field ) {
37
+			self::validate_field($posted_field, $errors, $values, $args);
38
+			unset($posted_field);
39
+		}
40 40
 
41
-        // check for spam
42
-        self::spam_check( $exclude, $values, $errors );
41
+		// check for spam
42
+		self::spam_check( $exclude, $values, $errors );
43 43
 
44
-        $errors = apply_filters( 'frm_validate_entry', $errors, $values, compact('exclude') );
44
+		$errors = apply_filters( 'frm_validate_entry', $errors, $values, compact('exclude') );
45 45
 
46
-        return $errors;
47
-    }
46
+		return $errors;
47
+	}
48 48
 
49
-    public static function validate_field( $posted_field, &$errors, $values, $args = array() ) {
50
-        $defaults = array(
51
-            'id'              => $posted_field->id,
52
-            'parent_field_id' => '', // the id of the repeat or embed form
53
-            'key_pointer'     => '', // the pointer in the posted array
54
-            'exclude'         => array(), // exclude these field types from validation
55
-        );
56
-        $args = wp_parse_args( $args, $defaults );
49
+	public static function validate_field( $posted_field, &$errors, $values, $args = array() ) {
50
+		$defaults = array(
51
+			'id'              => $posted_field->id,
52
+			'parent_field_id' => '', // the id of the repeat or embed form
53
+			'key_pointer'     => '', // the pointer in the posted array
54
+			'exclude'         => array(), // exclude these field types from validation
55
+		);
56
+		$args = wp_parse_args( $args, $defaults );
57 57
 
58
-        if ( empty($args['parent_field_id']) ) {
58
+		if ( empty($args['parent_field_id']) ) {
59 59
 			$value = isset( $values['item_meta'][ $args['id'] ] ) ? $values['item_meta'][ $args['id'] ] : '';
60
-        } else {
61
-            // value is from a nested form
62
-            $value = $values;
63
-        }
60
+		} else {
61
+			// value is from a nested form
62
+			$value = $values;
63
+		}
64 64
 
65
-        // Check for values in "Other" fields
66
-        FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args );
65
+		// Check for values in "Other" fields
66
+		FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args );
67 67
 
68 68
 		self::maybe_clear_value_for_default_blank_setting( $posted_field, $value );
69 69
 
@@ -72,11 +72,11 @@  discard block
 block discarded – undo
72 72
 			$value = reset($value);
73 73
 		}
74 74
 
75
-        if ( $posted_field->required == '1' && ! is_array( $value ) && trim( $value ) == '' ) {
75
+		if ( $posted_field->required == '1' && ! is_array( $value ) && trim( $value ) == '' ) {
76 76
 			$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'blank' );
77
-        } else if ( $posted_field->type == 'text' && ! isset( $_POST['item_name'] ) ) {
78
-            $_POST['item_name'] = $value;
79
-        }
77
+		} else if ( $posted_field->type == 'text' && ! isset( $_POST['item_name'] ) ) {
78
+			$_POST['item_name'] = $value;
79
+		}
80 80
 
81 81
 		if ( $value != '' ) {
82 82
 			self::validate_url_field( $errors, $posted_field, $value, $args );
@@ -85,13 +85,13 @@  discard block
 block discarded – undo
85 85
 			self::validate_phone_field( $errors, $posted_field, $value, $args );
86 86
 		}
87 87
 
88
-        FrmEntriesHelper::set_posted_value($posted_field, $value, $args);
88
+		FrmEntriesHelper::set_posted_value($posted_field, $value, $args);
89 89
 
90
-        self::validate_recaptcha($errors, $posted_field, $args);
90
+		self::validate_recaptcha($errors, $posted_field, $args);
91 91
 
92 92
 		$errors = apply_filters( 'frm_validate_' . $posted_field->type . '_field_entry', $errors, $posted_field, $value, $args );
93 93
 		$errors = apply_filters( 'frm_validate_field_entry', $errors, $posted_field, $value, $args );
94
-    }
94
+	}
95 95
 
96 96
 	private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) {
97 97
 		if ( FrmField::is_option_true_in_object( $field, 'default_blank' ) && $value == $field->default_value ) {
@@ -101,32 +101,32 @@  discard block
 block discarded – undo
101 101
 
102 102
 	public static function validate_url_field( &$errors, $field, &$value, $args ) {
103 103
 		if ( $value == '' || ! in_array( $field->type, array( 'website', 'url', 'image' ) ) ) {
104
-            return;
105
-        }
104
+			return;
105
+		}
106 106
 
107
-        if ( trim($value) == 'http://' ) {
108
-            $value = '';
109
-        } else {
110
-            $value = esc_url_raw( $value );
107
+		if ( trim($value) == 'http://' ) {
108
+			$value = '';
109
+		} else {
110
+			$value = esc_url_raw( $value );
111 111
 			$value = preg_match( '/^(https?|ftps?|mailto|news|feed|telnet):/is', $value ) ? $value : 'http://' . $value;
112
-        }
112
+		}
113 113
 
114
-        // validate the url format
114
+		// validate the url format
115 115
 		if ( ! preg_match('/^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i', $value) ) {
116 116
 			$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
117 117
 		}
118
-    }
118
+	}
119 119
 
120 120
 	public static function validate_email_field( &$errors, $field, $value, $args ) {
121
-        if ( $value == '' || $field->type != 'email' ) {
122
-            return;
123
-        }
121
+		if ( $value == '' || $field->type != 'email' ) {
122
+			return;
123
+		}
124 124
 
125
-        //validate the email format
126
-        if ( ! is_email($value) ) {
125
+		//validate the email format
126
+		if ( ! is_email($value) ) {
127 127
 			$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
128
-        }
129
-    }
128
+		}
129
+	}
130 130
 
131 131
 	public static function validate_number_field( &$errors, $field, $value, $args ) {
132 132
 		//validate the number format
@@ -220,9 +220,9 @@  discard block
 block discarded – undo
220 220
 	}
221 221
 
222 222
 	public static function validate_recaptcha( &$errors, $field, $args ) {
223
-        if ( $field->type != 'captcha' || FrmAppHelper::is_admin() || apply_filters( 'frm_is_field_hidden', false, $field, stripslashes_deep( $_POST ) ) ) {
224
-            return;
225
-        }
223
+		if ( $field->type != 'captcha' || FrmAppHelper::is_admin() || apply_filters( 'frm_is_field_hidden', false, $field, stripslashes_deep( $_POST ) ) ) {
224
+			return;
225
+		}
226 226
 
227 227
 		$frm_settings = FrmAppHelper::get_settings();
228 228
 		if ( empty( $frm_settings->pubkey ) ) {
@@ -230,57 +230,57 @@  discard block
 block discarded – undo
230 230
 			return;
231 231
 		}
232 232
 
233
-        if ( ! isset($_POST['g-recaptcha-response']) ) {
234
-            // If captcha is missing, check if it was already verified
233
+		if ( ! isset($_POST['g-recaptcha-response']) ) {
234
+			// If captcha is missing, check if it was already verified
235 235
 			if ( ! isset( $_POST['recaptcha_checked'] ) || ! wp_verify_nonce( $_POST['recaptcha_checked'], 'frm_ajax' ) ) {
236
-                // There was no captcha submitted
236
+				// There was no captcha submitted
237 237
 				$errors[ 'field' . $args['id'] ] = __( 'The captcha is missing from this form', 'formidable' );
238
-            }
239
-            return;
240
-        }
238
+			}
239
+			return;
240
+		}
241 241
 
242
-        $arg_array = array(
243
-            'body'      => array(
242
+		$arg_array = array(
243
+			'body'      => array(
244 244
 				'secret'   => $frm_settings->privkey,
245 245
 				'response' => $_POST['g-recaptcha-response'],
246 246
 				'remoteip' => FrmAppHelper::get_ip_address(),
247 247
 			),
248 248
 		);
249
-        $resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array );
250
-        $response = json_decode(wp_remote_retrieve_body( $resp ), true);
249
+		$resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array );
250
+		$response = json_decode(wp_remote_retrieve_body( $resp ), true);
251 251
 
252
-        if ( isset( $response['success'] ) && ! $response['success'] ) {
253
-            // What happens when the CAPTCHA was entered incorrectly
252
+		if ( isset( $response['success'] ) && ! $response['success'] ) {
253
+			// What happens when the CAPTCHA was entered incorrectly
254 254
 			$errors[ 'field' . $args['id'] ] = ( ! isset( $field->field_options['invalid'] ) || $field->field_options['invalid'] == '' ) ? $frm_settings->re_msg : $field->field_options['invalid'];
255
-        } else if ( is_wp_error( $resp ) ) {
255
+		} else if ( is_wp_error( $resp ) ) {
256 256
 			$error_string = $resp->get_error_message();
257 257
 			$errors[ 'field' . $args['id'] ] = __( 'There was a problem verifying your recaptcha', 'formidable' );
258 258
 			$errors[ 'field' . $args['id'] ] .= ' ' . $error_string;
259
-        }
260
-    }
261
-
262
-    /**
263
-     * check for spam
264
-     * @param boolean $exclude
265
-     * @param array $values
266
-     * @param array $errors by reference
267
-     */
268
-    public static function spam_check( $exclude, $values, &$errors ) {
269
-        if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
270
-            // only check spam if there are no other errors
271
-            return;
272
-        }
273
-
274
-        if ( self::is_akismet_spam( $values ) ) {
259
+		}
260
+	}
261
+
262
+	/**
263
+	 * check for spam
264
+	 * @param boolean $exclude
265
+	 * @param array $values
266
+	 * @param array $errors by reference
267
+	 */
268
+	public static function spam_check( $exclude, $values, &$errors ) {
269
+		if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
270
+			// only check spam if there are no other errors
271
+			return;
272
+		}
273
+
274
+		if ( self::is_akismet_spam( $values ) ) {
275 275
 			if ( self::is_akismet_enabled_for_user( $values['form_id'] ) ) {
276 276
 				$errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
277 277
 			}
278
-	    }
278
+		}
279 279
 
280
-    	if ( self::blacklist_check( $values ) ) {
281
-            $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
282
-    	}
283
-    }
280
+		if ( self::blacklist_check( $values ) ) {
281
+			$errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
282
+		}
283
+	}
284 284
 
285 285
 	private static function is_akismet_spam( $values ) {
286 286
 		global $wpcom_api_key;
@@ -292,54 +292,54 @@  discard block
 block discarded – undo
292 292
 		return ( isset( $form->options['akismet'] ) && ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] != 'logged' || ! is_user_logged_in() ) );
293 293
 	}
294 294
 
295
-    public static function blacklist_check( $values ) {
296
-        if ( ! apply_filters('frm_check_blacklist', true, $values) ) {
297
-            return false;
298
-        }
295
+	public static function blacklist_check( $values ) {
296
+		if ( ! apply_filters('frm_check_blacklist', true, $values) ) {
297
+			return false;
298
+		}
299 299
 
300
-    	$mod_keys = trim( get_option( 'blacklist_keys' ) );
300
+		$mod_keys = trim( get_option( 'blacklist_keys' ) );
301 301
 
302
-    	if ( empty( $mod_keys ) ) {
303
-    		return false;
304
-    	}
302
+		if ( empty( $mod_keys ) ) {
303
+			return false;
304
+		}
305 305
 
306
-    	$content = FrmEntriesHelper::entry_array_to_string($values);
306
+		$content = FrmEntriesHelper::entry_array_to_string($values);
307 307
 
308 308
 		if ( empty($content) ) {
309
-		    return false;
309
+			return false;
310 310
 		}
311 311
 
312
-    	$words = explode( "\n", $mod_keys );
312
+		$words = explode( "\n", $mod_keys );
313 313
 
314
-    	foreach ( (array) $words as $word ) {
315
-    		$word = trim( $word );
314
+		foreach ( (array) $words as $word ) {
315
+			$word = trim( $word );
316 316
 
317
-    		if ( empty($word) ) {
318
-    			continue;
319
-    		}
317
+			if ( empty($word) ) {
318
+				continue;
319
+			}
320 320
 
321
-    		if ( preg_match('#' . preg_quote( $word, '#' ) . '#', $content) ) {
322
-    			return true;
323
-    		}
324
-    	}
321
+			if ( preg_match('#' . preg_quote( $word, '#' ) . '#', $content) ) {
322
+				return true;
323
+			}
324
+		}
325 325
 
326
-    	return false;
327
-    }
326
+		return false;
327
+	}
328 328
 
329
-    /**
330
-     * Check entries for spam
331
-     *
332
-     * @return boolean true if is spam
333
-     */
334
-    public static function akismet( $values ) {
335
-	    $content = FrmEntriesHelper::entry_array_to_string( $values );
329
+	/**
330
+	 * Check entries for spam
331
+	 *
332
+	 * @return boolean true if is spam
333
+	 */
334
+	public static function akismet( $values ) {
335
+		$content = FrmEntriesHelper::entry_array_to_string( $values );
336 336
 
337 337
 		if ( empty( $content ) ) {
338
-		    return false;
338
+			return false;
339 339
 		}
340 340
 
341
-        $datas = array();
342
-        self::parse_akismet_array( $datas, $content );
341
+		$datas = array();
342
+		self::parse_akismet_array( $datas, $content );
343 343
 
344 344
 		$query_string = '';
345 345
 		foreach ( $datas as $key => $data ) {
@@ -347,35 +347,35 @@  discard block
 block discarded – undo
347 347
 			unset( $key, $data );
348 348
 		}
349 349
 
350
-        $response = Akismet::http_post($query_string, 'comment-check');
350
+		$response = Akismet::http_post($query_string, 'comment-check');
351 351
 
352 352
 		return ( is_array( $response ) && $response[1] == 'true' );
353
-    }
354
-
355
-    /**
356
-     * @since 2.0
357
-     * @param string $content
358
-     */
359
-    private  static function parse_akismet_array( &$datas, $content ) {
360
-        $datas['blog'] = FrmAppHelper::site_url();
361
-        $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() );
353
+	}
354
+
355
+	/**
356
+	 * @since 2.0
357
+	 * @param string $content
358
+	 */
359
+	private  static function parse_akismet_array( &$datas, $content ) {
360
+		$datas['blog'] = FrmAppHelper::site_url();
361
+		$datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() );
362 362
 		$datas['user_agent'] = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
363 363
 		$datas['referrer'] = isset( $_SERVER['HTTP_REFERER'] ) ? FrmAppHelper::get_server_value( 'HTTP_REFERER' ) : false;
364
-        $datas['comment_type'] = 'formidable';
365
-        $datas['comment_content'] = $content;
364
+		$datas['comment_type'] = 'formidable';
365
+		$datas['comment_content'] = $content;
366 366
 
367
-        if ( $permalink = get_permalink() ) {
368
-            $datas['permalink'] = $permalink;
369
-        }
367
+		if ( $permalink = get_permalink() ) {
368
+			$datas['permalink'] = $permalink;
369
+		}
370 370
 
371
-        foreach ( $_SERVER as $key => $value ) {
371
+		foreach ( $_SERVER as $key => $value ) {
372 372
 			if ( ! in_array( $key, array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ) ) && is_string( $value ) ) {
373 373
 				$datas[ $key ] = wp_strip_all_tags( $value );
374
-            } else {
374
+			} else {
375 375
 				$datas[ $key ] = '';
376
-            }
376
+			}
377 377
 
378
-            unset($key, $value);
379
-        }
380
-    }
378
+			unset($key, $value);
379
+		}
380
+	}
381 381
 }
Please login to merge, or discard this patch.