]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/time.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 IPFire Team #
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. #
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. #
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/>. #
20 ###############################################################################
23 use POSIX
qw(strftime);
25 # enable only the following on debugging purpose
27 #use CGI::Carp 'fatalsToBrowser';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
34 my $errormessage = '';
36 &Header
::showhttpheaders
();
38 $timesettings{'ACTION'} = '';
39 $timesettings{'VALID'} = '';
41 $timesettings{'ENABLENTP'} = 'off';
42 $timesettings{'NTP_ADDR_1'} = '';
43 $timesettings{'NTP_ADDR_2'} = '';
44 $timesettings{'UPDATE_METHOD'} = 'manually';
45 $timesettings{'UPDATE_VALUE'} = '0';
46 $timesettings{'UPDATE_PERIOD'} = '';
47 $timesettings{'ENABLECLNTP'} = 'off';
48 $timesettings{'ENABLESETONBOOT'} = 'off';
50 &Header
::getcgihash
(\
%timesettings);
52 if ($timesettings{'ACTION'} eq $Lang::tr
{'save'})
54 if ($timesettings{'ENABLENTP'} eq 'on')
56 if ( ! ( &General
::validfqdn
($timesettings{'NTP_ADDR_1'}) ||
57 &General
::validip
($timesettings{'NTP_ADDR_1'})))
59 $errormessage = $Lang::tr
{'invalid primary ntp'};
63 if ($timesettings{'NTP_ADDR_2'})
65 if ( ! ( &General
::validfqdn
($timesettings{'NTP_ADDR_2'}) ||
66 &General
::validip
($timesettings{'NTP_ADDR_2'})))
68 $errormessage = $Lang::tr
{'invalid secondary ntp'};
72 if (!($timesettings{'NTP_ADDR_1'}) && $timesettings{'NTP_ADDR_2'})
74 $errormessage = $Lang::tr
{'cannot specify secondary ntp without specifying primary'};
78 if (!($timesettings{'UPDATE_VALUE'} =~ /^\d+$/) || $timesettings{'UPDATE_VALUE'} <= 0)
80 $errormessage = $Lang::tr
{'invalid time period'};
84 if ($timesettings{'ENABLENTP'} ne "on" && $timesettings{'ENABLECLNTP'} eq "on")
86 $errormessage = $Lang::tr
{'ntp must be enabled to have clients'};
89 if ($timesettings{'ENABLENTP'} eq "on" && !($timesettings{'NTP_ADDR_1'}) && !($timesettings{'NTP_ADDR_2'}))
91 $errormessage = $Lang::tr
{'cannot enable ntp without specifying primary'};
96 $timesettings{'VALID'} = 'no'; }
98 $timesettings{'VALID'} = 'yes'; }
100 &General
::writehash
("${General::swroot}/time/settings", \
%timesettings);
101 open(FILE
, ">/${General::swroot}/time/settime.conf") or die "Unable to write settime.conf file";
103 print FILE
"$timesettings{'NTP_ADDR_1'} $timesettings{'NTP_ADDR_2'}\n";
108 if ($timesettings{'UPDATE_PERIOD'} eq 'daily') {
109 $updateperiod = $timesettings{'UPDATE_VALUE'} * 1440; }
110 elsif ($timesettings{'UPDATE_PERIOD'} eq 'weekly') {
111 $updateperiod = $timesettings{'UPDATE_VALUE'} * 10080; }
112 elsif ($timesettings{'UPDATE_PERIOD'} eq 'monthly') {
113 $updateperiod = $timesettings{'UPDATE_VALUE'} * 40320; }
115 $updateperiod = $timesettings{'UPDATE_VALUE'} * 60; }
117 $updateperiod = $updateperiod - 5;
119 if ($updateperiod <= 5) {
122 open(FILE
, ">/${General::swroot}/time/counter.conf") or die "Unable to write counter.conf file";
124 print FILE
"$updateperiod\n";
127 if ($timesettings{'ENABLENTP'} eq 'on' && $timesettings{'VALID'} eq 'yes')
129 &General
::system('/usr/bin/touch', "${General::swroot}/time/enable");
130 &General
::system('/usr/local/bin/timectrl', 'enable');
131 &General
::log($Lang::tr
{'ntp syncro enabled'});
132 unlink "/var/lock/time/counter";
133 if ($timesettings{'UPDATE_METHOD'} eq 'periodically')
135 open(FILE
, ">/var/lock/time/counter") or die "Unable to write counter file";
137 print FILE
"$updateperiod\n";
140 if ($timesettings{'ENABLECLNTP'} eq 'on') # DPC added to 1.3.1
142 &General
::system('/usr/bin/touch', "${General::swroot}/time/allowclients"); # DPC added to 1.3.1
143 &General
::log($Lang::tr
{'ntpd restarted'}); # DPC added to 1.3.1
145 unlink "${General::swroot}/time/allowclients";
151 unlink "${General::swroot}/time/enable";
152 unlink "/var/lock/time/settimenow";
153 unlink "${General::swroot}/time/allowclients"; # DPC added to 1.3.1
154 &General
::system('/usr/local/bin/timectrl', 'disable');
155 &General
::log($Lang::tr
{'ntp syncro disabled'})
157 if (! $errormessage) {
158 &General
::system('/usr/local/bin/timectrl', 'restart'); # DPC added to 1.3.1
162 # To enter an ' into a pushbutton solution is to use ' in it's definition
163 # but returned value when pressed is ' not the code. Cleanhtml recode the ' to enable comparison.
164 $timesettings{'ACTION'} = &Header
::cleanhtml
($timesettings{'ACTION'});
165 if ($timesettings{'ACTION'} eq $Lang::tr
{'set time now'} && $timesettings{'ENABLENTP'} eq 'on')
167 &General
::system('/usr/bin/touch', "/var/lock/time/settimenow");
170 &General
::readhash
("${General::swroot}/time/settings", \
%timesettings);
172 if ($timesettings{'VALID'} eq '')
174 $timesettings{'ENABLENTP'} = 'off';
175 $timesettings{'UPDATE_METHOD'} = 'manually';
176 $timesettings{'UPDATE_VALUE'} = '1';
177 $timesettings{'UPDATE_PERIOD'} = 'daily';
178 $timesettings{'NTP_ADDR_1'} = '0.ipfire.pool.ntp.org';
179 $timesettings{'NTP_ADDR_2'} = '1.ipfire.pool.ntp.org';
180 $timesettings{'ENABLESETONBOOT'} = 'off';
183 unless ($errormessage) {
185 my $date = strftime
("%m %e %Y %H %M", localtime);
187 # Split date string into single values.
188 my ($month, $day, $year, $hour, $minute) = split(/ /, $date);
190 # Assign values to the hash.
191 $timesettings{'SETMONTH'} = $month;
192 $timesettings{'SETDAY'} = $day;
193 $timesettings{'SETYEAR'} = $year;
194 $timesettings{'SETHOUR'} = $hour;
195 $timesettings{'SETMINUTES'} = $minute;
196 $_=$timesettings{'SETDAY'};
197 $timesettings{'SETDAY'}=~ tr/ /0/;
203 $checked{'ENABLENTP'}{'off'} = '';
204 $checked{'ENABLENTP'}{'on'} = '';
205 $checked{'ENABLENTP'}{$timesettings{'ENABLENTP'}} = "checked='checked'";
207 $checked{'ENABLECLNTP'}{'off'} = '';
208 $checked{'ENABLECLNTP'}{'on'} = '';
209 $checked{'ENABLECLNTP'}{$timesettings{'ENABLECLNTP'}} = "checked='checked'";
211 $checked{'ENABLESETONBOOT'}{'off'} = '';
212 $checked{'ENABLESETONBOOT'}{'on'} = '';
213 $checked{'ENABLESETONBOOT'}{$timesettings{'ENABLESETONBOOT'}} = "checked='checked'";
215 $checked{'UPDATE_METHOD'}{'manually'} = '';
216 $checked{'UPDATE_METHOD'}{'periodically'} = '';
217 $checked{'UPDATE_METHOD'}{$timesettings{'UPDATE_METHOD'}} = "checked='checked'";
219 $selected{'UPDATE_PERIOD'}{'hourly'} = '';
220 $selected{'UPDATE_PERIOD'}{'daily'} = '';
221 $selected{'UPDATE_PERIOD'}{'weekly'} = '';
222 $selected{'UPDATE_PERIOD'}{'monthly'} = '';
223 $selected{'UPDATE_PERIOD'}{$timesettings{'UPDATE_PERIOD'}} = "selected='selected'";
225 # added to v0.0.4 to refresh screen if syncro event queued
227 if ( -e
"/var/lock/time/settimenow") {
228 $refresh = "<meta http-equiv='refresh' content='60;' />";
231 &Header
::openpage
($Lang::tr
{'ntp configuration'}, 1, $refresh);
233 &Header
::openbigbox
('100%', 'left', '', $errormessage);
235 # DPC move error message to top so it is seen!
237 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
238 print "<font class='base'>$errormessage </font>\n";
242 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
244 &Header
::openbox
('100%', 'left', $Lang::tr
{'ntp common settings'});
248 <td><input type='checkbox' name='ENABLENTP' $checked{'ENABLENTP'}{'on'} /></td>
249 <td width='100%' colspan='4' class='base'>$Lang::tr{'network time from'}</td>
253 <td width='100%' class='base' colspan='4'>
257 if ( -e
"/var/lock/time/lastset")
259 print "$Lang::tr{'clock last synchronized at'}\n";
260 open(FILE
, "</var/lock/time/lastset") or die "Unable to read lastset";
267 print "$Lang::tr{'clock has not been synchronized'}\n";
274 <td width='25%' class='base'>$Lang::tr{'primary ntp server'}: <img src='/blob.gif' alt='*' /></td>
275 <td width='25%'><input type='text' name='NTP_ADDR_1' value='$timesettings{'NTP_ADDR_1'}' /></td>
276 <td width='25%' class='base'>$Lang::tr{'secondary ntp server'}:</td>
277 <td width='25%'><input type='text' name='NTP_ADDR_2' value='$timesettings{'NTP_ADDR_2'}' /></td>
281 <td class='base' colspan='4'><input type='checkbox' name='ENABLECLNTP' $checked{'ENABLECLNTP'}{'on'} /> $Lang::tr{'clenabled'}</td>
285 <td class='base' colspan='4'><input type='checkbox' name='ENABLESETONBOOT' $checked{'ENABLESETONBOOT'}{'on'} /> $Lang::tr{'Set time on boot'}</td>
291 &Header
::openbox
('100%',1,$Lang::tr
{'ntp sync'});
295 <td class='base'><input type='radio' name='UPDATE_METHOD' value='periodically' $checked{'UPDATE_METHOD'}{'periodically'} /></td>
296 <td width='10%'>$Lang::tr{'every'}</td>
297 <td width='45%'><input type='text' name='UPDATE_VALUE' size='3' maxlength='3' value='$timesettings{'UPDATE_VALUE'}' />
298 <select name='UPDATE_PERIOD'>
299 <option value='hourly' $selected{'UPDATE_PERIOD'}{'hourly'}>$Lang::tr{'hours'}</option>
300 <option value='daily' $selected{'UPDATE_PERIOD'}{'daily'}>$Lang::tr{'days'}</option>
301 <option value='weekly' $selected{'UPDATE_PERIOD'}{'weekly'}>$Lang::tr{'weeks'}</option>
302 <option value='monthly' $selected{'UPDATE_PERIOD'}{'monthly'}>$Lang::tr{'months'}</option>
304 <td width='50%'> </td>
307 <td class='base'><input type='radio' name='UPDATE_METHOD' value='manually' $checked{'UPDATE_METHOD'}{'manually'} /></td>
308 <td colspan='2'>$Lang::tr{'manually'}</td>
311 <td colspan='4'><br><br><strong>$Lang::tr{'update time'}</strong></td>
315 <td class='base' colspan='3'>$Lang::tr{'set time now help'}</td>
320 if ( -e
"/var/lock/time/settimenow") {
321 print "<tr>\n<td align='center'><img src='/images/clock.gif' alt='' /></td>\n";
322 print "<td colspan='2'><font color='red'>$Lang::tr{'waiting to synchronize clock'}...</font></td></tr>\n";
330 <td width='30%'><img src='/blob.gif' alt='*' /> $Lang::tr{'required field'}</td>
331 <td width='65%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'set time now'}' /></td>
332 <td width='5%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
342 &Header
::closebigbox
();
344 &Header
::closepage
();