summaryrefslogtreecommitdiffstats
path: root/modules-available/usblockoff/api.inc.php
blob: 14bc68058e20fcf1a4a31a2bb8b948246f0f50bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?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));
}