#!/usr/bin/perl -T
#$Id: show_img.cgi,v 1.22 2023/07/04 11:26:12 sqko Exp $
use strict;
use warnings;
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 $ssl = CGI::https();
my $allow = 0;

my $url = param('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)
{
    print header("Data gateway for $hostname");
    print start_html("Data gateway for $hostname");
    print center(h1('Warning: You are not allowed to access data on this server!'));
    print end_html();
    exit;
}

my $url2 = $url;
$url2 =~ s/^.*\/rig/\/rig/;
my $html = undef;

my $f = '/home/http/html'.$url2;
if (-e $f)
{
    my $BLOCK_SIZE=1024;
    open MYIN,'<',$f or die "Could not opn file $f\n";
    binmode(MYIN);
    my $buf;
    my $ct=0;
    my @l = ();
    while(read(MYIN,$buf,$BLOCK_SIZE,$ct*$BLOCK_SIZE))
    {
	push @l,$buf;
    }
    close MYIN;
    $html = join('',@l);
}
else
{
    $html = get $url;
}

my $length = length($html);
print "Content-type: image/gif\n";
print "Content-length: $length \n\n";
print $html;
exit;
