diff options
Diffstat (limited to 'modules-available/runmode')
-rw-r--r-- | modules-available/runmode/baseconfig/getconfig.inc.php | 13 | ||||
-rw-r--r-- | modules-available/runmode/inc/runmode.inc.php | 76 | ||||
-rw-r--r-- | modules-available/runmode/page.inc.php | 43 |
3 files changed, 67 insertions, 65 deletions
diff --git a/modules-available/runmode/baseconfig/getconfig.inc.php b/modules-available/runmode/baseconfig/getconfig.inc.php index 9c36cc75..a5de1053 100644 --- a/modules-available/runmode/baseconfig/getconfig.inc.php +++ b/modules-available/runmode/baseconfig/getconfig.inc.php @@ -1,20 +1,23 @@ <?php -(function($machineUuid) { +/** @var ?string $uuid */ +/** @var ?string $ip */ + +if ($uuid !== null) { $res = Database::queryFirst('SELECT module, modeid, modedata FROM runmode WHERE machineuuid = :uuid', - array('uuid' => $machineUuid)); + array('uuid' => $uuid)); if ($res === false) return; $config = RunMode::getModuleConfig($res['module']); - if ($config === false) + if ($config === null) return; if (!Module::isAvailable($res['module'])) return; // Not really possible because getModuleConfig would have failed but we should make sure if ($config->configHook !== false) { - call_user_func($config->configHook, $machineUuid, $res['modeid'], $res['modedata']); + call_user_func($config->configHook, $uuid, $res['modeid'], $res['modedata']); } if ($config->systemdDefaultTarget !== false) { ConfigHolder::add('SLX_SYSTEMD_TARGET', $config->systemdDefaultTarget, 10000); } ConfigHolder::add('SLX_RUNMODE_MODULE', $res['module']); -})($uuid); +} diff --git a/modules-available/runmode/inc/runmode.inc.php b/modules-available/runmode/inc/runmode.inc.php index 4d077f02..2d676cae 100644 --- a/modules-available/runmode/inc/runmode.inc.php +++ b/modules-available/runmode/inc/runmode.inc.php @@ -12,36 +12,36 @@ class RunMode * Get runmode config for a specific module * * @param string $module name of module - * @return \RunModeModuleConfig|false config, false if moudles doesn't support run modes + * @return ?RunModeModuleConfig config, null if module doesn't support run modes */ - public static function getModuleConfig($module) + public static function getModuleConfig(string $module): ?RunModeModuleConfig { if (isset(self::$moduleConfigs[$module])) return self::$moduleConfigs[$module]; if (Module::get($module) === false) - return false; + return null; $file = 'modules/' . $module . '/hooks/runmode/config.json'; if (!file_exists($file)) - return false; + return null; return (self::$moduleConfigs[$module] = new RunModeModuleConfig($file)); } /** - * @param string $machineuuid * @param string|\Module $moduleId * @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/deleted */ - public static function setRunMode($machineuuid, $moduleId, $modeId, $modeData = null, $isClient = null) + public static function setRunMode(string $machineuuid, $moduleId, ?string $modeId, + ?string $modeData = null, ?bool $isClient = null): bool { if (is_object($moduleId)) { $moduleId = $moduleId->getIdentifier(); } // - Check if machine exists $machine = Statistics::getMachine($machineuuid, Machine::NO_DATA); - if ($machine === false) + if ($machine === null) return false; // - Delete entry if mode is null if ($modeId === null) { @@ -72,25 +72,21 @@ class RunMode * Change the isClient flag for existing client. * @param string $machineUuid existing machine with some runmode * @param string $moduleId module that assigned the current runmode of that client - * @param bool $isClient + * @param bool $isClient should this machine be considered a normal client? */ - public static function updateClientFlag($machineUuid, $moduleId, $isClient) + public static function updateClientFlag(string $machineUuid, string $moduleId, bool $isClient): void { Database::exec('UPDATE runmode SET isclient = :isclient WHERE machineuuid = :uuid AND module = :module', ['uuid' => $machineUuid, 'module' => $moduleId, 'isclient' => ($isClient ? 1 : 0)]); } /** - * @param string $machineuuid * @param int $returnData bitfield of data to return * @return false|array {'machineuuid', 'isclient', 'module', 'modeid', 'modedata', * ('hostname', 'clientip', 'macaddr', 'locationid', 'lastseen'), ('moduleName', 'modeName')} */ - public static function getRunMode($machineuuid, $returnData = self::DATA_MACHINE_DATA) + public static function getRunMode(string $machineuuid, int $returnData = self::DATA_MACHINE_DATA) { - 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'; @@ -127,11 +123,11 @@ class RunMode } /** - * @param string|\Module $module - * @param bool true = wrap in array where key is modeid - * @return array key=machineuuid, value={'machineuuid', 'modeid', 'modedata'} + * @param string|Module $module + * @param bool $groupByModeId true = wrap in array where key is modeid + * @return array - format depending on $groupByModeId */ - public static function getForModule($module, $groupByModeId = false) + public static function getForModule($module, bool $groupByModeId = false): array { if (is_object($module)) { $module = $module->getIdentifier(); @@ -139,7 +135,7 @@ class RunMode $res = Database::simpleQuery('SELECT machineuuid, modeid, modedata FROM runmode WHERE module = :module', compact('module')); $ret = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if ($groupByModeId) { if (!isset($ret[$row['modeid']])) { $ret[$row['modeid']] = array(); @@ -153,14 +149,14 @@ class RunMode } /** - * @param string|\Module $module - * @param string $modeId + * @param string|Module $module Module the mode belongs to + * @param string $modeId module-specific runmode identifier * @param bool $detailed whether to return meta data about machine, not just machineuuid * @param bool $assoc use machineuuid as array key * @return array <key=machineuuid>, value={'machineuuid', 'modedata', * <'hostname', 'clientip', 'macaddr', 'locationid', 'lastseen'>} */ - public static function getForMode($module, $modeId, $detailed = false, $assoc = false) + public static function getForMode($module, string $modeId, bool $detailed = false, bool $assoc = false): array { if (is_object($module)) { $module = $module->getIdentifier(); @@ -177,7 +173,7 @@ class RunMode WHERE module = :module AND modeid = :modeId", compact('module', 'modeId')); $ret = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if ($detailed && empty($row['hostname'])) { $row['hostname'] = $row['clientip']; } @@ -192,11 +188,12 @@ 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 + * @param bool|null $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) + public static function getAllClients(bool $withData = false, bool $isClient = null): array { $xtra = ''; if ($withData) { @@ -204,7 +201,7 @@ class RunMode } $res = Database::simpleQuery("SELECT machineuuid, module, modeid, isclient $xtra FROM runmode"); $ret = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if (!is_null($isClient) && ($row['isclient'] != 0) !== $isClient) continue; $ret[$row['machineuuid']] = $row; @@ -218,11 +215,11 @@ class RunMode * that method is passed through. getModeName by contract should return false if * the module doesn't think the given modeId exists. * - * @param string|\Module $module - * @param string $modeId - * @return string|bool mode name if known, modeId as fallback, or false if mode is not known by module + * @param string|Module $module module the runmode belongs to + * @param string $modeId module-specific runmode identifier + * @return string|false mode name if known, modeId as fallback, or false if mode is not known by module */ - public static function getModeName($module, $modeId) + public static function getModeName($module, string $modeId) { if (is_object($module)) { $module = $module->getIdentifier(); @@ -236,10 +233,10 @@ class RunMode /** * Delete given runmode. * - * @param string|\Module $module Module runmode belongs to + * @param string|Module $module Module runmode belongs to * @param string $modeId run mode id */ - public static function deleteMode($module, $modeId) + public static function deleteMode($module, string $modeId): void { if (is_object($module)) { $module = $module->getIdentifier(); @@ -297,7 +294,7 @@ class RunModeModuleConfig */ public $permission = false; - public function __construct($file) + public function __construct(string $file) { $data = json_decode(file_get_contents($file), true); if (!is_array($data)) @@ -313,19 +310,16 @@ class RunModeModuleConfig $this->loadType($data, 'permission', 'string'); } - private function loadType($data, $key, $type) + private function loadType(array $data, string $key, string $type): void { if (!isset($data[$key])) - return false; - if (is_string($type) && gettype($data[$key]) !== $type) - return false; - if (is_array($type) && !in_array(gettype($data[$key]), $type)) - return false; + return; + if (gettype($data[$key]) !== $type) + return; $this->{$key} = $data[$key]; - return true; } - public function userHasPermission($locationId) + public function userHasPermission(?int $locationId): bool { return $this->permission === false || User::hasPermission($this->permission, $locationId); } diff --git a/modules-available/runmode/page.inc.php b/modules-available/runmode/page.inc.php index 0b6dfa02..5654456a 100644 --- a/modules-available/runmode/page.inc.php +++ b/modules-available/runmode/page.inc.php @@ -30,7 +30,7 @@ class Page_RunMode extends Page $module = Request::post('module', false, 'string'); $modeId = Request::post('modeid', false, 'string'); $modConfig = RunMode::getModuleConfig($module); - if ($modConfig === false) { + if ($modConfig === null) { Message::addError('runmode.module-hasnt-runmode', $module); return; } @@ -70,7 +70,7 @@ class Page_RunMode extends Page if ($oldMachineMode !== false) { $machineLocation = $oldMachineMode['locationid']; $oldModule = RunMode::getModuleConfig($oldMachineMode['module']); - if ($oldModule !== false) { + if ($oldModule !== null) { if ($oldMachineMode['module'] !== $module || $oldMachineMode['modeid'] !== $modeId) { if (!$oldModule->allowGenericEditor || $oldModule->deleteUrlSnippet !== false) { Message::addError('runmode.machine-still-assigned', $machine, $oldMachineMode['module']); @@ -87,7 +87,7 @@ class Page_RunMode extends Page } else { // Not existing, no old mode - query machine to get location, so we can do a perm-check for new loc $m = Statistics::getMachine($machine, Machine::NO_DATA); - if ($m !== false) { + if ($m !== null) { $machineLocation = $m->locationid; } } @@ -134,7 +134,7 @@ class Page_RunMode extends Page return; } $modConfig = RunMode::getModuleConfig($mode['module']); - if ($modConfig === false) { + if ($modConfig === null) { Message::addError('module-hasnt-runmode', $mode['moduleName']); return; } @@ -172,7 +172,7 @@ class Page_RunMode extends Page } $module->activate(1, false); $config = RunMode::getModuleConfig($moduleId); - if ($config === false) { + if ($config === null) { Message::addError('module-hasnt-runmode', $moduleId); Util::redirect('?do=runmode'); } @@ -187,7 +187,6 @@ class Page_RunMode extends Page if (!$config->userHasPermission(null) && !User::hasPermission('list-all')) { Message::addError('main.no-permission'); Util::redirect('?do=runmode'); - return; } // Show list of machines with assigned mode for this module $this->renderClientList($moduleId); @@ -198,19 +197,24 @@ class Page_RunMode extends Page { if ($onlyModule === false) { $where = ''; + $args = []; } else { $where = ' AND r.module = :moduleId '; + $args = ['moduleId' => $onlyModule]; } $res = Database::simpleQuery("SELECT m.machineuuid, m.hostname, m.clientip, r.module, r.modeid, r.isclient" . " FROM runmode r" . " INNER JOIN machine m ON (m.machineuuid = r.machineuuid $where )" - . " ORDER BY m.hostname ASC, m.clientip ASC", array('moduleId' => $onlyModule)); + . " ORDER BY m.hostname ASC, m.clientip ASC", $args); $modules = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if (!isset($modules[$row['module']])) { if (!Module::isAvailable($row['module'])) continue; - $modules[$row['module']] = array('config' => RunMode::getModuleConfig($row['module']), 'list' => array()); + $config = RunMode::getModuleConfig($row['module']); + if ($config === null) + continue; + $modules[$row['module']] = array('config' => $config, 'list' => array()); } if (empty($row['hostname'])) { $row['hostname'] = $row['clientip']; @@ -220,16 +224,20 @@ class Page_RunMode extends Page } $modules[$row['module']]['list'][] = $row; } + $anythingOk = false; foreach ($modules as $moduleId => $rows) { $disabled = ''; if ($onlyModule === false) { // Permissions - not required if rendering specific module, since it's been already done + // in $this->renderModule() + /** @var array{config: RunModeModuleConfig} $rows */ if (!$rows['config']->userHasPermission(null)) { if (!User::hasPermission('list-all')) continue; $disabled = 'disabled'; } // </Permissions> } + $anythingOk = true; $module = Module::get($moduleId); if ($module === false) continue; @@ -238,19 +246,17 @@ class Page_RunMode extends Page 'list' => $rows['list'], 'modulename' => $module->getDisplayName(), 'module' => $moduleId, - 'canedit' => $config !== false && $config->allowGenericEditor && $config->deleteUrlSnippet === false, + 'canedit' => $config !== null && $config->allowGenericEditor && $config->deleteUrlSnippet === false, 'deleteUrl' => $config->deleteUrlSnippet, 'disabled' => $disabled, )); } + if (!empty($modules) && !$anythingOk) { + User::assertPermission('list-all'); + } } - /** - * @param \Module $module - * @param string $modeId - * @param \RunModeModuleConfig $config - */ - private function renderModuleMode($module, $modeId, $config) + private function renderModuleMode(Module $module, string $modeId, RunModeModuleConfig $config) { $moduleId = $module->getIdentifier(); $modeName = RunMode::getModeName($moduleId, $modeId); @@ -274,7 +280,6 @@ class Page_RunMode extends Page } else { Message::addError('main.no-permission'); Util::redirect('?do=runmode'); - return; } $machines = RunMode::getForMode($module, $modeId, true); if ($config->permission !== false) { @@ -308,7 +313,7 @@ class Page_RunMode extends Page $config = RunMode::getModuleConfig(Request::any('module', '', 'string')); $returnObject = ['machines' => []]; - if ($config !== false) { + if ($config !== null) { $params = ['query' => "%$query%"]; if ($config->permission === false) { // Global @@ -332,7 +337,7 @@ class Page_RunMode extends Page LIMIT 100", $params); $returnObject = [ - 'machines' => $result->fetchAll(PDO::FETCH_ASSOC) + 'machines' => $result->fetchAll() ]; } } |