3 # Save an existing cron job, or create a new one
5 require './cron-lib.pl';
6 &error_setup($text{'save_err'});
9 @jobs = &list_cron_jobs();
11 $access{'create'} || &error($text{'save_ecannot2'});
12 $job = { 'type' => 0 };
15 $oldjob = $jobs[$in{'idx'}];
16 $job->{'type'} = $oldjob->{'type'};
17 $job->{'file'} = $oldjob->{'file'};
18 $job->{'line'} = $oldjob->{'line'};
19 $job->{'nolog'} = $oldjob->{'nolog'};
22 @files = &unique((map { $_->{'file'} } @jobs),
23 "$config{'cron_dir'}/$in{'user'}");
24 foreach $f (@files) { &lock_file($f); }
26 # Check and parse inputs
27 if ($in{"cmd"} !~ /\S/ && $access{'command'}) {
28 &error($text{'save_ecmd'});
30 if (&supports_users()) {
32 &error($text{'save_euser'});
34 if (!defined(getpwnam($in{'user'}))) {
35 &error(&text('save_euser2', $in{'user'}));
38 &parse_times_input($job, \%in);
39 $in{input} =~ s/\r//g; $in{input} =~ s/%/\\%/g;
40 $in{cmd} =~ s/%/\\%/g;
41 $job->{'active'} = $in{'active'};
42 if ($access{'command'}) {
43 $job->{'command'} = $in{'cmd'};
44 if ($in{input} =~ /\S/) {
45 @inlines = split(/\n/ , $in{input});
46 $job->{'command'} .= '%'.join('%' , @inlines);
50 $job->{'command'} = $oldjob->{'command'};
52 $job->{'comment'} = $in{'comment'};
53 &unconvert_comment($job);
55 if (&supports_users()) {
56 # Check if this user is allowed to execute cron jobs
57 &can_use_cron($in{'user'}) ||
58 &error(&text('save_eallow', $in{'user'}));
59 $job->{'user'} = $in{'user'};
62 if (defined($in{'range_def'})) {
64 &parse_range_input($job, \%in);
65 &unconvert_range($job);
68 # Check module access control
69 &can_edit_user(\%access, $in{'user'}) ||
70 &error(&text('save_ecannot', $in{'user'}));
73 # Editing an existing job
74 &can_edit_user(\%access, $oldjob->{'user'}) ||
75 &error(&text('save_ecannot', $oldjob->{'user'}));
76 if ($job->{'user'} eq $oldjob->{'user'}) {
77 &change_cron_job($job);
80 &delete_cron_job($oldjob);
81 &create_cron_job($job);
83 # Find new index, which will change due to user move
84 undef(@cron_jobs_cache);
86 foreach $newjob (&list_cron_jobs()) {
87 if ($newjob->{'user'} eq $job->{'user'} &&
88 $newjob->{'active'} eq $job->{'active'} &&
89 $newjob->{'command'} eq $job->{'command'}) {
90 $in{'idx'} = $newjob->{'index'};
97 &create_cron_job($job);
100 foreach $f (@files) { &unlock_file($f); }
102 &webmin_log("create", "cron", $in{'user'}, \%in);
105 &webmin_log("modify", "cron", $in{'user'}, \%in);
108 if ($in{'saverun'}) {
109 # Redirect to execute form
110 defined($in{'idx'}) || &error($text{'save_eidx'});
111 &redirect("exec_cron.cgi?idx=$in{'idx'}");
114 # Just go back to main menu