noalyss-commit
[Top][All Lists]
Advanced

[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");
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]