diff options
Diffstat (limited to 'modules-available/runmode/inc/runmode.inc.php')
-rw-r--r-- | modules-available/runmode/inc/runmode.inc.php | 141 |
1 files changed, 107 insertions, 34 deletions
diff --git a/modules-available/runmode/inc/runmode.inc.php b/modules-available/runmode/inc/runmode.inc.php index 271542b8..d333af58 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(); @@ -29,58 +32,86 @@ class RunMode * @param string|null $modeId an ID specific to the module to further specify the run mode, NULL to delete the run mode entry * @param string|null $modeData optional, additional data for the run mode * @param bool|null $isClient whether to count the machine as a client (in statistics etc.) NULL for looking at module's general runmode config - * @return bool whether it was set + * @return bool whether it was set/deleted */ - public static function setRunMode($machineuuid, $moduleId, $modeId, $modeData, $isClient) + public static function setRunMode($machineuuid, $moduleId, $modeId, $modeData = null, $isClient = null) { - // - Check if module provides runmode config at all - $config = self::getModuleConfig($moduleId); - if ($config === false) - return false; + if (is_object($moduleId)) { + $moduleId = $moduleId->getIdentifier(); + } // - Check if machine exists $machine = Statistics::getMachine($machineuuid, Machine::NO_DATA); if ($machine === false) return false; + // - Delete entry if mode is null + if ($modeId === null) { + return Database::exec('DELETE FROM runmode WHERE machineuuid = :machineuuid', compact('machineuuid')) > 0; + } // - Add/replace entry in runmode table - if (is_null($modeId)) { - Database::exec('DELETE FROM runmode WHERE machineuuid = :machineuuid', compact('machineuuid')); - } else { - if ($isClient === null) { - $isClient = $config->isClient; - } - Database::exec('INSERT INTO runmode (machineuuid, module, modeid, modedata, isclient)' - . ' VALUES (:uuid, :module, :modeid, :modedata, :isclient)' - . ' ON DUPLICATE KEY' - . ' UPDATE module = VALUES(module), modeid = VALUES(modeid), modedata = VALUES(modedata), isclient = VALUES(isclient)', array( - 'uuid' => $machineuuid, - 'module' => $moduleId, - 'modeid' => $modeId, - 'modedata' => $modeData, - 'isclient' => ($isClient ? 1 : 0), - )); + // - Check if module provides runmode config at all + $config = self::getModuleConfig($moduleId); + if ($config === false) + return false; + if ($isClient === null) { + $isClient = $config->isClient; } + Database::exec('INSERT INTO runmode (machineuuid, module, modeid, modedata, isclient)' + . ' VALUES (:uuid, :module, :modeid, :modedata, :isclient)' + . ' ON DUPLICATE KEY' + . ' UPDATE module = VALUES(module), modeid = VALUES(modeid), modedata = VALUES(modedata), isclient = VALUES(isclient)', array( + 'uuid' => $machineuuid, + 'module' => $moduleId, + 'modeid' => $modeId, + 'modedata' => $modeData, + 'isclient' => ($isClient ? 1 : 0), + )); return true; } /** * @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; } /** @@ -129,7 +160,7 @@ class RunMode $join = $sel = ''; } $res = Database::simpleQuery( - "SELECT r.machineuuid, r.modedata $sel + "SELECT r.machineuuid, r.modedata, r.isclient $sel FROM runmode r $join WHERE module = :module AND modeid = :modeId", compact('module', 'modeId')); @@ -148,6 +179,28 @@ class RunMode } /** + * Return assoc array of all configured clients. + * @param bool $withData also return data field? + * @param bool $isClient true = return clients only, false = return non-clients only, null = return both + * @return array all the entries from the table + */ + public static function getAllClients($withData = false, $isClient = null) + { + $xtra = ''; + if ($withData) { + $xtra .= ', modedata'; + } + $res = Database::simpleQuery("SELECT machineuuid, module, modeid, isclient $xtra FROM runmode"); + $ret = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + if (!is_null($isClient) && ($row['isclient'] != 0) !== $isClient) + continue; + $ret[$row['machineuuid']] = $row; + } + return $ret; + } + + /** * Get display name of a module's mode. If the module doesn't have a getModeName * method configured, the modeId is simply returned. Otherwise the return value of * that method is passed through. getModeName by contract should return false if @@ -168,6 +221,21 @@ class RunMode return call_user_func($conf->getModeName, $modeId); } + /** + * Delete given runmode. + * + * @param string|\Module $module Module runmode belongs to + * @param string $modeId run mode id + */ + public static function deleteMode($module, $modeId) + { + if (is_object($module)) { + $module = $module->getIdentifier(); + } + Database::exec('DELETE FROM runmode WHERE module = :module AND modeid = :modeId', + compact('module', 'modeId')); + } + } /* *\ @@ -207,6 +275,10 @@ class RunModeModuleConfig * @var bool If true, config.tgz should not be downloaded by the client */ public $noSysconfig = false; + /** + * @var bool Allow adding and removing machines to this mode via the generic form + */ + public $allowGenericEditor = true; public function __construct($file) { @@ -220,6 +292,7 @@ class RunModeModuleConfig $this->loadType($data, 'configHook', 'string'); $this->loadType($data, 'isClient', 'boolean'); $this->loadType($data, 'noSysconfig', 'boolean'); + $this->loadType($data, 'allowGenericEditor', 'boolean'); } private function loadType($data, $key, $type) |