#!/usr/bin/perl -T
# $Id: search_reports.cgi,v 1.35 2026/02/23 10:56:28 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2005              ###
###                                                      ###
############################################################
############################################################

use strict;
use warnings;
use RFC::Main;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;
use CGI qw(:standard :netscape);
use Fcntl qw(:DEFAULT :flock);
use SemaforeFile;
use Sys::Hostname;
use URI::Escape;

my $ref = &RFC::RFCCGI::get_user_data_ref();
my $uid = $$ref{'uid'};

my $usermode = $$ref{'auth'};
my $readmode = $$ref{'auth'};
my $modifymode = $$ref{'cert'};

my %access = ();
my $rig = &get_CGI_value_clean('rig');

if (!defined(param))
{
    print process('to_login.tt');
}
else
{
    if ($uid)
    {
	my $authid = &get_cv('global','report_authorisationid');
	if ($authid)
	{
	    $readmode = $$ref{'allow_search_reports'};
	    $modifymode = &RFC::Main::passwd_client('safety_cert',$$ref{'name'},$authid);
	}
        print process('main_nav.tt');
        $_ = param('action');
      CASE:
        {
            /^view_inf/i and do {&print_info; last CASE;};
            /^search/i and do {&main_menu; &search; last CASE;};
            ### Insert case statements ###
    	    &main_menu;
        }
        print process('end_html.tt');
    }
    else 
    {
        print process('not_auth.tt');
    }
}
exit;




sub print_report
{

    my $user = RFC::RFCCGI::get_user;
    my $pass = RFC::RFCCGI::get_pass;
    my $file = param('file');
    my $pdf = param('pdf');
    my $cell = '';
    my $short = '';
    my $test = $file;
    $test =~ /(\d+)test(\d+)/;
    my $rig = $1;
    $test = $2;
    $short = '_short' if ($file =~ /_short/ );
    my $auth = &RFC::RFCCGI::rig_data_test_access_allow($rig,$test);
    unless ($auth)
    {
        print process('main_nav.tt');
        print h1("You are not authorised wot view the report for $rig test $test!");
        print process('end_html.tt');
	exit;
    }
    if (($pdf) && ($pdf =~ /YES/))
    {
        $pdf = 1;
	my $fname = $rig.'test'.$test.$short.'.pdf';
        print header(-type=>'application/pdf',-Content_disposition=>"attachment; filename=$fname");
    }
    else
    {
        $pdf = 0;
        my $fname = $rig.'test'.$test.$short.'.ps';
        print header(-type=>'application/postscript',-Content_disposition=>"attachment; filename=$fname");
    }
    my $infofile = $RFC::Header::publicdir.'rig'.$rig.'/'.$rig.'test'.$test.'/info.txt';
    my $i = SemaforeFile->new($infofile);
    my @lines = $i->readlines;
    for my $line (@lines)
    {
	if ($line =~ /(?:Cell|Sample) number\s+\:\s+([\d\w\-]+)/)
	{
	    $cell = $1;
	    last;
	}
    }
    my $testfile = '/home/rig'.$rig.'/'.$rig.'test'.$test.'/'.$rig.'test'.$test.'_'.$cell.$short.'.ps';
    $testfile = '/home/rig'.$rig.'/'.$rig.'test'.$test.'/'.$rig.'test'.$test.'_'.$cell.$short.'.pdf' if ($pdf);
    if (!-e($testfile))
    {
	### Revert to main file if short version does not exsist ###
	$testfile = '/home/rig'.$rig.'/'.$rig.'test'.$test.'/'.$rig.'test'.$test.'_'.$cell.'.ps';
	$testfile = '/home/rig'.$rig.'/'.$rig.'test'.$test.'/'.$rig.'test'.$test.'_'.$cell.'.pdf' if ($pdf);
    }
    my $f = SemaforeFile->new($testfile);
    my $res = join("\n",$f->readlines);
    print $res;
}




sub print_info()
{
    unless ($readmode)
    {
	print center(h1({-class=>'Alert'},"You are not authorised to view reports for rig $rig!"));
	return;
    }
    my $file = &get_CGI_value_clean('file');
    $file =~ /(\d+)test(\d+)/;
    my $rig = $1+0;
    my $test = $2+0;
    my $testfile = $RFC::Header::publicdir.'rig'.$rig.'/'.$file.'/info.txt';
    my $user = $_[1];
    my $pass = $_[2];
    my $infodir = $RFC::Header::homedir.'infofiles/';
    my $lock = $RFC::Header::homedir.'info.lock';
    my $f = SemaforeFile->new($testfile,$lock);
    my @dat = $f->readlines;
    my $proglog = $RFC::Header::publicdir.'rig'.$rig.'/'.$file.'/proglog';
    my $progf = SemaforeFile->new($proglog,$lock);
    my @proginf = $progf->readlines;;
    my $buffer = join("\n",@dat);
    print p();
    print center(h2({-class=>'C'},"Cell information:"));
    print center(textarea(-name=>'ccell_inf',-value=>$buffer,-rows=>'20',-wrap=>'physical',-style=>'width:80%'));
    print p('');
    if (-e($proglog))
    {
	my $string = join("\n",@proginf);
	print center(h2({-class=>'C'},"Commands given:"));
	print center(textarea(-anmE=>'proginfo',-value=>$string,-rows=>'10',-wrap=>'physical',-style=>'width:80%'));
    }
    print p('');
    my $wdir = '/rig'.$rig.'/'.$rig.'test'.$test;
    my $dir = $RFC::Header::publicdir.$wdir;
    if (-d $dir)
    {
	my $hostname = hostname();
	my $st = 'http://'.$hostname.$wdir.'/';
	$st = 'https://'.$hostname.$wdir.'/' if (CGI::https());
	my $n = param('name');
	my $p = param('pass');
	print center(button(-name=>'show',-onclick=>"openInNewTab('/cgi-bin/show_data.cgi?url=$st&name=$n&pass=$p')",-value=>'Raw data',-class=>'rfcbutton'));
    }
}


sub main_menu
{
    print '<div class="content-container fit-content" style="margin-top:30px;">';
    print start_form(-class=>'form');
    &print_hidden();
    print "<table class=main_search>\n";
    print '<tr><td colspan=4 class="heading"> Search for cell test standard reports </td></tr>';
    print "<TR><TH>\n";
    print 'Search for:';
    print "</TH><TH>\n";
    print textfield(-name=>'search',-value=>'',-autofocus=>1);
    print "</TH><TH>\n";
    print submit(-name=>'action',-value=>'search',-class=>'rfcbutton');
    print "</TH></TR></TABLE><center>\n";
    print end_form;
    print start_form;
    &print_hidden();
    print hidden(-name=>'search',-value=>'test',-force=>'1');
    print hidden(-name=>'action',-value=>'search',-force=>'1');
    print submit(-class=>'rfcbutton',-name=>'dummy',-value=>'Show all tested cells',-style=>'width:250px;');
    print end_form;
    print '</center></div>';
    print <<END;


<style>

.main_search {
    border-spacing: 10px;
    border-collapse: separate;
}

</style>

END

}    


sub search
{
    my $user = RFC::RFCCGI::get_user;
    my $pass = RFC::RFCCGI::get_pass;
    my @list = ();
    my $listserver = get_cv('servers','listserver');
    if ($listserver)
    {
        chomp $listserver;
        my @l = split(/\t+/,&RFC::Main::report_client($listserver,'servers'));
        foreach (@l)
        {
            if (/([\w\.\-]+)/)
            {
                push @list,$1;
            }
        }
    }
    @list = (get_cv('servers','server_names')) unless (@list);
    my $st = param('search');
    $st =~ /([\w\-\(\)\\\+]+)/;
    $st = $1;
    my $port = 4040;
    for my $hostname (@list)
    {
	$hostname =~ /([\w\.\-]+)/;
	$hostname = $1;
	my $string = &RFC::Main::report_client($hostname,'search',$st);
	my @res = ();
	@res = split(/\n+/,$string) if ($string);
	&print_search_tabel($user,$pass,$hostname,@res);
    }
}


sub print_search_tabel()
{
    my $user = shift(@_);
    my $pass = shift(@_);
    my $server = shift(@_);
    my $bline;
    my $linenr = 0;
    my @rows = ();
    while ($bline = shift(@_))
    {
        if (length($bline) > 10)
        {
            my $report = 0;
            my $repf = 0;
            my ($line,$rs) = split(/\s*\#\s*/,$bline);
	    my $pdf = 0;
            if (($rs =~ /REPORT/) || ($rs =~ /PDF/))
            {
                $report = 1;
                if ($rs =~ /FINISHED/)
                {
                    $repf = 1;
                }
		$pdf = 1 if ($rs =~ /PDF/);
            }
            my $string = '';
            my @dat = ();
            my @data = split(/\s+/,$line);
            $dat[0] = join(' ',$data[1],$data[2],$data[3]);
	    my $userallow = 0;
	    if ($data[5] && $data[5] =~ /(\d+)test/)
	    {
		my $r = $1;
		eval {
		    $userallow = &get_rig_auth($r,$server);
		};
	    }
            $string = &push_button($data[5],$user,$pass,$server);
	    if ($userallow || $modifymode)
	    {
		if ($pdf)
		{
		    $string = $string.&push_button_report_pdf($data[5].'_short',$user,$pass,$data[6],$report,$repf,$server);
		    $string = $string.&push_button_report_pdf($data[5],$user,$pass,$data[6],$report,$repf,$server);
		}
		else
		{
		    $string = $string.&push_button_report($data[5].'_short',$user,$pass,$data[6],$report,$repf,$server);
		    $string = $string.&push_button_report($data[5],$user,$pass,$data[6],$report,$repf,$server);
		}
	    }
	    else
	    {
		if ($report)
		{
		    $string .= '<th>Report</th><th>Report</th>';		    
		}
		else
		{
		    $string .= '<th>No report</th><th>No report</th>';		    
		}
	    }
            push @dat,$data[4];
            for (my $x = 6;$x<scalar(@data);$x++)
            {
                push @dat,$data[$x];
            }
	    foreach (@dat)
	    {
		$_ = xss($_);
	    }
	    if (@dat > 5)
	    {
		for (my $x = 0;$x < 5;$x++)
		{
		    $string = join('',$string,td[$dat[$x]]);
		}
		$string .= ' '. td[join(' ',@dat[5..scalar(@dat)])];
	    }
	    else
	    {
		for (my $x = 0;$x < scalar(@dat);$x++)
		{
		    $string = join('',$string,td[$dat[$x]]);
		}
	    }
            push(@rows,$string);
            $linenr++;
        }
    }
    print '<div class="content-container fit-content">';
    print h3({-class=>'C'},"Total number of matches on $server : $linenr");
    if ($linenr > 0)
    {
        print
            table({-border=>'0',-cellspacing=>5},
                  Tr([@rows]));
    }
    print '</div>';
}

sub push_button($$$)
{
    my $testfile = $RFC::Header::homedir.'infofiles/'.$_[0].'.inf';
    my $user = $_[1];
    my $pass = $_[2];
    my $server = $_[3];
    my $hostname = hostname;
    $hostname =~ /([\w\-]+)/;
    $hostname = $1;
    my $string = '<TD><CENTER>';
    if ($hostname eq $server)
    {
        $string = join('',$string,start_form);
    }
    else
    {
	my $st = 'http://'.$server.'/cgi-bin/celltest/search_reports.cgi';
	$st = 'https://'.$server.'/cgi-bin/celltest/search_reports.cgi' if (CGI::https());
        $string = join('',$string,start_form(-action=>$st,-class=>'form'));
    }
    $string = join('',$string,hidden(-name=>'time',-value=>&RFC::RFCCGI::get_logout_time,-force=>'1'));
    $string = join('',$string,hidden(-name=>'jscheck',-value=>'1',-force=>'1'));
    $string = join('',$string,hidden(-name=>'action',-value=>'view_inf',-force=>'1'));
    $string = join('',$string,hidden(-name=>'file',-value=>$_[0],-force=>'1'));
    $string = join('',$string,hidden(-name=>'name',-value=>$user,-force=>'1'));
    $string = join('',$string,hidden(-name=>'pass',-value=>$pass,-force=>'1'));
    $string = join('',$string,hidden(-name=>'search'));
    $string = join('',$string,submit(-class=>'rfcbutton',-name=>'view',-value=>$_[0]));
    my ($rig,$test) = split(/test/,$_[0]);
    $string = join('',$string,hidden(-name=>'rig',-value=>$rig,-force=>1));
    $string = join('',$string,hidden(-name=>'test',-value=>$test,-force=>1));
    $string = join('',$string,end_form);
    $string = join('',$string,'</CENTER></TD>');
    return $string;
}


sub push_button_report($$$$)
{
    my $test = $_[0];
    my $user = $_[1];
    my $pass = $_[2];
    my $cell = $_[3];
    my $rep = $_[4];
    my $fin = $_[5];
    my $server = $_[6];
    my $hostname = hostname;
    $hostname =~ /([\w\-]+)/;
    $hostname = $1;
    my $string = '';
    if ($rep)
    {
        my $class = 'rfcbutton';
#        if ($fin)
#        {
#            $class = 's3';
#        }
        $string = '<TD><CENTER>';
        if ($hostname eq $server)
        {
            $string = join('',$string,start_form);
        }
        else
        {
            my $st = 'http://'.$server.'/cgi-bin/celltest/search_reports.cgi';
	    $st = 'https://'.$server.'/cgi-bin/celltest/search_reports.cgi' if (CGI::https());
            $string = join('',$string,start_form(-action=>$st));
        }
	$string = join('',$string,hidden(-name=>'jscheck',-value=>'1',-force=>'1'));
	$string = join('',$string,hidden(-name=>'action',-value=>'report',-force=>'1'));
        $string = join('',$string,hidden(-name=>'time',-value=>&RFC::RFCCGI::get_logout_time,-force=>'1'));
        $string = join('',$string,hidden(-name=>'file',-value=>$_[0],-force=>'1'));
	$string = join('',$string,hidden(-name=>'name',-value=>$user,-force=>'1'));
	$string = join('',$string,hidden(-name=>'pass',-value=>$pass,-force=>'1'));
        $string = join('',$string,hidden(-name=>'search'));
        my $val = 'Report';
        $val = 'Full version' if ($_[0] !~ /_short/);
        $string = join('',$string,hidden(-name=>'view',-value=>'report',-force=>'1'));
        $string = join('',$string,submit(-class=>$class,-name=>'abcedfg',-value=>$val));
	my ($rig,$test) = split(/test/,$_[0]);
	$string = join('',$string,hidden(-name=>'rig',-value=>$rig,-force=>1));
	$string = join('',$string,hidden(-name=>'test',-value=>$test,-force=>1));
        $string = join('',$string,end_form);
        $string = join('',$string,'</CENTER></TD>');
    }
    else
    {
        $string = '<TD><CENTER>  </CENTER></TD>';
    }
    return $string;
}

sub push_button_report_pdf($$$$)
{
    my $test = $_[0];
    my $user = $_[1];
    my $pass = $_[2];
    my $cell = $_[3];
    my $rep = $_[4];
    my $fin = $_[5];
    my $server = $_[6];
    my $hostname = hostname;
    $hostname =~ /([\w\-]+)/;
    $hostname = $1;
    my $string = '';
    if ($rep)
    {
        my $class = 'rfcbutton';
#        if ($fin)
#        {
#            $class = 's3';
#        }
        $string = '<TD><CENTER>';
        if ($hostname eq $server)
        {
            $string = join('',$string,start_form);
        }
        else
        {
            my $st = 'http://'.$server.'/cgi-bin/celltest/search_reports.cgi';
	    $st = 'https://'.$server.'/cgi-bin/celltest/search_reports.cgi' if (CGI::https());
            $string = join('',$string,start_form(-action=>$st));
        }
	$string = join('',$string,hidden(-name=>'jscheck',-value=>'1',-force=>'1'));
	$string = join('',$string,hidden(-name=>'action',-value=>'report',-force=>'1'));
        $string = join('',$string,hidden(-name=>'time',-value=>&RFC::RFCCGI::get_logout_time,-force=>'1'));
        $string = join('',$string,hidden(-name=>'file',-value=>$_[0],-force=>'1'));
	$string = join('',$string,hidden(-name=>'name',-value=>$user,-force=>'1'));
	$string = join('',$string,hidden(-name=>'pass',-value=>$pass,-force=>'1'));
	$string = join('',$string,hidden(-name=>'pdf',-value=>'YES',-force=>'1'));
        $string = join('',$string,hidden(-name=>'search'));
        my $val = 'Report';
        $val = 'Full version' if ($_[0] !~ /_short/);
        $string = join('',$string,hidden(-name=>'view',-value=>'report',-force=>'1'));
        $string = join('',$string,submit(-class=>$class,-name=>'abcedfg',-value=>$val));
	my ($rig,$test) = split(/test/,$_[0]);
	$string = join('',$string,hidden(-name=>'rig',-value=>$rig,-force=>1));
	$string = join('',$string,hidden(-name=>'test',-value=>$test,-force=>1));
        $string = join('',$string,end_form);
        $string = join('',$string,'</CENTER></TD>');
    }
    else
    {
        $string = '<TD><CENTER>  </CENTER></TD>';
    }
    return $string;
}



sub get_rig_auth($)
{
    my $rig = shift;
    unless (exists $access{$rig})
    {
	my $server = shift;
	$access{$rig} = &RFC::Main::report_client($server,'rig_auth',$rig,&get_user());
    }
    return $access{$rig};
}
