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 |
298
|
stefan
|
|
31 |
|
|
// Must include code to stop this file being access directly
|
32 |
4
|
ryan
|
if(defined('WB_PATH') == false) { exit("Cannot access this file directly"); }
|
33 |
298
|
stefan
|
|
34 |
4
|
ryan
|
// Function for generating an optionsfor a select field
|
35 |
298
|
stefan
|
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 |
4
|
ryan
|
// Function for generating a checkbox
|
47 |
298
|
stefan
|
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 |
4
|
ryan
|
// Function for generating a radio button
|
55 |
298
|
stefan
|
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 |
4
|
ryan
|
}
|
61 |
298
|
stefan
|
|
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 |
4
|
ryan
|
font-size: 11px;
|
99 |
298
|
stefan
|
cursor: pointer;
|
100 |
|
|
}
|
101 |
|
|
.radio_label {
|
102 |
4
|
ryan
|
font-size: 11px;
|
103 |
298
|
stefan
|
cursor: pointer;
|
104 |
4
|
ryan
|
}
|
105 |
298
|
stefan
|
.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,use_captcha FROM ".TABLE_PREFIX."mod_form_settings WHERE section_id = '$section_id'");
|
114 |
|
|
if($query_settings->numRows() > 0) {
|
115 |
|
|
$fetch_settings = $query_settings->fetchRow();
|
116 |
|
|
$header = str_replace('{WB_URL}',WB_URL,$fetch_settings['header']);
|
117 |
|
|
$field_loop = $fetch_settings['field_loop'];
|
118 |
227
|
ryan
|
$footer = str_replace('{WB_URL}',WB_URL,$fetch_settings['footer']);
|
119 |
298
|
stefan
|
$use_captcha = $fetch_settings['use_captcha'];
|
120 |
|
|
} else {
|
121 |
|
|
$header = '';
|
122 |
|
|
$field_loop = '';
|
123 |
|
|
$footer = '';
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
// Add form starter code
|
127 |
|
|
?>
|
128 |
|
|
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
|
129 |
|
|
<?php
|
130 |
|
|
|
131 |
|
|
// Print header
|
132 |
|
|
echo $header;
|
133 |
|
|
|
134 |
|
|
// Get list of fields
|
135 |
|
|
$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
|
136 |
|
|
if($query_fields->numRows() > 0) {
|
137 |
|
|
while($field = $query_fields->fetchRow()) {
|
138 |
|
|
// Set field values
|
139 |
|
|
$field_id = $field['field_id'];
|
140 |
|
|
$value = $field['value'];
|
141 |
|
|
// Print field_loop after replacing vars with values
|
142 |
|
|
$vars = array('{TITLE}', '{REQUIRED}');
|
143 |
|
|
$values = array($field['title']);
|
144 |
|
|
if($field['required'] == 1) {
|
145 |
|
|
$values[] = '<font class="required">*</font>';
|
146 |
|
|
} else {
|
147 |
|
|
$values[] = '';
|
148 |
4
|
ryan
|
}
|
149 |
298
|
stefan
|
if($field['type'] == 'textfield') {
|
150 |
|
|
$vars[] = '{FIELD}';
|
151 |
|
|
$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" maxlength="'.$field['extra'].'" value="'.$value.'" class="textfield" />';
|
152 |
|
|
} elseif($field['type'] == 'textarea') {
|
153 |
|
|
$vars[] = '{FIELD}';
|
154 |
|
|
$values[] = '<textarea name="field'.$field_id.'" id="field'.$field_id.'" class="textarea">'.$value.'</textarea>';
|
155 |
|
|
} elseif($field['type'] == 'select') {
|
156 |
|
|
$vars[] = '{FIELD}';
|
157 |
|
|
$options = explode(',', $value);
|
158 |
|
|
array_walk($options, 'make_option');
|
159 |
|
|
$field['extra'] = explode(',',$field['extra']);
|
160 |
|
|
$values[] = '<select name="field'.$field_id.'[]" id="field'.$field_id.'" size="'.$field['extra'][0].'" '.$field['extra'][1].' class="select">'.implode($options).'</select>';
|
161 |
|
|
} elseif($field['type'] == 'heading') {
|
162 |
|
|
$vars[] = '{FIELD}';
|
163 |
|
|
$values[] = '<input type="hidden" name="field'.$field_id.'" id="field'.$field_id.'" value="===['.$field['title'].']===" />';
|
164 |
|
|
$tmp_field_loop = $field_loop; // temporarily modify the field loop template
|
165 |
|
|
$field_loop = $field['extra'];
|
166 |
|
|
} elseif($field['type'] == 'checkbox') {
|
167 |
|
|
$vars[] = '{FIELD}';
|
168 |
|
|
$options = explode(',', $value);
|
169 |
|
|
array_walk($options, 'make_checkbox',array($field_id,$field['extra']));
|
170 |
|
|
$values[] = implode($options);
|
171 |
|
|
} elseif($field['type'] == 'radio') {
|
172 |
|
|
$vars[] = '{FIELD}';
|
173 |
|
|
$options = explode(',', $value);
|
174 |
|
|
array_walk($options, 'make_radio',array($field_id,$field['title'],$field['extra']));
|
175 |
|
|
$values[] = implode($options);
|
176 |
|
|
} elseif($field['type'] == 'email') {
|
177 |
|
|
$vars[] = '{FIELD}';
|
178 |
|
|
$values[] = '<input type="text" name="field'.$field_id.'" id="field'.$field_id.'" maxlength="'.$field['extra'].'" class="email" />';
|
179 |
|
|
}
|
180 |
|
|
if($field['type'] != '') {
|
181 |
4
|
ryan
|
echo str_replace($vars, $values, $field_loop);
|
182 |
298
|
stefan
|
}
|
183 |
|
|
if (isset($tmp_field_loop)) $field_loop = $tmp_field_loop;
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
227
|
ryan
|
|
187 |
|
|
// Captcha
|
188 |
|
|
if($use_captcha) {
|
189 |
|
|
$_SESSION['captcha'] = '';
|
190 |
|
|
for($i = 0; $i < 5; $i++) {
|
191 |
|
|
$_SESSION['captcha'] .= rand(0,9);
|
192 |
|
|
}
|
193 |
235
|
ryan
|
?><tr><td class="field_title"><?php echo $TEXT['VERIFICATION']; ?>:</td><td>
|
194 |
227
|
ryan
|
<table cellpadding="2" cellspacing="0" border="0">
|
195 |
298
|
stefan
|
<tr><td><img src="<?php echo WB_URL; ?>/include/captcha.php?t=<?php echo time(); ?>" alt="Captcha" /></td>
|
196 |
227
|
ryan
|
<td><input type="text" name="captcha" maxlength="5" /></td>
|
197 |
|
|
</tr></table>
|
198 |
|
|
</td></tr>
|
199 |
|
|
<?php
|
200 |
|
|
}
|
201 |
298
|
stefan
|
|
202 |
|
|
// Print footer
|
203 |
|
|
echo $footer;
|
204 |
|
|
|
205 |
|
|
// Add form end code
|
206 |
|
|
?>
|
207 |
|
|
</form>
|
208 |
|
|
<?php
|
209 |
|
|
|
210 |
|
|
} else {
|
211 |
|
|
|
212 |
|
|
// Submit form data
|
213 |
|
|
// First start message settings
|
214 |
|
|
$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'");
|
215 |
|
|
if($query_settings->numRows() > 0) {
|
216 |
|
|
$fetch_settings = $query_settings->fetchRow();
|
217 |
|
|
$email_to = $fetch_settings['email_to'];
|
218 |
106
|
stefan
|
$email_from = $fetch_settings['email_from'];
|
219 |
4
|
ryan
|
if(substr($email_from, 0, 5) == 'field') {
|
220 |
|
|
// Set the email from field to what the user entered in the specified field
|
221 |
106
|
stefan
|
$email_from = $wb->add_slashes($_POST[$email_from]);
|
222 |
298
|
stefan
|
}
|
223 |
|
|
$email_subject = $fetch_settings['email_subject'];
|
224 |
106
|
stefan
|
$success_message = $fetch_settings['success_message'];
|
225 |
|
|
$max_submissions = $fetch_settings['max_submissions'];
|
226 |
265
|
ryan
|
$stored_submissions = $fetch_settings['stored_submissions'];
|
227 |
298
|
stefan
|
$use_captcha = $fetch_settings['use_captcha'];
|
228 |
|
|
} else {
|
229 |
|
|
exit($TEXT['UNDER_CONSTRUCTION']);
|
230 |
|
|
}
|
231 |
|
|
$email_body = '';
|
232 |
|
|
|
233 |
|
|
// Create blank "required" array
|
234 |
|
|
$required = array();
|
235 |
|
|
|
236 |
|
|
// Loop through fields and add to message body
|
237 |
|
|
// Get list of fields
|
238 |
|
|
$query_fields = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_form_fields WHERE section_id = '$section_id' ORDER BY position ASC");
|
239 |
|
|
if($query_fields->numRows() > 0) {
|
240 |
|
|
while($field = $query_fields->fetchRow()) {
|
241 |
4
|
ryan
|
// Add to message body
|
242 |
298
|
stefan
|
if($field['type'] != '') {
|
243 |
255
|
ryan
|
if(!empty($_POST['field'.$field['field_id']])) {
|
244 |
|
|
if($field['type'] == 'email' AND $admin->validate_email($_POST['field'.$field['field_id']]) == false) {
|
245 |
|
|
$email_error = $MESSAGE['USERS']['INVALID_EMAIL'];
|
246 |
298
|
stefan
|
}
|
247 |
|
|
if($field['type'] == 'heading') {
|
248 |
|
|
$email_body .= $_POST['field'.$field['field_id']]."\n\n";
|
249 |
|
|
} elseif (!is_array($_POST['field'.$field['field_id']])) {
|
250 |
|
|
$email_body .= $field['title'].': '.$_POST['field'.$field['field_id']]."\n\n";
|
251 |
|
|
} else {
|
252 |
|
|
$email_body .= $field['title'].": \n";
|
253 |
|
|
foreach ($_POST['field'.$field['field_id']] as $k=>$v) {
|
254 |
|
|
$email_body .= $v."\n";
|
255 |
254
|
ryan
|
}
|
256 |
298
|
stefan
|
$email_body .= "\n";
|
257 |
4
|
ryan
|
}
|
258 |
298
|
stefan
|
} elseif($field['required'] == 1) {
|
259 |
254
|
ryan
|
$required[] = $field['title'];
|
260 |
298
|
stefan
|
}
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
4
|
ryan
|
|
265 |
153
|
ryan
|
// Captcha
|
266 |
|
|
if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) { /* Make's sure GD library is installed */
|
267 |
265
|
ryan
|
if($use_captcha) {
|
268 |
|
|
if(isset($_POST['captcha']) AND $_POST['captcha'] != ''){
|
269 |
|
|
// Check for a mismatch
|
270 |
|
|
if(!isset($_POST['captcha']) OR !isset($_SESSION['captcha']) OR $_POST['captcha'] != $_SESSION['captcha']) {
|
271 |
|
|
$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
|
272 |
|
|
}
|
273 |
|
|
} else {
|
274 |
215
|
stefan
|
$captcha_error = $MESSAGE['MOD_FORM']['INCORRECT_CAPTCHA'];
|
275 |
153
|
ryan
|
}
|
276 |
|
|
}
|
277 |
|
|
}
|
278 |
281
|
stefan
|
if(isset($_SESSION['captcha'])) { unset($_SESSION['captcha']); }
|
279 |
153
|
ryan
|
|
280 |
40
|
stefan
|
// Addslashes to email body - proposed by Icheb in topic=1170.0
|
281 |
106
|
stefan
|
// $email_body = $wb->add_slashes($email_body);
|
282 |
298
|
stefan
|
|
283 |
|
|
// Check if the user forgot to enter values into all the required fields
|
284 |
|
|
if($required != array()) {
|
285 |
4
|
ryan
|
if(!isset($MESSAGE['MOD_FORM']['REQUIRED_FIELDS'])) {
|
286 |
|
|
echo 'You must enter details for the following fields';
|
287 |
|
|
} else {
|
288 |
|
|
echo $MESSAGE['MOD_FORM']['REQUIRED_FIELDS'];
|
289 |
155
|
ryan
|
}
|
290 |
298
|
stefan
|
echo ':<br /><ul>';
|
291 |
|
|
foreach($required AS $field_title) {
|
292 |
|
|
echo '<li>'.$field_title;
|
293 |
|
|
}
|
294 |
255
|
ryan
|
if(isset($email_error)) { echo '<li>'.$email_error.'</li>'; }
|
295 |
298
|
stefan
|
if(isset($captcha_error)) { echo '<li>'.$captcha_error.'</li>'; }
|
296 |
|
|
echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
|
297 |
4
|
ryan
|
|
298 |
298
|
stefan
|
} else {
|
299 |
|
|
|
300 |
255
|
ryan
|
if(isset($email_error)) {
|
301 |
155
|
ryan
|
echo '<br /><ul>';
|
302 |
255
|
ryan
|
echo '<li>'.$email_error.'</li>';
|
303 |
|
|
echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
|
304 |
|
|
} elseif(isset($captcha_error)) {
|
305 |
|
|
echo '<br /><ul>';
|
306 |
155
|
ryan
|
echo '<li>'.$captcha_error.'</li>';
|
307 |
|
|
echo '</ul><a href="javascript: history.go(-1);">'.$TEXT['BACK'].'</a>';
|
308 |
|
|
} else {
|
309 |
|
|
|
310 |
4
|
ryan
|
// Check how many times form has been submitted in last hour
|
311 |
298
|
stefan
|
$last_hour = time()-3600;
|
312 |
|
|
$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions WHERE submitted_when >= '$last_hour'");
|
313 |
4
|
ryan
|
if($query_submissions->numRows() > $max_submissions) {
|
314 |
|
|
// Too many submissions so far this hour
|
315 |
|
|
echo $MESSAGE['MOD_FORM']['EXCESS_SUBMISSIONS'];
|
316 |
|
|
$success = false;
|
317 |
298
|
stefan
|
} else {
|
318 |
4
|
ryan
|
// Now send the email
|
319 |
298
|
stefan
|
if($email_to != '') {
|
320 |
|
|
if($email_from != '') {
|
321 |
307
|
stefan
|
if($wb->mail($email_from,$email_to,$email_subject,$email_body)) { $success = true; }
|
322 |
4
|
ryan
|
}
|
323 |
|
|
}
|
324 |
|
|
// Write submission to database
|
325 |
|
|
if(isset($admin) AND $admin->get_user_id() > 0) {
|
326 |
|
|
$admin->get_user_id();
|
327 |
|
|
} else {
|
328 |
|
|
$submitted_by = 0;
|
329 |
298
|
stefan
|
}
|
330 |
106
|
stefan
|
$email_body = $wb->add_slashes($email_body);
|
331 |
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')");
|
332 |
|
|
// Make sure submissions table isn't too full
|
333 |
|
|
$query_submissions = $database->query("SELECT submission_id FROM ".TABLE_PREFIX."mod_form_submissions ORDER BY submitted_when");
|
334 |
|
|
$num_submissions = $query_submissions->numRows();
|
335 |
|
|
if($num_submissions > $stored_submissions) {
|
336 |
|
|
// Remove excess submission
|
337 |
|
|
$num_to_remove = $num_submissions-$stored_submissions;
|
338 |
|
|
while($submission = $query_submissions->fetchRow()) {
|
339 |
|
|
if($num_to_remove > 0) {
|
340 |
|
|
$submission_id = $submission['submission_id'];
|
341 |
|
|
$database->query("DELETE FROM ".TABLE_PREFIX."mod_form_submissions WHERE submission_id = '$submission_id'");
|
342 |
|
|
$num_to_remove = $num_to_remove-1;
|
343 |
|
|
}
|
344 |
|
|
}
|
345 |
|
|
}
|
346 |
|
|
if(!$database->is_error()) {
|
347 |
|
|
$success = true;
|
348 |
|
|
}
|
349 |
|
|
}
|
350 |
298
|
stefan
|
|
351 |
|
|
// Now check if the email was sent successfully
|
352 |
|
|
if(isset($success) AND $success == true) {
|
353 |
|
|
echo $success_message;
|
354 |
|
|
} else {
|
355 |
|
|
echo $TEXT['ERROR'];
|
356 |
155
|
ryan
|
}
|
357 |
298
|
stefan
|
|
358 |
|
|
}
|
359 |
|
|
}
|
360 |
|
|
|
361 |
4
|
ryan
|
}
|
362 |
298
|
stefan
|
|
363 |
307
|
stefan
|
?>
|