-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewlist.php
More file actions
519 lines (420 loc) · 12.7 KB
/
viewlist.php
File metadata and controls
519 lines (420 loc) · 12.7 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<?php
// GiftWeb: PHP/PostgreSQL online Gift Registry System
// Matthew T. Jachimstal
// Copyright (C) 2000-2008 Matthew T. Jachimstal
//
// email: matthew@jachimstal.com
//
// Snail mail: Matthew Jachimstal
// 1106 Canterbury Ct, Unit D
// Elgin, IL 60120
//
// 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.
//
//
require "./main.inc";
// passed $s=session#, $u=target user id
require "./util.php";
$s = $_COOKIE["GiftWeb_Session"];
$u = $_GET["u"];
$uid = validatesession($s);
$conn = ADONewConnection($dbtype);
$conn->PConnect($dbhost, $dbuser, $dbpass, $dbname);
$num_cols = 4;
cleanup_items();
// FIX:
// make sure the user trying to view this list is a member of the same
// group. Otherwise, you can simply change the uid in the GET line
// and view anyone's list
if ( isset($_POST["submit1"]) ) // adding items not on list
{
add_items();
}
if ( isset($_POST["purch"]) ) // marking items purchased
{
purchase_items();
}
if ( isset($_POST["unpurch"]) ) // marking items unpurchased
{
unpurchase_items();
}
display_list();
function add_items() // add items not on the list
{
global $conn;
global $u;
global $uid;
$rows = 0;
for ($i=1; $i <= 10; $i++)
{
$desc = "idesc$i";
$item = chop ($_POST[$desc]);
if ( $item != "" )
{
$sql = "INSERT INTO items";
$sql .= " (uid, description, purchased, added, grp_id)";
$sql .= " VALUES ('$u', '$item', '$uid', 't', '-1')";
$result = $conn->Execute($sql);
//$rows += $result->RecordCount();
}
}
if ($rows != 0)
{
$res1 = "<h2>An error occured adding an item. Please try again.<br>";
$res1 .= "If the problem persists, please contact the site ";
$res1 .= "administrator.</h2>\n";
}
else
{
$res1 = "<h2>Item(s) added.</h2>\n";
}
}
// purchase_items and unpurchase_items() do error checking as follows:
// the itemlist[] array potentially contains purchased and unpurchased
// items. Each function (purch/unpurch) filters the UPDATE or DELETE
// SQL statement by the current purchased value.
//
// If it's marking items as purchased by the current user, it only UPDATEs
// those items whose purchased field is -1. This prevents them from trying
// to remark items they've already purchased, although that would have
// no net effect. It's just the easiest way to error check this condition.
// It also prevents them from somehow hijacking items that were already
// marked purchased by a third user. This could possibly happen from someone
// writing their own page whose form action POSTs to this php page.
// This is also a concurrency issue. If two people load the page and one
// submits a purchase and the second doesn't refresh before submitting
// the same purchase. The second user will not be able to update, because
// it will only update WHERE purchased=-1. They will reload the page after
// submission, and see that the item's already been marked by someone
// else. Potential cause for UTS and ID-10-T errors.
//
// When unpurchasing, the current value of purchased must be the user's
// uid. If it is -1, they're trying to unpurchased an item that hasn't been
// marked purchased. If it's another uid, they somehow got an invalid
// form that presented them with items they hadn't purchased. This is the
// the last error check to prevent that. Again, that could conceivably
// be caused by a malicious form POSTing to this script.
function purchase_items()
{
global $conn;
global $uid;
$itemlist = $_POST["itemlist"];
$cnt = count ($itemlist);
$marked = 0;
for ( $i=0; $i<$cnt; $i++ )
{
// update the purchased field to this user's $uid only if
// item is not purchased
$sql = "UPDATE items SET purchased='$uid'";
$sql .= " WHERE item_num='$itemlist[$i]' and purchased='-1'";
$result = $conn->Execute($sql);
/*
* FIX
* check for error conditions (are any possible?)
* keep count of how many are marked
* can use if($result) and $conn->ErrorMessage(), but to
* what effect here
*/
}
}
function unpurchase_items()
{
global $conn;
global $uid;
$itemlist = $_POST["itemlist"];
$cnt = count($itemlist);
$unmarked = 0;
$removed = 0;
for ( $i=0; $i<$cnt; $i++ )
{
// unmarking has two conditions: item added by requestor
// and item added by purchaser.
// In both cases we set the purchaser to -1.
// Then, we look for any rows with purchaser=-1 and added=t
// Those are unpurchased items that were added by the purchaser
// and need to be deleted.
// Also, we need to make that purchaser=uid, so we don't hijack
// anyone else's purchases.
$sql = "UPDATE items SET purchased='-1'";
$sql .= " WHERE item_num='$itemlist[$i]' and purchased='$uid'";
$result = $conn->Execute($sql);
/*
* FIX
* check for error condition (are any possible?)
* count how many are unmarked
*/
}
$sql = "DELETE FROM items WHERE purchased='-1' and added='t'";
$result = $conn->Execute($sql);
/*
* FIX
* count how many were deleted
*/
}
function display_list()
{
global $conn;
global $u;
global $uid;
global $num_cols;
$sql = "SELECT last_name, first_name FROM users WHERE uid='$u'";
$result = $conn->Execute($sql);
$name = chop($result->fields["last_name"]);
$name .= ", " . chop($result->fields["first_name"]);
$fname = chop($result->fields["first_name"]);
startpage_old("$fname's Wish list", 0);
?>
<script language="JavaScript" type="text/javascript">
<!--
function CheckAll()
{
for (var i = 0; i < document.itemList.elements.length; i++)
{
if(document.itemList.elements[i].type == 'checkbox')
{
document.itemList.elements[i].checked = true;
}
}
}
function ViewNote(inum)
{
window.open("viewnote.php?i="+inum,
"ViewNotes", "width=600, height=300");
}
//-->
</script>
<?php
if ( isset($res1))
{
echo $res1;
}
echo "<h2>$fname's wish list:</h2>";
?>
<form name="itemList" method="post"
action="viewlist.php?u=<?php echo $u?>">
<table cellspacing=0 cellpadding=1 border=1>
<?php
// Display items in the viewed user's list
// First, the overall header
echo "<tr><th width=40%>Description</th>";
echo "<th width=30%>Store/Web Address</th>";
echo "<th width=10%>Notes</th>";
echo "<th width=10%>Select</th></tr>\n";
// Find items purchased by the viewing user
$sql = "SELECT * FROM items WHERE uid='$u' ";
$sql .= "and purchased='$uid' ORDER BY description";
$mypurch = $conn->Execute($sql);
if (!$mypurch->EOF)
{
echo "<tr><td class=\"purch_hdr\" colspan=$num_cols align=center>";
echo "<b>Items purchased by current user</b></td></tr>\n";
show_items($mypurch, 1);
}
// Find items purchased by other users
$sql = "SELECT * FROM items WHERE uid='$u' ";
$sql .= "and purchased > 0 and purchased != '$uid' ";
$sql .= "ORDER BY description";
$opurch = $conn->Execute($sql);
if (!$opurch->EOF)
{
echo "<tr><td class=\"purch_hdr\" colspan=$num_cols align=center>";
echo "<b>Items purchased by other users</b></td></tr>\n";
show_items($opurch, 2);
}
// Finally, find unpurchased items
$sql = "SELECT * FROM items, item_grp ";
$sql .= "WHERE items.uid='$u' ";
$sql .= "and items.purchased = -1 ";
$sql .= "and items.grp_id = item_grp.grp_id ";
$sql .= "and items.hidden = 'f' ";
$sql .= "ORDER BY item_grp.grp_name, items.description";
$unpurch = $conn->Execute($sql);
if (!$unpurch->EOF)
{
echo "<tr><td class=\"purch_hdr\" colspan=$num_cols align=center>";
echo "<b>Items not purchased by any users</b></td></tr>\n";
show_items($unpurch, 3);
}
// build the events selector
$sql = "SELECT * FROM events ";
$sql .= "WHERE uid='-1' or uid='$u' ";
$sql .= "ORDER BY month, day";
$result = $conn->Execute($sql);
$sel_list = "<option value=\"0\">None/Other</option>";
$result->MoveFirst();
while (!$result->EOF)
{
$evdate = $result->fields["month"]."/".$result->fields["day"];
$sel_list .= "<option value=\"" . $result->fields["eid"] . "\">";
$sel_list .= $result->fields["description"] . " ($evdate)";
$sel_list .= "</option>\n";
$result->MoveNext();
}
?>
</table>
<br>
Select an event these items are being purchased for:
<br>
<select name="event_sel">
<?php echo $sel_list?>
</select>
<br>
<input type=submit name="purch" value="Purchase Selected Items">
<input type=submit name="unpurch" value="Un-purchase Selected Items">
<input type=reset name=reset value="Unselect all items">
<input type="button" value="Select all items"
onClick=CheckAll()>
</form>
<?php
// add a section where users can add items they've purchased
// that aren't on the list
?>
<p>
<hr width=70% size=5
<?php
echo "<h4><ul><li>If you've purchased items not on $fname's list, ";
echo "please add them below.</li>\n";
echo "<li>They will be displayed to other users to see that ";
echo "they've been purchased.</li>\n";
echo "<li>$fname will not see these items and will not be ";
echo "able to delete them.</li></ul></h4>";
?>
<form method="post" action="viewlist.php?u=<?php echo $u?>">
<table cellspacing=0 cellpadding=2>
<tr><td></td><td align=center>Description</td></tr>
<tr><td>1</td><td><input type="text" name="idesc1" size=50></td></tr>
<tr><td>2</td><td><input type="text" name="idesc2" size=50></td></tr>
<tr><td>3</td><td><input type="text" name="idesc3" size=50></td></tr>
<tr><td>4</td><td><input type="text" name="idesc4" size=50></td></tr>
<tr><td>5</td><td><input type="text" name="idesc5" size=50></td></tr>
<br>
Select an event these items are being purchased for:
<br>
<select name="event_sel">
<?php echo $sel_list?>
</select>
<br>
<tr><td></td><td align=center><input type="submit" name="submit1"
value="Add item(s)">
<input type="reset" name="reset" value="Clear"></tr></tr>
</table>
</form>
<?php
endpage_old(0);
}
// show_items(res, type)
// res: result set from the Execute() call
// type: 1->purchased by current user
// 2->purchased by other users
// 3->unpurchased
function show_items($res, $type)
{
global $conn;
global $u;
global $num_cols;
if ($type == 3)
{
// build list of all groups for this user
// associative array indexed by grp_id
$grp_sql = "SELECT * FROM item_grp WHERE uid='$u'
OR uid='-1'";
$grp_res = $conn->Execute($grp_sql);
$grp_res->MoveFirst();
while(!$grp_res->EOF)
{
$groups[$grp_res->fields["grp_id"]] = $grp_res->fields["grp_name"];
$grp_res->MoveNext();
}
}
$res->MoveFirst();
$prev_grp = "";
while(!$res->EOF)
{
$desc = $res->fields["description"];
$url = chop($res->fields["url"]);
$image = chop($res->fields["image"]);
$purchased = $res->fields["purchased"];
$added = $res->fields["added"];
$item_num = $res->fields["item_num"];
$item_grp = $res->fields["grp_id"];
if ($pad_images && !$image)
{
$image = "./images/60-60-blank.gif";
}
if(filter_var($url, FILTER_VALIDATE_URL))
{
$host = parse_url($url, PHP_URL_HOST);
$url = "<a href=\"$url\" target=\"_new\">$host</a>";
}
if ($type == 3)
{
// See if we're starting a new group
if ($prev_grp != $item_grp)
{
echo "<tr><td class=\"group_hdr\" colspan=$num_cols>";
echo "<b> $groups[$item_grp]</b></td></td>\n";
$prev_grp = $item_grp;
}
}
if ($image)
{
echo "<tr><td><img src=\"$image\" width=60 height=60 align=middle> $desc</td>";
}
else
{
echo "<tr><td>$desc</td>";
}
echo "<td>$url </td>";
$sql = "SELECT * FROM notes WHERE item_num='$item_num'";
$res2 = $conn->Execute($sql);
if (!$res2->EOF)
{
$note_text = "<a href=\"javascript:ViewNote($item_num)\">";
$note_text .= "ViewNotes</a>";
}
else
{
$note_text = " ";
}
if ($added == 't' )
{
if ($type == 1)
$note_text = "Added by<br>current user";
else
$note_text = "Added by<br>another user";
}
switch ($type)
{
case 1:
$sel_text = "<input type=checkbox name=\"itemlist[]\" ";
$sel_text .= "value=\"$item_num\">";
break;
case 2:
$sel_text = " ";
break;
case 3:
$sel_text = "<input type=checkbox name=\"itemlist[]\" ";
$sel_text .= "value=\"$item_num\">";
break;
}
?>
<td align=center><?php echo $note_text?></td>
<td align=center><?php echo $sel_text?></td>
</tr>
<?php
$res->MoveNext();
}
}
?>