From bb8976c9c16d80dbd677005694a4545f51c570b0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 30 Nov 2017 14:24:55 +0100 Subject: [runmode] Overhauled getRunMode with its params --- modules-available/runmode/inc/runmode.inc.php | 50 +++++++++++++++++++++------ 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/modules-available/runmode/inc/runmode.inc.php b/modules-available/runmode/inc/runmode.inc.php index f3e7f3d5..59f8cf01 100644 --- a/modules-available/runmode/inc/runmode.inc.php +++ b/modules-available/runmode/inc/runmode.inc.php @@ -2,6 +2,9 @@ class RunMode { + const DATA_DETAILED = 1; + const DATA_MACHINE_DATA = 2; + const DATA_STRINGS = 4; private static $moduleConfigs = array(); @@ -64,23 +67,48 @@ class RunMode /** * @param string $machineuuid - * @param bool $detailed whether to return meta data about machine, not just machineuuid - * @param bool $assoc use machineuuid as array key + * @param int $returnData bitfield of data to return * @return false|array {'machineuuid', 'isclient', 'module', 'modeid', 'modedata', * <'hostname', 'clientip', 'macaddr', 'locationid', 'lastseen'>} */ - public static function getRunMode($machineuuid, $detailed = false) + public static function getRunMode($machineuuid, $returnData = self::DATA_MACHINE_DATA) { - if ($detailed) { - $sel = ', m.hostname, m.clientip, m.macaddr, m.locationid, m.lastseen'; + if ($returnData === true) { + $returnData = self::DATA_MACHINE_DATA | self::DATA_DETAILED; + } + if ($returnData & self::DATA_MACHINE_DATA) { + if ($returnData & self::DATA_DETAILED) { + $sel = ', m.hostname, m.clientip, m.macaddr, m.locationid, m.lastseen'; + } else { + $sel = ''; + } + $res = Database::queryFirst( + "SELECT m.machineuuid, r.isclient, r.module, r.modeid, r.modedata $sel + FROM machine m INNER JOIN runmode r USING (machineuuid) + WHERE m.machineuuid = :machineuuid LIMIT 1", + compact('machineuuid')); } else { - $sel = ''; + $res = Database::queryFirst('SELECT r.machineuuid, r.isclient, r.module, r.modeid, r.modedata + FROM runmode r + WHERE r.machineuuid = :machineuuid LIMIT 1', + compact('machineuuid')); + } + if ($res === false) + return false; + if ($returnData & self::DATA_STRINGS) { + $module = Module::get($res['module']); + if ($module === false) { + $res['moduleName'] = $res['module']; + } else { + $res['moduleName'] = $module->getDisplayName(); + } + $mode = self::getModeName($res['module'], $res['modeid']); + if ($mode === false) { + $mode = '???? unknown'; + } + $res['modeName'] = $mode; } - return Database::queryFirst( - "SELECT m.machineuuid, r.isclient, r.module, r.modeid, r.modedata $sel - FROM machine m INNER JOIN runmode r USING (machineuuid) - WHERE m.machineuuid = :machineuuid LIMIT 1", - compact('machineuuid')); + return $res; } /** -- cgit v1.2.3-55-g7522