Handle hostnames with upper-case letters
[webmin.git] / webmincron / webmincron-lib.pl
1 =head1 webmincron-lib.pl
2
3 Functions for creating and listing Webmin scheduled functions.
4
5 =cut
6
7 BEGIN { push(@INC, ".."); };
8 use WebminCore;
9 &init_config();
10
11 $webmin_crons_directory = "$module_config_directory/crons";
12 @special_modes = ( 'hourly', 'daily', 'weekly', 'monthly', 'yearly' );
13
14 =head2 list_webmin_crons
15
16 Returns a list of all scheduled Webmin functions. Each of which is a hash ref
17 with keys :
18
19 =item id - A unique ID number
20
21 =item module - The module in which the function is defined
22
23 =item file - File in which the function is declared
24
25 =item func - Name of the function to call
26
27 =item args - Array ref of strings to pass to the function as parameters
28
29 =item interval - Number of seconds between runs (optional)
30
31 =item mins - Minutes on which to run. Can be * or a comma-separated list
32
33 =item hours - Hours on which to run
34
35 =item days - Days of the month on which to run
36
37 =item months - Months of the year on which to run
38
39 =item weekdays - Days of week on which to run
40
41 =item special - Can be 'hourly', 'daily', 'weekly' or 'monthly'
42
43 =cut
44 sub list_webmin_crons
45 {
46 my @rv;
47 opendir(CRONS, $webmin_crons_directory) || return ( );
48 foreach my $f (readdir(CRONS)) {
49         if ($f =~ /^(\d+)\.cron$/) {
50                 my %cron;
51                 &read_file_cached("$webmin_crons_directory/$f", \%cron);
52                 $cron{'id'} = $1;
53                 my @args;
54                 for(my $i=0; defined($cron{'arg'.$i}); $i++) {
55                         push(@args, $cron{'arg'.$i});
56                         delete($cron{'arg'.$i});
57                         }
58                 if (@args) {
59                         $cron{'args'} = \@args;
60                         }
61                 push(@rv, \%cron);
62                 }
63         }
64 return @rv;
65 }
66
67 =head2 save_webmin_crons(&cron)
68
69 Create or update a webmin cron function. Also locks the file being written to.
70
71 =cut
72 sub save_webmin_cron
73 {
74 my ($cron) = @_;
75 if (!-d $webmin_crons_directory) {
76         &make_dir($webmin_crons_directory, 0700);
77         }
78 if (!$cron->{'id'}) {
79         $cron->{'id'} = time().$$;
80         }
81 my $file = "$webmin_crons_directory/$cron->{'id'}.cron";
82 my %wcron = %$cron;
83 if ($wcron{'args'}) {
84         for(my $i=0; $i<@{$wcron{'args'}}; $i++) {
85                 $wcron{'arg'.$i} = $wcron{'args'}->[$i];
86                 }
87         delete($wcron{'args'});
88         }
89 &lock_file($file);
90 &write_file($file, \%wcron);
91 &unlock_file($file);
92 eval {
93         # Reload may fail in Webmin isn't running
94         $main::error_must_die = 1;
95         &reload_miniserv();
96         };
97 }
98
99 =head2 delete_webmin_cron(&cron)
100
101 Deletes the file for a webmin cron function. Also does locking.
102
103 =cut
104 sub delete_webmin_cron
105 {
106 my ($cron) = @_;
107 my $file = "$webmin_crons_directory/$cron->{'id'}.cron";
108 &lock_file($file);
109 &unlink_file($file);
110 &unlock_file($file);
111 &reload_miniserv();
112 }
113
114 =head2 find_webmin_cron(module, function, [&args])
115
116 Returns a Webmin cron hash ref matching the given module and function
117
118 =cut
119 sub find_webmin_cron
120 {
121 my ($module, $func, $args) = @_;
122 my @crons = &list_webmin_crons();
123 foreach my $oc (@crons) {
124         next if ($oc->{'module'} ne $module);
125         next if ($oc->{'func'} ne $func);
126         if ($args) {
127                 my $sameargs = 1;
128                 for(my $i=0; $i < scalar(@{$oc->{'args'}}) ||
129                              $i < scalar(@$args); $i++) {
130                         $sameargs = 0 if ($oc->{'args'}->[$i] ne $args->[$i]);
131                         }
132                 next if (!$sameargs);
133                 }
134         return $oc;
135         }
136 return undef;
137 }
138
139 =head2 create_webmin_cron(&cron, [old-cron-command])
140
141 Create or update a webmin cron job that calls some function.
142 If the old-cron parameter is given, find and replace the regular cron job
143 of that name.
144
145 =cut
146 sub create_webmin_cron
147 {
148 my ($cron, $old_cmd) = @_;
149
150 # Find and replace existing cron with same module, function and args
151 my $already = &find_webmin_cron($cron->{'module'}, $cron->{'func'},
152                                 $cron->{'args'});
153 if ($already) {
154         # Update existing, possibly with new interval
155         $cron->{'id'} = $already->{'id'};
156         }
157 &save_webmin_cron($cron);
158
159 # Find and delete any Unix cron job that this is replacing
160 if ($old_cmd && &foreign_installed("cron")) {
161         &foreign_require("cron");
162         my @jobs = &cron::list_cron_jobs();
163         @jobs = grep {
164              $_->{'user'} eq 'root' &&
165              $_->{'command'} =~ /(^|[ \|\&;\/])\Q$old_cmd\E($|[ \|\&><;])/
166              } @jobs;
167         foreach my $job (reverse(@jobs)) {
168                 &lock_file(&cron::cron_file($job));
169                 &cron::delete_cron_job($job);
170                 &unlock_file(&cron::cron_file($job));
171                 }
172         }
173 }
174
175 =head2 delete_webmin_module_crons(module)
176
177 Remove all Webmin cron jobs for some module
178
179 =cut
180 sub delete_webmin_module_crons
181 {
182 my ($mod) = @_;
183 foreach my $cron (&list_webmin_crons()) {
184         if ($cron->{'module'} eq $mod) {
185                 &delete_webmin_cron($cron);
186                 }
187         }
188 }
189
190 =head2 show_times_input(&job, [special])
191
192 Returns HTML for inputs for selecting the schedule for a cron job, defined
193 by the first parameter which must be a hash ref returned by list_cron_jobs.
194
195 =item job - Hash ref for a webmincron object
196
197 =item special - 0=don't allow special times (like @hourly), 1=allow
198
199 =cut
200 sub show_times_input
201 {
202 my ($job, $special) = @_;
203 $special = 0 if (!defined($special));
204 my $rv = "<table width=100%>\n";
205 if ($special || $job->{'special'}) {
206         # Allow selection of special @ times
207         $rv .= "<tr $cb> <td colspan=6>\n";
208         $rv .= &ui_radio("special_def", $job->{'special'} ? 1 : 0,
209                 [ [ 1, $text{'edit_special1'}." ".
210                        &ui_select("special", $job->{'special'},
211                                   [ map { [ $_, $text{'edit_special_'.$_} ] }
212                                         @special_modes ]) ],
213                   [ 0, $text{'edit_special0'} ] ]);
214         $rv .= "</td></tr>\n";
215         }
216
217 # Javascript to disable and enable fields
218 $rv .= <<EOF;
219 <script>
220 function enable_cron_fields(name, form, ena)
221 {
222 var els = form.elements[name];
223 els.disabled = !ena;
224 for(i=0; i<els.length; i++) {
225   els[i].disabled = !ena;
226   }
227 }
228 </script>
229 EOF
230
231 $rv .= "<tr $tb>\n";
232 $rv .= "<td><b>$text{'edit_mins'}</b></td> ".
233        "<td><b>$text{'edit_hours'}</b></td> ".
234        "<td><b>$text{'edit_days'}</b></td> ".
235        "<td><b>$text{'edit_months'}</b></td> ".
236        "<td><b>$text{'edit_weekdays'}</b></td>";
237 $rv .= "</tr> <tr $cb>\n";
238
239 my @mins = (0..59);
240 my @hours = (0..23);
241 my @days = (1..31);
242 my @months = map { $text{"month_$_"}."=".$_ } (1 .. 12);
243 my @weekdays = map { $text{"day_$_"}."=".$_ } (0 .. 6);
244 my $arrmap = { 'mins' => \@mins,
245                'hours' => \@hours,
246                'days' => \@days,
247                'months' => \@months,
248                'weekdays' => \@weekdays };
249
250 foreach my $arr ("mins", "hours", "days", "months", "weekdays") {
251         # Find out which ones are being used
252         my %inuse;
253         my $min = ($arr =~ /days|months/ ? 1 : 0);
254         my $max = $min+scalar(@{$arrmap->{$arr}})-1;
255         foreach my $w (split(/,/ , $job->{$arr})) {
256                 if ($w eq "*") {
257                         # all values
258                         for(my $j=$min; $j<=$max; $j++) { $inuse{$j}++; }
259                         }
260                 elsif ($w =~ /^\*\/(\d+)$/) {
261                         # only every Nth
262                         for(my $j=$min; $j<=$max; $j+=$1) { $inuse{$j}++; }
263                         }
264                 elsif ($w =~ /^(\d+)-(\d+)\/(\d+)$/) {
265                         # only every Nth of some range
266                         for(my $j=$1; $j<=$2; $j+=$3) { $inuse{int($j)}++; }
267                         }
268                 elsif ($w =~ /^(\d+)-(\d+)$/) {
269                         # all of some range
270                         for(my $j=$1; $j<=$2; $j++) { $inuse{int($j)}++; }
271                         }
272                 else {
273                         # One value
274                         $inuse{int($w)}++;
275                         }
276                 }
277         if ($job->{$arr} eq "*") { undef(%inuse); }
278
279         # Output selection list
280         $rv .= "<td valign=top>\n";
281         $rv .= sprintf
282                 "<input type=radio name=all_$arr value=1 %s %s %s> %s<br>\n",
283                 $arr eq "mins" && $hourly_only ? "disabled" : "",
284                 $job->{$arr} eq "*" ||  $job->{$arr} eq "" ? "checked" : "",
285                 "onClick='enable_cron_fields(\"$arr\", form, 0)'",
286                 $text{'edit_all'};
287         $rv .= sprintf
288                 "<input type=radio name=all_$arr value=0 %s %s> %s<br>\n",
289                 $job->{$arr} eq "*" || $job->{$arr} eq "" ? "" : "checked",
290                 "onClick='enable_cron_fields(\"$arr\", form, 1)'",
291                 $text{'edit_selected'};
292         $rv .= "<table> <tr>\n";
293         my @arrlist = @{$arrmap->{$arr}};
294         for(my $j=0; $j<@arrlist; $j+=12) {
295                 my $jj = $j + 11;
296                 if ($jj >= @arrlist) { $jj = @arrlist - 1; }
297                 my @sec = @arrlist[$j .. $jj];
298                 $rv .= sprintf
299                         "<td valign=top><select %s size=%d name=$arr %s>\n",
300                         $arr eq "mins" && $hourly_only ? "" : "multiple",
301                         @sec > 12 ? 12 : scalar(@sec),
302                         $job->{$arr} eq "*" ||  $job->{$arr} eq "" ?
303                                 "disabled" : "";
304                 foreach my $v (@sec) {
305                         if ($v =~ /^(.*)=(.*)$/) { $disp = $1; $code = $2; }
306                         else { $disp = $code = $v; }
307                         $rv .= sprintf "<option value=\"$code\" %s>$disp\n",
308                                 $inuse{$code} ? "selected" : "";
309                         }
310                 $rv .= "</select></td>\n";
311                 }
312         $rv .= "</tr></table></td>\n";
313         }
314 $rv .= "</tr></table>\n";
315 return $rv;
316 }
317
318 =head2 parse_times_input(&job, &in)
319
320 Parses inputs from the form generated by show_times_input, and updates a cron
321 job hash ref. The in parameter must be a hash ref as generated by the 
322 ReadParse function.
323
324 =cut
325 sub parse_times_input
326 {
327 my $job = $_[0];
328 my %in = %{$_[1]};
329 my @pers = ("mins", "hours", "days", "months", "weekdays");
330 if ($in{'special_def'}) {
331         # Job time is a special period
332         foreach my $arr (@pers) {
333                 delete($job->{$arr});
334                 }
335         $job->{'special'} = $in{'special'};
336         }
337 else {
338         # User selection of times
339         foreach my $arr (@pers) {
340                 if ($in{"all_$arr"}) {
341                         # All mins/hrs/etc.. chosen
342                         $job->{$arr} = "*";
343                         }
344                 elsif (defined($in{$arr})) {
345                         # Need to work out and simplify ranges selected
346                         my @range = split(/\0/, $in{$arr});
347                         my @range = sort { $a <=> $b } @range;
348                         my @newrange;
349                         my $start = -1;
350                         for(my $i=0; $i<@range; $i++) {
351                                 if ($i && $range[$i]-1 == $range[$i-1]) {
352                                         # ok.. looks like a range
353                                         if ($start < 0) { $start = $i-1; }
354                                         }
355                                 elsif ($start < 0) {
356                                         # Not in a range at all
357                                         push(@newrange, $range[$i]);
358                                         }
359                                 else {
360                                         # End of the range.. add it
361                                         $newrange[@newrange - 1] =
362                                                 "$range[$start]-".$range[$i-1];
363                                         push(@newrange, $range[$i]);
364                                         $start = -1;
365                                         }
366                                 }
367                         if ($start >= 0) {
368                                 # Reached the end while in a range
369                                 $newrange[@newrange - 1] =
370                                         "$range[$start]-".$range[$i-1];
371                                 }
372                         $job->{$arr} = join(',' , @newrange);
373                         }
374                 else {
375                         &error(&text('save_enone', $text{"edit_$arr"}));
376                         }
377                 }
378         delete($job->{'special'});
379         }
380 }
381
382
383
384 1;
385