diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index cc7e1ae1495..da5e7799a84 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -2082,7 +2082,14 @@ function dolPrintText($s) */ function dolPrintHTML($s, $allowiframe = 0) { - return dol_escape_htmltag(dol_htmlwithnojs(dol_string_onlythesehtmltags(dol_htmlentitiesbr($s), 1, 1, 1, $allowiframe)), 1, 1, 'common', 0, 1); + // If text is already HTML, we want to escape only dangerous chars else we want to escape all content. + //$isAlreadyHTML = dol_textishtml($s); + + // dol_htmlentitiesbr encode all chars except "'" if string is not already HTML, but + // encode only special char like é but not &, <, >, ", ' if already HTML. + $stringWithEntitesForSpecialChar = dol_htmlentitiesbr($s); + + return dol_escape_htmltag(dol_htmlwithnojs(dol_string_onlythesehtmltags($stringWithEntitesForSpecialChar, 1, 1, 1, $allowiframe)), 1, 1, 'common', 0, 1); } /** @@ -2143,7 +2150,7 @@ function dolPrintHTMLForTextArea($s, $allowiframe = 0) */ function dolPrintPassword($s) { - return htmlspecialchars($s, ENT_COMPAT, 'UTF-8'); + return htmlspecialchars($s, ENT_HTML5, 'UTF-8'); } @@ -2151,8 +2158,8 @@ function dolPrintPassword($s) * Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input fields. * When we need to output strings on pages, we should use: * - dolPrintLabel... - * - dolPrintHTML... that is dol_escape_htmltag(dol_htmlwithnojs(dol_string_onlythesehtmltags(dol_htmlentitiesbr(), 1, 1, 1)), 1, 1) for notes or descriptions into textarea, add 'common' if into a html content - * - dolPrintPassword that is abelhtmlspecialchars( , ENT_COMPAT, 'UTF-8') for passwords. + * - dolPrintHTML... that is dol_escape_htmltag(dol_htmlwithnojs(dol_string_onlythesehtmltags(dol_htmlentitiesbr(...), 1, 1, 1, 0)), 1, 1, 'common', 0, 1) for notes or descriptions into textarea, add 'common' if into a html content + * - dolPrintPassword that is a simple htmlspecialchars(... , ENT_COMPAT, 'UTF-8') for passwords. * * @param string $stringtoescape String to escape * @param int $keepb 1=Replace b tags with escaped value (except if in $noescapetags), 0=Remove them completely @@ -2254,12 +2261,13 @@ function dol_escape_htmltag($stringtoescape, $keepb = 0, $keepn = 0, $noescapeta } while ($diff); } + $tmp = str_ireplace('&', '__ANDNOSEMICOLON__', $tmp); $tmp = str_ireplace('"', '__DOUBLEQUOTENOSEMICOLON__', $tmp); $tmp = str_ireplace('<', '__LESSTHAN__', $tmp); $tmp = str_ireplace('>', '__GREATERTHAN__', $tmp); } - // Warning: htmlentities encode HTML tags like & into & and more (but not < > "es; ' ' & that remains untouched). + // Warning: htmlentities encode all special chars that remains (except "'" with ENT_COMPAT). $result = htmlentities($tmp, ENT_COMPAT, 'UTF-8'); //print $result; @@ -2276,6 +2284,7 @@ function dol_escape_htmltag($stringtoescape, $keepb = 0, $keepn = 0, $noescapeta $result = str_ireplace('__DOUBLEQUOTE__', '"', $result); + $result = str_ireplace('__ANDNOSEMICOLON__', '&', $result); $result = str_ireplace('__DOUBLEQUOTENOSEMICOLON__', '"', $result); $result = str_ireplace('__LESSTHAN__', '<', $result); $result = str_ireplace('__GREATERTHAN__', '>', $result); @@ -8527,7 +8536,7 @@ function dol_nl2br($stringtoencode, $nl2brmode = 0, $forxml = false) } /** - * Sanitize a HTML to remove js, dangerous content and external link. + * Sanitize a HTML to remove js, dangerous content and external links. * This function is used by dolPrintHTML... function for example. * * @param string $stringtoencode String to encode @@ -8588,6 +8597,8 @@ function dol_htmlwithnojs($stringtoencode, $nouseofiframesandbox = 0, $check = ' // Tidy can't be used for restricthtmlallowunvalid and restricthtmlallowlinkscript // TODO Try to implement a hack for restricthtmlallowlinkscript by renaming tag and