package Apache::MyFilter;

# Apache::MyFilter
# mod_perl content filtering
#
# Created by Wim Kerkhoff
# July 19, 2000
# wim@netmaster.com

use strict;
use Apache;
use Apache::Constants qw(:common M_GET);
use Apache::Proxy ();

sub handler {
	my $r = shift;

	# only proceed if this is a proxy request
	return DECLINED unless $r->proxyreq;

	my $host = $r->connection->remote_ip() ;
	my $url = $r->uri;

	my $allowed = 1;

	if ($allowed) {
		proxy_handler($r, $url);
	}
	elsif ($allowed == 0) {
		PrintNotAllowed($r, "Sorry, you can't view this page", $url);
	}
	else {
		return FORBIDDEN;
	}
}

sub proxy_handler {
	my $r = shift;

	# let mod_proxy do the proxying for us...
	my $proxyuri = $r->uri;
	
	my $status = Apache::Proxy->pass($r, $proxyuri);

	my $uri = $proxyuri;

	my $footer =<<END;
<p align=right>
<font size="1" face="arial,helvetica">
<b>
Proxying provided by Apache::MyFilter;
</b>
</font>
</p>
END
	$r->print($footer) if ($uri !~ /gif$|png$|jpeg$|jpg$|bmp$|gz|txt/i);
	return DONE;
}

sub PrintNotAllowed {
	my $r = shift;
	my $message = shift;
	my $uri = shift;

	$r->send_cgi_header("Content-Type: text/html\n\n");
	
	$r->print ( <<END );
<head>
	<title>Not Allowed</title>
</head>

<body bgcolor="white">

<font face="arial,helvetica">
<table width=100% height=100% bgcolor="white" border=0><tr><td valign=middle align=center>
<table width=500 height=200 bgcolor="white" border=0><tr><td>

	<b>$message</b>

</td></tr></table>
</td></tr></table>
</body></html>
END
	return DONE;
}

1;

__END__
