Rev 539 Rev 543
1 <?php 1 <?php
2   2  
3 // Direct Link is technology used for permanent link into deep place of the web. 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 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 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 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 7 // are wrappers only. We can then look which language was used and generate
8 // the right language version of link or selection. 8 // the right language version of link or selection.
9 // 9 //
10 // For example www.mlab.cz?xxx 10 // For example www.mlab.cz?xxx
11 // 11 //
12 // If there is invalid keyword (xxx in the example above) the script generates 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 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 14 // (found in a list) then the page is automatically redirected to the right
15 // place. We try to use user's prefered language. 15 // place. We try to use user's prefered language.
16 // 16 //
17 // In the file DirectLink.inc there is list of keywords and links to go. 17 // In the file DirectLink.inc there is list of keywords and links to go.
18 // 18 //
19 // Written by (c)miho2007 for www.mlab.cz 19 // Written by (c)miho2007 for www.mlab.cz
20   20  
21   21  
22 // Clear variables 22 // Clear variables
23 unset($Redirect); 23 unset($Redirect);
24 unset($RedirectUpper); 24 unset($RedirectUpper);
25 unset($RedirectToPage); 25 unset($RedirectToPage);
26 unset($Lang); 26 unset($Lang);
27   27  
28 // Include redirect definitions (as an array) 28 // Include redirect definitions (as an array)
29 include_once("DirectLink.inc"); 29 include_once("DirectLink.inc");
30   30  
31 // Make all keywords upcase 31 // Make all keywords upcase
32 foreach($Redirect as $Key => $Value) 32 foreach($Redirect as $Key => $Value)
33 { 33 {
34 $RedirectUpper[strtoupper($Key)]=$Value; 34 $RedirectUpper[strtoupper($Key)]=$Value;
35 } 35 }
36   36  
37 // Process language (extract lang from the name of the script) 37 // Process language (extract lang from the name of the script)
38 $Lang=$_SERVER[SCRIPT_FILENAME]; 38 $Lang=$_SERVER[SCRIPT_FILENAME];
39 $Lang=preg_replace("/(?:.+)\.(.+)\.(.*)/i","$1",$Lang); 39 $Lang=preg_replace("/(?:.+)\.(.+)\.(.*)/i","$1",$Lang);
40 if ($Lang==$_SERVER[SCRIPT_FILENAME]) $Lang=""; 40 if ($Lang==$_SERVER[SCRIPT_FILENAME]) $Lang="";
41   41  
42 // Find redirect 42 // Find redirect
43 $RedirectToPage=$RedirectUpper[strtoupper(urldecode($_SERVER[QUERY_STRING]))]; 43 $RedirectToPage=$RedirectUpper[strtoupper(urldecode($_SERVER[QUERY_STRING]))];
44   44  
45 // If not found -> list all posibilities 45 // If not found -> list all posibilities
46 if ($RedirectToPage=="") 46 if ($RedirectToPage=="")
47 { 47 {
48 // Template File Name 48 // Template File Name
49 if ($Lang=="") 49 if ($Lang=="")
50 $Lang="en"; 50 $Lang="en";
51 $FileName="DirectLink.$Lang.tmpl"; 51 $FileName="DirectLink.$Lang.tmpl";
52 52
53 // Read Template 53 // Read Template
54 $Template=@file($FileName); 54 $Template=@file($FileName);
55   55  
56 // Trim Lines 56 // Trim Lines
57 if ($Template<>FALSE) 57 if ($Template<>FALSE)
58 { 58 {
59 foreach($Template as $Key => $Line) 59 foreach($Template as $Key => $Line)
60 { 60 {
61 $Template[$Key]=rtrim($Line); 61 $Template[$Key]=rtrim($Line);
62 } 62 }
63 $Template=implode("\n",$Template); 63 $Template=implode("\n",$Template);
64 } 64 }
65   65  
66 // Read parameters from Template 66 // Read parameters from Template
67 // <<DIRECTLINK Rychlý odkaz>> 67 // <<DIRECTLINK Rychlý odkaz>>
68 $TextDL=""; 68 $TextDL="";
69 if (preg_match("/.*<<DIRECTLINK +(.*)>>/i",$Template,$Matches)) 69 if (preg_match("/.*<<DIRECTLINK +(.*)>>/i",$Template,$Matches))
70 { 70 {
71 $TextDL=$Matches[1]; 71 $TextDL=$Matches[1];
72 unset($Matches); 72 unset($Matches);
73 } 73 }
74 $Template=preg_replace("/.*<<DIRECTLINK +.*>>.*\n/im","",$Template); 74 $Template=preg_replace("/.*<<DIRECTLINK +.*>>.*\n/im","",$Template);
75   75  
76 // <<REDIRECTION Přesměrování>> 76 // <<REDIRECTION Přesměrování>>
77 $TextREDIR=""; 77 $TextREDIR="";
78 if (preg_match("/.*<<REDIRECTION +(.*)>>/i",$Template,$Matches)) 78 if (preg_match("/.*<<REDIRECTION +(.*)>>/i",$Template,$Matches))
79 { 79 {
80 $TextREDIR=$Matches[1]; 80 $TextREDIR=$Matches[1];
81 unset($Matches); 81 unset($Matches);
82 } 82 }
83 $Template=preg_replace("/.*<<REDIRECTION +.*>>.*\n/im","",$Template); 83 $Template=preg_replace("/.*<<REDIRECTION +.*>>.*\n/im","",$Template);
84   84  
85 // Find <<TABLE>> 85 // Find <<TABLE>>
86 $Indent=""; 86 $Indent="";
87 if (preg_match("/( *)(<<TABLE>>)/i",$Template,$Matches)) 87 if (preg_match("/( *)(<<TABLE>>)/i",$Template,$Matches))
88 { 88 {
89 $Indent=$Matches[1]; 89 $Indent=$Matches[1];
90 unset($Matches); 90 unset($Matches);
91 } 91 }
92   92  
93 // Generate List (table) 93 // Generate List (table)
94 $Table = "<table>\n"; 94 $Table = "<table>\n";
95 $Table.=$Indent." <tr>\n"; 95 $Table.=$Indent." <tr>\n";
96 $Table.=$Indent." <th> $TextDL </th>\n"; 96 $Table.=$Indent." <th> $TextDL </th>\n";
97 $Table.=$Indent." <th> $TextREDIR </th>\n"; 97 $Table.=$Indent." <th> $TextREDIR </th>\n";
98 $Table.=$Indent." </tr>\n"; 98 $Table.=$Indent." </tr>\n";
99 foreach($Redirect as $Key => $Value) 99 foreach($Redirect as $Key => $Value)
100 { 100 {
101 $Path=$Value; 101 $Path=$Value;
102 if ($Lang<>"") 102 if ($Lang<>"")
103 if (file_exists($_SERVER["DOCUMENT_ROOT"].$Path.".".$Lang.".html")) 103 if (file_exists($_SERVER["DOCUMENT_ROOT"].$Path.".".$Lang.".html"))
104 $Path.=".".$Lang.".html"; 104 $Path.=".".$Lang.".html";
105 $Table.=$Indent." <tr>\n"; 105 $Table.=$Indent." <tr>\n";
106 $Table.=$Indent." <td> <a href=\"$Path\">$Key</a></td>\n"; 106 $Table.=$Indent." <td> <a href=\"$Path\">$Key</a></td>\n";
107 $Table.=$Indent." <td> $Value </td>\n"; 107 $Table.=$Indent." <td> $Value </td>\n";
108 $Table.=$Indent." </tr>\n"; 108 $Table.=$Indent." </tr>\n";
109 } 109 }
110 $Table.=$Indent."</table>\n"; 110 $Table.=$Indent."</table>\n";
111 111
112 // Put it into Template 112 // Put it into Template
113 if ($Template<>"") 113 if ($Template<>"")
114 print str_ireplace("<<TABLE>>", $Table, $Template); 114 print str_ireplace("<<TABLE>>", $Table, $Template);
115 else 115 else
116 print $Table; // We have No Template 116 print $Table; // We have No Template
117 117
118 // Finished 118 // Finished
119 return 0; 119 return 0;
120 } 120 }
121 else 121 else
122 { 122 {
123 // We have redirection 123 // We have redirection
124 if ($Lang<>"") 124 if ($Lang<>"")
125 if (file_exists($_SERVER["DOCUMENT_ROOT"].$RedirectToPage.".".$Lang.".html")) 125 if (file_exists($_SERVER["DOCUMENT_ROOT"].$RedirectToPage.".".$Lang.".html"))
126 $RedirectToPage.=".".$Lang.".html"; 126 $RedirectToPage.=".".$Lang.".html";
127 } 127 }
128   128  
129 // Redirect page 129 // Redirect page
130 header("HTTP/1.1 301 Moved Permanently"); 130 header("HTTP/1.1 301 Moved Permanently");
131 header("Location: ".$RedirectToPage); 131 header("Location: ".$RedirectToPage);
132 header("Connection: close"); 132 header("Connection: close");
133   133  
134 ?> 134 ?>