\n";
return $return;
} #end of small_calendar
#####################################################################
# Print out a small calendar
# Linked dates to view date's details
#####################################################################
sub small_calendar2 {
my $month = shift;
my $year = shift;
my $current_date = shift;
my $month_name = $months[$month-1];
my $bgcolor;
&PerpetualCalendar($month,1,$year);
$start_day = $perp_dow;
$days_in_month = $perp_eom;
$curr_day = 0;
my $return = <<"END";
${month_name} $year
END
foreach $date (1-$start_day .. $days_in_month) {
if ($curr_day == 0) { $return .= "
";
}
# Put the generated output back into the template
$template =~ s|<%OUTPUT DAY%>(.+?)<%/OUTPUT%>|$output|si;
# Put in the rest of the variables
# --------------------------------
$vars{'session'} = $sessionID;
$template = &parse_template($template, \%vars);
# Remove special comments from template
# -------------------------------------
$template =~ s|\r?\n?||sig;
print $template;
exit;
} #end of DISPLAY
#####################################################################
sub userlogged {
my $Session = $_[0];
$user_data_database = 'userdbfile.psv';
open(USERDB,"$user_data_database");
@AllUsers = ;
close USERDB;
$TopLine = $AllUsers[0];
shift @AllUsers;
my $Counter = 0;
my $user;
foreach $user (@AllUsers) {
chomp $user;
my ($login, $password, $sessionID, $login_time) = split(/\|/,$user,4);
if ($Session eq $sessionID) {
if (($login_time + 3600) > time) {
return(1);
last;
}
else {
$AllUsers[$Counter] = join('|',$login,$password,'',"\n");
open(USERS, ">$user_data_database");
print USERS $TopLine;
print USERS @AllUsers;
close USERS;
return(0);
}
}
$Counter++;
}
return(0);
}
#####################################################################
#
# DO_ADD()
#
# Add an entry
#
#####################################################################
sub DO_ADD {
$UserName = &GetUserFromSession($in{session});
&read_cal_data;
$in{description} =~ s|[\r\n]+| |gs;
$Month = $in{Month};
$in{Month} =~ s|^(\d)$|0$1|;
$in{Date} =~ s|^(\d)$|0$1|;
my $datestamp = $in{Year} . $in{Month} . $in{Date};
if ($datestamp =~ /^\d\d\d\d\d\d\d\d$/) {
open(OUT,">> $vars{calendar_file}") || &Error("Can't open $calendar_file for writing!");
eval "flock OUT,2";
print OUT "$vars{new_id}|$datestamp|$in{heading}|$in{description}|$UserName\n";
close(OUT);
}
else { &Error("error"); }
print <<"END";
Your entry has been added. You may now Return to the calendar.
END
# &DISPLAY;
exit(0);
} #end of ADD
#####################################################################
sub GetUserFromSession {
my $Session = $_[0];
$user_data_database = 'userdbfile.psv';
open(USERDB,"$user_data_database");
my @AllUsers = ;
close USERDB;
$TopLine = $AllUsers[0];
shift @AllUsers;
my $Counter = 0;
my $user;
foreach $user (@AllUsers) {
chomp $user;
my ($login, $password, $sessionID, $login_time) = split(/\|/,$user,4);
if ($Session eq $sessionID) {
if (($login_time + 3600) > time) {
return($login);
last;
}
else {
print "Your Session Has Expired, Please Login Again.";
$AllUsers[$Counter] = join('|',$login,$password,'',"\n");
open(USERS, ">$user_data_database");
print USERS $TopLine;
print USERS @AllUsers;
close USERS;
exit(0);
}
}
$Counter++;
}
print "Your Session Has Expired, Please Login Again.";
exit(0);
}
#####################################################################
#
# VIEWDAY()
#
# Display entries for a single day
#
#####################################################################
sub VIEWDAY {
&read_cal_data;
$vars{small_calendar} = &small_calendar2( $vars{month} , $vars{year} , $vars{date} );
&PerpetualCalendar(int($vars{'month'}),1,int($vars{'year'}));
# Load the template and do the replace
# ------------------------------------
$template = &load_template("$vars{viewday_html}");
$template = &parse_output( "EVENTS" , $template , \%{$events{$vars{datestamp}}} );
$template = &parse_template($template, \%vars);
print $template;
exit;
} # end of VIEWDAY
#####################################################################
#
# Initialize variables
#
#####################################################################
sub INITIALIZE {
my $x,$i,$neg_pos,$hours;
# Current date and stuff
$time = time;
($neg_pos,$hours) = ($vars{time_offset} =~ /([+-])(\d+)/);
if ($neg_pos eq '+') {
$time += (60*60 * $hours);
}
else {
$time -= (60*60 * $hours);
}
($mday,$month,$year) = (localtime($time))[3,4,5];
$month = $month+1;
$year = $year+1900;
$datestamp = sprintf("%4.4d%2.2d%2.2d",$year,$month,$mday);
}
#####################################################################
#
# Read in calendar data file
#
#####################################################################
sub read_cal_data {
undef %events;
my $id,$xdatestamp, $xmonth;
$vars{new_id}=0;
open(IN,"$vars{calendar_file}") || &Error("Can't open $vars{calendar_file}");
my $header = ;
# If file isnt in right format, give error
unless ($header =~ m/^#id\|datestamp\|label\|description\|username/) {
close(IN);
print "The events file is not in the correct format for this version. Run the calendar_admin script to automatically update the format.";
exit(0);
}
while () {
chomp;
next unless /^\d/;
($id,$xdatestamp,$label,$desc,$username) = split(/\|/,$_,5);
$xdatestamp =~ s|^0000|$vars{year}|;
if ($id > $vars{new_id}) { $vars{new_id} = $id; }
# Skip it unless it's from the current month
($xmonth = $vars{month}) =~ s|^(\d)$|0$1|;
next unless ($xdatestamp =~ m|^$vars{year}$xmonth|);
if ($vars{html_heading} ne "yes") {
$label =~ s|[<>]||g;
}
if ($vars{html_description} ne "yes") {
$desc =~ s|[<>]||g;
}
$total_events{$xdatestamp}++;
${$events{$xdatestamp}}{$total_events{$xdatestamp}}{description} = $desc;
${$events{$xdatestamp}}{$total_events{$xdatestamp}}{label} = $label;
${$events{$xdatestamp}}{$total_events{$xdatestamp}}{id} = $id;
${$events{$xdatestamp}}{$total_events{$xdatestamp}}{username} = $username;
}
close(IN);
$vars{new_id}++;
}
#####################################
# show editable events
#####################################
sub show_editable_events {
# show the editable events
my $x;
$x = $vars{month};
$monthselected{$x} = " SELECTED ";
$x = $vars{year};
$yearselected{$x} = " SELECTED ";
print <<"END";
Calendar Administration
Edit Events
END
exit(0);
}
#####################################
sub do_edit {
# Get ID of button pressed
foreach (keys %in) { if (/^ID(\d+)/) { $id = $1; } }
if ($in{"ID$id"} eq "Edit") {
&EDITFORM($id);
}
elsif ($in{"ID$id"} eq "Delete") {
&DELETE($id);
}
else { &ADMIN; }
exit(0);
}
######################################
# print the form to edit an event
######################################
sub EDITFORM {
$vars{id} = shift;
open(IN,"$vars{calendar_file}") || &Error( "Can't open $vars{calendar_file}");
my $header = ;
while () {
chomp;
($id,$xdatestamp,$label,$description,$username) = split(/\|/,$_,5);
next unless ($id == $vars{id});
($year,$month,$date) = ($xdatestamp =~ /(\d\d\d\d)(\d\d)(\d\d)/);
$description =~ s| |\n|gis;
$description =~ s|"|"|gs;
$label =~ s|"|"|gs;
last;
}
close(IN);
print <<"END";
Calendar
$ADMIN_PAGE_TOP
$ADMIN_PAGE_BOTTOM
END
exit(0);
}
############################################
# delete the selected event from the caledar
############################################
sub DELETE {
$in{id} = shift;
my %events;
open(IN,"$vars{calendar_file}") || &Error( "Can't open $vars{calendar_file}");
my $header = ;
while () {
chomp;
($id,$xdatestamp,$label,$description,$username) = split(/\|/,$_,5);
next if ($id == $in{id});
$events{$id}{datestamp} = $xdatestamp;
$events{$id}{label} = $label;
$events{$id}{description} = $description;
$events{$id}{username} = $username;
}
close(IN);
open(OUT,"> $vars{calendar_file}") || &Error( "Can't open $calendar_file for writing!");
eval "flock OUT,2";
print OUT "#id|datestamp|label|description|username\n";
foreach $id ( sort {$a <=> $b} keys %events ) {
print OUT "$id|$events{$id}{datestamp}|$events{$id}{label}|$events{$id}{description}|$events{$id}{username}\n";
}
close(OUT);
$vars{'message'} = "Event Deleted";
undef %events;
&read_cal_data;
&show_editable_events;
exit(0);
}
#####################################
sub save_edit {
# save the edited event, go back to editable events screen
my %events;
$in{description} =~ s|[\r\n]+| |gs;
$in{month} =~ s|^(\d)$|0$1|;
$in{date} =~ s|^(\d)$|0$1|;
my $datestamp = $in{year} . $in{month} . $in{date};
open(IN,"$vars{calendar_file}") || &Error( "Can't open $vars{calendar_file}");
my $header = ;
while () {
chomp;
($id,$xdatestamp,$label,$description,$username) = split(/\|/,$_,5);
$events{$id}{datestamp} = $xdatestamp;
$events{$id}{label} = $label;
$events{$id}{description} = $description;
$events{$id}{username} = $username;
}
close(IN);
$events{$in{id}}{datestamp} = $datestamp;
$events{$in{id}}{label} = $in{heading};
$events{$in{id}}{description} = $in{description};
$events{$in{id}}{username} = &GetUserFromSession($in{session});
open(OUT,"> $vars{calendar_file}") || &Error( "Can't open $calendar_file for writing!");
eval "flock OUT,2";
print OUT "#id|datestamp|label|description|username\n";
foreach $id ( sort {$a <=> $b} keys %events ) {
print OUT "$id|$events{$id}{datestamp}|$events{$id}{label}|$events{$id}{description}|$events{$id}{username}\n";
}
close(OUT);
$vars{'message'} = "Changes Saved";
undef %events;
&read_cal_data;
$vars{month} = $in{month}; $vars{month} =~ s|^0||;
$vars{year} = $in{year};
# &EDIT;
# go back to editable events screen
&read_cal_data;
&show_editable_events;
}
#####################################
sub EVENTDETAILS {
open(IN,"$vars{calendar_file}") || &Error( "Can't open $vars{calendar_file}");
my $header = ;
while () {
chomp;
($id,$xdatestamp,$label,$description,$username) = split(/\|/,$_,5);
next unless ($id == $in{id});
($year,$month,$date) = ($xdatestamp =~ /(\d\d\d\d)(\d\d)(\d\d)/);
if ($year eq "0000") {
$year = " (Annual)";
}
else {
$year = ", $year";
}
last;
}
close(IN);
print <<"END";
$label
$label
When
$month/$date$year
Description
$description
END
exit(0);
} # end
#####################################
sub LOGIN {
$ButtonSTYLES = <<"END";
INPUT.button { background-color:#eeeeee;font:arial;font-weight:bold;color:#000080;font-size:xx-small; }
END
print <<"END";
Calendar user login
Calendar Login
END
exit(0);
}
#####
sub do_login {
$user_data_database = 'userdbfile.psv';
# check their username and password
if (-e"$user_data_database") {
open(USERFILE, "$user_data_database");
@Users = ;
close USERFILE;
$topRow = $Users[0];
shift @Users; # remove the top row
$LoginVerified = 0;
my $Counter = 0;
foreach $user (@Users) { # loop through the users file to find the login name supplied
($login, $password, $sessionID, $login_time) = split(/\|/,$user,4);
if ($in{'username'} eq $login) {
if ($in{'password'} eq $password) {
$sessionID = &CreateID;
$login_time = time;
$Users[$Counter] = join('|', $login, $password, $sessionID, $login_time . "\n");
$LoginVerified = 1;
last;
}
else {
# the password supplied was incorrect
print "
The password you supplied is incorrect!
Please try again.";
exit;
}
}
$Counter++;
}
}
else {
# error the user information database is non-existant
print "Configuration Error, could not find the user information database";
exit;
}
if ($LoginVerified) { # if they entered the correct username/password combination
# add the session ID to the database
open(USERFILE, ">$user_data_database");
print USERFILE $topRow;
print USERFILE @Users;
close USERFILE;
# now display the calendar, with the add form (put their sessionID in a hidden tag on the add form)
$vars{calendar_html} = './templates/calendar_Default.html';
&DISPLAY;
}
else {
# the login name/password was incorrect
print "
The username you supplied is incorrect!
Please try again.";
exit;
}
}
#####
sub CreateID {
srand (time|$$);
my ($SessionID) = int(rand(1000000));
$SessionID .= "_$$";
$SessionID =~ s/-//g;
return $SessionID
}
### end subs
&GetCwd;
&ReadParse;
# Protect against "OPEN" vulnerability
# ------------------------------------
$in{config} =~ s|[^\s\w\.\/]||g;
$in{template} =~ s|[^\s\w\.\/]||g;
&read_config;
$vars{"cgi"} = $ENV{'SCRIPT_NAME'};
&INITIALIZE;
# Generate general variables
# --------------------------
$vars{'month'} = $in{'Month'} || $month;
$vars{'current_month'} = $month;
$vars{'year'} = $in{'Year'} || $year;
$vars{'current_year'} = $year;
$vars{'date'} = $in{'Date'};
if (!$vars{date} && $vars{month}==$month && $vars{year}==$year) {
$vars{date} = $mday;
}
$vars{'type'} = $DefaultType;
$vars{'monthname'} = @months[int($vars{'month'})-1];
$vars{'datestamp'} = sprintf("%4.4d%2.2d%2.2d",$vars{year},$vars{month},$vars{date});
# Routine to get working directory
# --------------------------------
sub GetCwd {
if ($base_dir) { $vars{base_dir} = $base_dir; }
return if $vars{base_dir};
my $path = $ENV{'PATH_TRANSLATED'} || $ENV{'SCRIPT_FILENAME'};
unless ($path) {
&Error("Your server does not provide the PATH_TRANSLATED or SCRIPT_FILENAME environment variables.
Please see the installation documentation for how to set the \$base_dir variable manually.");
exit(0);
}
$path =~ s|[^/\\]*$||;
$vars{base_dir} = $path;
}
#####################################################################
#
# Decide what to do based on the ACTION parameter
#
#####################################################################
if ($in{"ACTION"} eq "VIEWDAY") {
&VIEWDAY;
}
elsif ($in{"ACTION"} eq "DO_ADD") {
&DO_ADD;
}
elsif ($in{"ACTION"} eq "LOGIN") {
# added 6/12/2001 - users must login to add events to the calendar
&LOGIN;
}
elsif ($in{"ACTION"} eq "EVENTDETAILS") {
&EVENTDETAILS;
}
elsif ($in{"ACTION"} eq "do_login") {
&do_login;
}
elsif ($in{"ACTION"} eq "EDIT") {
&read_cal_data;
&show_editable_events;
}
elsif ($in{"ACTION"} eq "do_edit") {
&do_edit;
}
elsif ($in{"ACTION"} eq "SAVE_EDIT") {
&save_edit;
}
else {
&DISPLAY;
}
exit(0);