From 77f982d1a8c3fa490c40c95e06d39f51731da055 Mon Sep 17 00:00:00 2001 From: atm-florian Date: Fri, 10 Jan 2025 16:25:27 +0100 Subject: [PATCH 1/3] FIX 17.0 - collisions in cache for dol_getIdFromCode --- htdocs/core/lib/functions.lib.php | 49 ++++++++++++++++--------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 7fe94ff83c444..61bb1c78a2e14 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -8876,12 +8876,13 @@ function dol_osencode($str) * @param string $fieldid Field to get * @param int $entityfilter Filter by entity * @param string $filters Filters to add. WARNING: string must be escaped for SQL and not coming from user input. + * @param bool $useCache If true (default), cache will be queried and updated. * @return int <0 if KO, Id of code if OK * @see $langs->getLabelFromKey */ -function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = 'id', $entityfilter = 0, $filters = '') +function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = 'id', $entityfilter = 0, $filters = '', $useCache = true) { - global $cache_codes; + static $cache_codes = array(); // If key empty if ($key == '') { @@ -8889,8 +8890,8 @@ function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = } // Check in cache - if (isset($cache_codes[$tablename][$key][$fieldid])) { // Can be defined to 0 or '' - return $cache_codes[$tablename][$key][$fieldid]; // Found in cache + if ($useCache && isset($cache_codes[$tablename][$fieldkey][$fieldid][$entityfilter][$filters][$key])) { // Can be defined to 0 or '' + return $cache_codes[$tablename][$fieldkey][$fieldid][$entityfilter][$filters][$key]; // Found in cache } dol_syslog('dol_getIdFromCode (value for field '.$fieldid.' from key '.$key.' not found into cache)', LOG_DEBUG); @@ -8908,13 +8909,15 @@ function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); + $valuetoget = ''; if ($obj) { - $cache_codes[$tablename][$key][$fieldid] = $obj->valuetoget; - } else { - $cache_codes[$tablename][$key][$fieldid] = ''; + $valuetoget = $obj->valuetoget; } $db->free($resql); - return $cache_codes[$tablename][$key][$fieldid]; + if ($useCache) { + $cache_codes[$tablename][$fieldkey][$fieldid][$entityfilter][$filters][$key] = $valuetoget; + } + return $valuetoget; } else { return -1; } @@ -10839,21 +10842,21 @@ function dolGetStatus($statusLabel = '', $statusLabelShort = '', $html = '', $st * @param int|boolean $userRight User action right * // phpcs:disable * @param array $params = [ // Various params for future : recommended rather than adding more function arguments - * 'attr' => [ // to add or override button attributes - * 'xxxxx' => '', // your xxxxx attribute you want - * 'class' => 'reposition', // to add more css class to the button class attribute - * 'classOverride' => '' // to replace class attribute of the button - * ], - * 'confirm' => [ - * 'url' => 'http://', // Overide Url to go when user click on action btn, if empty default url is $url.?confirm=yes, for no js compatibility use $url for fallback confirm. - * 'title' => '', // Overide title of modal, if empty default title use "ConfirmBtnCommonTitle" lang key - * 'action-btn-label' => '', // Overide label of action button, if empty default label use "Confirm" lang key - * 'cancel-btn-label' => '', // Overide label of cancel button, if empty default label use "CloseDialog" lang key - * 'content' => '', // Overide text of content, if empty default content use "ConfirmBtnCommonContent" lang key - * 'modal' => true, // true|false to display dialog as a modal (with dark background) - * 'isDropDrown' => false, // true|false to display dialog as a dropdown (with dark background) - * ], - * ] + * 'attr' => [ // to add or override button attributes + * 'xxxxx' => '', // your xxxxx attribute you want + * 'class' => 'reposition', // to add more css class to the button class attribute + * 'classOverride' => '' // to replace class attribute of the button + * ], + * 'confirm' => [ + * 'url' => 'http://', // Overide Url to go when user click on action btn, if empty default url is $url.?confirm=yes, for no js compatibility use $url for fallback confirm. + * 'title' => '', // Overide title of modal, if empty default title use "ConfirmBtnCommonTitle" lang key + * 'action-btn-label' => '', // Overide label of action button, if empty default label use "Confirm" lang key + * 'cancel-btn-label' => '', // Overide label of cancel button, if empty default label use "CloseDialog" lang key + * 'content' => '', // Overide text of content, if empty default content use "ConfirmBtnCommonContent" lang key + * 'modal' => true, // true|false to display dialog as a modal (with dark background) + * 'isDropDrown' => false, // true|false to display dialog as a dropdown (with dark background) + * ], + * ] * // phpcs:enable * @return string html button */ From 762e4ec8d1c720785a4f4adb309798acd7f12959 Mon Sep 17 00:00:00 2001 From: atm-florian Date: Fri, 10 Jan 2025 17:01:59 +0100 Subject: [PATCH 2/3] updated local pre-commit hook -> fix typo --- htdocs/core/lib/functions.lib.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 61bb1c78a2e14..610e19b293af7 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -10848,11 +10848,11 @@ function dolGetStatus($statusLabel = '', $statusLabelShort = '', $html = '', $st * 'classOverride' => '' // to replace class attribute of the button * ], * 'confirm' => [ - * 'url' => 'http://', // Overide Url to go when user click on action btn, if empty default url is $url.?confirm=yes, for no js compatibility use $url for fallback confirm. - * 'title' => '', // Overide title of modal, if empty default title use "ConfirmBtnCommonTitle" lang key - * 'action-btn-label' => '', // Overide label of action button, if empty default label use "Confirm" lang key - * 'cancel-btn-label' => '', // Overide label of cancel button, if empty default label use "CloseDialog" lang key - * 'content' => '', // Overide text of content, if empty default content use "ConfirmBtnCommonContent" lang key + * 'url' => 'http://', // Override Url to go when user click on action btn, if empty default url is $url.?confirm=yes, for no js compatibility use $url for fallback confirm. + * 'title' => '', // Override title of modal, if empty default title use "ConfirmBtnCommonTitle" lang key + * 'action-btn-label' => '', // Override label of action button, if empty default label use "Confirm" lang key + * 'cancel-btn-label' => '', // Override label of cancel button, if empty default label use "CloseDialog" lang key + * 'content' => '', // Override text of content, if empty default content use "ConfirmBtnCommonContent" lang key * 'modal' => true, // true|false to display dialog as a modal (with dark background) * 'isDropDrown' => false, // true|false to display dialog as a dropdown (with dark background) * ], From da61078dffa1a67d8f1c657d3772ee435fe13660 Mon Sep 17 00:00:00 2001 From: atm-florian Date: Mon, 13 Jan 2025 10:01:36 +0100 Subject: [PATCH 3/3] =?UTF-8?q?T240540:=20sp=C3=A9=20koesio:=20backport=20?= =?UTF-8?q?PR=20c=C5=93ur=2032621:=20ajout=20"guirlandes"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/core/lib/functions.lib.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 610e19b293af7..bf71b1cdcb565 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -8882,7 +8882,8 @@ function dol_osencode($str) */ function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = 'id', $entityfilter = 0, $filters = '', $useCache = true) { - static $cache_codes = array(); + /* START SPE KOESIO T240540: BACKPORT PR #32621 */ + global $cache_codes; // If key empty if ($key == '') { @@ -8921,6 +8922,7 @@ function dol_getIdFromCode($db, $key, $tablename, $fieldkey = 'code', $fieldid = } else { return -1; } + /* END SPE KOESIO T240540: BACKPORT PR #32621 */ } /**