From 38323111671dca3030adde900fa4f8036dda6fdb Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Mon, 30 Mar 2009 22:48:37 +0000 Subject: [PATCH] Completed example module --- foobar/foobar-lib.pl | 52 +++++++++++++++++++++++++++++++++++++++++ foobar/install_check.pl | 23 ++++++++++++++++++ foobar/lang/en | 10 ++++++++ foobar/log_parser.pl | 16 +++++++++++++ foobar/save.cgi | 48 +++++++++++++++++++++++++++++++++++++ 5 files changed, 149 insertions(+) create mode 100644 foobar/install_check.pl create mode 100644 foobar/log_parser.pl create mode 100644 foobar/save.cgi diff --git a/foobar/foobar-lib.pl b/foobar/foobar-lib.pl index d06d0f192..5a299f6c2 100644 --- a/foobar/foobar-lib.pl +++ b/foobar/foobar-lib.pl @@ -34,5 +34,57 @@ close(CONF); return @rv; } +=head2 create_foobar_website(&site) + +Adds a new website, specified by the C hash reference parameter, which +must contain C and C 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 hash reference parameter, which +must be a modified entry returned from the C 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 hash reference parameter, which +must have been one of the elements returned by C + +=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 index 000000000..7e75970dd --- /dev/null +++ b/foobar/install_check.pl @@ -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; +} + diff --git a/foobar/lang/en b/foobar/lang/en index c364ad4c4..d1aa15d2b 100644 --- a/foobar/lang/en +++ b/foobar/lang/en @@ -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 index 000000000..abc470cee --- /dev/null +++ b/foobar/log_parser.pl @@ -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, ¶ms) + +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, ''.html_escape($object).''); +} + diff --git a/foobar/save.cgi b/foobar/save.cgi new file mode 100644 index 000000000..ca7795875 --- /dev/null +++ b/foobar/save.cgi @@ -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(''); +