Project

General

Profile

1 1457 Luisehahne
<?php
2
/**
3
 *
4
 * @category        module
5
 * @package         Form
6
 * @author          WebsiteBaker Project
7 1714 Luisehahne
 * @copyright       2009-2012, WebsiteBaker Org. e.V.
8 1457 Luisehahne
 * @link			http://www.websitebaker2.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11
 * @requirements    PHP 5.2.2 and higher
12
 * @version         $Id$
13
 * @filesource		$HeadURL$
14
 * @lastmodified    $Date$
15 1553 Luisehahne
 * @description
16 1457 Luisehahne
 */
17
18
// Must include code to stop this file being access directly
19 1538 Luisehahne
/* -------------------------------------------------------- */
20
if(defined('WB_PATH') == false)
21
{
22
	// Stop this file being access directly
23 1714 Luisehahne
		die('<h2 style="color:red;margin:3em auto;text-align:center;">Cannot access this file directly</h2>');
24 1538 Luisehahne
}
25
/* -------------------------------------------------------- */
26 1457 Luisehahne
27 1553 Luisehahne
// load module language file
28
$lang = (dirname(__FILE__)) . '/languages/' . LANGUAGE . '.php';
29
require_once(!file_exists($lang) ? (dirname(__FILE__)) . '/languages/EN.php' : $lang );
30 1573 Luisehahne
31
include_once(WB_PATH .'/framework/functions.php');
32 1655 Luisehahne
$aWebsiteTitle = (defined('WEBSITE_TITLE') && WEBSITE_TITLE != '' ? WEBSITE_TITLE : $_SERVER['SERVER_NAME']);
33
$replace = array('WEBSITE_TITLE' => $aWebsiteTitle );
34
$MOD_FORM['EMAIL_SUBJECT'] = replace_vars($MOD_FORM['EMAIL_SUBJECT'], $replace);
35
$MOD_FORM['SUCCESS_EMAIL_TEXT'] = replace_vars($MOD_FORM['SUCCESS_EMAIL_TEXT'], $replace);
36
$MOD_FORM['SUCCESS_EMAIL_SUBJECT'] = replace_vars($MOD_FORM['SUCCESS_EMAIL_SUBJECT'], $replace);
37 1553 Luisehahne
/*
38
function removebreaks($value) {
39
	return trim(preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', null, $value));
40 1457 Luisehahne
}
41 1553 Luisehahne
function checkbreaks($value) {
42
	return $value === removebreaks($value);
43
}
44
*/
45 1714 Luisehahne
$aSuccess =array();
46 1573 Luisehahne
if (!function_exists('emailAdmin')) {
47
	function emailAdmin() {
48
		global $database,$admin;
49
        $retval = $admin->get_email();
50
        if($admin->get_user_id()!='1') {
51
			$sql  = 'SELECT `email` FROM `'.TABLE_PREFIX.'users` ';
52
			$sql .= 'WHERE `user_id`=\'1\' ';
53
	        $retval = $database->get_one($sql);
54
55
        }
56
		return $retval;
57
	}
58
}
59
60 1457 Luisehahne
// Function for generating an optionsfor a select field
61
if (!function_exists('make_option')) {
62 1553 Luisehahne
	function make_option(&$n, $k, $values) {
63
		// start option group if it exists
64
		if (substr($n,0,2) == '[=') {
65
		 	$n = '<optgroup label="'.substr($n,2,strlen($n)).'">'.PHP_EOL;
66
		} elseif ($n == ']') {
67
			$n = '</optgroup>'.PHP_EOL;
68 1457 Luisehahne
		} else {
69 1553 Luisehahne
			if(in_array($n, $values)) {
70
				$n = '<option selected="selected" value="'.$n.'">'.$n.'</option>'.PHP_EOL;
71
			} else {
72
				$n = '<option value="'.$n.'">'.$n.'</option>'.PHP_EOL;
73
			}
74 1457 Luisehahne
		}
75
	}
76
}
77
// Function for generating a checkbox
78
if (!function_exists('make_checkbox')) {
79 1553 Luisehahne
	function make_checkbox(&$key, $idx, $params) {
80
		$field_id = $params[0][0];
81
		$seperator = $params[0][1];
82
83 1582 Luisehahne
		$label_id = 'wb_'.preg_replace('/[^a-z0-9]/i', '_', $key).$field_id;
84 1553 Luisehahne
		if(in_array($key, $params[1])) {
85
			$key = '<input class="frm-field_checkbox" type="checkbox" id="'.$label_id.'" name="field'.$field_id.'['.$idx.']" value="'.$key.'" />'.'<label for="'.$label_id.'" class="frm-checkbox_label">'.$key.'</lable>'.$seperator.PHP_EOL;
86
		} else {
87
			$key = '<input class="frm-field_checkbox" type="checkbox" id="'.$label_id.'" name="field'.$field_id.'['.$idx.']" value="'.$key.'" />'.'<label for="'.$label_id.'" class="frm-checkbox_label">'.$key.'</label>'.$seperator.PHP_EOL;
88
		}
89
	}
90 1457 Luisehahne
}
91
// Function for generating a radio button
92
if (!function_exists('make_radio')) {
93 1553 Luisehahne
	function make_radio(&$n, $idx, $params) {
94
		$field_id = $params[0];
95
		$group = $params[1];
96
		$seperator = $params[2];
97 1582 Luisehahne
		$label_id = 'wb_'.preg_replace('/[^a-z0-9]/i', '_', $n).$field_id;
98 1553 Luisehahne
		if($n == $params[3]) {
99
			$n = '<input class="frm-field_checkbox" type="radio" id="'.$label_id.'" name="field'.$field_id.'" value="'.$n.'" checked="checked" />'.'<label for="'.$label_id.'" class="frm-checkbox_label">'.$n.'</label>'.$seperator.PHP_EOL;
100
		} else {
101
			$n = '<input class="frm-field_checkbox" type="radio" id="'.$label_id.'" name="field'.$field_id.'" value="'.$n.'" />'.'<label for="'.$label_id.'" class="frm-checkbox_label">'.$n.'</label>'.$seperator.PHP_EOL;
102
		}
103 1457 Luisehahne
	}
104
}
105
106
if (!function_exists("new_submission_id") ) {
107
	function new_submission_id() {
108
		$submission_id = '';
109
		$salt = "abchefghjkmnpqrstuvwxyz0123456789";
110
		srand((double)microtime()*1000000);
111
		$i = 0;
112
		while ($i <= 7) {
113
			$num = rand() % 33;
114
			$tmp = substr($salt, $num, 1);
115
			$submission_id = $submission_id . $tmp;
116
			$i++;
117
		}
118
		return $submission_id;
119
	}
120
}
121
122
// Work-out if the form has been submitted or not
123 1655 Luisehahne
if($_POST == array())
124
{
125 1553 Luisehahne
	require_once(WB_PATH.'/include/captcha/captcha.php');
126 1457 Luisehahne
127 1553 Luisehahne
	// Set new submission ID in session
128
	$_SESSION['form_submission_id'] = new_submission_id();
129
    $out = '';
130 1457 Luisehahne
	$header = '';
131
	$field_loop = '';
132
	$footer = '';
133
	$form_name = 'form';
134
	$use_xhtml_strict = false;
135 1553 Luisehahne
	// Get settings
136
	$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'mod_form_settings` ';
137
	$sql .= 'WHERE section_id = '.$section_id.' ';
138 1655 Luisehahne
	if($query_settings = $database->query($sql))
139
	{
140
		if($query_settings->numRows() > 0)
141
		{
142 1553 Luisehahne
			$fetch_settings = $query_settings->fetchRow(MYSQL_ASSOC);
143
			$header = str_replace('{WB_URL}',WB_URL,$fetch_settings['header']);
144
			$field_loop = $fetch_settings['field_loop'];
145
			$footer = str_replace('{WB_URL}',WB_URL,$fetch_settings['footer']);
146
			$use_captcha = $fetch_settings['use_captcha'];
147
			$form_name = 'form';
148
			$use_xhtml_strict = false;
149 1655 Luisehahne
			$page_id = $fetch_settings['page_id'];
150 1553 Luisehahne
		}
151
	}
152 1457 Luisehahne
153 1553 Luisehahne
// do not use sec_anchor, can destroy some layouts
154 1714 Luisehahne
$sec_anchor = (defined( 'SEC_ANCHOR' ) && ( SEC_ANCHOR != '' )  ? '#'.SEC_ANCHOR.$fetch_settings['section_id'] : '' );
155 1457 Luisehahne
156 1553 Luisehahne
	// Get list of fields
157
	$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'mod_form_fields` ';
158
	$sql .= 'WHERE section_id = '.$section_id.' ';
159
	$sql .= 'ORDER BY position ASC ';
160 1457 Luisehahne
161 1553 Luisehahne
	if($query_fields = $database->query($sql)) {
162
		if($query_fields->numRows() > 0) {
163
?>
164
			<form <?php echo ( ( (strlen($form_name) > 0) AND (false == $use_xhtml_strict) ) ? "name=\"".$form_name."\"" : ""); ?> action="<?php echo htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'';?>" method="post">
165
				<input type="hidden" name="submission_id" value="<?php echo $_SESSION['form_submission_id']; ?>" />
166 1590 darkviper
				<?php
167
				$iFormRequestId = isset($_GET['fri']) ? intval($_GET['fri']) : 0;
168
				if($iFormRequestId) {
169
					echo '<input type="hidden" name="fri" value="'.$iFormRequestId.'" />'."\n";
170
				}
171
				?>
172 1553 Luisehahne
				<?php // echo $admin->getFTAN(); ?>
173
				<?php
174
				if(ENABLED_ASP) { // first add some honeypot-fields
175
				?>
176
					<input type="hidden" name="submitted_when" value="<?php $t=time(); echo $t; $_SESSION['submitted_when']=$t; ?>" />
177 1714 Luisehahne
					<p class="nixhier">
178 1553 Luisehahne
					email address:
179
					<label for="email">Leave this field email-address blank:</label>
180
					<input id="email" name="email" size="56" value="" /><br />
181
					Homepage:
182
					<label for="homepage">Leave this field homepage blank:</label>
183
					<input id="homepage" name="homepage" size="55" value="" /><br />
184
					URL:
185
					<label for="url">Leave this field url blank:</label>
186
					<input id="url" name="url" size="61" value="" /><br />
187
					Comment:
188
					<label for="comment">Leave this field comment blank:</label>
189
					<textarea id="comment" name="comment" cols="50" rows="10"></textarea><br />
190
					</p>
191
			<?php }
192 1457 Luisehahne
193 1553 Luisehahne
	// Print header  MYSQL_ASSOC
194 1655 Luisehahne
		   echo $header."\n";
195 1553 Luisehahne
			while($field = $query_fields->fetchRow(MYSQL_ASSOC)) {
196
				// Set field values
197
				$field_id = $field['field_id'];
198
				$value = $field['value'];
199
				// Print field_loop after replacing vars with values
200
				$vars = array('{TITLE}', '{REQUIRED}');
201
				if (($field['type'] == "radio") || ($field['type'] == "checkbox")) {
202
					$field_title = $field['title'];
203
				} else {
204
					$field_title = '<label for="field'.$field_id.'">'.$field['title'].'</label>'.PHP_EOL;
205
				}
206
				$values = array($field_title);
207
				if ($field['required'] == 1) {
208 1655 Luisehahne
					$values[] = '<span class="frm-required">*</span>'."\n";
209 1553 Luisehahne
				} else {
210
					$values[] = '';
211
				}
212
				if($field['type'] == 'textfield') {
213
					$vars[] = '{FIELD}';
214 1607 Luisehahne
					$max_lenght_para = (intval($field['extra']) ? ' maxlength="'.intval($field['extra']).'"' : '');
215 1553 Luisehahne
					$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="frm-textfield" />'.PHP_EOL;
216
				} elseif($field['type'] == 'textarea') {
217
					$vars[] = '{FIELD}';
218
					$values[] = '<textarea name="field'.$field_id.'" id="field'.$field_id.'" class="frm-textarea" cols="30" rows="8">'.(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:$value).'</textarea>'.PHP_EOL;
219
				} elseif($field['type'] == 'select') {
220
					$vars[] = '{FIELD}';
221
					$options = explode(',', $value);
222
					array_walk($options, 'make_option', (isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array()));
223
					$field['extra'] = explode(',',$field['extra']);
224 1740 Luisehahne
					$field['extra'][1] = ($field['extra'][1]=='multiple') ? $field['extra'][1].'="'.$field['extra'][1].'"' : '';
225 1553 Luisehahne
					$values[] = '<select name="field'.$field_id.'[]" id="field'.$field_id.'" size="'.$field['extra'][0].'" '.$field['extra'][1].' class="frm-select">'.implode($options).'</select>'.PHP_EOL;
226
				} elseif($field['type'] == 'heading') {
227
					$vars[] = '{FIELD}';
228
					$str = '<input type="hidden" name="field'.$field_id.'" id="field'.$field_id.'" value="===['.$field['title'].']===" />';
229
					$values[] = ( true == $use_xhtml_strict) ? "<div>".$str."</div>" : $str;
230
					$tmp_field_loop = $field_loop;		// temporarily modify the field loop template
231
					$field_loop = $field['extra'];
232
				} elseif($field['type'] == 'checkbox') {
233
					$vars[] = '{FIELD}';
234
					$options = explode(',', $value);
235
					array_walk($options, 'make_checkbox', array(array($field_id,$field['extra']),(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array())));
236
                    $x = sizeof($options)-1;
237
					$options[$x]=substr($options[$x],0,strlen($options[$x]));
238
					$values[] = implode($options);
239
				} elseif($field['type'] == 'radio') {
240
					$vars[] = '{FIELD}';
241
					$options = explode(',', $value);
242
					array_walk($options, 'make_radio', array($field_id,$field['title'],$field['extra'], (isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:'')));
243
                    $x = sizeof($options)-1;
244
					$options[$x]=substr($options[$x],0,strlen($options[$x]));
245
					$values[] = implode($options);
246
				} elseif($field['type'] == 'email') {
247
					$vars[] = '{FIELD}';
248 1607 Luisehahne
					$max_lenght_para = (intval($field['extra']) ? ' maxlength="'.intval($field['extra']).'"' : '');
249 1553 Luisehahne
					$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="frm-email" />'.PHP_EOL;
250
				}
251
				if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
252
				if($field['type'] != '') {
253
					echo str_replace($vars, $values, $field_loop);
254
				}
255
				if (isset($tmp_field_loop)){ $field_loop = $tmp_field_loop; }
256
			}
257
			// Captcha
258
			if($use_captcha) { ?>
259
				<tr>
260
				<td class="frm-field_title"><?php echo $TEXT['VERIFICATION']; ?>:</td>
261
				<td><?php call_captcha(); ?></td>
262
				</tr>
263
				<?php
264
			}
265
		// Print footer
266
		// $out = $footer.PHP_EOL;
267
		$out .= str_replace('{SUBMIT_FORM}', $MOD_FORM['SUBMIT_FORM'], $footer);
268
		echo $out;
269 1457 Luisehahne
// Add form end code
270
?>
271
</form>
272
<?php
273 1553 Luisehahne
		}
274
	}
275 1457 Luisehahne
276
} else {
277
278
	// Check that submission ID matches
279
	if(isset($_SESSION['form_submission_id']) AND isset($_POST['submission_id']) AND $_SESSION['form_submission_id'] == $_POST['submission_id']) {
280 1553 Luisehahne
281 1655 Luisehahne
	   $mail_replyto = '';
282
	   $mail_replyName = '';
283
		if( $wb->is_authenticated() && $wb->get_email() ) {
284
		   $mail_replyto = $wb->get_email();
285
		   $mail_replyName = htmlspecialchars($wb->add_slashes($wb->get_display_name()));
286
		}
287
288 1457 Luisehahne
		// Set new submission ID in session
289
		$_SESSION['form_submission_id'] = new_submission_id();
290 1553 Luisehahne
291 1457 Luisehahne
		if(ENABLED_ASP && ( // form faked? Check the honeypot-fields.
292 1553 Luisehahne
			(!isset($_POST['submitted_when']) OR !isset($_SESSION['submitted_when'])) OR
293 1457 Luisehahne
			($_POST['submitted_when'] != $_SESSION['submitted_when']) OR
294
			(!isset($_POST['email']) OR $_POST['email']) OR
295
			(!isset($_POST['homepage']) OR $_POST['homepage']) OR
296
			(!isset($_POST['comment']) OR $_POST['comment']) OR
297
			(!isset($_POST['url']) OR $_POST['url'])
298
		)) {
299 1553 Luisehahne
			// spam
300 1714 Luisehahne
			header("Location: ".WB_URL."");
301 1553 Luisehahne
            exit();
302 1457 Luisehahne
		}
303
		// Submit form data
304
		// First start message settings
305 1553 Luisehahne
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'mod_form_settings` ';
306
		$sql .= 'WHERE `section_id` = '.(int)$section_id.'';
307 1714 Luisehahne
		if($query_settings = $database->query($sql) )
308
		{
309
			if($query_settings->numRows() > 0)
310
			{
311 1553 Luisehahne
				$fetch_settings = $query_settings->fetchRow(MYSQL_ASSOC);
312
313 1573 Luisehahne
				// $email_to = $fetch_settings['email_to'];
314
				$email_to = (($fetch_settings['email_to'] != '') ? $fetch_settings['email_to'] : emailAdmin());
315 1714 Luisehahne
				$email_from = $wb->add_slashes(SERVER_EMAIL);
316 1579 Luisehahne
/*
317 1553 Luisehahne
				if(substr($email_from, 0, 5) == 'field') {
318
					// Set the email from field to what the user entered in the specified field
319
					$email_from = htmlspecialchars($wb->add_slashes($_POST[$email_from]));
320
				}
321 1579 Luisehahne
*/
322 1553 Luisehahne
				$email_fromname = $fetch_settings['email_fromname'];
323 1714 Luisehahne
 				$email_fromname = (($mail_replyName='') ? $fetch_settings['email_fromname'] : $mail_replyName);
324
// 				$email_fromname = (($mail_replyName='') ? htmlspecialchars($wb->add_slashes($fetch_settings['email_fromname'])) : $mail_replyName);
325 1655 Luisehahne
326 1553 Luisehahne
				if(substr($email_fromname, 0, 5) == 'field') {
327
					// Set the email_fromname to field to what the user entered in the specified field
328
					$email_fromname = htmlspecialchars($wb->add_slashes($_POST[$email_fromname]));
329
				}
330 1655 Luisehahne
331 1573 Luisehahne
				$email_subject = (($fetch_settings['email_subject'] != '') ? $fetch_settings['email_subject'] : $MOD_FORM['EMAIL_SUBJECT']);
332 1553 Luisehahne
				$success_page = $fetch_settings['success_page'];
333 1655 Luisehahne
				$success_email_to = $mail_replyto;
334 1714 Luisehahne
				$success_email_from = $wb->add_slashes(SERVER_EMAIL);
335
				$success_email_fromname = $fetch_settings['success_email_fromname'];
336 1655 Luisehahne
/*
337 1714 Luisehahne
*/
338
				if($mail_replyto == '') {
339
					$success_email_to = (($fetch_settings['success_email_to'] != '') ? $fetch_settings['success_email_to'] : '');
340
					if(substr($success_email_to, 0, 5) == 'field') {
341
						// Set the success_email to field to what the user entered in the specified field
342
						 $mail_replyto = $success_email_to = htmlspecialchars($wb->add_slashes($_POST[$success_email_to]));
343
					}
344
					$success_email_to = '';
345
					$email_fromname = $TEXT['UNKNOWN'];
346
//					$success_email_fromname = $TEXT['UNKNOWN'];
347
//					$email_from = $TEXT['UNKNOWN'];
348 1553 Luisehahne
				}
349 1714 Luisehahne
350 1553 Luisehahne
				$success_email_text = htmlspecialchars($wb->add_slashes($fetch_settings['success_email_text']));
351
				$success_email_text = (($success_email_text != '') ? $success_email_text : $MOD_FORM['SUCCESS_EMAIL_TEXT']);
352 1573 Luisehahne
				$success_email_subject = (($fetch_settings['success_email_subject'] != '') ? $fetch_settings['success_email_subject'] : $MOD_FORM['SUCCESS_EMAIL_SUBJECT']);
353 1553 Luisehahne
				$max_submissions = $fetch_settings['max_submissions'];
354
				$stored_submissions = $fetch_settings['stored_submissions'];
355
				$use_captcha = $fetch_settings['use_captcha'];
356
			} else {
357
				exit($TEXT['UNDER_CONSTRUCTION']);
358 1457 Luisehahne
			}
359
		}
360 1655 Luisehahne
361 1457 Luisehahne
		$email_body = '';
362
		// Create blank "required" array
363
		$required = array();
364 1553 Luisehahne
365 1457 Luisehahne
		// Captcha
366
		if($use_captcha) {
367
			if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
368 1573 Luisehahne
				// Check for a mismatch get email user_id
369 1457 Luisehahne
				if(!isset($_POST['captcha']) OR !isset($_SESSION['captcha']) OR $_POST['captcha'] != $_SESSION['captcha']) {
370 1573 Luisehahne
					$replace = array('webmaster_email' => emailAdmin() );
371 1575 Luisehahne
					$captcha_error = replace_vars($MOD_FORM['INCORRECT_CAPTCHA'], $replace);
372 1655 Luisehahne
					$required[]= '';
373 1457 Luisehahne
				}
374
			} else {
375 1573 Luisehahne
				$replace = array('webmaster_email'=>emailAdmin() );
376
				$captcha_error = replace_vars($MOD_FORM['INCORRECT_CAPTCHA'],$replace );
377 1655 Luisehahne
				$required[]= '';
378 1457 Luisehahne
			}
379
		}
380
		if(isset($_SESSION['captcha'])) { unset($_SESSION['captcha']); }
381
382
		// Loop through fields and add to message body
383
		// Get list of fields
384 1553 Luisehahne
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'mod_form_fields` ';
385
		$sql .= 'WHERE `section_id` = '.(int)$section_id.' ';
386
		$sql .= 'ORDER BY position ASC';
387
		if($query_fields = $database->query($sql)) {
388
			if($query_fields->numRows() > 0) {
389
				while($field = $query_fields->fetchRow(MYSQL_ASSOC)) {
390
					// Add to message body
391
					if($field['type'] != '') {
392 1655 Luisehahne
						if(!empty($_POST['field'.$field['field_id']]))
393
						{
394 1553 Luisehahne
							// do not allow droplets in user input!
395
							if (is_array($_POST['field'.$field['field_id']])) {
396 1655 Luisehahne
								$_SESSION['field'.$field['field_id']] = str_replace(array("[[", "]]"), array("&#91;&#91;", "&#93;&#93;"), $wb->strip_slashes($_POST['field'.$field['field_id']]));
397 1553 Luisehahne
							} else {
398 1655 Luisehahne
								$_SESSION['field'.$field['field_id']] = str_replace(array("[[", "]]"), array("&#91;&#91;", "&#93;&#93;"), htmlspecialchars($wb->strip_slashes($_POST['field'.$field['field_id']])));
399 1457 Luisehahne
							}
400 1655 Luisehahne
401 1714 Luisehahne
							if($field['type'] == 'email' AND $wb->validate_email($_POST['field'.$field['field_id']]) == false) {
402 1573 Luisehahne
								$email_error = $MESSAGE['USERS_INVALID_EMAIL'];
403 1655 Luisehahne
								$required[]= '';
404 1553 Luisehahne
							}
405
							if($field['type'] == 'heading') {
406
								$email_body .= $_POST['field'.$field['field_id']]."\n\n";
407
							} elseif (!is_array($_POST['field'.$field['field_id']])) {
408
								$email_body .= $field['title'].': '.$_POST['field'.$field['field_id']]."\n\n";
409
							} else {
410
								$email_body .= $field['title'].": \n";
411
								foreach ($_POST['field'.$field['field_id']] as $k=>$v) {
412
									$email_body .= $v."\n";
413
								}
414
								$email_body .= "\n";
415
							}
416 1655 Luisehahne
417 1553 Luisehahne
						} elseif($field['required'] == 1) {
418
							$required[] = $field['title'];
419 1457 Luisehahne
						}
420
					}
421 1553 Luisehahne
				} //  while
422
			}  // numRows
423
		} //  query
424 1655 Luisehahne
425 1553 Luisehahne
// Check if the user forgot to enter values into all the required fields
426
		if(sizeof($required )) {
427
428
			if(!isset($MESSAGE['MOD_FORM_REQUIRED_FIELDS'])) {
429
				echo '<h3>You must enter details for the following fields</h3>';
430 1457 Luisehahne
			} else {
431 1553 Luisehahne
				echo '<h3>'.$MESSAGE['MOD_FORM_REQUIRED_FIELDS'].'</h3>';
432 1457 Luisehahne
			}
433 1714 Luisehahne
			echo "<ol class=\"warning\">\n";
434 1457 Luisehahne
			foreach($required AS $field_title) {
435 1655 Luisehahne
				if($field_title!=''){
436
					echo '<li>'.$field_title."</li>\n";
437
				}
438 1457 Luisehahne
			}
439 1655 Luisehahne
440 1457 Luisehahne
			if(isset($email_error)) {
441 1655 Luisehahne
				echo '<li>'.$email_error."</li>\n";
442 1457 Luisehahne
			}
443 1655 Luisehahne
444 1457 Luisehahne
			if(isset($captcha_error)) {
445 1655 Luisehahne
				echo '<li>'.$captcha_error."</li>\n";
446 1457 Luisehahne
			}
447 1553 Luisehahne
			// Create blank "required" array
448
			$required = array();
449 1714 Luisehahne
			echo "</ol>\n";
450 1655 Luisehahne
451
			echo '<p>&nbsp;</p>'."\n".'<p><a href="'.htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'">'.$TEXT['BACK'].'</a></p>'."\n";
452 1457 Luisehahne
		} else {
453
			if(isset($email_error)) {
454 1714 Luisehahne
				echo '<br /><ol class=\"warning\">'."\n";
455 1655 Luisehahne
				echo '<li>'.$email_error.'</li>'."\n";
456 1714 Luisehahne
				echo '</ol>'."\n";
457 1553 Luisehahne
				echo '<a href="'.htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'">'.$TEXT['BACK'].'</a>';
458 1457 Luisehahne
			} elseif(isset($captcha_error)) {
459 1714 Luisehahne
				echo '<br /><ol class=\"warning\">'."\n";
460 1655 Luisehahne
				echo '<li>'.$captcha_error.'</li>'."\n";
461 1714 Luisehahne
				echo '</ol>'."\n";
462 1655 Luisehahne
				echo '<p>&nbsp;</p>'."\n".'<p><a href="'.htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'">'.$TEXT['BACK'].'</a></p>'."\n";
463 1457 Luisehahne
			} else {
464
				// Check how many times form has been submitted in last hour
465
				$last_hour = time()-3600;
466 1553 Luisehahne
				$sql  = 'SELECT `submission_id` FROM `'.TABLE_PREFIX.'mod_form_submissions` ';
467
				$sql .= 'WHERE `submitted_when` >= '.$last_hour.'';
468
				$sql .= '';
469 1655 Luisehahne
				if($query_submissions = $database->query($sql))
470
				{
471
					if($query_submissions->numRows() > $max_submissions)
472
					{
473 1553 Luisehahne
						// Too many submissions so far this hour
474
						echo $MESSAGE['MOD_FORM_EXCESS_SUBMISSIONS'];
475
						$success = false;
476
					} else {
477
						// Adding the IP to the body and try to send the email
478
						// $email_body .= "\n\nIP: ".$_SERVER['REMOTE_ADDR'];
479 1594 Luisehahne
						$iFormRequestId = isset($_POST['fri']) ? intval($_POST['fri']) : 0;
480 1590 darkviper
						if($iFormRequestId) {
481
							$email_body .= "\n\nFormRequestID: ".$iFormRequestId;
482
						}
483 1553 Luisehahne
						$recipient = preg_replace( "/[^a-z0-9 !?:;,.\/_\-=+@#$&\*\(\)]/im", "", $email_fromname );
484
						$email_fromname = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "", $recipient );
485
						$email_body = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "", $email_body );
486 1655 Luisehahne
487 1714 Luisehahne
						if($mail_replyto != '') {
488
489 1553 Luisehahne
							if($email_from != '') {
490 1655 Luisehahne
								$success = $wb->mail(SERVER_EMAIL,$email_to,$email_subject,$email_body,$email_fromname,$mail_replyto);
491 1553 Luisehahne
							} else {
492 1655 Luisehahne
								$success = $wb->mail('',$email_to,$email_subject,$email_body,$email_fromname,$mail_replyto);
493 1553 Luisehahne
							}
494
						}
495 1491 Luisehahne
496 1655 Luisehahne
						if($success==true)
497
						{
498
							$recipient = preg_replace( "/[^a-z0-9 !?:;,.\/_\-=+@#$&\*\(\)]/im", "", $success_email_fromname );
499
							$success_email_fromname = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "", $recipient );
500
							$success_email_text = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "", $success_email_text );
501 1714 Luisehahne
							if($success_email_to != '')
502
							{
503
								if($success_email_from != '')
504
								{
505
									$success = $wb->mail(SERVER_EMAIL,$success_email_to,$success_email_subject,($success_email_text).'<br /><br />'.($email_body).$MOD_FORM['SUCCESS_EMAIL_TEXT_GENERATED'],$success_email_fromname);
506 1655 Luisehahne
								} else {
507 1714 Luisehahne
									$success = $wb->mail('',$success_email_to,$success_email_subject,($success_email_text).'<br /><br />'.($email_body).$MOD_FORM['SUCCESS_EMAIL_TEXT_GENERATED'],$success_email_fromname);
508 1553 Luisehahne
								}
509 1457 Luisehahne
							}
510
						}
511 1553 Luisehahne
512 1655 Luisehahne
						if($success==true)
513
						{
514 1714 Luisehahne
							$aSuccess[] .= 'INSERT INTO '.TABLE_PREFIX.'mod_form_submissions<br /> ';;
515 1655 Luisehahne
							// Write submission to database
516 1714 Luisehahne
							if(isset($wb) AND $wb->is_authenticated() AND $wb->get_user_id() > 0) {
517
								$submitted_by = $wb->get_user_id();
518 1655 Luisehahne
							} else {
519
								$submitted_by = 0;
520
							}
521
							$email_body = htmlspecialchars($wb->add_slashes($email_body));
522
							$sql  = 'INSERT INTO '.TABLE_PREFIX.'mod_form_submissions ';
523
							$sql .= 'SET ';
524
							$sql .= 'page_id='.$wb->page_id.',';
525
							$sql .= 'section_id='.$section_id.',';
526
							$sql .= 'submitted_when='.time().',';
527
							$sql .= 'submitted_by=\''.$submitted_by.'\', ';
528
							$sql .= 'body=\''.$email_body.'\' ';
529
							if($database->query($sql))
530
							{
531 1714 Luisehahne
								// Get the page id
532
								$iSubmissionId = intval($database->get_one("SELECT LAST_INSERT_ID()"));
533
534 1655 Luisehahne
								if(!$database->is_error()) {
535
									$success = true;
536 1553 Luisehahne
								}
537 1655 Luisehahne
								// Make sure submissions table isn't too full
538
								$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions ORDER BY submitted_when");
539
								$num_submissions = $query_submissions->numRows();
540
								if($num_submissions > $stored_submissions)
541
								{
542
									// Remove excess submission
543
									$num_to_remove = $num_submissions-$stored_submissions;
544
									while($submission = $query_submissions->fetchRow(MYSQL_ASSOC))
545
									{
546
										if($num_to_remove > 0)
547
										{
548
											$submission_id = $submission['submission_id'];
549
											$database->query("DELETE FROM ".TABLE_PREFIX."mod_form_submissions WHERE submission_id = '$submission_id'");
550
											$num_to_remove = $num_to_remove-1;
551
										}
552
									}
553
								} // $num_submissions
554
							}  // numRows
555
						} // $success
556
		 			}
557
	 			} // end how many times form has been submitted in last hour
558 1553 Luisehahne
			}
559
		}  // email_error
560
	} else {
561
562
	echo '<p>&nbsp;</p>'.PHP_EOL.'<p><a href="'.htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'">'.$TEXT['BACK'].'</a></p>'.PHP_EOL;
563 1457 Luisehahne
	}
564 1553 Luisehahne
565 1655 Luisehahne
	$success_page = ( (isset($success_page) ) ? $success_page : $page_id);
566
	$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
567
	$sql .= 'WHERE `page_id` = '.(int)$success_page;
568
	$sSuccessLink = WB_URL;  // if failed set default
569
	if( ($link = $database->get_one($sql)) ) {
570
	   $sSuccessLink = WB_URL.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
571
	}
572 1457 Luisehahne
	// Now check if the email was sent successfully
573 1655 Luisehahne
	if(isset($success) && $success == true)
574
	{
575 1457 Luisehahne
	   if ($success_page=='none') {
576 1714 Luisehahne
577
			// Get submission details
578
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'mod_form_submissions` ';
579
			$sql .= 'WHERE submission_id = '.$iSubmissionId.' ';
580
			if($query_content = $database->query($sql)) {
581
				$submission = $query_content->fetchRow(MYSQL_ASSOC);
582
			}
583
			$Message = '';
584
			$NixHier = 'nixhier';
585
			// Get the user details of whoever did this submission
586
			$sql  = 'SELECT `username`,`display_name` FROM `'.TABLE_PREFIX.'users` ';
587
			$sql .= 'WHERE `user_id` = '.$submission['submitted_by'];
588
			if($get_user = $database->query($sql))
589
			{
590
				if($get_user->numRows() != 0) {
591
					$user = $get_user->fetchRow(MYSQL_ASSOC);
592
				} else {
593
					$Message = $MOD_FORM['PRINT'];
594
					$NixHier = '';
595
					$user['display_name'] = $TEXT['GUEST'];
596
					$user['username'] = $TEXT['UNKNOWN'];
597
				}
598
			}
599
600
			$aSubSuccess = array();
601
			// set template file and assign module and template block
602
			$oTpl = new Template(WB_PATH.'/modules/form/htt','keep');
603
			// $tpl = new Template(dirname($admin->correct_theme_source('switchform.htt')),'keep');
604
			$oTpl->set_file('page', 'submessage.htt');
605
			$oTpl->debug = false; // false, true
606
			$oTpl->set_block('page', 'main_block', 'main');
607
            $oTpl->set_var(array(
608
            		'ADMIN_URL' => ADMIN_URL,
609
            		'THEME_URL' => THEME_URL,
610
            		'MODULE_URL' => dirname(__FILE__),
611
            		'WB_URL' => WB_URL
612
            	)
613
            );
614
			$oTpl->set_var(array(
615
					'SUCCESS_EMAIL_TEXT' => $success_email_text,
616
					'TEXT_SUBMISSION_ID' => $TEXT['SUBMISSION_ID'],
617
					'submission_submission_id' => $submission['submission_id'],
618
					'TEXT_SUBMITTED' => $TEXT['SUBMITTED'],
619
					'submission_submitted_when' => gmdate( DATE_FORMAT .', '.TIME_FORMAT, $submission['submitted_when']+TIMEZONE ),
620
					'NIX_HIER' => $NixHier,
621
					'TEXT_USER' => $TEXT['USER'],
622
					'TEXT_PRINT_PAGE' => $TEXT['PRINT_PAGE'],
623
					'TEXT_REQUIRED_JS' => $TEXT['REQUIRED_JS'],
624
					'user_display_name' => $user['display_name'],
625
					'user_username' => $user['username'],
626
					'SUCCESS_PRINT' => $Message,
627
					'submission_body' => nl2br($submission['body'])
628
					)
629
				);
630
631
			$oTpl->parse('main', 'main_block', false);
632
			$output = $oTpl->finish($oTpl->parse('output', 'page'));
633
			unset($oTpl);
634
			print $output;
635
636 1457 Luisehahne
  		} else {
637 1655 Luisehahne
			echo "<script type='text/javascript'>location.href='".$sSuccessLink."';</script>";
638 1457 Luisehahne
		}
639
		// clearing session on success
640
		$query_fields = $database->query("SELECT field_id FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id'");
641 1553 Luisehahne
		while($field = $query_fields->fetchRow(MYSQL_ASSOC)) {
642
			$field_id = $field['field_id'];
643 1457 Luisehahne
			if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
644
		}
645
	} else {
646 1655 Luisehahne
		if(isset($success) && $success == false) {
647
			echo '<br />'.$MOD_FORM['ERROR'];
648
			echo '<p>&nbsp;</p>'.PHP_EOL.'<p><a href="'.htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'">'.$TEXT['BACK'].'</a></p>'.PHP_EOL;
649 1457 Luisehahne
		}
650
	}
651 1553 Luisehahne
652 1590 darkviper
}