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