1#!/usr/local/bin/perl
2# save_env.cgi
3# Save an existing environment variable, or create a new one
4
5require './cron-lib.pl';
6&error_setup($text{'env_err'});
7&ReadParse();
8
9@jobs = &list_cron_jobs();
10if ($in{'new'}) {
11	$job = { };
12	}
13else {
14	$oldjob = $jobs[$in{'idx'}];
15	$job->{'file'} = $oldjob->{'file'};
16	$job->{'line'} = $oldjob->{'line'};
17	}
18
19if ($in{'delete'}) {
20	# Just re-direct to delete CGI
21	&redirect("delete_env.cgi?idx=$in{'idx'}");
22	exit;
23	}
24
25# Check if this user is allowed to execute cron jobs
26&can_use_cron($in{'user'}) ||
27	&error(&text('save_eallow', $in{'user'}));
28
29# Check module access control
30&can_edit_user(\%access, $in{'user'}) ||
31	&error(&text('save_ecannot', $in{'user'}));
32
33@files = &unique((map { $_->{'file'} } @jobs),
34	         "$config{'cron_dir'}/$in{'user'}");
35foreach $f (@files) { &lock_file($f); }
36
37# Check and parse inputs
38if ($in{'name'} !~ /^\S+$/) {
39	&error($text{'save_ename'});
40	}
41if (!$in{'user'}) {
42	&error($text{'save_euser'});
43	}
44if (!defined(getpwnam($in{'user'}))) {
45	&error(&text('save_euser2', $in{'user'}));
46	}
47$job->{'active'} = $in{'active'};
48$job->{'name'} = $in{'name'};
49$job->{'value'} = $in{'value'};
50$job->{'user'} = $in{'user'};
51
52if (!$in{'new'}) {
53	# Editing an existing variable
54	&can_edit_user(\%access, $oldjob->{'user'}) ||
55		&error(&text('save_ecannot', $oldjob->{'user'}));
56	if ($job->{'user'} eq $oldjob->{'user'}) {
57		# Not changing user
58		if ($in{'where'}) {
59			&delete_cron_job($job);
60			&insert_cron_job($job);
61			}
62		else {
63			&change_cron_job($job);
64			}
65		}
66	else {
67		# Changing user
68		&delete_cron_job($oldjob);
69		if ($in{'where'}) {
70			&insert_cron_job($job);
71			}
72		else {
73			&create_cron_job($job);
74			}
75		}
76	}
77else {
78	# Creating a new variable
79	if ($in{'where'}) {
80		&insert_cron_job($job);
81		}
82	else {
83		&create_cron_job($job);
84		}
85	}
86
87foreach $f (@files) { &unlock_file($f); }
88if ($in{'new'}) {
89	&webmin_log("create", "env", $in{'user'}, \%in);
90	}
91else {
92	&webmin_log("modify", "env", $in{'user'}, \%in);
93	}
94&redirect("");
95
96
97