206 lines
6.4 KiB
PHP
206 lines
6.4 KiB
PHP
<?php
|
|
// File name: altlang.inc.php
|
|
// Description: PHP subroutines to obtain the URL of an alternative language version
|
|
// Date: 2004-10-15
|
|
// Author: imacat <imacat@pristine.com.tw>
|
|
// Copyright: Copyright (C) 2004-2008 Pristine Communications
|
|
|
|
// Set the include path
|
|
if (!defined("INCPATH_SET")) {
|
|
require_once dirname(__FILE__) . "/incpath.inc.php";
|
|
}
|
|
// Referenced subroutines
|
|
require_once "monica/cgiemu.inc.php";
|
|
require_once "monica/decform.inc.php";
|
|
require_once "monica/echoform.inc.php";
|
|
require_once "monica/lninfo.inc.php";
|
|
require_once "monica/requri.inc.php";
|
|
require_once "monica/unicode.inc.php";
|
|
|
|
// altlang: Obtain the URL of an alternative language version
|
|
function altlang($lang, $args)
|
|
{
|
|
// Obtain the URLs
|
|
set_altlang_urls($args);
|
|
// Return it
|
|
return $args["altlang"][$lang];
|
|
}
|
|
|
|
// set_altlang_urls: Set the URLs of the language variants
|
|
function set_altlang_urls(&$args)
|
|
{
|
|
// Return if already obtained
|
|
if (array_key_exists("altlang", $args)) {
|
|
return;
|
|
}
|
|
|
|
// Set the path
|
|
$path = $args["path"];
|
|
if (substr($path, -11) == "/index.html") {
|
|
$path = substr($path, 0, -10);
|
|
}
|
|
|
|
// Set the URL parameter
|
|
if ($args["static"]) {
|
|
$len = strlen(ln($args["lang"], LN_FILENAME)) + 2;
|
|
$path = "/%s/" . substr($path, $len);
|
|
} else {
|
|
// GET arguments not converted to UTF-8 yet
|
|
if (!array_key_exists("USER_INPUT", $GLOBALS)) {
|
|
decode_forms();
|
|
}
|
|
global $USER_INPUT;
|
|
// Whether we need to specify the character set
|
|
$need_charset = !$USER_INPUT["GET_CSERR"]
|
|
&& !is_usascii_printable($USER_INPUT["GET_UTF8"]);
|
|
$args0 = array();
|
|
if ( array_key_exists("QUERY_STRING", $_SERVER)
|
|
&& $_SERVER["QUERY_STRING"] != "") {
|
|
$args0 = _altlang_parse_qs($_SERVER["QUERY_STRING"], $USER_INPUT["GET_CHARSET"]);
|
|
}
|
|
// Remove the session ID.
|
|
$args0 = _altlang_qsargs_rem_arg($args0, session_name());
|
|
// US-ASCII -- we don't need to specify the character set
|
|
if (!$need_charset) {
|
|
$args0 = _altlang_qsargs_rem_arg($args0, "charset");
|
|
}
|
|
// Append the referer
|
|
if (auto_keep_referer()) {
|
|
$args0 = _altlang_qsargs_set_arg($args0, "referer", $_SERVER["HTTP_REFERER"]);
|
|
}
|
|
}
|
|
|
|
// Deal with each language
|
|
$urls = array();
|
|
foreach ($args["all_linguas"] as $lang) {
|
|
if ($args["static"]) {
|
|
$urls[$lang] = sprintf($path, ln($lang, LN_FILENAME));
|
|
} else {
|
|
// Make a copy of the variables
|
|
$args1 = $args0;
|
|
// Set the language
|
|
$args1 = _altlang_qsargs_set_arg($args1, "lang", $lang);
|
|
// We need to specify the character set
|
|
if ($need_charset) {
|
|
$charset1 = ln($lang, LN_CHARSET);
|
|
$args1 = _altlang_qsargs_set_arg($args1, "charset", $charset1);
|
|
for ($i = 0; $i < count($args1); $i++) {
|
|
if (is_null($args1[$i]["name"])) {
|
|
$args1[$i] = rawurlencode(h_encode($args1[$i]["val"], $charset1));
|
|
} else {
|
|
$args1[$i] = rawurlencode(h_encode($args1[$i]["name"], $charset1))
|
|
. "=" . rawurlencode(h_encode($args1[$i]["val"], $charset1));
|
|
}
|
|
}
|
|
} else {
|
|
for ($i = 0; $i < count($args1); $i++) {
|
|
if (is_null($args1[$i]["name"])) {
|
|
$args1[$i] = rawurlencode($args1[$i]["val"]);
|
|
} else {
|
|
$args1[$i] = rawurlencode($args1[$i]["name"])
|
|
. "=" . rawurlencode($args1[$i]["val"]);
|
|
}
|
|
}
|
|
}
|
|
$urls[$lang] = REQUEST_FILE . "?" . implode("&", $args1);
|
|
}
|
|
$urls[$lang] = $urls[$lang];
|
|
}
|
|
|
|
// Record it
|
|
$args["altlang"] = $urls;
|
|
return;
|
|
}
|
|
|
|
// _altlang_parse_qs: Parse the query string
|
|
function _altlang_parse_qs($qs, $charset)
|
|
{
|
|
$args = explode("&", $qs);
|
|
for ($i = 0; $i < count($args); $i++) {
|
|
if (preg_match("/^([^=]*)=(.*)$/", $args[$i], $m)) {
|
|
$name = h_decode(urldecode($m[1]), $charset);
|
|
if (is_null($name)) {
|
|
$name = "";
|
|
}
|
|
// PHP treats [] as array indices but not part of the names
|
|
$phpname = preg_replace("/^([^\[]*)\[.*$/", "$1", $name);
|
|
$val = h_decode(urldecode($m[2]), $charset);
|
|
if (is_null($val)) {
|
|
$val = "";
|
|
}
|
|
} else {
|
|
$name = null;
|
|
$phpname = null;
|
|
$val = h_decode(urldecode($args[$i]), $charset);
|
|
if (is_null($val)) {
|
|
$val = "";
|
|
}
|
|
}
|
|
$args[$i] = array(
|
|
"name" => $name,
|
|
"phpname" => $phpname,
|
|
"val" => $val,
|
|
);
|
|
}
|
|
return $args;
|
|
}
|
|
|
|
// _altlang_qsargs_set_arg: Set a query argument
|
|
function _altlang_qsargs_set_arg($args, $name, $val)
|
|
{
|
|
$found = false;
|
|
for ($i = 0; $i < count($args); $i++) {
|
|
// Not this argument
|
|
if ( is_null($args[$i]["phpname"])
|
|
|| $args[$i]["phpname"] != $name) {
|
|
continue;
|
|
}
|
|
// The first occurrence
|
|
if (!$found) {
|
|
$args[$i] = array(
|
|
"name" => $name,
|
|
"phpname" => $name,
|
|
"val" => $val,
|
|
);
|
|
$found = true;
|
|
// Drop the rest of the occurrences
|
|
} else {
|
|
$args = array_merge(
|
|
array_slice($args, 0, $i),
|
|
array_slice($args, $i + 1)
|
|
);
|
|
$i--;
|
|
}
|
|
}
|
|
// Add it if not found
|
|
if (!$found) {
|
|
$args[] = array(
|
|
"name" => $name,
|
|
"phpname" => $name,
|
|
"val" => $val,
|
|
);
|
|
}
|
|
return $args;
|
|
}
|
|
|
|
// _altlang_qsargs_rem_arg: Remove a query argument
|
|
function _altlang_qsargs_rem_arg($args, $name)
|
|
{
|
|
for ($i = 0; $i < count($args); $i++) {
|
|
// Not this argument
|
|
if ( is_null($args[$i]["phpname"])
|
|
|| $args[$i]["phpname"] != $name) {
|
|
continue;
|
|
}
|
|
// Remove this argument
|
|
$args = array_merge(
|
|
array_slice($args, 0, $i),
|
|
array_slice($args, $i + 1)
|
|
);
|
|
$i--;
|
|
}
|
|
return $args;
|
|
}
|
|
|
|
?>
|