Handle hostnames with upper-case letters
[webmin.git] / foobar / foobar-lib.pl
1 =head1 foobar-lib.pl
2
3 Functions for the Foobar Web Server. This is an example Webmin module for a
4 simple fictional webserver.
5
6 =cut
7
8 use WebminCore;
9 init_config();
10
11 =head2 list_foobar_websites()
12
13 Returns a list of all websites served by the Foobar webserver, as hash
14 references with C<domain> and C<directory> keys.
15
16 =cut
17 sub list_foobar_websites
18 {
19 my @rv;
20 my $lnum = 0;
21 open(CONF, $config{'foobar_conf'});
22 while(<CONF>) {
23         s/\r|\n//g;
24         s/#.*$//;
25         my ($dom, $dir) = split(/\s+/, $_);
26         if ($dom && $dir) {
27                 push(@rv, { 'domain' => $dom,
28                             'directory' => $dir,
29                             'line' => $lnum });
30                 }
31         $lnum++;
32         }
33 close(CONF);
34 return @rv;
35 }
36
37 =head2 create_foobar_website(&site)
38
39 Adds a new website, specified by the C<site> hash reference parameter, which
40 must contain C<domain> and C<directory> keys.
41
42 =cut
43 sub create_foobar_website
44 {
45 my ($site) = @_;
46 open_tempfile(CONF, ">>$config{'foobar_conf'}");
47 print_tempfile(CONF, $site->{'domain'}." ".$site->{'directory'}."\n");
48 close_tempfile(CONF);
49 }
50
51 =head2 modify_foobar_website(&site)
52
53 Updates a website specified by the C<site> hash reference parameter, which
54 must be a modified entry returned from the C<list_foobar_websites> function.
55
56 =cut
57 sub modify_foobar_website
58 {
59 my ($site) = @_;
60 my $lref = read_file_lines($config{'foobar_conf'});
61 $lref->[$site->{'line'}] = $site->{'domain'}." ".$site->{'directory'};
62 flush_file_lines($config{'foobar_conf'});
63 }
64
65 =head2 delete_foobar_website(&site)
66
67 Deletes a website, specified by the C<site> hash reference parameter, which
68 must have been one of the elements returned by C<list_foobar_websites>
69
70 =cut
71 sub delete_foobar_website
72 {
73 my ($site) = @_;
74 my $lref = read_file_lines($config{'foobar_conf'});
75 splice(@$lref, $site->{'line'}, 1);
76 flush_file_lines($config{'foobar_conf'});
77 }
78
79 =head2 apply_configuration()
80
81 Signal the Foobar webserver process to re-read it's configuration files.
82
83 =cut
84 sub apply_configuration
85 {
86 kill_byname_logged('HUP', 'foobard');
87 }
88
89 1;
90