#!/usr/bin/perl

#########################################################
require '/home/elite/public_html/cgi-bin/nws.cfg';

%in = &ReadForm;

#########################################################
# Fetch news

if($news)
{
    open(news_data_file,"<$news_data");
   	while(<news_data_file>)
   	{
        ($ID,$news_datae,$news_head,$news_body) = split(/\|/,$_);
        $push = "$ID\|$news_datae\|$news_head\|$news_body\n";
        push(@reprint,$_);
    }
  	close(news_data_file);
}

#########################################################

&Template("/home/elite/public_html/cgi-bin/nws.tem");

open(news_data_file,"<$news_data");
@news_data_content = <news_data_file>;
close(news_data_file);

foreach$content(@news_data_content)
{
    ($ID,$news_datae,$news_head,$news_body) = split(/\|/,$content);
    $push = "$ID\|$news_datae\|$news_head\|$news_body";
    push(@news_data,$push);
}

#########################################################

$show_max = $news_per_page;

for($news_content = $show; $news_content < scalar @news_data;)
{
    ($ID,$news_date,$news_head,$news_body) = split(/\|/,$news_data[$news_content]);
	if($ID ne "")
	{
              	$display_headline .= &Cell('news_body');
	}
    $news_content++;
    last if $news_content-$show == $show_max;
}

if($news_content > $show_max || $news_content < @news_data)
{
	if($news_content > $show_max)
	{
  	    $show_back = $show-$show_max;
      	$back_span = "<b><a href=\"$script_url\?show=$show_back\">Back</a></b>";
	}

	if($news_content < @news_data)
	{
	    if($news_content-$show == $show_max)
	    {
          	$show_next = $show+$show_max;
          	$next_span = "<b><a href=\"$script_url\?show=$show_next\">Next</a></b>";
     	}
	}

	$display_span .= &Cell("show_span");
}

if(!$display_headline)
{
    $display_headline = "$no_news"
};

print &Template("/home/elite/public_html/cgi-bin/nws.tem",'temp');

#########################################################
# TEMPLATE

sub Template
{
  	local(*FILE);

  	if    ($_[1] eq 'temp')
  	{
  	    print "Content-type: text/html\n\n"  unless ($ContentType++ > 0);
  	}
  	elsif ($_[1] eq 'text')
  	{
  	    print "Content-type: text/plain\n\n" unless ($ContentType++ > 0);
  	}

  	if    (!$_[0])
  	{
  	    return "<br>\nTemplate : No file was specified<br>\n";
  	}
  	elsif (!-e "$_[0]")
  	{
  	    return "<br>\nTemplate : File '$_[0]' does not exist<br>\n";
  	}
  	else
  	{
    	open(FILE, "<$_[0]") || return "<br>\nTemplate : Could open $_[0]<br>\n";
    	while (<FILE>)
    	{
    	    $FILE .= $_;
    	}
    	close(FILE);
    	for ($FILE)
    	{
      	    s/<!-- ins : (.*?) -->/\1/gi;				# show hidden inserts
          	s/<!-- def : (\w+) -->(?:\r\n|\n)?(.*?)<!-- \/def : \1 -->/
	        $CELL{$1}=$2;''/ges;					# read/remove template cells
          	s/\%(\w+)\%/${$1}/g;					# translate %%
      	}
    }
  	return $FILE;
}

#########################################################
# TRANSLATE CELL

sub Cell
{
  	my($CELL);
  	for (0..$#_)
  	{
  	    if ($_[$_])
  	    {
  	        $CELL .= $CELL{$_[$_]};
  	    }
  	}

  	if    (!$_[0])
  	{
  	    return "<br>\nCell : No cell was specified<br>\n";
  	}
  	elsif (!$CELL)
  	{
  	    return "<br>\nCell : Cell '$_[0]' is not defined<br>\n";
  	}
  	else
  	{
  	    $CELL =~ s/\%(\w+)\%/${$1}/g;   # translate %%
  	}
  	return $CELL;
}

#########################################################
# PARSE FORM

sub ReadForm
{
  	my($max) = $_[1];					# Max Input Size
  	my($name,$value,$pair,@pairs,$buffer,%hash);		# localize variables

  	# Check input size if max input size is defined
  	if ($max && ($ENV{'CONTENT_LENGTH'}||length $ENV{'QUERY_STRING'}) > $max) {
    	die("ReadForm : Input exceeds max input limit of $max bytes\n");
    	}

  	# Read GET or POST form into $buffer
 	if    ($ENV{'REQUEST_METHOD'} eq 'POST') 
	{ 
	    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); 
	}
 	if    ($ENV{'QUERY_STRING'}) 
	{ 
	    $buffer .= $ENV{'QUERY_STRING'}; 
	}

  	@pairs = split(/&/, $buffer);				# Split into name/value pairs
  	foreach $pair (@pairs) 
	{
   	    ($name, $value) = split(/=/, $pair);		# split into $name and $value
    	$value =~ tr/+/ /;					# replace "+" with " "
    	$value =~ s/%([A-F0-9]{2})/pack("C", hex($1))/egi;	# replace %hex with char
    	$$name = $value;
    }

  	return %hash;

  }
