Fixing http%3A bug for after adding a new iCal Calendar
[plans.git] / email_reminders.cgi
blobb3e5523837e18638bd3f2fb9de4215deb9c3a4e4
1 #!/usr/bin/perl
3 # before anything else, the script needs to find out its own name
5 # some servers (notably IIS on windows) don't set the cwd to the script's
6 # directory before executing it. So we get that information
7 # from $0 (the full name & path of the script).
8 BEGIN{($_=$0)=~s![\\/][^\\/]+$!!;push@INC,$_}
10 $name = $0;
11 $name =~ s/.+\/.+\///; # for unix
12 $name =~ s/.+\\.+\\//; # for windows
13 $path = $0;
14 $path =~ s/(.+\/).+/$1/g; # for unix
15 $path =~ s/(.+\\).+/$1/g; # for windows
18 if ($path ne "") {
19 chdir $path;
20 push @INC,$path;
22 # check for required modules.
24 my $module_found=0;
25 foreach $temp_path (@INC) {
26 if (-e "$temp_path/CGI")
27 {$module_found=1;}
30 if ($module_found == 0) {
31 $fatal_error=1;
32 $error_info .= "unable to locate required module <b>CGI</b>!\n";
34 else
35 {use CGI;}
38 $module_found=0;
39 foreach $temp_path (@INC) {
40 if (-e "$temp_path/CGI/Carp.pm")
41 {$module_found=1;}
44 if ($module_found == 0) {
45 $fatal_error=1;
46 $error_info .= "unable to locate required module <b>CGI::Carp</b>!\n";
48 else
49 {use CGI::Carp qw/fatalsToBrowser/;}
51 $module_found=0;
52 foreach $temp_path (@INC) {
53 if (-e "$temp_path/Time")
54 {$module_found=1;}
57 if ($module_found == 0) {
58 $fatal_error=1;
59 $error_info .= "unable to locate required module <b>Time.pm</b>!\n";
61 else
62 {use Time::Local;}
64 $module_found=0;
65 foreach $temp_path (@INC) {
66 if (-e "$temp_path/IO.pm")
67 {$module_found=1;}
70 if ($module_found == 0) {
71 $fatal_error=1;
72 $error_info .= "unable to locate required module <b>IO.pm</b>!\n";
73 } else
74 {use IO::Socket;}
76 if ($fatal_error == 1) {
77 &fatal_error(); # print error and bail out
80 $module_found=0;
81 foreach $temp_path (@INC) {
82 if (-e "$temp_path/plans_config.pl")
83 {$module_found=1;}
86 if ($module_found == 0) {
87 $fatal_error=1;
88 $error_info .= "Unable to locate <b>plans_config.pl</b>! It should be in the same directory as plans.cgi!\n";
90 else {require "plans_config.pl";}
92 $module_found=0;
93 foreach $temp_path (@INC) {
94 if (-r "$temp_path/plans_lib.pl")
95 {$module_found=1;}
98 if ($module_found == 0) {
99 $fatal_error=1;
100 $error_info .= "Unable to locate <b>plans_lib.pl</b>! It should be in the same directory as plans.cgi!\n";
102 else {require "plans_lib.pl";}
104 # get the language file, if one is defined
106 if (defined $options{language_files}) {
107 my @language_files = split(',', $options{language_files});
109 foreach $language_file (@language_files) {
111 $module_found=0;
112 foreach $temp_path (@INC) {
113 if (-r "$temp_path/$language_file")
114 {$module_found=1;}
117 if ($module_found == 0) {
118 $fatal_error=1;
119 $error_info .= "Unable to locate language file <b>$language_file</b>! It should be in the same directory as plans.cgi!\n";
121 else {require $language_file;}
123 } else {
124 $fatal_error=1;
125 $error_info .= "No language files defined in plans.config!\n";
128 # check for perl version
129 local $perl_version = (sprintf ("%vd",$^V));
130 my $temp = substr($perl_version,0,3);
131 if ($temp < 5.6) {
132 $fatal_error=1;
133 $error_info .= "Your version of perl ($perl_version) is too old! Plans requires perl version 5.6 or better.\n";
137 if ($fatal_error == 1) {
138 &fatal_error(); # print error and bail out
141 my $new_lines = "";
142 my @events_in_file = ();
144 &load_calendars();
145 local $current_cal_id = 0;
148 # load upcoming event data
149 open (FH, "$options{email_reminders_datafile}") || ($debug_info .="\nUnable to open file $options{email_reminders_datafile}");
150 flock FH,2;
151 my @lines=<FH>;
152 close datafile;
154 foreach $line (@lines) { # need pre-load to ensure we only need one call to normalize_timezone
155 if ($line !~ /\w/) {next}; # skip blank spaces
157 my $temp_line = $line;
158 $temp_line =~ s/<\/?email_reminder>//g;
159 my ($evt_id) = &xml_quick_extract($temp_line, "evt_id");
160 my ($before) = &xml_quick_extract($temp_line, "before");
161 my ($script_url) = &xml_quick_extract($temp_line, "script_url");
162 $script_url = &decode($script_url);
163 my ($extra_text) = &xml_quick_extract($temp_line, "extra_text");
164 $extra_text = &decode($extra_text);
165 my ($to_address) = &xml_quick_extract($temp_line, "email_address");
166 $to_address = &decode($to_address);
168 &load_event($evt_id);
169 push @events_in_file, {evt_id => $evt_id,
170 before => $before,
171 script_url => $script_url,
172 extra_text => $extra_text,
173 to_address => $to_address,
174 line => $line};
176 #push @events_to_remind, $evt_id;
178 &normalize_timezone();
180 my $results = "";
182 foreach $event_reminder_ref (@events_in_file) {
183 my %event_reminder_stuff = %{$event_reminder_ref};
184 my $evt_id = $event_reminder_stuff{evt_id};
186 %current_event = %{$events{$evt_id}};
187 my $current_cal_id = $current_event{cal_ids}[0];
189 %current_calendar = %{$calendars{$current_cal_id}};
191 $rightnow = time() + 3600 * $current_calendar{gmtime_diff};
193 my $to_address = $event_reminder_stuff{to_address};
194 my $extra_text = $event_reminder_stuff{extra_text};
195 my $script_url = $event_reminder_stuff{script_url};
196 my $before = $event_reminder_stuff{before};
198 my $event_timestamp = $current_event{start};
199 $date_string = &nice_date_range_format($current_event{start}, $current_event{end}, " - ");
201 my $event_time = "";
202 if ($current_event{all_day_event} ne "1") {
203 $event_time = &nice_time_range_format($current_event{start}, $current_event{end});
206 my $reminder_text = $lang{email_reminder_text};
208 $reminder_text =~ s/###time###/$event_time/g;
209 $reminder_text =~ s/###title###/$current_event{title}/g;
210 $reminder_text =~ s/###date###/$date_string/g;
211 $reminder_text =~ s/###details###/$current_event{details}/g;
212 $reminder_text =~ s/###extra text###/$extra_text/g;
213 $reminder_text =~ s/###link###/$script_url?view_event=1&evt_id=$current_event{id}/g;
215 my $check_timestamp = $event_timestamp;
216 $check_timestamp -= $calendars{$current_event{cal_ids}[0]}{gmtime_diff};
218 if (($check_timestamp - $rightnow) < $before) {
219 if ($current_event{title} ne "") { # blank title == deleted event
220 $test_reminder_results = &send_email_reminder(\%current_event, $to_address, $reminder_text);
221 if ($test_reminder_results eq "1") {
222 $results .= "Reminder for event $evt_id ($current_event{title}) sent successfully to <i>$to_address</i>!\n";
223 } else {
224 $results .= "Reminder not sent to <i>$to_address</i>:<br/><br/>($test_reminder_results)\n";
227 } else {
228 $new_lines .= $event_reminder_stuff{line};
232 open (FH, ">$options{email_reminders_datafile}") || ($debug_info .="\nUnable to open file $options{email_reminders_datafile} for writing!");
233 flock FH,2;
234 print FH $new_lines;
235 close datafile;
237 $results = "No email reminders to send!" if ($results eq "");
239 $results =~ s/\n/<br\/>\n/g;
240 $debug_info =~ s/\n/<br\/>\n/g;
242 print <<p1;
243 Cache-control: no-cache,no-store,private
244 Content-Type: text/html; charset=iso-8859-1\n
245 <html>
246 <body>
247 $results
248 $debug_info
249 </html>
250 </body>
253 sub fatal_error() {
254 $error_info =~ s/\n/<br>/g;
256 $html_output .=<<p1;
257 Cache-control: no-cache,no-store,private
258 Content-Type: text/html; charset=iso-8859-1\n
259 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
260 <html>
261 <head>
262 <title>Plans error!</title>
263 </head>
264 <body>
266 <b>Plans error:</b><br>
267 $error_info
269 if ($debug_info ne "") {
270 $debug_info =~ s/\n/<br>/g;
271 $html_output .=<<p1;
272 <hr>
273 Debug info:<br>
274 $debug_info
279 $html_output .=<<p1;
280 </body>
281 </html>
284 print $html_output;
285 exit(0);