Subversion Repositories VORC

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#!/usr/bin/perl -w
2
 
3
use strict;
4
use lib "/home/rollerco/perl5/lib/perl5";
5
use RollerCon;
6
use CGI;
7
use CGI::Cookie;
8
use DBI;
9
use Email::Valid;
10
 
11
my ($FORM, $cookie_string, $ERRMSG);
12
my @ERRORS;
13
my $dsn = "DBI:mysql:database=rollerco_data;host=localhost;port=3306";
14
my $dbh = DBI->connect($dsn, 'rollerco_www', 'www-data');
15
 
16
my $query = new CGI;
17
$FORM->{'SUB'} = $query->param('submit') || '';
18
$FORM->{'RCid'} = $query->param('RCid') || '';
19
if ($FORM->{'SUB'} eq '') {
20
	if ($ENV{'REQUEST_URI'}) {
21
		my ($g, $keep) = split /\?/, $ENV{'REQUEST_URI'};
22
		if ($keep) {
23
			foreach (split /&/, $keep) {
24
				my ($k, $v) = split /=/;
25
				$k =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
26
				$v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
27
				$k eq "submit" ? $FORM->{'SUB'} = $v : $FORM->{$k} = $v;
28
			}
29
		}
30
	}
31
}
32
 
33
my $goback;
34
if (!$ENV{HTTP_REFERER}) {
35
	$goback = "/schedule/";
36
} elsif ($ENV{HTTP_REFERER} !~ /manage_user.pl/) {
37
	$goback = $ENV{HTTP_REFERER};
38
} else {
39
	if ($FORM->{referer}) {
40
		$goback = $FORM->{referer};
41
	} else {
42
		$goback = "/schedule/";
43
	}
44
}
45
 
46
if ($FORM->{'SUB'} eq 'Save') {
47
	$FORM->{email}      = $query->param('email') || '';
48
	$FORM->{password}   = $query->param('password') || '';
49
	$FORM->{derby_name} = $query->param('derby_name') || '';
50
	$FORM->{real_name}  = $query->param('real_name') || '';
51
	$FORM->{phone}      = $query->param('phone') || '';
52
	$FORM->{level}      = $query->param('level') || '';
53
	$FORM->{type}       = $query->param('type') || '';
54
	$FORM->{RCid}         = $query->param('RCid') || '';
55
	$FORM->{access}			= $query->param('access') || 0;
56
	if (defined $query->param('clinic_pass')) {
57
		$FORM->{clinic_pass} = 1;
58
	} else {
59
		$FORM->{clinic_pass} = 0;
60
	}
61
 
62
#	$FORM->{clinic_pass}			= exists $query->param('clinic_pass') ? 1 : 0;
63
 
64
	if ($FORM->{RCid} eq "New") {
65
		if (!$FORM->{password})   { push @ERRORS, "Blank Password!"; }
66
		if (!$FORM->{real_name})  { push @ERRORS, "Blank Real Name!"; }
67
		if (!$FORM->{derby_name}) { $FORM->{derby_name} = $FORM->{real_name}; } # If they leave derby_name blank, use their real_name
68
		if (checkDupes('derby_name', $FORM->{derby_name})) { push @ERRORS, "Derby Name already in use. Pick a different one."; $FORM->{derby_name} = ""; }
69
		if (!$FORM->{level})      { $FORM->{level} = "B"; } # People keep leaving level blank.  Default 'em if they do.
70
		if (!$FORM->{type})       { $FORM->{type} = "official"; } # and now they left the other drop-down blank!!!
71
		if (!$FORM->{email})      { push @ERRORS, "Blank Email (User-ID)!"; } else {
72
			$FORM->{email} =~ s/\s+//g; # make sure people aren't accidentally including spaces
73
			$FORM->{email} = lc $FORM->{email}; # sometimes people capitalize their email addresses and that's annoying...
74
			if (! Email::Valid->address(-address => $FORM->{email}, -mxcheck => 1, -tldcheck => 1)) { push @ERRORS, "Mal-formatted (or fake) Email Address!"; $FORM->{email} = ""; }
75
		}
76
		if (checkDupes('email', $FORM->{email})) { push @ERRORS, "Email Address already in use. Pick a different one."; $FORM->{email} = ""; }
77
 
78
		if (scalar @ERRORS) {
79
			$ERRMSG = join "<br>", @ERRORS;
80
			$FORM->{'SUB'} = 'New User';
81
		} else {
82
			# We have a correctly formatted email address with a mail host record, go ahead and add the user
83
			my $sth = $dbh->prepare("insert into official (email, password, derby_name, real_name, phone, level, type, access, clinic_pass) values (?, password(?), ?, ?, ?, ?, ?, ?, ?)");
84
 
85
			$sth->execute($FORM->{email}, $FORM->{password}, $FORM->{derby_name}, $FORM->{real_name}, $FORM->{phone}, $FORM->{level}, $FORM->{type}, 0, 0);
86
 
87
			$sth = $dbh->prepare("select RCid from official where email = ?");
88
			$sth->execute($FORM->{email});
89
			($FORM->{RCid}) = $sth->fetchrow();
90
			logit($FORM->{RCid}, "New User Registration");
91
			sendEMail("New User", $FORM);
92
			$cookie_string = authenticate(1);
93
		}
94
	} else {
95
		$cookie_string = authenticate(1);
96
		my ($EM, $PWD, $AL) = split /&/, $cookie_string;
97
		if (lc($EM) eq lc($FORM->{email})) { # They're editing their own record.
98
 
99
			# Don't let users change their own clinic_pass setting...
100
		  $FORM->{clinic_pass} = getUser($EM)->{clinic_pass};
101
#		  $FORM->{clinic_pass} = $RollerCon::ORCUSER->{clinic_pass};
102
 
103
			if ($FORM->{password}) { # They've possibly included an updated password.
104
				my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, clinic_pass) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?)");
105
				$sth->execute($FORM->{RCid}, $EM, $FORM->{password}, $FORM->{derby_name}, $FORM->{real_name}, $FORM->{phone}, $FORM->{level}, $FORM->{type}, $FORM->{access}, $FORM->{clinic_pass})
106
					or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
107
			} else { # No password was included, just keep the existing one.
108
				my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, clinic_pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
109
				$sth->execute($FORM->{RCid}, $EM, $PWD, $FORM->{derby_name}, $FORM->{real_name}, $FORM->{phone}, $FORM->{level}, $FORM->{type}, $FORM->{access}, $FORM->{clinic_pass})
110
					or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
111
			}
112
			if ($ERRMSG) {
113
				logit($FORM->{RCid}, "DB ERROR: Updating Self Details: $ERRMSG");
114
			} else {
115
				logit($FORM->{RCid}, "Updated User Details");
116
			}
117
		} elsif ($AL > 1) { # A lead or higher is updating someone else's record
118
			if ($FORM->{password}) {
119
				my $sth = $dbh->prepare("replace into official (RCid, email, password, derby_name, real_name, phone, level, type, access, clinic_pass) values (?, ?, password(?), ?, ?, ?, ?, ?, ?, ?)");
120
				$sth->execute($FORM->{RCid}, $FORM->{email}, $FORM->{password}, $FORM->{derby_name}, $FORM->{real_name}, $FORM->{phone}, $FORM->{level}, $FORM->{type}, $FORM->{access}, $FORM->{clinic_pass})
121
					or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
122
			} else {
123
				my $sth = $dbh->prepare("update official set email = ?, derby_name = ?, real_name = ?, phone = ?, level = ?, type = ?, access = ?, clinic_pass = ? where RCid = ?");
124
				$sth->execute($FORM->{email}, $FORM->{derby_name}, $FORM->{real_name}, $FORM->{phone}, $FORM->{level}, $FORM->{type}, $FORM->{access}, $FORM->{clinic_pass}, $FORM->{RCid})
125
					or $ERRMSG = "ERROR: Can't execute SQL statement: ".$sth->errstr()."\n";
126
			}
127
			if ($ERRMSG) {
128
				logit($FORM->{RCid}, "DB ERROR: Updating Someone Else: $ERRMSG");
129
			} else {
130
				logit($FORM->{RCid}, "Updated User Details (by ".getUser($EM)->{derby_name}.")");
131
				logit(getUser($EM)->{RCid}, "Updated User Details: ".$FORM->{derby_name}." (".$FORM->{RCid}.")");
132
			}
133
		} else {
134
			$ERRMSG = "Attempting to update someone else's record, and you don't have permission to do that.";
135
			logit($FORM->{RCid}, "FAIL: $EM doesn't have access to update $FORM->{email}'s record");
136
		}
137
	}
138
	$FORM->{password} = "*******";
139
	$FORM->{buttons}		= "<INPUT type=hidden name=RCid value=$FORM->{RCid}><INPUT TYPE=submit name=submit value=Edit>";
140
	my $checked = $FORM->{clinic_pass} ? "checked" : "";
141
	$FORM->{clinic_pass}	= "<INPUT type='checkbox' name='clinic_pass' $checked disabled readonly>";
142
 
143
}
144
 
145
if ($FORM->{'SUB'} eq 'Edit') {
146
	$cookie_string = authenticate(1);
147
	my ($EM, $PWD, $AL) = split /&/, $cookie_string;
148
	my $sth = $dbh->prepare("select * from official where RCid = ?");
149
	$sth->execute($FORM->{RCid});
150
	$FORM = $sth->fetchrow_hashref();
151
	if (lc($EM) eq lc($FORM->{email}) or $AL > 1) {
152
		if (lc($EM) eq lc($FORM->{email}) or $AL < $FORM->{access}) {
153
			$FORM->{access}			= "<INPUT TYPE=hidden NAME=access VALUE='$FORM->{access}'>$FORM->{access}";
154
		} else {
155
			$FORM->{access}			= "<SELECT NAME=access>".selectOptions($FORM->{access}, [-1..$AL])."</SELECT>";
156
		}
157
		my $checked = $FORM->{clinic_pass} ? "checked" : "";
158
		if ($AL > 2) {
159
			$FORM->{clinic_pass} = "<INPUT type='checkbox' name='clinic_pass' value=$FORM->{clinic_pass} $checked>";
160
		} else {
161
			$FORM->{clinic_pass} = "<INPUT type='checkbox' name='clinic_pass' $checked disabled readonly>";
162
		}
163
		$FORM->{email}      = "<INPUT TYPE=text NAME=email VALUE='$FORM->{email}' readonly>";
164
		$FORM->{password}   = "<INPUT TYPE=password NAME=password VALUE=''>";
165
		$FORM->{derby_name} = "<INPUT TYPE=text NAME=derby_name VALUE=\"$FORM->{derby_name}\">";
166
		$FORM->{real_name}  = "<INPUT TYPE=text NAME=real_name VALUE='$FORM->{real_name}'>";
167
		$FORM->{phone}      = "<INPUT TYPE=text NAME=phone VALUE='$FORM->{phone}'>";
168
		$FORM->{level}      = "<SELECT NAME=level>".selectOptions($FORM->{level}, [qw(AA A B C)])."</SELECT>";
169
		$FORM->{type}       = "<SELECT NAME=type>".selectOptions($FORM->{type}, [qw(official nso referee)])."</SELECT>";
170
		$FORM->{RCid}         = "<INPUT TYPE=hidden NAME=RCid VALUE='$FORM->{RCid}'>$FORM->{RCid}&nbsp;";
171
		$FORM->{buttons}		= "<INPUT TYPE=submit name=submit value=Save> <INPUT TYPE=reset value=Reset> <INPUT TYPE=submit name=submit value=Cancel>";
172
	} else {
173
		$ERRMSG = "Attempting to update someone else's record, and you don't have permission to do that.";
174
	}
175
 
176
} elsif ($FORM->{'SUB'} eq 'New User') {
177
	# Skip authentication
178
	$FORM->{email}      = "<INPUT TYPE=text NAME=email VALUE='$FORM->{email}'>";
179
	$FORM->{password}   = "<INPUT TYPE=password NAME=password VALUE=''>";
180
	$FORM->{derby_name} = "<INPUT TYPE=text NAME=derby_name VALUE='$FORM->{derby_name}'>";
181
	$FORM->{real_name}  = "<INPUT TYPE=text NAME=real_name VALUE='$FORM->{real_name}'>";
182
	$FORM->{phone}      = "<INPUT TYPE=text NAME=phone VALUE='$FORM->{phone}'>";
183
	$FORM->{level}      = "<SELECT NAME=level>".selectOptions($FORM->{level}, ["", qw(AA A B C)])."</SELECT>";
184
	$FORM->{type}       = "<SELECT NAME=type>".selectOptions($FORM->{type}, ["", qw(official nso referee)])."</SELECT>";
185
	$FORM->{RCid}         = "<INPUT TYPE=hidden NAME=RCid VALUE='New'>TBD&nbsp;";
186
	$FORM->{access}			= "<INPUT TYPE=hidden NAME=access VALUE='0'>0";
187
	$FORM->{clinic_pass}	= "<INPUT type='checkbox' name='clinic_pass' disabled readonly>";
188
	$FORM->{buttons}		= "<INPUT TYPE=submit name=submit value=Save> <INPUT TYPE=reset value=Reset> <INPUT TYPE=submit name=submit value=Cancel>";
189
	$cookie_string = '';
190
} elsif ($FORM->{'SUB'} eq 'View' or $FORM->{'SUB'} eq 'Cancel' or $FORM->{'SUB'} eq '') {
191
	$cookie_string = authenticate(1);
192
	my ($EM, $PWD, $AL) = split /&/, $cookie_string;
193
 
194
	if ($FORM->{'SUB'} eq '') {
195
		my $sth = $dbh->prepare("select RCid from official where email = ?");
196
		$sth->execute($EM);
197
		($FORM->{'RCid'}) = $sth->fetchrow;
198
	}
199
 
200
	# Check to make sure they're only looking up their own ID unless they're a lead or higher
201
	my $currentuser = getUser($EM);
202
	if ($currentuser->{RCid} ne $FORM->{RCid} and $AL < 2) {
203
	  logit($currentuser->{RCid}, "SECURITY: $currentuser->{derby_name} attempted to view another user's ($FORM->{RCid}) info");
204
  	$FORM->{email}      = "&nbsp;";
205
  	$FORM->{password}   = "&nbsp;";
206
  	$FORM->{derby_name} = "&nbsp;";
207
  	$FORM->{real_name}  = "&nbsp;";
208
  	$FORM->{phone}      = "&nbsp;";
209
  	$FORM->{level}      = "&nbsp;";
210
  	$FORM->{type}       = "&nbsp;";
211
  	$FORM->{RCid}         = "&nbsp;";
212
  	$FORM->{access}			= "&nbsp;";
213
  	$FORM->{clinic_pass}	= "&nbsp;";
214
  	$FORM->{buttons}		= "&nbsp;";
215
	} else {
216
  	my $sth = $dbh->prepare("select * from official where RCid = ?");
217
  	$sth->execute($FORM->{'RCid'});
218
  	$FORM = $sth->fetchrow_hashref();
219
 
220
  	$FORM->{'password'} = "*******";
221
  	my $checked = $FORM->{clinic_pass} ? "checked" : "";
222
  	$FORM->{clinic_pass}	= "<INPUT type='checkbox' name='clinic_pass' value=$FORM->{clinic_pass} $checked disabled readonly>";
223
  }
224
 
225
	if (lc($EM) eq lc($FORM->{email}) || $AL > 1) {
226
		$FORM->{buttons} = "<INPUT TYPE=hidden name=RCid value=$FORM->{'RCid'}><INPUT TYPE=submit name=submit value=Edit>";
227
	} else {
228
		$FORM->{buttons} = "";
229
	}
230
} #else {
231
#	$cookie_string = authenticate(1);
232
#	$FORM->{email}      = "&nbsp;";
233
#	$FORM->{password}   = "&nbsp;";
234
#	$FORM->{derby_name} = "&nbsp;";
235
#	$FORM->{real_name}  = "&nbsp;";
236
#	$FORM->{phone}      = "&nbsp;";
237
#	$FORM->{level}      = "&nbsp;";
238
#	$FORM->{type}       = "&nbsp;";
239
#	$FORM->{RCid}         = "&nbsp;";
240
#	$FORM->{access}			= "&nbsp;";
241
#	$FORM->{clinic_pass}	= "&nbsp;";
242
#	$FORM->{buttons}		= "&nbsp;";
243
#}
244
 
245
 
246
my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
247
 
248
print CGI::header(-cookie=>$RCAUTH_cookie);
249
 
250
foreach (keys %ENV) {
251
#	print "$_: $ENV{$_}\n<br>";
252
}
253
 
254
if ($ERRMSG) {
255
	$ERRMSG = "<TR><TD colspan=2><FONT color=red><B>".$ERRMSG."</B></FONT></TD></TR>";
256
} else {
257
	$ERRMSG = "";
258
}
259
 
260
printRCHeader("User Manager");
261
print<<body;
262
$ERRMSG
263
<form action="$ENV{SCRIPT_NAME}" method=POST name=Req>
264
<input type=hidden name=referer value="$goback">
265
	<TR><TD align=right colspan=2>&nbsp;</TD></TR>
266
	<TR>
267
		<TD align=right>User-ID / Email Address: </TD>
268
		<TD align=left>$FORM->{email}</TD>
269
	</TR>
270
	<TR>
271
		<TD align=right>Password: </TD>
272
		<TD align=left>$FORM->{password}</TD>
273
	</TR>
274
	<TR>
275
		<TD align=right>Derby Name: </TD>
276
		<TD align=left>$FORM->{derby_name}</TD>
277
	</TR>
278
	<TR>
279
		<TD align=right>Real Name: </TD>
280
		<TD align=left>$FORM->{real_name}</TD>
281
	</TR>
282
	<TR>
283
		<TD align=right>Phone: </TD>
284
		<TD align=left>$FORM->{phone}</TD>
285
	</TR>
286
	<TR>
287
		<TD align=right>Experience Level: </TD>
288
		<TD align=left>$FORM->{level}</TD>
289
	</TR>
290
	<TR>
291
		<TD align=right>type: </TD>
292
		<TD align=left>$FORM->{type}</TD>
293
	</TR>
294
	<TR>
295
		<TD align=right>Database ID: </TD>
296
		<TD align=left>$FORM->{RCid}</TD>
297
	</TR>
298
	<TR>
299
		<TD align=right>Access Level: </TD>
300
		<TD align=left>$FORM->{access}</TD>
301
	</TR>
302
	<TR>
303
		<TD align=right>Clinic Pass: </TD>
304
		<TD align=left>$FORM->{clinic_pass}</TD>
305
	</TR>
306
	<TR><TD colspan=2>&nbsp</TD></TR>
307
	<TR>
308
		<TD align=right><A HREF="/schedule/index.pl">[go home]</a>&nbsp<A HREF="$goback">[go back]</a></TD>
309
		<TD align=left>$FORM->{buttons}</TD>
310
	</TR>
311
</FORM>
312
</TABLE>
313
 
314
body
315
 
316
 
317
sub selectOptions {
318
	my $selectedOption = shift;
319
	my $options = shift;
320
	return join " ", map { $selectedOption eq $_ ?
321
													"<OPTION value='$_' selected>$_</OPTION>" :
322
													"<OPTION value='$_'>$_</OPTION>"
323
						 					} @$options;
324
}
325
 
326
sub sendEMail {
327
	my $context = shift;
328
	my $data = shift;
329
	use RCMailer;
330
 
331
	my $email = $data->{email};
332
	my $subject = 'Officiating RollerCon Schedule Manager - New User Request';
333
	my $body = "Greetings,
334
 
335
It appears as though you've registered a new account to Officiate at RollerCon with the following information:
336
 
337
		Derby Name: $data->{derby_name}
338
		Real Name: 	$data->{real_name}
339
		Email Address: $data->{email}
340
		Phone: $data->{phone}
341
		Type: $data->{type}
342
		Level:  $data->{level}
343
 
344
Please be patient while our Admins are reviewing your account request.  Each user is manually approved to help ensure robots, spiders, and shift hoggers don't get in.
345
 
346
YOU WILL NOT BE ABLE TO LOG IN UNTIL YOU RECEIVE ANOTHER EMAIL STATING YOUR ACCOUNT REQUEST HAS BEEN APPROVED!
347
 
348
Once approved, you'll be able to log in and view the schedule and sign up for shifts.  Please be considerate of others and don't hogger all of the shifts.  If you do, we will find you and randomly drop your shifts.
349
 
350
http://officials.rollercon.com/schedule/
351
 
352
If you didn't make this request, well, you're still the only one who received this email, and you now have an account request.  You should probably let us know that someone is messing with you.
353
 
354
-RollerCon Officiating Management
355
";
356
	# send the message
357
	EmailUser($email, $subject, $body);
358
 
359
}
360
 
361
sub checkDupes {
362
  my $field = shift;
363
  my $nametocheck = shift;
364
  my $han = $dbh->prepare("select RCid from official where $field = ?");
365
  $han->execute($nametocheck);
366
  my ($rcid) = $han->fetchrow();
367
  return $rcid;
368
}
369