Skip to content

Commit

Permalink
Merge pull request #29695 from YoitoFes/fix_missing_setlocale
Browse files Browse the repository at this point in the history
Fix missing setlocale with php 8
  • Loading branch information
PVince81 authored Nov 16, 2021
2 parents 6ea81dd + 6fc8694 commit 67ff36b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lib/private/legacy/OC_Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -1239,22 +1239,38 @@ public function isHtaccessWorking(\OCP\IConfig $config) {
}

/**
* Check if the setlocal call does not work. This can happen if the right
* Check if current locale is non-UTF8
*
* @return bool
*/
private static function isNonUTF8Locale() {
if (function_exists('escapeshellcmd')) {
return '' === escapeshellcmd('§');
} elseif (function_exists('escapeshellarg')) {
return '\'\'' === escapeshellarg('§');
} else {
return 0 === preg_match('/utf-?8/i', setlocale(LC_CTYPE, 0));
}
}

/**
* Check if the setlocale 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 ('' === basename('§')) {
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 ('' === basename('§')) {
return false;
// Check again
if (self::isNonUTF8Locale()) {
return false;
}
}

return true;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/lib/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ public function testEncodePath() {
$this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result);
}

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);
}

public function testFileInfoLoaded() {
$expected = function_exists('finfo_open');
$this->assertEquals($expected, \OC_Util::fileInfoLoaded());
Expand Down

0 comments on commit 67ff36b

Please sign in to comment.