Handle hostnames with upper-case letters
[webmin.git] / apache / create_htaccess.cgi
1 #!/usr/local/bin/perl
2 # create_htaccess.cgi
3 # Creates a new .htaccess file for some directory
4
5 require './apache-lib.pl';
6 &error_setup($text{'htaccess_err'});
7 &ReadParse();
8 $access{'global'} || &error($text{'htaccess_ecannot'});
9 $conf = &get_config();
10 $in{'file'} || &error($text{'htaccess_eempty'});
11 $in{'file'} =~ /^\// && $in{'file'} !~ /\.\./ ||
12         &error($text{'htaccess_eabsolute'});
13
14 if (-d $in{'file'}) {
15         # user entered a directory.. create a file in that directory
16         $accfile = &find_directive("AccessFile", $conf);
17         if (!$accfile) { $accfile = ".htaccess"; }
18         $file = "$in{'file'}/$accfile";
19         }
20 else {
21         $file = $in{'file'};
22         }
23 &allowed_auth_file($file) ||
24         &error($text{'htaccess_ecreate'});
25
26 # create the file (if needed), and add to the known list
27 &lock_file($file);
28 if (!(-r $file)) {
29         &open_tempfile(HTACCESS, ">$file");
30         &close_tempfile(HTACCESS);
31         chmod(0755, $file);
32
33         $u = &find_directive("User", $conf);
34         if ($u =~ /#(\d+)/) { $u = $1; }
35         elsif (defined($u)) { $u = getpwnam($u); }
36
37         $g = &find_directive("Group", $conf);
38         if ($g =~ /#(\d+)/) { $g = $1; }
39         elsif (defined($g)) { $g = getgrnam($g); }
40
41         chown(defined($u) ? $u : $< , defined($g) ? $g : $( , $file);
42         }
43 &read_file("$module_config_directory/site", \%site);
44 @ht = split(/\s+/, $site{'htaccess'});
45 $site{'htaccess'} = join(' ', &unique(@ht, $file));
46 &write_file("$module_config_directory/site", \%site);
47 &unlock_file($file);
48 &webmin_log("htaccess", "create", $file);
49
50 # redirect to editing index
51 &redirect("htaccess_index.cgi?file=".&urlize($file));
52