Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
OC_Util: Add fallbacks to check if current locale is UTF8
Using escapeshellcmd to get current locale causes error
if the function is disabled.

Add fallbacks to prevent the error.

Signed-off-by: Naoto Kobayashi <[email protected]>
  • Loading branch information
YoitoFes authored and backportbot[bot] committed Nov 17, 2021
commit 0bb3232a33831a37914df3f319c162690d0b9188
19 changes: 17 additions & 2 deletions lib/private/legacy/OC_Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -1273,20 +1273,35 @@ public function isHtaccessWorking(\OCP\IConfig $config) {
return $content !== $testContent && $fallbackContent !== $testContent;
}

/**
* Check if current locale is non-UTF8
*
* @return bool
*/
private static function isNonUTF8Locale() {
if (function_exists("escapeshellcmd")) {
return ('' === escapeshellcmd('§'));
} else if (function_exists("escapeshellarg")) {
return ('\'\'' === escapeshellarg('§'));
} else {
return (0 === preg_match('/utf-?8/i', setlocale(LC_CTYPE, 0)));
}
}

/**
* Check if the setlocal call does not work. This can happen if the right
* local packages are not available on the server.
*
* @return bool
*/
public static function isSetLocaleWorking() {
if ('' === escapeshellcmd('§')) {
if (self::isNonUTF8Locale()) {
// Borrowed from \Patchwork\Utf8\Bootup::initLocale
setlocale(LC_ALL, 'C.UTF-8', 'C');
setlocale(LC_CTYPE, 'en_US.UTF-8', 'fr_FR.UTF-8', 'es_ES.UTF-8', 'de_DE.UTF-8', 'ru_RU.UTF-8', 'pt_BR.UTF-8', 'it_IT.UTF-8', 'ja_JP.UTF-8', 'zh_CN.UTF-8', '0');

// Check again
if ('' === escapeshellcmd('§')) {
if (self::isNonUTF8Locale()) {
return false;
}
}
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ public function testEncodePath() {
$this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result);
}

public function testIsSetLocaleWorking() {
// OC_Util::isSetLocaleWorking() assumes escapeshellcmd('§') returns '' with non-UTF-8 locale.
public function testIsNonUTF8Locale() {
// OC_Util::isNonUTF8Locale() assumes escapeshellcmd('§') returns '' with non-UTF-8 locale.
$locale = setlocale(LC_CTYPE, 0);
setlocale(LC_CTYPE, 'C');
$this->assertEquals('', escapeshellcmd('§'));
$this->assertEquals('\'\'', escapeshellarg('§'));
setlocale(LC_CTYPE, 'C.UTF-8');
$this->assertEquals('§', escapeshellcmd('§'));
$this->assertEquals('\'§\'', escapeshellarg('§'));
setlocale(LC_CTYPE, $locale);
}

Expand Down