#!/usr/bin/perl
#WHMADDON:configsupport:Configure Support Request Submission
# cpanelpro - addon_configsupport.cgi        Copyright(c) 2004-2006 cPanel, Inc.
#                                                           All Rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use lib '/usr/local/cpanel', '/usr/local/cpanel/whostmgr/docroot/cgi';
use strict;
use warnings;
use whmlib;
require 'parseform.pl';

my %FORM = parseform();

print "Content-type: text/html\r\n\r\n";

defheader('Support Request Configuration');

if ( !-e "/var/cpanel/pro" ) {
    mkdir "/var/cpanel/pro", 0755;
}

my $remote_user = $ENV{'REMOTE_USER'} ? $ENV{'REMOTE_USER'} : 'root';
$remote_user =~ s/\///g;    # Prevent directory traversal

my $conffile = $remote_user ne 'root' ? '/var/cpanel/pro/' . $remote_user . '_support.conf' : '/var/cpanel/pro/support.conf';

if ( !$FORM{'cgiaction'} ) {
    my %support_conf;
    if ( open my $conf_fh, '<', $conffile ) {
        while ( my $line = readline $conf_fh ) {
            chomp $line;
            my ( $name, $value ) = split /\s*=\s*/, $line, 2;
            next if !$name;
            $support_conf{$name} = $value;
        }
        close $conf_fh;
    }
    my %support_conf_name;
    $support_conf_name{'displayhostnamesubject'} = "Display the hostname of the server in the subject.";
    $support_conf_name{'displaydomainsubject'}   = "Display the customer\'s domain name in the subject.";
    $support_conf_name{'displayusersubject'}     = "Display the customer\'s username in the subject.";
    $support_conf_name{'displayipsubject'}       = "Display the customer\'s client ip in the subject.";
    $support_conf_name{'displaybrowsersubject'}  = "Display the customer\'s browser in the subject.";
    $support_conf_name{'displayhostnamebody'}    = "Display the hostname of the server in the body.";
    $support_conf_name{'displaydomainbody'}      = "Display the customer\'s domain name in the body.";
    $support_conf_name{'displayuserbody'}        = "Display the customer\'s username in the body.";
    $support_conf_name{'displayipbody'}          = "Display the customer\'s client ip in the body.";
    $support_conf_name{'displaybrowserbody'}     = "Display the customer\'s browser in the body.";

    my $checked         = 'checked="checked"';
    my $emailchecked    = $checked;
    my $redirectchecked = '';
    my $disablechecked  = '';

    if ( $support_conf{'type'} =~ m/redirect/i ) {
          $redirectchecked = $checked;
          $emailchecked    = '';
          $disablechecked  = '';
    }
    elsif ( $support_conf{'type'} =~ m/disable/i ) {
          $redirectchecked = '';
          $emailchecked    = '';
          $disablechecked  = $checked;
    }

    print <<"EOM";
<br />
<div align="center">
<b>This feature allows you to configure where support requests go when they are submitted through cPanel.</b>
</div>
<br /><br />
<form action="addon_configsupport.cgi">
<input type="hidden" name="cgiaction" value="save">
<input type="radio" name="type" value="email" $emailchecked><b>Email support requests to</b>&nbsp;</input>
    <input size="40" type="text" name="supportaddy" value="$support_conf{'supportaddy'}">
<br />
<b>&nbsp;&nbsp;or</b>
<br />
<b>Pipe support requests to</b>&nbsp;<input size="60" type="text" name="emailpipecmd" value="$support_conf{'emailpipecmd'}">
<blockquote>
EOM

    foreach my $conf ( sort keys %support_conf_name ) {
          my $status = $support_conf{$conf} ? $checked : '';
          print <<"EOM";
<input type="checkbox" value="1" name="$conf" $status>&nbsp;$support_conf_name{$conf}</input>
<br />
EOM
    }
    print <<"EOM";
</blockquote>
<br />
<input type="radio" name="type" value="redirect" $redirectchecked><b>Redirect the user to the following url:</b></input>
<input type="text" name="supporturl" value="$support_conf{'supporturl'}" size="80" />
<br /><br />
<input type="radio" name="type" value="disable" $disablechecked><b>Disable this feature</b></input>
<br /><br />
<input type="submit" value="Save" />
EOM
}
elsif ( $FORM{'cgiaction'} =~ m/save/i ) {
      delete $FORM{'cgiaction'};

      if ( open my $support_conf, '>', $conffile ) {
          foreach my $var ( sort keys %FORM ) {
              print {$support_conf} $var . '=' . $FORM{$var} . "\n";
          }
          close $support_conf;
          print <<'EOM';
<br /><br />
<div align="center">
<b>Your selection has been saved.</b>
<br /><br />
<a href="addon_configsupport.cgi"><b>Go Back</b></a>
</div>

EOM
      }
      else {
          print "Unable to save configuration. Error was: $!";
      }
}

1;
