#!/usr/bin/perl

####################################################################
#
#This software is released under the GPL.  Please
#see the included LICENSE file.
#
#Copyright (C) 2001  Michael Spiceland
#Copyright (C) 2001  Jay Kline
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#Use this script at your own risk!  I make no 
#warranties that it is hack proof!
#
#
# This script is part of the Fuzzymonkey Perl Script Archive at
# www.fuzzymonkey.net/perl/
#
# Edited by Jay Kline www.slushpupie.com
#
####################################################################


use CGI qw(:standard);
require "common.pl";
require "sitevariables.pl";
############ BEGIN MAIN PROGRAM ################################
$cur = CGI->new();
$mode = "default";
if($cur->param("mode")) {
	$mode = $cur->param("mode");
}

if($cur->param("month") && $cur->param("year")){
	$month = $cur->param("month");
	$year = $cur->param("year");
	($sec,$min,$hr,$mday,$mon,$year_localtime,$wday,$yday,$isdst) = localtime(time());
}else{
	($sec,$min,$hr,$mday,$mon,$year_localtime,$wday,$yday,$isdst) = localtime(time());
	$year = $year_localtime + 1900;
	$month = $mon + 1;
}

@pretty_months = ("","January","February","March","April","May","June","July","August","September","October","November","December");
%apps = get_appointments("appointments.txt");
%apps = add_appointments("appointments-imported.txt",%apps);



if($cur->param("day")){
	$day = $cur->param("day");
	$content .= print_full_day($month,$day);
	print "Content-type: text/html\n\n<html><body>$content</body></html>";
	exit(1);
} else {
	$content.= print_month($month,$day);
}
printpage($content);

############ BEGIN SUBROUTINES #################################

# Print the calendar
#

sub print_month($$) {
	my $return;
	my $month = $_[0];
	my $day = $_[1];


	$calendar = '';
	$calendar .= "<table width=100% border=0><tr><td width=14% align=left>";

	#create previous month button
	$month_button = $month-1;
	if($month_button == 0){
		$month_button = 12;
		$year_button = $year-1;
	}else{
		$year_button = $year;
	}

	$calendar .= "<a href=\"index.cgi?month=$month_button&year=$year_button#top\"> <--$pretty_months[$month_button], $year_button</a>";

	$calendar .= "</td><td align=center>";

	#create "today" button
	$month_button = $mon + 1;
	$year_button = $year_localtime + 1900;
	$calendar .= "<a href=\"index.cgi?month=$month_button&year=$year_button#top\">Today</a>";

	$calendar .= "</td><td width=14% align=right>";

	#create next month button
	$month_button = $month+1;
	if($month_button == 13){
		$month_button = 1;
		$year_button = $year+1;
	}else{
		$year_button = $year;
	}
	$calendar .= "<a href=\"index.cgi?month=$month_button&year=$year_button#top\">$pretty_months[$month_button], $year_button --></a>";

	$calendar .= "</td></tr></table>";

	$cal = `cal $month $year`;
	@weeks= split(/\n/,"$cal");
	shift @weeks; shift @weeks;
	$calendar .= "<table cellspacing=1 border=0 width=100%>";
	$calendar .= "<tr class=headercell>
			<td align=middle width=14% class=headercell><font class=default>Sunday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Monday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Tuesday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Wednesday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Thursday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Friday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Saturday</font></td>
		</tr>";
	foreach $week (@weeks){
		$calendar .= "<tr height=20%>";
		@days= split " ",$week;

		# Pad @days with leading blanks if it's the first week
		if ($days[0] == 1) {
			foreach (0 .. (6 - $days[-1])) {
	        		$calendar .= "<td width=14% height=20% class=fillday>&nbsp;</td>";
			}
		}

		foreach my $day (@days){
			$calendar .= print_day($month,$day);
		}
		unless($days[0] ==1){
			$left = 7-@days;
			until ($left == 0){
	        		$calendar .= "<td class=fillday>&nbsp;</td>";
				$left--;
			}
		}

		$calendar .= "</tr>";
	}
	$calendar .= "<tr bgcolor=$weekdaybg>
			<td align=middle width=14% class=headercell><font class=default>Sunday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Monday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Tuesday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Wednesday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Thursday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Friday</font></td>
			<td align=middle width=14% class=headercell><font class=default>Saturday</font></td>
		</tr>";

	$calendar .= "</table>";

	if($pscal){
		$pdf_link = "<A HREF=\"#null\" class=\"smalltext\" OnClick=\" javascript:window.open(\'printable.cgi?year=$year\',\'Popup\',\'width=400,height=300,scrollbars=auto\')\">printable version</a>";	
	}
	if($import =~ /yes/i){
		$import_link = "<A HREF=\"protected/import.cgi\" class=\"smalltext\">import from Outlook</a></small>";	
	}
	

	$return .= "<center><font class=default><a name=\"top\"></a>$pretty_months[$month], $year</font></center>\n";
	$return .= $calendar;
	$return .= "<br>$pdf_link<br>$import_link";

	return $return;


}


# Print one day at a time
#
sub print_full_day($$) {
	my $return;
	my $month = $_[0];
	my $day = $_[1];
	my $appointment;
	my $appointment_text = "<html>
<head>
<title>My Calendar</title>
<link rel=stylesheet type=\"text/css\" href=$csslink></head>";
	my @appointment_array;
	if(($year == ($year_localtime+1900)) && ($mon == ($month-1)) && ($mday == $day)){
		$class = "today";
	}else{
		$class = "emptyday";
	}
	@appointments_array = @{$apps{"$month-$day-$year"}};
	@appointments_array = (@appointments_array,@{$apps{"$month-$day-*"}});
	@appointments_array = (@appointments_array,@{$apps{"*-$day-*"}});
	foreach $appointment (@appointments_array){
	#	($appointment) = /(.{0,5})/; #get only the first 100chars
		$appointment_text .="<tr><td class=emptyday><font class=default>$appointment</font></td></tr>\n";
	}

	$return .= "
		<p><center><table border=0 cellspacing=0 cellpadding=5 width=375>
		<tr><td class=headercell align=center><font class=default><b>Today\'s Events</b></font></td></tr>
		$appointment_text
		</table></center>";

	return $return;

}

sub print_day($$) {
	my $return;
	my $month = $_[0];
	my $day = $_[1];
	my $appointment;
	my $appointment_text;
	my @appointment_array;

	
	#$appointments = "$apps{$day}";
	#@appointments_array = split(/##/,$appointments);
	@appointments_array = @{$apps{"$month-$day-$year"}};
	@appointments_array = (@appointments_array,@{$apps{"$month-$day-*"}});
	@appointments_array = (@appointments_array,@{$apps{"*-$day-*"}});
	$numapps=0;
	foreach $appointment (@appointments_array){
		$numapps++;
		if ($numapps > 3) {
			$appointment_text .= "...";
			last;
		}
		$_ = $appointment;
		$count = tr///c; #dont display more than 20 chars
		#if ($count > 40) {
		#	$appointment = substr($appointment,0,40) . "...";
		#}
		$appointment_text .="$appointment<br>";
	}

	if(($year == ($year_localtime+1900)) && ($mon == ($month-1)) && ($mday == $day)){
		$color = "today";
	} elsif ("$appointment_text") {
		$color = "busyday";
	} else {
		$color = "emptyday";
	}
	
	$return .= "<td class=$color width=14% height=100 valign=top>
		<table border=0 width=100% height=5><tr><td><font class=default>
		<A HREF=\"#null\" OnClick=\" javascript:window.open(\'index.cgi?day=$day&month=$month&year=$year\',\'Popup\',\'width=400,height=200,scrollbars=auto\')\">
		$day</a></font></td><td align=right><font class=smalltext><a href=\"protected/update.cgi?day=$day&month=$month&year=$year\">+/-</a></td></tr></table>
		<font class=smalltext>
		<br>$appointment_text
		</font></td>";

	return $return;

}


#####################################################
#subs from slashdot to remove bad html
#####################################################
# Thanks to Slashdot for these
# Approved HTML tags for HTML posting
@approvedtags = (
        'B','I','P .*','P','A',
        'LI','OL','UL','EM','BR',
        'STRONG','BLOCKQUOTE',
        'HR','DIV .*','DIV','TT'
        );

sub stripBadHtml
{
        my ($str) = @_;

        $str =~ s/(\S{90})/$1 /g;
        $str =~ s/<(?!.*?>)//;
        $str =~ s/<(.*?)>/approvetag($1,@approvedtags)/sge; #replace tags with approved ones
        return $str;
}

sub approvetag
{
        my ($tag,@apptag) = @_;

        $tag =~ s/^\s*?(.*)\s*?$/$1/e; #trim leading and trailing spaces

        if (uc(substr ($tag, 0, 2)) eq 'A ')
        {
                $tag =~ s/^.*?href="?(.*?)"?$/A HREF="$1"/i; #enforce "s
                return "<" . $tag . ">";
        }

        foreach my $goodtag (@apptag)
        {
                $tag = uc $tag;
                if ($tag eq $goodtag || $tag eq '/' . $goodtag)
                        {return "<" . $tag . ">";}
                #check against my list of tags
        }
        return "";
}

