#!/usr/bin/perl

use CGI::Carp;
use CGI::Carp qw(fatalsToBrowser);

print "Content-type:text/html\n\n";

	if ($ENV{'REQUEST_METHOD'} eq 'GET') {
		@pairs = split(/&/, $ENV{'QUERY_STRING'});
	}
	elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
		read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 		@pairs = split(/&/, $buffer);
	}
	else {
		print "This script must be called from the Web\nusing either GET or POST requests\n\n";
	}

foreach $pair (@pairs) {
  ($name, $value) = split(/=/, $pair);
  $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $INPUT{$name} = $value;
}

$INPUT{'a'} = "" if ($INPUT{'a'} eq "-1");    # ME/WASH RECENT HEADLINES produces a -1 for this month

if ($INPUT{'function'} eq "right") {
	$target = "target=_top";
	&right;
}
if ($INPUT{'function'} eq "left") {
	$target = "target=_top";
	&left;
}
if ($INPUT{'function'} eq "middle") {
	$target = "target=_top";
	&middle;
}
if ($INPUT{'function'} eq "mer") {
	$target = "target=_top";
	&mer;
}

&normal if ($INPUT{'function'} eq "");
&plugin if ($INPUT{'function'} eq "plugin");
&popup if ($INPUT{'function'} eq "popup");

if ($INPUT{'function'} eq "cache")
{
	if (-e "redirect/" . $INPUT{'num'} . ".txt")
	{
		&cache;
	}
}

sub popup {

$target = "target=main";
#print "<table width=100%><Tr><td>";
#&right;
#print "</td><td>";
#&left;
#print "</td></tr></table>";

$notpop = 1;
&middle;

}


sub cache {

open(IN, "redirect/" . $INPUT{'num'} . ".txt");
@indata = <IN>;
close(IN);

print "<html><head><Title>MEReport $INPUT{'num'}</title></head>";

if ($indata[0] =~ /base href=/) {
	foreach $line (@indata) {
		print $line;
	}
} else {
	print "<font size=2 face=Verdana color=black>\n\n";
	$total = "";
	$good = 0;
	foreach $line (@indata) {
		$line =~ s/<br>/\r/g;
		$line =~ s/\r/<br>/g;
		$line =~ s/<br><br>/<br>/g;
		$total = $total . $line;
	}
	if ($good == 0) {
		print "<table width=600><Tr><td><font face=Verdana><font size=2>$total</font></td></tr></table>";
	} else {
		$cnt=0;
		$counter=0;
		$mul = 70;
		$from = $counter * $mul;
		$to = ($counter+1) * $mul;
		while (substr($total, $from, $to) ne "")  {
			$extract[$counter] = substr($total, $from, $mul);
			$counter++;
			$from = $counter * $mul;
			$to = ($counter+1) * $mul;
		} 	
		for ($x=0; $x < $counter; $x++) {
			$entry = $extract[$x];
			if ($entry ne "") {
				print $entry . "<br>\n";
			}
		}
	}
	print "";
}

}


sub left {

&archived;

print "<html><head><Title>MEReport</title></head><body bgcolor=#42527B><br>";

$url = "redirect.cgi?archive=$INPUT{'archive'}&function=top&num=$INPUT{'num'}&background=white&empty=no" if ($INPUT{'empty'} eq "yes");
$white = "white";


print "<font size=2 face=Verdana color=white>";
print "<b><table><tr><td><a $target href=\"http://www.middleeast.org/launch/\"><font color=$white><b>Back to MER World</b></font></a></td>" if ($INPUT{'c'} eq "");
print "<b><table><tr><td><a $target href=\"http://www.mereport.com/\"><font color=$white><b>Back to ME Report</b></font></a></td>" if ($INPUT{'c'} eq "2");
print "<b><table><tr><td><a $target href=\"http://www.washreport.com/\"><font color=$white><b>Back to WASH Report</b></font></a></td>" if ($INPUT{'c'} eq "3");

($junk, $keep) = split(/<!-- /, $desc);
($keep, $junk) = split(/ -->/, $keep);
$filename = "redirect/" . $keep . ".txt";
if (-e $filename) {
	print "<td><font size=2 face=Verdana><b><a onmouseover=\"window.status='Read Archived Version'; return true\" onmouseout =\"window.status=''\" href=redirect.cgi?function=cache&num=$keep target=newwindow><font color=white>Read Archived Version</a></b></td>";
}
print "</tr></table>";

if ($INPUT{'c'} ne "3") {
	print "<li><a $target href=\"http://www.middleeast.org/\"><font color=$white>MER Homepage</font></a>";
	print "<li><a $target href=\"http://www.middleeast.org/mertv.htm\"><font color=$white>MERTV</font></a>";
	print "<br><br>";
}

if ($INPUT{'window'} eq "1") {
	$target ="newBrowserWindow";
	$url ="javascript:window.close()";
	print "<li><a href=$url><font color=$white>Close this frame</font></a>";
}

print "</body></html>";

}



sub mer {

print <<endofhtml;
<body topmargin=5 bgcolor="#446699" leftmargin=0 rightmargin=0 bottommargin=0 marginheight=0 marginwidth=0>
<center><font size=2 face=Verdana color=white>You are viewing a selection from <a href='http://www.middleeast.org' target='_top'><font color=white>MiddleEast.org</a></center>
</body>
endofhtml
;

		print "<script language=javascript>";
		print "var popupURL = \"redirect.cgi?window=1&type=$INPUT{'type'}&archive=$arcnum&function=popup&c=$INPUT{'c'}&num=$INPUT{'num'}&background=white\";" if ($INPUT{'c'} eq "3");
		print "var popupURL = \"redirect.cgi?window=1&type=$INPUT{'type'}&archive=$arcnum&function=popup&c=$INPUT{'c'}&num=$INPUT{'num'}\";" if ($INPUT{'c'} ne "3");
		print "var popup = window.open(popupURL,\"Popup2\",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=400,height=300');";
		print "if( navigator.appName.substring(0,8) == \"Netscape\" )";
		print "{";
		print "popup.location = popupURL;";
		print "}";
		print "</script>\n";
}


sub archived {

open(IN, "archives/counter.txt");
@indata=<IN>;
close(IN);

if (($INPUT{'archive'} eq $indata[0]) || ($INPUT{'archive'} eq "")) {
	open(IN, "news.txt");
	@indata=<IN>;
	close(IN);
}
else {
	$filename = "archives/" . $INPUT{'archive'} . ".txt";
	open(IN, "$filename");
	@indata=<IN>;
	close(IN);
}

$record = @indata-$INPUT{'num'};
($title, $url, $desc, $head) = split(/\|/, $indata[$record]);

}


sub middle {

&archived;

$bgcolor = "#42527B";
$bgcolor = "white" if ($INPUT{'background'} eq "white");

print "<html><Head><title>MER</title></heaD><body bgcolor=$bgcolor topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0 marginheight=0 marginwidth=0>";

$bg = "#42527B" if ($INPUT{'background'} ne "white");
$bg = "#FFFFFF" if ($INPUT{'background'} eq "white");
$white = "white" if ($INPUT{'background'} ne "white");
$white = "black" if ($INPUT{'background'} eq "white");

print "<table width=100% cellpadding=9><Tr><td>";

$bgcolor = "white";
$bgcolor = "black" if ($INPUT{'background'} eq "white");

print "<font size=2 face=Verdana color=$bgcolor>";

if ($notpop == 1) {
	print "<table width=100%><tr><td><a $target href=http://www.mereport.com><font color=$bgcolor size=1 face=Verdana><b>Back to MiddleEast.org</b></a></td>" if ($INPUT{'c'} eq "2");
	print "<table width=100%><tr><td><a $target href=http://www.washreport.com><font color=$bgcolor size=1 face=Verdana><b>Back to WASH REPORT</b></a></td>" if ($INPUT{'c'} eq "3");

	($junk, $keep) = split(/<!-- /, $desc);
	($keep, $junk) = split(/ -->/, $keep);
	$filename = "redirect/" . $keep . ".txt";
	if (-e $filename) {
		print "<td align=right><font size=1 face=Verdana><b><a href=redirect.cgi?function=cache&num=$keep target=newwindow><font color=$bgcolor>Read Archived Version</a></b></td>";
	}
	print "</tr></table>";
}

if (!($desc =~ /<b off><\/b>/)) {
	if (($INPUT{'c'} eq "2") || ($INPUT{'c'} eq "3")) { print "<b><font size=2 color=$bgcolor>Comment</font></b><hr>"; } 
	else { print "<b><font size=2 color=$bgcolor>MER Comment</font></b><hr>"; }
	print "<font size=2 color=$bgcolor>$desc</font><br><br>";
}
print "</td></tr></table><br><br>";

}

sub right {

print "<html><body bgcolor=#42527B topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0 marginheight=0 marginwidth=0>";
print "<font size=2 face=Verdana color=$white>";

open(IN, "news.txt");
@indata=<IN>;
close(IN);

print <<endofhtml;

<style type="text/css">
<!--
A { text-decoration:none }
-->
</STYLE>

<!-- Start Dynamic scroller -->
<table width="200" cellspacing="0" border="1" bgcolor="#FFFFFF" bordercolor="#008080" bordercolorlight="#FFFFFF">
		<tr>
		<td width="25%" align="center" valign="bottom">
		<a href="Javascript:scrolldown()"><b><font face="Verdana" size="2" color=black>Down</font></b></a>
		</td>
		<td width="25%" align="center" valign="bottom">
		<a href="Javascript:scrollup()"><b><font face="Verdana" size="2" color=black>Up</font></b></a>
		</td>
		<td width="25%" align="center" valign="bottom">
		<a href="Javascript:stopall()"><b><font face="Verdana" size="2" color=black>Stop</font></b></a>
		</td>
		<td width="25%" align="center" valign="bottom">
		<a href="Javascript:shifttotop()"><b><font face="Verdana" size="2" color=black>Top</font></b></a>
		</td>
		</tr>	
</table>
<script language="JavaScript1.2">
iens6=document.all||document.getElementById
ns4=document.layers

<!-- Lower is faster -->
var scrollspeed = 60

<!-- Container setup -->
if (iens6){
document.write('<div id="container" style="position:relative;width:200;height:100px;overflow:hidden;border:1px ridge white">')
document.write('<div id="content" style="position:absolute;width:200;left:0;top:0">')
}
</script>
<ilayer name="nscontainer" width=200 height=100 clip="0,0,200,100">
<layer name="nscontent" width=200 height=100>

<!--INSERT CONTENT HERE-->
<!--<table bgcolor="#000000"><tr><Td>-->
<table><tr><Td>
endofhtml
;


$total=@indata;
$stop = $total-20;
$cnt2=0;
foreach $line (@indata) {
	if ($line ne "") {
		($title, $url, $desc, $head, $preview, $date) = split(/\|/, $line);
		if (($head ne "off") && ($head ne "hidden")) {
			$url =~ s/\?/QUESTIONMARK/g;
			$url =~ s/\&/AND/g;
			$url =~ s/\=/EQUAL/g;
			if ($INPUT{'c'} eq "3") {
				if (!($preview =~ /wash/)) {
					print "<b><font size=1 face=Verdana><a $target onmouseover=\"window.status=''; return true\" onmouseout =\"window.status=''\" href='http://www.middleeast.org/launch/redirect.cgi?c=3&num=$total'><font color=white>$title</font></a></b><br><br>\n";
				}
			} elsif ($INPUT{'c'} eq "2") {
				if (!($preview =~ /me/)) {
					print "<b><font size=1 face=Verdana><a $target href='http://www.middleeast.org/launch/redirect.cgi?c=2&num=$total'><font color=white>$title</font></a></b><br><br>\n";
				}
			} else {
				print "<b><font size=1 face=Verdana><a $target href='http://www.middleeast.org/launch/redirect.cgi?c=$INPUT{'c'}&num=$total'><font color=white>$title</font></a></b><br><br>\n";
			}
			last if ($cnt2 == 40);
			$cnt2++;
		}
		$total--;
	}
}

print <<endofhtml;
</td></tr></table>
<!--END CONTENT-->
</layer></ilayer>

<script language="JavaScript1.2">
var step=1
if (iens6){
	document.write('</div></div>')
	var contentobj=document.getElementById? document.getElementById("content") : document.all.content
	var contentheight=contentobj.offsetHeight
	if (document.getElementById&&!document.all){
   		step=5
	}
}
else if(ns4){
	var contentobj=document.nscontainer.document.nscontent
	var contentheight=contentobj.clip.height
}
<!-- scroll down function -->
function scrolldown() {
	if(window.scrolltimerup){
		clearTimeout(scrolltimerup)
	}
	if(iens6&&parseInt(contentobj.style.top)>=(contentheight*(-1)+100)){
		contentobj.style.top=parseInt(contentobj.style.top)-step
	}else if(ns4&&contentobj.top>=(contentheight*(-1)+100)){
		contentobj.top-=step
	}
	scrolltimerdown = setTimeout("scrolldown()",scrollspeed)
}
<!-- scroll up function -->
function scrollup() {
	if(window.scrolltimerdown){
		clearTimeout(scrolltimerdown)
	}
	if(iens6&&parseInt(contentobj.style.top)<=0){
		contentobj.style.top=parseInt(contentobj.style.top)+step
	}else if(ns4&&contentobj.top<=0){ 
		contentobj.top+=step
	}
	scrolltimerup = setTimeout("scrollup()",scrollspeed)	
}
<!-- function to stop all scroll timers -->
function stopall() {
	if(window.scrolltimerup){
		clearTimeout(scrolltimerup)
	}
	if(window.scrolltimerdown){
		clearTimeout(scrolltimerdown)
	}
}
<!-- function that shifts the object to top -->
function shifttotop(){
	stopall()
	if (iens6) {
		contentobj.style.top=0
	}else{
		contentobj.top=0
	}
}

function getcontent_height(){
	if (iens6)
	contentheight=contentobj.offsetHeight
}
window.onload=getcontent_height
scrolldown();
</script>
<!-- End of Dynamic scroller -->


endofhtml
;


}


sub normal {


$arcnum = $INPUT{'a'} if ($INPUT{'a'} ne "");
$arcnum = $INPUT{'archive'} if ($INPUT{'archive'} ne "");

 $theminute = (localtime(time()))[1];
 $thehour = (localtime(time()))[2];
 $theday = (localtime(time()))[3];
 $thedayyear = (localtime(time()))[7];
 $themonth = (localtime(time()))[4];
 $themonth++;
 $theyear = (localtime(time()))[5];
 $theyear = $theyear + 1900;

if ($INPUT{'t'} eq "p") {
	open(OUT, ">>../promote/h_logs.txt");
	print OUT "$ENV{'HTTP_REFERER'}|$themonth $theday $theyear|$INPUT{'archive'}|$INPUT{'num'}\n";
	close(OUT);
}
if ($INPUT{'t'} eq "f") {
	open(OUT, ">>../promote/f_logs.txt");
	print OUT "$ENV{'HTTP_REFERER'}|$themonth $theday $theyear|$INPUT{'archive'}|$INPUT{'num'}\n";
	close(OUT);
}

open(IN, "archives/counter.txt");
@indata=<IN>;
close(IN);

if (($arcnum eq $indata[0]) || ($arcnum eq "")) {
open(IN, "news.txt");
@indata=<IN>;
close(IN);
}
else {
$filename = "archives/" . $arcnum . ".txt";
open(IN, "$filename");
@indata=<IN>;
close(IN);

}

$record = @indata-$INPUT{'num'};
($title, $url, $desc, $head) = split(/\|/, $indata[$record]);

($junk, $showtitle) = split(/-->/, $title);
$showtitle = $title if ($showtitle eq "");
$url =~ s/QUESTIONMARK/\?/g;
$url =~ s/AND/\&/g;
$url =~ s/EQUAL/=/g;
$INPUT{'location'} =~ s/QUESTIONMARK/\?/g;
$INPUT{'location'} =~ s/AND/\&/g;
$INPUT{'location'} =~ s/EQUAL/=/g;

open(IN, "framebusters.txt");
@in = <IN>;
close(IN);
$framebuster=0;
foreach $line (@in) {
	chop $line;
	if ($url =~ /$line/) {
		$framebuster=1;
	}
}

if ($framebuster == 0) {
	if ($url eq "") {
		print "<html><head><title>MER - $showtitle</title></head>";
		print "<FRAMESET cols=\"*\" rows=\"*,130\" frameborder=\"NO\" border=\"0\" framespacing=\"0\">";
		print "<FRAME marginwidth=0 marginheight=0 scrolling=\"YES\" noresize src=\"redirect.cgi?type=$INPUT{'type'}&archive=$arcnum&function=middle&c=$INPUT{'c'}&num=$INPUT{'num'}&background=white\" name=\"middle\">\n";
		#print "<FRAMESET cols=\"200,*,220\" rows=\"*\" frameborder=\"NO\" border=\"0\" framespacing=\"0\" scrolling=no>";
		print "<FRAMESET cols=\"200,*\" rows=\"*\" frameborder=\"NO\" border=\"0\" framespacing=\"0\" scrolling=no>";
		print "<FRAME marginwidth=0 marginheight=0 scrolling=\"YES\" noresize src=\"redirect.cgi?type=$INPUT{'type'}&archive=$arcnum&function=left&c=$INPUT{'c'}&num=$INPUT{'num'}&empty=no&background=black\" name=\"left\" scrolling=no>\n";
		print "<FRAME marginwidth=0 marginheight=0 scrolling=\"YES\" noresize src=\"blueblank.htm\" name=\"middle\">\n";
		#print "<FRAME marginwidth=0 marginheight=0 scrolling=\"YES\" noresize src=\"redirect.cgi?type=$INPUT{'type'}&archive=$arcnum&function=right&c=$INPUT{'c'}&num=$INPUT{'num'}&empty=no&background=black\" name=\"right\" scrolling=no>\n";
		print "</FRAMESET>";
		print "</FRAMESET>";
		print "</html>";
	}
	else {
		print "<html><head><title>MER - $showtitle</title></head>";
		print "<FRAMESET cols=\"*\" rows=\"25,*\" frameborder=\"NO\" border=\"0\" framespacing=\"0\">";
		print "<FRAME marginwidth=0 marginheight=0 scrolling=\"NO\" noresize src=\"redirect.cgi?type=$INPUT{'type'}&archive=$arcnum&function=mer&c=$INPUT{'c'}&num=$INPUT{'num'}&empty=no&background=white\" name=\"left\" scrolling=no>\n";
		print "<FRAME marginwidth=0 marginheight=0 src=\"$url\" name=\"main\">\n";
		print "</FRAMESET>";
		print "</html>";
	}
}
else {
		print "<html><head><title>Redirect</title>\n";
		print "<META http-equiv=\"refresh\" content=\"0; URL=$url\">\n</head>\n";
$test = $desc;
$test =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs;
if (length($test) > 5)
{
		print "<script language=javascript>";
		print "var popupURL = \"redirect.cgi?window=1&type=$INPUT{'type'}&archive=$arcnum&function=popup&c=$INPUT{'c'}&num=$INPUT{'num'}&background=white\";" if ($INPUT{'c'} eq "3");
		print "var popupURL = \"redirect.cgi?window=1&type=$INPUT{'type'}&archive=$arcnum&function=popup&c=$INPUT{'c'}&num=$INPUT{'num'}\";" if ($INPUT{'c'} ne "3");
		print "var popup = window.open(popupURL,\"Popup2\",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=400,height=300');";
		print "if( navigator.appName.substring(0,8) == \"Netscape\" )";
		print "{";
		print "popup.location = popupURL;";
		print "}";
		print "</script>\n";
}
		print "</html>";
}


print <<endofhtml;

<NOFRAMES>
<body bgcolor="#FFFFFF">
$desc
</body>
</NOFRAMES>

</html>

endofhtml
;


}