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