-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
1167 lines (1132 loc) · 35.4 KB
/
index.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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
# Copyright 2014 Benjamin April
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
include('./phpqrcode/qrlib.php');
#Config:
$SALT = "T0kenz_";
$LOGO = "/images/logo.jpg";
$URL = "http://127.0.0.1/";
$PATH_TO_IMAGES = '/usr/share/nginx/html/images/';
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=tokens";
$credentials = "user=web_user password=foobar123!";
session_start(); // start up your PHP session!
header('Token: 3C575679');
ob_implicit_flush(1);
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ob_end_clean();
set_time_limit(0);
function RandomString()
{
$characters = '0123456789ABCDEF';
$randstring = '';
for ($i = 0; $i < 8; $i++) {
$randstring .= $characters[rand(0, 15)];
}
return $randstring;
}
$alerts = array();
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Error : Unable to open database";
$alert->fatal = 1;
array_push($alerts, $alert);
}
if(!array_key_exists('action',$_REQUEST)){ $_REQUEST["action"] = 'null';}
if(!array_key_exists('loggedin',$_SESSION)) {$_SESSION['loggedin'] = 0;}
if($_SESSION['loggedin'] == 0 and $_REQUEST["action"] != 'login'){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not logged in.";
$alert->fatal = 0;
array_push($alerts, $alert);
$_REQUEST['action'] = 'need_login';
}
$sets = array();
pg_prepare($db, "submit_log", 'INSERT INTO LOGS (TYPE,SOURCE,USER_IDENT,REQUEST,HEADERS) VALUES ($1,$2,$3,$4,$5)');
pg_prepare($db, "submit_log_anon", 'INSERT INTO LOGS (TYPE,SOURCE,REQUEST,HEADERS) VALUES ($1,$2,$3,$4)');
pg_prepare($db, "set_winner", "UPDATE USERS set winner = 't' where user_ident = $1");
switch ($_REQUEST["action"]) {
case "logout":
session_destroy();
session_start(); // start up your PHP session!
$_SESSION['loggedin'] = 0;
$alert = new alert;
$alert->type = 'success';
$alert->message = "Logout successful.";
array_push($alerts, $alert);
break;
case "login": #Only action allowed when not logged in.
$result = pg_query_params($db, "SELECT name,admin,(date_part('epoch', now()) - date_part('epoch',CODE_AT)),id FROM users WHERE user_ident = $1 and password = crypt($2,password)", array($_REQUEST["user"],$_REQUEST["password"]));
if(!$result){
$alert = new alert;
$alert->type = 'danger';
$alert->message = pg_last_error($db);
array_push($alerts, $alert);
} else {
if(pg_num_rows($result) == 1){
$alert = new alert;
$alert->type = 'success';
$alert->message = "Login successful.";
array_push($alerts, $alert);
$row = pg_fetch_row($result);
$_SESSION['user_ident'] = $_REQUEST["user"];
$_SESSION['user_name'] = $row[0];
$_SESSION['last_token'] = $row[2];
$_SESSION['loggedin'] = 1;
$_SESSION['sem_id'] = $row[3];
if($row[1] == 't') {
$_SESSION['admin'] = 1;
} else {
$_SESSION['admin'] = 0;
}
if(array_key_exists('token',$_REQUEST)){
header('Location: '.$URL.'index.php?action=submit_token&token='.$_REQUEST['token'],true, 302);
die();
} else {
$_REQUEST['action'] = 'tokens';
}
} else {
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Login failed.";
array_push($alerts, $alert);
$result = pg_execute($db, "submit_log",array("Login Failed",$_SERVER['REMOTE_ADDR'],$_REQUEST["user"], print_r($_SERVER, true),$_REQUEST["user"]));
$_REQUEST['action'] = 'need_login';
}
}
break;
case "leader":
$result = pg_query($db,'select u.name,(select count(code) from tickets where user_ident = u.user_ident) as count from users u order by count desc');
$output = array();
while($row = pg_fetch_assoc ( $result ) ) {
$output{$row{'name'}} = $row{'count'};
}
break;
case "tokens":
break;
case "submit_token":
$alert = new alert;
$alert->type = 'info';
$alert->message = "Sorry, game is over. ";
$alert->fatal = 1;
array_push($alerts, $alert);
return;
$semaphore = sem_get($_SESSION['sem_id'], 1, 0666, 1);
sem_acquire($semaphore);
$reset_timer = 1;
$result = pg_query_params($db, "SELECT (date_part('epoch', now()) - date_part('epoch',CODE_AT)) FROM users WHERE user_ident = $1", array($_SESSION["user_ident"]));
if(!$result){
$alert_info = pg_last_error($db);
} else {
if(pg_num_rows($result) == 1){
$row = pg_fetch_row($result);
$_SESSION['last_token'] = $row[0];
}
}
#Sleep until we are 15 seconds after last submit.
if ($_SESSION['last_token'] < 15) {
sleep (15 - $_SESSION['last_token']);
$alert = new alert;
$alert->type = 'info';
$alert->message = "Sleeping ".(16 - $_SESSION['last_token'])." before trying token";
array_push($alerts, $alert);
}
$hash = hash('sha256',$SALT.strtoupper($_REQUEST['token']));
#Check if we have redeemed this token already.
$result = pg_query_params($db, 'SELECT id FROM tickets
WHERE user_ident = $1
and code = $2', array($_SESSION["user_ident"],$hash));
if( pg_num_rows($result) > 0) {
$alert = new alert;
$alert->type = 'info';
$alert->message = "You have already redeemed this token.";
array_push($alerts, $alert);
} else {
#Check if token exists in DB.
$result = pg_query_params($db, 'SELECT DESCRIPTION,QTY,MULTI,DEC,EVENT FROM tokens where code = $1 and (MULTI > 0 or MULTI IS NULL)', array($hash));
if( pg_num_rows($result) == 1) {
$row = pg_fetch_row($result);
$description = $row[0];
$value = $row[1];
$multi = $row[2];
$deci = $row[3];
$event = $row[4];
#Check if the token is still eligible to redeem.
if(!$multi or $multi > 0){
#Convert to tickets.
if($event){
#Get team of current user for this event.
$uresult = pg_query_params($db, 'select user_ident from user_teams where team_ident in (
select ut.team_ident from user_teams ut,teams t where ut.user_ident = $1 and ut.team_ident = t.team_ident and t.event_ident = $2)', array($_SESSION["user_ident"],$event));
$i = pg_num_rows($uresult);
if($i){
while($i){
$urow = pg_fetch_row($uresult);
get_tickets($db,$urow[0],$hash,$value);
$i--;
}
decrement_ticket_counter($db,$hash,$deci,$multi);
} else {
$alert = new alert;
$alert->type = 'warning';
$alert->message = "Team not assigned.";
array_push($alerts, $alert);
$reset_timer = 0;
}
} else {
get_tickets($db,$_SESSION["user_ident"],$hash,$value);
decrement_ticket_counter($db,$hash,$deci,$multi);
}
$alert = new alert;
$alert->type = 'success';
$alert->message = "Code is valid.";
array_push($alerts, $alert);
$reset_timer = 0;
} else {
$alert = new alert;
$alert->type = 'warning';
$alert->message = "Code has already been claimed.";
array_push($alerts, $alert);
}
} else {
$alert = new alert;
$alert->type = 'warning';
$alert->message = "Sorry, This code is invalid.";
array_push($alerts, $alert);
$result = pg_execute($db, "submit_log",array("Invalid Token",$_SERVER['REMOTE_ADDR'],$_SESSION["user_ident"], print_r($_SERVER, true),$_REQUEST['token']));
}
}
#Switch to token display as part of results.
$_REQUEST['action'] = 'tokens';
if($reset_timer == 1){
#set new last_submit time.
$result = pg_prepare($db, "update_code_at", 'UPDATE users SET code_at = now() where user_ident = $1');
$result = pg_execute($db, "update_code_at",array($_SESSION["user_ident"]));
}
sem_release($semaphore);
break;
case "raffle":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
break;
case "gen_token":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
break;
case "change_password":
pg_prepare($db, "change_pass", "UPDATE USERS SET password = crypt($3,gen_salt('md5')) where user_ident = $1 and password = crypt($2,password)");
if($_REQUEST["new_password"] == $_REQUEST["new_password_confirm"]){
$result = pg_query_params($db, "SELECT name,admin,(date_part('epoch', now()) - date_part('epoch',CODE_AT)),id FROM users WHERE user_ident = $1 and password = crypt($2,password)", array($_SESSION["user_ident"],$_REQUEST["password"]));
if(!$result){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Error Querying DB";
$alert->fatal = 1;
array_push($alerts, $alert);
} else {
if(pg_num_rows($result) == 1){
pg_execute($db,"change_pass",array($_SESSION["user_ident"],$_REQUEST["password"],$_REQUEST["new_password_confirm"]));
$alert = new alert;
$alert->type = 'success';
$alert->message = "Password updated. ";
array_push($alerts, $alert);
} else {
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Password invalid. ";
array_push($alerts, $alert);
}
}
} else {
$alert = new alert;
$alert->type = 'danger';
$alert->message = "New passwords do not match. ";
array_push($alerts, $alert);
}
break;
case "about":
break;
case "password":
break;
case "token_list":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
break;
case "manage_sets":
$result = pg_query_params($db, "select tag from tokens where tag NOTNULL group by tag");
if(!$result){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Error Querying DB";
$alert->fatal = 1;
array_push($alerts, $alert);
} else {
if(pg_num_rows($result) > 0){
$row = pg_fetch_row($result);
array($sets, $row[0]);
}
}
break;
case "manage_users":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
break;
case "manage_events":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
break;
case "create_event":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
pg_prepare($db, "create_event", "INSERT INTO EVENTS (EVENT_IDENT) VALUES ($1)");
pg_execute($db,"create_event",array($_REQUEST["name"]));
$_REQUEST["action"] = "manage_events";
break;
case "create_team":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
pg_prepare($db, "create_team", "INSERT INTO TEAMS (EVENT_IDENT,TEAM_IDENT) VALUES ($1,$2)");
pg_execute($db,"create_team",array($_REQUEST['event'],$_REQUEST["name"]));
$_REQUEST["action"] = "edit_event";
break;
case "edit_event":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
break;
case "create_token":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
break;
case "view_user":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
break;
case "assign_team":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
if ($_REQUEST['team'] != "NULL"){
pg_prepare($db, "assign_team", "INSERT INTO USER_TEAMS
(TEAM_IDENT,USER_IDENT) VALUES ($1,$2)");
pg_prepare($db, "delete_team", "DELETE FROM user_teams
WHERE TEAM_IDENT = $1 and USER_IDENT = $2");
if($_REQUEST['delete']){
pg_execute($db,"delete_team",array($_REQUEST['delete'],$_REQUEST["user"]));
}
pg_execute($db,"assign_team",array($_REQUEST['team'],$_REQUEST["user"]));
}
$_REQUEST["action"] = "edit_event";
break;
case 'need_login':
break;
case 'null': # No action needs no alarm.
break;
case 'view_event':
break;
default:
if(array_key_exists('user_ident',$_REQUEST)){
pg_execute($db, "submit_log",array("Unknown Action",$_SERVER['REMOTE_ADDR'],$_SESSION["user_ident"], print_r($_SERVER, true),$_REQUEST["action"]));
} else {
pg_execute($db, "submit_log_anon",array("Unknown Action",$_SERVER['REMOTE_ADDR'], print_r($_SERVER, true),$_REQUEST["action"]));
}
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Action not valid.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>T0ken Portal</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- 239618CC -->
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Tokens</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<?php
if(isset($_SESSION['loggedin']) and $_SESSION['loggedin'] == 1){
?>
<li class="toggle"><a href="?action=tokens">My Tokens</a></li>
<li class="toggle"><a href="?action=leader">Leader Board</a></li>
<li class="toggle"><a href="?action=about">About</a></li>
<?php if($_SESSION['admin']){ ?>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Admin <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="?action=raffle">Raffle</a></li>
<li><a href="?action=gen_token">Generate Token</a></li>
<li><a href="?action=token_list">List all Tokens</a></li>
<li><a href="?action=manage_sets">Manage sets</a></li>
<li><a href="?action=password">Change Password</a></li>
<li><a href="?action=manage_events">Manage Events</a></li>
<li><a href="?action=manage_users">Manage Users</a></li>
</ul>
</li>
<?php } else { ?>
<li class="toggle"><a href="?action=password">Change Password</a></li>
<?php } ?>
<li class="toggle"><a href="?action=logout">Logout</a></li>
<li class="toggle">
<form class="navbar-form navbar-right" role="form" method=POST>
<div class="form-group">
<input type="hidden" name="action" value="submit_token">
<input type="text" placeholder="Token" name=token class="form-control">
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
</li>
<?php } ?>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
<div class="container">
<br><br><br>
<?php
foreach ($alerts as $alert) {
$alert->display($db);
}
?>
<?php
switch ($_REQUEST["action"]) {
case "about": ?>
<div class="jumbotron">
Blah Blah Blah what is this all about.
<!-- 883F6059 -->
</div>
<?php
break;
case "token_list":
print "<TABLE class=\"table\">";
print "<TR><TH>Token:</TH><TH>Count</TH></TR>";
$result = pg_query($db, "select tokens.description,tokens.code,count(tickets.user_ident) from tokens,tickets where tokens.code = tickets.code group by tokens.description,tokens.code order by count desc,tokens.description");
if($result){
$i = pg_num_rows($result);
while($i) {
$row = pg_fetch_row($result);
print "<TR><TD>";
print $row[0]."</A></TD><TD>".$row[2]."</TD></TR>";
$result2 = pg_query_params($db,"select users.name,count(users.name) as count from users, tickets where users.user_ident = tickets.user_ident and tickets.code = $1 group by users.name;",array($row[1]));
print "<TR><TD></TD><TD></TD><TD><TABLE class=\"table\">";
$j = pg_num_rows($result2);
while($j){
$row = pg_fetch_row($result2);
print "<TR><TD>".$row[0]."</TD><TD>".$row[1]."</TD></TR>";
$j--;
}
print "</table></TD></TR>";
$i--;
}
}
print "</table>";
case "leader":
print "<TABLE class=\"table\">";
print "<!-- 921CE6E9 -->";
print "<TR><TH>User</TH><TH></TH><TH>Tickets</TH>\n";
foreach ($output as $key => $value ) {
print "<TR><TD>$key</TD><TD> </TD><TD>$value</TD></TR>";
}
print "</TABLE>";
print "<H2>Events</H2>";
print "<TABLE class=\"table\">";
$result = pg_query($db, "select event_ident from events order by event_ident");
if($result){
$i = pg_num_rows($result);
while($i) {
$row = pg_fetch_row($result);
print "<TR><TD>".$row[0]."</TD><TD><A HREF=/?action=view_event&event=".$row[0].">View Event</A></TD></TR>\n";
$i--;
}
}
print "</TABLE>";
break;
case "tokens":
print "<TABLE class=\"table\">";
print "<TR><TH>Token</TH><TH></TH><TH>Count</TH>\n";
$output = load_tokens($db,$_SESSION['user_ident']);
foreach ($output as $key => $value ) {
print "<TR><TD>$key</TD><TD> </TD><TD>$value</TD></TR>";
}
print "</TABLE>";
print "<!-- F3C5C56F -->";
break;
case "need_login":
?><div>
<form role="form" method=POST>
<div class="form-group">
<input type=hidden name=action value=login>
<?php
if (array_key_exists('token',$_REQUEST)){
print "<input type=hidden name=token value=".$_REQUEST['token'].">";
}
?>
<input type="text" placeholder="user" name=user class="form-control">
<input type="password" placeholder="Password" name=password class="form-control">
<button type="submit" class="btn btn-success">Sign in</button>
</div>
</form>
</div>
<?php
break;
case "password":
?><div>
<form role="form" method=POST>
<input type=hidden name=action value=change_password>
Old Password: <input type=text name=password><br>
New Password: <input type=text name=new_password><br>
Confirm Password: <input type=text name=new_password_confirm><br>
<input type=submit>
</form>
</div>
<?php
break;
case "raffle":
#Set winners here, if you want to recuse someone from the raffle.
$winner_3 = draw_winner($db);
$winner_2 = draw_winner($db);
$winner_1 = draw_winner($db);
$winner1 = pg_query($db,"select u.user_ident,u.name from tickets t, users u where t.user_ident = u.user_ident and u.winner = 'f' order by random() limit 1");
?>
<ul class="list-group">
<li class="list-group-item">Starting Drawing procedure:</li>
<li class="list-group-item">Managers being remove from Draw:</li>
<li class="list-group-item"><ul class="list-group">
<li class="list-group-item">Ben April</li>
<li class="list-group-item">Bob McArdle</li>
<li class="list-group-item">Ryan Flores</li>
<li class="list-group-item">Martin Roesler</li>
<li class="list-group-item">Simon Ko</li>
<li class="list-group-item">Pawan Kinger</li>
</ul></li>
<li class="list-group-item">Running Draw Query.</li>
<li class="list-group-item">
<div id=winner1 style="visibility:hidden">
<H1><?php echo $winner_1 ?></H1>
</div>
</li>
<li class="list-group-item">
<div id=winner2 style="visibility:hidden">
<H1><?php echo $winner_2 ?></H1>
</div>
</li>
<li class="list-group-item">
<div id=winner3 style="visibility:hidden">
<H1><?php echo $winner_3 ?></H1>
</div>
</li>
<li class="list-group-item">
<div id="button3_div">
<input type="button" id="button3" value="Show 3rd Prize Winner"
onclick="document.getElementById('winner3').style.visibility = 'visible';
document.getElementById('button2_div').style.visibility = 'visible';">
</div>
<div id="button2_div" style="visibility:hidden">
<input type="button" id="button2" value="Show 2nd Prize Winner"
onclick="document.getElementById('winner2').style.visibility = 'visible';
document.getElementById('button1_div').style.visibility = 'visible';">
</div>
<div id="button1_div" style="visibility:hidden">
<input type="button" id="button1" value="Show 1st Prize Winner" onclick="document.getElementById('winner1').style.visibility = 'visible';">
</div>
</li>
</ul>
<?php
break;
case "gen_token":
$event_list = array();
$result = pg_query($db, "select event_ident from events order by event_ident");
if($result){
$i = pg_num_rows($result);
while($i) {
$row = pg_fetch_row($result);
array_push($event_list,$row[0]);
$i--;
}
}
?>
<div class="form-group">
<form role="form" method=POST>
<input type=hidden name=action value=create_token>
<table class="table">
<tr><th colspan=3><H2>Create new token(s)</H2></TH></TR>
<tr>
<TH>Description:</th>
<td><input type=text name=description></td>
<td> </td>
</tr>
<tr>
<th>Code:</th>
<td><input type=text name=code></td>
<td> <i>Leave blank to auto-generate</i></td>
</tr>
<tr><th>Count:</th>
<td><select name=value>
<option>1
<option>2
<option>3
<option>4
<option>5
<option>6
<option>7
<option>8
<option>9
<option>10
<option>21
<option>22
<option>23
<option>24
<option>25
<option>26
<option>27
<option>28
<option>29
<option>20
<option>31
<option>32
<option>33
<option>34
<option>35
<option>36
<option>37
<option>38
<option>39
<option>30
</select></td>
<td><i>How many tickets will be granted</i></td>
</tr>
<tr>
<th>Multi:</th>
<td><select name=multi>
<option value=NULL>-- One per user --
<option>1
<option>2
<option>3
<option>4
<option>5
<option>6
</select></td>
<td><i>How many users may redeem this token<i></td>
</tr>
<tr>
<th>Qty:</th>
<td><select name=qty>
<option>1
<option>2
<option>3
<option>4
<option>5
<option>6
<option>12
<option>25
</select></td>
<td><i>How many tokens to create<i></td>
</tr>
<tr>
<th>Decrement value per issue:</th>
<td> <select name=decrement>
<option>0
<option>1
<option>2
<option>3
<option>4
<option>5
<option>6
<option>7
<option>8
<option>9
<option>10
</select></td>
<td><i>How much to decrease the value every time the token is awarded</i></td>
</tr>
<tr>
<th>Team Event:</th>
<td><SELECT NAME=event>
<option value=NULL> -- No Event
<?php
foreach ($event_list as $event){
print "<option> $event\n";
}
?>
</select></td>
<td><i>If submitted by one member, all members of that team get the same reward.</i></td>
</tr>
<tr>
<th>Tag:</th>
<td><input type=text name=tag></td>
<td><i>Tag-set to link this token with.</i></td>
</tr>
<tr><td colspan=3><input type=submit></td></tr>
</table>
</form>
</div>
<?php
break;
case 'view_event':
?>
<meta http-equiv="refresh" content="30">
<H2>Event: <?php $_REQUEST['event']?></H2>
<table class="table table-condensed">
<TR><TH>Team</TH><TH>Members Token Score</TH></TR>
<?php
$team_list = array();
$result = pg_query_params($db, "select team_ident from teams where event_ident = $1 order by team_ident",array($_REQUEST['event']));
if($result){
$i = pg_num_rows($result);
while($i) {
$row = pg_fetch_row($result);
array_push($team_list,$row[0]);
$i--;
}
}
$result = pg_query_params($db, "select team_ident from teams where event_ident = $1 order by team_ident",array($_REQUEST['event']));
if($result){
$i = pg_num_rows($result);
while($i) {
$row = pg_fetch_row($result);
print "<TR><TD>".$row[0]."</TD><TD>";
$r1 = pg_query_params($db, "select ut.user_ident,u.name from user_teams ut, users u where ut.team_ident = $1 and ut.user_ident = u.user_ident order by user_ident",array($row[0]));
if($r1){
print "<table class=\"table table-condensed\">";
$j = pg_num_rows($r1);
$first = 1;
$event_total = 0;
$l = $j;
while($l > 0) {
if($j){
$row1 = pg_fetch_row($r1);
}
if($first == 1){
$result2 = pg_query_params($db, "SELECT tokens.description,count(tickets.code)
FROM tokens, tickets
WHERE tickets.user_ident = $1
AND tokens.event = $2
AND tickets.code = tokens.code group by tokens.description;
",array($row1[0],$_REQUEST['event']));
$k = pg_num_rows($result2);
$l += $k;
$first = 0;
}
if($j){
print "<TR><TD>".$row1[1]."</TD>";
$j--;
$l--;
} else if ($k){
print "<TR><TD></TD>";
}
if($k) {
$row2 = pg_fetch_row($result2);
print "<TD>".$row2[0]."</TD><TD>".$row2[1]."</TD>";
$event_total += $row2[1];
$k--;
$l--;
}
print "</TD></TR>";
}
print "<TR><TD></TD><TD></TD><TH>Total:</TH><TD>".$event_total."</TD></TR>";
print "</table>";
}
print "</TD></TR>";
$i--;
}
}
break;
case "manage_events":
?>
<table class="table">
<TR><TH>Event</TH><TH>Edit</TH></TR>
<?php
$result = pg_query($db, "select event_ident from events order by event_ident");
if($result){
$i = pg_num_rows($result);
while($i) {
$row = pg_fetch_row($result);
print "<TR><TD>".$row[0]."</TD><TD><A HREF=/?action=edit_event&event=".$row[0].">Edit Event</A></TD></TR>\n";
$i--;
}
}
?></TITLE>
<div>
<form method=POST>
<input type=hidden name=action value=create_event>
Name: <input type=text name=name><br>
<input type=submit>
</form>
</div>
<?php
break;
case "manage_users":
?>
<table class="table">
<TR><TH>User ID</TH><TH>Name</TH><TH>Winner</TH><TH>Edit</TH></TR>
<?php
$result = pg_query($db, "select user_ident,name,winner from users order by user_ident");
if($result){
$i = pg_num_rows($result);
while($i) {
$row = pg_fetch_row($result);
print "<TR><TD>".$row[0]."</TD><TD>".$row[1]."</TD>";
print "<TD>".$row[2]."</TD>";
print "<TD><A HREF=/?action=view_user&user=".$row[0].">Edit User</A></TD></TR>\n";
$i--;
}
}
?></TABLE>
<div>
<form method=POST>
<input type=hidden name=action value=create_event>
Name: <input type=text name=name><br>
<input type=submit>
</form>
</div>
<?php
break;
case "edit_event":
#List team.
print "<table class=\"table\">";
$team_list = array();
$result = pg_query_params($db, "select team_ident from teams where event_ident = $1 order by team_ident",array($_REQUEST['event']));
if($result){
$i = pg_num_rows($result);
while($i) {
$row = pg_fetch_row($result);
array_push($team_list,$row[0]);
$i--;
}
}
$result = pg_query_params($db, "select team_ident from teams where event_ident = $1 order by team_ident",array($_REQUEST['event']));
if($result){
$i = pg_num_rows($result);
while($i) {
$row = pg_fetch_row($result);
print "<TR><TD>".$row[0]."</TD><TD>";
$r1 = pg_query_params($db, "select ut.user_ident,u.name from user_teams ut, users u where ut.team_ident = $1 and ut.user_ident = u.user_ident order by user_ident",array($row[0]));
if($r1){
print "<table class=\"table\">";
$j = pg_num_rows($r1);
while($j > 0) {
$row1 = pg_fetch_row($r1);
print "<TR><TD>".$row1[1]."</TD><TD>";
print "<form action='/' METHOD=POST>";
print "<INPUT TYPE=HIDDEN NAME=action VALUE=assign_team>";
print "<INPUT TYPE=HIDDEN NAME=delete VALUE=".$row[0].">";
print "<INPUT TYPE=HIDDEN NAME=event VALUE=".$_REQUEST['event'].">";
print "<INPUT TYPE=HIDDEN NAME=user value=".$row1[0].">";
print "<SELECT NAME=team>";
print "<option value=NULL> -- Select Team";
foreach ($team_list as $team){
print "<option> $team\n";
}
print "</select>";
print "<input type=submit>";
print "</form>";
$j--;
}
print "</table>";
}
print "</TD></TR>";
$i--;
}
}
print "<TR><TD>Not Assigned</TD><TD>";
$r1 = pg_query_params($db, "SELECT user_ident,name from users where user_ident not in (
SELECT ut.user_ident from user_teams ut, teams t
where t.team_ident = ut.team_ident and
t.event_ident = $1) order by user_ident",array($_REQUEST['event']));
if($r1){
print "<table class=\"table\">";
$j = pg_num_rows($r1);
while($j > 0) {
$row = pg_fetch_row($r1);
print "<TR><TD>".$row[1]."</TD><TD>";
print "<form action='/' METHOD=POST>";
print "<INPUT TYPE=HIDDEN NAME=action VALUE=assign_team>";
print "<INPUT TYPE=HIDDEN NAME=event VALUE=".$_REQUEST['event'].">";
print "<INPUT TYPE=HIDDEN NAME=user value=".$row[0].">";
print "<SELECT NAME=team>";
print "<option value=NULL> -- Select Team";
foreach ($team_list as $team){
print "<option> $team\n";
}
print "</select>";
print "<input type=submit>";
print "</form>";
$j--;
}
print "</table>";
}
/*
SELECT user_ident from users where user_ident not in (
SELECT ut.user_ident from user_teams ut, teams t
where t.team_ident = ut.team_ident and
t.event_ident = 'CTF');
*/
print "</TABLE>";
#List users not connected to a team.
?></TABLE>
<div>
<form method=POST action="/">
<input type=hidden name=action value=create_team>
<input type=hidden name=event value=<?php echo $_REQUEST['event']?>>
Team Name: <input type=text name=name><br>
<input type=submit>
</form>
</div>
<?php
break;
case "create_token":
if(!$_SESSION['admin']){
$alert = new alert;
$alert->type = 'danger';
$alert->message = "Sorry, You are not an admin.";
$alert->fatal = 1;
array_push($alerts, $alert);
}
$code = $_REQUEST['code'];