Subversion Repositories CoffeeCatalog

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 - 1
## RollerCon support functions...
2
 
3
use strict;
4
use Exporter;
5
use CGI qw/:standard :netscape/;
6
use CGI::Cookie;
7
use DBI;
8
 
9
my $dsn = "DBI:mysql:database=rollerco_data;host=localhost;port=3306";
10
# my $dbh = DBI->connect($dsn, 'rollerco_www', 'www-data');
11
use WebDB;
12
my $dbh = WebDB::connect ();
13
 
14
our $ORCUSER;
15
 
16
sub authDB {
17
	my $src = shift;
18
	my $id = shift;
19
	my $pass = shift;
20
	my $level = shift;
21
	my ($result, $encpass);
22
 
23
	my $sth = $dbh->prepare("select * from official where email = ?");
24
	$sth->execute($id);
25
	my $RCDBIDHASH = $sth->fetchrow_hashref();
26
 
27
	if ($src eq "form") {
28
		my $pwdhan = $dbh->prepare("select password(?)");
29
		$pwdhan->execute($pass);
30
		($encpass) = $pwdhan->fetchrow();
31
	} else {
32
		$encpass = $pass;
33
	}
34
 
35
	if (!$RCDBIDHASH) {
36
		$result->{ERRMSG} = "User-ID/Email Address not found!";
37
		$result->{cookie_string} = '';
38
		$result->{RCid} = '';
39
		logit(0, "Account not found: $id");
40
		$result->{authenticated} = 'false';
41
	} elsif ($RCDBIDHASH->{'password'} ne $encpass) {
42
		$result->{ERRMSG} = "Incorrect Password!";
43
		$result->{cookie_string} = '';
44
		$result->{RCid} = $RCDBIDHASH->{'RCid'};
45
		logit($RCDBIDHASH->{'RCid'}, "Incorrect Password");
46
		$result->{authenticated} = 'false';
47
	} elsif ($RCDBIDHASH->{'access'} < $level) {
48
		$result->{ERRMSG} = "Your account either needs to be activated, or doesn't have access to this page!";
49
		$result->{cookie_string} = "${id}&${encpass}&$RCDBIDHASH->{'access'}";
50
		$result->{RCid} = $RCDBIDHASH->{'RCid'};
51
		logit($RCDBIDHASH->{'RCid'}, "Insufficient Privileges");
52
		$result->{authenticated} = 'false';
53
	} else {
54
		$result->{ERRMSG} = '';
55
		$result->{cookie_string} = "${id}&${encpass}&$RCDBIDHASH->{'access'}";
56
		$result->{RCid} = $RCDBIDHASH->{'RCid'};
57
		logit($RCDBIDHASH->{'RCid'}, "Logged In") if $src eq "form";
58
		$result->{authenticated} = 'true';
59
		$ORCUSER=$RCDBIDHASH;
60
	}
61
	return $result;
62
}
63
 
64
sub authenticate {									# Verifies the user has logged in or puts up a log in screen
65
# Check to see if the user has already logged in (there should be cookies with their authentication)?
66
	my $MINLEVEL = shift || 1;
67
#	my $MINLEVEL = 1;
68
	my ($ERRMSG, $authenticated, %FORM);
69
	my $sth = $dbh->prepare("select * from official where email = '?'");
70
 
71
	my $query = new CGI;
72
	$FORM{'ID'} = $query->param('id') || '';
73
	$FORM{'PASS'} = $query->param('pass') || '';
74
	$FORM{'SUB'} = $query->param('login') || '';
75
	my $RCAUTH = $query->cookie('RCAUTH');
76
 
77
	if ($FORM{'SUB'}) {
78
		#a log in form was submited
79
		if ($FORM{'SUB'} eq "Submit") {
80
			$authenticated = authDB('form', $FORM{'ID'}, $FORM{'PASS'}, $MINLEVEL);
81
		} elsif ($FORM{'SUB'} eq "New User") {
82
			# Print the new user form and exit
83
		}
84
	} elsif ($RCAUTH) {
85
		#We have an authenication cookie.  Double-check it
86
		my ($RCID, $RCPASS, $RCLVL) = split /&/, $RCAUTH;
87
		$authenticated = authDB('cookie', $RCID, $RCPASS, $MINLEVEL);
88
	} else {
89
		$authenticated->{authenticated} = 'false';
90
	}
91
 
92
 
93
	if ($authenticated->{authenticated} eq 'true') {
94
		return $authenticated->{cookie_string};
95
	}
96
 
97
 
98
 
99
# If we get here, the user has failed authentication; throw up the log-in screen and die.
100
 
101
	my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"",-expires=>"now");
102
 
103
if ($authenticated->{ERRMSG}) {
104
	$authenticated->{ERRMSG} = "<TR><TD colspan=2 align=center><font color=red><b>".$authenticated->{ERRMSG}."</b></font>&nbsp</TD></TR>";
105
	# Log the failed access attempt
106
} else {
107
	$authenticated->{ERRMSG} = "";
108
	# Since there was no ERRMSG, no need to log anything.
109
}
110
 
111
	print header(-cookie=>$RCAUTH_cookie);
112
	printRCHeader("Please Sign In");
113
	print<<authpage;
114
	<form action="$ENV{REQUEST_URI}" method=POST name=Req id=Req>
115
		<TR><TD colspan=2 align=center><b><font size=+2>Please Sign In</font>
116
 
117
		</TD></TR>
118
		<TR><TD colspan=2>&nbsp</TD></TR>
119
		$authenticated->{ERRMSG}
120
		<TR>
121
			<TD align=right><B>User ID:</TD><TD><INPUT type=text name=id></TD>
122
		</TR>
123
		<TR>
124
			<TD align=right><B>Password:</TD><TD><INPUT type=password name=pass></TD>
125
		</TR>
126
		<TR><TD></TD><TD><INPUT type=submit name=login value=Submit></TD></TR>
127
		<TR><TD colspan=2 align=center>&nbsp;</TD></TR>
128
		<TR><TD colspan=2 align=center><A HREF="/schedule/manage_user.pl?submit=New%20User">[register as a new user]</A></TD></TR>
129
		<TR><TD colspan=2 align=center><A HREF="/schedule/password_reset.pl">[reset your password]</A></TD></TR>
130
	</TABLE>
131
	</FORM>
132
 
133
	<SCRIPT language="JavaScript">
134
	<!--
135
 
136
	function Login () {
137
		document.getElementById('Req').action = "$ENV{SCRIPT_NAME}";
138
		document.getElementById('Req').submit.click();
139
		return true;
140
	}
141
 
142
 
143
	//-->
144
	</SCRIPT>
145
 
146
authpage
147
 
148
#foreach (keys %ENV) {
149
#	print "$_: $ENV{$_}<br>";
150
#}
151
#	&JScript;
152
	exit;
153
}
154
 
155
sub getSetting {
156
	my $k = shift;
157
	my $sth = $dbh->prepare("select setting.value from setting where setting.key = ?");
158
	$sth->execute($k);
159
	return $sth->fetchrow_hashref()->{value};
160
}
161
 
162
sub getUser {
163
	my $EML = shift;
164
	my $sth = $dbh->prepare("select * from official where email = ?");
165
	$sth->execute($EML);
166
	return $sth->fetchrow_hashref();
167
}
168
 
169
sub getUserEmail {
170
	my $RCid = shift;
171
	my $sth = $dbh->prepare("select email from official where RCid = ?");
172
	$sth->execute($RCid);
173
	my ($email) = $sth->fetchrow_array();
174
	return $email;
175
}
176
 
177
sub getUserDerbyName {
178
	my $RCid = shift;
179
	my $sth = $dbh->prepare("select derby_name from official where RCid = ?");
180
	$sth->execute($RCid);
181
	my ($dname) = $sth->fetchrow_array();
182
	return $dname;
183
}
184
 
185
sub getYears {
186
#	my $sth = $dbh->prepare("select distinct year(date) from v_shift_admin_view union select year(now())");
187
#	$sth->execute();
188
	my @years = ("2022");
189
#	while (my ($y) =$sth->fetchrow_array()) { push @years, $y; }
190
 
191
	return \@years;
192
}
193
 
194
sub printRCHeader {
195
	my $PAGE_TITLE = shift;
196
	my $loggedinas = $ORCUSER ? "Currently logged in as: $ORCUSER->{derby_name}" : "";
197
 
198
	print<<rcheader;
199
<html><head><title>Officials' RollerCon Schedule Manager - $PAGE_TITLE</title>
200
<link rel="stylesheet" type="text/css" href="/rollercon.css">
201
</head>
202
<body text="#000000" bgcolor="#FFFFFF" link="#0000EE" vlink="#551A8B" alink="#FF0000">
203
<TABLE>
204
	<TR class="nostripe">
205
		<TD align=right><img SRC="/logo.jpg"></TD>
206
		<TD align=center valign=middle><b><font size=+3>Officials' RollerCon<br>Schedule Manager<br>$PAGE_TITLE</FONT></b>
207
		<p align=right><font size=-2>$loggedinas <a href='index.pl' onClick="document.cookie = 'RCAUTH=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';return true;">[Log Out]</a></font></TD>
208
	</TR>
209
 
210
rcheader
211
}
212
 
213
sub changeShift {
214
	my ($change, $game_id, $role, $user_id) = @_;
215
	my $countbypass = 0;
216
 
217
	my $sth = $dbh->prepare("select type from game where id = ?");
218
	$sth->execute($game_id);
219
	my ($game_type) = $sth->fetchrow_array;
220
 
221
	if (lc($user_id) ne lc($ORCUSER->{RCid})) {
222
	  if ($ORCUSER->{access} < 2) {
223
	    print "<br>Denied! You are not authorized to change someone else's schedule.<br>\n";
224
	    logit($ORCUSER->{RCid}, "Unauthorized attempt to change someone else's schedule. ($change, $game_id, $role, $user_id)");
225
	    return;
226
	  } else {
227
	    logit($ORCUSER->{RCid}, "$ORCUSER->{derby_name} changed someone else's schedule. ($change, $game_id, $role, $user_id)");
228
	    $countbypass = 1;
229
	  }
230
	}
231
 
232
	my $MAXSHIFTS = $game_type eq "clinic" ? getSetting("MAX_CLINIC_SIGNUPS") : $game_type eq "observation" ? getSetting("MAX_OBS_SIGNUPS") : getSetting("MAX_SHIFT_SIGNUP_PER_DAY");
233
#	my $MAXSHIFTS = getSetting("MAX_SHIFT_SIGNUP_PER_DAY");
234
 
235
	my $daily_count = signUpCount('get', $user_id, $game_type);
236
	if ($change eq "add" and $daily_count >= $MAXSHIFTS and !$countbypass) {
237
		print "<br>Denied! You may only sign up for $MAXSHIFTS $game_type shifts in one day!<br>\n";
238
		return;
239
	}
240
 
241
	if ($change eq "add" and ($daily_count < $MAXSHIFTS or $countbypass)) {
242
		$sth = $dbh->prepare("insert into assignment (Gid, role, RCid) values (?, ?, ?)");
243
	} elsif ($change eq "del") {
244
		$sth = $dbh->prepare("delete from assignment where Gid = ? and role = ? and RCid= ?");
245
	}
246
	print "<br>attempting to make DB changes...<br>";
247
	if ($sth->execute($game_id, $role, $user_id)) {
248
		$daily_count = signUpCount($change, $user_id, $game_type) unless $countbypass;
249
		logit($user_id, "Shift ".ucfirst($change).": $game_id -> $role");
250
		if ($game_type eq "clinic") {
251
	 		print "Success!...<br>You've signed up for $daily_count clinic shifts (you're currently allowed to sign up for $MAXSHIFTS total).<br>\n";
252
		} elsif ($game_type eq "observation") {
253
	 		print "Success!...<br>You've signed up for $daily_count clinic observation game (you're currently allowed to sign up for $MAXSHIFTS total).<br>\n";
254
		} else {
255
  		print "Success!...<br>You've signed up for $daily_count challenge / scrimmage shifts today (you're currently allowed to sign up for $MAXSHIFTS per day).<br>\n";
256
  	}
257
	} else {
258
    print "<br><b>You did not get the shift</b>, most likely because someone else took it while you were looking.<br>\nERROR: ", $sth->errstr();
259
	}
260
}
261
 
262
sub signUpCount {
263
	my $action = shift;
264
	my $id = shift;
265
	my $gtype = shift // "";
266
 
267
	if ($gtype ne "clinic" and $gtype ne "observation" and $id eq $ORCUSER->{RCid}) {
268
		if ($action eq 'add') {
269
			if (signUpCount('get', $id)) {
270
				$dbh->do("update sign_up_count set sign_ups = sign_ups + 1 where date = curdate() and RCid = $id");
271
			} else {
272
				$dbh->do("replace into sign_up_count values (curdate(), $id, 1)");
273
			}
274
		} elsif ($action eq 'del') {
275
			if (signUpCount('get', $id)) {
276
				$dbh->do("update sign_up_count set sign_ups = sign_ups - 1 where date = curdate() and RCid = $id");
277
			}
278
		}
279
	}
280
 
281
	my $get;
282
	if ($gtype eq "clinic") {
283
		$get = $dbh->prepare("select count(*) from v_shift where RCid = ? and gtype = 'clinic' and date >= '2019'");
284
	} elsif ($gtype eq "observation") {
285
		$get = $dbh->prepare("select count(*) from v_shift where RCid = ? and gtype = 'observation' and date >= '2019'");
286
	} else {
287
		$get = $dbh->prepare("select sign_ups from sign_up_count where RCid = ? and date = curdate()");
288
	}
289
	$get->execute($id);
290
	my ($R) = $get->fetchrow_array();
291
 
292
	return $R ? $R : '0';
293
}
294
 
295
sub signUpEligible {
296
	my $user = shift;
297
	my $t = shift;
298
 
299
  if (findConflict($user->{RCid}, $t->{id})) { return 0; }
300
 
301
	if (!exists $user->{sign_ups_today}) {
302
		$user->{sign_ups_today} = signUpCount('get', $user->{RCid});
303
	}
304
	if (!exists $user->{clinic_sign_ups}) {
305
		$user->{clinic_sign_ups} = signUpCount('get', $user->{RCid}, "clinic");
306
	}
307
	if (!exists $user->{obs_sign_ups}) {
308
		$user->{obs_sign_ups} = signUpCount('get', $user->{RCid}, "observation");
309
	}
310
 
311
	if ($t->{gtype} eq "clinic") {
312
	  # Uncomment to open clinic games to everyone...
313
     return 1;
314
    #---------------------------------
315
		if ($user->{clinic_pass} and $user->{clinic_sign_ups} < getSetting("MAX_CLINIC_SIGNUPS")) {
316
			return 1;
317
		} else {
318
			return 0;
319
		}
320
	} elsif ($t->{gtype} eq "observation") {
321
	  # Uncomment to open observation games to everyone...
322
    # return 1;
323
    #---------------------------------
324
		if ($user->{clinic_pass} and $user->{obs_sign_ups} < getSetting("MAX_OBS_SIGNUPS")) {
325
			return 1;
326
		} else {
327
			return 0;
328
		}
329
	} elsif ($t->{gtype} ne "selected staffing" and $t->{gtype} ne "short track" and $user->{sign_ups_today} < getSetting("MAX_SHIFT_SIGNUP_PER_DAY")) {
330
		return 1;
331
	} else {
332
		return 0;
333
	}
334
 
335
}
336
 
337
sub findConflict {
338
  my $rcid = shift;
339
  my $gid = shift;
340
  my $conflicts;
341
 
342
  # Are they already signed up for this game?
343
  my $sth0 = $dbh->prepare("select count(*) from v_shift where id = ? and RCid = ?");
344
  $sth0->execute($gid, $rcid);
345
  ($conflicts) = $sth0->fetchrow_array;
346
  if ($conflicts) { return 1; }
347
 
348
  # Are they signed up for any games that would conflict with this one?
349
#  my $sth = $dbh->prepare("select count(*) from v_shift where id in (select id from game where date = (select date from game where id = ?) and ((end_time > (select time from game where id = ?) and end_time < (select end_time from game where id = ?)) or (time > (select time from game where id = ?) and time < (select end_time from game where id = ?)) or (time < (select time from game where id = ?) and end_time > (select end_time from game where id = ?)))) and RCid = ?");
350
  my $sth = $dbh->prepare("select count(*) from v_shift where id in (select id from game where date = (select date from game where id = ?) and ((time <= (select time from game where id = ?) and end_time > (select time from game where id = ?)) or (time > (select time from game where id = ?) and time < (select end_time from game where id = ?)))) and RCid = ?");
351
#  $sth->execute($gid, $gid, $gid, $gid, $gid, $gid, $gid, $rcid);
352
  $sth->execute($gid, $gid, $gid, $gid, $gid, $rcid);
353
  ($conflicts) = $sth->fetchrow_array;
354
 
355
  return $conflicts;
356
}
357
 
358
sub changeLeadShift {
359
	my ($change, $lshift, $user_id) = @_;
360
	my $ERRMSG;
361
 
362
	my $sth = $dbh->prepare("update lead_shift set assignee_id = ? where id = ?");
363
 
364
	print "<br>attempting to make DB changes...<br>";
365
	if ($change eq "add") {
366
		$sth->execute($user_id, $lshift)
367
    	or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
368
	} elsif ($change eq "del") {
369
		$sth->execute('', $lshift)
370
    	or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
371
	}
372
	if ($ERRMSG) {
373
		print $ERRMSG;
374
	} else {
375
		logit($user_id, "Lead Shift ".ucfirst($change).": $lshift");
376
  	print "Success.<br>";
377
  }
378
}
379
 
380
sub logit {
381
	my $RCid = shift;
382
	my $msg = shift;
383
	my $sth = $dbh->prepare("insert into log (RCid, event) values (?, ?)");
384
	$sth->execute($RCid, $msg);
385
}
386
 
387
1;