immernoch das alte Problem...
[ipfire-2.x.git] / html / cgi-bin / time.cgi
CommitLineData
cd1a2927
MT
1#!/usr/bin/perl\r
2#\r
3# IPCop CGIs\r
4#\r
5# This file is part of the IPCop Project\r
6# \r
7# This code is distributed under the terms of the GPL\r
8#\r
9# (c) Eric Oberlander June 2002\r
10#\r
11# (c) Darren Critchley June 2003 - added real time clock setting, etc\r
12#\r
13# $Id: time.cgi,v 1.4.2.11 2005/05/28 12:16:18 eoberlander Exp $\r
14#\r
15\r
16use strict;\r
17\r
18# enable only the following on debugging purpose\r
19#use warnings;\r
20#use CGI::Carp 'fatalsToBrowser';\r
21\r
22require 'CONFIG_ROOT/general-functions.pl';\r
23require "${General::swroot}/lang.pl";\r
24require "${General::swroot}/header.pl";\r
25\r
26my %timesettings=();\r
27my $errormessage = '';\r
28\r
29&Header::showhttpheaders();\r
30\r
31$timesettings{'ACTION'} = '';\r
32$timesettings{'VALID'} = '';\r
33\r
34$timesettings{'ENABLENTP'} = 'off';\r
35$timesettings{'NTP_ADDR_1'} = '';\r
36$timesettings{'NTP_ADDR_2'} = '';\r
37$timesettings{'UPDATE_METHOD'} = 'manually';\r
38$timesettings{'UPDATE_VALUE'} = '0';\r
39$timesettings{'UPDATE_PERIOD'} = '';\r
40$timesettings{'ENABLECLNTP'} = 'off';\r
41$timesettings{'SETHOUR'} = '';\r
42$timesettings{'SETMINUTES'} = '';\r
43$timesettings{'SETDAY'} = '';\r
44$timesettings{'SETMONTH'} = '';\r
45$timesettings{'SETYEAR'} = '';\r
46\r
47&Header::getcgihash(\%timesettings);\r
48\r
49if ($timesettings{'ACTION'} eq $Lang::tr{'instant update'})\r
50{\r
51 if ($timesettings{'SETHOUR'} eq '' || $timesettings{'SETHOUR'} < 0 || $timesettings{'SETHOUR'} > 23) {\r
52 $errormessage = $Lang::tr{'invalid time entered'};\r
53 goto UPDTERROR;\r
54 }\r
55 if ($timesettings{'SETMINUTES'} eq '' || $timesettings{'SETMINUTES'} < 0 || $timesettings{'SETMINUTES'} > 59) {\r
56 $errormessage = $Lang::tr{'invalid time entered'};\r
57 goto UPDTERROR;\r
58 }\r
59 if ($timesettings{'SETDAY'} eq '' || $timesettings{'SETDAY'} < 1 || $timesettings{'SETDAY'} > 31) {\r
60 $errormessage = $Lang::tr{'invalid date entered'};\r
61 goto UPDTERROR;\r
62 }\r
63 if ($timesettings{'SETMONTH'} eq '' || $timesettings{'SETMONTH'} < 1 || $timesettings{'SETMONTH'} > 12) {\r
64 $errormessage = $Lang::tr{'invalid date entered'};\r
65 goto UPDTERROR;\r
66 }\r
67 if ($timesettings{'SETYEAR'} eq '' || $timesettings{'SETYEAR'} < 2003 || $timesettings{'SETYEAR'} > 2030) {\r
68 $errormessage = $Lang::tr{'invalid date entered'};\r
69 goto UPDTERROR;\r
70 }\r
71\r
72UPDTERROR:\r
73 if ($errormessage) {\r
74 $timesettings{'VALID'} = 'no'; }\r
75 else {\r
76 $timesettings{'VALID'} = 'yes'; }\r
77\r
78 if ($timesettings{'VALID'} eq 'yes') {\r
79 # we want date in YYYY-MM-DD HH:MM format for date command\r
80 # EAO changed datestring to ISO 6801 format 2003-08-11\r
81 my $datestring = "$timesettings{'SETYEAR'}-$timesettings{'SETMONTH'}-$timesettings{'SETDAY'}";\r
82 my $timestring = "$timesettings{'SETHOUR'}:$timesettings{'SETMINUTES'}";\r
83 # EAO setdate.c also revised for ISO 6801 date format 2003-08-11\r
84 system ('/usr/local/bin/setdate', $datestring, $timestring);\r
85 &General::log("$Lang::tr{'time date manually reset'} $datestring $timestring");\r
86 }\r
87 unless ($errormessage) {\r
88 undef %timesettings;\r
89 }\r
90}\r
91\r
92if ($timesettings{'ACTION'} eq $Lang::tr{'save'})\r
93{ \r
94 if ($timesettings{'ENABLENTP'} eq 'on')\r
95 {\r
96 if ( ! ( &General::validfqdn($timesettings{'NTP_ADDR_1'}) ||\r
97 &General::validip ($timesettings{'NTP_ADDR_1'})))\r
98 {\r
99 $errormessage = $Lang::tr{'invalid primary ntp'};\r
100 goto ERROR;\r
101 }\r
102 }\r
103 if ($timesettings{'NTP_ADDR_2'})\r
104 {\r
105 if ( ! ( &General::validfqdn($timesettings{'NTP_ADDR_2'}) ||\r
106 &General::validip ($timesettings{'NTP_ADDR_2'})))\r
107 {\r
108 $errormessage = $Lang::tr{'invalid secondary ntp'};\r
109 goto ERROR;\r
110 }\r
111 }\r
112 if (!($timesettings{'NTP_ADDR_1'}) && $timesettings{'NTP_ADDR_2'})\r
113 {\r
114 $errormessage = $Lang::tr{'cannot specify secondary ntp without specifying primary'};\r
115 goto ERROR;\r
116 }\r
117\r
118 if (!($timesettings{'UPDATE_VALUE'} =~ /^\d+$/) || $timesettings{'UPDATE_VALUE'} <= 0)\r
119 {\r
120 $errormessage = $Lang::tr{'invalid time period'};\r
121 goto ERROR;\r
122 }\r
123\r
124 if ($timesettings{'ENABLENTP'} ne "on" && $timesettings{'ENABLECLNTP'} eq "on")\r
125 {\r
126 $errormessage = $Lang::tr{'ntp must be enabled to have clients'};\r
127 goto ERROR;\r
128 }\r
129 if ($timesettings{'ENABLENTP'} eq "on" && !($timesettings{'NTP_ADDR_1'}) && !($timesettings{'NTP_ADDR_2'}))\r
130 {\r
131 $errormessage = $Lang::tr{'cannot enable ntp without specifying primary'};\r
132 goto ERROR;\r
133 }\r
134ERROR:\r
135 if ($errormessage) {\r
136 $timesettings{'VALID'} = 'no'; }\r
137 else {\r
138 $timesettings{'VALID'} = 'yes'; }\r
139\r
140 &General::writehash("${General::swroot}/time/settings", \%timesettings);\r
141 open(FILE, ">/${General::swroot}/time/settime.conf") or die "Unable to write settime.conf file";\r
142 flock(FILE, 2);\r
143 print FILE "$timesettings{'NTP_ADDR_1'} $timesettings{'NTP_ADDR_2'}\n";\r
144 close FILE;\r
145\r
146 my $updateperiod=0;\r
147\r
148 if ($timesettings{'UPDATE_PERIOD'} eq 'daily') {\r
149 $updateperiod = $timesettings{'UPDATE_VALUE'} * 1440; }\r
150 elsif ($timesettings{'UPDATE_PERIOD'} eq 'weekly') {\r
151 $updateperiod = $timesettings{'UPDATE_VALUE'} * 10080; }\r
152 elsif ($timesettings{'UPDATE_PERIOD'} eq 'monthly') {\r
153 $updateperiod = $timesettings{'UPDATE_VALUE'} * 40320; }\r
154 else {\r
155 $updateperiod = $timesettings{'UPDATE_VALUE'} * 60; }\r
156\r
157 $updateperiod = $updateperiod - 5;\r
158\r
159 if ($updateperiod <= 5) {\r
160 $updateperiod = 5; }\r
161\r
162 open(FILE, ">/${General::swroot}/time/counter.conf") or die "Unable to write counter.conf file";\r
163 flock(FILE, 2);\r
164 print FILE "$updateperiod\n";\r
165 close FILE;\r
166\r
167 if ($timesettings{'ENABLENTP'} eq 'on' && $timesettings{'VALID'} eq 'yes')\r
168 {\r
169 system ('/bin/touch', "${General::swroot}/time/enable");\r
170 &General::log($Lang::tr{'ntp syncro enabled'});\r
171 unlink "${General::swroot}/time/counter";\r
172 if ($timesettings{'UPDATE_METHOD'} eq 'periodically')\r
173 {\r
174 open(FILE, ">/${General::swroot}/time/counter") or die "Unable to write counter file";\r
175 flock(FILE, 2);\r
176 print FILE "$updateperiod\n";\r
177 close FILE;\r
178 }\r
179 if ($timesettings{'ENABLECLNTP'} eq 'on') # DPC added to 1.3.1\r
180 {\r
181 system ('/bin/touch', "${General::swroot}/time/allowclients"); # DPC added to 1.3.1\r
182 &General::log($Lang::tr{'ntpd restarted'}); # DPC added to 1.3.1\r
183 } else {\r
184 unlink "${General::swroot}/time/allowclients";\r
185 }\r
186 \r
187 }\r
188 else\r
189 {\r
190 unlink "${General::swroot}/time/enable";\r
191 unlink "${General::swroot}/time/settimenow";\r
192 unlink "${General::swroot}/time/allowclients"; # DPC added to 1.3.1\r
193 &General::log($Lang::tr{'ntp syncro disabled'})\r
194 }\r
195 if (! $errormessage) {\r
196 system ('/usr/local/bin/restartntpd'); # DPC added to 1.3.1\r
197 }\r
198}\r
199\r
200# To enter an ' into a pushbutton solution is to use &#039; in it's definition\r
201# but returned value when pressed is ' not the code. Cleanhtml recode the ' to enable comparison.\r
202$timesettings{'ACTION'} = &Header::cleanhtml ($timesettings{'ACTION'});\r
203if ($timesettings{'ACTION'} eq $Lang::tr{'set time now'} && $timesettings{'ENABLENTP'} eq 'on')\r
204{\r
205 system ('/bin/touch', "${General::swroot}/time/settimenow");\r
206}\r
207\r
208&General::readhash("${General::swroot}/time/settings", \%timesettings);\r
209\r
210if ($timesettings{'VALID'} eq '')\r
211{\r
212 $timesettings{'ENABLENTP'} = 'off';\r
213 $timesettings{'UPDATE_METHOD'} = 'manually';\r
214 $timesettings{'UPDATE_VALUE'} = '1';\r
215 $timesettings{'UPDATE_PERIOD'} = 'daily';\r
216 $timesettings{'NTP_ADDR_1'} = 'pool.ntp.org';\r
217 $timesettings{'NTP_ADDR_2'} = 'pool.ntp.org';\r
218}\r
219\r
220unless ($errormessage) {\r
221 $timesettings{'SETMONTH'} = `date +'%m %e %Y %H %M'|cut -c 1-2`;\r
222 $timesettings{'SETDAY'} = `date +'%m %e %Y %H %M'|cut -c 4-5`;\r
223 $timesettings{'SETYEAR'} = `date +'%m %e %Y %H %M'|cut -c 7-10`;\r
224 $timesettings{'SETHOUR'} = `date +'%m %e %Y %H %M'|cut -c 12-13`;\r
225 $timesettings{'SETMINUTES'} = `date +'%m %e %Y %H %M'|cut -c 15-16`;\r
226 $_=$timesettings{'SETDAY'};\r
227 $timesettings{'SETDAY'}=~ tr/ /0/;\r
228}\r
229\r
230my %selected=();\r
231my %checked=();\r
232\r
233$checked{'ENABLENTP'}{'off'} = '';\r
234$checked{'ENABLENTP'}{'on'} = '';\r
235$checked{'ENABLENTP'}{$timesettings{'ENABLENTP'}} = "checked='checked'";\r
236\r
237$checked{'ENABLECLNTP'}{'off'} = '';\r
238$checked{'ENABLECLNTP'}{'on'} = '';\r
239$checked{'ENABLECLNTP'}{$timesettings{'ENABLECLNTP'}} = "checked='checked'";\r
240\r
241$checked{'UPDATE_METHOD'}{'manually'} = '';\r
242$checked{'UPDATE_METHOD'}{'periodically'} = '';\r
243$checked{'UPDATE_METHOD'}{$timesettings{'UPDATE_METHOD'}} = "checked='checked'";\r
244\r
245$selected{'UPDATE_PERIOD'}{'hourly'} = '';\r
246$selected{'UPDATE_PERIOD'}{'daily'} = '';\r
247$selected{'UPDATE_PERIOD'}{'weekly'} = '';\r
248$selected{'UPDATE_PERIOD'}{'monthly'} = '';\r
249$selected{'UPDATE_PERIOD'}{$timesettings{'UPDATE_PERIOD'}} = "selected='selected'";\r
250\r
251# added to v0.0.4 to refresh screen if syncro event queued \r
252my $refresh = '';\r
253if ( -e "${General::swroot}/time/settimenow") {\r
254 $refresh = "<meta http-equiv='refresh' content='60;' />";\r
255}\r
256\r
257&Header::openpage($Lang::tr{'ntp configuration'}, 1, $refresh);\r
258\r
259&Header::openbigbox('100%', 'left', '', $errormessage);\r
260\r
261# DPC move error message to top so it is seen!\r
262if ($errormessage) {\r
263 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});\r
264 print "<font class='base'>$errormessage&nbsp;</font>\n";\r
265 &Header::closebox();\r
266 }\r
267\r
268print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";\r
269\r
270&Header::openbox('100%', 'left', $Lang::tr{'network time'});\r
271print <<END\r
272<table width='100%'>\r
273<tr>\r
274 <td><input type='checkbox' name='ENABLENTP' $checked{'ENABLENTP'}{'on'} /></td>\r
275 <td width='100%' colspan='4' class='base'>$Lang::tr{'network time from'}</td>\r
276</tr>\r
277<tr>\r
278 <td>&nbsp;</td>\r
279 <td width='100%' class='base' colspan='4'>\r
280END\r
281;\r
282\r
283if ( -e "${General::swroot}/time/lastset")\r
284{\r
285 print "$Lang::tr{'clock last synchronized at'}\n";\r
286 my $output = `cat ${General::swroot}/time/lastset`;\r
287 print $output;\r
288}\r
289else\r
290{\r
291 print "$Lang::tr{'clock has not been synchronized'}\n";\r
292}\r
293\r
294print <<END\r
295</td></tr>\r
296<tr>\r
297 <td>&nbsp;</td>\r
298 <td width='25%' class='base'>$Lang::tr{'primary ntp server'}:</td>\r
299 <td width='25%'><input type='text' name='NTP_ADDR_1' value='$timesettings{'NTP_ADDR_1'}' /></td>\r
300 <td width='25%' class='base'>$Lang::tr{'secondary ntp server'}: &nbsp;<img src='/blob.gif' align='top' alt='*' /></td>\r
301 <td width='25%'><input type='text' name='NTP_ADDR_2' value='$timesettings{'NTP_ADDR_2'}' /></td>\r
302</tr>\r
303<tr>\r
304 <td>&nbsp;</td>\r
305 <td class='base' colspan='4'><input type='checkbox' name='ENABLECLNTP' $checked{'ENABLECLNTP'}{'on'} /> $Lang::tr{'clenabled'}</td>\r
306</tr>\r
307</table>\r
308<table width='100%'>\r
309<tr>\r
310 <td colspan='4'><hr /><b>$Lang::tr{'update time'}</b></td>\r
311</tr>\r
312<tr>\r
313 <td>&nbsp;</td>\r
314 <td class='base' colspan='2'>$Lang::tr{'set time now help'}</td>\r
315</tr>\r
316<tr>\r
317 <td class='base'><input type='radio' name='UPDATE_METHOD' value='periodically' $checked{'UPDATE_METHOD'}{'periodically'} /></td>\r
318 <td width='15%'>$Lang::tr{'every'}: </td>\r
319 <td width='35%'><input type='text' name='UPDATE_VALUE' size='3' maxlength='3' value='$timesettings{'UPDATE_VALUE'}' />\r
320 <select name='UPDATE_PERIOD'>\r
321 <option value='hourly' $selected{'UPDATE_PERIOD'}{'hourly'}>$Lang::tr{'hours'}</option>\r
322 <option value='daily' $selected{'UPDATE_PERIOD'}{'daily'}>$Lang::tr{'days'}</option>\r
323 <option value='weekly' $selected{'UPDATE_PERIOD'}{'weekly'}>$Lang::tr{'weeks'}</option>\r
324 <option value='monthly' $selected{'UPDATE_PERIOD'}{'monthly'}>$Lang::tr{'months'}</option>\r
325 </select></td>\r
326 <td width='50%'>&nbsp;</td>\r
327</tr>\r
328<tr>\r
329 <td class='base'><input type='radio' name='UPDATE_METHOD' value='manually' $checked{'UPDATE_METHOD'}{'manually'} /></td>\r
330 <td colspan='2'>$Lang::tr{'manually'}</td>\r
331</tr>\r
332END\r
333;\r
334\r
335if ( -e "${General::swroot}/time/settimenow") {\r
336 print "<tr>\n<td align='center'><img src='/images/clock.gif' alt='' /></td>\n";\r
337 print "<td colspan='2'><font color='red'>$Lang::tr{'waiting to synchronize clock'}...</font></td></tr>\n";\r
338}\r
339print <<END\r
340</table>\r
341<br />\r
342<hr />\r
343<table width='100%'>\r
344<tr>\r
345 <td width='30%'><img src='/blob.gif' alt='*' /> $Lang::tr{'this field may be blank'}</td>\r
346 <td width='40%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'set time now'}' /></td>\r
347 <td width='25%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>\r
348 <td width='5%' align='right'>\r
349 <a href='${General::adminmanualurl}/services.html#services_time' target='_blank'><img src='/images/web-support.png' title='$Lang::tr{'online help en'}' /></a>\r
350 </td>\r
351</tr>\r
352</table>\r
353END\r
354;\r
355\r
356&Header::closebox();\r
357\r
358&Header::openbox('100%', 'left', $Lang::tr{'update time'});\r
359\r
360print <<END\r
361<table width='100%'>\r
362<tr>\r
363 <td width='65%' class='base'>\r
364 <table> \r
365 <tr>\r
366 <td>$Lang::tr{'year'}:&nbsp;</td>\r
367 <td><input type='text' name='SETYEAR' size='4' maxlength='4' value='$timesettings{'SETYEAR'}' /></td>\r
368 <td>&nbsp;$Lang::tr{'month'}:&nbsp;</td>\r
369 <td><input type='text' name='SETMONTH' size='2' maxlength='2' value='$timesettings{'SETMONTH'}' /></td>\r
370 <td>&nbsp;$Lang::tr{'day'}:&nbsp;</td>\r
371 <td><input type='text' name='SETDAY' size='2' maxlength='2' value='$timesettings{'SETDAY'}' /></td>\r
372 <td>&nbsp;&nbsp;&nbsp;&nbsp;$Lang::tr{'hours2'}:&nbsp;</td>\r
373 <td><input type='text' name='SETHOUR' size='2' maxlength='2' value='$timesettings{'SETHOUR'}' /></td>\r
374 <td>&nbsp;$Lang::tr{'minutes'}:&nbsp;</td>\r
375 <td><input type='text' name='SETMINUTES' size='2' maxlength='2' value='$timesettings{'SETMINUTES'}' /></td>\r
376 </tr> \r
377 </table>\r
378 </td>\r
379 <td width='35%' align='center' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'instant update'}' /></td>\r
380</tr>\r
381</table>\r
382END\r
383;\r
384&Header::closebox();\r
385\r
386print "</form>\n";\r
387\r
388&Header::closebigbox();\r
389\r
390&Header::closepage();\r
391\r