Programmupdate:
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / connscheduler.cgi
1 #!/usr/bin/perl
2 #
3 # IPCop Connection Scheduler Web-Iface
4 #
5 # This code is distributed under the terms of the GPL
6 #
7 #
8 # 2006-04-xx created by weizen_42 at ipcop-forum dot de
9 # 2006-06-xx added reboot and shutdown
10 # 2006-08-20 added ipsecctrl
11 #
12
13
14 use strict;
15
16 # enable only the following on debugging purpose
17 use warnings;
18 use CGI::Carp 'fatalsToBrowser';
19
20 require '/var/ipfire/general-functions.pl';
21 require "${General::swroot}/lang.pl";
22 require "${General::swroot}/header.pl";
23
24 require '/var/ipcop/connscheduler/lib.pl';
25
26 my $buttontext = $Lang::tr{'add'};
27 my $hiddenvalue = 'add';
28 my $day;
29 my $hour;
30 my $minute;
31 my %temppppsettings=();
32 my @profilenames=();
33
34
35 #
36 # defaults for settings
37 #
38 my $selected_hour = '00';
39 my $selected_minute = '00';
40 my $checked_connect = "checked='checked'";
41 my $checked_profile = '';
42 my %selected = ();
43 $selected{'reconnect'} = '';
44 $selected{'dial'} = '';
45 $selected{'hangup'} = '';
46 $selected{'reboot'} = '';
47 $selected{'shutdown'} = '';
48 $selected{'ipsecstart'} = '';
49 $selected{'ipsecstop'} = '';
50 my $selected_profile = 1;
51 my $checked_days = "checked='checked'";
52 my $selected_daystart = 1;
53 my $selected_dayend = 31;
54 my $checked_weekdays = '';
55 my $checked_mon = "checked='checked'";
56 my $checked_tue = "checked='checked'";
57 my $checked_wed = "checked='checked'";
58 my $checked_thu = "checked='checked'";
59 my $checked_fri = "checked='checked'";
60 my $checked_sat = "checked='checked'";
61 my $checked_sun = "checked='checked'";
62 my $comment = '';
63
64 my %cgiparams = ();
65
66 $cgiparams{'ACTION'} = ''; # add/edit/update/remove/wakeup
67 $cgiparams{'ACTION_ACTION'} = ''; # CONNECT/PROFILE
68 $cgiparams{'ACTION_CONNECT'} = ''; # connect/disconnect/reconnect
69 $cgiparams{'ACTION_PROFILENR'} = 0;
70 $cgiparams{'ACTION_HOUR'} = '';
71 $cgiparams{'ACTION_MINUTE'} = '';
72 $cgiparams{'ACTION_DAYSTYPE'} = '';
73 $cgiparams{'ACTION_DAYSTART'} = 1;
74 $cgiparams{'ACTION_DAYEND'} = 31;
75 $cgiparams{'Mon'} = '';
76 $cgiparams{'Tue'} = '';
77 $cgiparams{'Wed'} = '';
78 $cgiparams{'Thu'} = '';
79 $cgiparams{'Fri'} = '';
80 $cgiparams{'Sat'} = '';
81 $cgiparams{'Sun'} = '';
82 $cgiparams{'ACTION_COMMENT'} = '';
83
84 &Header::getcgihash(\%cgiparams);
85
86
87 # read the profile names
88 my $i=0;
89 for ($i = 1; $i <= $CONNSCHED::maxprofiles; $i++)
90 {
91 %temppppsettings = ();
92 $temppppsettings{'PROFILENAME'} = $Lang::tr{'empty'};
93 &General::readhash("${General::swroot}/ppp/settings-$i", \%temppppsettings);
94 $profilenames[$i] = $temppppsettings{'PROFILENAME'};
95 }
96
97 &Header::showhttpheaders();
98 &Header::openpage($Lang::tr{'connscheduler'}, 1, '');
99 &Header::openbigbox('100%', 'left', '', '');
100
101
102 # Found this usefull piece of code in BlockOutTraffic AddOn 8-)
103 # fwrules.cgi
104 ###############
105 # DEBUG DEBUG
106 #&Header::openbox('100%', 'left', 'DEBUG');
107 #my $debugCount = 0;
108 #foreach my $line (sort keys %cgiparams) {
109 # print "$line = $cgiparams{$line}<br />\n";
110 # $debugCount++;
111 #}
112 #print "&nbsp;Count: $debugCount\n";
113 #&Header::closebox();
114 # DEBUG DEBUG
115 ###############
116
117
118 if ( $cgiparams{'ACTION'} eq 'toggle' )
119 {
120 if ( $CONNSCHED::config[$cgiparams{'ID'}]{'ACTIVE'} eq 'on' )
121 {
122 $CONNSCHED::config[$cgiparams{'ID'}]{'ACTIVE'} = 'off';
123 }
124 else
125 {
126 $CONNSCHED::config[$cgiparams{'ID'}]{'ACTIVE'} = 'on';
127 }
128
129 &CONNSCHED::WriteConfig;
130 }
131
132 if ( ($cgiparams{'ACTION'} eq 'add') || ($cgiparams{'ACTION'} eq 'update') )
133 {
134 my $l_action = $cgiparams{'ACTION_CONNECT'};
135 my $l_profilenr = '';
136 my $l_days = '';
137 my $l_weekdays = '';
138
139 if ( $cgiparams{'ACTION'} eq 'add' )
140 {
141 $i = $#CONNSCHED::config + 1;
142 $CONNSCHED::config[$i]{'ACTIVE'} = 'on';
143 }
144 else
145 {
146 $i = $cgiparams{'UPDATE_ID'};
147 }
148
149 if ( $cgiparams{'ACTION_ACTION'} eq 'PROFILE')
150 {
151 $l_action = 'select profile';
152 $l_profilenr = $cgiparams{'ACTION_PROFILENR'};
153 }
154
155 if ( $cgiparams{'ACTION_DAYSTYPE'} eq 'WEEKDAYS' )
156 {
157 if ( $cgiparams{'Mon'} eq 'on' ) { $l_weekdays .= 'Mon '; }
158 if ( $cgiparams{'Tue'} eq 'on' ) { $l_weekdays .= 'Tue '; }
159 if ( $cgiparams{'Wed'} eq 'on' ) { $l_weekdays .= 'Wed '; }
160 if ( $cgiparams{'Thu'} eq 'on' ) { $l_weekdays .= 'Thu '; }
161 if ( $cgiparams{'Fri'} eq 'on' ) { $l_weekdays .= 'Fri '; }
162 if ( $cgiparams{'Sat'} eq 'on' ) { $l_weekdays .= 'Sat '; }
163 if ( $cgiparams{'Sun'} eq 'on' ) { $l_weekdays .= 'Sun '; }
164 }
165 else
166 {
167 $l_days = "$cgiparams{'ACTION_DAYSTART'} - $cgiparams{'ACTION_DAYEND'}";
168 }
169
170 $CONNSCHED::config[$i]{'ACTION'} = $l_action;
171 $CONNSCHED::config[$i]{'PROFILENR'} = $l_profilenr;
172 $CONNSCHED::config[$i]{'TIME'} = "$cgiparams{'ACTION_HOUR'}:$cgiparams{'ACTION_MINUTE'}";
173 $CONNSCHED::config[$i]{'DAYSTYPE'} = lc($cgiparams{'ACTION_DAYSTYPE'});
174 $CONNSCHED::config[$i]{'DAYS'} = $l_days;
175 $CONNSCHED::config[$i]{'WEEKDAYS'} = $l_weekdays;
176 $CONNSCHED::config[$i]{'COMMENT'} = &Header::cleanhtml($cgiparams{'ACTION_COMMENT'});
177
178 &CONNSCHED::WriteConfig;
179 }
180
181 if ( $cgiparams{'ACTION'} eq 'edit' )
182 {
183 $i = $cgiparams{'ID'};
184
185 $selected_hour = substr($CONNSCHED::config[$i]{'TIME'},0,2);
186 $selected_minute = substr($CONNSCHED::config[$i]{'TIME'},3,2);
187
188 if ( $CONNSCHED::config[$i]{'ACTION'} eq 'select profile' )
189 {
190 $checked_connect = '';
191 $checked_profile = "checked='checked'";
192 $selected_profile = $CONNSCHED::config[$i]{'PROFILENR'};
193 }
194 else
195 {
196 $selected{"$CONNSCHED::config[$i]{'ACTION'}"} = "selected='selected'";
197 }
198
199 if ( $CONNSCHED::config[$i]{'DAYSTYPE'} eq 'days' )
200 {
201 my @temp = split(/-/,$CONNSCHED::config[$i]{'DAYS'},2);
202
203 $selected_daystart = substr($temp[0], 0, -1);
204 $selected_dayend = substr($temp[1], 1);
205 }
206 else
207 {
208 my $wd = $CONNSCHED::config[$i]{'WEEKDAYS'};
209 $checked_mon = '' if ( index($wd, 'Mon') == -1 ) ;
210 $checked_tue = '' if ( index($wd, 'Tue') == -1 ) ;
211 $checked_wed = '' if ( index($wd, 'Wed') == -1 ) ;
212 $checked_thu = '' if ( index($wd, 'Thu') == -1 ) ;
213 $checked_fri = '' if ( index($wd, 'Fri') == -1 ) ;
214 $checked_sat = '' if ( index($wd, 'Sat') == -1 ) ;
215 $checked_sun = '' if ( index($wd, 'Sun') == -1 ) ;
216
217 $checked_days = '';
218 $checked_weekdays = "checked='checked'";
219 }
220
221 $comment = $CONNSCHED::config[$cgiparams{'ID'}]{'COMMENT'};
222
223 $buttontext = $Lang::tr{'update'};
224 $hiddenvalue = 'update';
225 }
226
227 if ( $cgiparams{'ACTION'} eq 'remove' )
228 {
229 # simply set ACTIVE to empty, WriteConfig will handle the gory details
230 $CONNSCHED::config[$cgiparams{'ID'}]{'ACTIVE'} = '';
231 &CONNSCHED::WriteConfig;
232 }
233 if ( ($cgiparams{'ACTION'} eq 'down') || ($cgiparams{'ACTION'} eq 'up') )
234 {
235 my $action = @CONNSCHED::config[$cgiparams{'ID'}];
236 my $newpos = 0;
237
238 splice(@CONNSCHED::config, $cgiparams{'ID'}, 1);
239
240 if ( ($cgiparams{'ACTION'} eq 'down') )
241 {
242 $newpos = $cgiparams{'ID'} + 1;
243 }
244 else
245 {
246 $newpos = $cgiparams{'ID'} - 1;
247 }
248
249 splice(@CONNSCHED::config, $newpos, 0, $action);
250
251 &CONNSCHED::WriteConfig;
252 }
253
254
255 #
256 # Add / Edit Box
257 #
258
259 &Header::openbox('100%', 'left', $Lang::tr{'ConnSched add action'});
260
261 print <<END
262 <form method='post' name='addevent' action='$ENV{'SCRIPT_NAME'}'>
263 <table width='100%' border='0' cellspacing='6' cellpadding='0'>
264 <tr>
265 <td width='15%' class='base'>$Lang::tr{'ConnSched time'}</td>
266 <td><select name='ACTION_HOUR'>
267 END
268 ;
269 for ($hour = 0; $hour <= 23; $hour++)
270 {
271 my $hour00 = $hour < 10 ? "0$hour" : $hour;
272 if ( $hour00 eq $selected_hour )
273 {
274 print "<option value='$hour00' selected='selected'>$hour00</option>";
275 }
276 else
277 {
278 print "<option value='$hour00'>$hour00</option>";
279 }
280 }
281 print "</select>&nbsp;:&nbsp;<select name='ACTION_MINUTE'>";
282 for ($minute = 0; $minute <= 55; $minute += 5)
283 {
284 my $minute00 = $minute < 10 ? "0$minute" : $minute;
285 if ( $minute00 eq $selected_minute )
286 {
287 print "<option value='$minute00' selected='selected'>$minute00</option>";
288 }
289 else
290 {
291 print "<option value='$minute00'>$minute00</option>";
292 }
293 }
294
295 print <<END
296 </select></td></tr>
297 <tr><td colspan='2'><hr /></td></tr>
298 <tr><td width='15%' class='base'>$Lang::tr{'ConnSched action'}</td><td>
299 <input type='radio' value='CONNECT' name='ACTION_ACTION' $checked_connect />&nbsp;<select name='ACTION_CONNECT'>
300 <option value='reconnect' $selected{'reconnect'}>$Lang::tr{'ConnSched reconnect'}</option>
301 <option value='dial' $selected{'dial'}>$Lang::tr{'dial'}</option>
302 <option value='hangup' $selected{'hangup'}>$Lang::tr{'hangup'}</option>
303 <option value='reboot' $selected{'reboot'}>$Lang::tr{'reboot'}</option>
304 <option value='shutdown' $selected{'shutdown'}>$Lang::tr{'shutdown'}</option>
305 <option value='ipsecstart' $selected{'ipsecstart'}>$Lang::tr{'ConnSched ipsecstart'}</option>
306 <option value='ipsecstop' $selected{'ipsecstop'}>$Lang::tr{'ConnSched ipsecstop'}</option>
307 </select></td></tr>
308 <tr><td width='15%' class='base'>&nbsp;</td>
309 <td><input type='radio' value='PROFILE' name='ACTION_ACTION' $checked_profile />&nbsp;$Lang::tr{'ConnSched change profile title'}&nbsp;<select name='ACTION_PROFILENR'>
310 END
311 ;
312 for ($i = 1; $i <= $CONNSCHED::maxprofiles; $i++)
313 {
314 if ( $i == $selected_profile )
315 {
316 print "<option value='$i' selected='selected'>$i. $profilenames[$i]</option>";
317 }
318 else
319 {
320 print "<option value='$i'>$i. $profilenames[$i]</option>";
321 }
322 }
323 print <<END
324 </select></td></tr>
325 <tr><td colspan='2'><hr /></td></tr>
326 <tr><td width='15%' class='base'>$Lang::tr{'ConnSched days'}</td>
327 <td><input type='radio' value='DAYS' name='ACTION_DAYSTYPE' $checked_days />&nbsp;<select name='ACTION_DAYSTART'>
328 END
329 ;
330 for ($day = 1; $day <= 31; $day++)
331 {
332 if ( $day == $selected_daystart )
333 {
334 print "<option value='$day' selected='selected'>$day</option>";
335 }
336 else
337 {
338 print "<option value='$day'>$day</option>";
339 }
340 }
341 print "</select>&nbsp;-&nbsp;<select name='ACTION_DAYEND'>";
342 for ($day = 1; $day <= 31; $day++)
343 {
344 if ( $day == $selected_dayend )
345 {
346 print "<option value='$day' selected='selected'>$day</option>";
347 }
348 else
349 {
350 print "<option value='$day'>$day</option>";
351 }
352 }
353
354 print <<END
355 </select></td></tr>
356 <tr><td width='15%' class='base'>&nbsp;</td><td><input type='radio' value='WEEKDAYS' name='ACTION_DAYSTYPE' $checked_weekdays />&nbsp;$Lang::tr{'ConnSched weekdays'}<br />
357 &nbsp;&nbsp;<input type='checkbox' name='Mon' $checked_mon />$Lang::tr{'monday'}<br />
358 &nbsp;&nbsp;<input type='checkbox' name='Tue' $checked_tue />$Lang::tr{'tuesday'}<br />
359 &nbsp;&nbsp;<input type='checkbox' name='Wed' $checked_wed />$Lang::tr{'wednesday'}<br />
360 &nbsp;&nbsp;<input type='checkbox' name='Thu' $checked_thu />$Lang::tr{'thursday'}<br />
361 &nbsp;&nbsp;<input type='checkbox' name='Fri' $checked_fri />$Lang::tr{'friday'}<br />
362 &nbsp;&nbsp;<input type='checkbox' name='Sat' $checked_sat />$Lang::tr{'saturday'}<br />
363 &nbsp;&nbsp;<input type='checkbox' name='Sun' $checked_sun />$Lang::tr{'sunday'}
364 </td></tr>
365 <tr><td colspan='2'><hr /></td></tr>
366 <tr><td width='15%' class='base'>$Lang::tr{'remark title'}&nbsp;<img src='/blob.gif' alt='*' /></td>
367 <td><input type='text' name='ACTION_COMMENT' size='40' value='$comment' /></td></tr></table>
368 <hr />
369 <table width='100%'><tr>
370 <td class='base' valign='top'><img src='/blob.gif' alt='*' /></td>
371 <td width='55%' class='base'>$Lang::tr{'this field may be blank'}</td>
372 <td width='40%' align='center'><input type='submit' name='SUBMIT' value='$buttontext' />
373 <input type='hidden' name='ACTION' value='$hiddenvalue' /></td>
374 <input type='hidden' name='UPDATE_ID' value='$cgiparams{'ID'}' /></td>
375 </tr></table>
376 </form>
377
378 END
379 ;
380
381 &Header::closebox();
382
383 #
384 # Box with List of events
385 #
386
387 &Header::openbox('100%', 'left', $Lang::tr{'ConnSched scheduled actions'});
388 print <<END
389 <table width='100%' border='0' cellspacing='1' cellpadding='0'>
390 <tr>
391 <td align='center' width='10%'><b>$Lang::tr{'time'}</b></td>
392 <td width='15%'>&nbsp;</td>
393 <td align='center' width='60%'><b>$Lang::tr{'remark'}</b></td>
394 <td align='center' colspan='5' width='5%'><b>$Lang::tr{'action'}</b></td>
395 </tr>
396 END
397 ;
398
399 for my $id ( 0 .. $#CONNSCHED::config )
400 {
401 my $trcolor;
402
403 if ( ($cgiparams{'ACTION'} eq 'edit') && ($id == $cgiparams{'ID'}) )
404 {
405 $trcolor = "<tr bgcolor='${Header::colouryellow}'>";
406 }
407 elsif ( $id % 2 )
408 {
409 $trcolor = "<tr bgcolor='$Header::table2colour'>";
410 }
411 else
412 {
413 $trcolor = "<tr bgcolor='$Header::table1colour'>";
414 }
415
416 print <<END
417 $trcolor
418 <td align='center'>$CONNSCHED::config[$id]{'TIME'}</td>
419 <td>$Lang::tr{"ConnSched $CONNSCHED::config[$id]{'ACTION'}"}&nbsp;$CONNSCHED::config[$id]{'PROFILENR'}</td>
420 <td>$CONNSCHED::config[$id]{'COMMENT'}</td>
421 <td align='center'>
422 <form method='post' name='frm$id' action='$ENV{'SCRIPT_NAME'}'>
423 <input type='hidden' name='ACTION' value='toggle' />
424 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$CONNSCHED::config[$id]{'ACTIVE'}.gif' alt='$Lang::tr{'toggle enable disable'}' title='$Lang::tr{'toggle enable disable'}' />
425 <input type='hidden' name='ID' value='$id' />
426 </form>
427 </td>
428 <td align='center'>
429 <form method='post' name='frm$id' action='$ENV{'SCRIPT_NAME'}'>
430 <input type='hidden' name='ACTION' value='edit' />
431 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
432 <input type='hidden' name='ID' value='$id' />
433 </form>
434 </td>
435 <td align='center'>
436 <form method='post' name='frm$id' action='$ENV{'SCRIPT_NAME'}'>
437 <input type='hidden' name='ACTION' value='remove' />
438 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
439 <input type='hidden' name='ID' value='$id' />
440 </form>
441 </td>
442 <td align='center'>
443 <form method='post' name='frm$id' action='$ENV{'SCRIPT_NAME'}'>
444 <input type='hidden' name='ACTION' value='up' />
445 <input type='image' name='$Lang::tr{'up'}' src='/images/up.gif' alt='$Lang::tr{'ConnSched up'}' title='$Lang::tr{'ConnSched up'}' />
446 <input type='hidden' name='ID' value='$id' />
447 </form>
448 </td>
449 <td align='center'>
450 <form method='post' name='frm$id' action='$ENV{'SCRIPT_NAME'}'>
451 <input type='hidden' name='ACTION' value='down' />
452 <input type='image' name='$Lang::tr{'down'}' src='/images/down.gif' alt='$Lang::tr{'ConnSched down'}' title='$Lang::tr{'ConnSched down'}' />
453 <input type='hidden' name='ID' value='$id' />
454 </form>
455 </td>
456 </tr>
457 $trcolor
458 <td>&nbsp;</td>
459 <td colspan='7'>$CONNSCHED::config[$id]{'DAYS'}$CONNSCHED::config[$id]{'WEEKDAYS_PR'}&nbsp;</td>
460 </tr>
461 END
462 ;
463 }
464
465 print <<END
466 </table>
467 <br />
468 <hr />
469 <table width='100%'>
470 <tr>
471 <td>&nbsp;</td>
472 <td align='right'>
473 <b><small><a href="http://www.ban-solms.de/t/IPCop.html" target="_blank">Connection Scheduler $CONNSCHED::version</a></small></b>
474 </td>
475 </tr>
476 </table>
477 END
478 ;
479
480
481 &Header::closebox();
482
483 &Header::closebigbox();
484 &Header::closepage();