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  
538 miho 66 // Read parameters from Template
537 miho 67 // <<DIRECTLINK Rychlý odkaz>>
538 miho 68 $TextDL="";
69 if (preg_match("/.*<<DIRECTLINK +(.*)>>/i",$Template,$Matches))
70 {
71 $TextDL=$Matches[1];
72 unset($Matches);
73 }
74 $Template=preg_replace("/.*<<DIRECTLINK +.*>>.*\n/im","",$Template);
75  
537 miho 76 // <<REDIRECTION Přesměrování>>
538 miho 77 $TextREDIR="";
78 if (preg_match("/.*<<REDIRECTION +(.*)>>/i",$Template,$Matches))
79 {
80 $TextREDIR=$Matches[1];
81 unset($Matches);
82 }
83 $Template=preg_replace("/.*<<REDIRECTION +.*>>.*\n/im","",$Template);
537 miho 84  
538 miho 85 // Find <<TABLE>>
86 $Indent="";
87 if (preg_match("/( *)(<<TABLE>>)/i",$Template,$Matches))
88 {
89 $Indent=$Matches[1];
90 unset($Matches);
91 }
92  
537 miho 93 // Generate List (table)
538 miho 94 $Table = "<table>\n";
95 $Table.=$Indent." <tr>\n";
539 miho 96 $Table.=$Indent." <th> $TextDL </th>\n";
538 miho 97 $Table.=$Indent." <th> $TextREDIR </th>\n";
98 $Table.=$Indent." </tr>\n";
536 miho 99 foreach($Redirect as $Key => $Value)
100 {
539 miho 101 $Path=$Value;
102 if ($Lang<>"")
103 if (file_exists($_SERVER["DOCUMENT_ROOT"].$Path.".".$Lang.".html"))
104 $Path.=".".$Lang.".html";
538 miho 105 $Table.=$Indent." <tr>\n";
539 miho 106 $Table.=$Indent." <td> <a href=\"$Path\">$Key</a></td>\n";
538 miho 107 $Table.=$Indent." <td> $Value </td>\n";
108 $Table.=$Indent." </tr>\n";
536 miho 109 }
538 miho 110 $Table.=$Indent."</table>\n";
537 miho 111  
536 miho 112 // Put it into Template
113 if ($Template<>"")
537 miho 114 print str_ireplace("<<TABLE>>", $Table, $Template);
536 miho 115 else
537 miho 116 print $Table; // We have No Template
117  
536 miho 118 // Finished
119 return 0;
537 miho 120 }
531 miho 121 else
537 miho 122 {
123 // We have redirection
536 miho 124 if ($Lang<>"")
125 if (file_exists($_SERVER["DOCUMENT_ROOT"].$RedirectToPage.".".$Lang.".html"))
531 miho 126 $RedirectToPage.=".".$Lang.".html";
537 miho 127 }
531 miho 128  
129 // Redirect page
130 header("HTTP/1.1 301 Moved Permanently");
131 header("Location: ".$RedirectToPage);
132 header("Connection: close");
133  
537 miho 134 ?>