/
database.php
392 lines (371 loc) · 15 KB
/
database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/*
#############################################################
# >>> PHP Surveyor #
#############################################################
# > Author: Jason Cleeland #
# > E-mail: jason@cleeland.org #
# > Mail: Box 99, Trades Hall, 54 Victoria St, #
# > CARLTON SOUTH 3053, AUSTRALIA
# > Date: 20 February 2003 #
# #
# This set of scripts allows you to develop, publish and #
# perform data-entry on surveys. #
#############################################################
# #
# Copyright (C) 2003 Jason Cleeland #
# #
# This program is free software; you can redistribute #
# it and/or modify it under the terms of the GNU General #
# Public License as published by the Free Software #
# Foundation; either version 2 of the License, or (at your #
# option) any later version. #
# #
# This program is distributed in the hope that it will be #
# useful, but WITHOUT ANY WARRANTY; without even the #
# implied warranty of MERCHANTABILITY or FITNESS FOR A #
# PARTICULAR PURPOSE. See the GNU General Public License #
# for more details. #
# #
# You should have received a copy of the GNU General #
# Public License along with this program; if not, write to #
# the Free Software Foundation, Inc., 59 Temple Place - #
# Suite 330, Boston, MA 02111-1307, USA. #
#############################################################
*/
if ($_POST['action']) {$action = $_POST['action'];}
if ($_GET['action']) {$action = $_GET['action'];}
if ($action == "insertnewgroup")
{
if (!$_POST['group_name'])
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your group could not be added! It did not include a group name (which is required)\")\n //-->\n</script>\n";
}
else
{
if (get_magic_quotes_gpc() == "0")
{
$_POST['description'] = addcslashes($_POST['description'], "'");
$_POST['group_name'] = addcslashes($_POST['group_name'], "'");
}
$query = "INSERT INTO groups (sid, group_name, description) VALUES ('{$_POST['sid']}', '{$_POST['group_name']}', '{$_POST['description']}')";
$result = mysql_query($query);
if ($result)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"New group ($group_name) has been created for survey id $sid\")\n //-->\n</script>\n";
$query = "SELECT gid FROM groups WHERE group_name='$group_name' AND sid={$_POST['sid']}";
$result = mysql_query($query);
while ($res = mysql_fetch_array($result)) {$gid = $res['gid'];}
$groupselect = getgrouplist($gid);
}
else
{
echo "The database reported the following error:<br />\n";
echo "<font color='red'>" . mysql_error() . "</font>\n";
echo "<pre>$query</pre>\n";
echo "</body>\n</html>";
exit;
}
}
}
elseif ($action == "updategroup")
{
if (get_magic_quotes_gpc() == "0")
{
$_POST['description'] = addcslashes($_POST['description'], "'");
$_POST['group_name'] = addcslashes($_POST['group_name'], "'");
}
$ugquery = "UPDATE groups SET group_name='{$_POST['group_name']}', description='{$_POST['description']}' WHERE sid={$_POST['sid']} AND gid={$_POST['gid']}";
$ugresult = mysql_query($ugquery);
if ($ugresult)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your Group ($group_name) has been updated!\")\n //-->\n</script>\n";
$groupsummary = getgrouplist($_POST['gid']);
}
else
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your group could not be updated!\")\n //-->\n</script>\n";
}
}
elseif ($action == "delgroup")
{
$query = "DELETE FROM groups WHERE sid=$sid AND gid=$gid";
$result = mysql_query($query);
if ($result)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"Group id($gid) for survey $sid has been deleted!\")\n //-->\n</script>\n";
$gid = "";
$groupselect = getgrouplist($gid);
}
else
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Group id($gid) for survey $sid was NOT DELETED!\n$error\")\n //-->\n</script>\n";
}
}
elseif ($action == "insertnewquestion")
{
if (get_magic_quotes_gpc() == "0")
{
$_POST['title'] = addcslashes($_POST['title'], "'");
$_POST['question'] = addcslashes($_POST['question'], "'");
$_POST['help'] = addcslashes($_POST['help'], "'");
}
$query = "INSERT INTO questions (qid, sid, gid, type, title, question, help, other, mandatory)"
." VALUES ('', '{$_POST['sid']}', '{$_POST['gid']}', '{$_POST['type']}', '{$_POST['title']}',"
." '{$_POST['question']}', '{$_POST['help']}', '{$_POST['other']}', '{$_POST['mandatory']}')";
$result = mysql_query($query);
if ($result)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"New question ($title) has been created for survey id $sid, group id $gid\")\n //-->\n</script>\n";
}
}
elseif ($action == "updatequestion")
{
$cqquery = "SELECT type FROM questions WHERE qid={$_POST['qid']}";
$cqresult=mysql_query($cqquery) or die ("Couldn't get question type to check for change<br />$cqquery<br />".mysql_error());
while ($cqr=mysql_fetch_array($cqresult)) {$oldtype=$cqr['type'];}
if ($oldtype != $_POST['type'])
{
//Make sure there are no conditions based on this question, since we are changing the type
$ccquery = "SELECT * FROM conditions WHERE cqid={$_POST['qid']}";
$ccresult = mysql_query($ccquery) or die ("Couldn't get list of cqids for this question<br />$ccquery<br />".mysql_error());
$cccount=mysql_num_rows($ccresult);
while ($ccr=mysql_fetch_array($ccresult)) {$qidarray[]=$ccr['qid'];}
if ($qidarray) {$qidlist=implode(", ", $qidarray);}
}
if (get_magic_quotes_gpc() == "0")
{
$_POST['title'] = addcslashes($_POST['title'], "'");
$_POST['question'] = addcslashes($_POST['question'], "'");
$_POST['help'] = addcslashes($_POST['help'], "'");
}
if ($cccount)
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your question could not be updated! Other questions (qid $qidlist) are conditional based on the responses to this question and changing the type will cause problems. You must remove any conditions based on this question before changing this question type.\")\n //-->\n</script>\n";
}
else
{
$uqquery = "UPDATE questions SET type='{$_POST['type']}', title='{$_POST['title']}', "
. "question='{$_POST['question']}', help='{$_POST['help']}', gid='{$_POST['gid']}', "
. "other='{$_POST['other']}', mandatory='{$_POST['mandatory']}' "
. "WHERE sid={$_POST['sid']} AND qid={$_POST['qid']}";
//echo $uqquery;
$uqresult = mysql_query($uqquery);
if ($uqresult)
{
}
else
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your question could not be updated!\")\n //-->\n</script>\n";
}
}
}
elseif ($action == "copynewquestion")
{
if (get_magic_quotes_gpc() == "0")
{
$_POST['title'] = addcslashes($_POST['title'], "'");
$_POST['question'] = addcslashes($_POST['question'], "'");
$_POST['help'] = addcslashes($_POST['help'], "'");
}
$query = "INSERT INTO questions (qid, sid, gid, type, title, question, help, other, mandatory) VALUES ('', '{$_POST['sid']}', '{$_POST['gid']}', '{$_POST['type']}', '{$_POST['title']}', '{$_POST['question']}', '{$_POST['help']}', '{$_POST['other']}', '{$_POST['mandatory']}')";
$result = mysql_query($query);
if ($result)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"New question ($title) has been created for survey id $sid, group id $gid\")\n //-->\n</script>\n";
}
//echo "COPYANSWERS: $copyanswers, OLDQID: $oldqid";
if ($copyanswers == "Y")
{
$q2 = "SELECT qid FROM questions ORDER BY qid DESC LIMIT 1";
$r2 = mysql_query($q2);
while ($qr2 = mysql_fetch_array($r2)) {$newqid = $qr2['qid'];}
$q1 = "SELECT * FROM answers WHERE qid='$oldqid' ORDER BY code";
$r1 = mysql_query($q1);
while ($qr1 = mysql_fetch_array($r1))
{
$i1 = "INSERT INTO answers (qid, code, answer, `default`) VALUES ('$newqid', '{$qr1['code']}', '{$qr1['answer']}', '{$qr1['default']}')";
$ir1 = mysql_query($i1);
}
}
}
elseif ($action == "delquestion")
{
$query = "DELETE FROM questions WHERE qid=$qid";
$result = mysql_query($query);
if ($result)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"Question id($qid) has been deleted!\")\n //-->\n</script>\n";
$qid = "";
}
else
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Question id($sid) was NOT DELETED!\n$error\")\n //-->\n</script>\n";
}
}
elseif ($action == "insertnewanswer")
{
if (!$_POST['code'] || !$_POST['answer'])
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your answer could not be created! It does not contain both an answer code and answer text (which are required).\")\n //-->\n</script>\n";
}
else
{
$iaquery="SELECT * FROM answers WHERE qid={$_POST['qid']} AND code='{$_POST['code']}'";
$iaresult=mysql_query($iaquery) or die ("Error checking for answers with the same code<br />$iaquery<br />".mysql_error());
$matchcount=mysql_num_rows($iaresult);
if ($matchcount)
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your answer could not be created! There is already an answer to this question which has the same code. Your codes should be unique for each answer.\")\n //-->\n</script>\n";
}
else
{
if (get_magic_quotes_gpc() == "0")
{
$_POST['answer'] = addcslashes($_POST['answer'], "'");
}
$iaquery = "INSERT INTO answers (qid, code, answer, `default`) VALUES ('{$_POST['qid']}', '{$_POST['code']}', '{$_POST['answer']}', '{$_POST['default']}')";
$iaresult = mysql_query ($iaquery);
if ($iaresult)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your New Answer has been added!\")\n //-->\n</script>\n";
$surveyselect = getsurveylist();
}
else
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your answer could not be created!\")\n //-->\n</script>\n";
}
}
}
}
elseif ($action == "updateanswer")
{
if (get_magic_quotes_gpc() == "0")
{
$_POST['answer'] = addcslashes($_POST['answer'], "'");
}
if ($_POST['code'] != $_POST['old_code'])
{
$uaquery = "SELECT * FROM answers WHERE code = '{$_POST['code']}' AND qid={$_POST['qid']}";
$uaresult = mysql_query($uaquery) or die ("Cannot check for duplicate codes<br />$uaquery<br />".mysql_error());
$matchcount = mysql_num_rows($uaresult);
}
if ($matchcount)
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your answer could not be updated! There is another answer to this question with the same code. All codes should be unique.\")\n //-->\n</script>\n";
}
else
{
$uaquery = "UPDATE answers SET code='{$_POST['code']}', answer='{$_POST['answer']}', `default`='{$_POST['default']}' WHERE qid={$_POST['qid']} AND code='{$_POST['old_code']}'";
//echo $uaquery;
$uaresult = mysql_query($uaquery);
if ($uaresult)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your Answer ($qid, $code) has been updated!\")\n //-->\n</script>\n";
}
else
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your answer could not be updated!\")\n //-->\n</script>\n";
}
}
}
elseif ($action == "delanswer")
{
$query = "DELETE FROM answers WHERE qid=$qid AND code='$code'";
$result = mysql_query($query);
if ($result)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"Answer for question $qid ($code) has been deleted!\")\n //-->\n</script>\n";
$code = "";
}
else
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Answer for question $qid was NOT DELETED!\n$error\")\n //-->\n</script>\n";
}
}
elseif ($action == "insertnewsurvey")
{
if (!$_POST['short_title'])
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your survey could not be created because it did not have a short title.\")\n //-->\n</script>\n";
}
else
{
if (get_magic_quotes_gpc()=="0")
{
$_POST['short_title'] = addcslashes($_POST['short_title'], "'");
$_POST['description'] = addcslashes($_POST['description'], "'");
$_POST['welcome'] = addcslashes($_POST['welcome'], "'");
}
$isquery = "INSERT INTO surveys (sid, short_title, description, admin, active, welcome, expires,";
$isquery .= " adminemail, private, faxto, format, template, url, urldescrip) VALUES ('', '{$_POST['short_title']}', '{$_POST['description']}',";
$isquery .= " '{$_POST['admin']}', 'N', '".str_replace("\n", "<br />", $_POST['welcome'])."',";
$isquery .= " '{$_POST['expires']}', '{$_POST['adminemail']}', '{$_POST['private']}',";
$isquery .= " '{$_POST['faxto']}', '{$_POST['format']}', '{$_POST['template']}', '{$_POST['url']}', '{$_POST['urldescrip']}')";
$isresult = mysql_query ($isquery);
if ($isresult)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your Survey ($short_title) has been created!\")\n //-->\n</script>\n";
$isquery = "SELECT sid FROM surveys WHERE short_title like '{$_POST['short_title']}'";
$isquery .= " AND description like '{$_POST['description']}' AND admin like '{$_POST['admin']}'";
$isresult = mysql_query($isquery);
while ($isr = mysql_fetch_array($isresult)) {$sid = $isr['sid'];}
$surveyselect = getsurveylist();
}
else
{
$errormsg="Your survey could not be created - ".mysql_error();
echo "<script type=\"text/javascript\">\n<!--\n alert(\"$errormsg\")\n //-->\n</script>\n";
}
}
}
elseif ($action == "updatesurvey")
{
if (get_magic_quotes_gpc() == "0")
{
$_POST['short_title'] = addcslashes($_POST['short_title'], "'");
$_POST['description'] = addcslashes($_POST['description'], "'");
$_POST['welcome'] = addcslashes($_POST['welcome'], "'");
}
$usquery = "UPDATE surveys SET short_title='{$_POST['short_title']}', description='{$_POST['description']}',";
$usquery .= " admin='{$_POST['admin']}', welcome='".str_replace("\n", "<br />", $_POST['welcome'])."',";
$usquery .= " expires='{$_POST['expires']}', adminemail='{$_POST['adminemail']}',";
$usquery .= " private='{$_POST['private']}', faxto='{$_POST['faxto']}',";
$usquery .= " format='{$_POST['format']}', template='{$_POST['template']}', ";
$usquery .= " url='{$_POST['url']}', urldescrip='{$_POST['urldescrip']}'";
$usquery .= " WHERE sid={$_POST['sid']}";
$usresult = mysql_query($usquery) or die("Error updating<br />$usquery<br /><br /><b>".mysql_error());
if ($usresult)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your Survey ($short_title) has been updated!\")\n //-->\n</script>\n";
$surveyselect = getsurveylist();
}
else
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Your survey could not be updated! " . mysql_error() ." ($usquery)\")\n //-->\n</script>\n";
}
}
elseif ($action == "delsurvey")
{
$query = "DELETE FROM surveys WHERE sid=$sid";
$result = mysql_query($query);
if ($result)
{
//echo "<script type=\"text/javascript\">\n<!--\n alert(\"Survey id($sid) has been deleted!\")\n //-->\n</script>\n";
$sid = "";
$surveyselect = getsurveylist();
}
else
{
echo "<script type=\"text/javascript\">\n<!--\n alert(\"Survey id($sid) was NOT DELETED!\n$error\")\n //-->\n</script>\n";
}
}
elseif ($action == "adduser")
{
exec("$homedir\htpasswd.exe -b .htpasswd {$_POST['user']} {$_POST['pass']}");
}
else
{
echo "$action Not Yet Available!";
}
?>