#!/usr/bin/perl -T
#$Id: show_data.cgi,v 1.34 2023/10/06 05:13:19 sqko Exp $
use strict;
use warnings;
use RFC::Main;
use RFC::Rig;
use RFC::RFCCGI qw(:cgi :html);
use CGI qw(:standard :netscape);
use Sys::Hostname;
use LWP::Simple qw(get);
my $hostname = hostname();
my $ref = &RFC::RFCCGI::get_user_data_ref('nocheck');
my $uid = $$ref{'uid'};
my $ssl = CGI::https();
my $u = $$ref{'name'};
my $p = $$ref{'pass'};
unless ($uid)
{
    print process('show_data_login.tt');
    exit;
}

my $allow = 0;

my $doc_root = '/home/http/html/';

my $url = param('url');
if (param('rig'))
{
    $url = 'http://'.$hostname.'/rig'.param('rig').'/';
}
$url = 'http://'.$hostname unless ($url);

if ($url && $url =~ /\/(\d+)test(\d+)/)
{
    my $r = $1;
    my $t = $2;
    $allow = &RFC::RFCCGI::rig_data_test_access_allow($r,$t,$$ref{'name'});
}
else
{
    $allow = &RFC::RFCCGI::rig_data_access_allow($$ref{'name'});
}

unless ($allow)
{
    my @authids = &get_cv('global','data_access_authorisationid');
    my %data = (
	'authorisations' => [@authids],
	);
    print process('show_data_not_auth.tt',&get_template_data_ref_nocheck(\%data));
    exit;
}


my $baseurl = $url;
$baseurl =~ s/[\w\.\-\:]+$//;

my $html = get $url;
if ($url=~ /\.pdf$/i)
{
    my $length = length($html);
    print "Content-type: application/pdf\n";
    if ($url =~/([\w\-]+\.pdf)$/)
    {
        print "Content-Disposition: inline; filename=$1\n";
    }
    print "Content-length: $length \n\n";
    print $html;
    exit;
}
if ($url=~ /\.ps$|\.eps$/i)
{
    my $length = length($html);
    print "Content-type: application/postscript\n";
    if ($url =~/([\w\-]+\.\w+)$/)
    {
        print "Content-Disposition: inline; filename=$1\n";
    }
    print "Content-length: $length \n\n";
    print $html;
    exit;
}
if ($url=~ /\.png$|\.jpg$/i)
{
    my $length = length($html);
    print "Content-type: image/gif\n";
    if ($url =~/([\w\-]+\.\w+)$/)
    {
        print "Content-Disposition: inline; filename=$1\n";
    }
    print "Content-length: $length \n\n";
    print $html;
    exit;
}

if ($url=~ /\.gz$|\.tgz$|\.zip/i)
{
    my $length = length($html);
    print "Content-type: application/x-gzip\n";
    if ($url =~/([\w\-]+\.\w+)$/)
    {
     print "Content-Disposition: inline; filename=$1\n";
    }
    print "Content-length: $length \n\n";
    print $html;
    exit;
}

my $base = 'cgi-bin/show_data.cgi?name='.$u.'&pass='.$p.'&url=';
my $baseimg = 'cgi-bin/show_img.cgi?name='.$u.'&pass='.$p.'&url=';
$html =~ s/(\<a\s+href)/\n$1/g;
$html =~ s/(\<img)/\n$1/g;
my @lines = split(/\n/,$html);

if ($url =~ /configfile|proglog|errorlog|raadata|jdata|numbers|\.txt|\.gp|tabel$|\.i2b|\.conf|\/\w+$/i)
{
    print header(-type=>'text');
}
else
{
    print header();
}

foreach my $line (@lines)
{
    if ($line =~/name="name" value=""/)
    {
	$line =~ s/name="name" value=""/name="name" value="$u"/g;
    }
    if ($line =~/name="pass" value=""/)
    {
	$line =~ s/name="pass" value=""/name="pass" value="$p"/g;
    }
    if ($line =~ /^<a href="([\w\.\:\-\/]+)\s*"\s*>/)
    {
	my $link = $1;
	if ($link =~ /http/)
	{
	    
	}
	else
	{
	    $link = '/'.$base.$baseurl.$link;
	}
	$line =~ s/^<a href="([\w\.\:\-\/]+)\s*"\s*>/<a href="$link">/;
    }
    if ($line =~ /^<img\s+src="([\w\.\-\:\/]+)"/ && $line !~ /icons/)
    {
	my $link = $1;
	$link = '/'.$baseimg.$baseurl.$link;
	$line =~ s/^<img\s+src="[\w\.\-\:\/]+"/<img src="$link"/;
    }
    print $line,"\n";
}
