Handle hostnames with upper-case letters
[webmin.git] / webalizer / save_log.cgi
1 #!/usr/local/bin/perl
2 # save_log.cgi
3 # Save, create or delete options for a log file
4
5 require './webalizer-lib.pl';
6 &foreign_require("cron", "cron-lib.pl");
7 &ReadParse();
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'});
12
13 # Find the cron job
14 if (!$in{'new'} && !$in{'view'} && !$in{'run'}) {
15         @jobs = &cron::list_cron_jobs();
16         foreach $j (@jobs) {
17                 $job = $j if ($j->{'command'} eq "$cron_cmd $in{'file'}");
18                 }
19         }
20
21 if ($in{'view'}) {
22         # Re-direct to the view page
23         &redirect("view_log.cgi/".&urlize(&urlize($in{'file'}))."/index.html");
24         }
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'}");
28         }
29 elsif ($in{'run'}) {
30         # Force report generation and show the output
31         &ui_print_unbuffered_header(undef, $text{'gen_title'}, "");
32
33         $lconf = &get_log_config($in{'file'});
34         print "<b>",&text('gen_header', "<tt>$in{'file'}</tt>"),"</b><br>\n";
35         print "<pre>";
36         $rv = &generate_report($in{'file'}, STDOUT, 1);
37         print "</pre>\n";
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";
42                 }
43         elsif ($rv) {
44                 print "<b>$text{'gen_nothing'}</b><p>\n";
45                 }
46         else {
47                 print "<b>$text{'gen_failed'}</b><p>\n";
48                 }
49
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'});
54         exit;
55         }
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;
61         if ($job) {
62                 &lock_file($job->{'file'});
63                 &foreign_call("cron", "delete_cron_job", $job);
64                 &unlock_file($job->{'file'});
65                 }
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'});
71         }
72 else {
73         # Validate and store inputs
74         if ($in{'new'}) {
75                 -r $in{'file'} && !-d $in{'file'} ||
76                         &error($text{'save_efile'});
77                 }
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'};
84                 }
85         elsif (!$in{'new'} && $lconf->{'dir'}) {
86                 # This is not a new log, so the user cannot be changed
87                 }
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;
94                 }
95         else {
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'};
99                 }
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);
106
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);
118                 }
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); 
123                 }
124         elsif ($lconf->{'sched'} && $oldjob) {
125                 # Update the cron job
126                 &foreign_call("cron", "change_cron_job", $job); 
127                 }
128         elsif (!$lconf->{'sched'} && $oldjob) {
129                 # Delete the cron job
130                 &foreign_call("cron", "delete_cron_job", $job);
131                 }
132         &unlock_file(&cron::cron_file($job)) if ($job);
133
134         if ($in{'new'}) {
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);
141                 }
142
143         # Create or link the custom webalizer.conf file
144         local $cfile = &config_file_name($in{'file'});
145         if ($in{'cmode'} == 0) {
146                 # None at all
147                 &unlink_logged($cfile);
148                 }
149         elsif ($in{'cmode'} == 1) {
150                 # File under /etc/webmin/webalizer
151                 if (-l $cfile) {
152                         &unlink_logged($cfile);
153                         }
154                 if (!-r $cfile) {
155                         # Need to copy into place
156                         &copy_source_dest($config{'webalizer_conf'}, $cfile);
157                         }
158                 }
159         elsif ($in{'cmode'} == 2) {
160                 # Symbolic link to somewhere
161                 &unlink_logged($cfile);
162                 symlink($in{'cfile'}, $cfile);
163                 }
164
165         # Update the log file's options
166         local $cfile = &log_config_name($in{'file'});
167         &lock_file($cfile);
168         &save_log_config($in{'file'}, $lconf);
169         &unlock_file($cfile);
170         &webmin_log($in{'new'} ? "create" : "modify", "log", $in{'file'});
171         }
172 &redirect("");
173