From c8ecdb8e7a2b9d043f6e7d95ffae9f3a938c6be0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Mon, 15 Jan 2024 00:02:34 +0100 Subject: [PATCH 1/4] add function isDolValidTms --- htdocs/core/lib/functions.lib.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index c9fed272eaa..886a6473da1 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -234,6 +234,25 @@ function isModEnabled($module) //return !empty($conf->$module->enabled); } +/** + * isDolValidTms check if a timestamp is valid. + * + * @param int|string|null $timestamp + * @return bool + */ +function isDolValidTms($timestamp) +{ + if ($timestamp == '') { + dol_syslog('Using empty string for a timestamp is deprecated, prefer use of null when calling page '.$_SERVER["PHP_SELF"], LOG_NOTICE); + return false; + } + if (is_null($timestamp) || !is_numeric($timestamp)) { + return false; + } + + return true; +} + /** * Return a DoliDB instance (database handler). * From e20a6c05c3bdd45282cecb680af7eeb51d3872e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Mon, 15 Jan 2024 00:05:19 +0100 Subject: [PATCH 2/4] example of use --- htdocs/projet/class/project.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/projet/class/project.class.php b/htdocs/projet/class/project.class.php index f79bb10a1b9..5fc55e2da5f 100644 --- a/htdocs/projet/class/project.class.php +++ b/htdocs/projet/class/project.class.php @@ -849,9 +849,9 @@ class Project extends CommonObject $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$tablename." WHERE ".$projectkey." IN (".$this->db->sanitize($ids).") AND entity IN (".getEntity($type).")"; } - if ($date_start > 0 && $type == 'loan') { + if (isDolValidTms($date_start) && $type == 'loan') { $sql .= " AND (dateend > '".$this->db->idate($date_start)."' OR dateend IS NULL)"; - } elseif ($date_start > 0 && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table + } elseif (isDolValidTms($date_start) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table if (empty($datefieldname) && !empty($this->table_element_date)) { $datefieldname = $this->table_element_date; } @@ -861,9 +861,9 @@ class Project extends CommonObject $sql .= " AND (".$datefieldname." >= '".$this->db->idate($date_start)."' OR ".$datefieldname." IS NULL)"; } - if ($date_end > 0 && $type == 'loan') { + if (isDolValidTms($date_end) && $type == 'loan') { $sql .= " AND (datestart < '".$this->db->idate($date_end)."' OR datestart IS NULL)"; - } elseif ($date_end > 0 && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table + } elseif (isDolValidTms($date_end) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table if (empty($datefieldname) && !empty($this->table_element_date)) { $datefieldname = $this->table_element_date; } From 15b1ff5b69786508e360130baa498038ab0cd683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Mon, 15 Jan 2024 00:08:02 +0100 Subject: [PATCH 3/4] fix phpcs --- htdocs/core/lib/functions.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 886a6473da1..751aed5aef5 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -237,7 +237,7 @@ function isModEnabled($module) /** * isDolValidTms check if a timestamp is valid. * - * @param int|string|null $timestamp + * @param int|string|null $timestamp timestamp to check * @return bool */ function isDolValidTms($timestamp) From 4fbb5b01bea22842d64aabe66cd931cce280b3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20France?= Date: Mon, 15 Jan 2024 17:48:20 +0100 Subject: [PATCH 4/4] use shorter name for function --- htdocs/core/lib/functions.lib.php | 6 +++--- htdocs/projet/class/project.class.php | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 751aed5aef5..623f409103b 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -235,14 +235,14 @@ function isModEnabled($module) } /** - * isDolValidTms check if a timestamp is valid. + * isDolTms check if a timestamp is valid. * * @param int|string|null $timestamp timestamp to check * @return bool */ -function isDolValidTms($timestamp) +function isDolTms($timestamp) { - if ($timestamp == '') { + if ($timestamp === '') { dol_syslog('Using empty string for a timestamp is deprecated, prefer use of null when calling page '.$_SERVER["PHP_SELF"], LOG_NOTICE); return false; } diff --git a/htdocs/projet/class/project.class.php b/htdocs/projet/class/project.class.php index 5fc55e2da5f..5d84390383e 100644 --- a/htdocs/projet/class/project.class.php +++ b/htdocs/projet/class/project.class.php @@ -849,9 +849,9 @@ class Project extends CommonObject $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$tablename." WHERE ".$projectkey." IN (".$this->db->sanitize($ids).") AND entity IN (".getEntity($type).")"; } - if (isDolValidTms($date_start) && $type == 'loan') { + if (isDolTms($date_start) && $type == 'loan') { $sql .= " AND (dateend > '".$this->db->idate($date_start)."' OR dateend IS NULL)"; - } elseif (isDolValidTms($date_start) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table + } elseif (isDolTms($date_start) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table if (empty($datefieldname) && !empty($this->table_element_date)) { $datefieldname = $this->table_element_date; } @@ -861,9 +861,9 @@ class Project extends CommonObject $sql .= " AND (".$datefieldname." >= '".$this->db->idate($date_start)."' OR ".$datefieldname." IS NULL)"; } - if (isDolValidTms($date_end) && $type == 'loan') { + if (isDolTms($date_end) && $type == 'loan') { $sql .= " AND (datestart < '".$this->db->idate($date_end)."' OR datestart IS NULL)"; - } elseif (isDolValidTms($date_end) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table + } elseif (isDolTms($date_end) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table if (empty($datefieldname) && !empty($this->table_element_date)) { $datefieldname = $this->table_element_date; }