Project

General

Profile

1 4 ryan
<?php
2
3 36 stefan
// $Id$
4 4 ryan
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2005, 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
// Work-out if the form has been submitted or not
63
if($_POST == array()) {
64
65
?>
66
<style type="text/css">
67
.required {
68
	color: #FF0000;
69
}
70
.field_title {
71
	font-size: 12px;
72
	width: 100px;
73
	vertical-align: top;
74
	text-align:right;
75
}
76
.textfield {
77
	font-size: 12px;
78
	width: 200px;
79
}
80
.textarea {
81
	font-size: 12px;
82
	width: 90%;
83
	height: 100px;
84
}
85
.field_heading {
86
	font-size: 12px;
87
	font-weight: bold;
88
	border-bottom-width: 2px;
89
	border-bottom-style: solid;
90
	border-bottom-color: #666666;
91
	padding-top: 10px;
92
	color: #666666;
93
}
94
.select {
95
	font-size: 12px;
96
}
97
.checkbox_label {
98
	font-size: 11px;
99
	cursor: pointer;
100
}
101
.radio_label {
102
	font-size: 11px;
103
	cursor: pointer;
104
}
105
.email {
106
	font-size: 12px;
107
	width: 200px;
108
}
109
</style>
110
<?php
111
112
// Get settings
113
$query_settings = $database->query("SELECT header,field_loop,footer FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
114
if($query_settings->numRows() > 0) {
115
	$fetch_settings = $query_settings->fetchRow();
116 153 ryan
	$header = str_replace('{WB_URL}',WB_URL,$fetch_settings['header']);
117 106 stefan
	$field_loop = $fetch_settings['field_loop'];
118 153 ryan
	$footer = str_replace('{WB_URL}',WB_URL,$fetch_settings['footer']);
119 4 ryan
} else {
120
	$header = '';
121
	$field_loop = '';
122
	$footer = '';
123
}
124
125
// Add form starter code
126
?>
127
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
128
<?php
129
130
// Print header
131
echo $header;
132
133
// Get list of fields
134
$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
135
if($query_fields->numRows() > 0) {
136
	while($field = $query_fields->fetchRow()) {
137
		// Set field values
138
		$field_id = $field['field_id'];
139 106 stefan
		$value = $field['value'];
140 4 ryan
		// Print field_loop after replacing vars with values
141
		$vars = array('{TITLE}', '{REQUIRED}');
142
		$values = array($field['title']);
143
		if($field['required'] == 1) {
144
			$values[] = '<font class="required">*</font>';
145
		} else {
146
			$values[] = '';
147
		}
148
		if($field['type'] == 'textfield') {
149
			$vars[] = '{FIELD}';
150
			$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" maxlength="'.$field['extra'].'" value="'.$value.'" class="textfield" />';
151
		} elseif($field['type'] == 'textarea') {
152
			$vars[] = '{FIELD}';
153
			$values[] = '<textarea name="field'.$field_id.'" id="field'.$field_id.'" class="textarea">'.$value.'</textarea>';
154
		} elseif($field['type'] == 'select') {
155
			$vars[] = '{FIELD}';
156
			$options = explode(',', $value);
157
			array_walk($options, 'make_option');
158
			$field['extra'] = explode(',',$field['extra']);
159
			$values[] = '<select name="field'.$field_id.'[]" id="field'.$field_id.'" size="'.$field['extra'][0].'" '.$field['extra'][1].' class="select">'.implode($options).'</select>';
160
		} elseif($field['type'] == 'heading') {
161
			$vars[] = '{FIELD}';
162
			$values[] = '<input type="hidden" name="field'.$field_id.'" id="field'.$field_id.'" value="===['.$field['title'].']===" />';
163
			$tmp_field_loop = $field_loop;		// temporarily modify the field loop template
164
			$field_loop = $field['extra'];
165
		} elseif($field['type'] == 'checkbox') {
166
			$vars[] = '{FIELD}';
167
			$options = explode(',', $value);
168
			array_walk($options, 'make_checkbox',array($field_id,$field['extra']));
169
			$values[] = implode($options);
170
		} elseif($field['type'] == 'radio') {
171
			$vars[] = '{FIELD}';
172
			$options = explode(',', $value);
173
			array_walk($options, 'make_radio',array($field_id,$field['title'],$field['extra']));
174
			$values[] = implode($options);
175
		} elseif($field['type'] == 'email') {
176
			$vars[] = '{FIELD}';
177
			$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" maxlength="'.$field['extra'].'" class="email" />';
178
		}
179
		if($field['type'] != '') {
180
			echo str_replace($vars, $values, $field_loop);
181
		}
182
		if (isset($tmp_field_loop)) $field_loop = $tmp_field_loop;
183
	}
184
}
185
186
// Print footer
187
echo $footer;
188
189
// Add form end code
190
?>
191
</form>
192
<?php
193
194
} else {
195
196
	// Submit form data
197
	// First start message settings
198
	$query_settings = $database->query("SELECT email_to,email_from,email_subject,success_message,max_submissions,stored_submissions FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
199
	if($query_settings->numRows() > 0) {
200
		$fetch_settings = $query_settings->fetchRow();
201 106 stefan
		$email_to = $fetch_settings['email_to'];
202
		$email_from = $fetch_settings['email_from'];
203 4 ryan
		if(substr($email_from, 0, 5) == 'field') {
204
			// Set the email from field to what the user entered in the specified field
205 106 stefan
			$email_from = $wb->add_slashes($_POST[$email_from]);
206 4 ryan
		}
207 106 stefan
		$email_subject = $fetch_settings['email_subject'];
208
		$success_message = $fetch_settings['success_message'];
209
		$max_submissions = $fetch_settings['max_submissions'];
210
		$stored_submissions = $fetch_settings['stored_submissions'];
211 4 ryan
	} else {
212
		exit($TEXT['UNDER_CONSTRUCTION']);
213
	}
214
	$email_body = '';
215
216
	// Create blank "required" array
217
	$required = array();
218
219
	// Loop through fields and add to message body
220
	// Get list of fields
221
	$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
222
	if($query_fields->numRows() > 0) {
223
		while($field = $query_fields->fetchRow()) {
224
			// Add to message body
225
			if($field['type'] != '') {
226
				if(!empty($_POST['field'.$field['field_id']])) {
227
					if($field['type'] == 'heading') {
228
					$email_body .= '
229
230
	'.$_POST['field'.$field['field_id']];
231
					} elseif (!is_array($_POST['field'.$field['field_id']])) {
232
					$email_body .= '
233
234 106 stefan
	'.$field['title'].': '.$_POST['field'.$field['field_id']]."\n";
235 4 ryan
					} else {
236
						$email_body .= '
237
238 106 stefan
	'.$field['title'].": \n";
239 4 ryan
						foreach ($_POST['field'.$field['field_id']] as $k=>$v) {
240
							$email_body .= '
241
242
	'.$v;
243
						}
244
					}
245
				} elseif($field['required'] == 1) {
246 106 stefan
				$required[] = $field['title'];
247 4 ryan
				}
248
			}
249
		}
250
	}
251
252 153 ryan
	// Captcha
253
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) { /* Make's sure GD library is installed */
254 215 stefan
		if(isset($_POST['captcha']) AND $_POST['CAPTCHA']!=''){
255 153 ryan
			// User-supplied captcha
256
			$user_captcha = $_POST['captcha'];
257
			// Computer generated
258
			if(isset($_SESSION['captcha'])) {
259
				$system_captcha = $_SESSION['captcha'];
260
			}
261
			// Check for a mismatch
262
			if($user_captcha != $system_captcha) {
263 215 stefan
				$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
264 153 ryan
			} else {
265
				unset($_SESSION['captcha']);
266
			}
267 215 stefan
		} else {
268
			$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
269 153 ryan
		}
270
	}
271
272 40 stefan
	// Addslashes to email body - proposed by Icheb in topic=1170.0
273 106 stefan
	// $email_body = $wb->add_slashes($email_body);
274 4 ryan
275
	// Check if the user forgot to enter values into all the required fields
276
	if($required != array()) {
277
		if(!isset($MESSAGE['MOD_FORM']['REQUIRED_FIELDS'])) {
278
			echo 'You must enter details for the following fields';
279
		} else {
280
			echo $MESSAGE['MOD_FORM']['REQUIRED_FIELDS'];
281
		}
282
		echo ':<br /><ul>';
283
		foreach($required AS $field_title) {
284
			echo '<li>'.$field_title;
285 155 ryan
		}
286
		if(isset($captcha_error)) { echo '<li>'.$captcha_error.'</li>'; }
287 4 ryan
		echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
288
289
	} else {
290
291 155 ryan
		if(isset($captcha_error)) {
292
			echo '<br /><ul>';
293
			echo '<li>'.$captcha_error.'</li>';
294
			echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
295
		} else {
296
297 4 ryan
		// Check how many times form has been submitted in last hour
298
		$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions WHERE submitted_when >= '3600'");
299
		if($query_submissions->numRows() > $max_submissions) {
300
			// Too many submissions so far this hour
301
			echo $MESSAGE['MOD_FORM']['EXCESS_SUBMISSIONS'];
302
			$success = false;
303
		} else {
304
			// Now send the email
305
			if($email_to != '') {
306
				if($email_from != '') {
307
					if(mail($email_to,$email_subject,str_replace('\n', '', $email_body),"From: ".$email_from)) { $success = true; }
308
				} else {
309
					if(mail($email_to,$email_subject,str_replace('\n', '', $email_body))) { $success = true; }
310
				}
311
			}
312
			// Write submission to database
313
			if(isset($admin) AND $admin->get_user_id() > 0) {
314
				$admin->get_user_id();
315
			} else {
316
				$submitted_by = 0;
317
			}
318 106 stefan
			$email_body = $wb->add_slashes($email_body);
319 4 ryan
			$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')");
320
			// Make sure submissions table isn't too full
321
			$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions ORDER BY submitted_when");
322
			$num_submissions = $query_submissions->numRows();
323
			if($num_submissions > $stored_submissions) {
324
				// Remove excess submission
325
				$num_to_remove = $num_submissions-$stored_submissions;
326
				while($submission = $query_submissions->fetchRow()) {
327
					if($num_to_remove > 0) {
328
						$submission_id = $submission['submission_id'];
329
						$database->query("DELETE FROM ".TABLE_PREFIX."mod_form_submissions WHERE submission_id = '$submission_id'");
330
						$num_to_remove = $num_to_remove-1;
331
					}
332
				}
333
			}
334
			if(!$database->is_error()) {
335
				$success = true;
336
			}
337
		}
338
339
		// Now check if the email was sent successfully
340
		if(isset($success) AND $success == true) {
341
			echo $success_message;
342
		} else {
343
			echo $TEXT['ERROR'];
344 155 ryan
		}
345
346 4 ryan
		}
347
	}
348
349
}
350
351
?>