#!/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 .=<
p1 if (scalar @calendars_to_show > 1) { $list_html .=< p1 foreach $cal_id (@calendars_to_show) { $list_html .=<$calendars{$cal_id}{title} p1 } $list_html .=<
p1 } $list_html .=<
$default_event_list
p1 $temp_list_index++; open (datafile, ">$output_file") || ($debug_info .="unable to open data file $output_file for writing\n"); flock datafile,2; print datafile $list_html; close datafile; $html_output .= $list_html; $html_output .= <
p1 } $debug_info =~ s/\n/
/g; print < Upcoming events results: $debug_info

$html_output p1 exit(0); ############################################################################################################ sub generate_upcoming_events { my $return_text = ""; ($calendar_id, $temp_id) = @_; my @selected_cal_events; #$debug_info .= "include background calendars: ".$list->{include_background_calendars}."\n"; foreach $event_id (keys %events) { foreach $temp_cal_id (@{$events{$event_id}{cal_ids}}) { #$debug_info .= "checking event $event_id cal_id $temp_cal_id\n"; my $found = 0; if ($temp_cal_id eq $calendar_id) { push @selected_cal_events, $event_id; last; } if ($list->{include_background_calendars} eq "merge" || $list->{one_big_list} =~ /y/) { foreach $background_cal_id (keys %{$calendars{$calendar_id}{local_background_calendars}}) { if ($temp_cal_id eq $background_cal_id) { #$debug_info .= "match!\n"; push @selected_cal_events, $event_id; $found = 1; last; } } } if ($found==1) {last;} } } $return_text .=< p1 #display events for selected org foreach $event_id (sort {$events{$a}{start} <=> $events{$b}{start}} @selected_cal_events) { my %event = %{$events{$event_id}}; if (&time_overlap($events{$event_id}{start},$events{$event_id}{end},$list_start_timestamp, $list_end_timestamp)) { @event_start_timestamp_array = gmtime $event{start}; my $date_string; my $weekday_string; $date_string = &nice_date_range_format($event{start}, $event{end}, "-"); # abbreviate months for($l1=0;$l1<12;$l1++) {$date_string =~ s/$months[$l1]/$months_abv[$l1]/g;} # remove year $date_string =~ s/, \d{4}//g; if ($event{days} == 1) { #single-day event $date_string="$months_abv[$event_start_timestamp_array[4]] $event_start_timestamp_array[3]"; $weekday_string = $day_names_abv[$event_start_timestamp_array[6]] } else #multi-day event { @event_end_timestamp_array = gmtime $event{end}; if ($event_start_timestamp_array[4] eq $event_end_timestamp_array[4]) { $date_string="$months_abv[$event_start_timestamp_array[4]] $event_start_timestamp_array[3]-$event_end_timestamp_array[3]"; } else { $date_string="$months_abv[$event_start_timestamp_array[4]] $event_start_timestamp_array[3] - $months_abv[$event_end_timestamp_array[4]] $event_end_timestamp_array[3]"; } $weekday_string = "$day_names_abv[$event_start_timestamp_array[6]]-$day_names_abv[$event_end_timestamp_array[6]]"; } my $weekday_abv_string = $weekday_string; for ($l1=0;$l1/g; } if ($event{icon} eq "blank") { $icon_text .= "$unit_icon_text"; } else { $icon_text .= "$unit_icon_text\"\"/"; } $event{title} =~ s/^\s+//; my $event_time = ""; if ($event{all_day_event} ne "1") { $event_time = &nice_time_range_format($event{start}, $event{end}); $event_time = " $event_time "; } my $temp = $upcoming_item_template; $temp =~ s/###id###/$event_id/g; $temp =~ s/###time###/$event_time/g; $temp =~ s/###date###/$date_string/g; $temp =~ s/###background color###/$event{bgcolor}/g; $temp =~ s/###icon###/$icon_text/g; $temp =~ s/###title###/$event{title}/g; $temp_item_text =~ s/###weekday###/$weekday_string/g; $temp_item_text =~ s/###weekday_abv###/$weekday_abv_string/g; $return_text .= $temp; } } $return_text .=< p1 return $return_text; } #********************end generate_upcoming_events subroutine********************** sub generate_event_details_javascript { my ($events_start_timestamp, $events_end_timestamp) = @_; my $return_string=""; my $num_events = 0; $index=0; #loop through the events, check to see if they fall #within the current calendar month #debug end timestamp foreach $event_id (keys %events) { my %event = %{$events{$event_id}}; if (&time_overlap($event{start},$event{end},$events_start_timestamp,$events_end_timestamp)) { #$debug_info .= "event details for $event{title}\n"; $num_events++; #show cal_details my $cal_detail_text=""; #$debug_info .= "generating event details for $event{title}\n"; my $event_details = &generate_event_details(\%event); $event_detail_text =</g; $html_output .=< Plans error! Plans error:
$error_info p1 if ($debug_info ne "") { $debug_info =~ s/\n/
/g; $html_output .=< Debug info:
$debug_info p1 } $html_output .=< p1 print $html_output; exit(0); }