Completed example module
authorJamie Cameron <jcameron@webmin.com>
Mon, 30 Mar 2009 22:48:37 +0000 (22:48 +0000)
committerJamie Cameron <jcameron@webmin.com>
Mon, 30 Mar 2009 22:48:37 +0000 (22:48 +0000)
foobar/foobar-lib.pl
foobar/install_check.pl [new file with mode: 0644]
foobar/lang/en
foobar/log_parser.pl [new file with mode: 0644]
foobar/save.cgi [new file with mode: 0644]

index d06d0f1..5a299f6 100644 (file)
@@ -34,5 +34,57 @@ 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;
 
diff --git a/foobar/install_check.pl b/foobar/install_check.pl
new file mode 100644 (file)
index 0000000..7e75970
--- /dev/null
@@ -0,0 +1,23 @@
+
+do 'foobar-lib.pl';
+
+=head2 is_installed(mode)
+
+For mode 1, returns 2 if Foobar is installed and configured for use by
+Webmin, 1 if installed but not configured, or 0 otherwise.
+For mode 0, returns 1 if installed, 0 if not
+
+=cut
+sub is_installed
+{
+my ($mode) = @_;
+
+# This is the code that you'd want if the Foobar webserver really existed
+#if (-r $config{'foobar_conf'}) {
+#      return $mode + 1;
+#      }
+#return 0;
+
+return $mode + 1;
+}
+
index c364ad4..d1aa15d 100644 (file)
@@ -9,3 +9,13 @@ create_title=Create Website
 edit_header=Foobar Webserver website details
 edit_domain=Website domain name
 edit_directory=Directory containing website contents
+
+save_err=Failed to save website
+save_edomain=Missing or invalid domain name
+save_edirectory=Directory must be an absolute path
+save_edirectory2=Directory does not exist
+save_egone=Website no longer exists
+
+log_create=Created website $1
+log_modify=Modified website $1
+log_delete=Deleted website $1
diff --git a/foobar/log_parser.pl b/foobar/log_parser.pl
new file mode 100644 (file)
index 0000000..abc470c
--- /dev/null
@@ -0,0 +1,16 @@
+# log_parser.pl
+# Functions for parsing this module's logs
+
+do 'foobar-lib.pl';
+
+=head2 parse_webmin_log(user, script, action, type, object, &params)
+
+Converts logged information from this module into human-readable form
+
+=cut
+sub parse_webmin_log
+{
+my ($user, $script, $action, $type, $object, $p) = @_;
+return &text('log_'.$action, '<tt>'.html_escape($object).'</tt>');
+}
+
diff --git a/foobar/save.cgi b/foobar/save.cgi
new file mode 100644 (file)
index 0000000..ca77958
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+# Create, update or delete a website
+
+require 'foobar-lib.pl';
+ReadParse();
+error_setup($text{'save_err'});
+lock_file($config{'foobar_conf'});
+
+# Get the old site object
+if (!$in{'new'}) {
+       my @sites = list_foobar_websites();
+        ($site) = grep { $_->{'domain'} eq $in{'old'} } @sites;
+       $site || error($text{'save_egone'});
+       }
+
+if ($in{'delete'}) {
+       # Just delete it
+       delete_foobar_website($site);
+       }
+else {
+       # Validate inputs
+       $in{'domain'} =~ /^[a-z0-9\.\-\_]+$/i ||
+               error($text{'save_edomain'});
+       $in{'directory'} =~ /^\// ||
+               error($text{'save_edirectory'});
+       -d $in{'directory'} ||
+               error($text{'save_edirectory2'});
+       $site->{'domain'} = $in{'domain'};
+       $site->{'directory'} = $in{'directory'};
+
+       # Update or create
+       if ($in{'new'}) {
+               create_foobar_website($site);
+               }
+       else {
+               modify_foobar_website($site);
+               }
+       }
+
+# Log the change
+unlock_file($config{'foobar_conf'});
+apply_configuration();
+webmin_log($in{'new'} ? 'create' :
+          $in{'delete'} ? 'delete' : 'modify',
+          'site',
+          $site->{'domain'});
+&redirect('');
+