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) {