# -------------------------------------------------------------------------------------
# flo::plugin::Redirect
# -------------------------------------------------------------------------------------
#
#       Author : Jean-Michel Hiver (jhiver@cisedi.com).
#    Copyright : (c) MKDoc Holdings Ltd, 2001
# 
#      Unauthorized modification, use, reuse, distribution or redistribution
#      of this module is stricly forbidden
#
#    Description:
#
#      A plugin that is used to redirect HTTP requests that ask for a document
#      which has been moved to another location
#
# -------------------------------------------------------------------------------------
package flo::plugin::Redirect;
use lib::Exception;
use flo::Standard;
use lib::sql::Table;
use lib::cgi::CGI;
use strict;


##
# $class->main;
# -------------
#   Redirects the HTTP query, if needed
##
sub main
{
    my $class = shift;
    my $cgi = cgi;
    
    # if the current url has no trailing slash, then
    # we should perform a redirection
    my $raw_path_info = $ENV{PATH_INFO} || "";
    if ($raw_path_info !~ /\/$/)
    {
	my $old_path = quotemeta ($raw_path_info);
	my $new_path = $raw_path_info . '/';
	$cgi->path_info ($new_path);
	print $cgi->redirect ($cgi->self_url);
	return 'TERMINATE';
    }
    
    # if there are any uppercase characters, we need to lowercase
    # the URI and redirect
    $raw_path_info = $ENV{PATH_INFO} || "";
    if ($raw_path_info =~ /[A-Z]/)
    {
	$cgi->path_info (lc ($cgi->path_info));
	print $cgi->redirect ($cgi->self_url);
	return 'TERMINATE';
    }
    
    # if there is a defined redirect, redirects the user
    if (path_info)
    {
	my $redirect_table = table ('Redirect');
	my $redirect = $redirect_table->get ( Old_Path => path_info );
	if (defined $redirect)
	{
	    $cgi->path_info ($redirect->{New_Path});
	    print $cgi->redirect ($cgi->self_url);
	    return 'TERMINATE';
	}
    }
}


1;
