#!/usr/bin/perl
use lib '/home/erealms/ethereal/mgmt/perl';

################################################################################
# Created       : Martin Foster
# Modified      : 14-Dec-2006
################################################################################
#
# Webring - Script part of Ethereal Realms, designed to link up hybrid and
#           publicly listed realms.           
# Copyright (C) 2000-2006  Martin Foster
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Author of this script can be contacted at the following:
#       E-Mail  : martin@ethereal-realms.org
#       Address : 3-3456 Wolfe Cres
#                 Halifax, Nova Scotia
#                 B3L 3S2
#
#################################################################################

use CGI;							# Common gateway interface
use CGI::Carp qw(fatalsToBrowser);				# CGI Error logs
use strict;							# Strict variable enforcement

use Ethereal::Database;						# Database handler
use Ethereal::Param;						# Parameter control

#################################################################################
# Gobal override
#################################################################################
$CGI::POST_MAX=1024 * 50;					# Maximum posts
$CGI::DISABLE_UPLOADS = 1;					# Disable uploads

################################################################################
# Data Members
################################################################################
my $cgi;							# Common gateway interface handle
my $database;							# Database handle

my $sname;							# Simply the script name
my $sparam;							# Scripted parameter string

my %sparam;							# Scripted parameters

################################################################################
# Program Area
################################################################################

	# Initial handles
	$cgi      = new CGI;
	$database = new Ethereal::Database();

	# Connect and fetch
	$database->Connect($cgi);


	# Pull script name
	$sname = $cgi->url(-full=>1);
	$sparam = $cgi->url(-path_info=>1);

	# Determine need
	# Escape
	$sname = quotemeta($sname);

	# Needed addition
	$sparam = ($sparam =~ /\/$/) ? $sparam : "$sparam/";

	# Truncate
	$sparam =~ s/^$sname\///;

	# Retrieve parameters
	($sparam{'ROOM'},
	 $sparam{'MOVE'}) = split(/\//, $sparam);


	# Need to redirect
	if ((defined($sparam{'ROOM'}))
	 && (defined($sparam{'MOVE'})))
	{
		# Unescape
		$sparam{'ROOM'} = $cgi->unescape($sparam{'ROOM'});

		# Call subroutine
		Redirect($database, \%sparam);
	}
	else
	{
		# Redirect to safe place
		print $cgi->redirect($database->{'SYS'}{'LocScriptHub'});
	}



################################################################################
# Sub-Routines
################################################################################

#####################
# Redirect
#
# This will handle the information gathering, determine where the user currently
# is and send him off to a brave new world.

sub Redirect
{
	#####################
	# Data members

	my $database = shift;					# Database handle
	my $sparam   = shift;					# Uncovered parameters

	my $res;						# Results page
	my $statement;						# Statement handle

	my $count;						# Current position
	my $pos;						# Set position
	my $size;						# Returned entries

	my %val;						# Referencing sanity


	#####################
	# Program area

	# Sanity
	%val = (
		RealmName	=> 0,
		RealmHomepage	=> 1
	);


	# Retrieve list
	# Prepare and execute
	$database->Pull(\$statement, "SELECT
		 RealmName     AS \"RealmName\",
		 RealmHomepage AS \"RealmHomepage\"
		FROM Realm
		WHERE RealmHomepage IS NOT NULL
		ORDER BY RealmName");

	# Pull results
	if ($res = $statement->fetchall_arrayref())
	{
		# Determine size
		$size = @{$res};

		# Loop and search
		for ($count=0; $count < $size; $count++)
		{
			# Bypass misses
			next if ($res->[$count]->[$val{'RealmName'}] !~ /$sparam->{'ROOM'}/i);


			# Match found
			# Next
			if ($sparam->{'MOVE'} =~ /NEXT/i)
			{
				# Determine position
				$pos = (($count+1) < $size) ? $count + 1 : 0;
			}

			# Previous
			elsif ($sparam->{'MOVE'} =~ /PREV/i)
			{
				# Determine position
				$pos = (($count-1) >= 0) ? $count - 1 : $size - 1;
			}

			# Random
			elsif ($sparam->{'MOVE'} =~ /RAND/i)
			{
				# Legacy random 
				srand();

				# Randomly figure it out
				$pos = int(rand() * $size);
			}


			# Improper commands
			else
			{
				# Redirect
				print $cgi->redirect($database->{'SYS'}{'LocScriptHub'});

				# Premature exit
				last;
			}


			# Redirect to proper location
			print $cgi->redirect($res->[$pos]->[$val{'RealmHomepage'}]);

			# Premature exit
			last;
		}


		# End query
		$statement->finish();


		# Return
		return 1;
	}


	# Redirect
	print $cgi->redirect($database->{'SYS'}{'LocScriptHub'});

	# Retunr
	return 0;
}
