From 6f4b2a890560ca7f346ebd5130dd638272ef2a77 Mon Sep 17 00:00:00 2001 From: Eric - CAP-REL <1468823+rycks@users.noreply.github.com> Date: Tue, 11 Mar 2025 02:13:06 +0100 Subject: [PATCH] allow spaces on natural search (#33324) * allow spaces on natural search like documentation says * clear comment * sanitize backport from 19.0 * update sanitize call to be compatible with dolibarr 19.0 backported function --- htdocs/core/db/DoliDB.class.php | 22 ++++++++++------------ htdocs/core/lib/functions.lib.php | 9 +++++++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index f23a66bbee4..a205a7ad18e 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -121,19 +121,17 @@ abstract class DoliDB implements Database } /** - * Sanitize a string for SQL forging - * - * @param string $stringtosanitize String to escape - * @param int $allowsimplequote 1=Allow simple quotes in string. When string is used as a list of SQL string ('aa', 'bb', ...) - * @return string String escaped - */ - public function sanitize($stringtosanitize, $allowsimplequote = 0) + * Sanitize a string for SQL forging + * + * @param string $stringtosanitize String to escape + * @param int $allowsimplequote 1=Allow simple quotes in string. When string is used as a list of SQL string ('aa', 'bb', ...) + * @param int $allowsequals 1=Allow equals sign + * @param int $allowsspace 1=Allow space char + * @return string String escaped + */ + public function sanitize($stringtosanitize, $allowsimplequote = 0, $allowsequals = 0, $allowsspace = 0) { - if ($allowsimplequote) { - return preg_replace('/[^a-z0-9_\-\.,\']/i', '', $stringtosanitize); - } else { - return preg_replace('/[^a-z0-9_\-\.,]/i', '', $stringtosanitize); - } + return preg_replace('/[^a-z0-9_\-\.,'.($allowsequals ? '=' : '').($allowsimplequote ? "\'" : '').($allowsspace ? ' ' : '').']/i', '', $stringtosanitize); } /** diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 219d6bfee8a..15056c88412 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -9022,7 +9022,12 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0) $value = preg_replace('/\s*\|\s*/', '|', $value); - $crits = explode(' ', $value); + //natural mode search type 3 allow spaces into search ... + if ($mode == 3 || $mode == -3) { + $crits = explode(',', $value); + } else { + $crits = explode(' ', $value); + } $res = ''; if (!is_array($fields)) { $fields = array($fields); @@ -9072,7 +9077,7 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0) $listofcodes .= "'".$db->escape($val)."'"; } } - $newres .= ($i2 > 0 ? ' OR ' : '').$field." ".($mode == -3 ? 'NOT ' : '')."IN (".$db->sanitize($listofcodes, 1).")"; + $newres .= ($i2 > 0 ? ' OR ' : '').$field." ".($mode == -3 ? 'NOT ' : '')."IN (".$db->sanitize($listofcodes, 1, 0, 1).")"; $i2++; // a criteria was added to string } if ($mode == -3) {