Gro├čes Update:
[ipfire-2.x.git] / html / cgi-bin / shutdown.cgi
CommitLineData
ac1cfefa
MT
1#!/usr/bin/perl
2#
3# SmoothWall CGIs
4#
5# This code is distributed under the terms of the GPL
6#
7# (c) The SmoothWall Team
8#
ac1cfefa
MT
9
10use strict;
11
12# enable only the following on debugging purpose
13#use warnings;
14#use CGI::Carp 'fatalsToBrowser';
15
16require 'CONFIG_ROOT/general-functions.pl';
17require "${General::swroot}/lang.pl";
18require "${General::swroot}/header.pl";
19
20my %cgiparams=();
21my $death = 0;
22my $rebirth = 0;
23my $default_time = '03:15';
24
25&Header::showhttpheaders();
26
27$cgiparams{'ACTION'} = '';
28&Header::getcgihash(\%cgiparams);
29
30if ($cgiparams{'ACTION'} eq $Lang::tr{'shutdown'}) {
31 $death = 1;
78331e30 32 &General::log($Lang::tr{'shutting down ipfire'});
8891c0e8
MT
33 #system '/usr/local/bin/ipfiredeath';
34 system '/usr/local/bin/ipfirereboot down';
ac1cfefa
MT
35} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'reboot'}) {
36 $rebirth = 1;
78331e30 37 &General::log($Lang::tr{'rebooting ipfire'});
8891c0e8
MT
38 #system '/usr/local/bin/ipfirerebirth';
39 system '/usr/local/bin/ipfirereboot boot';
ac1cfefa
MT
40} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
41 my $days='';
42 my $n = 1;
43 # build list of days
44 map ($cgiparams{$_} eq 'on' ? $days .= ",".$n++ : $n++ ,
45 ('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY') );
46
47 # if days is empty, it is a remove else it is a change
48 if (length ($days)){
49 substr($days,0,1) = ''; #kill front comma
50 &General::log("Scheduling reboot on $days at $cgiparams{'TIME'}");
51 my $min;
52 my $hour;
53 ($hour,$min) = split (':', $cgiparams{'TIME'});
54 $days = "'*'" if ($days eq '1,2,3,4,5,6,7');
55 my $mode = ($cgiparams{'MODE'} eq 'halt') ? '-h' : '-r';
8891c0e8 56 system "/usr/local/bin/ipfirereboot cron+ $min $hour $days $mode"; #reboot checks values of $hour & $min
ac1cfefa
MT
57 } else {
58 &General::log("Remove scheduled reboot");
8891c0e8 59 system '/usr/local/bin/ipfirereboot cron-';
ac1cfefa
MT
60 }
61}
62if ($death == 0 && $rebirth == 0) {
63
64 &Header::openpage($Lang::tr{'shutdown control'}, 1, '');
65
66 &Header::openbigbox('100%', 'left');
67
68 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
69
70 &Header::openbox('100%', 'left', $Lang::tr{'shutdown2'});
71 print <<END
72<table width='100%'>
73<tr>
74 <td width='50%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reboot'}' /></td>
75 <td width='50%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'shutdown'}' /></td>
76</tr>
77</table>
78END
79 ;
80 &Header::closebox();
81
82 &Header::openbox('100%', 'left', $Lang::tr{'reboot schedule'});
83 my %checked=();
84 my $reboot_at = $default_time;
85 my $days = '';
86
87 #decode the shutdown line stored in crontab
88 #get the line
8891c0e8 89 open(FILE, "/usr/local/bin/ipfirereboot cron?|");
ac1cfefa
MT
90 my $schedule = <FILE>;
91 close (FILE);
92
93 if ($schedule) { # something exist
94 $schedule =~ /(\d+) (\d+) \* \* ([1234567*,]+) .* (-[h|r])/;
95 $reboot_at = sprintf("%.02d",$2) . ':' . sprintf("%.02d",$1); # hour (03:45)
96 $days = $3; # 1,2,3... or *
97 if ($4 eq '-h') {
98 $checked{'MODE'}{'halt'} = "checked='checked'";
99 } else {
100 $checked{'MODE'}{'reboot'} = "checked='checked'";
101 }
102 }
103 #decode $days
104 if ($days eq '*') {
105 $checked{'MONDAY'} = "checked='checked'";
106 $checked{'TUESDAY'} = "checked='checked'";
107 $checked{'WEDNESDAY'} = "checked='checked'";
108 $checked{'THURSDAY'} = "checked='checked'";
109 $checked{'FRIDAY'} = "checked='checked'";
110 $checked{'SATURDAY'} = "checked='checked'";
111 $checked{'SUNDAY'} = "checked='checked'";
112 } else {
113 $checked{'MONDAY'} = "checked='checked'" if ($days =~ /1/);
114 $checked{'TUESDAY'} = "checked='checked'" if ($days =~ /2/);
115 $checked{'WEDNESDAY'} = "checked='checked'" if ($days =~ /3/);
116 $checked{'THURSDAY'} = "checked='checked'" if ($days =~ /4/);
117 $checked{'FRIDAY'} = "checked='checked'" if ($days =~ /5/);
118 $checked{'SATURDAY'} = "checked='checked'" if ($days =~ /6/);
119 $checked{'SUNDAY'} = "checked='checked'" if ($days =~ /7/);
120 }
121
122 print <<END
123<table width='100%'>
124<tr>
125 <td class='boldbase' colspan='2'><b>$Lang::tr{'time'}</b></td>
126 <td class='boldbase' colspan='2'><b>$Lang::tr{'day'}</b></td>
127 <td class='boldbase'><b>$Lang::tr{'action'}</b></td>
128</tr>
129<tr>
130END
131 ;
132 print "<td align='left' width='15%' class='base' valign='top' rowspan='2'>", &select_hour_var("TIME", $reboot_at);
133 print <<END
134</td>
135 <td>
136 <input type='checkbox' name='MONDAY' $checked{'MONDAY'}></td>
137 <td width='15%' class='base'>
138 $Lang::tr{'monday'}</td>
139 <td>
140 <input type='radio' name='MODE' value='reboot' $checked{'MODE'}{'reboot'} /></td>
141 <td width='70%' class='base'>$Lang::tr{'reboot'}</td></tr>
142<tr>
143 <td>
144 <input type='checkbox' name='TUESDAY' $checked{'TUESDAY'}></td>
145 <td width='15%' class='base'>
146 $Lang::tr{'tuesday'}</td>
147 <td>
148 <input type='radio' name='MODE' value='halt' $checked{'MODE'}{'halt'} /></td>
149 <td class='base'>$Lang::tr{'shutdown'}</td></tr>
150<tr>
151 <td>&nbsp;</td>
152 <td>
153 <input type='checkbox' name='WEDNESDAY' $checked{'WEDNESDAY'}></td>
154 <td width='15%' class='base'>
155 $Lang::tr{'wednesday'}</td></tr>
156<tr>
157 <td>&nbsp;</td>
158 <td>
159 <input type='checkbox' name='THURSDAY' $checked{'THURSDAY'}></td>
160 <td width='15%' class='base'>
161 $Lang::tr{'thursday'}</td></tr>
162<tr>
163 <td>&nbsp;</td>
164 <td>
165 <input type='checkbox' name='FRIDAY' $checked{'FRIDAY'}></td>
166 <td width='15%' class='base'>
167 $Lang::tr{'friday'}</td></tr>
168<tr>
169 <td>&nbsp;</td>
170 <td>
171 <input type='checkbox' name='SATURDAY' $checked{'SATURDAY'}></td>
172 <td width='15%' class='base'>
173 $Lang::tr{'saturday'}</td></tr>
174<tr>
175 <td>&nbsp;</td>
176 <td>
177 <input type='checkbox' name='SUNDAY' $checked{'SUNDAY'}></td>
178 <td width='15%' class='base'>
179 $Lang::tr{'sunday'}</td></tr>
180</table>
181
182<table width='100%'>
183<hr />
184<tr>
185 <td width='60%'>&nbsp;</td>
186 <td width='30%' align='center'>
187 <input type='submit' name='ACTION' value='$Lang::tr{'save'}' />
188 </td>
189 <td width='10%' align='right'>
190 <a href='${General::adminmanualurl}/system.html#shutdown' target='_blank'>
191 <img src='/images/web-support.png' title='$Lang::tr{'online help en'}' /></a></td>
192</tr>
193</table>
194
195END
196;
197 &Header::closebox();
198 print "</form>\n";
199} else {
200 my $message='';
201 my $title='';
202 my $refresh = "<meta http-equiv='refresh' content='5; URL=/cgi-bin/index.cgi' />";
203 if ($death) {
204 $title = $Lang::tr{'shutting down'};
78331e30 205 $message = $Lang::tr{'ipfire has now shutdown'};
ac1cfefa
MT
206 } else {
207 $title = $Lang::tr{'rebooting'};
78331e30 208 $message = $Lang::tr{'ipfire has now rebooted'};
ac1cfefa
MT
209 }
210 &Header::openpage($title, 0, $refresh);
211
212 &Header::openbigbox('100%', 'center');
213 print <<END
214<div align='center'>
215<table width='100%' bgcolor='#ffffff'>
216<tr><td align='center'>
b34fd95d 217<br /><br /><img src='/ipfire_big.gif' /><br /><br /><br />
ac1cfefa
MT
218</td></tr>
219</table>
220<br />
221<font size='6'>$message</font>
222</div>
223END
224 ;
225}
226
227&Header::closebigbox();
228&Header::closepage();
229
230
231
232# Create a named select box containing valid times from quarter to quarter.
233sub select_hour_var {
234 # Create a variable containing the SELECT with selected value variable name and current value selected
235 my $select_hour_var = shift;
236 my $selected_hour = shift;
237
238 my $select_hour = "<select name='$select_hour_var'>";
239 my $hh = 0;
240 my $mm = 15;
241 my $str = '00:00';
242 for (my $x=0; $x<(24*4); $x++) {
243 my $check = $selected_hour eq $str ? "selected='selected'" : '';
244 $select_hour .= "<Option $check value='$str'>$str";
245 $str = sprintf("%.02d", $hh) . ":" . sprintf("%.02d", $mm);
246 $mm += 15;
247 if ($mm==60) {$mm=0; $hh++; }
248 }
249 $select_hour .= "</select>\n";
250 return ($select_hour);
251}