package Apache::Publicprofile::Redirector;

# by Joel Bernstein - joel@fysh.org - for David Whewell
# no warranty attached

use Apache::Constants qw/:common/;
use strict;
use warnings;
use CGI qw/header path_info internal_redirect path_translated/;

sub handler {
        my $r = shift;  # cgi response object
        my $handlerpage = "http://test.micontacts.com/?page=publicprofile.php";
	my $pathinfo = $r->path_info;	# http://foo.bar.com/THISBITHERE
	my @exceptions = qw/ special_offers something something_else /; # exceptions to the rule - space separated
	
	if ( $pathinfo =~ /\./ ) {
		# then it's a file not a name, so we don't deal with it
		return DECLINED;
	}

	if ( $pathinfo =~ /^\?/ ) {
		# then it's a query on the root page, again let the server deal with it
		return DECLINED;
	}

	if ( $pathinfo eq '/' || $pathinfo eq '' ) {
		# then it's the root page, let the server deal with it
		return DECLINED;
	}
	
	for my $i (@exceptions) {
		if ( $pathinfo eq $i ) {
			# then it's one of the special cases which we don't process
			return DECLINED;
		}
	}
	
	# if we get this far, then we've covered all the bases, so lets redirect!
	
	unless ( -d $pathinfo || -f $pathinfo || -l $pathinfo ) {
		# $pathinfo isn't a local file, directory or symlink
		my $login = $pathinfo;
		$r->internal_redirect($handlerpage+"&"+"login="+$login);
		return OK;
	} else {
		# $pathinfo _is_ a local file, dir or symlink
		# so we can allow the webserver to serve it
		# not sure whether we oughtn't to test specifically for this above
		return DECLINED;
	}

	# and if we get this far, then nothing's matched
	# so we *definitely* want to let the webserver take control back
	# probably so it can 404...
	
	return DECLINED;
}
1;

__END__

