Project

General

Profile

1
<?php
2

    
3
// $Id: view.php 356 2006-05-20 11:09:53Z ryan $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2006, 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
// Generate temp submission id
66
$submission_id = '';
67
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
68
srand((double)microtime()*1000000);
69
$i = 0;
70
while ($i <= 7) {
71
	$num = rand() % 33;
72
	$tmp = substr($salt, $num, 1);
73
	$submission_id = $submission_id . $tmp;
74
	$i++;
75
}
76

    
77
// Set submission ID in session
78
$_SESSION['form_submission_id'] = $submission_id;
79

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

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

    
141
// Add form starter code
142
?>
143
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
144
<input type="hidden" name="submission_id" value="<?php echo $submission_id; ?>" />
145
<?php
146

    
147
// Print header
148
echo $header;
149

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

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

    
218
// Print footer
219
echo $footer;
220

    
221
// Add form end code
222
?>
223
</form>
224
<?php
225

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

    
384
?>
(16-16/17)