#!/usr/bin/perl -T
# $Id: confmanage.cgi,v 1.3 2016/01/27 07:09:51 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2005              ###
###                                                      ###
############################################################
############################################################
use strict;
use warnings;
use CGI qw(:standard :netscape);
use Fcntl qw(:DEFAULT :flock);
use RFC::Main;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;
use Sys::Hostname;

my $config = RFC::Main::config();


if (!defined(param))
{
    &print_header("Welcome to the maintenance for the $name");
    print '<center>';
    RFC::RFCCGI::login();
    print '</center>';
    &print_end;
}
else
{
    if ((&login_ok()) && (&is_admin()))
    { 
        print_header("$name main page");
	print RFC_start_html(-title=>"$name main page", -style=>{src=>'/celltest/RFCcontrol.css'});
        top_nav_bar_start();
	top_no_button;
	top_no_button;
	top_no_button;
        tab_newrow();
        js_back();
        top_js_return('#006600','white');
	top_no_button;
	top_no_button;
	top_no_button;
        top_nav_bar_button('main.cgi','','','To menu','s4');
        end_top_bar;
	$_ = param('action');
      CASE:
	{
	    /^save$/ and do {&save(); &show_complete_file(); last CASE;};
	    ### Default response ###
	    &show_complete_file();
	}
	&print_end;
    }
    elsif (&login_ok())
    {
	&print_header("$name maintenance center");
	print h2("You do not have superuser priviledges!");
	print h2("Only superusers may change configuration files");
	print '<center>';
	RFC::RFCCGI::login();
	print '</center>';
	&print_end;
    }
    else
    {
	&not_auth;
    }
}

sub show_complete_file
{
    my $host = hostname;
    print center(h3("Global configuration for $name on $host"));
    print p();
    print start_form(-class=>'form');
    &print_hidden();
    &action('save');
    my $string = join("\n",$config->readlines);
    print center(textarea(-name=>'conf',-value=>$string,-cols=>'120',-rows=>'24',-wrap=>'off'));
    print br;
    print center(submit(-name=>'save',-value=>'Save',-class=>'s0',-style=>"width:200px;"));
    print end_form;
}

sub save
{
    my $string = &get_CGI_value('conf');
    $string =~ s/[\f\r\b]+//g;
    return undef unless ($string);
    $config->writeline($string);
}
