manfire/manfire.cgi (Plain text version)

#!/usr/bin/perl
use PerlHP;

<%
	# manfire.cgi by J. Manfire
	#
	# ported to PerlHP by !WAHa.06x36

	use strict;

	our $file;

	$file=escape_html($file);
	my @allfiles=grep { !/^s_/ } glob("*.jpg");
	my $bigimage=($file or $allfiles[0]);
	my $inlineimage="s_$bigimage";
	$inlineimage=$bigimage unless -e $inlineimage;

	open IMAGE,$inlineimage;
	my ($width,$height)=analyze_jpeg(\*IMAGE);
	close IMAGE;

	my $index=GETINDEX();
	my $previmage=$index!=0?$allfiles[$index-1]:undef;
	my $nextimage=$index!=$#allfiles?$allfiles[$index+1]:undef;

	sub GETINDEX
	{
		foreach my $index ( 0 .. $#allfiles ) { return $index if $allfiles[$index] eq $bigimage; }
		return undef;
	}
%>

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<title><%= $bigimage %></title>
<style type="text/css">
body { font-family: verdana, sans-serif; font-size: .7em; }
form { margin: 0px; }
#all { display: table; }
#display a { color: #000; }
#display a:hover { color: #f83; }
#display img { border-width: 1px; }
.navi { width: 100%; display: table; text-align: right;  border: 1px solid #000; margin: 0.5em 0em; }
.prevnext { float:left; padding: 6px; }
.filelist { float:right; padding: 2px; }
.filelist input { background: #fff; border: 1px solid #000;  }
.filelist input:hover { background: #000; color: #fff; border: 1px solid #000;  }
.prevnext a { color: #d62; padding: 4px; text-decoration: none; }
.prevnext a:hover { background: #fec; padding: 3px; border: 1px solid #d62; -moz-border-radius: 4px; }
</style>
</head>

<body><div id="all">

<div id="topnavi" class="navi"><% PRINTNAVI() %></div>

<div id="display">
<a href="<%= $bigimage %>"><img width="<%= $width %>" height="<%= $height %>" src="<%= $inlineimage %>" /></a>
<p>Click image to enlarge.</p>
<% unless($nextimage) { %>
	<p>The photo tour ends here, but you can <a href="<%= "$ENV{SCRIPT_NAME}?file=$allfiles[0]" %>">go back to the beginning</a>.
<% } %>
</div>

<div id="bottomnavi" class="navi"><% PRINTNAVI() %></div>

</div></body></html>

<% sub PRINTNAVI() { %>
	<div class="prevnext">
	<% if($previmage) { print "<a href=\"$ENV{SCRIPT_NAME}?file=$previmage\">&lt; Previous</a>" } else { print "&lt; Previous" } %>
	<% if($nextimage) { print "<a href=\"$ENV{SCRIPT_NAME}?file=$nextimage\">Next &gt;</a>" } else { print "Next &gt;" } %>
	</div>
	<div class="filelist"><form method="get" action="<% print $ENV{SCRIPT_NAME} %>">
	<select name="file"><% for(@allfiles) {
		if($_ eq $file) { print "<option selected=\"selected\" value=\"$_\">$_</option>" }
		else { print "<option value=\"$_\">$_</option>" }
	} %></select>
	<input type="submit" value="Go" />
	</form></div>
<% } %>

<%

sub analyze_jpeg($)
{
	my ($file)=@_;
	my ($buffer);

	read($file,$buffer,2);

	if($buffer eq "\xff\xd8")
	{
		OUTER:
		for(;;)
		{
			for(;;)
			{
				last OUTER unless(read($file,$buffer,1));
				last if($buffer eq "\xff");
			}

			last unless(read($file,$buffer,3)==3);
			my ($mark,$size)=unpack("Cn",$buffer);
			last if($mark==0xda or $mark==0xd9);  # SOS/EOI
			die "Possible virus in image" if($size<2); # MS GDI+ JPEG exploit uses short chunks

			if($mark>=0xc0 and $mark<=0xc2) # SOF0..SOF2 - what the hell are the rest? 
			{
				last unless(read($file,$buffer,5)==5);
				my ($bits,$height,$width)=unpack("Cnn",$buffer);
				seek($file,0,0);

				return($width,$height);
			}

			seek($file,$size-2,1);
		}
	}

	seek($file,0,0);

	return ();
}

%>