summaryrefslogtreecommitdiffstats
path: root/modules-available/runmode/inc/runmode.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/runmode/inc/runmode.inc.php')
-rw-r--r--modules-available/runmode/inc/runmode.inc.php76
1 files changed, 35 insertions, 41 deletions
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);
}