Project

General

Profile

1
<?php
2

    
3
// $Id: view.php 519 2007-12-23 14:37:02Z Ruebenwurzel $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25

    
26
/*
27
The Website Baker Project would like to thank Rudolph Lartey <www.carbonect.com>
28
for his contributions to this module - adding extra field types
29
*/
30

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

    
34
// Function for generating an optionsfor a select field
35
function make_option(&$n) {
36
	// start option group if it exists
37
	if (substr($n,0,2) == '[=') {
38
	 	$n = '<optgroup label="'.substr($n,2,strlen($n)).'">';
39
	} elseif ($n == ']') {
40
		$n = '</optgroup>';
41
	} else {
42
		$n = '<option value="'.$n.'">'.$n.'</option>';
43
	}
44
}
45

    
46
// Function for generating a checkbox
47
function make_checkbox(&$n, $idx, $params) {
48
	$field_id = $params[0];
49
	$seperator = $params[1];
50
	//$n = '<input class="field_checkbox" type="checkbox" id="'.$n.'" name="field'.$field_id.'" value="'.$n.'">'.'<font class="checkbox_label" onclick="javascript: document.getElementById(\''.$n.'\').checked = !document.getElementById(\''.$n.'\').checked;">'.$n.'</font>'.$seperator;
51
	$n = '<input class="field_checkbox" type="checkbox" id="'.$n.'" name="field'.$field_id.'['.$idx.']" value="'.$n.'">'.'<font class="checkbox_label" onclick="javascript: document.getElementById(\''.$n.'\').checked = !document.getElementById(\''.$n.'\').checked;">'.$n.'</font>'.$seperator;
52
}
53

    
54
// Function for generating a radio button
55
function make_radio(&$n, $idx, $params) {
56
	$field_id = $params[0];
57
	$group = $params[1];
58
	$seperator = $params[2];
59
	$n = '<input class="field_radio" type="radio" id="'.$n.'" name="field'.$field_id.'" value="'.$n.'">'.'<font class="radio_label" onclick="javascript: document.getElementById(\''.$n.'\').checked = true;">'.$n.'</font>'.$seperator;
60
}
61

    
62
// Generate temp submission id
63
function new_submission_id() {
64
	$submission_id = '';
65
	$salt = "abchefghjkmnpqrstuvwxyz0123456789";
66
	srand((double)microtime()*1000000);
67
	$i = 0;
68
	while ($i <= 7) {
69
		$num = rand() % 33;
70
		$tmp = substr($salt, $num, 1);
71
		$submission_id = $submission_id . $tmp;
72
		$i++;
73
	}
74
	return $submission_id;
75
}
76

    
77
// Work-out if the form has been submitted or not
78
if($_POST == array()) {
79

    
80
// Set new submission ID in session
81
$_SESSION['form_submission_id'] = new_submission_id();
82

    
83
?>
84
<style type="text/css">
85
.required {
86
	color: #FF0000;
87
}
88
.field_title {
89
	font-size: 12px;
90
	width: 100px;
91
	vertical-align: top;
92
	text-align:right;
93
}
94
.textfield {
95
	font-size: 12px;
96
	width: 200px;
97
}
98
.textarea {
99
	font-size: 12px;
100
	width: 90%;
101
	height: 100px;
102
}
103
.field_heading {
104
	font-size: 12px;
105
	font-weight: bold;
106
	border-bottom-width: 2px;
107
	border-bottom-style: solid;
108
	border-bottom-color: #666666;
109
	padding-top: 10px;
110
	color: #666666;
111
}
112
.select {
113
	font-size: 12px;
114
}
115
.checkbox_label {
116
	font-size: 11px;
117
	cursor: pointer;
118
}
119
.radio_label {
120
	font-size: 11px;
121
	cursor: pointer;
122
}
123
.email {
124
	font-size: 12px;
125
	width: 200px;
126
}
127
</style>
128
<?php
129

    
130
// Get settings
131
$query_settings = $database->query("SELECT header,field_loop,footer,use_captcha FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
132
if($query_settings->numRows() > 0) {
133
	$fetch_settings = $query_settings->fetchRow();
134
	$header = str_replace('{WB_URL}',WB_URL,$fetch_settings['header']);
135
	$field_loop = $fetch_settings['field_loop'];
136
	$footer = str_replace('{WB_URL}',WB_URL,$fetch_settings['footer']);
137
	$use_captcha = $fetch_settings['use_captcha'];
138
} else {
139
	$header = '';
140
	$field_loop = '';
141
	$footer = '';
142
}
143

    
144
// Add form starter code
145
?>
146
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
147
<input type="hidden" name="submission_id" value="<?php echo $_SESSION['form_submission_id']; ?>" />
148
<?php
149

    
150
// Print header
151
echo $header;
152

    
153
// Get list of fields
154
$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
155
if($query_fields->numRows() > 0) {
156
	while($field = $query_fields->fetchRow()) {
157
		// Set field values
158
		$field_id = $field['field_id'];
159
		$value = $field['value'];
160
		// Print field_loop after replacing vars with values
161
		$vars = array('{TITLE}', '{REQUIRED}');
162
		$values = array($field['title']);
163
		if($field['required'] == 1) {
164
			$values[] = '<font class="required">*</font>';
165
		} else {
166
			$values[] = '';
167
		}
168
		if($field['type'] == 'textfield') {
169
			$vars[] = '{FIELD}';
170
			$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" maxlength="'.$field['extra'].'" value="'.$value.'" class="textfield" />';
171
		} elseif($field['type'] == 'textarea') {
172
			$vars[] = '{FIELD}';
173
			$values[] = '<textarea name="field'.$field_id.'" id="field'.$field_id.'" class="textarea">'.$value.'</textarea>';
174
		} elseif($field['type'] == 'select') {
175
			$vars[] = '{FIELD}';
176
			$options = explode(',', $value);
177
			array_walk($options, 'make_option');
178
			$field['extra'] = explode(',',$field['extra']); 
179
			$values[] = '<select name="field'.$field_id.'[]" id="field'.$field_id.'" size="'.$field['extra'][0].'" '.$field['extra'][1].' class="select">'.implode($options).'</select>';
180
		} elseif($field['type'] == 'heading') {
181
			$vars[] = '{FIELD}';
182
			$values[] = '<input type="hidden" name="field'.$field_id.'" id="field'.$field_id.'" value="===['.$field['title'].']===" />';
183
			$tmp_field_loop = $field_loop;		// temporarily modify the field loop template
184
			$field_loop = $field['extra'];
185
		} elseif($field['type'] == 'checkbox') {
186
			$vars[] = '{FIELD}';
187
			$options = explode(',', $value);
188
			array_walk($options, 'make_checkbox',array($field_id,$field['extra']));
189
			$values[] = implode($options);
190
		} elseif($field['type'] == 'radio') {
191
			$vars[] = '{FIELD}';
192
			$options = explode(',', $value);
193
			array_walk($options, 'make_radio',array($field_id,$field['title'],$field['extra']));
194
			$values[] = implode($options);
195
		} elseif($field['type'] == 'email') {
196
			$vars[] = '{FIELD}';
197
			$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" maxlength="'.$field['extra'].'" class="email" />';
198
		}
199
		if($field['type'] != '') {
200
			echo str_replace($vars, $values, $field_loop);
201
		}
202
		if (isset($tmp_field_loop)) $field_loop = $tmp_field_loop;
203
	}
204
}
205

    
206
// Captcha
207
if($use_captcha) {
208
	$_SESSION['captcha'] = '';
209
	for($i = 0; $i < 5; $i++) {
210
		$_SESSION['captcha'] .= rand(0,9);
211
	}
212
	?><tr><td class="field_title"><?php echo $TEXT['VERIFICATION']; ?>:</td><td>
213
	<table cellpadding="2" cellspacing="0" border="0">
214
	<tr><td><img src="<?php echo WB_URL; ?>/include/captcha.php?t=<?php echo time(); ?>" alt="Captcha" /></td>
215
	<td><input type="text" name="captcha" maxlength="5" /></td>
216
	</tr></table>
217
	</td></tr>
218
	<?php
219
}
220

    
221
// Print footer
222
echo $footer;
223

    
224
// Add form end code
225
?>
226
</form>
227
<?php
228

    
229
} else {
230
	
231
	// Check that submission ID matches
232
	if(isset($_SESSION['form_submission_id']) AND isset($_POST['submission_id']) AND $_SESSION['form_submission_id'] == $_POST['submission_id']) {
233
		
234
		// Set new submission ID in session
235
		$_SESSION['form_submission_id'] = new_submission_id();
236
		
237
		// Submit form data
238
		// First start message settings
239
		$query_settings = $database->query("SELECT email_to,email_from,email_subject,success_message,max_submissions,stored_submissions,use_captcha FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
240
		if($query_settings->numRows() > 0) {
241
			$fetch_settings = $query_settings->fetchRow();
242
			$email_to = $fetch_settings['email_to'];
243
			$email_from = $fetch_settings['email_from'];
244
			if(substr($email_from, 0, 5) == 'field') {
245
				// Set the email from field to what the user entered in the specified field
246
				$email_from = $wb->add_slashes($_POST[$email_from]);
247
			}
248
			$email_subject = $fetch_settings['email_subject'];
249
			$success_message = $fetch_settings['success_message'];
250
			$max_submissions = $fetch_settings['max_submissions'];
251
			$stored_submissions = $fetch_settings['stored_submissions'];
252
			$use_captcha = $fetch_settings['use_captcha'];
253
		} else {
254
			exit($TEXT['UNDER_CONSTRUCTION']);
255
		}
256
		$email_body = '';
257
		
258
		// Create blank "required" array
259
		$required = array();
260
		
261
		// Loop through fields and add to message body
262
		// Get list of fields
263
		$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
264
		if($query_fields->numRows() > 0) {
265
			while($field = $query_fields->fetchRow()) {
266
				// Add to message body
267
				if($field['type'] != '') {
268
					if(!empty($_POST['field'.$field['field_id']])) {
269
						if($field['type'] == 'email' AND $admin->validate_email($_POST['field'.$field['field_id']]) == false) {
270
							$email_error = $MESSAGE['USERS']['INVALID_EMAIL'];
271
						}
272
						if($field['type'] == 'heading') {
273
							$email_body .= $_POST['field'.$field['field_id']]."\n\n";
274
						} elseif (!is_array($_POST['field'.$field['field_id']])) {
275
							$email_body .= $field['title'].': '.$_POST['field'.$field['field_id']]."\n\n";
276
						} else {
277
							$email_body .= $field['title'].": \n";
278
							foreach ($_POST['field'.$field['field_id']] as $k=>$v) {
279
								$email_body .= $v."\n";
280
							}
281
							$email_body .= "\n";
282
						}
283
					} elseif($field['required'] == 1) {
284
						$required[] = $field['title'];
285
					}
286
				}
287
			}
288
		}
289
		
290
		// Captcha
291
		if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) { /* Make's sure GD library is installed */
292
			if($use_captcha) {
293
				if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
294
					// Check for a mismatch
295
					if(!isset($_POST['captcha']) OR !isset($_SESSION['captcha']) OR $_POST['captcha'] != $_SESSION['captcha']) {
296
						$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
297
					}
298
				} else {
299
					$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
300
				}
301
			}
302
		}
303
		if(isset($_SESSION['captcha'])) { unset($_SESSION['captcha']); }
304
		
305
		// Addslashes to email body - proposed by Icheb in topic=1170.0
306
		// $email_body = $wb->add_slashes($email_body);
307
		
308
		// Check if the user forgot to enter values into all the required fields
309
		if($required != array()) {
310
			if(!isset($MESSAGE['MOD_FORM']['REQUIRED_FIELDS'])) {
311
				echo 'You must enter details for the following fields';
312
			} else {
313
				echo $MESSAGE['MOD_FORM']['REQUIRED_FIELDS'];
314
			}
315
			echo ':<br /><ul>';
316
			foreach($required AS $field_title) {
317
				echo '<li>'.$field_title;
318
			}
319
			if(isset($email_error)) { echo '<li>'.$email_error.'</li>'; }
320
			if(isset($captcha_error)) { echo '<li>'.$captcha_error.'</li>'; }
321
			echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
322
			
323
		} else {
324
			
325
			if(isset($email_error)) {
326
				echo '<br /><ul>';
327
				echo '<li>'.$email_error.'</li>';
328
				echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
329
			} elseif(isset($captcha_error)) {
330
				echo '<br /><ul>';
331
				echo '<li>'.$captcha_error.'</li>';
332
				echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
333
			} else {
334
				
335
				// Check how many times form has been submitted in last hour
336
				$last_hour = time()-3600;
337
				$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions WHERE submitted_when >= '$last_hour'");
338
				if($query_submissions->numRows() > $max_submissions) {
339
					// Too many submissions so far this hour
340
					echo $MESSAGE['MOD_FORM']['EXCESS_SUBMISSIONS'];
341
					$success = false;
342
				} else {
343
					// Now send the email
344
					if($email_to != '') {
345
						if($email_from != '') {
346
							if($wb->mail($email_from,$email_to,$email_subject,$email_body)) { $success = true; }
347
						}
348
					}				
349
					// Write submission to database
350
					if(isset($admin) AND $admin->get_user_id() > 0) {
351
						$admin->get_user_id();
352
					} else {
353
						$submitted_by = 0;
354
					}
355
					$email_body = $wb->add_slashes($email_body);
356
					$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')");
357
					// Make sure submissions table isn't too full
358
					$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions ORDER BY submitted_when");
359
					$num_submissions = $query_submissions->numRows();
360
					if($num_submissions > $stored_submissions) {
361
						// Remove excess submission
362
						$num_to_remove = $num_submissions-$stored_submissions;
363
						while($submission = $query_submissions->fetchRow()) {
364
							if($num_to_remove > 0) {
365
								$submission_id = $submission['submission_id'];
366
								$database->query("DELETE FROM ".TABLE_PREFIX."mod_form_submissions WHERE submission_id = '$submission_id'");
367
								$num_to_remove = $num_to_remove-1;
368
							}
369
						}
370
					}
371
					if(!$database->is_error()) {
372
						$success = true;
373
					}
374
				}
375
			}	
376
		}
377
	}
378
	
379
	// Now check if the email was sent successfully
380
	if(isset($success) AND $success == true) {
381
		echo $success_message;
382
	} else {
383
		if(isset($success) AND $success == false) {
384
			echo $TEXT['ERROR'];
385
		}
386
	}
387
	
388
}
389

    
390
?>
(16-16/17)