Project

General

Profile

« Previous | Next » 

Revision 356

Added by ryan over 18 years ago

Added multi-submission protection to form module #119

View differences:

view.php
62 62
// Work-out if the form has been submitted or not
63 63
if($_POST == array()) {
64 64

  
65
// Generate temp submission id
66
$submission_id = '';
67
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
68
srand((double)microtime()*1000000);
69
$i = 0;
70
while ($i <= 7) {
71
	$num = rand() % 33;
72
	$tmp = substr($salt, $num, 1);
73
	$submission_id = $submission_id . $tmp;
74
	$i++;
75
}
76

  
77
// Set submission ID in session
78
$_SESSION['form_submission_id'] = $submission_id;
79

  
65 80
?>
66 81
<style type="text/css">
67 82
.required {
......
126 141
// Add form starter code
127 142
?>
128 143
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
144
<input type="hidden" name="submission_id" value="<?php echo $submission_id; ?>" />
129 145
<?php
130 146

  
131 147
// Print header
......
209 225

  
210 226
} else {
211 227
	
212
	// Submit form data
213
	// First start message settings
214
	$query_settings = $database->query("SELECT email_to,email_from,email_subject,success_message,max_submissions,stored_submissions,use_captcha FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
215
	if($query_settings->numRows() > 0) {
216
		$fetch_settings = $query_settings->fetchRow();
217
		$email_to = $fetch_settings['email_to'];
218
		$email_from = $fetch_settings['email_from'];
219
		if(substr($email_from, 0, 5) == 'field') {
220
			// Set the email from field to what the user entered in the specified field
221
			$email_from = $wb->add_slashes($_POST[$email_from]);
228
	// Check that submission ID matches
229
	if(isset($_SESSION['form_submission_id']) AND isset($_POST['submission_id']) AND $_SESSION['form_submission_id'] == $_POST['submission_id']) {
230
		
231
		// Submit form data
232
		// First start message settings
233
		$query_settings = $database->query("SELECT email_to,email_from,email_subject,success_message,max_submissions,stored_submissions,use_captcha FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
234
		if($query_settings->numRows() > 0) {
235
			$fetch_settings = $query_settings->fetchRow();
236
			$email_to = $fetch_settings['email_to'];
237
			$email_from = $fetch_settings['email_from'];
238
			if(substr($email_from, 0, 5) == 'field') {
239
				// Set the email from field to what the user entered in the specified field
240
				$email_from = $wb->add_slashes($_POST[$email_from]);
241
			}
242
			$email_subject = $fetch_settings['email_subject'];
243
			$success_message = $fetch_settings['success_message'];
244
			$max_submissions = $fetch_settings['max_submissions'];
245
			$stored_submissions = $fetch_settings['stored_submissions'];
246
			$use_captcha = $fetch_settings['use_captcha'];
247
		} else {
248
			exit($TEXT['UNDER_CONSTRUCTION']);
222 249
		}
223
		$email_subject = $fetch_settings['email_subject'];
224
		$success_message = $fetch_settings['success_message'];
225
		$max_submissions = $fetch_settings['max_submissions'];
226
		$stored_submissions = $fetch_settings['stored_submissions'];
227
		$use_captcha = $fetch_settings['use_captcha'];
228
	} else {
229
		exit($TEXT['UNDER_CONSTRUCTION']);
230
	}
231
	$email_body = '';
232
	
233
	// Create blank "required" array
234
	$required = array();
235
	
236
	// Loop through fields and add to message body
237
	// Get list of fields
238
	$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
239
	if($query_fields->numRows() > 0) {
240
		while($field = $query_fields->fetchRow()) {
241
			// Add to message body
242
			if($field['type'] != '') {
243
				if(!empty($_POST['field'.$field['field_id']])) {
244
					if($field['type'] == 'email' AND $admin->validate_email($_POST['field'.$field['field_id']]) == false) {
245
						$email_error = $MESSAGE['USERS']['INVALID_EMAIL'];
246
					}
247
					if($field['type'] == 'heading') {
248
						$email_body .= $_POST['field'.$field['field_id']]."\n\n";
249
					} elseif (!is_array($_POST['field'.$field['field_id']])) {
250
						$email_body .= $field['title'].': '.$_POST['field'.$field['field_id']]."\n\n";
251
					} else {
252
						$email_body .= $field['title'].": \n";
253
						foreach ($_POST['field'.$field['field_id']] as $k=>$v) {
254
							$email_body .= $v."\n";
250
		$email_body = '';
251
		
252
		// Create blank "required" array
253
		$required = array();
254
		
255
		// Loop through fields and add to message body
256
		// Get list of fields
257
		$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
258
		if($query_fields->numRows() > 0) {
259
			while($field = $query_fields->fetchRow()) {
260
				// Add to message body
261
				if($field['type'] != '') {
262
					if(!empty($_POST['field'.$field['field_id']])) {
263
						if($field['type'] == 'email' AND $admin->validate_email($_POST['field'.$field['field_id']]) == false) {
264
							$email_error = $MESSAGE['USERS']['INVALID_EMAIL'];
255 265
						}
256
						$email_body .= "\n";
266
						if($field['type'] == 'heading') {
267
							$email_body .= $_POST['field'.$field['field_id']]."\n\n";
268
						} elseif (!is_array($_POST['field'.$field['field_id']])) {
269
							$email_body .= $field['title'].': '.$_POST['field'.$field['field_id']]."\n\n";
270
						} else {
271
							$email_body .= $field['title'].": \n";
272
							foreach ($_POST['field'.$field['field_id']] as $k=>$v) {
273
								$email_body .= $v."\n";
274
							}
275
							$email_body .= "\n";
276
						}
277
					} elseif($field['required'] == 1) {
278
						$required[] = $field['title'];
257 279
					}
258
				} elseif($field['required'] == 1) {
259
					$required[] = $field['title'];
260 280
				}
261 281
			}
262 282
		}
263
	}
264
	
265
	// Captcha
266
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) { /* Make's sure GD library is installed */
267
		if($use_captcha) {
268
			if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
269
				// Check for a mismatch
270
				if(!isset($_POST['captcha']) OR !isset($_SESSION['captcha']) OR $_POST['captcha'] != $_SESSION['captcha']) {
283
		
284
		// Captcha
285
		if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) { /* Make's sure GD library is installed */
286
			if($use_captcha) {
287
				if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
288
					// Check for a mismatch
289
					if(!isset($_POST['captcha']) OR !isset($_SESSION['captcha']) OR $_POST['captcha'] != $_SESSION['captcha']) {
290
						$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
291
					}
292
				} else {
271 293
					$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
272 294
				}
273
			} else {
274
				$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
275 295
			}
276 296
		}
277
	}
278
	if(isset($_SESSION['captcha'])) { unset($_SESSION['captcha']); }
279
	
280
	// Addslashes to email body - proposed by Icheb in topic=1170.0
281
	// $email_body = $wb->add_slashes($email_body);
282
	
283
	// Check if the user forgot to enter values into all the required fields
284
	if($required != array()) {
285
		if(!isset($MESSAGE['MOD_FORM']['REQUIRED_FIELDS'])) {
286
			echo 'You must enter details for the following fields';
287
		} else {
288
			echo $MESSAGE['MOD_FORM']['REQUIRED_FIELDS'];
289
		}
290
		echo ':<br /><ul>';
291
		foreach($required AS $field_title) {
292
			echo '<li>'.$field_title;
293
		}
294
		if(isset($email_error)) { echo '<li>'.$email_error.'</li>'; }
295
		if(isset($captcha_error)) { echo '<li>'.$captcha_error.'</li>'; }
296
		echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
297
		if(isset($_SESSION['captcha'])) { unset($_SESSION['captcha']); }
297 298
		
298
	} else {
299
		// Addslashes to email body - proposed by Icheb in topic=1170.0
300
		// $email_body = $wb->add_slashes($email_body);
299 301
		
300
		if(isset($email_error)) {
301
			echo '<br /><ul>';
302
			echo '<li>'.$email_error.'</li>';
302
		// Check if the user forgot to enter values into all the required fields
303
		if($required != array()) {
304
			if(!isset($MESSAGE['MOD_FORM']['REQUIRED_FIELDS'])) {
305
				echo 'You must enter details for the following fields';
306
			} else {
307
				echo $MESSAGE['MOD_FORM']['REQUIRED_FIELDS'];
308
			}
309
			echo ':<br /><ul>';
310
			foreach($required AS $field_title) {
311
				echo '<li>'.$field_title;
312
			}
313
			if(isset($email_error)) { echo '<li>'.$email_error.'</li>'; }
314
			if(isset($captcha_error)) { echo '<li>'.$captcha_error.'</li>'; }
303 315
			echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
304
		} elseif(isset($captcha_error)) {
305
			echo '<br /><ul>';
306
			echo '<li>'.$captcha_error.'</li>';
307
			echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
316
			
308 317
		} else {
309
		
310
		// Check how many times form has been submitted in last hour
311
		$last_hour = time()-3600;
312
		$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions WHERE submitted_when >= '$last_hour'");
313
		if($query_submissions->numRows() > $max_submissions) {
314
			// Too many submissions so far this hour
315
			echo $MESSAGE['MOD_FORM']['EXCESS_SUBMISSIONS'];
316
			$success = false;
317
		} else {
318
			// Now send the email
319
			if($email_to != '') {
320
				if($email_from != '') {
321
					if($wb->mail($email_from,$email_to,$email_subject,$email_body)) { $success = true; }
322
				}
323
			}				
324
			// Write submission to database
325
			if(isset($admin) AND $admin->get_user_id() > 0) {
326
				$admin->get_user_id();
318
			
319
			if(isset($email_error)) {
320
				echo '<br /><ul>';
321
				echo '<li>'.$email_error.'</li>';
322
				echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
323
			} elseif(isset($captcha_error)) {
324
				echo '<br /><ul>';
325
				echo '<li>'.$captcha_error.'</li>';
326
				echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
327 327
			} else {
328
				$submitted_by = 0;
329
			}
330
			$email_body = $wb->add_slashes($email_body);
331
			$database->query("INSERT INTO ".TABLE_PREFIX."mod_form_submissions (page_id,section_id,submitted_when,submitted_by,body) VALUES ('".PAGE_ID."','$section_id','".mktime()."','$submitted_by','$email_body')");
332
			// Make sure submissions table isn't too full
333
			$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions ORDER BY submitted_when");
334
			$num_submissions = $query_submissions->numRows();
335
			if($num_submissions > $stored_submissions) {
336
				// Remove excess submission
337
				$num_to_remove = $num_submissions-$stored_submissions;
338
				while($submission = $query_submissions->fetchRow()) {
339
					if($num_to_remove > 0) {
340
						$submission_id = $submission['submission_id'];
341
						$database->query("DELETE FROM ".TABLE_PREFIX."mod_form_submissions WHERE submission_id = '$submission_id'");
342
						$num_to_remove = $num_to_remove-1;
328
			
329
			// Check how many times form has been submitted in last hour
330
			$last_hour = time()-3600;
331
			$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions WHERE submitted_when >= '$last_hour'");
332
			if($query_submissions->numRows() > $max_submissions) {
333
				// Too many submissions so far this hour
334
				echo $MESSAGE['MOD_FORM']['EXCESS_SUBMISSIONS'];
335
				$success = false;
336
			} else {
337
				// Now send the email
338
				if($email_to != '') {
339
					if($email_from != '') {
340
						if($wb->mail($email_from,$email_to,$email_subject,$email_body)) { $success = true; }
343 341
					}
342
				}				
343
				// Write submission to database
344
				if(isset($admin) AND $admin->get_user_id() > 0) {
345
					$admin->get_user_id();
346
				} else {
347
					$submitted_by = 0;
344 348
				}
349
				$email_body = $wb->add_slashes($email_body);
350
				$database->query("INSERT INTO ".TABLE_PREFIX."mod_form_submissions (page_id,section_id,submitted_when,submitted_by,body) VALUES ('".PAGE_ID."','$section_id','".mktime()."','$submitted_by','$email_body')");
351
				// Make sure submissions table isn't too full
352
				$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions ORDER BY submitted_when");
353
				$num_submissions = $query_submissions->numRows();
354
				if($num_submissions > $stored_submissions) {
355
					// Remove excess submission
356
					$num_to_remove = $num_submissions-$stored_submissions;
357
					while($submission = $query_submissions->fetchRow()) {
358
						if($num_to_remove > 0) {
359
							$submission_id = $submission['submission_id'];
360
							$database->query("DELETE FROM ".TABLE_PREFIX."mod_form_submissions WHERE submission_id = '$submission_id'");
361
							$num_to_remove = $num_to_remove-1;
362
						}
363
					}
364
				}
365
				if(!$database->is_error()) {
366
					$success = true;
367
				}
345 368
			}
346
			if(!$database->is_error()) {
347
				$success = true;
348
			}
369
		
349 370
		}
350 371
		
351 372
		// Now check if the email was sent successfully
......
360 381
	
361 382
}
362 383

  
363
?>
384
?>

Also available in: Unified diff