From d8580ffca6c843169518746556a79738c54ed255 Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Fri, 23 Aug 2024 16:33:08 +0200 Subject: [PATCH] plugin classes: php8 only; added variable types (WIP) --- .../classes/plugins_renderer.class.php | 2 + .../plugins_shellcmd_request.class.php | 95 +++++++++++-------- 2 files changed, 57 insertions(+), 40 deletions(-) diff --git a/public_html/deployment/classes/plugins_renderer.class.php b/public_html/deployment/classes/plugins_renderer.class.php index 00abde24..ff8c1833 100644 --- a/public_html/deployment/classes/plugins_renderer.class.php +++ b/public_html/deployment/classes/plugins_renderer.class.php @@ -17,6 +17,8 @@ require_once('plugins.class.php'); * * * @author axel + * + * 2024-08-23 v1.1 Axel Hahn php8 only; added variable types */ class plugin_renderer extends ciplugins { diff --git a/public_html/deployment/classes/plugins_shellcmd_request.class.php b/public_html/deployment/classes/plugins_shellcmd_request.class.php index d0064926..805378fd 100644 --- a/public_html/deployment/classes/plugins_shellcmd_request.class.php +++ b/public_html/deployment/classes/plugins_shellcmd_request.class.php @@ -5,54 +5,56 @@ * * Used in ../plugins/shellcmd/getdata.php * + * 2024-08-23 v1.1 Axel Hahn php8 only; added variable types */ require_once('plugins.class.php'); -class req_shellcmd { +class req_shellcmd +{ /** * plugin name * @var string */ - protected $_sPlugin=false; + protected string $_sPlugin = ''; /** * pligin object * @var object */ - protected $_oPlugin=false; + protected object $_oPlugin; /** * Array of return items * TODO: might be removed * @var array + protected array $_aReturn = []; */ - protected $_aReturn=[]; /** * Configuration for the plugin * @var array */ - protected $_aPluginConfig=[]; + protected array $_aPluginConfig = []; /** * Result after execution * @var array */ - protected $_aResult=[]; + protected array $_aResult = []; /** * Flag: enable debug? Default: false * @var bool */ - protected $_debug=false; + protected bool $_debug = false; /** - * plugins class instance + * ciplugins class instance * @var object */ - protected $CI_plugins=false; + protected $CI_plugins = false; // --------------------------------------------------------------- // CONSTRUCTOR @@ -61,7 +63,8 @@ class req_shellcmd { /** * Constructor */ - public function __construct(){ + public function __construct() + { $this->detectPlugin(); } @@ -70,39 +73,45 @@ class req_shellcmd { // --------------------------------------------------------------- /** - * helper execute a given command and return array with executed + * Helper: execute a given command and return array with executed * command, returncode, output + * @param string $sCmd command to execute * @return */ - protected function _execCommand($sCmd){ + protected function _execCommand(string $sCmd): array + { exec("$sCmd", $aOut, $iResult); return [ - 'command'=>$sCmd, - 'exitcode'=>$iResult, - 'time'=>date("H:i:s"), - 'output'=>$aOut, + 'command' => $sCmd, + 'exitcode' => $iResult, + 'time' => date("H:i:s"), + 'output' => $aOut, ]; } /** - * initialize the shellcmd plugin + * Initialize the shellcmd plugin * @returm boolean */ - protected function _loadPlugin(){ - $this->CI_plugins=new ciplugins(); + protected function _loadPlugin(): bool + { + $this->CI_plugins = new ciplugins(); $this->CI_plugins->setPlugin($this->_sPlugin, 'shellcmd'); - $sPluginclass=$this->CI_plugins->getPluginClassname(); - $this->_oPlugin=new $sPluginclass(); + $sPluginclass = $this->CI_plugins->getPluginClassname(); + $this->_oPlugin = new $sPluginclass(); + return true; } /** - * write debug output ... if enabled + * Write debug output ... if enabled + * @return void */ - protected function _wd($s){ - echo $this->_debug ? 'DEBUG '.__CLASS__ . ' '.$s."<br>\n" : ''; + protected function _wd($s): void + { + echo $this->_debug ? 'DEBUG ' . __CLASS__ . ' ' . $s . "<br>\n" : ''; } // --------------------------------------------------------------- @@ -110,40 +119,46 @@ class req_shellcmd { // --------------------------------------------------------------- /** - * detect plugin name to load from GET param "plugin" + * Detect plugin name to load from GET param "plugin" + * @return bool */ - public function detectPlugin(){ - $this->_sPlugin=isset($_GET['plugin']) && $_GET['plugin'] ? preg_replace('/^a-z0-9/', '', $_GET['plugin']) : false; - $this->_wd("detected plugin: ".$this->_sPlugin); + public function detectPlugin(): bool + { + $this->_sPlugin = isset($_GET['plugin']) && $_GET['plugin'] ? preg_replace('/^a-z0-9/', '', $_GET['plugin']) : false; + $this->_wd("detected plugin: " . $this->_sPlugin); return true; } - /** - * get data from plugin command and return array with executed + * Get data from plugin command and return array with executed * command, returncode, output, parsed data * @return array */ - public function get(){ + public function get(): array + { $this->_loadPlugin(); - if (!$this->_oPlugin){ - return $this->_aReturn; + if (!$this->_oPlugin) { + // return $this->_aReturn; + return []; } $this->CI_plugins->getPluginConfig(); - $sCmd=$this->CI_plugins->getConfigitem('command'); + $sCmd = $this->CI_plugins->getConfigitem('command'); $this->_wd("sCmd=$sCmd"); - - $this->_aResult=$this->_execCommand($sCmd); - if (method_exists($this->_oPlugin, "parsedata")){ - $this->_aResult=$this->_oPlugin->parsedata($this->_aResult); + + $this->_aResult = $this->_execCommand($sCmd); + if (method_exists($this->_oPlugin, "parsedata")) { + $this->_aResult = $this->_oPlugin->parsedata($this->_aResult); } return $this->_aResult; } /** - * send response as json + * Send response as json. + * It sends http response header and json data in body + * @return void */ - public function sendResponse(){ + public function sendResponse(): void + { header('Content-Type: application/json'); echo json_encode($this->get(), JSON_PRETTY_PRINT); } -- GitLab