#!/usr/bin/perl
use strict;
use CGI qw(:standard);
use XML::Simple;

my $remote_ip = $ENV{'REMOTE_ADDR'};
my $debug= "";
my @template;
my @website;
my $tmp;

sub error_page {
	print "<center>
		<h1>The following error has been occored: @_</h1><br><br><br>
		<a href=\"index.cgi\">home</a><br>
		</center>
	" ;
exit 1;
}

open(TEMPLATE, "<data/template.htm") || error_page "Cannot open template!";
@template = <TEMPLATE>;
close(TEMPLATE);

@website = split ("#cutmark#", join ( "" ,@template) );

my $page = $ENV{'QUERY_STRING'};
$page=~ s/(\.\.)|\\|\[|\]|\(|\)|\&|\;//gi;
$page="home.htm" if ( $ENV{'QUERY_STRING'} eq "" );

if ( $page =~ /\.htm/ ) {

	my $pageid=$page;
	$pageid=~ s/\.htm|\.html//gi;

        print header(),
"$website[0]
<!-- Maintext start -->
";

	open(STATIC, "<data/static/$page") || error_page "Cannot open $page!";
	print <STATIC>;
	close(STATIC);

	print "
<!-- Maintext end -->
$website[1]";

} else {

	my $content_xml = XMLin ( "data/xml/$page.xml", , forcearray => 1);

	print header(),
"$website[0]
<!-- Maintext start -->
";

	print "<table border=0 width=100% height=100% cellspacing=0 cellpadding=0>";

	my $counter=0;
	my $float="left";

	while ($content_xml->{'paragraph'}->[$counter] or $content_xml->{'text'}->[$counter] or $content_xml->{'menu'}->[$counter]) {
	
		my $pcounter=0;

		print "<tr><td colspan=2><h2>$content_xml->{'paragraph'}->[$counter]->{'caption'}</h2></td></tr>" if $content_xml->{'paragraph'}->[$counter]->{'caption'};
		print "<tr><td><font size=2>";
		while ( $content_xml->{'paragraph'}->[$counter]->{'text'}->[$pcounter] ) {
			if ( $content_xml->{'paragraph'}->[$counter]->{'image'}->[$pcounter]->{'file'} ) {
				print "<img src=\"images/$content_xml->{'paragraph'}->[$counter]->{'image'}->[$pcounter]->{'file'}\" style=\"float:$float; margin:4px;\">" ;
				if ($float == "left") { $float="right" } else { $float="left" };
			}
			if ($content_xml->{'paragraph'}->[$counter]->{'text'}->[$pcounter]) {
				$tmp=$content_xml->{'paragraph'}->[$counter]->{'text'}->[$pcounter];
	
				$tmp=~ s/#br#/<br>/gi;
				$tmp=~ s/#amp#/&amp;/gi;
				$tmp=~ s/#open#/</gi;
				$tmp=~ s/#close#/>/gi;
				$tmp=~ s/#ls#/<a href=\"/gi;
				$tmp=~ s/#lm#/\">/gi;
				$tmp=~ s/#le#/<\/a>/gi;
	
				print "$tmp<br>";
			}
			$pcounter++;
		}

		my $mcounter=0;

		print "<table align=center celspacing=0 celpadding=0 border=0 width=40%>\n";
		while ( $content_xml->{'menu'}->[$counter]->{'menuline'}->[$mcounter]->{'text'} ) {
			if ($content_xml->{'menu'}->[$counter]->{'menuline'}->[$mcounter]->{'link'}) {
				print "<tr align=\"center\"><td id=\"menuline\">&nbsp;<a href=\"$content_xml->{'menu'}->[$counter]->{'menuline'}->[$mcounter]->{'link'}\">$content_xml->{'menu'}->[$counter]->{'menuline'}->[$mcounter]->{'text'}</a>&nbsp;</td></tr>\n"
			} else {
				print "<tr align=\"center\"><td id=\"menuline\">&nbsp;<font color=white>$content_xml->{'menu'}->[$counter]->{'menuline'}->[$mcounter]->{'text'}</font>&nbsp;</td></tr>\n"
			}
			$mcounter++;
		}
		print "</table>\n";

		print "</font></td></tr>\n";

		$counter++;
	};

	print "</table>";
	print "$debug";

	print "
<!-- Maintext end -->
$website[1]";
}


