<?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');
$client = Database::queryFirst("SELECT m.machineuuid AS 'muid', m.currentuser AS 'user' FROM machine AS m WHERE m.clientip=:ip", array('ip' => $ip));
// $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);
// TODO: product and vendor id necessary? It's already in the hwname part.
list($vid, $pid) = explode(':', $id);
$hwProps = array(
'vendorid' => $vid,
'productid' => $pid,
'name' => $name
);
// TODO: WITH INTERFACE in the HW table?! Should be equal for every device but not guaranteed (ODROID).
$deviceProps = array(
'hash' => Request::get('hash', '', 'string'),
'parent-hash' => Request::get('parent-hash', '', 'string'),
'via-port' => Request::get('via-port', '', 'string'),
'with-interface' => Request::get('with-interface', '', 'string'),
'interface-policy' => Request::get('interface-policy', '', 'string'),
'machineuuid' => $client['muid'],
'user' => $client['user'],
'lastseen' => time()
);
newDevice($id, $serial, $hwProps, $deviceProps);
} 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, $hwProps, $deviceProps)
{
// Add or Update the usb device in the statistic_hw table.
$hwid = (int)Database::insertIgnore('statistic_hw', 'hwid', array(
'hwtype' => DeviceType::USB,
'hwname' => $id));
// TODO: Is it okay to use the id (vendor:product) as hwname to identify a usb device?
// Add all the global prop values to the statistics_hw_prop table.
// productid, vendorid, name, interfaces
// TODO:
addHwProps('statistic_hw_prop', $hwid, $hwProps);
// Add the hwid -> serial in the usblockoff_hw table if not already existent.
$dbquery2 = Database::queryFirst("Select * FROM `usblockoff_hw` WHERE hwid=:hwid AND serial=:serial", array(
'hwid' => $hwid,
'serial' => $serial));
if (empty($dbquery2)) {
Database::exec("INSERT INTO `usblockoff_hw` (hwid, serial) VALUES (:hwid, :serial)", array(
'hwid' => $hwid,
'serial' => $serial
));
}
// Add all the prop values to the usblockoff_hw_prop table.
// PROP: serial, machineuuid, time, user, ruleInformation, Port, hash, interface-policy
addUSBHwProps('usblockoff_hw_prop', $hwid, $serial, $deviceProps);
echo "Successfully added";
}
function addHwProps($table, $hwid, $propArray) {
foreach ($propArray as $prop => $value) {
if (empty($value)) {
continue;
}
Database::exec("INSERT INTO " . $table . " (hwid, prop, value) VALUES (:hwid, :prop, :value) ON DUPLICATE KEY UPDATE value=:value", array(
'hwid' => $hwid,
'prop' => $prop,
'value' => $value
));
}
}
function addUSBHwProps($table, $hwid, $serial, $propArray) {
foreach ($propArray as $prop => $value) {
if (empty($value)) {
continue;
}
Database::exec("INSERT INTO " . $table . " (hwid, serial, prop, value) VALUES (:hwid, :serial, :prop, :value) ON DUPLICATE KEY UPDATE value=:value", array(
'hwid' => $hwid,
'serial' => $serial,
'prop' => $prop,
'value' => $value
));
}
}
/**
* 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.
*/
/* VERSION WITH OLD DB ---------------------------------------------------------------------------------------
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.
*/
// TODO: Edit for the new db struct.
function deleteDevice($serial)
{
$dbquery = Database::exec("DELETE FROM `usb_devices` WHERE serial=:serial", array('serial' => $serial));
}