Project

General

Profile

1 560 Ruebenwurz
<?php
2
3
// $Id$
4
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, Ryan Djurovich
9
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24
*/
25
26
/*
27
The Website Baker Project would like to thank Rudolph Lartey <www.carbonect.com>
28
for his contributions to this module - adding extra field types
29
*/
30
31
// Must include code to stop this file being access directly
32
if(defined('WB_PATH') == false) { exit("Cannot access this file directly"); }
33
34
// check if frontend.css file needs to be included into the <body></body> of view.php
35 570 doc
if((!function_exists('register_frontend_modfiles') || !defined('MOD_FRONTEND_CSS_REGISTERED')) &&
36
	file_exists(WB_PATH .'/modules/form/frontend.css')) {
37
	echo '<style type="text/css">';
38
	include(WB_PATH .'/modules/form/frontend.css');
39
	echo "\n</style>\n";
40 560 Ruebenwurz
}
41
42 596 thorn
require_once(WB_PATH.'/include/captcha/captcha.php');
43 739 thorn
require_once(WB_PATH.'/include/captcha/asp.php');
44 596 thorn
45 560 Ruebenwurz
// Function for generating an optionsfor a select field
46 570 doc
if (!function_exists('make_option')) {
47 618 thorn
function make_option(&$n, $k, $values) {
48 560 Ruebenwurz
	// start option group if it exists
49
	if (substr($n,0,2) == '[=') {
50
	 	$n = '<optgroup label="'.substr($n,2,strlen($n)).'">';
51
	} elseif ($n == ']') {
52
		$n = '</optgroup>';
53
	} else {
54 760 Ruebenwurz
		if(in_array($n, $values)) {
55 618 thorn
			$n = '<option selected="selected" value="'.$n.'">'.$n.'</option>';
56 760 Ruebenwurz
		} else {
57 618 thorn
			$n = '<option value="'.$n.'">'.$n.'</option>';
58 760 Ruebenwurz
		}
59 560 Ruebenwurz
	}
60
}
61
}
62
// Function for generating a checkbox
63 570 doc
if (!function_exists('make_checkbox')) {
64 560 Ruebenwurz
function make_checkbox(&$n, $idx, $params) {
65 618 thorn
	$field_id = $params[0][0];
66
	$seperator = $params[0][1];
67 760 Ruebenwurz
	$label_id = 'wb_'.str_replace(" ", "_", $n);
68
	if(in_array($n, $params[1])) {
69
		$n = '<input class="field_checkbox" type="checkbox" id="'.$label_id.'" name="field'.$field_id.'['.$idx.']" value="'.$n.'" checked="checked" />'.'<label for="'.$label_id.'" class="checkbox_label">'.$n.'</lable>'.$seperator;
70
	} else {
71
		$n = '<input class="field_checkbox" type="checkbox" id="'.$label_id.'" name="field'.$field_id.'['.$idx.']" value="'.$n.'" />'.'<label for="'.$label_id.'" class="checkbox_label">'.$n.'</label>'.$seperator;
72
	}
73 560 Ruebenwurz
}
74
}
75
// Function for generating a radio button
76 570 doc
if (!function_exists('make_radio')) {
77 560 Ruebenwurz
function make_radio(&$n, $idx, $params) {
78
	$field_id = $params[0];
79
	$group = $params[1];
80
	$seperator = $params[2];
81 760 Ruebenwurz
	$label_id = 'wb_'.str_replace(" ", "_", $n);
82
	if($n == $params[3]) {
83
		$n = '<input class="field_radio" type="radio" id="'.$label_id.'" name="field'.$field_id.'" value="'.$n.'" checked="checked" />'.'<label for="'.$label_id.'" class="radio_label">'.$n.'</label>'.$seperator;
84
	} else {
85
		$n = '<input class="field_radio" type="radio" id="'.$label_id.'" name="field'.$field_id.'" value="'.$n.'" />'.'<label for="'.$label_id.'" class="radio_label">'.$n.'</label>'.$seperator;
86
	}
87 560 Ruebenwurz
}
88
}
89
// Generate temp submission id
90
function new_submission_id() {
91
	$submission_id = '';
92
	$salt = "abchefghjkmnpqrstuvwxyz0123456789";
93
	srand((double)microtime()*1000000);
94
	$i = 0;
95
	while ($i <= 7) {
96
		$num = rand() % 33;
97
		$tmp = substr($salt, $num, 1);
98
		$submission_id = $submission_id . $tmp;
99
		$i++;
100
	}
101
	return $submission_id;
102
}
103
104
// Work-out if the form has been submitted or not
105
if($_POST == array()) {
106
107
// Set new submission ID in session
108
$_SESSION['form_submission_id'] = new_submission_id();
109
110
// Get settings
111
$query_settings = $database->query("SELECT header,field_loop,footer,use_captcha FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
112
if($query_settings->numRows() > 0) {
113
	$fetch_settings = $query_settings->fetchRow();
114
	$header = str_replace('{WB_URL}',WB_URL,$fetch_settings['header']);
115
	$field_loop = $fetch_settings['field_loop'];
116
	$footer = str_replace('{WB_URL}',WB_URL,$fetch_settings['footer']);
117
	$use_captcha = $fetch_settings['use_captcha'];
118
} else {
119
	$header = '';
120
	$field_loop = '';
121
	$footer = '';
122
}
123
124
?>
125 760 Ruebenwurz
<form name="form" action="<?php echo htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])); ?>" method="post">
126 560 Ruebenwurz
<input type="hidden" name="submission_id" value="<?php echo $_SESSION['form_submission_id']; ?>" />
127
<?php
128 596 thorn
if(ENABLED_ASP) { // first add some honeypot-fields
129
?>
130
<input type="hidden" name="submitted_when" value="<?php $t=time(); echo $t; $_SESSION['submitted_when']=$t; ?>" />
131 739 thorn
<p class="nixhier">
132 596 thorn
email address:
133
<label for="email">Your e-mail address is not relevant Leave this field blank:</label>
134
<input id="email" name="email" size="56" value="" /><br />
135
Homepage:
136
<label for="homepage">Do not enter a homepage-url www.whatever.com here:</label>
137
<input id="homepage" name="homepage" size="55" value="" /><br />
138
URL:
139
<label for="url">Don't write anything in this field:</label>
140
<input id="url" name="url" size="61" value="" /><br />
141
Comment:
142
<label for="comment">Enter not your comment here:</label>
143 760 Ruebenwurz
<textarea id="comment" name="comment" cols="50" rows="10"></textarea><br />
144 596 thorn
</p>
145 760 Ruebenwurz
146 596 thorn
<?php }
147
148 760 Ruebenwurz
// Print header
149
echo $header;
150
151 560 Ruebenwurz
// Get list of fields
152
$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
153 760 Ruebenwurz
154 560 Ruebenwurz
if($query_fields->numRows() > 0) {
155
	while($field = $query_fields->fetchRow()) {
156
		// Set field values
157
		$field_id = $field['field_id'];
158
		$value = $field['value'];
159
		// Print field_loop after replacing vars with values
160
		$vars = array('{TITLE}', '{REQUIRED}');
161 760 Ruebenwurz
		if (($field['type'] == "radio") || ($field['type'] == "checkbox")) {
162
			$field_title = $field['title'];
163 560 Ruebenwurz
		} else {
164 760 Ruebenwurz
			$field_title = '<label for="field'.$field_id.'">'.$field['title'].'</label>';
165
		}
166
		$values = array($field_title);
167
		if ($field['required'] == 1) {
168
			$values[] = '<span class="required">*</span>';
169
		} else {
170 560 Ruebenwurz
			$values[] = '';
171
		}
172
		if($field['type'] == 'textfield') {
173
			$vars[] = '{FIELD}';
174 618 thorn
			$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" maxlength="'.$field['extra'].'" value="'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:$value).'" class="textfield" />';
175 560 Ruebenwurz
		} elseif($field['type'] == 'textarea') {
176
			$vars[] = '{FIELD}';
177 760 Ruebenwurz
			$values[] = '<textarea name="field'.$field_id.'" id="field'.$field_id.'" class="textarea" cols="25" rows="5">'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:$value).'</textarea>';
178 560 Ruebenwurz
		} elseif($field['type'] == 'select') {
179
			$vars[] = '{FIELD}';
180
			$options = explode(',', $value);
181 618 thorn
			array_walk($options, 'make_option', (isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array()));
182 760 Ruebenwurz
			$field['extra'] = explode(',',$field['extra']);
183
			$values[] = '<select name="field'.$field_id.'[]" id="field'.$field_id.'" size="'.$field['extra'][0].'" '.$field['extra'][1].'="'.$field['extra'][1].'" class="select">'.implode($options).'</select>';
184 560 Ruebenwurz
		} elseif($field['type'] == 'heading') {
185
			$vars[] = '{FIELD}';
186
			$values[] = '<input type="hidden" name="field'.$field_id.'" id="field'.$field_id.'" value="===['.$field['title'].']===" />';
187
			$tmp_field_loop = $field_loop;		// temporarily modify the field loop template
188
			$field_loop = $field['extra'];
189
		} elseif($field['type'] == 'checkbox') {
190
			$vars[] = '{FIELD}';
191
			$options = explode(',', $value);
192 618 thorn
			array_walk($options, 'make_checkbox', array(array($field_id,$field['extra']),(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array())));
193 560 Ruebenwurz
			$options[count($options)-1]=substr($options[count($options)-1],0,strlen($options[count($options)-1])-strlen($field['extra']));
194
			$values[] = implode($options);
195
		} elseif($field['type'] == 'radio') {
196
			$vars[] = '{FIELD}';
197
			$options = explode(',', $value);
198 618 thorn
			array_walk($options, 'make_radio', array($field_id,$field['title'],$field['extra'], (isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:'')));
199 560 Ruebenwurz
			$options[count($options)-1]=substr($options[count($options)-1],0,strlen($options[count($options)-1])-strlen($field['extra']));
200
			$values[] = implode($options);
201
		} elseif($field['type'] == 'email') {
202
			$vars[] = '{FIELD}';
203 760 Ruebenwurz
			$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" value="'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:'').'" maxlength="'.$field['extra'].'" class="email" />';
204 560 Ruebenwurz
		}
205 618 thorn
		if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
206 560 Ruebenwurz
		if($field['type'] != '') {
207
			echo str_replace($vars, $values, $field_loop);
208
		}
209
		if (isset($tmp_field_loop)) $field_loop = $tmp_field_loop;
210
	}
211
}
212
213
// Captcha
214 596 thorn
if($use_captcha) { ?>
215
	<tr>
216
	<td class="field_title"><?php echo $TEXT['VERIFICATION']; ?>:</td>
217
	<td><?php call_captcha(); ?></td>
218
	</tr>
219 560 Ruebenwurz
	<?php
220
}
221
222
// Print footer
223
echo $footer;
224
225
// Add form end code
226
?>
227
</form>
228
<?php
229
230
} else {
231
232
	// Check that submission ID matches
233
	if(isset($_SESSION['form_submission_id']) AND isset($_POST['submission_id']) AND $_SESSION['form_submission_id'] == $_POST['submission_id']) {
234
235
		// Set new submission ID in session
236
		$_SESSION['form_submission_id'] = new_submission_id();
237
238 596 thorn
		if(ENABLED_ASP && ( // form faked? Check the honeypot-fields.
239
			(!isset($_POST['submitted_when']) OR !isset($_SESSION['submitted_when'])) OR
240
			($_POST['submitted_when'] != $_SESSION['submitted_when']) OR
241
			(!isset($_POST['email']) OR $_POST['email']) OR
242
			(!isset($_POST['homepage']) OR $_POST['homepage']) OR
243
			(!isset($_POST['comment']) OR $_POST['comment']) OR
244
			(!isset($_POST['url']) OR $_POST['url'])
245
		)) {
246
			exit(header("Location: ".WB_URL.PAGES_DIRECTORY.""));
247
		}
248
249 560 Ruebenwurz
		// Submit form data
250
		// First start message settings
251
		$query_settings = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
252
		if($query_settings->numRows() > 0) {
253
			$fetch_settings = $query_settings->fetchRow();
254
			$email_to = $fetch_settings['email_to'];
255
			$email_from = $fetch_settings['email_from'];
256
			if(substr($email_from, 0, 5) == 'field') {
257
				// Set the email from field to what the user entered in the specified field
258 656 thorn
				$email_from = htmlspecialchars($wb->add_slashes($_POST[$email_from]));
259 560 Ruebenwurz
			}
260 638 thorn
			$email_fromname = $fetch_settings['email_fromname'];
261 560 Ruebenwurz
			$email_subject = $fetch_settings['email_subject'];
262
			$success_page = $fetch_settings['success_page'];
263
			$success_email_to = $fetch_settings['success_email_to'];
264
			if(substr($success_email_to, 0, 5) == 'field') {
265
				// Set the success_email to field to what the user entered in the specified field
266 656 thorn
				$success_email_to = htmlspecialchars($wb->add_slashes($_POST[$success_email_to]));
267 560 Ruebenwurz
			}
268
			$success_email_from = $fetch_settings['success_email_from'];
269 638 thorn
			$success_email_fromname = $fetch_settings['success_email_fromname'];
270 560 Ruebenwurz
			$success_email_text = $fetch_settings['success_email_text'];
271
			$success_email_subject = $fetch_settings['success_email_subject'];
272
			$max_submissions = $fetch_settings['max_submissions'];
273
			$stored_submissions = $fetch_settings['stored_submissions'];
274
			$use_captcha = $fetch_settings['use_captcha'];
275
		} else {
276
			exit($TEXT['UNDER_CONSTRUCTION']);
277
		}
278
		$email_body = '';
279
280
		// Create blank "required" array
281
		$required = array();
282
283 618 thorn
		// Captcha
284
		if($use_captcha) {
285
			if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
286
				// Check for a mismatch
287
				if(!isset($_POST['captcha']) OR !isset($_SESSION['captcha']) OR $_POST['captcha'] != $_SESSION['captcha']) {
288
					$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
289
				}
290
			} else {
291
				$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
292
			}
293
		}
294
		if(isset($_SESSION['captcha'])) { unset($_SESSION['captcha']); }
295
296 560 Ruebenwurz
		// Loop through fields and add to message body
297
		// Get list of fields
298
		$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
299
		if($query_fields->numRows() > 0) {
300
			while($field = $query_fields->fetchRow()) {
301
				// Add to message body
302
				if($field['type'] != '') {
303
					if(!empty($_POST['field'.$field['field_id']])) {
304 760 Ruebenwurz
						if (is_array($_POST['field'.$field['field_id']])) {
305
							$_SESSION['field'.$field['field_id']] = $_POST['field'.$field['field_id']];
306
						} else {
307
							$_SESSION['field'.$field['field_id']] = htmlspecialchars($_POST['field'.$field['field_id']]);
308
						}
309 560 Ruebenwurz
						if($field['type'] == 'email' AND $admin->validate_email($_POST['field'.$field['field_id']]) == false) {
310
							$email_error = $MESSAGE['USERS']['INVALID_EMAIL'];
311
						}
312
						if($field['type'] == 'heading') {
313
							$email_body .= $_POST['field'.$field['field_id']]."\n\n";
314
						} elseif (!is_array($_POST['field'.$field['field_id']])) {
315
							$email_body .= $field['title'].': '.$_POST['field'.$field['field_id']]."\n\n";
316
						} else {
317
							$email_body .= $field['title'].": \n";
318
							foreach ($_POST['field'.$field['field_id']] as $k=>$v) {
319
								$email_body .= $v."\n";
320
							}
321
							$email_body .= "\n";
322
						}
323
					} elseif($field['required'] == 1) {
324
						$required[] = $field['title'];
325
					}
326
				}
327
			}
328
		}
329 760 Ruebenwurz
330 560 Ruebenwurz
		// Check if the user forgot to enter values into all the required fields
331
		if($required != array()) {
332
			if(!isset($MESSAGE['MOD_FORM']['REQUIRED_FIELDS'])) {
333
				echo 'You must enter details for the following fields';
334
			} else {
335
				echo $MESSAGE['MOD_FORM']['REQUIRED_FIELDS'];
336
			}
337
			echo ':<br /><ul>';
338
			foreach($required AS $field_title) {
339
				echo '<li>'.$field_title;
340
			}
341 760 Ruebenwurz
			if(isset($email_error)) {
342
				echo '<li>'.$email_error.'</li>';
343
			}
344
			if(isset($captcha_error)) {
345
				echo '<li>'.$captcha_error.'</li>';
346
			}
347
			echo '</ul><a href="'.htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])).'">'.$TEXT['BACK'].'</a>';
348 560 Ruebenwurz
		} else {
349
			if(isset($email_error)) {
350
				echo '<br /><ul>';
351
				echo '<li>'.$email_error.'</li>';
352 760 Ruebenwurz
				echo '</ul><a href="'.htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])).'">'.$TEXT['BACK'].'</a>';
353 560 Ruebenwurz
			} elseif(isset($captcha_error)) {
354
				echo '<br /><ul>';
355
				echo '<li>'.$captcha_error.'</li>';
356 760 Ruebenwurz
				echo '</ul><a href="'.htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])).'">'.$TEXT['BACK'].'</a>';
357 560 Ruebenwurz
			} else {
358
				// Check how many times form has been submitted in last hour
359
				$last_hour = time()-3600;
360
				$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions WHERE submitted_when >= '$last_hour'");
361
				if($query_submissions->numRows() > $max_submissions) {
362
					// Too many submissions so far this hour
363
					echo $MESSAGE['MOD_FORM']['EXCESS_SUBMISSIONS'];
364
					$success = false;
365
				} else {
366
					// Now send the email
367
					if($email_to != '') {
368
						if($email_from != '') {
369 638 thorn
							if($wb->mail($email_from,$email_to,$email_subject,$email_body,$email_fromname)) {
370 560 Ruebenwurz
								$success = true;
371
							}
372
						} else {
373 638 thorn
							if($wb->mail('',$email_to,$email_subject,$email_body,$email_fromname)) {
374 560 Ruebenwurz
								$success = true;
375
							}
376
						}
377
					}
378
					if($success_email_to != '') {
379
						if($success_email_from != '') {
380 638 thorn
							if($wb->mail($success_email_from,$success_email_to,$success_email_subject,$success_email_text,$success_email_fromname)) {
381 560 Ruebenwurz
								$success = true;
382
							}
383
						} else {
384 638 thorn
							if($wb->mail('',$success_email_to,$success_email_subject,$success_email_text,$success_email_fromname)) {
385 560 Ruebenwurz
								$success = true;
386
							}
387
						}
388
					}
389
390
					// Write submission to database
391 596 thorn
					if(isset($admin) AND $admin->is_authenticated() AND $admin->get_user_id() > 0) {
392
						$submitted_by = $admin->get_user_id();
393 560 Ruebenwurz
					} else {
394
						$submitted_by = 0;
395
					}
396
					$email_body = $wb->add_slashes($email_body);
397
					$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')");
398
					// Make sure submissions table isn't too full
399
					$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions ORDER BY submitted_when");
400
					$num_submissions = $query_submissions->numRows();
401
					if($num_submissions > $stored_submissions) {
402
						// Remove excess submission
403
						$num_to_remove = $num_submissions-$stored_submissions;
404
						while($submission = $query_submissions->fetchRow()) {
405
							if($num_to_remove > 0) {
406
								$submission_id = $submission['submission_id'];
407
								$database->query("DELETE FROM ".TABLE_PREFIX."mod_form_submissions WHERE submission_id = '$submission_id'");
408
								$num_to_remove = $num_to_remove-1;
409
							}
410
						}
411
					}
412
					if(!$database->is_error()) {
413
						$success = true;
414
					}
415
				}
416
			}
417
		}
418
	}
419
420
	// Now check if the email was sent successfully
421
	if(isset($success) AND $success == true) {
422 760 Ruebenwurz
	   if ($success_page=='none') {
423 560 Ruebenwurz
			echo str_replace("\n","<br />",$success_email_text);
424
  		} else {
425
			$query_menu = $database->query("SELECT link,target FROM ".TABLE_PREFIX."pages WHERE `page_id` = '$success_page'");
426
			if($query_menu->numRows() > 0) {
427 760 Ruebenwurz
  	        	$fetch_settings = $query_menu->fetchRow();
428
			   $link = WB_URL.PAGES_DIRECTORY.$fetch_settings['link'].PAGE_EXTENSION;
429
			   echo "<script type='text/javascript'>location.href='".$link."';</script>";
430 560 Ruebenwurz
			}
431
		}
432 760 Ruebenwurz
		// clearing session on success
433
		$query_fields = $database->query("SELECT field_id FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' AND required = 1");
434
		while($field = $query_fields->fetchRow()) {
435
			$field_id = $field[0];
436
			if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
437
		}
438 560 Ruebenwurz
	} else {
439 760 Ruebenwurz
		if(isset($success) AND $success == false) {
440
			echo $TEXT['ERROR'];
441
		}
442
	}
443 560 Ruebenwurz
}
444
445 760 Ruebenwurz
?>