#!/bin/sh
eval 'if [ -x /usr/local/cpanel/3rdparty/bin/perl ]; then exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}; else exec /usr/bin/perl -x $0 ${1+"$@"}; fi;'
  if 0;

#!/usr/bin/perl
#WHMADDON:spamdconf:Setup Spamd Startup Configuration
#-------------------------------------------------------------------------------
# cpanel - addon_spamdconf.cgi                    Copyright(c) 2012 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cpanel license. Unauthorized copying is prohibited
#-------------------------------------------------------------------------------

##########################################################

BEGIN {
    unshift @INC, '/usr/local/cpanel', '/usr/local/cpanel/whostmgr/docroot/cgi';
}

use whmlib;

our %ACL;

if ( !$ACL{all} ) {
    print "Content-Type: text/html\n\n";
    print "Sorry, you do not have permission to modify spamd's startup options.\n";
    exit();
}

require "parseform.pl";
my %FORM = parseform();

########################################################
if ( $FORM{'cgiaction'} eq "submit" ) {
    print "Content-Type: text/html\n\n";
    handleForm();
}
else {
    printForm();
}

########################################################
# printForm
########################################################
sub printForm {
    my %spamd_conf    = load_Config("/etc/cpspamd.conf");
    my %spamd_options = load_Config("/scripts/spamd.options");
    print "Content-Type: text/html\n\n";
    defheader("Spamd Startup Configuration");
    print "<fieldset><legend>Spamd Startup Options</legend>";
    print "The following options will be used upon execution of the spamd daemon.<br/><br/>";
    print "<form action=\"./addon_spamdconf.cgi\">\n";
    print "<input type=\"hidden\" name=\"cgiaction\" value=\"submit\">\n";
    print "<table align=\"center\" width=\"80%\" cellspacing=\"0\" cellpadding=\"2\" border=\"1\">\n";
    my $bg           = "2";
    my $option       = "";
    my $optionval    = "";
    my $optionname   = "";
    my $optionstring = "";

    while ( ( $option, $optionval ) = each(%spamd_options) ) {
        my ( $optionname, $optionstring ) = formatoption( $spamd_options{$option} );
        print "<tr class=\"tdshade$bg\">";
        print "<td width=\"35%\">$optionname</td>";
        print "<td width=\"15%\"><div align=\"center\">";
        print "<input type=\"textbox\" name=\"$option\" value=\"$spamd_conf{$option}\"";
        print "></div></td>";
        print "<td width=\"50%\">$optionstring</td>";
        print "</tr>";
        if   ( $bg eq "2" ) { $bg = "1"; }
        else                { $bg = "2"; }
    }
    print "</table>&nbsp;<br/>";
    print "&nbsp;<br/><div align=\"center\"><input class='submit' type=submit value='Submit'>";
    print "</div></form>";
    print "</fieldset>";
}

####################################################
# handleForm
####################################################
sub handleForm {
    my %spamd_conf = load_Config("/etc/cpspamd.conf");
    delete $spamd_conf{''};
    my %spamd_options = load_Config("/scripts/spamd.options");

    defheader("Spamd Startup Configuration");
    print "<fieldset><legend>Spamd Configuration Results</legend>";

    my $option = "";
    while ( ($option) = each(%spamd_options) ) {
        if ( !( $FORM{$option} eq "" ) ) {
            my ( $optionname, $optionstring, $optionformat ) = formatoption( $spamd_options{$option} );
            print "Checking if $option=$FORM{$option} is valid ...";
            if ( $FORM{$option} =~ /^$optionformat$/ ) {
                if ( $option eq "pidfile" ) {
                    $FORM{$option} =~ /(.*\/)[^\/]+$/;
                    print "$1 is a directory";
                    if ( !-d $1 ) {
                        print ".....Invalid. The directory $1 does not exist!<br/>\n";
                        delete( $spamd_conf{$option} );
                        next;
                    }
                }
                $spamd_conf{$option} = $FORM{$option};
                print ".....OK<br/>";
            }
            else {
                delete( $spamd_conf{$option} );
                print ".....Invalid<br/>";
            }
        }
        else {
            print "Option $option removed<br/>";
            delete( $spamd_conf{$option} );
        }
    }

    # Write spamd_conf out to file.
    flushConfig( \%spamd_conf, "/etc/cpspamd.conf" );
    print "<b>Restarting Exim ....</b><br/>\n";
    restartSpamd();
    print "</fieldset>";
    print "<div align=\"center\">[ <a href=\"./addon_spamdconf.cgi\">Go Back</a> ]</div>";
}

##############################################################################
#   load_Config - Parses the given file which should be hold key/value pairs
#     one pair per line of the format, Key=Value
#     The returned hash is a representation of that file.
##############################################################################
sub load_Config {
    my ($file) = @_;
    if ( !-e $file ) { return (); }
    my %conf;
    open( CONF, "<", $file ) or return undef;
    my @dconf = <CONF>;
    close(CONF);
    @dconf = map { chomp; $_; } @dconf;
    foreach my $line (@dconf) {
        if ( $line =~ /^[\s\t]*#/ ) { next; }
        my ( $var, $val ) = split( /=/, $line, 2 );
        if ( $val eq "" ) { next; }
        $conf{$var} = $val;
    }
    return %conf;
}

##############################################################################
# formatoption
##############################################################################\
sub formatoption {
    my ($string) = @_;
    my ( $optionname, $optionstring, $optionformat ) = split( /=/, $string );
    return ( $optionname, $optionstring, $optionformat );
}

##############################################################################
# restartSpamd
##############################################################################
sub restartSpamd {
    system("/scripts/restartsrv_exim");
    print "<br/><br/><b>Exim and Spamd Restarted.</b>";
}

##############################################################################
# flushConfig
##############################################################################
sub flushConfig {
    my ( $conf, $filename ) = @_;

    my @aconf = map( "$_=$$conf{$_}", sort keys %$conf );
    open( CONF, ">", $filename ) or return 0;
    flock( CONF, 2 );    # Ask for an exclusive lock.. Though it's only a suggestion, why not..?
    print CONF join( "\n", @aconf );
    flock( CONF, 8 );    # Release the lock..
    close(CONF);
    return 1;
}
