#!/usr/bin/perl # check-reg.cgi - check registration use CGI qw/:standard :html3 :netscape/; use CGI::Carp qw(fatalsToBrowser carpout); use HTML::Entities (); # $encoded = HTML::Entities::encode($a); # $decoded = HTML::Entities::decode($a); use vars qw { $DEBUG }; $DEBUG = 0; use vars qw{ @REQUIRED @OPTIONAL @HIDDEN @VIEWABLE}; @REQUIRED = qw/ Last_name TTitle First_name Email /; @OPTIONAL = qw/ Enum EmpType NoNumber Phone Fax URL Address CCompany CTitle /; @HIDDEN = qw / EmpNum /; use vars qw { $Enum, $enum, $EmpType, $Last_name }; use vars qw {$ContactDir $ContactFile $SequenceNumbers}; $webdir = '/WWW/technofile/Talumni'; $ContactDir = "$webdir/Contacts"; $ContactFile = "$webdir/contactData.htm"; $SequenceNumbers = "$webdir/NextInSequence"; #main { chdir($webdir); print header, start_html(-title=>'Taligent Alumni Address Book Registration', -background=>"", -bgcolor=>"white", ), h1("Taligent Alumni Address Book Registration"); $_ = param('action'); CASE: { /^search|modify/i and do { my $found = do_checks('search'); generate_regform('create') if ($found == 0); last CASE; }; /^retrieve/i and do { do_checks('retrieve'); generate_regform(); last CASE; }; /^create/i and do { my $found = do_checks('create'); generate_regform('create') if ($found == 0); last CASE; }; /^update/i and do { generate_regform(); last CASE; }; /^register/i and do { create_record(); last CASE; }; # default generate_checkform(); } print end_html; } sub do_checks { my $options = shift; my @contact_list; my $found; my $record; my $empnumfile; my $Enum = param('Enum'); my $EmpType = param('EmpType'); my $Last_name = param('Last_name'); my $First_name = param('First_name'); my $NoNumber = param('NoNumber'); my $TTitle = param('TTitle'); my $Phone = param('Phone'); my $Fax = param('Fax'); my $Email = param('Email'); my $URL = param('URL'); my $Address = param('Address'); my $CCompany = param('CCompany'); my $CTitle = param('CTitle'); if ($options =~ /update/) { $found = 1; } else { unless ($Last_name && $First_name) { print_warning("Please enter First and Last name " . "for search"); generate_checkform(); return (-1); } else { $grep_cmd = "grep -il '^

$Last_name, $First_name

' "; } @contact_list = `$grep_cmd $ContactDir/*.htm`; unless ($NoNumber || $Enum eq '') { $EmpNum = normalize_empnum($Enum, $EmpType); $empnumfile = "$ContactDir/${EmpNum}.htm"; } $found = scalar(@contact_list); } if ($found == 0) { my $empnum = $EmpNum ? $EmpNum : 'unknown'; unless ($options =~ /create/) { print_warning("No records found for $Last_name, $First_name"); print_warning("You can try again to find your record, or create a " . "new record for this employee number.", p); if ($options =~ /search/) { generate_checkform('trycreate'); } else { generate_checkform('tryagain'); } } return ($found); } $found = 0; foreach my $filename (@contact_list) { chomp($filename); next unless (-e $filename); if (-e $empnumfile) { next unless ($filename eq $empnumfile); } $found++; ($Enum = $filename) =~ s/$ContactDir\///; $Enum =~ s/\.htm//; $Enum =~ m/^([TCX])/; if ($1 =~ /C/) { $EmpType='Contractor'; } else { $EmpType='Permanent'; } $EmpNum = normalize_empnum($Enum, $EmpType); $record = read_record($filename, 'nohr'); $records{$EmpNum} = $record; $emptype{$EmpNum} = $EmpType; $emptype{$EmpNum} = $EmpType; $mod_date{$EmpNum} = mod_date($filename); } if ($found > 1) { print p, "

Found: $found records

"; print p, "If one of these records matches, re-enter your lookup " . "information, plus employee number, below.", p, "If no record matches, click \"Create new Record\"."; param(-name=>'NoNumber', -value=>""); generate_checkform('retrieve'); foreach $EmpNum (sort (keys %records)) { print p, "

Found: $Last_name, $First_name

" . "

$EmpNum ($EmpType)

" . hr({-width=>"150", -align=>'Left'}), p, $records{$EmpNum}, p, hr({-width=>"400", -align=>'Left'}), p; } } elsif ($found == 1) { print p, "

Found: $Last_name, $First_name

" . "

$EmpNum ($EmpType)

" . "Last modified: $mod_date{$EmpNum}", hr({-width=>"150", -align=>'Left'}), p; print $record; if ($options =~ /create/) { # Switch to search mode; this won't be a new record $options = 'search'; } if ($record =~ m{^

(.*?),\s(.*?)

(.*?)

}x ) { $Last_name = $1; $First_name = $2; $TTitle = $3; } if ($record =~ m{Current\sEmployment: (.*?)\s-\s(.*?)
}x ) { $CTitle = $1; $CCompany = $2; } if ($record =~ m|Phone:\s(.*?)
|) { $Phone = $1; } if ($record =~ m|Fax:\s(.*?)
|) { $Fax = $1; } if ($record =~ m|Email:(.*?)
|) { $Email = $1; if ($Email =~ m{ [A-z0-9\@\.]+ }x ) { $Email = $1; } } if ($record =~ m|URL:(.*?)
|) { $URL = $1; if ($URL =~ m{.*}) { $URL = $1; } } if ($record =~ m|Address:(.*?)
|) { $Address = $1; } if ($DEBUG) { print << "EOT";

Last_name = $Last_name
First_name = $First_name
TTitle = $TTitle
CTitle = $CTitle
CCompany = $CCompany
Phone = $Phone
Fax = $Fax
Email = $Email
URL = $URL
Address = $Address
EOT } print "\n"; if ($options =~ /search/) { print start_form(-method=>'POST'); } param(-name=>'Enum', -value=>"$Enum"); param(-name=>'EmpNum', -value=>"$EmpNum"); param(-name=>'EmpType', -value=>"$EmpType"); param(-name=>'Last_name', -value=>"$Last_name"); param(-name=>'First_name', -value=>"$First_name"); param(-name=>'TTitle', -value=>"$TTitle"); param(-name=>'CCompany', -value=>"$CCompany"); param(-name=>'CTitle', -value=>"$CTitle"); param(-name=>'Phone', -value=>"$Phone"); param(-name=>'Fax', -value=>"$Fax"); param(-name=>'Email', -value=>"$Email"); param(-name=>'URL', -value=>"$URL"); param(-name=>'Address', -value=>"$Address"); print hr({-width=>'150', -align=>'left'}); if ($options =~ /search/) { print p, submit(-name=>'action', -value=>'Update record'), p, submit(-name=>'action', -value=>'Modify Search'), ; } } else { if ($empnumfile) { print_warning("Found Record for employee number: $EmpNum " . "but name does not match $Last_name, $First_name."); print_warning("You can try again to find your record, or " . "create a new record.", p); param(-name=>'Enum', -value=>''); if (($options =~ /create/) || ($options =~ /search/)) { generate_checkform(); } else { generate_checkform('tryagain'); } return(-1); } } foreach (@REQUIRED, @OPTIONAL, @HIDDEN) { print hidden(-name=>$_); } print end_form; return($found); } sub normalize_empnum { my $EmpNum = shift; my $EmpType = shift; my $options = shift; my $type = ''; $EmpNum =~ s/^([TCX])//; $t = $1; $EmpNum = sprintf("%04d", $EmpNum); if ($options =~ /X/) { # new number $EmpNum = "X$EmpNum"; } elsif ($EmpType eq 'Permanent' || $t eq 'T') { $EmpNum = "T$EmpNum"; } elsif ($EmpType eq 'Contractor' || $t eq 'C') { $EmpNum = "C$EmpNum"; } return $EmpNum; } sub generate_checkform { my $options = shift; if ($options =~ /retrieve/) { print "Note: This is the Taligent Address Book registration page.", br, "This is not the way to sign up ", "for the mailing list or to change your address for the ", "mailing list!", br, "To subscribe or unsubscribe from the mailing list, go to the ", "", "mailing list page! ", p, hr, p; print "Enter your employee number and name", br, "Then click \"Retrieve My Record\""; print_warning("* Fields in red are required"); } elsif ($options =~ /tryagain/) { print "Check spelling. " . "Could you have registered under " . "a different variation on your name?)

\n"; if (defined param('EmpNum')) { print "Could you be mistaken about the employee number? " . "Try the search without entering an employee number."; } } else { print "Note: This is the Taligent Address Book registration page.", br, "This is not the way to sign up ", "for the mailing list or to change your address for the ", "mailing list!", br, "To subscribe or unsubscribe from the mailing list, go to the ", "", "mailing list page!", p, hr, p; print "Enter your employee number and name (First and Last).", br, " If you can't remember your number, leave it blank and check " . "the box that says \"I've forgotten...\".", p, "If you already have a ", a({-href=>'./list_contacts.cgi'}, 'data record'), " click \"Search for Record\" " . "and we'll look it up.", br, "Otherwise, you can create a new record.", p; print_warning("* Fields in red are required"); } print "\n", start_form(-method=>'POST'), table( TR({-align=>LEFT}, td(checkbox(-name=>'NoNumber', -checked=>0, -value=>'checked', -label=>'I\'ve forgotten my employee #.')), ), TR({-align=>LEFT}, th({-align=>'right'}, font({-color=>'red'},'* First Name') ), td({-colspan=>'2'}, textfield(-name=>'First_name', -size=>25)) ), TR({-align=>LEFT}, th({-align=>'right'}, font({-color=>'red'},'* Last Name') ), td({-colspan=>'2'}, textfield(-name=>'Last_name', -size=>25)) ), TR({-align=>LEFT}, th({-align=>'right'},'Tal Employee#:'), td(textfield(-name=>'Enum', -size=>5)), td(popup_menu(-name=>'EmpType', -value=>['Permanent', 'Contractor'], -default=>'Permanent')), ), ), br; foreach (@REQUIRED, @OPTIONAL, @HIDDEN) { print hidden(-name=>$_) if defined($_); } if ($options =~ /retrieve/ ) { print submit(-name=>'action',-value=>'Retrieve My Record'), p, submit(-name=>'action',-value=>'Create New Record'); print hr({-width=>"150", -align=>'Left'}), p; } elsif ($options =~ /tryagain/ ) { print submit(-name=>'action',-value=>'Search for Record'), p, submit(-name=>'action',-value=>'Create New Record'); } elsif ($options =~ /trycreate/ ) { print submit(-name=>'action',-value=>'Search for Record'), p; print hr({-width=>"150", -align=>'Left'}), p; print p, "Or create a new record below " . "(A new psudo-employee number will be assigned)", p; } else { print submit(-name=>'action',-value=>'Search for Record'), p, submit(-name=>'action',-value=>'Create New Record'); } print end_form; } sub generate_regform { my $Enum = param('Enum'); my $EmpNum = param('EmpNum'); my $EmpType = param('EmpType'); my $Last_name = param('Last_name'); my $First_name = param('First_name'); my $TTitle = param('TTitle'); my $Phone = param('Phone'); my $Fax = param('Fax'); my $Email = param('Email'); my $URL = param('URL'); my $Address = param('Address'); my $CCompany = param('CCompany'); my $CTitle = param('CTitle'); # param(-name=>'PrevLast_name', -value=>"$Last_name"); # param(-name=>'PrevFirst_name', -value=>"$First_name"); unless ($EmpNum) { my $x = ($EmpType eq 'Permanent') ? 'T' : 'C'; $Enum = get_next_number() unless $Enum; $EmpNum = normalize_empnum($Enum, $EmpType, '$x'); param(-name=>'Enum', -value=>"$Enum"); param(-name=>'EmpNum', -value=>"$EmpNum"); } print h3("Employee Number: $EmpNum ($EmpType)"), p, h3("Name: $Last_name, $First_name"), p; print_warning("* Fields in red are required"); print start_form(-method=>'POST'); print table( TR({-align=>RIGHT}, th({-align=>'right'}, font({-color=>'red'},'* Taligent Title/ Job description *') ), td(textfield(-name=>'TTitle', -size=>30)), ), TR({-align=>RIGHT}, th({-align=>'right'}, font({-color=>'red'},'* Current Email Address *') ), td(textfield(-name=>'Email', -size=>30)), th({-align=>'right'},'URL'), td(textfield(-name=>'URL', -size=>30)), ), TR({-align=>RIGHT}, th({-align=>'right'},'Current Title'), td(textfield(-name=>'CTitle', -size=>30)), th({-align=>'right'},'Current Company'), td(textfield(-name=>'CCompany', -size=>30)), ), TR({-align=>RIGHT}, th({-align=>'right'},'Phone Number'), td(textfield(-name=>'Phone', -size=>30)), th({-align=>'right'},'Fax Number'), td(textfield(-name=>'Fax', -size=>30)), ), ), table( TR({-align=>RIGHT, -colspan=>4}, th({-align=>'right'},'Address'), td(textfield(-name=>'Address', -size=>60)) ), ), br; foreach (@REQUIRED, @OPTIONAL, @HIDDEN) { print hidden(-name=>$_); } print submit(-name=>'action',-value=>'Register'), reset(-name=>'Reset'), end_form; } sub LOCK_SH { 1 } sub LOCK_EX { 2 } sub LOCK_UN { 8 } sub read_record { my $file = shift; my $options = shift; my $fh = Lock($file,0); unless ($fh) { warn; return undef; } $record = <$fh>; UnLock($fh); if ($options =~ /nohr/) { $record =~ s/



$//; } return $record; } sub get_next_number { my $count; my $fh = Lock($SequenceNumbers,3); unless ($fh) { warn "Couldn't get lock"; return undef; } $count = <$fh>; $count++; seek ($fh, 0, 0); # rewind file print $fh $count; UnLock($fh); return $count; } sub Lock { # Because this function designate the Filehandle, it can only Lock one # file at a time my $path = shift; my $how = shift; my $read = 0; my $write = 1; my $append = 2; my $readwrite = 3; my ($lock_Job,$path_name,$description); if ($how == $append) { $lock_Job = LOCK_EX; $path_name = ">>$path"; $description = 'append'; } elsif ($how == $readwrite) { $lock_Job = LOCK_EX; $path_name = "+<$path"; $description = 'read/write'; } elsif ($how == $write) { $lock_Job = LOCK_EX; $path_name = ">$path"; $description = 'writing'; } else { $lock_Job = LOCK_SH; $path_name = $path; $description = 'reading'; } local($msg,$oldsig); my $handler = sub { $msg='timed out'; $SIG{ALRM}=$oldsig; }; ($oldsig,$SIG{ALRM}) = ($SIG{ALRM},$handler); alarm($TIMEOUT); open (FH,$path_name) or warn("Couldn't open $path for $description: $!"), return undef; # now try to lock it unless (flock (FH,$lock_Job)) { warn("Couldn't get lock for $description (" . ($msg || "$!") . ")"); alarm(0); close FH; return undef; } alarm(0); return \*FH; } sub UnLock { my $fh = shift; flock($fh,LOCK_UN) || die("Couldn't unlock $fh\n"); close $fh; } sub print_warning { $message = shift; print font({-color=>'red'}, "$message", em(join(', ',@_))), p; } sub check_missing { my (%p); grep (param($_) ne '' && $p{$_}++,@_); return grep(!$p{$_},@REQUIRED); } sub create_record { my $EmpNum = param('EmpNum'); my $EmpType = param('EmpType'); my $Last_name = param('Last_name'); my $First_name = param('First_name'); my $PrevLast_name = param('PrevLast_name'); my $PrevFirst_name = param('PrevFirst_name'); my $TTitle = param('TTitle'); my $Phone = param('Phone'); my $Fax = param('Fax'); my $Email = param('Email'); my $URL = param('URL'); my $Address = param('Address'); my $CCompany = param('CCompany'); my $CTitle = param('CTitle'); my $ContactFile = "${EmpNum}.htm"; my $newfile = 0; # Check for Empty Required Fields my @missing = check_missing(param()); if (@missing) { print "

Record not added

"; print "
"; print_warning("Required fields missing"); print "Make sure you have entered: ", p; print "
Taligent Title, current Email " . "Address
"; print "

Or return to the home page, " . "if you want.<\P>"; generate_regform(); return undef; } my $pwd = `pwd`; chdir($ContactDir); if (! -e $ContactFile) { $newfile = 1; } unless ($newfile) { unless ( -e "RCS/${ContactFile},v" ) { # Contact file never inited in RCS system("/usr/bin/ci -u -q -t-'New Contact' $ContactFile"); } system("/usr/bin/co -l -q $ContactFile"); } if (-w $ContactFile || $newfile) { open(FILE, ">$ContactFile") or die "Error: Cannot Open $ContactFile: $!"; printf FILE ( "

%s, %s

" . "%s

" . "

Contact Info

" . "Current Employment:%s - %s
" . "Phone: %s
" . "Fax: %s
" . "Email:%s
" . "URL:%s
" . "Address:%s
" . "

\n", $Last_name, $First_name, $TTitle, $CTitle, $CCompany, $Phone, $Fax, $Email, $Email, $URL, $URL, $Address ); close(FILE); } else { print_warning "ERROR: Cannot write to Contact file"; return undef; } if ($newfile) { system("/usr/bin/ci -u -q -t-'New Contact' $ContactFile"); } else { system("/usr/bin/ci -u -q -m'Modified' $ContactFile"); } $mod_date = mod_date($ContactFile); print "

Registration Successful


"; print "Your contact info has been added. "; print "Modified: $mod_date"; chdir($pwd); printf("

%s, %s

" . "%s

" . "

Contact Info

" . "Current Employment:%s - %s
" . "Phone: %s
" . "Fax: %s
" . "Email:%s
" . "URL:%s
" . "Address:%s
" . "

\n", $Last_name, $First_name, $TTitle, $CTitle, $CCompany, $Phone, $Fax, $Email, $Email, $URL, $URL, $Address ); print "
You can go to the "; print "Contact List or return to the "; print "Home Page."; } sub mod_date { # # return the modification time my($filename) = @_; my(@mtime, $mod); my $mod_date = 'unknown'; # print "'$filename'

\n"; if (-e $filename) { @mtime = localtime((stat($filename))[9]); $mod_date = sprintf("%02d-%02d-%04d\n", $mtime[4]+1, $mtime[3], $mtime[5]+1900); } return($mod_date); } # sub mod_date