Merge pull request #29743 from nextcloud/backport/29695/stable22

[stable22] Fix missing setlocale with php 8
This commit is contained in:
MichaIng 2021-11-17 12:06:00 +01:00 committed by GitHub
commit b9d3f6e3ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 6 deletions

View file

@ -1274,22 +1274,38 @@ class OC_Util {
}
/**
* 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 (self::isNonUTF8Locale()) {
return false;
}
}
// Check again
if ('' === basename('§')) {
return false;
}
return true;
}

View file

@ -75,6 +75,18 @@ class UtilTest extends \Test\TestCase {
$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());