| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
function find_match($curlscore,$curcscore,$curgtlang,$langval,$charval, |
|---|
| 71 |
$gtlang) |
|---|
| 72 |
{ |
|---|
| 73 |
if($curlscore < $langval) { |
|---|
| 74 |
$curlscore=$langval; |
|---|
| 75 |
$curcscore=$charval; |
|---|
| 76 |
$curgtlang=$gtlang; |
|---|
| 77 |
} else if ($curlscore == $langval) { |
|---|
| 78 |
if($curcscore < $charval) { |
|---|
| 79 |
$curcscore=$charval; |
|---|
| 80 |
$curgtlang=$gtlang; |
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
return array($curlscore, $curcscore, $curgtlang); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
function al2gt($gettextlangs, $mime) { |
|---|
| 87 |
|
|---|
| 88 |
$acceptLang=(($_SERVER["HTTP_ACCEPT_LANGUAGE"] == '') ? '*' : |
|---|
| 89 |
$_SERVER["HTTP_ACCEPT_LANGUAGE"]); |
|---|
| 90 |
$acceptChar=(($_SERVER["HTTP_ACCEPT_CHARSET"] == '') ? '*' : |
|---|
| 91 |
$_SERVER["HTTP_ACCEPT_CHARSET"]); |
|---|
| 92 |
$alparts=@preg_split("/,/",$acceptLang); |
|---|
| 93 |
$acparts=@preg_split("/,/",$acceptChar); |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
foreach($alparts as $part) { |
|---|
| 97 |
$part=trim($part); |
|---|
| 98 |
if(preg_match("/;/", $part)) { |
|---|
| 99 |
$lang=@preg_split("/;/",$part); |
|---|
| 100 |
$score=@preg_split("/=/",$lang[1]); |
|---|
| 101 |
$alscores[$lang[0]]=$score[1]; |
|---|
| 102 |
} else { |
|---|
| 103 |
$alscores[$part]=1; |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
/* RFC2616: ``If no "*" is present in an Accept-Charset field, then |
|---|
| 110 |
* all character sets not explicitly mentioned get a quality value of |
|---|
| 111 |
* 0, except for ISO-8859-1, which gets a quality value of 1 if not |
|---|
| 112 |
* explicitly mentioned.'' |
|---|
| 113 |
* |
|---|
| 114 |
* Making it 2 for the time being, so that we |
|---|
| 115 |
* can distinguish between "not specified" and "specified as 1" later |
|---|
| 116 |
* on. */ |
|---|
| 117 |
$acscores["ISO-8859-1"]=2; |
|---|
| 118 |
|
|---|
| 119 |
foreach($acparts as $part) { |
|---|
| 120 |
$part=trim($part); |
|---|
| 121 |
if(preg_match("/;/", $part)) { |
|---|
| 122 |
$cs=@preg_split("/;/",$part); |
|---|
| 123 |
$score=@preg_split("/=/",$cs[1]); |
|---|
| 124 |
$acscores[strtoupper($cs[0])]=$score[1]; |
|---|
| 125 |
} else { |
|---|
| 126 |
$acscores[strtoupper($part)]=1; |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
if($acscores["ISO-8859-1"]==2) { |
|---|
| 130 |
$acscores["ISO-8859-1"]=(isset($acscores["*"])?$acscores["*"]:1); |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
* Loop through the available languages/encodings, and pick the one |
|---|
| 135 |
* with the highest score, excluding the ones with a charset the user |
|---|
| 136 |
* did not include. |
|---|
| 137 |
*/ |
|---|
| 138 |
$curlscore=0; |
|---|
| 139 |
$curcscore=0; |
|---|
| 140 |
$curgtlang=NULL; |
|---|
| 141 |
foreach($gettextlangs as $gtlang) { |
|---|
| 142 |
|
|---|
| 143 |
$tmp1=preg_replace("/\_/","-",$gtlang); |
|---|
| 144 |
$tmp2=@preg_split("/\./",$tmp1); |
|---|
| 145 |
$allang=strtolower($tmp2[0]); |
|---|
| 146 |
$gtcs=strtoupper($tmp2[1]); |
|---|
| 147 |
$noct=@preg_split("/-/",$allang); |
|---|
| 148 |
|
|---|
| 149 |
$testvals=array( |
|---|
| 150 |
array($alscores[$allang], $acscores[$gtcs]), |
|---|
| 151 |
array($alscores[$noct[0]], $acscores[$gtcs]), |
|---|
| 152 |
array($alscores[$allang], $acscores["*"]), |
|---|
| 153 |
array($alscores[$noct[0]], $acscores["*"]), |
|---|
| 154 |
array($alscores["*"], $acscores[$gtcs]), |
|---|
| 155 |
array($alscores["*"], $acscores["*"])); |
|---|
| 156 |
|
|---|
| 157 |
$found=FALSE; |
|---|
| 158 |
foreach($testvals as $tval) { |
|---|
| 159 |
if(!$found && isset($tval[0]) && isset($tval[1])) { |
|---|
| 160 |
$arr=find_match($curlscore, $curcscore, $curgtlang, $tval[0], |
|---|
| 161 |
$tval[1], $gtlang); |
|---|
| 162 |
$curlscore=$arr[0]; |
|---|
| 163 |
$curcscore=$arr[1]; |
|---|
| 164 |
$curgtlang=$arr[2]; |
|---|
| 165 |
$found=TRUE; |
|---|
| 166 |
} |
|---|
| 167 |
} |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
* through a "*" qualifier.*/ |
|---|
| 172 |
|
|---|
| 173 |
$gtparts=@preg_split("/\./",$curgtlang); |
|---|
| 174 |
$tmp=strtolower($gtparts[0]); |
|---|
| 175 |
$lang=preg_replace("/\_/", "-", $tmp); |
|---|
| 176 |
$charset=$gtparts[1]; |
|---|
| 177 |
|
|---|
| 178 |
header("Content-Language: $lang"); |
|---|
| 179 |
header("Content-Type: $mime; charset=$charset"); |
|---|
| 180 |
|
|---|
| 181 |
return $curgtlang; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
?> |
|---|
| 185 |
|
|---|