3 # Save, create or delete options for a log file
5 require './webalizer-lib.pl';
6 &foreign_require("cron", "cron-lib.pl");
8 &error_setup($text{'save_err'});
9 $access{'view'} && &error($text{'edit_ecannot'});
10 !$in{'new'} || $access{'add'} || &error($text{'edit_ecannot'});
11 &can_edit_log($in{'file'}) || &error($text{'edit_efilecannot'});
14 if (!$in{'new'} && !$in{'view'} && !$in{'run'}) {
15 @jobs = &cron::list_cron_jobs();
17 $job = $j if ($j->{'command'} eq "$cron_cmd $in{'file'}");
22 # Re-direct to the view page
23 &redirect("view_log.cgi/".&urlize(&urlize($in{'file'}))."/index.html");
25 elsif ($in{'global'}) {
26 # Re-direct to the options page
27 &redirect("edit_global.cgi?file=".&urlize($in{'file'})."&type=$in{'type'}&custom=$in{'custom'}");
30 # Force report generation and show the output
31 &ui_print_unbuffered_header(undef, $text{'gen_title'}, "");
33 $lconf = &get_log_config($in{'file'});
34 print "<b>",&text('gen_header', "<tt>$in{'file'}</tt>"),"</b><br>\n";
36 $rv = &generate_report($in{'file'}, STDOUT, 1);
38 if ($rv && -r "$lconf->{'dir'}/index.html") {
39 print "<b>$text{'gen_done'}</b><p>\n";
40 print "<a href='view_log.cgi/",&urlize(urlize($in{'file'})),
41 "/index.html'>$text{'gen_view'}</a><p>\n";
44 print "<b>$text{'gen_nothing'}</b><p>\n";
47 print "<b>$text{'gen_failed'}</b><p>\n";
50 &webmin_log("generate", "log", $in{'file'});
51 &ui_print_footer("edit_log.cgi?file=".&urlize($in{'file'}).
52 "&type=$in{'type'}&custom=$in{'custom'}", $text{'edit_return'},
53 "", $text{'index_return'});
56 elsif ($in{'delete'}) {
57 # Delete this custom log file from the configuration
58 &lock_file($custom_logs_file);
59 @custom = &read_custom_logs();
60 @custom = grep { $_->{'file'} ne $in{'file'} } @custom;
62 &lock_file($job->{'file'});
63 &foreign_call("cron", "delete_cron_job", $job);
64 &unlock_file($job->{'file'});
66 &write_custom_logs(@custom);
67 &unlock_file($custom_logs_file);
68 local $cfile = &log_config_name($in{'file'});
69 &unlink_logged($cfile);
70 &webmin_log("delete", "log", $in{'file'});
73 # Validate and store inputs
75 -r $in{'file'} && !-d $in{'file'} ||
76 &error($text{'save_efile'});
78 -d $in{'dir'} || &error($text{'save_edir'});
79 $in{'cmode'} != 2 || -r $in{'cfile'} || &error($text{'save_ecfile'});
80 if ($access{'user'} eq '*') {
81 # Set the user to whatever was entered
82 defined(getpwnam($in{'user'})) || &error($text{'save_euser'});
83 $lconf->{'user'} = $in{'user'};
85 elsif (!$in{'new'} && $lconf->{'dir'}) {
86 # This is not a new log, so the user cannot be changed
88 elsif ($access{'user'} eq '') {
89 # This is a new log, or one that has not been saved for
90 # the first time yet. Use the webmin user as the user
91 defined(getpwnam($remote_user)) ||
92 &error(&text('save_ewuser', $remote_user));
93 $lconf->{'user'} = $remote_user;
96 # This is a new log, or one that has not been saved for
97 # the first time yet. Use the user set in the ACL
98 $lconf->{'user'} = $access{'user'};
100 $lconf->{'dir'} = $in{'dir'};
101 $lconf->{'sched'} = $in{'sched'};
102 $lconf->{'type'} = $in{'type'};
103 $lconf->{'over'} = $in{'over'};
104 $lconf->{'clear'} = $in{'clear'};
105 &cron::parse_times_input($lconf, \%in);
107 # Create or delete the cron job
108 local $oldjob = $job;
109 if ($lconf->{'sched'}) {
110 # Create cron job and script
111 $job->{'user'} = 'root';
112 $job->{'active'} = 1;
113 $job->{'command'} = "$cron_cmd $in{'file'}";
114 &lconf_to_cron($lconf, $job);
115 &lock_file($cron_cmd);
116 &cron::create_wrapper($cron_cmd, $module_name, "webalizer.pl");
117 &unlock_file($cron_cmd);
119 &lock_file(&cron::cron_file($job)) if ($job);
120 if ($lconf->{'sched'} && !$oldjob) {
121 # Create the cron job
122 &foreign_call("cron", "create_cron_job", $job);
124 elsif ($lconf->{'sched'} && $oldjob) {
125 # Update the cron job
126 &foreign_call("cron", "change_cron_job", $job);
128 elsif (!$lconf->{'sched'} && $oldjob) {
129 # Delete the cron job
130 &foreign_call("cron", "delete_cron_job", $job);
132 &unlock_file(&cron::cron_file($job)) if ($job);
135 # Add a new custom log file to the configuration
136 &lock_file($custom_logs_file);
137 @custom = &read_custom_logs();
138 push(@custom, { 'file' => $in{'file'}, 'type' => $in{'type'} });
139 &write_custom_logs(@custom);
140 &unlock_file($custom_logs_file);
143 # Create or link the custom webalizer.conf file
144 local $cfile = &config_file_name($in{'file'});
145 if ($in{'cmode'} == 0) {
147 &unlink_logged($cfile);
149 elsif ($in{'cmode'} == 1) {
150 # File under /etc/webmin/webalizer
152 &unlink_logged($cfile);
155 # Need to copy into place
156 ©_source_dest($config{'webalizer_conf'}, $cfile);
159 elsif ($in{'cmode'} == 2) {
160 # Symbolic link to somewhere
161 &unlink_logged($cfile);
162 symlink($in{'cfile'}, $cfile);
165 # Update the log file's options
166 local $cfile = &log_config_name($in{'file'});
168 &save_log_config($in{'file'}, $lconf);
169 &unlock_file($cfile);
170 &webmin_log($in{'new'} ? "create" : "modify", "log", $in{'file'});