Fix empty string WEBSITE_OTHERLANG

If post WEBSITE_OTHERLANG with empty value or with value like this
'en,,sv' or 'en,sv,' we can get an otherlang with empty value. This can lead
unexpected behaviour when we use an element of otherlang to set the
subdirectory name to store pages for a sub lang.
This commit is contained in:
Indelog 2021-04-07 09:29:40 +02:00
parent 20fbfc5b15
commit 17fa9559a3
3 changed files with 20 additions and 1 deletions

View File

@ -104,10 +104,12 @@ function dolSavePageAlias($filealias, $object, $objectpage)
}
// Save also alias into all language subdirectories if it is a main language
elseif (empty($objectpage->lang) || !in_array($objectpage->lang, explode(',', $object->otherlang))) {
if (empty($conf->global->WEBSITE_DISABLE_MAIN_LANGUAGE_INTO_LANGSUBDIR) && !empty($object->otherlang)) {
if (empty($conf->global->WEBSITE_DISABLE_MAIN_LANGUAGE_INTO_LANGSUBDIR)) {
$dirname = dirname($filealias);
$filename = basename($filealias);
foreach (explode(',', $object->otherlang) as $sublang) {
// Avoid to erase main alias file if $sublang is empty string
if (empty(trim($sublang))) continue;
$filealiassub = $dirname.'/'.$sublang.'/'.$filename;
$aliascontent = '<?php'."\n";

View File

@ -184,6 +184,8 @@ class Website extends CommonObject
$tmparray = explode(',', $this->otherlang);
if (is_array($tmparray)) {
foreach ($tmparray as $key => $val) {
// It possible we have empty val here if postparam WEBSITE_OTHERLANG is empty or set like this : 'en,,sv' or 'en,sv,'
if (empty(trim($sublang))) continue;
$tmparray[$key] = preg_replace('/[_-].*$/', '', trim($val)); // en_US or en-US -> en
}
$this->otherlang = join(',', $tmparray);
@ -494,6 +496,8 @@ class Website extends CommonObject
$tmparray = explode(',', $this->otherlang);
if (is_array($tmparray)) {
foreach ($tmparray as $key => $val) {
// It possible we have empty val here if postparam WEBSITE_OTHERLANG is empty or set like this : 'en,,sv' or 'en,sv,'
if (empty(trim($val))) continue;
$tmparray[$key] = preg_replace('/[_-].*$/', '', trim($val)); // en_US or en-US -> en
}
$this->otherlang = join(',', $tmparray);

View File

@ -551,6 +551,8 @@ if ($action == 'addsite')
{
$arrayotherlang = explode(',', GETPOST('WEBSITE_OTHERLANG', 'alphanohtml'));
foreach ($arrayotherlang as $key => $val) {
// It possible we have empty val here if postparam WEBSITE_OTHERLANG is empty or set like this : 'en,,sv' or 'en,sv,'
if (empty(trim($val))) continue;
$arrayotherlang[$key] = substr(trim($val), 0, 2); // Kept short language code only
}
@ -1327,6 +1329,8 @@ if ($action == 'updatecss')
{
$arrayotherlang = explode(',', GETPOST('WEBSITE_OTHERLANG', 'alphanohtml'));
foreach ($arrayotherlang as $key => $val) {
// It possible we have empty val here if postparam WEBSITE_OTHERLANG is empty or set like this : 'en,,sv' or 'en,sv,'
if (empty(trim($val))) continue;
$arrayotherlang[$key] = substr(trim($val), 0, 2); // Kept short language code only
}
@ -1768,6 +1772,10 @@ if ($action == 'updatemeta')
$filename = basename($fileoldalias);
$sublangs = explode(',', $object->otherlang);
foreach ($sublangs as $sublang) {
// Under certain conditions $sublang can be an empty string
// ($object->otherlang with empty string or with string like this 'en,,sv')
// if is the case we try to re-delete the main alias file. Avoid it.
if (empty(trim($sublang))) continue;
$fileoldaliassub = $dirname.'/'.$sublang.'/'.$filename;
dol_delete_file($fileoldaliassub);
}
@ -1790,6 +1798,10 @@ if ($action == 'updatemeta')
$filename = basename($pathofwebsite.'/'.trim($tmpaliasalt).'.php');
$sublangs = explode(',', $object->otherlang);
foreach ($sublangs as $sublang) {
// Under certain conditions $ sublang can be an empty string
// ($object->otherlang with empty string or with string like this 'en,,sv')
// if is the case we try to re-delete the main alias file. Avoid it.
if (empty(trim($sublang))) continue;
$fileoldaliassub = $dirname.'/'.$sublang.'/'.$filename;
dol_delete_file($fileoldaliassub);
}
@ -2710,6 +2722,7 @@ if (!GETPOST('hide_websitemenu'))
$onlylang[$website->lang] = $website->lang.' ('.$langs->trans("Default").')';
}
foreach (explode(',', $website->otherlang) as $langkey) {
if (empty(trim($langkey))) continue;
$onlylang[$langkey] = $langkey;
}
$textifempty = $langs->trans("Default");