summaryrefslogblamecommitdiffstats
path: root/modules-available/usblockoff/api.inc.php
blob: 9029819d4391aa8a1ae1ade27b45c61badd082df (plain) (tree)


























































                                                                                                                                                                                                             
<?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));
}