Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions core/src/Revolution/modX.php
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@
} elseif (strpos(strtolower($src), "<script") !== false) {
$this->sjscripts[count($this->sjscripts)]= $src;
} else {
$this->sjscripts[count($this->sjscripts)]= '<script src="' . $src . '"></script>';

Check failure on line 1599 in core/src/Revolution/modX.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected at least 1 space before "="; 0 found
}
}
}
Expand All @@ -1619,7 +1619,7 @@
} elseif (strpos(strtolower($src), "<script") !== false) {
$this->jscripts[count($this->jscripts)]= $src;
} else {
$this->jscripts[count($this->jscripts)]= '<script src="' . $src . '"></script>';

Check failure on line 1622 in core/src/Revolution/modX.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected at least 1 space before "="; 0 found
}
}

Expand Down Expand Up @@ -2889,6 +2889,36 @@
}
}

/**
* Persist manager language preference in a cookie so it survives logout (issue #16072).
* Uses session cookie path and SameSite for consistency with session cookies.
*
* @param string $language Valid language code from lexicon->getLanguageList() (e.g. 'en', 'de')
* @return void
*/
public function setManagerLanguageCookie($language)
{
$cookiePath = $this->getOption('session_cookie_path', null, '');
if (empty($cookiePath)) {
$cookiePath = $this->getOption('base_url', null, MODX_BASE_URL);
}
if (empty($cookiePath)) {
$cookiePath = '/';
}
$cookieOptions = [
'expires' => time() + 31536000,
'path' => $cookiePath,
'domain' => '',
'secure' => (bool) $this->getOption('session_cookie_secure', null, false),
'httponly' => false,
];
$samesite = $this->getOption('session_cookie_samesite', null, 'Lax');
if ($samesite !== '') {
$cookieOptions['samesite'] = $samesite;
}
setcookie('modx_manager_language', $language, $cookieOptions);
}

/**
* Loads the modX system configuration settings.
*
Expand Down
7 changes: 6 additions & 1 deletion manager/assets/modext/core/modx.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,12 @@ Ext.extend(MODx,Ext.Component,{
,listeners: {
'success': {fn:function(r) {
if (this.fireEvent('afterLogout',r)) {
location.href = './';
var lang = Ext.util.Cookies && Ext.util.Cookies.get("modx_manager_language");
var url = "./";
if (lang) {
url = "./?manager_language=" + encodeURIComponent(lang);
}
location.href = url;
}
},scope:this}
}
Expand Down
3 changes: 2 additions & 1 deletion manager/controllers/default/language.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use MODX\Revolution\modParsedManagerController;

/**
* Switches the current manger language to requested.
* Switches the current manager language to requested.
*
* Class LanguageManagerController
*/
Expand All @@ -30,6 +30,7 @@ public function process(array $scriptProperties = [])
}
}
$_SESSION['manager_language'] = $targetLanguage;
$this->modx->setManagerLanguageCookie($targetLanguage);

$this->modx->sendRedirect(MODX_MANAGER_URL . (($targetProperties) ? '?' . http_build_query($targetProperties) : ''));
}
Expand Down
26 changes: 16 additions & 10 deletions manager/controllers/default/security/login.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,21 @@ public function handleLanguageChange()

$ml = $this->modx->sanitizeString($this->modx->getOption('manager_language', $_REQUEST));
if (!$ml || !in_array($ml, $languages)) {
$ml = $this->modx->getOption('manager_language', $_SESSION);
if (!$ml) {
// Try to detect default browser language
$accept_languages = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);
preg_match_all('#([\w-]+)(?:[^,\d]+([\d.]+))?#', $accept_languages, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$lang = trim(explode('-', $match[1])[0]);
if (in_array($lang, $languages)) {
$ml = $lang;
break;
$ml = isset($_COOKIE['modx_manager_language'])
? $this->modx->sanitizeString($_COOKIE['modx_manager_language'])
: null;
if (!$ml || !in_array($ml, $languages)) {
$ml = $this->modx->getOption('manager_language', $_SESSION);
if (!$ml) {
// Try to detect default browser language
$accept_languages = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '');
preg_match_all('#([\w-]+)(?:[^,\d]+([\d.]+))?#', $accept_languages, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$lang = trim(explode('-', $match[1])[0]);
if (in_array($lang, $languages)) {
$ml = $lang;
break;
}
}
}
}
Expand All @@ -225,6 +230,7 @@ public function handleLanguageChange()
}
// Save manager language to session
$_SESSION['manager_language'] = $ml;
$this->modx->setManagerLanguageCookie($ml);
// If user tried to change language - make redirect to hide it from url
if (!empty($_GET['manager_language'])) {
unset($_GET['manager_language']);
Expand Down
12 changes: 11 additions & 1 deletion manager/controllers/default/security/logout.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ public function loadCustomCssJs() {}
* @return mixed
*/
public function process(array $scriptProperties = []) {
$managerLanguage = null;
if (!empty($_SESSION['manager_language'])) {
$languages = $this->modx->lexicon->getLanguageList('core');
if (in_array($_SESSION['manager_language'], $languages)) {
$managerLanguage = $_SESSION['manager_language'];
}
}
$this->modx->runProcessor('security/logout');
$url = $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
$url = $this->modx->getOption('manager_url', null, MODX_MANAGER_URL);
if ($managerLanguage !== null) {
$url .= (strpos($url, '?') !== false ? '&' : '?') . 'manager_language=' . urlencode($managerLanguage);
}
$this->modx->sendRedirect($url);
}

Expand Down
Loading