Completed
Push — master ( cbe6b2...d6428c )
by Jamie
03:34
created
classes/models/FrmEntryValidate.php 1 patch
Indentation   +156 added lines, -156 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
 
@@ -73,11 +73,11 @@  discard block
 block discarded – undo
73 73
 			$value = reset($value);
74 74
 		}
75 75
 
76
-        if ( $posted_field->required == '1' && ! is_array( $value ) && trim( $value ) == '' ) {
76
+		if ( $posted_field->required == '1' && ! is_array( $value ) && trim( $value ) == '' ) {
77 77
 			$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'blank' );
78
-        } else if ( $posted_field->type == 'text' && ! isset( $_POST['item_name'] ) ) {
79
-            $_POST['item_name'] = $value;
80
-        }
78
+		} else if ( $posted_field->type == 'text' && ! isset( $_POST['item_name'] ) ) {
79
+			$_POST['item_name'] = $value;
80
+		}
81 81
 
82 82
 		if ( $value != '' ) {
83 83
 			self::validate_url_field( $errors, $posted_field, $value, $args );
@@ -86,13 +86,13 @@  discard block
 block discarded – undo
86 86
 			self::validate_phone_field( $errors, $posted_field, $value, $args );
87 87
 		}
88 88
 
89
-        FrmEntriesHelper::set_posted_value($posted_field, $value, $args);
89
+		FrmEntriesHelper::set_posted_value($posted_field, $value, $args);
90 90
 
91
-        self::validate_recaptcha($errors, $posted_field, $args);
91
+		self::validate_recaptcha($errors, $posted_field, $args);
92 92
 
93
-        $errors = apply_filters('frm_validate_field_entry', $errors, $posted_field, $value, $args);
93
+		$errors = apply_filters('frm_validate_field_entry', $errors, $posted_field, $value, $args);
94 94
 		$errors = apply_filters( 'frm_validate_' . $posted_field->type . '_field_entry', $errors, $posted_field, $value, $args );
95
-    }
95
+	}
96 96
 
97 97
 	private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) {
98 98
 		if ( FrmField::is_option_true_in_object( $field, 'default_blank' ) && $value == $field->default_value ) {
@@ -102,32 +102,32 @@  discard block
 block discarded – undo
102 102
 
103 103
 	public static function validate_url_field( &$errors, $field, &$value, $args ) {
104 104
 		if ( $value == '' || ! in_array( $field->type, array( 'website', 'url', 'image' ) ) ) {
105
-            return;
106
-        }
105
+			return;
106
+		}
107 107
 
108
-        if ( trim($value) == 'http://' ) {
109
-            $value = '';
110
-        } else {
111
-            $value = esc_url_raw( $value );
108
+		if ( trim($value) == 'http://' ) {
109
+			$value = '';
110
+		} else {
111
+			$value = esc_url_raw( $value );
112 112
 			$value = preg_match( '/^(https?|ftps?|mailto|news|feed|telnet):/is', $value ) ? $value : 'http://' . $value;
113
-        }
113
+		}
114 114
 
115
-        //validate the url format
116
-        if ( ! preg_match('/^http(s)?:\/\/([\da-z\.-]+)\.([\da-z\.-]+)/i', $value) ) {
115
+		//validate the url format
116
+		if ( ! preg_match('/^http(s)?:\/\/([\da-z\.-]+)\.([\da-z\.-]+)/i', $value) ) {
117 117
 			$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
118
-        }
119
-    }
118
+		}
119
+	}
120 120
 
121 121
 	public static function validate_email_field( &$errors, $field, $value, $args ) {
122
-        if ( $value == '' || $field->type != 'email' ) {
123
-            return;
124
-        }
122
+		if ( $value == '' || $field->type != 'email' ) {
123
+			return;
124
+		}
125 125
 
126
-        //validate the email format
127
-        if ( ! is_email($value) ) {
126
+		//validate the email format
127
+		if ( ! is_email($value) ) {
128 128
 			$errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
129
-        }
130
-    }
129
+		}
130
+	}
131 131
 
132 132
 	public static function validate_number_field( &$errors, $field, $value, $args ) {
133 133
 		//validate the number format
@@ -204,9 +204,9 @@  discard block
 block discarded – undo
204 204
 	}
205 205
 
206 206
 	public static function validate_recaptcha( &$errors, $field, $args ) {
207
-        if ( $field->type != 'captcha' || FrmAppHelper::is_admin() || apply_filters( 'frm_is_field_hidden', false, $field, stripslashes_deep( $_POST ) ) ) {
208
-            return;
209
-        }
207
+		if ( $field->type != 'captcha' || FrmAppHelper::is_admin() || apply_filters( 'frm_is_field_hidden', false, $field, stripslashes_deep( $_POST ) ) ) {
208
+			return;
209
+		}
210 210
 
211 211
 		$frm_settings = FrmAppHelper::get_settings();
212 212
 		if ( empty( $frm_settings->pubkey ) ) {
@@ -214,57 +214,57 @@  discard block
 block discarded – undo
214 214
 			return;
215 215
 		}
216 216
 
217
-        if ( ! isset($_POST['g-recaptcha-response']) ) {
218
-            // If captcha is missing, check if it was already verified
217
+		if ( ! isset($_POST['g-recaptcha-response']) ) {
218
+			// If captcha is missing, check if it was already verified
219 219
 			if ( ! isset( $_POST['recaptcha_checked'] ) || ! wp_verify_nonce( $_POST['recaptcha_checked'], 'frm_ajax' ) ) {
220
-                // There was no captcha submitted
220
+				// There was no captcha submitted
221 221
 				$errors[ 'field' . $args['id'] ] = __( 'The captcha is missing from this form', 'formidable' );
222
-            }
223
-            return;
224
-        }
222
+			}
223
+			return;
224
+		}
225 225
 
226
-        $arg_array = array(
227
-            'body'      => array(
226
+		$arg_array = array(
227
+			'body'      => array(
228 228
 				'secret'   => $frm_settings->privkey,
229 229
 				'response' => $_POST['g-recaptcha-response'],
230 230
 				'remoteip' => FrmAppHelper::get_ip_address(),
231 231
 			),
232 232
 		);
233
-        $resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array );
234
-        $response = json_decode(wp_remote_retrieve_body( $resp ), true);
233
+		$resp = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array );
234
+		$response = json_decode(wp_remote_retrieve_body( $resp ), true);
235 235
 
236
-        if ( isset( $response['success'] ) && ! $response['success'] ) {
237
-            // What happens when the CAPTCHA was entered incorrectly
236
+		if ( isset( $response['success'] ) && ! $response['success'] ) {
237
+			// What happens when the CAPTCHA was entered incorrectly
238 238
 			$errors[ 'field' . $args['id'] ] = ( ! isset( $field->field_options['invalid'] ) || $field->field_options['invalid'] == '' ) ? $frm_settings->re_msg : $field->field_options['invalid'];
239
-        } else if ( is_wp_error( $resp ) ) {
239
+		} else if ( is_wp_error( $resp ) ) {
240 240
 			$error_string = $resp->get_error_message();
241 241
 			$errors[ 'field' . $args['id'] ] = __( 'There was a problem verifying your recaptcha', 'formidable' );
242 242
 			$errors[ 'field' . $args['id'] ] .= ' ' . $error_string;
243
-        }
244
-    }
245
-
246
-    /**
247
-     * check for spam
248
-     * @param boolean $exclude
249
-     * @param array $values
250
-     * @param array $errors by reference
251
-     */
252
-    public static function spam_check( $exclude, $values, &$errors ) {
253
-        if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
254
-            // only check spam if there are no other errors
255
-            return;
256
-        }
257
-
258
-        if ( self::is_akismet_spam( $values ) ) {
243
+		}
244
+	}
245
+
246
+	/**
247
+	 * check for spam
248
+	 * @param boolean $exclude
249
+	 * @param array $values
250
+	 * @param array $errors by reference
251
+	 */
252
+	public static function spam_check( $exclude, $values, &$errors ) {
253
+		if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
254
+			// only check spam if there are no other errors
255
+			return;
256
+		}
257
+
258
+		if ( self::is_akismet_spam( $values ) ) {
259 259
 			if ( self::is_akismet_enabled_for_user( $values['form_id'] ) ) {
260 260
 				$errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
261 261
 			}
262
-	    }
262
+		}
263 263
 
264
-    	if ( self::blacklist_check( $values ) ) {
265
-            $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
266
-    	}
267
-    }
264
+		if ( self::blacklist_check( $values ) ) {
265
+			$errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
266
+		}
267
+	}
268 268
 
269 269
 	private static function is_akismet_spam( $values ) {
270 270
 		global $wpcom_api_key;
@@ -276,54 +276,54 @@  discard block
 block discarded – undo
276 276
 		return ( isset( $form->options['akismet'] ) && ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] != 'logged' || ! is_user_logged_in() ) );
277 277
 	}
278 278
 
279
-    public static function blacklist_check( $values ) {
280
-        if ( ! apply_filters('frm_check_blacklist', true, $values) ) {
281
-            return false;
282
-        }
279
+	public static function blacklist_check( $values ) {
280
+		if ( ! apply_filters('frm_check_blacklist', true, $values) ) {
281
+			return false;
282
+		}
283 283
 
284
-    	$mod_keys = trim( get_option( 'blacklist_keys' ) );
284
+		$mod_keys = trim( get_option( 'blacklist_keys' ) );
285 285
 
286
-    	if ( empty( $mod_keys ) ) {
287
-    		return false;
288
-    	}
286
+		if ( empty( $mod_keys ) ) {
287
+			return false;
288
+		}
289 289
 
290
-    	$content = FrmEntriesHelper::entry_array_to_string($values);
290
+		$content = FrmEntriesHelper::entry_array_to_string($values);
291 291
 
292 292
 		if ( empty($content) ) {
293
-		    return false;
293
+			return false;
294 294
 		}
295 295
 
296
-    	$words = explode( "\n", $mod_keys );
296
+		$words = explode( "\n", $mod_keys );
297 297
 
298
-    	foreach ( (array) $words as $word ) {
299
-    		$word = trim( $word );
298
+		foreach ( (array) $words as $word ) {
299
+			$word = trim( $word );
300 300
 
301
-    		if ( empty($word) ) {
302
-    			continue;
303
-    		}
301
+			if ( empty($word) ) {
302
+				continue;
303
+			}
304 304
 
305
-    		if ( preg_match('#' . preg_quote( $word, '#' ) . '#', $content) ) {
306
-    			return true;
307
-    		}
308
-    	}
305
+			if ( preg_match('#' . preg_quote( $word, '#' ) . '#', $content) ) {
306
+				return true;
307
+			}
308
+		}
309 309
 
310
-    	return false;
311
-    }
310
+		return false;
311
+	}
312 312
 
313
-    /**
314
-     * Check entries for spam
315
-     *
316
-     * @return boolean true if is spam
317
-     */
318
-    public static function akismet( $values ) {
319
-	    $content = FrmEntriesHelper::entry_array_to_string( $values );
313
+	/**
314
+	 * Check entries for spam
315
+	 *
316
+	 * @return boolean true if is spam
317
+	 */
318
+	public static function akismet( $values ) {
319
+		$content = FrmEntriesHelper::entry_array_to_string( $values );
320 320
 
321 321
 		if ( empty( $content ) ) {
322
-		    return false;
322
+			return false;
323 323
 		}
324 324
 
325
-        $datas = array();
326
-        self::parse_akismet_array( $datas, $content );
325
+		$datas = array();
326
+		self::parse_akismet_array( $datas, $content );
327 327
 
328 328
 		$query_string = '';
329 329
 		foreach ( $datas as $key => $data ) {
@@ -331,35 +331,35 @@  discard block
 block discarded – undo
331 331
 			unset( $key, $data );
332 332
 		}
333 333
 
334
-        $response = Akismet::http_post($query_string, 'comment-check');
334
+		$response = Akismet::http_post($query_string, 'comment-check');
335 335
 
336 336
 		return ( is_array( $response ) && $response[1] == 'true' );
337
-    }
338
-
339
-    /**
340
-     * @since 2.0
341
-     * @param string $content
342
-     */
343
-    private  static function parse_akismet_array( &$datas, $content ) {
344
-        $datas['blog'] = FrmAppHelper::site_url();
345
-        $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() );
337
+	}
338
+
339
+	/**
340
+	 * @since 2.0
341
+	 * @param string $content
342
+	 */
343
+	private  static function parse_akismet_array( &$datas, $content ) {
344
+		$datas['blog'] = FrmAppHelper::site_url();
345
+		$datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() );
346 346
 		$datas['user_agent'] = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
347 347
 		$datas['referrer'] = isset( $_SERVER['HTTP_REFERER'] ) ? FrmAppHelper::get_server_value( 'HTTP_REFERER' ) : false;
348
-        $datas['comment_type'] = 'formidable';
349
-        $datas['comment_content'] = $content;
348
+		$datas['comment_type'] = 'formidable';
349
+		$datas['comment_content'] = $content;
350 350
 
351
-        if ( $permalink = get_permalink() ) {
352
-            $datas['permalink'] = $permalink;
353
-        }
351
+		if ( $permalink = get_permalink() ) {
352
+			$datas['permalink'] = $permalink;
353
+		}
354 354
 
355
-        foreach ( $_SERVER as $key => $value ) {
355
+		foreach ( $_SERVER as $key => $value ) {
356 356
 			if ( ! in_array( $key, array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ) ) && is_string( $value ) ) {
357 357
 				$datas[ $key ] = wp_strip_all_tags( $value );
358
-            } else {
358
+			} else {
359 359
 				$datas[ $key ] = '';
360
-            }
360
+			}
361 361
 
362
-            unset($key, $value);
363
-        }
364
-    }
362
+			unset($key, $value);
363
+		}
364
+	}
365 365
 }
Please login to merge, or discard this patch.