diff --git a/public_html/deployment/classes/plugins.class.php b/public_html/deployment/classes/plugins.class.php index be93422595710cf3a052c50c4729469ac856f68c..187813dcb43068014ae15651bb437e67832be186 100644 --- a/public_html/deployment/classes/plugins.class.php +++ b/public_html/deployment/classes/plugins.class.php @@ -256,6 +256,13 @@ class ciplugins { // --------------------------------------------------------------- // getter for plugin // --------------------------------------------------------------- + /** + * get config entry + * @param string $sKey name of config value + */ + public function getConfigitem($sKey){ + return isset($this->_aConfig[$sKey]) ? $this->_aConfig[$sKey] : false; + } /** * get plugin config from its config.json * works with diff --git a/public_html/deployment/plugins/shellcmd/plugins_shellcmd.class.php b/public_html/deployment/classes/plugins_shellcmd_request.class.php similarity index 68% rename from public_html/deployment/plugins/shellcmd/plugins_shellcmd.class.php rename to public_html/deployment/classes/plugins_shellcmd_request.class.php index 19516e8092df6cc4b5d5f0a6550f8f5f0d26a195..a99ddd34b9d0d834fd601157df73ec1db55ed96c 100644 --- a/public_html/deployment/plugins/shellcmd/plugins_shellcmd.class.php +++ b/public_html/deployment/classes/plugins_shellcmd_request.class.php @@ -3,20 +3,33 @@ * GENERAL CLASS TO FETCH DATA FROM A SHELL COMMAND * FOR THE WEB UI * - * Used in ./getdata.php + * Used in ../plugins/shellcmd/getdata.php * - * TODO: replace this class with classes/plugins.class.php */ -class shellcmd { +require_once('plugins.class.php'); + +class req_shellcmd { protected $_sPlugin=false; protected $_oPlugin=false; protected $_aReturn=false; + protected $_aPluginConfig=[]; + protected $_aResult=[]; protected $_debug=false; + /** + * plugins class instance + * @var object + */ + protected $CI_plugins=false; + + // --------------------------------------------------------------- + // CONSTRUCTOR + // --------------------------------------------------------------- + /** * constructor * @return bool @@ -25,21 +38,23 @@ class shellcmd { return $this->detectPlugin(); } + // --------------------------------------------------------------- + // PRIVATE METHODS + // --------------------------------------------------------------- /** - * write debug output ... if enabled - */ - protected function _wd($s){ - echo $this->_debug ? 'DEBUG '.__CLASS__ . ' '.$s."<br>\n" : ''; - } - - /** - * detect plugin name to load from GET param "plugin" + * helper execute a given command and return array with executed + * command, returncode, output + * @return */ - public function detectPlugin(){ - $this->_sPlugin=isset($_GET['plugin']) && $_GET['plugin'] ? preg_replace('/^a-z0-9/', '', $_GET['plugin']) : false; - $this->_wd("detected plugin: ".$this->_sPlugin); - return true; + protected function _execCommand($sCmd){ + exec("$sCmd", $aOut, $iResult); + return [ + 'command'=>$sCmd, + 'exitcode'=>$iResult, + 'time'=>date("H:i:s"), + 'output'=>$aOut, + ]; } @@ -48,39 +63,35 @@ class shellcmd { * @returm boolean */ protected function _loadPlugin(){ - $this->_oPlugin=false; - if (!$this->_sPlugin){ - $this->_wd("Missing param for a plugin"); - $this->_aReturn=[ 'error' => 'Missing param for a plugin.' ]; - return false; - } - $sPluginfile=$this->_sPlugin.'/plugin.php'; - $sPluginclass='shellcmd_'.$this->_sPlugin; + $this->CI_plugins=new ciplugins(); + $this->CI_plugins->setPlugin($this->_sPlugin, 'shellcmd'); - if (!file_exists($sPluginfile)){ - $this->_wd("Plugin seems to be corrupt. File not found: '. $sPluginfile."); - $this->_aReturn=[ 'error' => 'Plugin seems to be corrupt. File not found: '. $sPluginfile ]; - return false; - } - include($sPluginfile); + $sPluginclass=$this->CI_plugins->getPluginClassname(); $this->_oPlugin=new $sPluginclass(); - return true; + } + /** - * helper execute a given command and return array with executed - * command, returncode, output - * @return + * write debug output ... if enabled */ - protected function _execCommand($sCmd){ - exec("$sCmd", $aOut, $iResult); - return [ - 'command'=>$sCmd, - 'exitcode'=>$iResult, - 'time'=>date("H:i:s"), - 'output'=>$aOut, - ]; + protected function _wd($s){ + echo $this->_debug ? 'DEBUG '.__CLASS__ . ' '.$s."<br>\n" : ''; + } + + // --------------------------------------------------------------- + // PUBLIC METHODS + // --------------------------------------------------------------- + + /** + * detect plugin name to load from GET param "plugin" + */ + public function detectPlugin(){ + $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 * command, returncode, output, parsed data @@ -91,9 +102,8 @@ class shellcmd { if (!$this->_oPlugin){ return $this->_aReturn; } - $sInfofile=$this->_sPlugin.'/config.json'; - $aMeta=json_decode(file_get_contents($sInfofile), 1); - $sCmd=isset($aMeta['command']) ? $aMeta['command'] : $this->_oPlugin->getCommand(); + $this->CI_plugins->getPluginConfig(); + $sCmd=$this->CI_plugins->getConfigitem('command'); $this->_wd("sCmd=$sCmd"); $this->_aResult=$this->_execCommand($sCmd); diff --git a/public_html/deployment/plugins/shellcmd/getdata.php b/public_html/deployment/plugins/shellcmd/getdata.php index fcfd2ff0b28f56390053998b9291e047af83d4e7..2c16324ed3e93dff3e6634bcdf35f9a5b6b8346f 100644 --- a/public_html/deployment/plugins/shellcmd/getdata.php +++ b/public_html/deployment/plugins/shellcmd/getdata.php @@ -9,7 +9,7 @@ */ header('Content-Type: application/json'); -require_once('plugins_shellcmd.class.php'); +require_once('../../classes/plugins_shellcmd_request.class.php'); -$oShell=new shellcmd(); +$oShell=new req_shellcmd(); $oShell->sendResponse(); diff --git a/public_html/deployment/plugins/shellcmd/load/plugin.php b/public_html/deployment/plugins/shellcmd/load/plugin.php index 1c6655bedc1a32af59d67b4188e0734802f57a6b..1eb0a12ca3ffb413ac78bfd4ca50ea70d75f7325 100644 --- a/public_html/deployment/plugins/shellcmd/load/plugin.php +++ b/public_html/deployment/plugins/shellcmd/load/plugin.php @@ -5,43 +5,15 @@ * * ---------------------------------------------------------------------- * 2022-08-05 axel.hahn@iml.unibe.ch + * 2023-12-13 axel.hahn@unibe.ch minified */ class shellcmd_load { - /** - * @var fallback command line to exectute; command in config.josn has priority - */ - protected $_command='uptime'; - - /** - * constructor ... returns command - * @return string - */ - public function __constructor(){ - return $this->getCommand(); - } - /** - * get column width - * @return string - */ - public function getColumns(){ - return false; - } - /** - * get command - * @return string - */ - public function getCommand(){ - return $this->_command; - } - - /** * parse output and extract wanted values in section "data" * @return array */ public function parsedata($aResult){ $aTmp1=array_reverse(explode(',', $aResult['output'][0])); - // print_r($aTmp1); $aResult['data']=[ 'uptime'=>(isset($aTmp1[5]) ? trim(substr($aTmp1[5], 10) . $aTmp1[4]) diff --git a/public_html/deployment/plugins/shellcmd/processes/plugin.php b/public_html/deployment/plugins/shellcmd/processes/plugin.php index 327065c94cc3cad91780ff755620ca42986b8447..ef858fea181bb5731966db5839ad7be0776b8f52 100644 --- a/public_html/deployment/plugins/shellcmd/processes/plugin.php +++ b/public_html/deployment/plugins/shellcmd/processes/plugin.php @@ -5,29 +5,9 @@ * * ---------------------------------------------------------------------- * 2022-09-19 axel.hahn@iml.unibe.ch + * 2023-12-13 axel.hahn@unibe.ch minified */ class shellcmd_processes { - /** - * @var fallback command line to exectute; command in config.josn has priority - */ - protected $_command="ps -ef --forest "; - - /** - * constructor ... returns command - * @return string - */ - public function __constructor(){ - return $this->getCommand(); - } - - /** - * get command - * @return string - */ - public function getCommand(){ - return $this->_command; - } - /** * parse output and extract wanted values in section "data" * @return array diff --git a/public_html/deployment/plugins/shellcmd/top/plugin.php b/public_html/deployment/plugins/shellcmd/top/plugin.php index 2b64044c2a45605bd643e5bbfa6c5eee74d23379..b0a5fa562b6c8a942c961017a9b00ad541469ac3 100644 --- a/public_html/deployment/plugins/shellcmd/top/plugin.php +++ b/public_html/deployment/plugins/shellcmd/top/plugin.php @@ -5,29 +5,9 @@ * * ---------------------------------------------------------------------- * 2023-11-23 axel.hahn@unibe.ch + * 2023-12-13 axel.hahn@unibe.ch minified */ class shellcmd_top { - /** - * @var fallback command line to exectute; command in config.josn has priority - */ - protected $_command="top -b -n 1 | head -20"; - - /** - * constructor ... returns command - * @return string - */ - public function __constructor(){ - return $this->getCommand(); - } - - /** - * get command - * @return string - */ - public function getCommand(){ - return $this->_command; - } - /** * parse output and extract wanted values in section "data" * @return array