#!/usr/bin/perl -T
# $Id: global_conf.cgi,v 1.11 2026/03/04 08:01:55 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();
my $ref = &RFC::RFCCGI::get_user_data_ref();
my $uid = $$ref{'uid'};

if (!defined(param))
{
    print process('to_login.tt');
}
else
{
    if ($uid)
    {
        print process('main_nav.tt');
        $_ = param('action');
      CASE:
        {
            ### Insert case statements ###
            ### Default response ####
	    /^save$/ and do {&save(); &show_complete_file(); last CASE;};
	    /^conf$/ and do {&conf(); last CASE;};
	    ### Default response ###
	    &show_complete_file();
        }
        print process('end_html.tt');
    }
    else 
    {
        print process('not_auth.tt');
    }
}

sub show_complete_file
{
    my %data = (
	'allow' => $$ref{'admin'}, 
	'mode' => 'global',
	'conf' =>  [$config->readlines()],
	);
    print process('edit_conf.tt',,&get_template_data_ref(\%data));
}

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

sub conf
{
    my $hostname = hostname;
    my @server_list = sort {$a cmp $b} (&get_cv('servers','server_names'));
    my %data = (
	'global' => {
	    'set_warning_email_on_start' => &my_get_cv('global','set_warning_email_on_start'),
		'report_authorisationid' => &my_get_cv('global','report_authorisationid'),
		'errormails' => &my_get_cv('global','errormails'),
		'allow_exec' => &my_get_cv('global','allow_exec'),
		'account_number_regexp' => &my_get_cv('global','account_number_regexp'),
		'remote_equipment_database' => &my_get_cv('global','remote_equipment_database'),
		'server_name' => &my_get_cv('global','server_name'),
		'mailhost' => &my_get_cv('global','mailhost'),
		'mailuser' => &my_get_cv('global','mailuser'),
		'only_allow_explicit_data_access' => &my_get_cv('global','only_allow_explicit_data_access'),
		'restrict_access_auth_only' => &my_get_cv('global','restrict_access_auth_only'),
		'GUI_reload_time' => &my_get_cv('global','GUI_reload_time'),
		'project_number_validate_url' => &my_get_cv('global','project_number_validate_url'),
		'project_number_autocomplete_url' => &my_get_cv('global','project_number_autocomplete_url'),
		'remote_log_url' => &my_get_cv('global','remote_log_url'),
		'logbook_url' => &my_get_cv('global','logbook_url'),
		'allow_access_current_test_to_auth_users' => &my_get_cv('global','allow_access_current_test_to_auth_users'),
		'allowed_ip' => &my_get_cv('global','allowed_ip'),
		'equipmentid' => &my_get_cv('global','equipmentid'),
	},
	'admin' => {
	    'save_raw_kanaldata' => &my_get_cv('admin','save_raw_kanaldata'),
	    'system_mail_users' => &my_get_cv('admin','system_mail_users'),
	},
	'passwds' => {
	    'passwd_server_port' => &my_get_cv('passwds','passwd_server_port'),
	    'passwd_server' => &my_get_cv('passwds','passwd_server'),
	    'passwd_server_url' => &my_get_cv('passwds','passwd_server_url'),
	    'sso_url' => &my_get_cv('passwds','sso_url'),
	    'sso_logout_url' => &my_get_cv('passwds','sso_logout_url'),
	},
	'servers' => {
	    'listserver' => &my_get_cv('servers','listserver'),
	    'server_names' => &my_get_cv('servers','server_names'),
	    'server_list' => [@server_list],
	},
	'celltest' => {
	    'rigs' => &my_get_cv('celltest','rigs'),
	    'start_test_mail_users' => &my_get_cv('celltest','start_test_mail_users'),
	    'rigs_'.$hostname => &my_get_cv('celltest','rigs_'.$hostname),
	    'all_rigs' => &my_get_cv('celltest','rigs_'.$hostname),
	    'all_rigs_key' => 'rigs_'.$hostname,
	},
	'iVcurves' => {
	    'current_limit' => &my_get_cv('iVcurves','current_limit'),
	    'minimum_cell_voltage_for_iv' => &my_get_cv('iVcurves','minimum_cell_voltage_for_iv'),
	    'maximum_cell_voltage_for_iv' => &my_get_cv('iVcurves','maximum_cell_voltage_for_iv'),
	},
	'hostname' => $hostname,
	);
    if (-e $RFC::Header::publicdir.'/RFCfurnace')
    {
	$data{'RFCfurnace'} = {
	    'project_regex' => &my_get_cv('RFCfurnace','project_regex'),
	    'host_allow' => &my_get_cv('RFCfurnace','host_allow'),
	    'host_certify' => &my_get_cv('RFCfurnace','host_certify'),
	    'remote_element_list_soap' => &my_get_cv('RFCfurnace','remote_element_list_soap'),
	};
	$data{'global'}{'sample_number_autocomplete_url'} = &my_get_cv('global','sample_number_autocomplete_url');
    }
    print process('server_conf.tt',,&get_template_data_ref(\%data));
}

sub my_get_cv()
{
    my $val = &get_cv(@_);
    $val = '' unless defined($val);
    return $val;
}
