diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index f8638cf1c63..c376da32463 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -12805,6 +12805,8 @@ function jsonOrUnserialize($stringtodecode) */ function forgeSQLFromUniversalSearchCriteria($filter, &$errorstr = '', $noand = 0, $nopar = 0, $noerror = 0) { + global $db, $user; + if ($filter === '') { return ''; } @@ -12838,7 +12840,16 @@ function forgeSQLFromUniversalSearchCriteria($filter, &$errorstr = '', $noand = } } - return ($noand ? "" : " AND ").($nopar ? "" : '(').preg_replace_callback('/'.$regexstring.'/i', 'dolForgeCriteriaCallback', $filter).($nopar ? "" : ')'); + $ret = ($noand ? "" : " AND ").($nopar ? "" : '(').preg_replace_callback('/'.$regexstring.'/i', 'dolForgeCriteriaCallback', $filter).($nopar ? "" : ')'); + + if (is_object($db)) { + $ret = str_replace('__NOW__', $db->idate(dol_now()), $ret); + } + if (is_object($user)) { + $ret = str_replace('__USER_ID__', (int) $user->id, $ret); + } + + return $ret; } /** diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index bd0f56b57cf..b68ae5ab6d5 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -216,6 +216,11 @@ class FunctionsLibTest extends CommonClassTest { global $conf, $langs, $db; + // Test on NOW + $filter = "(client:!=:8) AND (datefin:>=:'__NOW__')"; + $sql = forgeSQLFromUniversalSearchCriteria($filter); + $this->assertStringContainsStringIgnoringCase(" AND ((client <> 8) AND (datefin >= '", $sql); + // An attempt for SQL injection $filter = 'if(now()=sysdate()%2Csleep(6)%2C0)'; $sql = forgeSQLFromUniversalSearchCriteria($filter);