summaryrefslogtreecommitdiffstats
path: root/modules-available/remoteaccess/page.inc.php
blob: 2877fc9d09e8b99689d068b08caf77ec68132879 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

/*
 * TODO TODO TODO TODO
 * ### PERMISSIONS ###
 * TODO TODO TODO TODO
 */

class Page_RemoteAccess extends Page
{

	protected function doPreprocess()
	{
		User::load();
		if (!User::isLoggedIn()) {
			Message::addError('main.no-permission');
			Util::redirect('?do=Main');
		}
		$action = Request::post('action', false, 'string');
		// Add group adds a DB row and then falls through to regular saving
		if ($action === 'add-group') {
			Database::exec("INSERT INTO remoteaccess_group (groupname, wolcount, passwd, active)
				VALUES ('.new', 0, '', 0)");
			$action = 'save-settings';
			Message::addSuccess('group-added');
		}
		if ($action === 'save-settings') {
			$groups = Request::post('group', [], 'array');
			foreach ($groups as $id => $group) {
				Database::exec("UPDATE remoteaccess_group SET groupname = :name, wolcount = :wol,
                              passwd = :passwd, active = :active WHERE groupid = :id", [
					'id' => $id,
					'name' => isset($group['groupname']) ? $group['groupname'] : $id,
					'wol' => isset($group['wolcount']) ? $group['wolcount'] : 0,
					'passwd' => isset($group['passwd']) ? $group['passwd'] : 0,
					'active' => isset($group['active']) && $group['active'] ? 1 : 0,
				]);
			}
			Property::set(RemoteAccess::PROP_ALLOWED_VNC_NET, Request::post('allowed-source', '', 'string'));
			Property::set(RemoteAccess::PROP_TRY_VIRT_HANDOVER, Request::post('virt-handover', false, 'int'));
			Message::addSuccess('settings-saved');
		} elseif ($action === 'set-locations') {
			$groupid = Request::post('groupid', Request::REQUIRED, 'int');
			$group = Database::queryFirst("SELECT groupname FROM remoteaccess_group WHERE groupid = :id",
				['id' => $groupid]);
			if ($group === false) {
				Message::addError('group-not-found', $groupid);
				Util::redirect('?do=remoteaccess');
			}
			$locations = array_values(Request::post('location', [], 'array'));
			if (empty($locations)) {
				Database::exec("DELETE FROM remoteaccess_x_location WHERE groupid = :id", ['id' => $groupid]);
			} else {
				Database::exec("INSERT IGNORE INTO remoteaccess_x_location (groupid, locationid)
					VALUES :values", ['values' => array_map(function($item) use ($groupid) { return [$groupid, $item]; }, $locations)]);
				Database::exec("DELETE FROM remoteaccess_x_location WHERE groupid = :id AND locationid NOT IN (:locations)",
						['id' => $groupid, 'locations' => $locations]);
			}
			Message::addSuccess('group-updated', $group['groupname']);
		}
		if (Request::isPost()) {
			Util::redirect('?do=remoteaccess');
		}
	}

	protected function doRender()
	{
		$groupid = Request::get('groupid', false, 'int');
		if ($groupid === false) {
			// Edit list of groups and their settings
			$groups = Database::queryAll("SELECT g.groupid, g.groupname, g.wolcount, g.passwd,
       			Count(l.locationid) AS locs, If(g.active, 'checked', '') AS checked
					FROM remoteaccess_group g LEFT JOIN remoteaccess_x_location l USING (groupid)
					GROUP BY g.groupid, g.groupname
					ORDER BY g.groupname ASC");
			$data = [
				'allowed-source' => Property::get(RemoteAccess::PROP_ALLOWED_VNC_NET),
				'virt-handover_checked' => Property::get(RemoteAccess::PROP_TRY_VIRT_HANDOVER) ? 'checked' : '',
				'groups' => $groups,
			];
			Render::addTemplate('edit-settings', $data);
		} else {
			// Edit locations for group
			$group = Database::queryFirst("SELECT groupid, groupname FROM remoteaccess_group WHERE groupid = :id",
				['id' => $groupid]);
			if ($group === false) {
				Message::addError('group-not-found', $groupid);
				return;
			}
			$locationList = Location::getLocationsAssoc();
			$enabled = RemoteAccess::getEnabledLocations($groupid);
			foreach ($enabled as $lid) {
				if (isset($locationList[$lid])) {
					$locationList[$lid]['checked'] = 'checked';
				}
			}
			Render::addTemplate('edit-group', $group + ['locations' => array_values($locationList)]);
		}
	}

}