From ec385e9a93716b66463d0bd7c4dc6eab677a5608 Mon Sep 17 00:00:00 2001 From: Thibault Dockx <thibault.dockx@fusiondirectory.org> Date: Mon, 17 Mar 2025 11:36:18 +0000 Subject: [PATCH] :sparkles: Feat(Hooks) - correct error returned Hooks error returned correctly. --- include/errors/class_FusionDirectoryError.inc | 5 +++ include/errors/class_SimplePluginError.inc | 1 - .../errors/class_SimplePluginHookError.inc | 1 - include/simpleplugin/class_simplePlugin.inc | 34 +++++++++++++------ 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/include/errors/class_FusionDirectoryError.inc b/include/errors/class_FusionDirectoryError.inc index ba0bcf2d8..673157c14 100755 --- a/include/errors/class_FusionDirectoryError.inc +++ b/include/errors/class_FusionDirectoryError.inc @@ -24,10 +24,15 @@ class FusionDirectoryError extends Error { protected $htmlMessage; + protected $code; + protected ?Throwable $previous; public function __construct (string $htmlMessage = '', int $code = 0, Throwable $previous = NULL) { $this->htmlMessage = $htmlMessage; + // Code is present withing the parent class Error, we take it here to avoid calling a creation. + $this->code = $code; + $this->previous = $previous; } public function getHtmlMessage () diff --git a/include/errors/class_SimplePluginError.inc b/include/errors/class_SimplePluginError.inc index f93ed77ed..93dc38ac1 100755 --- a/include/errors/class_SimplePluginError.inc +++ b/include/errors/class_SimplePluginError.inc @@ -30,7 +30,6 @@ class SimplePluginError extends FusionDirectoryError implements \Stringable public function __construct ($origin, string $htmlMessage = '', int $code = 0, Throwable $previous = NULL) { $this->setOrigin($origin); - parent::__construct($htmlMessage, $code, $previous); } diff --git a/include/errors/class_SimplePluginHookError.inc b/include/errors/class_SimplePluginHookError.inc index 7df540ab7..dbfd35a9c 100755 --- a/include/errors/class_SimplePluginHookError.inc +++ b/include/errors/class_SimplePluginHookError.inc @@ -28,7 +28,6 @@ class SimplePluginHookError extends SimplePluginError public function __construct ($origin, string $type, string $output = '', int $code = 0, Throwable $previous = NULL) { $this->hookType = $type; - parent::__construct($origin, nl2br('<samp>'.htmlescape($output).'</samp>'), $code, $previous); } diff --git a/include/simpleplugin/class_simplePlugin.inc b/include/simpleplugin/class_simplePlugin.inc index 3eb098f8f..96e360307 100755 --- a/include/simpleplugin/class_simplePlugin.inc +++ b/include/simpleplugin/class_simplePlugin.inc @@ -1671,14 +1671,29 @@ class simplePlugin implements SimpleTab $command = templateHandling::parseString($command, $addAttrs, 'escapeshellarg'); logging::debug(DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $command, 'Execute'); - exec($command, $arr, $returnCode); + logging::debug(DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, "Processed Command: " . $command); - $command = static::passwordProtect($command); + // Use proc_open instead of exec + $descriptorspec = [ + 1 => ['pipe', 'w'], // stdout + 2 => ['pipe', 'w'] // stderr + ]; - $returnOutput = $arr; + $process = proc_open($command, $descriptorspec, $pipes); + if (is_resource($process)) { + $output = stream_get_contents($pipes[1]); + $errorOutput = stream_get_contents($pipes[2]); + fclose($pipes[1]); + fclose($pipes[2]); + $returnCode = proc_close($process); + + $returnOutput = array_filter(explode("\n", trim($output . "\n" . $errorOutput))); + } - if ($returnCode != 0) { - $str = implode("\n", $arr); + $command = static::passwordProtect($command); + + if ($returnCode !== 0) { + $str = implode("\n", $returnOutput); $str = static::passwordProtect($str); logging::debug(DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $command, 'Execution failed code: ' . $returnCode); logging::debug(DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $command, 'Output: ' . $str); @@ -1688,15 +1703,12 @@ class simplePlugin implements SimpleTab $str, $returnCode ); - } elseif (is_array($arr)) { - $str = implode("\n", $arr); + } elseif (!empty($returnOutput) && $config->get_cfg_value('displayHookOutput', 'FALSE') == 'TRUE') { + $str = implode("\n", $returnOutput); $str = static::passwordProtect($str); logging::debug(DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $command, 'Output: ' . $str); - if (!empty($str) && $config->get_cfg_value('displayHookOutput', 'FALSE') == 'TRUE') { - msg_dialog::display('[' . static::class . ' ' . strtolower((string) $cmd) . 'trigger] ' . $command, htmlescape($str), INFO_DIALOG); - } + msg_dialog::display('[' . static::class . ' ' . strtolower((string) $cmd) . ' trigger] ' . $command, htmlescape($str), INFO_DIALOG); } - unset($arr, $command, $returnCode); } return $messages; } -- GitLab