From 96cb7bfa01491c3558f89d382c17bc247dadfad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org> Date: Thu, 3 Jun 2021 11:19:06 +0200 Subject: [PATCH] :ambulance: fix(core) Fix array_remove_entries for array of arrays This will fix webservice removing of array values on complex multivaluated attributes. issue #6165 --- include/functions.inc | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/include/functions.inc b/include/functions.inc index 64388aaa9..65d1903d3 100644 --- a/include/functions.inc +++ b/include/functions.inc @@ -232,7 +232,7 @@ function get_template_path ($filename = '', $plugin = FALSE, $path = '') */ function array_remove_entries (array $needles, array $haystack) { - return array_values(array_diff($haystack, $needles)); + return array_values(array_udiff($haystack, $needles, 'array_cmp_recursive')); } @@ -241,6 +241,7 @@ function array_remove_entries (array $needles, array $haystack) * * Removes every element that is in $needles from the * array given as $haystack but case insensitive + * Only works on array of stringable * * \param array $needles array of the entries to remove * @@ -1075,25 +1076,39 @@ function array_differs (array $src, array $dst) * \return boolean TRUE or FALSE */ function array_differs_recursive ($src, $dst) +{ + return (array_cmp_recursive($src, $dst) !== 0); +} + +/*! + * \brief Determine if two arrays are different using recursion for sublevels + * + * \param array $src The source + * + * \param array $dst The destination + * + * \return <0, 0 or >0 if $src is <, = or > $dst + */ +function array_cmp_recursive ($src, $dst): int { if (is_array($src)) { if (!is_array($dst)) { - return TRUE; + return 1; } if (count($src) != count($dst)) { - return TRUE; + return count($src) - count($dst); } foreach ($src as $key => $value) { if (!isset($dst[$key])) { - return TRUE; + return 1; } - if (array_differs_recursive($dst[$key], $value)) { - return TRUE; + if (($cmp = array_cmp_recursive($dst[$key], $value)) !== 0) { + return $cmp; } } - return FALSE; + return 0; } - return ((string)$src != (string)$dst); + return strcmp($src, $dst); } /*! -- GitLab