Skip to content

Commit

Permalink
Additional commit for change set 77f9c55
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Jan 25, 2017
1 parent d3810e5 commit 2f6ebe3
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions public_html/switchlang.php
Expand Up @@ -38,32 +38,26 @@
* Switch language in a URL.
*
* @param string $url current URL
* @param string $newlang new language to switch to
* @param string $oldlang old, i.e. current language
* @param string $newLang new language to switch to
* @param string $oldLang old, i.e. current language
* @return string new URL after the language switch
*
*/
function switch_language($url, $newlang, $oldlang)
function switch_language($url, $newLang, $oldLang)
{
global $_CONF;

$retval = '';

if (empty($newlang) || empty($oldlang) ||
(strlen($newlang) !== strlen($oldlang))) {
if (empty($newLang) || empty($oldLang) || (strlen($newLang) !== strlen($oldLang))) {
return $url;
}

$lang_len = strlen($oldlang);
$lang_len = strlen($oldLang);
$url_rewrite = false;
$q = false;

if ($_CONF['url_rewrite']) {
// check for "rewritten" URLs with a '?', e.g. search query highlighting
$q = strpos($url, '?');
if ($q === false) {
$url_rewrite = true;
} elseif (substr($url, $q - 4, 4) !== '.php') {
if (($q === false) || (substr($url, $q - 4, 4) !== '.php')) {
$url_rewrite = true;
}
}
Expand All @@ -80,9 +74,8 @@ function switch_language($url, $newlang, $oldlang)
if (substr($p[$i], -4) === '.php') {
// found the script name - assume next parameter is the ID
if (isset($p[$i + 1])) {
if (substr($p[$i + 1], -($lang_len + 1)) === '_' . $oldlang) {
$p[$i + 1] = substr_replace($p[$i + 1], $newlang,
-$lang_len);
if (substr($p[$i + 1], -($lang_len + 1)) === '_' . $oldLang) {
$p[$i + 1] = substr_replace($p[$i + 1], $newLang, -$lang_len);
$changed = true;
}
}
Expand All @@ -92,40 +85,37 @@ function switch_language($url, $newlang, $oldlang)

if ($changed) {
// merge the pieces back together
if ($q === false) {
$url = implode('/', $p);
} else {
$url = implode('/', $p) . substr($url, $q);
}
$url = ($q === false)
? implode('/', $p)
: implode('/', $p) . substr($url, $q);
}

$retval = $url;
} else { // URL contains '?' or '&'
$url = explode('&', $url);
$urlpart = $url[0];
$urlPart = $url[0];
if (count($url) > 1) {
array_shift($url);
$extra_vars = '&' . implode('&', $url);
} else {
$extra_vars = '';
}

if (substr($urlpart, -($lang_len + 1)) === '_' . $oldlang) {
$urlpart = substr_replace($urlpart, $newlang, -$lang_len);
if (substr($urlPart, -($lang_len + 1)) === '_' . $oldLang) {
$urlPart = substr_replace($urlPart, $newLang, -$lang_len);
}

$retval = $urlpart . $extra_vars;
$retval = $urlPart . $extra_vars;
}

return $retval;
}

// MAIN
$ret_url = '';
if (isset($_SERVER['HTTP_REFERER'])) {
if (strpos($_SERVER['HTTP_REFERER'], $_CONF['site_url']) !== false) {
$ret_url = $_SERVER['HTTP_REFERER'];
}
if (isset($_SERVER['HTTP_REFERER']) &&
(strpos($_SERVER['HTTP_REFERER'], $_CONF['site_url']) !== false)) {
$ret_url = $_SERVER['HTTP_REFERER'];
}

// if not allowed, just ignore and return
Expand All @@ -134,30 +124,38 @@ function switch_language($url, $newlang, $oldlang)

$lang = strtolower(COM_applyFilter(COM_getArgument('lang')));
$lang = preg_replace('/[^a-z0-9\-_]/', '', $lang);
$oldlang = COM_getLanguageId();
$oldLang = COM_getLanguageId();

// Code provided by hiroron
if ($lang === $oldLang) {
$langFromUrl = COM_getLanguageFromURL($ret_url);
$oldLang = empty($langFromUrl) ? $oldLang : $langFromUrl;
}

// do we really have a new language to switch to?
if (!empty($lang) && array_key_exists($lang, $_CONF['language_files'])) {
// does such a language file exist?
$langfile = $_CONF['language_files'][$lang];
if (is_file($_CONF['path_language'] . $langfile . '.php')) {
$langFile = $_CONF['language_files'][$lang];

if (is_file($_CONF['path_language'] . $langFile . '.php')) {
// Set the language cookie.
// Mainly used for anonymous users so the rest of their session
// will remain in the selected language
setcookie($_CONF['cookie_language'], $langfile, time() + 31536000,
$_CONF['cookie_path'], $_CONF['cookiedomain'],
$_CONF['cookiesecure']);
setcookie(
$_CONF['cookie_language'], $langFile, time() + 31536000,
$_CONF['cookie_path'], $_CONF['cookiedomain'], $_CONF['cookiesecure']
);

// if user is not anonymous, store the preference in the database
if (!COM_isAnonUser()) {
DB_query("UPDATE {$_TABLES['users']} SET language = '{$langfile}' WHERE uid = {$_USER['uid']}");
DB_query("UPDATE {$_TABLES['users']} SET language = '" . DB_escapeString($langFile) . "' WHERE uid = {$_USER['uid']}");
}
}
}

// Change the language ID if needed
if (!empty($ret_url) && !empty($lang) && !empty($oldlang)) {
$ret_url = switch_language($ret_url, $lang, $oldlang);
if (!empty($ret_url) && !empty($lang) && !empty($oldLang)) {
$ret_url = switch_language($ret_url, $lang, $oldLang);
}
}

Expand Down

0 comments on commit 2f6ebe3

Please sign in to comment.