Rev Author Line No. Line
531 miho 1 <?php
2  
537 miho 3 // Direct Link is technology used for permanent link into deep place of the web.
4 // This script is opened when mod_rewrite gets url with no page but with some
5 // parameter (query string, separated by ?). The web is trying to find
6 // script with the best language (for example DirectLink.en.php). Those scripts
7 // are wrappers only. We can then look which language was used and generate
8 // the right language version of link or selection.
531 miho 9 //
537 miho 10 // For example www.mlab.cz?xxx
531 miho 11 //
537 miho 12 // If there is invalid keyword (xxx in the example above) the script generates
13 // selecteion HTML page (with table of posibilities). If the keyword is valid
14 // (found in a list) then the page is automatically redirected to the right
15 // place. We try to use user's prefered language.
16 //
17 // In the file DirectLink.inc there is list of keywords and links to go.
18 //
19 // Written by (c)miho2007 for www.mlab.cz
531 miho 20  
21  
22 // Clear variables
23 unset($Redirect);
536 miho 24 unset($RedirectUpper);
531 miho 25 unset($RedirectToPage);
26 unset($Lang);
27  
28 // Include redirect definitions (as an array)
29 include_once("DirectLink.inc");
30  
537 miho 31 // Make all keywords upcase
531 miho 32 foreach($Redirect as $Key => $Value)
33 {
537 miho 34 $RedirectUpper[strtoupper($Key)]=$Value;
531 miho 35 }
36  
537 miho 37 // Process language (extract lang from the name of the script)
531 miho 38 $Lang=$_SERVER[SCRIPT_FILENAME];
39 $Lang=preg_replace("/(?:.+)\.(.+)\.(.*)/i","$1",$Lang);
40 if ($Lang==$_SERVER[SCRIPT_FILENAME]) $Lang="";
41  
42 // Find redirect
536 miho 43 $RedirectToPage=$RedirectUpper[strtoupper(urldecode($_SERVER[QUERY_STRING]))];
531 miho 44  
45 // If not found -> list all posibilities
537 miho 46 if ($RedirectToPage=="")
47 {
48 // Template File Name
49 if ($Lang=="")
50 $Lang="en";
536 miho 51 $FileName="DirectLink.$Lang.tmpl";
537 miho 52  
536 miho 53 // Read Template
54 $Template=@file($FileName);
537 miho 55  
56 // Trim Lines
536 miho 57 if ($Template<>FALSE)
58 {
59 foreach($Template as $Key => $Line)
60 {
537 miho 61 $Template[$Key]=rtrim($Line);
536 miho 62 }
63 $Template=implode("\n",$Template);
64 }
537 miho 65  
66 // Read parametrs from Template
67  
68 // Dodělat tuto část včetně odsazení tabulky
69 // <<DIRECTLINK Rychlý odkaz>>
70 // <<REDIRECTION Přesměrování>>
71 $Template=preg_replace("/.*<<DIRECTLINK +(.*)>>/im","",$Template);
72 $Template=preg_replace("/.*<<REDIRECTION +(.*)>>/im","",$Template);
73  
74 // Generate List (table)
536 miho 75 $Table ="";
76 $Table.="<table>\n";
77 $Table.=" <tr>\n";
78 $Table.=" <th> Direct Link </th>\n";
79 $Table.=" <th> Redirected to </th>\n";
80 $Table.=" </tr>\n";
81 foreach($Redirect as $Key => $Value)
82 {
83 $Table.=" <tr>\n";
84 $Table.=" <td> $Key </td>\n";
85 $Table.=" <td> $Value </td>\n";
86 $Table.=" </tr>\n";
87 }
88 $Table.="<table>\n";
537 miho 89  
536 miho 90 // Put it into Template
91 if ($Template<>"")
537 miho 92 print str_ireplace("<<TABLE>>", $Table, $Template);
536 miho 93 else
537 miho 94 print $Table; // We have No Template
95  
536 miho 96 // Finished
97 return 0;
537 miho 98 }
531 miho 99 else
537 miho 100 {
101 // We have redirection
536 miho 102 if ($Lang<>"")
103 if (file_exists($_SERVER["DOCUMENT_ROOT"].$RedirectToPage.".".$Lang.".html"))
531 miho 104 $RedirectToPage.=".".$Lang.".html";
537 miho 105 }
531 miho 106  
107 // Redirect page
108 header("HTTP/1.1 301 Moved Permanently");
109 header("Location: ".$RedirectToPage);
110 header("Connection: close");
111  
537 miho 112 ?>