#!/usr/bin/perl
# before anything else, the script needs to find out its own name
#
# some servers (notably IIS on windows) don't set the cwd to the script's
# directory before executing it. So we get that information
# from $0 (the full name & path of the script).
BEGIN{($_=$0)=~s![\\/][^\\/]+$!!;push@INC,$_}
$name = $0;
$name =~ s/.+\/.+\///; # for unix
$name =~ s/.+\\.+\\//; # for windows
$path = $0;
$path =~ s/(.+\/).+/$1/g; # for unix
$path =~ s/(.+\\).+/$1/g; # for windows
if ($path ne "")
{
chdir $path;
push @INC,$path;
}
# finished discovering name
local $template_html;
local $event_details_template;
local $list_item_template;
local $calendar_item_template;
local $upcoming_item_template;
my $config_file = "upcoming_events.config";
my $plans_url = "";
# check for required modules.
my $module_found=0;
foreach $temp_path (@INC)
{
if (-e "$temp_path/CGI")
{$module_found=1;}
}
if ($module_found == 0)
{
$fatal_error=1;
$error_info .= "unable to locate required module CGI!\n";
}
else
{use CGI;}
$module_found=0;
foreach $temp_path (@INC)
{
if (-e "$temp_path/CGI/Carp.pm")
{$module_found=1;}
}
if ($module_found == 0)
{
$fatal_error=1;
$error_info .= "unable to locate required module CGI::Carp!\n";
}
else
{use CGI::Carp qw/fatalsToBrowser/;}
$module_found=0;
foreach $temp_path (@INC)
{
if (-e "$temp_path/Time")
{$module_found=1;}
}
if ($module_found == 0)
{
$fatal_error=1;
$error_info .= "unable to locate required module Time.pm!\n";
}
else
{use Time::Local;}
$module_found=0;
foreach $temp_path (@INC)
{
if (-e "$temp_path/IO.pm")
{$module_found=1;}
}
if ($module_found == 0)
{
$fatal_error=1;
$error_info .= "unable to locate required module IO.pm!\n";
}
else
{use IO::Socket;}
if ($fatal_error == 1) # print error and bail out
{
&fatal_error();
}
$module_found=0;
foreach $temp_path (@INC)
{
if (-e "$temp_path/plans_config.pl")
{$module_found=1;}
}
if ($module_found == 0)
{
$fatal_error=1;
$error_info .= "Unable to locate plans_config.pl! It should be in the same directory as plans.cgi!\n";
}
else {require "plans_config.pl";}
$module_found=0;
foreach $temp_path (@INC)
{
if (-r "$temp_path/plans_lib.pl")
{$module_found=1;}
}
if ($module_found == 0)
{
$fatal_error=1;
$error_info .= "Unable to locate plans_lib.pl! It should be in the same directory as plans.cgi!\n";
}
else {require "plans_lib.pl";}
# get the language file, if one is defined
if (defined $options{language_files})
{
my @language_files = split(',', $options{language_files});
foreach $language_file (@language_files)
{
$module_found=0;
foreach $temp_path (@INC)
{
if (-r "$temp_path/$language_file")
{$module_found=1;}
}
if ($module_found == 0)
{
$fatal_error=1;
$error_info .= "Unable to locate language file $language_file! It should be in the same directory as plans.cgi!\n";
}
else {require $language_file;}
}
}
else
{
$fatal_error=1;
$error_info .= "No language files defined in plans.config!\n";
}
# check for perl version
local $perl_version = (sprintf ("%vd",$^V));
my $temp = substr($perl_version,0,3);
if ($temp < 5.6) {
$fatal_error=1;
$error_info .= "Your version of perl ($perl_version) is too old! Plans requires perl version 5.6 or better.\n";
}
$module_found=0;
foreach $temp_path (@INC)
{
if (-e "$temp_path/$config_file")
{$module_found=1;}
}
if ($module_found == 0)
{
$fatal_error=1;
$error_info .= "Unable to locate config file $config_file! It should be in the same directory as upcoming_events.cgi!\n";
}
else {
}
if ($fatal_error == 1) # print error and bail out
{
&fatal_error();
}
# load discoveries
&load_discoveries();
$plans_url = $discoveries{script_url};
$theme_url = $discoveries{theme_url};
if ($theme_url eq "")
{$theme_url = "$plans_url/theme";}
$graphics_path ="$theme_url/graphics"; # where misc. graphics are
$icons_path = "$theme_url/icons"; # where icons are
$css_path = "$theme_url/plans.css"; # css file
# load config data
open (datafile, "$config_file") || ($debug_info .="\nunable to open file $config_file");
flock datafile,2;
my @config_lines=;
close datafile;
# strip #comments
foreach $config_line (@config_lines)
{
if ($config_line !~ /^\s+#/)
{
$config_data .= $config_line;
}
}
my %lists = %{&xml2hash($config_data,0)};
if ($current_calendar{custom_template} eq "" || $custom_template_file_found ==0)
{
if (!(-e "$options{default_template_path}"))
{
$fatal_error=1;
$error_info .= "Default template file:$options{default_template_path} does not appear to exist.";
&fatal_error();
}
else
{
open (FH, "$options{default_template_path}") || ($debug_info .=" unable to open default template file $options{default_template_path} for reading ");
flock FH,2;
my @template_lines=;
close FH;
$template_html = join "", @template_lines;
}
}
# load templates
&load_templates();
# load calendar data
&load_calendars();
# time-related globals
$rightnow = time() + 3600 * $current_calendar{gmtime_diff};
@rightnow_array = gmtime $rightnow;
$rightnow_year = $rightnow_array[5]+1900;
$rightnow_month = $rightnow_array[4];
$rightnow_mday = $rightnow_array[3];
$next_year = $rightnow_year+1;
$rightnow_description = formatted_time($rightnow, "hh:mm:ss mn md yy");
@weekday_sequence = @day_names;
my @lists_arrray;
if ($lists{upcoming_events_list} =~ /ARRAY/)
{
@lists_arrray = @{$lists{upcoming_events_list}};
}
else
{
@lists_arrray = ($lists{upcoming_events_list});
}
my $temp_list_index=0;
$debug_info .= <$rightnow_description
p1
foreach $list (@lists_arrray)
{
# remove leading and trailing whitespace
foreach $element (keys %{$list})
{
$list->{$element} =~ s/^\s+//;
$list->{$element} =~ s/\s+$//;
}
my $list_html = "";
my $output_file= $list->{output_filename};
$output_file =~ s/\s//g;
my @filestats = stat "$output_file";
my $last_modified_timestamp = $filestats[9];
my $nice_time = &formatted_time(($last_modified_timestamp + 3600 * $options{gmtime_diff}), "mn md yy hh:mm:ss");
$debug_info .= "\n\noutput_file: $output_file (last updated $nice_time)\n";
# extract timeframe
my $timeframe= $list->{timeframe};
$timeframe =~ s/\s//g;
# extract calendars to include
$list->{calendars_to_include} =~ s/\s//g;
my @calendars_to_show = split (',',$list->{calendars_to_include});
my %calendars_in_list;
foreach $calendar_to_show (@calendars_to_show)
{$calendars_in_list{$calendar_to_show} = 1;}
# load background calendars?
if ($list->{include_background_calendars} eq "dropdown")
{
#$debug_info .= "include background calendars!\n";
foreach $calendar_to_show (@calendars_to_show)
{
foreach $background_cal_id (keys %{$calendars{$calendar_to_show}{local_background_calendars}})
{
#$debug_info .= "background calendar: $background_cal_id\n";
if ($calendars_in_list{$background_cal_id} != 1)
{
push @calendars_to_show, $background_cal_id;
$calendars_in_list{$background_cal_id} = 1
}
}
}
}
# include background calendars in list?
if ($list->{include_background_calendars} eq "merge")
{
#$debug_info .= "include background calendars!\n";
foreach $calendar_to_show (@calendars_to_show)
{
foreach $background_cal_id (keys %{$calendars{$calendar_to_show}{local_background_calendars}})
{
#$debug_info .= "background calendar: $background_cal_id\n";
if ($calendars_in_list{$background_cal_id} != 1)
{
push @calendars_to_show, $background_cal_id;
$calendars_in_list{$background_cal_id} = 1
}
}
}
}
if ($list->{one_big_list} =~ /y/) # one big list?
{
$debug_info .= "Displaying as one big list\n";
# squelch all other calendars
my $main_calendar = $calendars_to_show[0];
foreach $calendar_to_show (@calendars_to_show)
{
if ($calendar_to_show ne $main_calendar)
{$calendars{$main_calendar}{local_background_calendars}{$calendar_to_show}=1;}
if ($list->{include_background_calendars} eq "merge")
{
foreach $background_cal_id (keys %{$calendars{$calendar_to_show}{local_background_calendars}})
{$calendars{$main_calendar}{local_background_calendars}{$background_cal_id}=1;}
}
}
@calendars_to_show = ();
#delete @calendars_to_show;
@calendars_to_show = ($main_calendar);
}
# $debug_info .= "Calendars in list: ";
# my $temp="";
# foreach $calendar_to_show (@calendars_to_show)
# {$temp .= " $calendars{$calendar_to_show}{title} ($calendar_to_show),";}
# chop $temp;
# $debug_info .= "$temp\n";
$list_start_timestamp = $rightnow;
$list_end_timestamp = $list_start_timestamp + 86400 * $timeframe;
my @temp2 = keys %calendars_in_list;
load_events($list_start_timestamp, $list_end_timestamp, \@temp2);
$current_cal = $calendars_to_show[0];
if ($current_calendar{custom_stylesheet} ne "")
{$css_path = "http://$current_calendar{custom_stylesheet}";}
my $default_event_list = "";
$default_event_list = &generate_upcoming_events($current_cal,$temp_list_index);
$list_html .=<