Fix protection against unknown specifier

This commit is contained in:
Laurent Destailleur (aka Eldy) 2025-01-28 13:55:25 +01:00
commit 163bce66d4
2 changed files with 42 additions and 4 deletions

View File

@ -682,7 +682,7 @@ class Translate
}
}
$str = preg_replace('/([^%])%([^0sd])/', '__percent_parenthesis__', $str);
$str = preg_replace('/([^%])%([^%0sdmYIMpHSBb])/', '\1__percent_with_bad_specifier__\2', $str);
if (strpos($key, 'Format') !== 0) {
try {
@ -693,7 +693,7 @@ class Translate
}
}
$str = str_replace('__percent_parenthesis__', '%)', $str);
$str = str_replace('__percent_with_bad_specifier__', '%', $str);
// We replace some HTML tags by __xx__ to avoid having them encoded by htmlentities because
// we want to keep '"' '<b>' '</b>' '<u>' '</u>' '<i>' '</i>' '<center> '</center>' '<strong' '</strong>' '<a ' '</a>' '<br>' '<span' '</span>' '< ' that are reliable HTML tags inside translation strings.

View File

@ -26,7 +26,7 @@
* \remarks To run this script as CLI: phpunit filename.php
*/
global $conf,$user,$langs,$db;
global $conf, $user, $langs, $db;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
//require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
@ -104,7 +104,7 @@ class LangTest extends CommonClassTest
}
/**
* testLang
* testTransWithHTMLInParam
*
* @return void
*/
@ -138,6 +138,44 @@ class LangTest extends CommonClassTest
return;
}
/**
* testTransWithPercent
*
* @return void
*/
public function testTransWithPercent(): void
{
global $conf,$user,$langs,$db;
$conf = $this->savconf;
$user = $this->savuser;
$langs = $this->savlangs;
$db = $this->savdb;
include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';
$newlang = new Translate('', $conf);
$newlang->setDefaultLang('fr_FR');
$newlang->load("main");
$result = $newlang->trans("DatabaseConnection");
print "result=".$result.PHP_EOL;
$this->assertEquals('Connexion &agrave; la base', $result);
$result = $newlang->transnoentities("FormatDateHourSecShort");
print "result=".$result.PHP_EOL;
$this->assertEquals('%d/%m/%Y %H:%M:%S', $result);
$newlang = new Translate('', $conf);
$newlang->setDefaultLang('en_US');
$newlang->load("main");
$result = $newlang->transnoentities("FormatDateHourText");
print "result=".$result.PHP_EOL;
$this->assertEquals('%B %d, %Y, %I:%M %p', $result);
return;
}
/**
* testLang
* @dataProvider langDataProvider