#!/usr/local/bin/perl # # Calendar Script # # Matt Kruse # http://mkruse.netexpress.net/ # $|=1; print "Content-type: text/html\n\n"; ##################################################################### # # Configuration Parameters # ##################################################################### $base_dir = "/usr/local/apache/htdocs/calendar/"; $template_dir = "/usr/local/apache/htdocs/calendar/templates/"; $vars{"CGI"} = "http://rshweb.com/calendar/calendar.cgi"; $vars{"BASE"} = "http://rshweb.com/calendar/"; $calendar_file = $base_dir . "calendar_events.txt"; $textcolor_today = "ff0000"; $cell_width=100; $MonSunWeek = 0; ##################################################################### # END CONFIG ##################################################################### use lib "."; unshift(@INC,$base_dir); require "parse_template.pl"; &INITIALIZE; &ReadParse; ##################################################################### # # Decide what to do based on the ACTION parameter # ##################################################################### if ($in{"ACTION"} eq "VIEWDAY") { &VIEWDAY; } elsif ($in{"ACTION"} eq "ADD") { &ADD; } else { &DISPLAY; } exit(0); ##################################################################### # # DISPLAY() # # Display the calendar # ##################################################################### sub DISPLAY { &read_cal_data; $in{'Month'} = $in{Month} || $month; $in{'Year'} = $in{Year} || $year; $in{'Type'} = $DefaultType; $xmonth = @months[int($in{'Month'})-1]; $lastyear = $in{Year}-1; $nextyear = $in{Year}+1; $lastmonth = $in{Month}-1; $nextmonth = $in{Month}+1; if ($lastmonth < 1) { $lastmonth=12; } if ($nextmonth > 12){ $nextmonth=1; } &PerpetualCalendar(int($in{'Month'}),1,int($in{'Year'})); # Create the calendar output into a variable # ------------------------------------------ $calendar = <<"END"; END if ($perp_dow > 0) { $calendar .= ""; } foreach $key (1..$perp_eom) { $calendar .= "\n"; $weekday = ($key+$perp_dow)-(int(($key+$perp_dow)/7)*7); if (($weekday == 0) && !($key == $perp_eom)) { $calendar .= "\n\n"; } } if ($weekday > 0) { $leftover = 7-$weekday; $calendar .= ""; } $calendar .= "\n

<   $in{Year}   >
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
 "; if (($in{'Year'} == $year) && ($in{'Month'} == $month) && ($key == $mday)) { $calendar .= ""; $calendar .= "

$key"; } else { $calendar .= "

$key"; } $perp_sofar++; $perp_togo -= 1; $calendar .= "\n
"; $xdatestamp = sprintf("%4.4d%2.2d%2.2d",$in{Year},$in{Month},$key); foreach (0 .. 3) { next unless ${$events{$xdatestamp}{labels}}[$_]; $calendar .= sprintf("%20.20s
\n",${$events{$xdatestamp}{labels}}[$_]); } $xcount = $#{$events{$xdatestamp}{labels}}; $xdatestamp = sprintf("0000%2.2d%2.2d",$in{Month},$key); if ($xcount < 3) { foreach (0 .. 2-$xcount) { $calendar .= sprintf("%20.20s
\n",${$events{$xdatestamp}{labels}}[$_]); } } $xcount = $xcount + $#{$events{$xdatestamp}{labels}}; if ( $xcount > 2) { $calendar .= "...\n"; } $xdatestamp = sprintf("%4.4d%2.2d%2.2d",$in{Year},$in{Month},$key); $calendar .= "
\n

 

\n"; # Load the template and do the replace # ------------------------------------ $template = &load_template("${template_dir}calendar.html"); $vars{"CALENDAR"} = $calendar; $template = &parse_template($template, \%vars); print $template; exit; } #end of DISPLAY ##################################################################### # # VIEWDAY() # # Display a single day's events # ##################################################################### sub VIEWDAY { &read_cal_data; ($year,$month,$date) = ($in{DATE} =~ /(....)(..)(..)/); $xmonth = $months[int($month)-1]; $vars{"TITLE"} = "$xmonth $date, $year"; $vars{"EVENTS"} = ""; $xdatestamp = sprintf("%4.4d%2.2d%2.2d",$year,$month,$date); foreach (0 .. $#{$events{$xdatestamp}{desc}}) { $vars{"EVENTS"} .= "
  • ${$events{$xdatestamp}{desc}}[$_]
    \n"; } $xdatestamp = sprintf("0000%2.2d%2.2d",$month,$date); foreach (0 .. $#{$events{$xdatestamp}{desc}}) { $vars{"EVENTS"} .= "
  • ${$events{$xdatestamp}{desc}}[$_]
    \n"; } $template = &load_template("${template_dir}calendar_day.html"); $template = &parse_template( $template, \%vars ); $template = &parse_template( $template, \%in ); print $template; exit; } #end of VIEWDAY ##################################################################### # # ADD() # # Add an entry # ##################################################################### sub ADD { $in{description} =~ s|[\r\n]| |gs; open(OUT,">> $calendar_file") || die "Can't open $calendar_file for writing!"; print OUT "$in{DATE}|$in{heading}|$in{description}\n"; close(OUT); print <<"END"; END exit(0); } #end of ADD ##################################################################### # # Initialize variables # ##################################################################### sub INITIALIZE { @months = (January,February,March,April,May,June,July,August,September,October,November,December); @shortmonths = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec); @days =(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday); @shortdays = (Sun,Mon,Tue,Wed,Thu,Fri,Sat); # Current date and stuff $time = time; ($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 { open(IN,"$calendar_file") || die "Can't open $calendar_file"; while () { chomp; next unless /^\d/; ($xdatestamp,$label,$desc) = split(/\|/,$_,3); push(@{$events{$xdatestamp}{desc}},$desc); push(@{$events{$xdatestamp}{labels}},$label); } close(IN); } ##################################################################### # # 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 ($MonSunWeek) { $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; } } } }