[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Noalyss-commit] [noalyss] 07/119: prevent divide by zero in formula
From: |
Dany De Bontridder |
Subject: |
[Noalyss-commit] [noalyss] 07/119: prevent divide by zero in formula |
Date: |
Mon, 26 Oct 2020 18:27:00 -0400 (EDT) |
sparkyx pushed a commit to branch master
in repository noalyss.
commit 5779e2f33c310baed5ea1639475a1a4b27a7ee08
Author: Dany De Bontridder <danydb@noalyss.eu>
AuthorDate: Tue Aug 18 19:11:46 2020 +0200
prevent divide by zero in formula
---
include/lib/ac_common.php | 23 ++++++++++++++++++++---
include/lib/impress.class.php | 4 ++++
2 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/include/lib/ac_common.php b/include/lib/ac_common.php
index 44ee884..ff0f1e8 100644
--- a/include/lib/ac_common.php
+++ b/include/lib/ac_common.php
@@ -209,13 +209,13 @@ function cmpDate($p_date, $p_date_oth)
/***!
* @brief check if the argument is a number
*
- * \param $p_int number to test
+ * @param $p_int number to test
*
- * \return
+ * @return
* - 1 it's a number
* - 0 it is not
*/
-function isNumber(&$p_int)
+function isNumber($p_int)
{
if (strlen(trim($p_int)) == 0)
return 0;
@@ -1375,3 +1375,20 @@ function convert_to_rtf($p_string)
}
return $result;
}
+/**
+ * When it is needed to eval a formula , this function prevent the divide by
zero.
+ * the formula is a math operation to evaluate like : 1.0+2.0/1 (...) , it is
used in "report",
+ * it removes the operation "divide by 0 "
+ *
+ * @param string $p_formula string containing a operation to evaluate
+ *
+ * @see Impress::parse_formula
+ */
+function remove_divide_zero($p_formula)
+{
+ $test=str_replace(" ","",$p_formula).";";
+
$p_formula=preg_replace("![0-9]+\.*[0-9]*/0\.{0,1}0*(\+|-|\*|/|;){1}!","0$1",$test);
+ $p_formula=trim($p_formula,';');
+ return $p_formula;
+}
+
diff --git a/include/lib/impress.class.php b/include/lib/impress.class.php
index 0a8beb3..8ea617f 100644
--- a/include/lib/impress.class.php
+++ b/include/lib/impress.class.php
@@ -153,6 +153,10 @@ class Impress
// $p_eval is true then we eval and returns result
if ( $p_eval == true)
{
+ /*-------------------------------------
+ * Protect againt division by zero
+ */
+ $p_formula=remove_divide_zero($p_formula);
$p_formula="\$result=".$p_formula.";";
eval("$p_formula");
- [Noalyss-commit] [noalyss] branch master updated (44dbd49 -> bc168d2), Dany De Bontridder, 2020/10/26
- [Noalyss-commit] [noalyss] 07/119: prevent divide by zero in formula,
Dany De Bontridder <=
- [Noalyss-commit] [noalyss] 09/119: compatibility PHP74 : join() : glue before piece, Dany De Bontridder, 2020/10/26
- [Noalyss-commit] [noalyss] 06/119: reconcilaction : possibility to select only one operation, Dany De Bontridder, 2020/10/26
- [Noalyss-commit] [noalyss] 13/119: Bug 1834 : order POST over GET, Dany De Bontridder, 2020/10/26
- [Noalyss-commit] [noalyss] 05/119: Bug : syntax error, Dany De Bontridder, 2020/10/26
- [Noalyss-commit] [noalyss] 04/119: Bug : use ilike instead of tilde, Dany De Bontridder, 2020/10/26
- [Noalyss-commit] [noalyss] 02/119: Bug : correct search remove ~, Dany De Bontridder, 2020/10/26
- [Noalyss-commit] [noalyss] 08/119: remove extract, Dany De Bontridder, 2020/10/26
- [Noalyss-commit] [noalyss] 03/119: Merge branch 'master' of gitlab.noalyss.eu:noalyss/noalyss, Dany De Bontridder, 2020/10/26
- [Noalyss-commit] [noalyss] 01/119: Fix warning with empty array, Dany De Bontridder, 2020/10/26
- [Noalyss-commit] [noalyss] 10/119: Fix bug : cannot use % when searching NEW : space can be used now, Dany De Bontridder, 2020/10/26