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