Project

General

Profile

1
<?php
2

    
3
// $Id: view.php 915 2009-01-21 19:27:01Z Ruebenwurzel $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, 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
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
} 
41

    
42
require_once(WB_PATH.'/include/captcha/captcha.php');
43

    
44
// obtain the settings of the output filter module
45
if(file_exists(WB_PATH.'/modules/output_filter/filter-routines.php')) {
46
	include_once(WB_PATH.'/modules/output_filter/filter-routines.php');
47
	$filter_settings = get_output_filter_settings();
48
} else {
49
	// no output filter used, define default settings
50
	$filter_settings['email_filter'] = 0;
51
}
52

    
53
// Function for generating an optionsfor a select field
54
if (!function_exists('make_option')) {
55
function make_option(&$n, $k, $values) {
56
	// start option group if it exists
57
	if (substr($n,0,2) == '[=') {
58
	 	$n = '<optgroup label="'.substr($n,2,strlen($n)).'">';
59
	} elseif ($n == ']') {
60
		$n = '</optgroup>';
61
	} else {
62
		if(in_array($n, $values)) {
63
			$n = '<option selected="selected" value="'.$n.'">'.$n.'</option>';
64
		} else {
65
			$n = '<option value="'.$n.'">'.$n.'</option>';
66
		}
67
	}
68
}
69
}
70
// Function for generating a checkbox
71
if (!function_exists('make_checkbox')) {
72
function make_checkbox(&$n, $idx, $params) {
73
	$field_id = $params[0][0];
74
	$seperator = $params[0][1];
75
	$label_id = 'wb_'.str_replace(" ", "_", $n);
76
	if(in_array($n, $params[1])) {
77
		$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;
78
	} else {
79
		$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;
80
	}	
81
}
82
}
83
// Function for generating a radio button
84
if (!function_exists('make_radio')) {
85
function make_radio(&$n, $idx, $params) {
86
	$field_id = $params[0];
87
	$group = $params[1];
88
	$seperator = $params[2];
89
	$label_id = 'wb_'.str_replace(" ", "_", $n);
90
	if($n == $params[3]) { 
91
		$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;
92
	} else {
93
		$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;
94
	}
95
}
96
}
97

    
98
if (!function_exists("new_submission_id") ) {
99
	function new_submission_id() {
100
		$submission_id = '';
101
		$salt = "abchefghjkmnpqrstuvwxyz0123456789";
102
		srand((double)microtime()*1000000);
103
		$i = 0;
104
		while ($i <= 7) {
105
			$num = rand() % 33;
106
			$tmp = substr($salt, $num, 1);
107
			$submission_id = $submission_id . $tmp;
108
			$i++;
109
		}
110
		return $submission_id;
111
	}
112
}
113
// Work-out if the form has been submitted or not
114
if($_POST == array()) {
115

    
116
// Set new submission ID in session
117
$_SESSION['form_submission_id'] = new_submission_id();
118

    
119
// Get settings
120
$query_settings = $database->query("SELECT header,field_loop,footer,use_captcha FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
121
if($query_settings->numRows() > 0) {
122
	$fetch_settings = $query_settings->fetchRow();
123
	$header = str_replace('{WB_URL}',WB_URL,$fetch_settings['header']);
124
	$field_loop = $fetch_settings['field_loop'];
125
	$footer = str_replace('{WB_URL}',WB_URL,$fetch_settings['footer']);
126
	$use_captcha = $fetch_settings['use_captcha'];
127
} else {
128
	$header = '';
129
	$field_loop = '';
130
	$footer = '';
131
}
132

    
133
?>
134
<form name="form" action="<?php echo htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])); ?>" method="post">
135
<input type="hidden" name="submission_id" value="<?php echo $_SESSION['form_submission_id']; ?>" />
136
<?php
137
if(ENABLED_ASP) { // first add some honeypot-fields
138
?>
139
<input type="hidden" name="submitted_when" value="<?php $t=time(); echo $t; $_SESSION['submitted_when']=$t; ?>" />
140
<p class="nixhier">
141
email address:
142
<label for="email">Leave this field email-address blank:</label>
143
<input id="email" name="email" size="56" value="" /><br />
144
Homepage:
145
<label for="homepage">Leave this field homepage blank:</label>
146
<input id="homepage" name="homepage" size="55" value="" /><br />
147
URL:
148
<label for="url">Leave this field url blank:</label>
149
<input id="url" name="url" size="61" value="" /><br />
150
Comment:
151
<label for="comment">Leave this field comment blank:</label>
152
<textarea id="comment" name="comment" cols="50" rows="10"></textarea><br />
153
</p>
154

    
155
<?php }
156

    
157
// Print header
158
echo $header;
159

    
160
// Get list of fields
161
$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
162

    
163
if($query_fields->numRows() > 0) {
164
	while($field = $query_fields->fetchRow()) {
165
		// Set field values
166
		$field_id = $field['field_id'];
167
		$value = $field['value'];
168
		// Print field_loop after replacing vars with values
169
		$vars = array('{TITLE}', '{REQUIRED}');
170
		if (($field['type'] == "radio") || ($field['type'] == "checkbox")) {
171
			$field_title = $field['title'];
172
		} else {
173
			$field_title = '<label for="field'.$field_id.'">'.$field['title'].'</label>';
174
		}
175
		$values = array($field_title);
176
		if ($field['required'] == 1) {
177
			$values[] = '<span class="required">*</span>';
178
		} else {
179
			$values[] = '';
180
		}
181
		if($field['type'] == 'textfield') {
182
			$vars[] = '{FIELD}';
183
			$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" />';
184
		} elseif($field['type'] == 'textarea') {
185
			$vars[] = '{FIELD}';
186
			$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>';
187
		} elseif($field['type'] == 'select') {
188
			$vars[] = '{FIELD}';
189
			$options = explode(',', $value);
190
			array_walk($options, 'make_option', (isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array()));
191
			$field['extra'] = explode(',',$field['extra']);
192
			$values[] = '<select name="field'.$field_id.'[]" id="field'.$field_id.'" size="'.$field['extra'][0].'" '.$field['extra'][1].' class="select">'.implode($options).'</select>';		
193
		} elseif($field['type'] == 'heading') {
194
			$vars[] = '{FIELD}';
195
			$values[] = '<input type="hidden" name="field'.$field_id.'" id="field'.$field_id.'" value="===['.$field['title'].']===" />';
196
			$tmp_field_loop = $field_loop;		// temporarily modify the field loop template
197
			$field_loop = $field['extra'];
198
		} elseif($field['type'] == 'checkbox') {
199
			$vars[] = '{FIELD}';
200
			$options = explode(',', $value);
201
			array_walk($options, 'make_checkbox', array(array($field_id,$field['extra']),(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array())));
202
			$options[count($options)-1]=substr($options[count($options)-1],0,strlen($options[count($options)-1])-strlen($field['extra']));
203
			$values[] = implode($options);
204
		} elseif($field['type'] == 'radio') {
205
			$vars[] = '{FIELD}';
206
			$options = explode(',', $value);
207
			array_walk($options, 'make_radio', array($field_id,$field['title'],$field['extra'], (isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:'')));
208
			$options[count($options)-1]=substr($options[count($options)-1],0,strlen($options[count($options)-1])-strlen($field['extra']));
209
			$values[] = implode($options);
210
		} elseif($field['type'] == 'email') {
211
			$vars[] = '{FIELD}';
212
			$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" />';
213
		}
214
		if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
215
		if($field['type'] != '') {
216
			echo str_replace($vars, $values, $field_loop);
217
		}
218
		if (isset($tmp_field_loop)) $field_loop = $tmp_field_loop;
219
	}
220
}
221

    
222
// Captcha
223
if($use_captcha) { ?>
224
	<tr>
225
	<td class="field_title"><?php echo $TEXT['VERIFICATION']; ?>:</td>
226
	<td><?php call_captcha(); ?></td>
227
	</tr>
228
	<?php
229
}
230

    
231
// Print footer
232
echo $footer;
233

    
234
/**
235
	NOTE: comment out the line ob_end_flush() if you indicate problems (e.g. when using ob_start in the index.php of your template)
236
	With ob_end_flush(): output filter will be disabled for this page (and all sections embedded on this page)
237
	Without ob_end_flush(): emails are rewritten (e.g. name@domain.com --> name(at)domain(dot)com) if output filter is enabled
238
	All replacements made by the Output-Filter module will be reverted before the email is send out
239
*/
240
if($filter_settings['email_filter'] && !($filter_settings['at_replacement']=='@' && $filter_settings['dot_replacement']=='.')) { 
241
	ob_end_flush();
242
}
243

    
244
// Add form end code
245
?>
246
</form>
247
<?php
248

    
249
} else {
250
	
251
	// Check that submission ID matches
252
	if(isset($_SESSION['form_submission_id']) AND isset($_POST['submission_id']) AND $_SESSION['form_submission_id'] == $_POST['submission_id']) {
253
		
254
		// Set new submission ID in session
255
		$_SESSION['form_submission_id'] = new_submission_id();
256
		
257
		if(ENABLED_ASP && ( // form faked? Check the honeypot-fields.
258
			(!isset($_POST['submitted_when']) OR !isset($_SESSION['submitted_when'])) OR 
259
			($_POST['submitted_when'] != $_SESSION['submitted_when']) OR
260
			(!isset($_POST['email']) OR $_POST['email']) OR
261
			(!isset($_POST['homepage']) OR $_POST['homepage']) OR
262
			(!isset($_POST['comment']) OR $_POST['comment']) OR
263
			(!isset($_POST['url']) OR $_POST['url'])
264
		)) {
265
			exit(header("Location: ".WB_URL.PAGES_DIRECTORY.""));
266
		}
267

    
268
		// Submit form data
269
		// First start message settings
270
		$query_settings = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
271
		if($query_settings->numRows() > 0) {
272
			$fetch_settings = $query_settings->fetchRow();
273
			$email_to = $fetch_settings['email_to'];
274
			$email_from = $fetch_settings['email_from'];
275
			if(substr($email_from, 0, 5) == 'field') {
276
				// Set the email from field to what the user entered in the specified field
277
				$email_from = htmlspecialchars($wb->add_slashes($_POST[$email_from]));
278
			}
279
			$email_fromname = $fetch_settings['email_fromname'];
280
			$email_subject = $fetch_settings['email_subject'];
281
			$success_page = $fetch_settings['success_page'];
282
			$success_email_to = $fetch_settings['success_email_to'];
283
			if(substr($success_email_to, 0, 5) == 'field') {
284
				// Set the success_email to field to what the user entered in the specified field
285
				$success_email_to = htmlspecialchars($wb->add_slashes($_POST[$success_email_to]));
286
			}
287
			$success_email_from = $fetch_settings['success_email_from'];
288
			$success_email_fromname = $fetch_settings['success_email_fromname'];
289
			$success_email_text = $fetch_settings['success_email_text'];
290
			$success_email_subject = $fetch_settings['success_email_subject'];		
291
			$max_submissions = $fetch_settings['max_submissions'];
292
			$stored_submissions = $fetch_settings['stored_submissions'];
293
			$use_captcha = $fetch_settings['use_captcha'];
294
		} else {
295
			exit($TEXT['UNDER_CONSTRUCTION']);
296
		}
297
		$email_body = '';
298
		
299
		// Create blank "required" array
300
		$required = array();
301
		
302
		// Captcha
303
		if($use_captcha) {
304
			if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
305
				// Check for a mismatch
306
				if(!isset($_POST['captcha']) OR !isset($_SESSION['captcha']) OR $_POST['captcha'] != $_SESSION['captcha']) {
307
					$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
308
				}
309
			} else {
310
				$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
311
			}
312
		}
313
		if(isset($_SESSION['captcha'])) { unset($_SESSION['captcha']); }
314

    
315
		// Loop through fields and add to message body
316
		// Get list of fields
317
		$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
318
		if($query_fields->numRows() > 0) {
319
			while($field = $query_fields->fetchRow()) {
320
				// Add to message body
321
				if($field['type'] != '') {
322
					if(!empty($_POST['field'.$field['field_id']])) {
323
						if (is_array($_POST['field'.$field['field_id']])) {
324
							$_SESSION['field'.$field['field_id']] = $_POST['field'.$field['field_id']];
325
						} else {
326
							$_SESSION['field'.$field['field_id']] = htmlspecialchars($_POST['field'.$field['field_id']]);
327
						}
328
						// if the output filter is active, we need to revert (dot) to . and (at) to @ (using current filter settings)
329
						// otherwise the entered mail will not be accepted and the recipient would see (dot), (at) etc.
330
						if ($filter_settings['email_filter']) {
331
							$field_value = $_POST['field'.$field['field_id']];
332
							$field_value = str_replace($filter_settings['at_replacement'], '@', $field_value);
333
							$field_value = str_replace($filter_settings['dot_replacement'], '.', $field_value);
334
							$_POST['field'.$field['field_id']] = $field_value;
335
						}
336
						if($field['type'] == 'email' AND $admin->validate_email($_POST['field'.$field['field_id']]) == false) {
337
							$email_error = $MESSAGE['USERS']['INVALID_EMAIL'];
338
						}
339
						if($field['type'] == 'heading') {
340
							$email_body .= $_POST['field'.$field['field_id']]."\n\n";
341
						} elseif (!is_array($_POST['field'.$field['field_id']])) {
342
							$email_body .= $field['title'].': '.$_POST['field'.$field['field_id']]."\n\n";
343
						} else {
344
							$email_body .= $field['title'].": \n";
345
							foreach ($_POST['field'.$field['field_id']] as $k=>$v) {
346
								$email_body .= $v."\n";
347
							}
348
							$email_body .= "\n";
349
						}
350
					} elseif($field['required'] == 1) {
351
						$required[] = $field['title'];
352
					}
353
				}
354
			}
355
		}
356
	
357
		// Check if the user forgot to enter values into all the required fields
358
		if($required != array()) {
359
			if(!isset($MESSAGE['MOD_FORM']['REQUIRED_FIELDS'])) {
360
				echo 'You must enter details for the following fields';
361
			} else {
362
				echo $MESSAGE['MOD_FORM']['REQUIRED_FIELDS'];
363
			}
364
			echo ':<br /><ul>';
365
			foreach($required AS $field_title) {
366
				echo '<li>'.$field_title;
367
			}
368
			if(isset($email_error)) {
369
				echo '<li>'.$email_error.'</li>';
370
			}
371
			if(isset($captcha_error)) {
372
				echo '<li>'.$captcha_error.'</li>';
373
			}
374
			echo '</ul><a href="'.htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])).'">'.$TEXT['BACK'].'</a>';
375
		} else {
376
			if(isset($email_error)) {
377
				echo '<br /><ul>';
378
				echo '<li>'.$email_error.'</li>';
379
				echo '</ul><a href="'.htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])).'">'.$TEXT['BACK'].'</a>';
380
			} elseif(isset($captcha_error)) {
381
				echo '<br /><ul>';
382
				echo '<li>'.$captcha_error.'</li>';
383
				echo '</ul><a href="'.htmlspecialchars(strip_tags($_SERVER['PHP_SELF'])).'">'.$TEXT['BACK'].'</a>';
384
			} else {
385
				// Check how many times form has been submitted in last hour
386
				$last_hour = time()-3600;
387
				$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions WHERE submitted_when >= '$last_hour'");
388
				if($query_submissions->numRows() > $max_submissions) {
389
					// Too many submissions so far this hour
390
					echo $MESSAGE['MOD_FORM']['EXCESS_SUBMISSIONS'];
391
					$success = false;
392
				} else {
393
					/**	
394
					 *	Adding the IP to the body and try to send the email
395
					 */
396
					$email_body .= "\n\nIP: ".$_SERVER['REMOTE_ADDR'];
397
					
398
					if($email_to != '') {
399
						if($email_from != '') {
400
							if($wb->mail($email_from,$email_to,$email_subject,$email_body,$email_fromname)) {
401
								$success = true;
402
							}
403
						} else {
404
							if($wb->mail('',$email_to,$email_subject,$email_body,$email_fromname)) { 
405
								$success = true; 
406
							}
407
						}
408
					}				
409
					if($success_email_to != '') {
410
						if($success_email_from != '') {
411
							if($wb->mail($success_email_from,$success_email_to,$success_email_subject,$success_email_text,$success_email_fromname)) {
412
								$success = true;
413
							}
414
						} else {
415
							if($wb->mail('',$success_email_to,$success_email_subject,$success_email_text,$success_email_fromname)) {
416
								$success = true;
417
							}
418
						}
419
					}				
420
			
421
					// Write submission to database
422
					if(isset($admin) AND $admin->is_authenticated() AND $admin->get_user_id() > 0) {
423
						$submitted_by = $admin->get_user_id();
424
					} else {
425
						$submitted_by = 0;
426
					}
427
					$email_body = $wb->add_slashes($email_body);
428
					$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')");
429
					// Make sure submissions table isn't too full
430
					$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions ORDER BY submitted_when");
431
					$num_submissions = $query_submissions->numRows();
432
					if($num_submissions > $stored_submissions) {
433
						// Remove excess submission
434
						$num_to_remove = $num_submissions-$stored_submissions;
435
						while($submission = $query_submissions->fetchRow()) {
436
							if($num_to_remove > 0) {
437
								$submission_id = $submission['submission_id'];
438
								$database->query("DELETE FROM ".TABLE_PREFIX."mod_form_submissions WHERE submission_id = '$submission_id'");
439
								$num_to_remove = $num_to_remove-1;
440
							}
441
						}
442
					}
443
					if(!$database->is_error()) {
444
						$success = true;
445
					}
446
				}
447
			}	
448
		}
449
	}
450
	
451
	// Now check if the email was sent successfully
452
	if(isset($success) AND $success == true) {
453
	   if ($success_page=='none') {
454
			echo str_replace("\n","<br />",$success_email_text);
455
  		} else {
456
			$query_menu = $database->query("SELECT link,target FROM ".TABLE_PREFIX."pages WHERE `page_id` = '$success_page'");
457
			if($query_menu->numRows() > 0) {
458
  	        	$fetch_settings = $query_menu->fetchRow();
459
			   $link = WB_URL.PAGES_DIRECTORY.$fetch_settings['link'].PAGE_EXTENSION;
460
			   echo "<script type='text/javascript'>location.href='".$link."';</script>";
461
			}    
462
		}
463
		// clearing session on success
464
		$query_fields = $database->query("SELECT field_id FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' AND required = 1");
465
		while($field = $query_fields->fetchRow()) {
466
			$field_id = $field[0];
467
			if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
468
		}
469
	} else {
470
		if(isset($success) AND $success == false) {
471
			echo $TEXT['ERROR'];
472
		}
473
	}	
474
}
475

    
476
?>
(20-20/21)