Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        module
5
 * @package         Form
6
 * @author          WebsiteBaker Project
7
 * @copyright       2009-2012, WebsiteBaker Org. e.V.
8
 * @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: view.php 1781 2012-10-10 17:49:22Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/modules/form/view.php $
14
 * @lastmodified    $Date: 2012-10-10 19:49:22 +0200 (Wed, 10 Oct 2012) $
15
 * @description
16
 */
17

    
18
// Must include code to stop this file being access directly
19
/* -------------------------------------------------------- */
20
if(defined('WB_PATH') == false)
21
{
22
	// Stop this file being access directly
23
		die('<h2 style="color:red;margin:3em auto;text-align:center;">Cannot access this file directly</h2>');
24
}
25
/* -------------------------------------------------------- */
26

    
27
// load module language file
28
$lang = (dirname(__FILE__)) . '/languages/' . LANGUAGE . '.php';
29
require_once(!file_exists($lang) ? (dirname(__FILE__)) . '/languages/EN.php' : $lang );
30

    
31
include_once(WB_PATH .'/framework/functions.php');
32
$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
/*
38
function removebreaks($value) {
39
	return trim(preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', null, $value));
40
}
41
function checkbreaks($value) {
42
	return $value === removebreaks($value);
43
}
44
*/
45
$aSuccess =array();
46
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
// Function for generating an optionsfor a select field
61
if (!function_exists('make_option')) {
62
	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
		} else {
69
			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
		}
75
	}
76
}
77
// Function for generating a checkbox
78
if (!function_exists('make_checkbox')) {
79
	function make_checkbox(&$key, $idx, $params) {
80
		$field_id = $params[0][0];
81
		$seperator = $params[0][1];
82

    
83
		$label_id = 'wb_'.preg_replace('/[^a-z0-9]/i', '_', $key).$field_id;
84
		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
}
91
// Function for generating a radio button
92
if (!function_exists('make_radio')) {
93
	function make_radio(&$n, $idx, $params) {
94
		$field_id = $params[0];
95
		$group = $params[1];
96
		$seperator = $params[2];
97
		$label_id = 'wb_'.preg_replace('/[^a-z0-9]/i', '_', $n).$field_id;
98
		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
	}
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
if($_POST == array())
124
{
125
	require_once(WB_PATH.'/include/captcha/captcha.php');
126

    
127
	// Set new submission ID in session
128
	$_SESSION['form_submission_id'] = new_submission_id();
129
    $out = '';
130
	$header = '';
131
	$field_loop = '';
132
	$footer = '';
133
	$form_name = 'form';
134
	$use_xhtml_strict = false;
135
	// Get settings
136
	$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'mod_form_settings` ';
137
	$sql .= 'WHERE section_id = '.$section_id.' ';
138
	if($query_settings = $database->query($sql))
139
	{
140
		if($query_settings->numRows() > 0)
141
		{
142
			$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
			$page_id = $fetch_settings['page_id'];
150
		}
151
	}
152

    
153
// do not use sec_anchor, can destroy some layouts
154
$sec_anchor = (defined( 'SEC_ANCHOR' ) && ( SEC_ANCHOR != '' )  ? '#'.SEC_ANCHOR.$fetch_settings['section_id'] : 'section_'.$section_id );
155

    
156
	// 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

    
161
	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) ) ? "id=\"".$form_name.$section_id."\"" : ""); ?> action="<?php echo htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'';?>" method="post">
165
            <fieldset>
166
				<input type="hidden" name="submission_id" value="<?php echo $_SESSION['form_submission_id']; ?>" />
167
				<?php
168
				$iFormRequestId = isset($_GET['fri']) ? intval($_GET['fri']) : 0;
169
				if($iFormRequestId) {
170
					echo '<input type="hidden" name="fri" value="'.$iFormRequestId.'" />'."\n";
171
				}
172
				?>
173
				<?php // echo $admin->getFTAN(); ?>
174
				<?php
175
				if(ENABLED_ASP) { // first add some honeypot-fields
176
				?>
177
					<input type="hidden" name="submitted_when" value="<?php $t=time(); echo $t; $_SESSION['submitted_when']=$t; ?>" />
178
					<p class="nixhier">
179
					email address:
180
					<label for="email">Leave this field email-address blank:</label>
181
					<input id="email" name="email" size="56" value="" /><br />
182
					Homepage:
183
					<label for="homepage">Leave this field homepage blank:</label>
184
					<input id="homepage" name="homepage" size="55" value="" /><br />
185
					URL:
186
					<label for="url">Leave this field url blank:</label>
187
					<input id="url" name="url" size="61" value="" /><br />
188
					Comment:
189
					<label for="comment">Leave this field comment blank:</label>
190
					<textarea id="comment" name="comment" cols="50" rows="10"></textarea><br />
191
					</p>
192
			<?php }
193

    
194
	// Print header  MYSQL_ASSOC
195
		   echo $header."\n";
196
			while($field = $query_fields->fetchRow(MYSQL_ASSOC)) {
197
				// Set field values
198
				$field_id = $field['field_id'];
199
				$value = $field['value'];
200
				// Print field_loop after replacing vars with values
201
				$vars = array('{TITLE}', '{REQUIRED}');
202
				if (($field['type'] == "radio") || ($field['type'] == "checkbox")) {
203
					$field_title = $field['title'];
204
				} else {
205
					$field_title = '<label for="field'.$field_id.'">'.$field['title'].'</label>'.PHP_EOL;
206
				}
207
				$values = array($field_title);
208
				if ($field['required'] == 1) {
209
					$values[] = '<span class="frm-required">*</span>'."\n";
210
				} else {
211
					$values[] = '';
212
				}
213
				if($field['type'] == 'textfield') {
214
					$vars[] = '{FIELD}';
215
					$max_lenght_para = (intval($field['extra']) ? ' maxlength="'.intval($field['extra']).'"' : '');
216
					$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;
217
				} elseif($field['type'] == 'textarea') {
218
					$vars[] = '{FIELD}';
219
					$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;
220
				} elseif($field['type'] == 'select') {
221
					$vars[] = '{FIELD}';
222
					$options = explode(',', $value);
223
					array_walk($options, 'make_option', (isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array()));
224
					$field['extra'] = explode(',',$field['extra']);
225
					$field['extra'][1] = ($field['extra'][1]=='multiple') ? $field['extra'][1].'="'.$field['extra'][1].'"' : '';
226
					$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;
227
				} elseif($field['type'] == 'heading') {
228
					$vars[] = '{FIELD}';
229
					$str = '<input type="hidden" name="field'.$field_id.'" id="field'.$field_id.'" value="===['.$field['title'].']===" />';
230
					$values[] = ( true == $use_xhtml_strict) ? "<div>".$str."</div>" : $str;
231
					$tmp_field_loop = $field_loop;		// temporarily modify the field loop template
232
					$field_loop = $field['extra'];
233
				} elseif($field['type'] == 'checkbox') {
234
					$vars[] = '{FIELD}';
235
					$options = explode(',', $value);
236
					array_walk($options, 'make_checkbox', array(array($field_id,$field['extra']),(isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:array())));
237
                    $x = sizeof($options)-1;
238
					$options[$x]=substr($options[$x],0,strlen($options[$x]));
239
					$values[] = implode($options);
240
				} elseif($field['type'] == 'radio') {
241
					$vars[] = '{FIELD}';
242
					$options = explode(',', $value);
243
					array_walk($options, 'make_radio', array($field_id,$field['title'],$field['extra'], (isset($_SESSION['field'.$field_id])?$_SESSION['field'.$field_id]:'')));
244
                    $x = sizeof($options)-1;
245
					$options[$x]=substr($options[$x],0,strlen($options[$x]));
246
					$values[] = implode($options);
247
				} elseif($field['type'] == 'email') {
248
					$vars[] = '{FIELD}';
249
					$max_lenght_para = (intval($field['extra']) ? ' maxlength="'.intval($field['extra']).'"' : '');
250
					$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;
251
				}
252
				if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
253
				if($field['type'] != '') {
254
					echo str_replace($vars, $values, $field_loop);
255
				}
256
				if (isset($tmp_field_loop)){ $field_loop = $tmp_field_loop; }
257
			}
258
			// Captcha
259
			if($use_captcha) { ?>
260
				<tr>
261
				<td class="frm-field_title"><?php echo $TEXT['VERIFICATION']; ?>:</td>
262
				<td><?php call_captcha(); ?></td>
263
				</tr>
264
				<?php
265
			}
266
		// Print footer
267
		// $out = $footer.PHP_EOL;
268
		$out .= str_replace('{SUBMIT_FORM}', $MOD_FORM['SUBMIT_FORM'], $footer);
269
		echo $out;
270
// Add form end code
271
?>
272
        </fieldset>
273
</form>
274
<?php
275
		}
276
	}
277

    
278
} else {
279

    
280
	// Check that submission ID matches
281
	if(isset($_SESSION['form_submission_id']) AND isset($_POST['submission_id']) AND $_SESSION['form_submission_id'] == $_POST['submission_id']) {
282

    
283
	   $mail_replyto = '';
284
	   $mail_replyName = '';
285
		if( $wb->is_authenticated() && $wb->get_email() ) {
286
		   $mail_replyto = $wb->get_email();
287
		   $mail_replyName = htmlspecialchars($wb->add_slashes($wb->get_display_name()));
288
		}
289

    
290
		// Set new submission ID in session
291
		$_SESSION['form_submission_id'] = new_submission_id();
292

    
293
		if(ENABLED_ASP && ( // form faked? Check the honeypot-fields.
294
			(!isset($_POST['submitted_when']) OR !isset($_SESSION['submitted_when'])) OR
295
			($_POST['submitted_when'] != $_SESSION['submitted_when']) OR
296
			(!isset($_POST['email']) OR $_POST['email']) OR
297
			(!isset($_POST['homepage']) OR $_POST['homepage']) OR
298
			(!isset($_POST['comment']) OR $_POST['comment']) OR
299
			(!isset($_POST['url']) OR $_POST['url'])
300
		)) {
301
			// spam
302
			header("Location: ".WB_URL."");
303
            exit();
304
		}
305
		// Submit form data
306
		// First start message settings
307
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'mod_form_settings` ';
308
		$sql .= 'WHERE `section_id` = '.(int)$section_id.'';
309
		if($query_settings = $database->query($sql) )
310
		{
311
			if($query_settings->numRows() > 0)
312
			{
313
				$fetch_settings = $query_settings->fetchRow(MYSQL_ASSOC);
314

    
315
				// $email_to = $fetch_settings['email_to'];
316
				$email_to = (($fetch_settings['email_to'] != '') ? $fetch_settings['email_to'] : emailAdmin());
317
				$email_from = $wb->add_slashes(SERVER_EMAIL);
318
/*
319
				if(substr($email_from, 0, 5) == 'field') {
320
					// Set the email from field to what the user entered in the specified field
321
					$email_from = htmlspecialchars($wb->add_slashes($_POST[$email_from]));
322
				}
323
*/
324
				$email_fromname = $fetch_settings['email_fromname'];
325
 				$email_fromname = (($mail_replyName='') ? $fetch_settings['email_fromname'] : $mail_replyName);
326
// 				$email_fromname = (($mail_replyName='') ? htmlspecialchars($wb->add_slashes($fetch_settings['email_fromname'])) : $mail_replyName);
327

    
328
				if(substr($email_fromname, 0, 5) == 'field') {
329
					// Set the email_fromname to field to what the user entered in the specified field
330
					$email_fromname = htmlspecialchars($wb->add_slashes($_POST[$email_fromname]));
331
				}
332

    
333
				$email_subject = (($fetch_settings['email_subject'] != '') ? $fetch_settings['email_subject'] : $MOD_FORM['EMAIL_SUBJECT']);
334
				$success_page = $fetch_settings['success_page'];
335
				$success_email_to = $mail_replyto;
336
				$success_email_from = $wb->add_slashes(SERVER_EMAIL);
337
				$success_email_fromname = $fetch_settings['success_email_fromname'];
338
/*
339
*/
340
				if($mail_replyto == '') {
341
					$success_email_to = (($fetch_settings['success_email_to'] != '') ? $fetch_settings['success_email_to'] : '');
342
					if(substr($success_email_to, 0, 5) == 'field') {
343
						// Set the success_email to field to what the user entered in the specified field
344
						 $mail_replyto = $success_email_to = htmlspecialchars($wb->add_slashes($_POST[$success_email_to]));
345
					}
346
					$success_email_to = '';
347
					$email_fromname = $TEXT['UNKNOWN'];
348
//					$success_email_fromname = $TEXT['UNKNOWN'];
349
//					$email_from = $TEXT['UNKNOWN'];
350
				}
351

    
352
				$success_email_text = htmlspecialchars($wb->add_slashes($fetch_settings['success_email_text']));
353
				$success_email_text = (($success_email_text != '') ? $success_email_text : $MOD_FORM['SUCCESS_EMAIL_TEXT']);
354
				$success_email_subject = (($fetch_settings['success_email_subject'] != '') ? $fetch_settings['success_email_subject'] : $MOD_FORM['SUCCESS_EMAIL_SUBJECT']);
355
				$max_submissions = $fetch_settings['max_submissions'];
356
				$stored_submissions = $fetch_settings['stored_submissions'];
357
				$use_captcha = $fetch_settings['use_captcha'];
358
			} else {
359
				exit($TEXT['UNDER_CONSTRUCTION']);
360
			}
361
		}
362

    
363
		$email_body = '';
364
		// Create blank "required" array
365
		$required = array();
366

    
367
		// Captcha
368
		if($use_captcha) {
369
			if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
370
				// Check for a mismatch get email user_id
371
				if(!isset($_POST['captcha']) OR !isset($_SESSION['captcha']) OR $_POST['captcha'] != $_SESSION['captcha']) {
372
					$replace = array('webmaster_email' => emailAdmin() );
373
					$captcha_error = replace_vars($MOD_FORM['INCORRECT_CAPTCHA'], $replace);
374
					$required[]= '';
375
				}
376
			} else {
377
				$replace = array('webmaster_email'=>emailAdmin() );
378
				$captcha_error = replace_vars($MOD_FORM['INCORRECT_CAPTCHA'],$replace );
379
				$required[]= '';
380
			}
381
		}
382
		if(isset($_SESSION['captcha'])) { unset($_SESSION['captcha']); }
383

    
384
		// Loop through fields and add to message body
385
		// Get list of fields
386
		$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'mod_form_fields` ';
387
		$sql .= 'WHERE `section_id` = '.(int)$section_id.' ';
388
		$sql .= 'ORDER BY position ASC';
389
		if($query_fields = $database->query($sql)) {
390
			if($query_fields->numRows() > 0) {
391
				while($field = $query_fields->fetchRow(MYSQL_ASSOC)) {
392
					// Add to message body
393
					if($field['type'] != '') {
394
						if(!empty($_POST['field'.$field['field_id']]))
395
						{
396
							// do not allow droplets in user input!
397
							if (is_array($_POST['field'.$field['field_id']])) {
398
								$_SESSION['field'.$field['field_id']] = str_replace(array("[[", "]]"), array("&#91;&#91;", "&#93;&#93;"), $wb->strip_slashes($_POST['field'.$field['field_id']]));
399
							} else {
400
								$_SESSION['field'.$field['field_id']] = str_replace(array("[[", "]]"), array("&#91;&#91;", "&#93;&#93;"), htmlspecialchars($wb->strip_slashes($_POST['field'.$field['field_id']])));
401
							}
402

    
403
							if($field['type'] == 'email' AND $wb->validate_email($_POST['field'.$field['field_id']]) == false) {
404
								$email_error = $MESSAGE['USERS_INVALID_EMAIL'];
405
								$required[]= '';
406
							}
407
							if($field['type'] == 'heading') {
408
								$email_body .= $_POST['field'.$field['field_id']]."\n\n";
409
							} elseif (!is_array($_POST['field'.$field['field_id']])) {
410
								$email_body .= $field['title'].': '.$_POST['field'.$field['field_id']]."\n\n";
411
							} else {
412
								$email_body .= $field['title'].": \n";
413
								foreach ($_POST['field'.$field['field_id']] as $k=>$v) {
414
									$email_body .= $v."\n";
415
								}
416
								$email_body .= "\n";
417
							}
418

    
419
						} elseif($field['required'] == 1) {
420
							$required[] = $field['title'];
421
						}
422
					}
423
				} //  while
424
			}  // numRows
425
		} //  query
426

    
427
// Check if the user forgot to enter values into all the required fields
428
		if(sizeof($required )) {
429

    
430
			if(!isset($MESSAGE['MOD_FORM_REQUIRED_FIELDS'])) {
431
				echo '<h3>You must enter details for the following fields</h3>';
432
			} else {
433
				echo '<h3>'.$MESSAGE['MOD_FORM_REQUIRED_FIELDS'].'</h3>';
434
			}
435
			echo "<ol class=\"warning\">\n";
436
			foreach($required AS $field_title) {
437
				if($field_title!=''){
438
					echo '<li>'.$field_title."</li>\n";
439
				}
440
			}
441

    
442
			if(isset($email_error)) {
443
				echo '<li>'.$email_error."</li>\n";
444
			}
445

    
446
			if(isset($captcha_error)) {
447
				echo '<li>'.$captcha_error."</li>\n";
448
			}
449
			// Create blank "required" array
450
			$required = array();
451
			echo "</ol>\n";
452

    
453
			echo '<p>&nbsp;</p>'."\n".'<p><a href="'.htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'">'.$TEXT['BACK'].'</a></p>'."\n";
454
		} else {
455
			if(isset($email_error)) {
456
				echo '<br /><ol class=\"warning\">'."\n";
457
				echo '<li>'.$email_error.'</li>'."\n";
458
				echo '</ol>'."\n";
459
				echo '<a href="'.htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'">'.$TEXT['BACK'].'</a>';
460
			} elseif(isset($captcha_error)) {
461
				echo '<br /><ol class=\"warning\">'."\n";
462
				echo '<li>'.$captcha_error.'</li>'."\n";
463
				echo '</ol>'."\n";
464
				echo '<p>&nbsp;</p>'."\n".'<p><a href="'.htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'">'.$TEXT['BACK'].'</a></p>'."\n";
465
			} else {
466
				// Check how many times form has been submitted in last hour
467
				$last_hour = time()-3600;
468
				$sql  = 'SELECT `submission_id` FROM `'.TABLE_PREFIX.'mod_form_submissions` ';
469
				$sql .= 'WHERE `submitted_when` >= '.$last_hour.'';
470
				$sql .= '';
471
				if($query_submissions = $database->query($sql))
472
				{
473
					if($query_submissions->numRows() > $max_submissions)
474
					{
475
						// Too many submissions so far this hour
476
						echo $MESSAGE['MOD_FORM_EXCESS_SUBMISSIONS'];
477
						$success = false;
478
					} else {
479
						// Adding the IP to the body and try to send the email
480
						// $email_body .= "\n\nIP: ".$_SERVER['REMOTE_ADDR'];
481
						$iFormRequestId = isset($_POST['fri']) ? intval($_POST['fri']) : 0;
482
						if($iFormRequestId) {
483
							$email_body .= "\n\nFormRequestID: ".$iFormRequestId;
484
						}
485
						$recipient = preg_replace( "/[^a-z0-9 !?:;,.\/_\-=+@#$&\*\(\)]/im", "", $email_fromname );
486
						$email_fromname = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "", $recipient );
487
						$email_body = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "", $email_body );
488

    
489
						if($mail_replyto != '') {
490

    
491
							if($email_from != '') {
492
								$success = $wb->mail(SERVER_EMAIL,$email_to,$email_subject,$email_body,$email_fromname,$mail_replyto);
493
							} else {
494
								$success = $wb->mail('',$email_to,$email_subject,$email_body,$email_fromname,$mail_replyto);
495
							}
496
						}
497

    
498
						if($success==true)
499
						{
500
							$recipient = preg_replace( "/[^a-z0-9 !?:;,.\/_\-=+@#$&\*\(\)]/im", "", $success_email_fromname );
501
							$success_email_fromname = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "", $recipient );
502
							$success_email_text = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "", $success_email_text );
503
							if($success_email_to != '')
504
							{
505
								if($success_email_from != '')
506
								{
507
									$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);
508
								} else {
509
									$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);
510
								}
511
							}
512
						}
513

    
514
						if($success==true)
515
						{
516
							$aSuccess[] .= 'INSERT INTO '.TABLE_PREFIX.'mod_form_submissions<br /> ';;
517
							// Write submission to database
518
							if(isset($wb) AND $wb->is_authenticated() AND $wb->get_user_id() > 0) {
519
								$submitted_by = $wb->get_user_id();
520
							} else {
521
								$submitted_by = 0;
522
							}
523
							$email_body = htmlspecialchars($wb->add_slashes($email_body));
524
							$sql  = 'INSERT INTO '.TABLE_PREFIX.'mod_form_submissions ';
525
							$sql .= 'SET ';
526
							$sql .= 'page_id='.$wb->page_id.',';
527
							$sql .= 'section_id='.$section_id.',';
528
							$sql .= 'submitted_when='.time().',';
529
							$sql .= 'submitted_by=\''.$submitted_by.'\', ';
530
							$sql .= 'body=\''.$email_body.'\' ';
531
							if($database->query($sql))
532
							{
533
								// Get the page id
534
								$iSubmissionId = intval($database->get_one("SELECT LAST_INSERT_ID()"));
535

    
536
								if(!$database->is_error()) {
537
									$success = true;
538
								}
539
								// Make sure submissions table isn't too full
540
								$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions ORDER BY submitted_when");
541
								$num_submissions = $query_submissions->numRows();
542
								if($num_submissions > $stored_submissions)
543
								{
544
									// Remove excess submission
545
									$num_to_remove = $num_submissions-$stored_submissions;
546
									while($submission = $query_submissions->fetchRow(MYSQL_ASSOC))
547
									{
548
										if($num_to_remove > 0)
549
										{
550
											$submission_id = $submission['submission_id'];
551
											$database->query("DELETE FROM ".TABLE_PREFIX."mod_form_submissions WHERE submission_id = '$submission_id'");
552
											$num_to_remove = $num_to_remove-1;
553
										}
554
									}
555
								} // $num_submissions
556
							}  // numRows
557
						} // $success
558
		 			}
559
	 			} // end how many times form has been submitted in last hour
560
			}
561
		}  // email_error
562
	} else {
563

    
564
	echo '<p>&nbsp;</p>'.PHP_EOL.'<p><a href="'.htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'">'.$TEXT['BACK'].'</a></p>'.PHP_EOL;
565
	}
566

    
567
	$success_page = ( (isset($success_page) ) ? $success_page : $page_id);
568
	$sql  = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` ';
569
	$sql .= 'WHERE `page_id` = '.(int)$success_page;
570
	$sSuccessLink = WB_URL;  // if failed set default
571
	if( ($link = $database->get_one($sql)) ) {
572
	   $sSuccessLink = WB_URL.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
573
	}
574
	// Now check if the email was sent successfully
575
	if(isset($success) && $success == true)
576
	{
577
	   if ($success_page=='none') {
578

    
579
			// Get submission details
580
			$sql  = 'SELECT * FROM `'.TABLE_PREFIX.'mod_form_submissions` ';
581
			$sql .= 'WHERE submission_id = '.$iSubmissionId.' ';
582
			if($query_content = $database->query($sql)) {
583
				$submission = $query_content->fetchRow(MYSQL_ASSOC);
584
			}
585
			$Message = '';
586
			$NixHier = 'nixhier';
587
			// Get the user details of whoever did this submission
588
			$sql  = 'SELECT `username`,`display_name` FROM `'.TABLE_PREFIX.'users` ';
589
			$sql .= 'WHERE `user_id` = '.$submission['submitted_by'];
590
			if($get_user = $database->query($sql))
591
			{
592
				if($get_user->numRows() != 0) {
593
					$user = $get_user->fetchRow(MYSQL_ASSOC);
594
				} else {
595
					$Message = $MOD_FORM['PRINT'];
596
					$NixHier = '';
597
					$user['display_name'] = $TEXT['GUEST'];
598
					$user['username'] = $TEXT['UNKNOWN'];
599
				}
600
			}
601

    
602
			$aSubSuccess = array();
603
			// set template file and assign module and template block
604
			$oTpl = new Template(WB_PATH.'/modules/form/htt','keep');
605
			// $tpl = new Template(dirname($admin->correct_theme_source('switchform.htt')),'keep');
606
			$oTpl->set_file('page', 'submessage.htt');
607
			$oTpl->debug = false; // false, true
608
			$oTpl->set_block('page', 'main_block', 'main');
609
            $oTpl->set_var(array(
610
            		'ADMIN_URL' => ADMIN_URL,
611
            		'THEME_URL' => THEME_URL,
612
            		'MODULE_URL' => WB_URL.'/modules/form',
613
            		'WB_URL' => WB_URL
614
            	)
615
            );
616
			$oTpl->set_var(array(
617
					'SUCCESS_EMAIL_TEXT' => $success_email_text,
618
					'TEXT_SUBMISSION_ID' => $TEXT['SUBMISSION_ID'],
619
					'submission_submission_id' => $submission['submission_id'],
620
					'TEXT_SUBMITTED' => $TEXT['SUBMITTED'],
621
					'submission_submitted_when' => gmdate( DATE_FORMAT .', '.TIME_FORMAT, $submission['submitted_when']+TIMEZONE ),
622
					'NIX_HIER' => $NixHier,
623
					'TEXT_USER' => $TEXT['USER'],
624
					'TEXT_PRINT_PAGE' => $TEXT['PRINT_PAGE'],
625
					'TEXT_REQUIRED_JS' => $TEXT['REQUIRED_JS'],
626
					'user_display_name' => $user['display_name'],
627
					'user_username' => $user['username'],
628
					'SUCCESS_PRINT' => $Message,
629
					'submission_body' => nl2br($submission['body'])
630
					)
631
				);
632

    
633
			$oTpl->parse('main', 'main_block', false);
634
			$output = $oTpl->finish($oTpl->parse('output', 'page'));
635
			unset($oTpl);
636
			print $output;
637

    
638
  		} else {
639
			echo "<script type='text/javascript'>location.href='".$sSuccessLink."';</script>";
640
		}
641
		// clearing session on success
642
		$query_fields = $database->query("SELECT field_id FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id'");
643
		while($field = $query_fields->fetchRow(MYSQL_ASSOC)) {
644
			$field_id = $field['field_id'];
645
			if(isset($_SESSION['field'.$field_id])) unset($_SESSION['field'.$field_id]);
646
		}
647
	} else {
648
		if(isset($success) && $success == false) {
649
			echo '<br />'.$MOD_FORM['ERROR'];
650
			echo '<p>&nbsp;</p>'.PHP_EOL.'<p><a href="'.htmlspecialchars(strip_tags($_SERVER['SCRIPT_NAME'])).'">'.$TEXT['BACK'].'</a></p>'.PHP_EOL;
651
		}
652
	}
653

    
654
}
(24-24/25)