<?php

// Process supplied parameters as direct link (link to some deep place
// in the web structure).
//
// This script is usualy called via mod_rewrite when user enter
// no page with some parameter.
//
// (c)miho2007 / www.mlab.cz


// Clear variables
unset($Redirect);
unset($RedirectUpper);
unset($RedirectToPage);
unset($Lang);

// Include redirect definitions (as an array)
include_once("DirectLink.inc");

// Do all upcase
foreach($Redirect as $Key => $Value)
{
    $RedirectUpper[strtoupper($Key)]=$Value;
}

// Process language
$Lang=$_SERVER[SCRIPT_FILENAME];
$Lang=preg_replace("/(?:.+)\.(.+)\.(.*)/i","$1",$Lang);
if ($Lang==$_SERVER[SCRIPT_FILENAME]) $Lang="";

// Find redirect
$RedirectToPage=$RedirectUpper[strtoupper(urldecode($_SERVER[QUERY_STRING]))];

// If not found -> list all posibilities
if ($RedirectToPage=="") 
    {
	// File Name
	if ($Lang=="")
	    $Lang="en";
	$FileName="DirectLink.$Lang.tmpl";
	// Read Template
	$Template=@file($FileName);
	// Trim Lines
	if ($Template<>FALSE)
	{
	    foreach($Template as $Key => $Line)
	    {
		$Template[$Key]=rtrim($Line);
	    }
	    $Template=implode("\n",$Template);
	}
	// Generate List
	$Table ="";
	$Table.="<table>\n";
	$Table.="  <tr>\n";
	$Table.="    <th> Direct Link </th>\n";
	$Table.="    <th> Redirected to </th>\n";
	$Table.="  </tr>\n";
	foreach($Redirect as $Key => $Value)
	{
	    $Table.="  <tr>\n";
	    $Table.="    <td> $Key </td>\n";
	    $Table.="    <td> $Value </td>\n";
	    $Table.="  </tr>\n";
	}
	$Table.="<table>\n";
	// Put it into Template
	if ($Template<>"")
	    print str_ireplace("<<Table>>", $Table, $Template);
	else
	    print $Table;
	// Finished
	return 0;
    }
else
    {
	if ($Lang<>"")
	if (file_exists($_SERVER["DOCUMENT_ROOT"].$RedirectToPage.".".$Lang.".html"))
	    $RedirectToPage.=".".$Lang.".html";
    }

// Redirect page
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$RedirectToPage);
header("Connection: close");

?>