From dd3a5949c031048f340c5644141ae5b78b1f3ee9 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 28 Apr 2020 14:25:07 +0200 Subject: [PATCH] FIX Avoid infinite loop when a fetch is inside a compute field. --- htdocs/core/class/commonobject.class.php | 26 ++++++++++++++++++++++-- htdocs/core/class/conf.class.php | 2 ++ htdocs/langs/en_US/admin.lang | 2 +- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 102f9da411a..adb6d21a828 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -5243,6 +5243,25 @@ abstract class CommonObject /* Functions for extrafields */ + /** + * Function to make a fetch but set environment to avoid to load computed values before. + * + * @param int $id ID of object + * @return int >0 if OK, 0 if not found, <0 if KO + */ + public function fetchNoCompute($id) + { + global $conf; + + $savDisableCompute = $conf->disable_compute; + $conf->disable_compute = 1; + + $ret = $this->fetch($id); + + $conf->disable_compute = $savDisableCompute; + + return $ret; + } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** @@ -5257,7 +5276,7 @@ abstract class CommonObject public function fetch_optionals($rowid = null, $optionsArray = null) { // phpcs:enable - global $extrafields; + global $conf, $extrafields; if (empty($rowid)) $rowid = $this->id; if (empty($rowid)) $rowid = $this->rowid; @@ -5341,7 +5360,10 @@ abstract class CommonObject foreach ($tab as $key => $value) { if (!empty($extrafields) && !empty($extrafields->attributes[$this->table_element]['computed'][$key])) { - $this->array_options["options_".$key] = dol_eval($extrafields->attributes[$this->table_element]['computed'][$key], 1, 0); + //var_dump($conf->disable_compute); + if (empty($conf->disable_compute)) { + $this->array_options["options_".$key] = dol_eval($extrafields->attributes[$this->table_element]['computed'][$key], 1, 0); + } } } } diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index 7a0c46a39c4..0a52301e608 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -48,6 +48,8 @@ class Conf //! To store if javascript/ajax is enabked public $use_javascript_ajax; + //! To store if javascript/ajax is enabked + public $disable_compute; //! Used to store current currency (ISO code like 'USD', 'EUR', ...) public $currency; //! Used to store current css (from theme) diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 58e5cecf39d..96bb77a5064 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -427,7 +427,7 @@ ExtrafieldCheckBox=Checkboxes ExtrafieldCheckBoxFromList=Checkboxes from table ExtrafieldLink=Link to an object ComputedFormula=Computed field -ComputedFormulaDesc=You can enter here a formula using other properties of object or any PHP coding to get a dynamic computed value. You can use any PHP compatible formulas including the "?" condition operator, and following global object: $db, $conf, $langs, $mysoc, $user, $object.
WARNING: Only some properties of $object may be available. If you need a properties not loaded, just fetch yourself the object into your formula like in the second example.
Using a computed field means you can't enter yourself any value from interface. Also, if there is a syntax error, the formula may return nothing.

Example of formula:
$object->id < 10 ? round($object->id / 2, 2): ($object->id + 2 * $user->id) * (int) substr($mysoc->zip, 1, 2)

Example to reload object
(($reloadedobj = new Societe($db)) && ($reloadedobj->fetch($obj->id ? $obj->id: ($obj->rowid ? $obj->rowid: $object->id)) > 0)) ? $reloadedobj->array_options['options_extrafieldkey'] * $reloadedobj->capital / 5: '-1'

Other example of formula to force load of object and its parent object:
(($reloadedobj = new Task($db)) && ($reloadedobj->fetch($object->id) > 0) && ($secondloadedobj = new Project($db)) && ($secondloadedobj->fetch($reloadedobj->fk_project) > 0)) ? $secondloadedobj->ref: 'Parent project not found' +ComputedFormulaDesc=You can enter here a formula using other properties of object or any PHP coding to get a dynamic computed value. You can use any PHP compatible formulas including the "?" condition operator, and following global object: $db, $conf, $langs, $mysoc, $user, $object.
WARNING: Only some properties of $object may be available. If you need a properties not loaded, just fetch yourself the object into your formula like in the second example.
Using a computed field means you can't enter yourself any value from interface. Also, if there is a syntax error, the formula may return nothing.

Example of formula:
$object->id < 10 ? round($object->id / 2, 2): ($object->id + 2 * $user->id) * (int) substr($mysoc->zip, 1, 2)

Example to reload object
(($reloadedobj = new Societe($db)) && ($reloadedobj->fetchNoCompute($obj->id ? $obj->id: ($obj->rowid ? $obj->rowid: $object->id)) > 0)) ? $reloadedobj->array_options['options_extrafieldkey'] * $reloadedobj->capital / 5: '-1'

Other example of formula to force load of object and its parent object:
(($reloadedobj = new Task($db)) && ($reloadedobj->fetchNoCompute($object->id) > 0) && ($secondloadedobj = new Project($db)) && ($secondloadedobj->fetchNoCompute($reloadedobj->fk_project) > 0)) ? $secondloadedobj->ref: 'Parent project not found' Computedpersistent=Store computed field ComputedpersistentDesc=Computed extra fields will be stored in the database, however, the value will only be recalculated when the object of this field is changed. If the computed field depends on other objects or global data this value might be wrong!! ExtrafieldParamHelpPassword=Leaving this field blank means this value will be stored without encryption (field must be only hidden with star on screen).
Set 'auto' to use the default encryption rule to save password into database (then value read will be the hash only, no way to retrieve original value)