Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        module
5
 * @package         Form
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, Website Baker Org. e.V.
9
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version         $Id: view.php 1457 2011-06-25 17:18:50Z Luisehahne $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/modules/form/view.php $
15
 * @lastmodified    $Date: 2011-06-25 19:18:50 +0200 (Sat, 25 Jun 2011) $
16
 * @description     
17
 */
18

    
19
// Must include code to stop this file being access directly
20
if(defined('WB_PATH') == false) { exit("Cannot access this file directly"); }
21

    
22
// check if frontend.css file needs to be included into the <body></body> of view.php
23
if((!function_exists('register_frontend_modfiles') || !defined('MOD_FRONTEND_CSS_REGISTERED')) &&
24
	file_exists(WB_PATH .'/modules/form/frontend.css')) {
25
	echo '<style type="text/css">';
26
	include(WB_PATH .'/modules/form/frontend.css');
27
	echo "\n</style>\n";
28
} 
29

    
30
require_once(WB_PATH.'/include/captcha/captcha.php');
31

    
32
// obtain the settings of the output filter module
33
if(file_exists(WB_PATH.'/modules/output_filter/filter-routines.php')) {
34
	include_once(WB_PATH.'/modules/output_filter/filter-routines.php');
35
	$filter_settings = get_output_filter_settings();
36
} else {
37
	// no output filter used, define default settings
38
	$filter_settings['email_filter'] = 0;
39
}
40

    
41
// Function for generating an optionsfor a select field
42
if (!function_exists('make_option')) {
43
function make_option(&$n, $k, $values) {
44
	// start option group if it exists
45
	if (substr($n,0,2) == '[=') {
46
	 	$n = '<optgroup label="'.substr($n,2,strlen($n)).'">';
47
	} elseif ($n == ']') {
48
		$n = '</optgroup>';
49
	} else {
50
		if(in_array($n, $values)) {
51
			$n = '<option selected="selected" value="'.$n.'">'.$n.'</option>';
52
		} else {
53
			$n = '<option value="'.$n.'">'.$n.'</option>';
54
		}
55
	}
56
}
57
}
58
// Function for generating a checkbox
59
if (!function_exists('make_checkbox')) {
60
function make_checkbox(&$n, $idx, $params) {
61
	$field_id = $params[0][0];
62
	$seperator = $params[0][1];
63
	$label_id = 'wb_'.preg_replace('/[^a-z0-1]/i', '_', $n);
64
	if(in_array($n, $params[1])) {
65
		$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;
66
	} else {
67
		$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;
68
	}	
69
}
70
}
71
// Function for generating a radio button
72
if (!function_exists('make_radio')) {
73
function make_radio(&$n, $idx, $params) {
74
	$field_id = $params[0];
75
	$group = $params[1];
76
	$seperator = $params[2];
77
	$label_id = 'wb_'.preg_replace('/[^a-z0-1]/i', '_', $n);
78
	if($n == $params[3]) { 
79
		$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;
80
	} else {
81
		$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;
82
	}
83
}
84
}
85

    
86
if (!function_exists("new_submission_id") ) {
87
	function new_submission_id() {
88
		$submission_id = '';
89
		$salt = "abchefghjkmnpqrstuvwxyz0123456789";
90
		srand((double)microtime()*1000000);
91
		$i = 0;
92
		while ($i <= 7) {
93
			$num = rand() % 33;
94
			$tmp = substr($salt, $num, 1);
95
			$submission_id = $submission_id . $tmp;
96
			$i++;
97
		}
98
		return $submission_id;
99
	}
100
}
101

    
102
// Work-out if the form has been submitted or not
103
if($_POST == array()) {
104

    
105
// Set new submission ID in session
106
$_SESSION['form_submission_id'] = new_submission_id();
107

    
108
// Get settings
109
$query_settings = $database->query("SELECT header,field_loop,footer,use_captcha FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
110
if($query_settings->numRows() > 0) {
111
	$fetch_settings = $query_settings->fetchRow();
112
	$header = str_replace('{WB_URL}',WB_URL,$fetch_settings['header']);
113
	$field_loop = $fetch_settings['field_loop'];
114
	$footer = str_replace('{WB_URL}',WB_URL,$fetch_settings['footer']);
115
	$use_captcha = $fetch_settings['use_captcha'];
116
	$form_name = 'form';
117
	$use_xhtml_strict = false;
118
} else {
119
	$header = '';
120
	$field_loop = '';
121
	$footer = '';
122
	$form_name = 'form';
123
	$use_xhtml_strict = false;
124
}
125

    
126
?>
127
<form <?php echo ( ( (strlen($form_name) > 0) AND (false == $use_xhtml_strict) ) ? "name=\"".$form_name."\"" : ""); ?> action="<?php echo htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])); ?>#wb_<?PHP echo $section_id;?>" method="post">
128
<div>
129
<input type="hidden" name="submission_id" value="<?php echo $_SESSION['form_submission_id']; ?>" />
130
<?php echo $admin->getFTAN(); ?>
131
</div>
132
<?php
133
if(ENABLED_ASP) { // first add some honeypot-fields
134
?>
135
<div>
136
<input type="hidden" name="submitted_when" value="<?php $t=time(); echo $t; $_SESSION['submitted_when']=$t; ?>" />
137
</div>
138
<p class="nixhier">
139
email address:
140
<label for="email">Leave this field email-address blank:</label>
141
<input id="email" name="email" size="56" value="" /><br />
142
Homepage:
143
<label for="homepage">Leave this field homepage blank:</label>
144
<input id="homepage" name="homepage" size="55" value="" /><br />
145
URL:
146
<label for="url">Leave this field url blank:</label>
147
<input id="url" name="url" size="61" value="" /><br />
148
Comment:
149
<label for="comment">Leave this field comment blank:</label>
150
<textarea id="comment" name="comment" cols="50" rows="10"></textarea><br />
151
</p>
152

    
153
<?php }
154

    
155
// Print header
156
echo $header;
157

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

    
161
if($query_fields->numRows() > 0) {
162
	while($field = $query_fields->fetchRow()) {
163
		// Set field values
164
		$field_id = $field['field_id'];
165
		$value = $field['value'];
166
		// Print field_loop after replacing vars with values
167
		$vars = array('{TITLE}', '{REQUIRED}');
168
		if (($field['type'] == "radio") || ($field['type'] == "checkbox")) {
169
			$field_title = $field['title'];
170
		} else {
171
			$field_title = '<label for="field'.$field_id.'">'.$field['title'].'</label>';
172
		}
173
		$values = array($field_title);
174
		if ($field['required'] == 1) {
175
			$values[] = '<span class="required">*</span>';
176
		} else {
177
			$values[] = '';
178
		}
179
		if($field['type'] == 'textfield') {
180
			$vars[] = '{FIELD}';
181
			$max_lenght_para = (intval($field['extra']) ? ' maxlenght="'.intval($field['extra']).'"' : '');
182
			$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'"'.$max_lenght_para.' value="'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:$value).'" class="textfield" />';
183
		} elseif($field['type'] == 'textarea') {
184
			$vars[] = '{FIELD}';
185
			$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>';
186
		} elseif($field['type'] == 'select') {
187
			$vars[] = '{FIELD}';
188
			$options = explode(',', $value);
189
			array_walk($options, 'make_option', (isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array()));
190
			$field['extra'] = explode(',',$field['extra']);
191
			$values[] = '<select name="field'.$field_id.'[]" id="field'.$field_id.'" size="'.$field['extra'][0].'" '.$field['extra'][1].' class="select">'.implode($options).'</select>';		
192
		} elseif($field['type'] == 'heading') {
193
			$vars[] = '{FIELD}';
194
			$str = '<input type="hidden" name="field'.$field_id.'" id="field'.$field_id.'" value="===['.$field['title'].']===" />';
195
			$values[] = ( true == $use_xhtml_strict) ? "<div>".$str."</div>" : $str;
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
			$max_lenght_para = (intval($field['extra']) ? ' maxlenght="'.intval($field['extra']).'"' : '');
213
			$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" value="'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:'').'"'.$max_lenght_para.' class="email" />';
214
		}
215
		if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
216
		if($field['type'] != '') {
217
			echo str_replace($vars, $values, $field_loop);
218
		}
219
		if (isset($tmp_field_loop)) $field_loop = $tmp_field_loop;
220
	}
221
}
222

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

    
232
// Print footer
233
echo $footer;
234

    
235
/**
236
	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)
237
	With ob_end_flush(): output filter will be disabled for this page (and all sections embedded on this page)
238
	Without ob_end_flush(): emails are rewritten (e.g. name@domain.com --> name(at)domain(dot)com) if output filter is enabled
239
	All replacements made by the Output-Filter module will be reverted before the email is send out
240
*/
241
if($filter_settings['email_filter'] && !($filter_settings['at_replacement']=='@' && $filter_settings['dot_replacement']=='.')) {
242
  /* 	ob_end_flush(); */
243
}
244

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

    
250
} else {
251

    
252
	// Check that submission ID matches
253
	if(isset($_SESSION['form_submission_id']) AND isset($_POST['submission_id']) AND $_SESSION['form_submission_id'] == $_POST['submission_id']) {
254
		
255
		// Set new submission ID in session
256
		$_SESSION['form_submission_id'] = new_submission_id();
257
		
258
		if(ENABLED_ASP && ( // form faked? Check the honeypot-fields.
259
			(!isset($_POST['submitted_when']) OR !isset($_SESSION['submitted_when'])) OR 
260
			($_POST['submitted_when'] != $_SESSION['submitted_when']) OR
261
			(!isset($_POST['email']) OR $_POST['email']) OR
262
			(!isset($_POST['homepage']) OR $_POST['homepage']) OR
263
			(!isset($_POST['comment']) OR $_POST['comment']) OR
264
			(!isset($_POST['url']) OR $_POST['url'])
265
		)) {
266
			exit(header("Location: ".WB_URL.PAGES_DIRECTORY.""));
267
		}
268
/*
269
		if (!$admin->checkFTAN())
270
		{
271
			$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS']);
272
			exit();
273
		}
274
*/
275
		// Submit form data
276
		// First start message settings
277
		$query_settings = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
278
		if($query_settings->numRows() > 0) {
279
			$fetch_settings = $query_settings->fetchRow();
280
			$email_to = $fetch_settings['email_to'];
281
			$email_from = $fetch_settings['email_from'];
282
			if(substr($email_from, 0, 5) == 'field') {
283
				// Set the email from field to what the user entered in the specified field
284
				$email_from = htmlspecialchars($wb->add_slashes($_POST[$email_from]));
285
			}
286
			$email_fromname = $fetch_settings['email_fromname'];
287
			$email_subject = $fetch_settings['email_subject'];
288
			$success_page = $fetch_settings['success_page'];
289
			$success_email_to = $fetch_settings['success_email_to'];
290
			if(substr($success_email_to, 0, 5) == 'field') {
291
				// Set the success_email to field to what the user entered in the specified field
292
				$success_email_to = htmlspecialchars($wb->add_slashes($_POST[$success_email_to]));
293
			}
294
			$success_email_from = $fetch_settings['success_email_from'];
295
			$success_email_fromname = $fetch_settings['success_email_fromname'];
296
			$success_email_text = $fetch_settings['success_email_text'];
297
			$success_email_subject = $fetch_settings['success_email_subject'];		
298
			$max_submissions = $fetch_settings['max_submissions'];
299
			$stored_submissions = $fetch_settings['stored_submissions'];
300
			$use_captcha = $fetch_settings['use_captcha'];
301
		} else {
302
			exit($TEXT['UNDER_CONSTRUCTION']);
303
		}
304
		$email_body = '';
305
		
306
		// Create blank "required" array
307
		$required = array();
308
		
309
		// Captcha
310
		if($use_captcha) {
311
			if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
312
				// Check for a mismatch
313
				if(!isset($_POST['captcha']) OR !isset($_SESSION['captcha']) OR $_POST['captcha'] != $_SESSION['captcha']) {
314
					$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
315
				}
316
			} else {
317
				$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
318
			}
319
		}
320
		if(isset($_SESSION['captcha'])) { unset($_SESSION['captcha']); }
321

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