<?php

// Direct Link is technology used for permanent link into deep place of the web.
// This script is opened when mod_rewrite gets url with no page but with some
// parameter (query string, separated by ?). The web is trying to find
// script with the best language (for example DirectLink.en.php). Those scripts
// are wrappers only. We can then look which language was used and generate
// the right language version of link or selection.
//
//    For example www.mlab.cz?xxx
//
// If there is invalid keyword (xxx in the example above) the script generates
// selecteion HTML page (with table of posibilities). If the keyword is valid
// (found in a list) then the page is automatically redirected to the right
// place. We try to use user's prefered language.
//
// In the file DirectLink.inc there is list of keywords and links to go.
//
// Written by (c)miho2007 for www.mlab.cz


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

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

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

// Process language (extract lang from the name of the script)
$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=="")
{
  // Template 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);
        }

  // Read parameters from Template
  // <<DIRECTLINK Rychlý odkaz>>
  $TextDL="";
  if (preg_match("/.*<<DIRECTLINK +(.*)>>/i",$Template,$Matches))
  {
    $TextDL=$Matches[1];
    unset($Matches);
  }
  $Template=preg_replace("/.*<<DIRECTLINK +.*>>.*\n/im","",$Template);

  // <<REDIRECTION Přesměrování>>
  $TextREDIR="";
  if (preg_match("/.*<<REDIRECTION +(.*)>>/i",$Template,$Matches))
  {
    $TextREDIR=$Matches[1];
    unset($Matches);
  }
  $Template=preg_replace("/.*<<REDIRECTION +.*>>.*\n/im","",$Template);

  // Find <<TABLE>>
  $Indent="";
  if (preg_match("/( *)(<<TABLE>>)/i",$Template,$Matches))
  {
    $Indent=$Matches[1];
    unset($Matches);
  }

  // Generate List (table)
        $Table =        "<table>\n";
        $Table.=$Indent."  <tr>\n";
        $Table.=$Indent."    <th> $TextDL </th>\n";
        $Table.=$Indent."    <th> $TextREDIR </th>\n";
        $Table.=$Indent."  </tr>\n";
        foreach($Redirect as $Key => $Value)
        {
            $Path=$Value;
            if ($Lang<>"")
            if (file_exists($_SERVER["DOCUMENT_ROOT"].$Path.".".$Lang.".html"))
                    $Path.=".".$Lang.".html";
            $Table.=$Indent."  <tr>\n";
            $Table.=$Indent."    <td> <a href=\"$Path\">$Key</a></td>\n";
            $Table.=$Indent."    <td> $Value </td>\n";
            $Table.=$Indent."  </tr>\n";
        }
        $Table.=$Indent."</table>\n";
        
        // Put it into Template
        if ($Template<>"")
            print str_ireplace("<<TABLE>>", $Table, $Template);
        else
      print $Table; // We have No Template
            
        // Finished
        return 0;
}
else
{
  // We have redirection
        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");

?>