diff options
Diffstat (limited to 'modules-available/usblockoff/api.inc.php')
-rw-r--r-- | modules-available/usblockoff/api.inc.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/modules-available/usblockoff/api.inc.php b/modules-available/usblockoff/api.inc.php new file mode 100644 index 00000000..9029819d --- /dev/null +++ b/modules-available/usblockoff/api.inc.php @@ -0,0 +1,59 @@ +<?php + +HandleParameters(); + +function HandleParameters() { + $getAction = Request::get('action', 0, 'string'); + if ($getAction == "newdevice") { + $id = Request::get('id', '', 'string'); + $serial = Request::get('serial', '', 'sting'); + $name = Request::get('name', '', 'string'); + $ip = Request::get('ip', 0, 'string'); + $ruleInformation['hash'] = Request::get('hash', '', 'string'); + $ruleInformation['parent-hash'] = Request::get('parent-hash', '', 'string'); + $ruleInformation['via-port'] = Request::get('via-port', '', 'string'); + $ruleInformation['with-interface'] = Request::get('with-interface', '', 'string'); + $ruleInformation['interface-policy'] = Request::get('interface-policy', '', 'string'); + newDevice($id, $serial, $name, $ip, $ruleInformation); + } elseif ($getAction == "deletedevice") { + $serial = Request::get('serial', '', 'string'); + deleteDevice($serial); + } +} + +/** + * Adds a new USB-Device to the db. + * + * @param string $id USB-Device id. + * @param string $serial USB-Device serial number. + * @param string $name USB-Device name. + */ +function newDevice($id, $serial, $name, $ip, $ruleInformation) { + $NOW = time(); + //$machineuuid = Database::queryFirst("SELECT machineuuid, currentuser FROM machine AS m WHERE m.clientip = :ip", array('ip' => $ip)); + $client = Database::queryFirst("SELECT m.machineuuid AS 'muid', m.currentuser AS 'user' FROM machine AS m WHERE m.clientip=:ip", array('ip' => $ip)); + + // TODO: Same device when ID/Serial/Name are the same? Maybe hash value but the hash value can be different on multiple unix. + $dbresult = Database::queryFirst("SELECT uid FROM `usb_devices` WHERE id=:id AND serial=:serialnr AND name=:name", array( + 'id' => $id, 'serialnr' => $serial, 'name' => $name)); + if (empty($dbresult)) { + $dbquery = Database::exec("INSERT INTO `usb_devices` (id, serial, name, machineuuid, time, user, ruleInformation) VALUES (:id, :serialnr, + :name, :machineuuid, :now, :user, :ruleInformation)", array('id' => $id, 'serialnr' => $serial, 'name' => $name, 'machineuuid' => $client['muid'], 'now' => $NOW, + 'user' => $client['user'], 'ruleInformation' => json_encode($ruleInformation))); + echo "Successfully added"; + } else { + $dbquery = Database::exec("UPDATE `usb_devices` SET machineuuid=:machineuuid, time=:now, user=:user, ruleInformation=:ruleInformation WHERE id=:id AND serial=:serialnr AND name=:name", + array('id' => $id, 'serialnr' => $serial, 'name' => $name, 'machineuuid' => $client['muid'], 'now' => $NOW, 'user' => $client['user'], 'ruleInformation' => json_encode($ruleInformation))); + echo "Successfully updated"; + } + +} + +/** + * Deletes a device from the db given a serial number. + * + * @param string $serial USB-Device serial number. + */ +function deleteDevice($serial) { + $dbquery = Database::exec("DELETE FROM `usb_devices` WHERE serial=:serial", array('serial' => $serial)); +} |