#!/usr/bin/perl
#
# Calendar Admin Script
#
# Matt Kruse
# http://www.mattkruse.com/
#
$|=1;
print "Content-type: text/html\n\n";
# $base_dir = "";
#####################################################################
#
# SPLASH()
#
# Splash Screen
#
#####################################################################
sub SPLASH {
print <<"END";
Calendar Administration
Calendar Administration
Author : Matt Kruse
Web Site : http://www.mattkruse.com/scripts/calendar/
THIS PROGRAM IS FREE SOFTWARE
However...
It may not be re-distributed
It may not be sold
THE POSTCARD INITIATIVE
This program is free, but I would appreciate receiving a postcard from you as a Thank-You for my time. If used for company purposes, a \$50 donation is appreciated. Actually, any donation at all is appreciated! :)
Matt Kruse 5510 31st Ave Ct. Moline, IL 61265
END
exit(0);
}
#####################################################################
#
# ADMIN()
#
# Main Admin page
#
#####################################################################
sub ADMIN {
if ($vars{html_heading} eq "yes") { $html_heading = " CHECKED "; }
if ($vars{html_description} eq "yes") { $html_description= " CHECKED "; }
$monsunweek{$vars{monsunweek}} = " CHECKED ";
unless ($vars{calendarurl} =~ /http/) {
$vars{calendarurl} = "http://" . $ENV{SERVER_NAME} . $vars{cgi};
$vars{calendarurl} =~ s|_admin||;
$vars{message} .= "\\n\\nYour calendar URL needs to be entered. A URL has been entered for you based on this admin utility.\\n\\nYou must Save Options before this takes effect.";
}
opendir(DIR,$vars{template_dir});
$calendar_template_options = "";
$viewday_template_options = "";
while ($file = readdir(DIR)) {
$selected = "";
if ($file =~ /^calendar.*\.html/) {
$title = $file;
$title =~ m|^calendar_?(.*)\.html|i;
$title = $1;
$title =~ s|_| |g;
if ($vars{calendar_html} =~ /$file/) {
$selected = " SELECTED ";
}
$calendar_template_options .= "$title";
}
if ($file =~ /^viewday.*\.html/) {
$title = $file;
$title =~ m|^viewday_?(.*)\.html|i;
$title = $1;
$title =~ s|_| |g;
if ($vars{viewday_html} =~ /$file/) {
$selected = " SELECTED ";
}
$viewday_template_options .= " $title";
}
}
closedir(DIR);
print <<"END";
Calendar Administration
$ADMIN_PAGE_TOP
$ADMIN_PAGE_BOTTOM
END
exit(0);
} #end of ADMIN
#####################################################################
#
# OPTIONS()
#
# Save Options
#
#####################################################################
sub OPTIONS {
my $config_file = $vars{base_dir} . $in{config};
open(IN,$config_file) || &Error("Can't open $config_file.");
undef $/;
my $cfg = ;
close(IN);
$in{months} = join(',', ($in{month1},$in{month2},$in{month3},$in{month4},$in{month5},$in{month6},$in{month7},$in{month8},$in{month9},$in{month10},$in{month11},$in{month12}) );
$in{shortmonths} = join(',', ($in{shortmonth1},$in{shortmonth2},$in{shortmonth3},$in{shortmonth4},$in{shortmonth5},$in{shortmonth6},$in{shortmonth7},$in{shortmonth8},$in{shortmonth9},$in{shortmonth10},$in{shortmonth11},$in{shortmonth12}) );
$in{days} = join(',' , ($in{dayname1}, $in{dayname2}, $in{dayname3}, $in{dayname4}, $in{dayname5}, $in{dayname6}, $in{dayname7}) );
foreach ( 'months',
'shortmonths',
'days',
'monsunweek',
'calendarurl',
'html_heading',
'html_description',
'bgcolor_today',
'bgcolor_current',
'bgcolor_day',
'calendar_html',
'viewday_html',
'time_offset'
) {
$cfg =~ s|^$_=.*|$_=$in{$_}|mg;
}
open(OUT,"> $config_file") || &Error( "Can't open $config_file for writing.");
eval "flock OUT,2";
print OUT $cfg;
close(OUT);
$/ = "\n";
&read_config;
$vars{message} = "Options Saved";
&ADMIN;
} # end of OPTIONS
#####################################################################
#
# Change username/password
#
#####################################################################
sub PASSWORD {
print <<"END";
Calendar Administration
$ADMIN_PAGE_TOP
$ADMIN_PAGE_BOTTOM
END
exit(0);
}
#####################################################################
#
# Save New Password
#
#####################################################################
sub SAVE_PASSWORD {
my $config_file = $vars{base_dir} . $in{config};
open(IN,$config_file) || &Error("Can't open $config_file.");
undef $/;
my $cfg = ;
close(IN);
$new_username = crypt($in{new_username},time);
$new_password = crypt($in{new_password},time);
$cfg =~ s|^username=.*|username=$new_username|mg;
$cfg =~ s|^password=.*|password=$new_password|mg;
open(OUT,"> $config_file") || &Error( "Can't open $config_file for writing.");
eval "flock OUT,2";
print OUT $cfg;
close(OUT);
$/ = "\n";
&read_config;
print <<"END";
Calendar Administration
$ADMIN_PAGE_TOP
Username/Password changed.
You will now need to re-login under the new settings.
$ADMIN_PAGE_BOTTOM
END
exit(0);
} # end of OPTIONS
#####################################################################
#
# Support Stuff
#
#####################################################################
sub SUPPORT {
print <<"END";
Calendar Administration
$ADMIN_PAGE_TOP
Support Information
I try to support this application when I can, how I can. Because I offer it for free, support is NOT GUARANTEED .
Please use my Script Support Forum for posting questions about this script. It is much easier for me than email, and others may be able to help. Before asking for support, be sure to check the Documentation !
If you have created a template that others may be interested in (such as a language-translation) please Mail Me and let me know!
If you have legal/license questions, see my Legal Information page.
Subscribe to my Email Update list to be mailed about updates to this program.
Please rate this script on a scale of 1-10, 10 being the best. This rating is
used at www.cgi-resources.com in
their list of rated resources and helps other people find this script and judge
its quality.
Please use this form to give me general feedback about this script. It will help me improve it and make it a better program! You can submit your comments anonymously if you wish.
$ADMIN_PAGE_BOTTOM
END
exit(0);
}
#####################################################################
#
# ADD()
#
# Form to add an entry
#
#####################################################################
sub ADD {
print <<"END";
Calendar Administration
$ADMIN_PAGE_TOP
$ADMIN_PAGE_BOTTOM
END
exit(0);
} #end of ADD
#####################################################################
#
# DO_ADD()
#
# Add an entry
#
#####################################################################
sub DO_ADD {
$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};
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}\n";
close(OUT);
$vars{'message'} = "Event Added";
}
else {
$vars{'message'} = "ERROR: Event not added";
}
&ADD;
exit(0);
} # end of DO_ADD
#####################################################################
#
# EDIT()
#
# Edit/Delete Entries
#
#####################################################################
sub EDIT {
my $x;
$x = $vars{month};
$monthselected{$x} = " SELECTED ";
$x = $vars{year};
$yearselected{$x} = " SELECTED ";
print <<"END";
Calendar Administration
$ADMIN_PAGE_TOP
January
February
March
April
May
June
July
August
September
October
November
December
1997
1998
1999
2000
2001
2002
2003
$ADMIN_PAGE_BOTTOM
END
exit(0);
} #end of EDIT
#####################################################################
#
# EVENTDETAILS()
#
# Show details of an event in pop-up window
#
#####################################################################
sub EVENTDETAILS {
open(IN,"$vars{calendar_file}") || &Error( "Can't open $vars{calendar_file}");
my $header = ;
while () {
chomp;
($id,$xdatestamp,$label,$description) = split(/\|/,$_,4);
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
#####################################################################
#
# DO_EDIT()
#
# Edit/Delete Entries
#
#####################################################################
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);
}
#####################################################################
#
# EDITFORM()
#
# Form to edit an entry
#
#####################################################################
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) = split(/\|/,$_,4);
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 Administration
$ADMIN_PAGE_TOP
$ADMIN_PAGE_BOTTOM
END
exit(0);
}
#####################################################################
#
# SAVE_EDIT()
#
# Save edits
#
#####################################################################
sub SAVE_EDIT {
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) = split(/\|/,$_,4);
$events{$id}{datestamp} = $xdatestamp;
$events{$id}{label} = $label;
$events{$id}{description} = $description;
}
close(IN);
$events{$in{id}}{datestamp} = $datestamp;
$events{$in{id}}{label} = $in{heading};
$events{$in{id}}{description} = $in{description};
open(OUT,"> $vars{calendar_file}") || &Error( "Can't open $calendar_file for writing!");
eval "flock OUT,2";
print OUT "#id|datestamp|label|description\n";
foreach $id ( sort {$a <=> $b} keys %events ) {
print OUT "$id|$events{$id}{datestamp}|$events{$id}{label}|$events{$id}{description}\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;
exit(0);
}
#####################################################################
#
# DELETE()
#
# Delete an entry
#
#####################################################################
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) = split(/\|/,$_,4);
next if ($id == $in{id});
$events{$id}{datestamp} = $xdatestamp;
$events{$id}{label} = $label;
$events{$id}{description} = $description;
}
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\n";
foreach $id ( sort {$a <=> $b} keys %events ) {
print OUT "$id|$events{$id}{datestamp}|$events{$id}{label}|$events{$id}{description}\n";
}
close(OUT);
$vars{'message'} = "Event Deleted";
undef %events;
&read_cal_data;
&EDIT;
exit(0);
}
#####################################################################
#
# 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, prompt to update it.
unless ($header =~ m/^#id\|datestamp\|label\|description/) {
close(IN);
&UPDATE_FORMAT;
}
while () {
chomp;
next unless /^\d/;
($id,$xdatestamp,$label,$desc) = split(/\|/,$_,4);
if ($id > $vars{new_id}) { $vars{new_id} = $id; }
$total_events{$xdatestamp}++;
${$events{$xdatestamp}}{$total_events{$xdatestamp}}{description} = $desc;
${$events{$xdatestamp}}{$total_events{$xdatestamp}}{label} = $label;
${$events{$xdatestamp}}{$total_events{$xdatestamp}}{id} = $id;
}
close(IN);
$vars{new_id}++;
}
#####################################################################
#
# Read in config file
#
#####################################################################
sub read_config {
my $config_file;
if ($in{config}) {
$vars{config} = $in{config};
unless ($vars{config} =~ m|^/|) {
$config_file = $vars{base_dir} . $vars{config};
}
else {
$config_file = $vars{config};
}
}
else {
$vars{config} = "calendar.cfg";
$config_file = $vars{base_dir} . $vars{config};
}
my $key,$val;
open(IN,$config_file) || &Error("Can't open config file $config_file.");
while() {
next if /^#/;
chomp;
($key,$val) = ( /([^=]+)=(.*)/ );
$key = lc($key);
$val =~ s|\[(\S+)\]|$vars{$1}|g;
$val =~ s|\s*$||;
$vars{$key} = $val;
}
close(IN);
@months = split(',',$vars{months});
($vars{month1},$vars{month2},$vars{month3},$vars{month4},$vars{month5},$vars{month6},$vars{month7},$vars{month8},$vars{month9},$vars{month10},$vars{month11},$vars{month12} ) = @months;
@shortmonths = split(',',$vars{shortmonths});
($vars{shortmonth1},$vars{shortmonth2},$vars{shortmonth3},$vars{shortmonth4},$vars{shortmonth5},$vars{shortmonth6},$vars{shortmonth7},$vars{shortmonth8},$vars{shortmonth9},$vars{shortmonth10},$vars{shortmonth11},$vars{shortmonth12} ) = @shortmonths;
@days = split(',',$vars{days});
($vars{dayname1},$vars{dayname2},$vars{dayname3},$vars{dayname4},$vars{dayname5},$vars{dayname6},$vars{dayname7} ) = @days;
}
#####################################################################
#
# Return an error screen
#
#####################################################################
sub Error {
$message = shift;
print <<"END";
Error
ERROR
Error Message:
$message
Debugging Info
File and Directory Locations:
Base Directory (\$base_dir): $base_dir
Template Directory (\$template_dir): $template_dir
Calendar Events File (\$calendar_file): $calendar_file
Current Calendar Template (\$calendar_html): $calendar_html
Current View Day Template (\$viewday_html): $viewday_html
File Permissions
Any potential problems caused by file permissions or locations will be listed here.
END
unless (-d $base_dir) { print "The base directory does not exist\n"; }
unless (-e $calendar_file) { print " The calendar events file does not exist\n"; }
unless (-w $calendar_file) { print " The calendar events file is not world-writeable (chmod 777)\n"; }
unless (-d $template_dir) { print " The Template directory does not exist\n"; }
unless (-e $calendar_html) { print " The calendar template file does not exist\n"; }
unless (-w $calendar_html) { print " The calendar template file is not world-writeable (chmod 777)\n"; }
unless (-e $viewday_html) { print " The view day template file does not exist\n"; }
unless (-w $viewday_html) { print " The view day template file is not world-writeable (chmod 777)\n"; }
print <<"END";
Online Documentation
END
exit(0);
}
#####################################################################
#
# TEMPLATES()
#
# HTML Template Options
#
#####################################################################
sub TEMPLATES {
opendir(DIR,$vars{template_dir});
$calendar_template_options = "";
$viewday_template_options = "";
while ($file = readdir(DIR)) {
$selected = "";
if ($file =~ /^calendar.*\.html/) {
$title = $file;
$title =~ m|^calendar_?(.*)\.html|i;
$title = $1;
$title =~ s|_| |g;
$calendar_templates{$file}{title} = $title;
$filename = $vars{template_dir} . "/" . $file;
$calendar_templates{$file}{lastmod} = int(-M $filename);
}
if ($file =~ /^viewday.*\.html/) {
$title = $file;
$title =~ m|^viewday_?(.*)\.html|i;
$title = $1;
$title =~ s|_| |g;
$viewday_templates{$file}{title} = $title;
$filename = $vars{template_dir} . "/" . $file;
$viewday_templates{$file}{lastmod} = int(-M $filename);
}
}
closedir(DIR);
print <<"END";
Calendar Administration
$ADMIN_PAGE_TOP
END
print "\n";
print "\n";
print <<"END";
$ADMIN_PAGE_BOTTOM
END
exit(0);
} #end of TEMPLATES
#####################################################################
#
# EDIT_TEMPLATE()
#
# Actually edit an HTML template
#
#####################################################################
sub EDIT_TEMPLATE {
$filename = $vars{template_dir} . "/" . $in{file};
open(IN,$filename);
undef $/;
$contents = ;
$/="=n";
close(IN);
$contents =~ s|<|\<\;|gs;
$contents =~ s|>|\>\;|gs;
$contents =~ s|\r\n|\n|gs;
print <<"END";
Calendar Administration
END
exit(0);
} #end of EDIT_TEMPLATE
#####################################################################
#
# SAVE_TEMPLATE()
#
# Save an HTML template
#
#####################################################################
sub SAVE_TEMPLATE {
$filename = $in{filename};
open(OUT,"> $filename") || &Error("Couldn't write to file $filename");
$contents =~ s|\r\n|\n|gs;
# $contents =~ s|<|\<\;|gs;
# $contents =~ s|>|\>\;|gs;
print OUT "$in{contents}";
close(OUT);
print <<"END";
Calendar Administration
END
exit(0);
} #end of SAVE_TEMPLATE
#####################################################################
#
# Parse input
#
#####################################################################
sub ReadParse {
local (*in) = @_ if @_; local ($i, $loc, $key, $val);
if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'};}
elsif ($ENV{'REQUEST_METHOD'} eq "POST")
{read(STDIN,$in,$ENV{'CONTENT_LENGTH'});}
@in = split(/&/,$in);
foreach $i (0 .. $#in) {
$in[$i] =~ s/\+/ /g; ($key, $val) = split(/=/,$in[$i],2);
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
$in{$key} .= "\0" if (defined($in{$key}));
$in{$key} .= $val;
}
return 1;
}
#####################################################################
#
# Calendar logic
#
#####################################################################
sub PerpetualCalendar {
# This perpetual calendar routine provides accurate day/date
# correspondences for dates from 1601 to 2899 A.D. It is based on
# the Gregorian calendar, so be aware that early correspondences
# may not always be historically accurate. The Gregorian calendar
# was adopted by the Italian states, Portugal and Spain in 1582,
# and by the Catholic German states in 1583. However, it was not
# adopted by the Protestant German states until 1699, by England
# and its colonies until 1752, by Sweden until 1753, by Japan
# until 1873, by China until 1912, by the Soviet Union until 1918,
# and by Greece until 1923.
($perp_mon,$perp_day,$perp_year) = @_;
%day_counts =
(1,0,2,31,3,59,4,90,5,120,6,151,7,181,
8,212,9,243,10,273,11,304,12,334);
$perp_days = (($perp_year-1601)*365)+(int(($perp_year-1601)/4));
$perp_days += $day_counts{$perp_mon};
$perp_days += $perp_day;
$perp_sofar = $day_counts{$perp_mon};
$perp_sofar += $perp_day;
$perp_togo = 365-$perp_sofar;
if (int(($perp_year-1600)/4) eq (($perp_year-1600)/4)) {
$perp_togo++;
if ($perp_mon > 2) {
$perp_days++;
$perp_sofar++;
$perp_togo -= 1;
}
}
foreach $key (1700,1800,1900,2100,2200,2300,2500,2600,2700) {
if ((($perp_year == $key) && ($perp_mon > 2))
|| ($perp_year > $key)) {
$perp_days -= 1;
}
}
$perp_dow = $perp_days - (int($perp_days/7)*7);
if ($perp_dow == 7) { $perp_dow = 0; }
if ($vars{monsunweek} eq "Yes") {
$perp_dow -= 1;
if ($perp_dow == -1) { $perp_dow = 6; }
}
$perp_eom = 31;
if (($perp_mon == 4) || ($perp_mon == 6)
|| ($perp_mon == 9) || ($perp_mon == 11)) {
$perp_eom = 30;
}
if (($perp_mon == 2)) {
$perp_eom = 28;
}
if ((int(($perp_year-1600)/4) eq (($perp_year-1600)/4))
&& ($perp_mon == 2)) {
$perp_eom = 29;
}
foreach $key (1700,1800,1900,2100,2200,2300,2500,2600,2700) {
if ($perp_year == $key) {
if ($perp_mon == 1) {
$perp_togo -= 1;
}
elsif ($perp_mon == 2) {
$perp_togo -= 1;
$perp_eom = 28;
}
else {
$perp_sofar -= 1;
}
}
}
}
#####################################################################
# load_template
#
# load_template( "filename" )
#
# load a file and return it
#
#####################################################################
sub load_template {
my($filename) = shift;
undef $/;
open(IN,"$filename") || &Error("Couldn't open file $filename in load_template: $!\n");
my($template) = ;
close(IN);
$/="\n";
return $template;
}
#####################################################################
# parse_template
#
# parse_template( $template_string , \%value_array )
#
# Replace <%=TAGS%> in template with values
#
#####################################################################
sub parse_template {
my ($template) = shift;
my ($data) = shift;
# Bring in #include files
$template =~ s|
|
package temp;
my $tmp;
my $file = $1;
if (!open(IN,$file)) {
$tmp = "[ INCLUDE ERROR: Couldn't find file '$file' to include ]";
}
else {
undef $/;
$tmp = ;
$/ = "\n";
close(IN);
$tmp;
}
|sgeix;
# Replace <%TAG%> <%/TAG%>
foreach (keys %$data) {
if (defined ${$data}{$_}) {
$template =~ s|<%$_%>(.+?)<%/$_%>|
my($tmp2) = $1;
$tmp2 =~ s!<%=VALUE%>!${$data}{$_}!s;
"$tmp2";
|esgx;
}
else {
$template =~ s|<%$_%>(.*?)<%/$_%>||sg;
}
}
# Replace <%=TAG%>
foreach (keys %$data) {
$template =~ s|<%=$_%>|${$data}{$_}|sg;
}
# Replace <%=ENCODE TAG%>
foreach (keys %$data) {
$temp = ${$data}{$_};
$temp =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
$template =~ s|<%=ENCODE $_%>|$temp|sg;
}
# Replace <% code %> with output from code
$template =~ s|
<%(.*?)%>
|
package temp;
my $tmp;
my $code = $1;
$code =~ s/\bprint\b/\$tmp .= /sg;
eval($code);
$tmp;
|sgex;
# Pass back parsed template
return $template;
}
#####################################################################
# parse_output
#
# parse_output( $tag , $template_string , \%value_array )
#
# Loop through values array and replace <%OUTPUT $tag%> %OUTPUT%>
#
#####################################################################
sub parse_output {
my ($tag) = shift;
my ($template) = shift;
my ($data) = shift;
# Parse the <%OUTPUT%> <%/OUTPUT%> tags for record format
my ($format) = ($template =~ m|<%OUTPUT $tag%>(.+?)<%/OUTPUT%>|si);
my($output) = "";
my($key,$tmp,$tmp2);
# Loop through data and create output data
foreach $key ( sort keys %$data ) {
$tmp = $format;
foreach (keys %{$$data{$key}}) {
if ($$data{$key}{$_} =~ /\S/) {
$tmp =~ s|<%$_%>(.+?)<%/$_%>|
$tmp2 = $1;
$tmp2 =~ s!<%=VALUE%>!$$data{$key}{$_}!s;
"$tmp2";
|esgx;
}
else {
$tmp =~ s|<%$_%>(.+?)<%/$_%>||sg;
}
}
foreach (keys %{$$data{$key}}) {
$tmp =~ s|<%=($_)%>|$$data{$key}{$1}|sg;
}
$output .= $tmp;
}
# Replace OUTPUT in template with generated output
$template =~ s|<%OUTPUT $tag%>.+?<%/OUTPUT%>|$output|s;
return $template;
}
#####################################################################
# html_escape
#
# html_escape ( $string )
#
# Escape a string for display in HTML forms, etc.
#
#####################################################################
sub html_escape {
my ($string) = shift;
$string =~ s|"|"|sg;
$string =~ s|<|<|sg;
$string =~ s|>|>|sg;
return $string;
}
&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;
#####################################################################
#
# Top of Admin Page
#
#####################################################################
$ADMIN_PAGE_TOP = <<"END";
END
#####################################################################
#
# Style Sheet for Admin Pages
#
#####################################################################
$ADMIN_STYLES = <<"END";
INPUT.button { background-color:#eeeeee;font:arial;font-weight:bold;color:#000080;font-size:xx-small; }
END
# 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;
}
# Generate general variables
# --------------------------
$vars{'month'} = $in{'Month'} || $month;
$vars{'year'} = $in{'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});
$vars{lastyear} = $vars{year};
$vars{nextyear} = $vars{year};
$vars{lastmonth} = $vars{month}-1;
$vars{nextmonth} = $vars{month}+1;
if ($vars{lastmonth} < 1) { $vars{lastmonth}=12; $vars{lastyear}--; }
if ($vars{nextmonth} > 12){ $vars{nextmonth}=1; $vars{nextyear}++; }
$vars{lastmonthname} = @months[int($vars{lastmonth})-1];
$vars{nextmonthname} = @months[int($vars{nextmonth})-1];
#####################################################################
#
# Decide what to do based on the ACTION parameter
#
#####################################################################
if ($in{"ACTION"} eq "EVENTDETAILS") {
&EVENTDETAILS;
}
unless ($in{"OK"} eq "OK") {
&SPLASH;
}
# If encrypted password is blank, go to passwd change screen
if (($in{"ACTION"} eq "SAVE_PASSWORD") && ($vars{username} !~ /\w/)) {
&SAVE_PASSWORD;
}
unless ( ($vars{username} =~ /\w/) && ($vars{username} =~ /\w/) ) {
$vars{message} = "You must create and save an administrator username and password before using the admin utility. The username and password you just entered have been put as the defaults above.";
&PASSWORD;
exit(0);
}
# Check the password
unless (
(crypt($in{"username"},$vars{username}) eq $vars{username})
&&
(crypt($in{"password"},$vars{password}) eq $vars{password})
) {
print "Username and/or password invalid";
exit(0);
}
if ($in{"ACTION"} eq "DO_UPDATE_FORMAT") {
&DO_UPDATE_FORMAT;
}
# Check file format before doing anything else
&read_cal_data;
if ($in{"ACTION"} eq "EDIT") {
&EDIT;
}
if ($in{"ACTION"} eq "DO_EDIT") {
&DO_EDIT;
}
if ($in{"ACTION"} eq "SAVE_EDIT") {
&SAVE_EDIT;
}
elsif ($in{"ACTION"} eq "ADD") {
&ADD;
}
elsif ($in{"ACTION"} eq "DO_ADD") {
&DO_ADD;
}
elsif ($in{"ACTION"} eq "OPTIONS") {
&OPTIONS;
}
elsif ($in{"ACTION"} eq "PASSWORD") {
&PASSWORD;
}
elsif ($in{"ACTION"} eq "SAVE_PASSWORD") {
&SAVE_PASSWORD;
}
elsif ($in{"ACTION"} eq "SUPPORT") {
&SUPPORT;
}
elsif ($in{"ACTION"} eq "TEMPLATES") {
&TEMPLATES;
}
elsif ($in{"ACTION"} eq "EDIT_TEMPLATE") {
&EDIT_TEMPLATE;
}
elsif ($in{"ACTION"} eq "SAVE_TEMPLATE") {
&SAVE_TEMPLATE;
}
else {
&ADMIN;
}
exit(0);