Skip to content
Snippets Groups Projects
Commit d8580ffc authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

plugin classes: php8 only; added variable types (WIP)

parent 6f5de738
No related branches found
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
...@@ -17,6 +17,8 @@ require_once('plugins.class.php'); ...@@ -17,6 +17,8 @@ require_once('plugins.class.php');
* *
* *
* @author axel * @author axel
*
* 2024-08-23 v1.1 Axel Hahn php8 only; added variable types
*/ */
class plugin_renderer extends ciplugins class plugin_renderer extends ciplugins
{ {
......
...@@ -5,51 +5,53 @@ ...@@ -5,51 +5,53 @@
* *
* Used in ../plugins/shellcmd/getdata.php * Used in ../plugins/shellcmd/getdata.php
* *
* 2024-08-23 v1.1 Axel Hahn php8 only; added variable types
*/ */
require_once('plugins.class.php'); require_once('plugins.class.php');
class req_shellcmd { class req_shellcmd
{
/** /**
* plugin name * plugin name
* @var string * @var string
*/ */
protected $_sPlugin=false; protected string $_sPlugin = '';
/** /**
* pligin object * pligin object
* @var object * @var object
*/ */
protected $_oPlugin=false; protected object $_oPlugin;
/** /**
* Array of return items * Array of return items
* TODO: might be removed * TODO: might be removed
* @var array * @var array
protected array $_aReturn = [];
*/ */
protected $_aReturn=[];
/** /**
* Configuration for the plugin * Configuration for the plugin
* @var array * @var array
*/ */
protected $_aPluginConfig=[]; protected array $_aPluginConfig = [];
/** /**
* Result after execution * Result after execution
* @var array * @var array
*/ */
protected $_aResult=[]; protected array $_aResult = [];
/** /**
* Flag: enable debug? Default: false * Flag: enable debug? Default: false
* @var bool * @var bool
*/ */
protected $_debug=false; protected bool $_debug = false;
/** /**
* plugins class instance * ciplugins class instance
* @var object * @var object
*/ */
protected $CI_plugins = false; protected $CI_plugins = false;
...@@ -61,7 +63,8 @@ class req_shellcmd { ...@@ -61,7 +63,8 @@ class req_shellcmd {
/** /**
* Constructor * Constructor
*/ */
public function __construct(){ public function __construct()
{
$this->detectPlugin(); $this->detectPlugin();
} }
...@@ -70,11 +73,13 @@ class req_shellcmd { ...@@ -70,11 +73,13 @@ 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 * command, returncode, output
* @param string $sCmd command to execute
* @return * @return
*/ */
protected function _execCommand($sCmd){ protected function _execCommand(string $sCmd): array
{
exec("$sCmd", $aOut, $iResult); exec("$sCmd", $aOut, $iResult);
return [ return [
'command' => $sCmd, 'command' => $sCmd,
...@@ -86,22 +91,26 @@ class req_shellcmd { ...@@ -86,22 +91,26 @@ class req_shellcmd {
/** /**
* initialize the shellcmd plugin * Initialize the shellcmd plugin
* @returm boolean * @returm boolean
*/ */
protected function _loadPlugin(){ protected function _loadPlugin(): bool
{
$this->CI_plugins = new ciplugins(); $this->CI_plugins = new ciplugins();
$this->CI_plugins->setPlugin($this->_sPlugin, 'shellcmd'); $this->CI_plugins->setPlugin($this->_sPlugin, 'shellcmd');
$sPluginclass = $this->CI_plugins->getPluginClassname(); $sPluginclass = $this->CI_plugins->getPluginClassname();
$this->_oPlugin = new $sPluginclass(); $this->_oPlugin = new $sPluginclass();
return true;
} }
/** /**
* write debug output ... if enabled * Write debug output ... if enabled
* @return void
*/ */
protected function _wd($s){ protected function _wd($s): void
{
echo $this->_debug ? 'DEBUG ' . __CLASS__ . ' ' . $s . "<br>\n" : ''; echo $this->_debug ? 'DEBUG ' . __CLASS__ . ' ' . $s . "<br>\n" : '';
} }
...@@ -110,24 +119,27 @@ class req_shellcmd { ...@@ -110,24 +119,27 @@ 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(){ public function detectPlugin(): bool
{
$this->_sPlugin = isset($_GET['plugin']) && $_GET['plugin'] ? preg_replace('/^a-z0-9/', '', $_GET['plugin']) : false; $this->_sPlugin = isset($_GET['plugin']) && $_GET['plugin'] ? preg_replace('/^a-z0-9/', '', $_GET['plugin']) : false;
$this->_wd("detected plugin: " . $this->_sPlugin); $this->_wd("detected plugin: " . $this->_sPlugin);
return true; 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 * command, returncode, output, parsed data
* @return array * @return array
*/ */
public function get(){ public function get(): array
{
$this->_loadPlugin(); $this->_loadPlugin();
if (!$this->_oPlugin) { if (!$this->_oPlugin) {
return $this->_aReturn; // return $this->_aReturn;
return [];
} }
$this->CI_plugins->getPluginConfig(); $this->CI_plugins->getPluginConfig();
$sCmd = $this->CI_plugins->getConfigitem('command'); $sCmd = $this->CI_plugins->getConfigitem('command');
...@@ -141,9 +153,12 @@ class req_shellcmd { ...@@ -141,9 +153,12 @@ class req_shellcmd {
} }
/** /**
* 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'); header('Content-Type: application/json');
echo json_encode($this->get(), JSON_PRETTY_PRINT); echo json_encode($this->get(), JSON_PRETTY_PRINT);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment