<?php /** * GENERAL CLASS TO FETCH DATA FROM A SHELL COMMAND * FOR THE WEB UI * * 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 { /** * plugin name * @var string */ protected string $_sPlugin = ''; /** * pligin object * @var object */ protected object $_oPlugin; /** * Array of return items * TODO: might be removed * @var array protected array $_aReturn = []; */ /** * Configuration for the plugin * @var array */ protected array $_aPluginConfig = []; /** * Result after execution * @var array */ protected array $_aResult = []; /** * Flag: enable debug? Default: false * @var bool */ protected bool $_debug = false; /** * ciplugins class instance * @var object */ protected $CI_plugins = false; // --------------------------------------------------------------- // CONSTRUCTOR // --------------------------------------------------------------- /** * Constructor */ public function __construct() { $this->detectPlugin(); } // --------------------------------------------------------------- // PRIVATE METHODS // --------------------------------------------------------------- /** * Helper: execute a given command and return array with executed * command, returncode, output * @param string $sCmd command to execute * @return */ protected function _execCommand(string $sCmd): array { exec("$sCmd", $aOut, $iResult); return [ 'command' => $sCmd, 'exitcode' => $iResult, 'time' => date("H:i:s"), 'output' => $aOut, ]; } /** * Initialize the shellcmd plugin * @returm boolean */ 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(); return true; } /** * Write debug output ... if enabled * @return void */ protected function _wd($s): void { echo $this->_debug ? 'DEBUG ' . __CLASS__ . ' ' . $s . "<br>\n" : ''; } // --------------------------------------------------------------- // PUBLIC METHODS // --------------------------------------------------------------- /** * Detect plugin name to load from GET param "plugin" * @return bool */ 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 * command, returncode, output, parsed data * @return array */ public function get(): array { $this->_loadPlugin(); if (!$this->_oPlugin) { // return $this->_aReturn; return []; } $this->CI_plugins->getPluginConfig(); $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); } return $this->_aResult; } /** * Send response as json. * It sends http response header and json data in body * @return void */ public function sendResponse(): void { header('Content-Type: application/json'); echo json_encode($this->get(), JSON_PRETTY_PRINT); } }