From b33be80be3f7a545419cef111984fc7dcc417b99 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 16 Sep 2021 21:16:49 +0200 Subject: [PATCH 1/4] Entity dynamic method calling improved --- pandora_console/include/lib/Entity.php | 54 ++++++++++++++++---------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/pandora_console/include/lib/Entity.php b/pandora_console/include/lib/Entity.php index d7f55ed780..c22076505d 100644 --- a/pandora_console/include/lib/Entity.php +++ b/pandora_console/include/lib/Entity.php @@ -153,6 +153,17 @@ abstract class Entity /** * Dynamically call methods in this object. * + * To dynamically switch between community methods and prioritize + * enterprise ones, define method visibility as 'protected' in both + * classes. + * + * For instance, in following situation: + * protected PandoraFMS\Agent::test() + * protected PandoraFMS\Enterprise\Agent::test() + * + * If enterprise is available, then PandoraFMS\Enterprise\Agent::test() + * will be executed, community method otherwise. + * * @param string $methodName Name of target method or attribute. * @param array $params Arguments for target method. * @@ -161,41 +172,42 @@ abstract class Entity */ public function __call(string $methodName, ?array $params=null) { - // Prioritize written methods over dynamic ones. - if (method_exists($this, $methodName) === true) { - return call_user_func_array( - $this->{$methodName}, - $params - ); - } - // Enterprise capabilities. + // Prioritize enterprise written methods over dynamic fields. if (\enterprise_installed() === true && $this->enterprise !== null && method_exists($this->enterprise, $methodName) === true ) { + $obj = $this->enterprise; + $callback = function (...$parameters) use ($obj, $methodName) { + return $obj->$methodName(...$parameters); + }; + return call_user_func_array( - [ - $this->enterprise, - $methodName, - ], + $callback, $params ); } - if (array_key_exists($methodName, $this->fields) === true) { - if (empty($params) === true) { - return $this->fields[$methodName]; - } else { - $this->fields[$methodName] = $params[0]; + if (method_exists($this, $methodName) === false) { + if (array_key_exists($methodName, $this->fields) === true) { + if (empty($params) === true) { + return $this->fields[$methodName]; + } else { + $this->fields[$methodName] = $params[0]; + } + + return null; } - return null; + throw new \Exception( + get_class($this).' error, method '.$methodName.' does not exist' + ); } - throw new \Exception( - get_class($this).' error, method '.$methodName.' does not exist' - ); + // Do not return nor throw exceptions after this point, allow php + // default __call behaviour to continue working with object method + // defined. } From 187c6700b1e77130f45a3904a3d8bd31ba7f2e34 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 16 Sep 2021 21:54:12 +0200 Subject: [PATCH 2/4] Simplified Entity --- pandora_console/include/lib/Entity.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/pandora_console/include/lib/Entity.php b/pandora_console/include/lib/Entity.php index c22076505d..3304c9e805 100644 --- a/pandora_console/include/lib/Entity.php +++ b/pandora_console/include/lib/Entity.php @@ -178,15 +178,7 @@ abstract class Entity && $this->enterprise !== null && method_exists($this->enterprise, $methodName) === true ) { - $obj = $this->enterprise; - $callback = function (...$parameters) use ($obj, $methodName) { - return $obj->$methodName(...$parameters); - }; - - return call_user_func_array( - $callback, - $params - ); + return $this->enterprise->$methodName(...$params); } if (method_exists($this, $methodName) === false) { From 47633b479495f97f7242281bdb704fd2e441e9e0 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 16 Sep 2021 21:57:23 +0200 Subject: [PATCH 3/4] A better explanation --- pandora_console/include/lib/Entity.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandora_console/include/lib/Entity.php b/pandora_console/include/lib/Entity.php index 3304c9e805..98b9aa8600 100644 --- a/pandora_console/include/lib/Entity.php +++ b/pandora_console/include/lib/Entity.php @@ -200,6 +200,10 @@ abstract class Entity // Do not return nor throw exceptions after this point, allow php // default __call behaviour to continue working with object method // defined. + // If you're receiving NULL as result of the method invocation, ensure + // it is not private, take in mind this method will mask any access + // level error or notification since it is public and has limited access + // to the object (public|protected). } From ddbb06a967f8ea792846ec4b44d2d442dd2a60b2 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Fri, 17 Sep 2021 11:46:59 +0200 Subject: [PATCH 4/4] Merged policies conf fixer tool --- .../tools/merged_policies_conf_fixer.php | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 pandora_console/tools/merged_policies_conf_fixer.php diff --git a/pandora_console/tools/merged_policies_conf_fixer.php b/pandora_console/tools/merged_policies_conf_fixer.php new file mode 100644 index 0000000000..3198b663d7 --- /dev/null +++ b/pandora_console/tools/merged_policies_conf_fixer.php @@ -0,0 +1,62 @@ +hasRemoteCapabilities() === true) { + $agent_policies = $agent->getConfPolicies(); + + $oldIds = []; + $newIds = []; + foreach ($agent_policies as $oldId => $name) { + $oldIds[] = $oldId; + $newIds[] = $policies[io_safe_input($name)]; + } + + $res_update_con_policy = $agent->updatePolicyIds( + $oldIds, + $newIds + ); + if ($res_update_con_policy === false) { + echo 'Failed ['.$agent->name()."]\n"; + } else { + echo 'Agent '.io_safe_output($agent->alias())." updated successfully\n"; + } + } else { + echo 'Agent '.io_safe_output($agent->alias())." skipped\n"; + } + } catch (Exception $e) { + echo $e->getMessage()."\n"; + } +}