1#!/usr/local/bin/perl
2
3use strict;
4use warnings;
5require './time-lib.pl';
6our (%in, %text, %config, %access, $base_remote_user, $get_hardware_time_error);
7
8my ($rawdate, $rawhwdate, %system_date, $rawtime, %hw_date, $txt);
9$txt = "";
10&ReadParse();
11
12&error($text{'acl_error'}) if ($access{'sysdate'} && $access{'hwdate'});
13
14my $arr;
15if (!$access{'sysdate'} && !$access{'hwdate'} && &support_hwtime()) {
16	$arr = "0,1";
17	}
18else {
19	$arr = "0";
20	}
21&ui_print_header(undef,  $text{ 'index_title' }, "", "index", 1, 1, undef,
22	&help_search_link("date hwclock ntpdate", "man"),
23	qq(<script src="time.js"></script>\n),
24	qq(onLoad="F=[$arr];timeInit(F); setTimeout('timeUpdate(F)', 5000);"));
25
26if (!$access{'sysdate'} && !&has_command("date")) {
27	print &text( 'error_cnf', "<tt>date</tt>"),"<p>\n";
28	&ui_print_footer("/", $text{'index'});
29	exit;
30	}
31
32# Show tabs for times, timezones and syncing
33my @tabs = ( );
34push(@tabs, [ "time", $text{'index_tabtime'}, "index.cgi?mode=time" ]);
35if ($access{'timezone'} && &has_timezone()) {
36	push(@tabs, [ "zone", $text{'index_tabzone'}, "index.cgi?mode=zone" ]);
37	}
38if ($access{'ntp'}) {
39	push(@tabs, [ "sync", $text{'index_tabsync'}, "index.cgi?mode=sync" ]);
40	}
41print &ui_tabs_start(\@tabs, "mode", $in{'mode'} || $tabs[0]->[0], 1);
42
43# Get the system time
44my @tm = &get_system_time();
45$system_date{ 'second' } = $tm[0];
46$system_date{ 'minute' } = $tm[1];
47$system_date{ 'hour' } = $tm[2];
48$system_date{ 'date' } = $tm[3];
49$system_date{ 'month' } = &number_to_month($tm[4]);
50$system_date{ 'year' } = $tm[5]+1900;
51$system_date{ 'day' } = &number_to_weekday($tm[6]);
52
53print &ui_tabs_start_tab("mode", "time");
54print $text{'index_desctime'},"<p>\n";
55
56if( !$access{'sysdate'} )
57{
58  # Show system time for editing
59  print &ui_form_start("apply.cgi");
60  print &ui_hidden("mode", "sysdate");
61  print &tabletime(&hlink($text{'sys_title'}, "system_time"), 0, %system_date);
62  print &ui_submit($text{'action_apply'}, "action");
63  if (&support_hwtime()) {
64	print &ui_submit($text{'action_sync'}, "action");
65  }
66  print &ui_form_end();
67}
68else
69{
70   # Just show current time
71   print &tabletime( &hlink( $text{ 'sys_title' }, "system_time" ), 1, %system_date ),"<p>\n";
72}
73
74# Get the hardware time
75if (&support_hwtime()) {
76	my @tm = &get_hardware_time();
77	if (@tm) {
78		$hw_date{'second'} = $tm[0];
79		$hw_date{'minute'} = $tm[1];
80		$hw_date{'hour'} = $tm[2];
81		$hw_date{'date'} = $tm[3];
82		$hw_date{'month'} = &number_to_month($tm[4]);
83		$hw_date{'year'} = $tm[5]+1900;
84		$hw_date{'day'} = &number_to_weekday($tm[6]);
85		}
86
87	if (!@tm) {
88		# Didn't actually work!
89		print $get_hardware_time_error || $text{'index_eformat'};
90		print "<p>\n";
91		}
92	elsif (!$access{'hwdate'}) {
93		# Allow editing of hardware time
94		if( !$access{ 'sysdate' } ) {
95		    $hw_date{ 'second' } = $system_date{ 'second' } if( $hw_date{ 'second' } - $system_date{ 'second' } <= $config{ 'lease' } );
96			}
97
98		print &ui_form_start("apply.cgi");
99		print &ui_hidden("mode", "hwdate");
100		print &tabletime(&hlink($text{'hw_title'}, "hardware_time"),
101				 0, %hw_date);
102		print &ui_submit($text{'action_save'}, "action");
103		if (support_hwtime()) {
104			print &ui_submit($text{'action_sync_s'}, "action");
105			}
106		print &ui_form_end();
107		}
108	else {
109		# Show show the hardware time
110		print &tabletime(&hlink($text{'hw_title'}, "hardware_time"),
111				 1, %hw_date ),"<p>\n";
112		}
113	}
114print &ui_tabs_end_tab();
115
116if ($access{'timezone'} && &has_timezone()) {
117	print &ui_tabs_start_tab("mode", "zone");
118	print $text{'index_desczone'},"<p>\n";
119
120	print &ui_form_start("save_timezone.cgi");
121	print &ui_table_start($text{'index_tzheader'}, "width=100%", 2);
122
123	my @zones = &list_timezones();
124	my $cz = &get_current_timezone();
125	my $found = 0;
126	my @opts = ( );
127	my $lastpfx = "";
128	foreach my $z (@zones) {
129		my $pfx;
130		if ($z->[0] =~ /^(.*)\/(.*)$/) {
131			$pfx = $1;
132			}
133		else {
134			$pfx = "";
135			}
136		if ($pfx ne $lastpfx && $z ne $zones[0]) {
137			push(@opts, [ '', '----------' ]);
138			}
139		$lastpfx = $pfx;
140		push(@opts, [ $z->[0], $z->[1] ? "$z->[0] ($z->[1])"
141					       : $z->[0] ]);
142		}
143	print "</select></td> </tr>\n";
144	print &ui_table_row($text{'index_tz'},
145		&ui_select("zone", $cz, \@opts, 1, 0, $cz ? 1 : 0));
146
147	print &ui_table_end();
148	print &ui_form_end([ [ undef, $text{'save'} ] ]);
149	print &ui_tabs_end_tab();
150	}
151
152if ( ( !$access{ 'sysdate' } && &has_command( "date" ) || !$access{ 'hwdate' } && &has_command( "hwclock" ) ) && $access{'ntp'} )
153{
154	# Show time server input
155	print &ui_tabs_start_tab("mode", "sync");
156	print $text{'index_descsync'},"<p>\n";
157
158	print &ui_form_start("apply.cgi");
159	print &ui_hidden("mode", "ntp");
160	print &ui_table_start(&hlink($text{'index_timeserver'}, "timeserver"),
161			      "width=100%", 2, [ "width=30%" ]);
162
163	print &ui_table_row($text{'index_addresses'},
164		&ui_textbox("timeserver", $config{'timeserver'}, 60));
165
166	# Show hardware time checkbox
167	if (&support_hwtime()) {
168		print &ui_table_row(" ",
169			&ui_checkbox("hardware", 1, $text{'index_hardware2'},
170				     $config{'timeserver_hardware'}));
171		}
172
173	# Show boot-time checkbox
174	my $job = &find_webmin_cron_job();
175	print &ui_table_row($text{'index_boot'},
176		&ui_yesno_radio("boot", $job && $job->{'boot'}));
177
178	# Show schedule input
179	print &ui_table_row($text{'index_sched'},
180		&ui_radio("sched", $job && !$job->{'disabled'} ? 1 : 0,
181		  [ [ 0, $text{'no'} ], [ 1, $text{'index_schedyes'} ] ]));
182	&seed_random();
183	$job ||= { 'mins' => int(rand()*60),
184		   'hours' => int(rand()*24),
185		   'days' => '*',
186		   'months' => '*',
187		   'weekdays' => '*' };
188	print &ui_table_row(undef,
189		&webmincron::show_times_input($job), 2);
190
191	print &ui_table_end();
192	print &ui_form_end([ [ "action", $text{'index_sync'} ] ]);
193	print &ui_tabs_end_tab();
194}
195print &ui_tabs_end(1);
196
197
198&ui_print_footer( "/", $text{ 'index' } );
199
200# tabletime(label, read-only, &time)
201# Output a table for setting the date and time
202sub tabletime
203{
204my ( $label, $ro, %src ) = @_,
205my %assoc_day = ( "Mon", $text{ 'day_1' }, "Tue", $text{ 'day_2' }, "Wed", $text{ 'day_3' }, "Thu", $text{ 'day_4' }, "Fri", $text{ 'day_5' }, "Sat", $text{ 'day_6' }, "Sun", $text{ 'day_0' } ),
206my %assoc_month = ( "Jan", $text{ 'month_1' }, "Feb", $text{ 'month_2' }, "Mar", $text{ 'month_3' }, "Apr", $text{ 'month_4' }, "May", $text{ 'month_5' }, "Jun", $text{ 'month_6' }, "Jul", $text{ 'month_7' }, "Aug", $text{ 'month_8' }, "Sep", $text{ 'month_9' }, "Oct", $text{ 'month_10' }, "Nov", $text{ 'month_11' }, "Dec", $text{ 'month_12' } );
207
208my $rv = &ui_table_start($label, "width=100%", 6);
209if (!$ro) {
210	$rv .= &ui_table_row($text{'date'},
211	    &ui_select("date", int($src{'date'}), [ 1 .. 31 ]));
212	$rv .= &ui_table_row($text{'month'},
213	    &ui_select("month",
214		       &zeropad(&month_to_number($src{'month'})+1, 2),
215                       [ map { [ &zeropad($_, 2), $text{'month_'.$_} ] }
216                             ( 1 .. 12 ) ]));
217	$rv .= &ui_table_row($text{'year'},
218	    &ui_select("year", $src{'year'}, [ 1969 .. 2037 ]));
219	$rv .= &ui_table_row($text{'hour'},
220	    &ui_select("hour", &zeropad($src{'hour'}, 2),
221		       [ map { &zeropad($_, 2) } (00 .. 23) ]));
222	$rv .= &ui_table_row($text{'minute'},
223		&ui_select("minute", &zeropad($src{'minute'}, 2),
224                       [ map { &zeropad($_, 2) } (00 .. 59) ]));
225	$rv .= &ui_table_row($text{'second'},
226		&ui_select("second", &zeropad($src{'second'}, 2),
227                       [ map { &zeropad($_, 2) } (00 .. 59) ]));
228	}
229else {
230	$rv .= &ui_table_row($text{'date'}, $src{'date'});
231	$rv .= &ui_table_row($text{'month'}, $src{'month'});
232	$rv .= &ui_table_row($text{'year'}, $src{'year'});
233	$rv .= &ui_table_row($text{'hour'}, $src{'hour'});
234	$rv .= &ui_table_row($text{'minute'}, $src{'minute'});
235	$rv .= &ui_table_row($text{'second'}, $src{'second'});
236	}
237$rv .= &ui_table_end();
238return $rv;
239}
240