summaryrefslogtreecommitdiffstats
path: root/modules-available/eventlog/pages/transports.inc.php
blob: 439c650cdb12365b9692de9fb0025bf8c08098c3 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php

class SubPage
{

	public static function doPreprocess()
	{
		if (Request::isPost()) {
			User::assertPermission('filter.transports.edit');
			$action = Request::post('action');
			if ($action === 'save-transport') {
				self::saveTransport();
			} elseif ($action === 'delete-transport') {
				self::deleteTransport();
			} else {
				Message::addError('main.invalid-action', $action);
			}
			Util::redirect('?do=eventlog&show=transports');
		}
	}

	private static function saveTransport()
	{
		User::assertPermission('filter.transports.edit');
		$id = Request::post('id', Request::REQUIRED, 'int');
		$rules = Request::post('rules', [], 'array');
		static $types = [
			'type' => [Request::REQUIRED, 'string', ['mail', 'irc', 'http', 'group']],
			'mail-config-id' => [0, 'int'],
			'mail-users' => [[], 'int[]'],
			'mail-extra-mails' => ['', 'string'],
			'irc-server' => ['', 'string'],
			'irc-server-password' => ['', 'string'],
			'irc-target' => ['', 'string'],
			'irc-nickname' => ['', 'string'],
			'http-uri' => ['', 'string'],
			'http-method' => ['', 'string', ['GET', 'POST']],
			'http-post-field' => ['', 'string'],
			'http-post-format' => ['', 'string', ['FORM', 'JSON', 'JSON_AUTO']],
			'group-list' => [[], 'int[]'],
		];
		$data = [];
		foreach ($types as $key => $def) {
			if (substr($def[1], -1) === ']') {
				$type = substr($def[1], 0, -2);
				$array = true;
			} else {
				$type = $def[1];
				$array = false;
			}
			if ($array) {
				$value = Request::post($key, [], 'array');
				foreach ($value as &$v) {
					settype($v, $type);
					if (isset($def[2]) && !in_array($v, $def[2])) {
						Message::addWarning('main.value-invalid', $key, $v);
					}
				}
			} else {
				$value = Request::post($key, $def[0], $type);
				if (isset($def[2]) && !in_array($value, $def[2])) {
					Message::addWarning('main.value-invalid', $key, $value);
				}
			}
			$data[$key] = $value;
		}
		//die(print_r($data));
		$params = [
			'title' => Request::post('title', 'Backend', 'string'),
			'description' => Request::post('description', '', 'string'),
			'data' => json_encode($data),
		];
		if ($id === 0) {
			$res = Database::exec("INSERT INTO notification_backend (title, description, data)
					VALUES (:title, :description, :data)", $params);
			$id = Database::lastInsertId();
		} else {
			$params['transportid'] = $id;
			$res = Database::exec("UPDATE notification_backend 
					SET title = :title, description = :description, data = :data
					WHERE transportid = :transportid", $params);
		}
		if (empty($rules)) {
			Database::exec("DELETE FROM notification_rule_x_transport WHERE transportid = :id", ['id' => $id]);
		} else {
			Database::exec("DELETE FROM notification_rule_x_transport
					WHERE transportid = :id AND ruleid NOT IN (:rules)",
				['id' => $id, 'rules' => $rules]);
			Database::exec("INSERT IGNORE INTO notification_rule_x_transport (ruleid, transportid)
					VALUES :list", ['list' => array_map(function ($i) use ($id) { return [$i, $id]; }, $rules)]);
		}
		if ($res > 0) {
			Message::addSuccess('transport-saved', $id);
		}
		Util::redirect('?do=eventlog&show=transports&section=transports');
	}

	private static function deleteTransport()
	{
		User::assertPermission('filter.transports.edit');
		$id = Request::post('id', Request::REQUIRED, 'int');
		Database::exec("DELETE FROM notification_backend WHERE transportid = :id", ['id' => $id]);
	}

	/*
	 *
	 */

	public static function doRender()
	{
		User::assertPermission('filter.transports.view');
		$id = Request::get('id', null, 'int');
		if ($id !== null) {
			self::showTransportEditor($id);
		} else {
			// LIST
			$data = [];
			$data['transports'] = [];
			foreach (Database::queryAll('SELECT transportid, title, data,
       				Count(ruleid) AS useCount
					FROM notification_backend nb
					LEFT JOIN notification_rule_x_transport sfxb USING (transportid)
					GROUP BY transportid, title
					ORDER BY title, transportid') as $transport) {
				$json = json_decode($transport['data'], true);
				$transport['type'] = $json['type'];
				$transport['details'] = NotificationTransport::getInstance($json);
				$data['transports'][] = $transport;
			}
			Render::addTemplate('page-filters-transports', $data);
		}
	}

	/**
	 * @param int $id Transport to edit, 0 to create a new one
	 */
	private static function showTransportEditor(int $id)
	{
		User::assertPermission('filter.transports.edit');
		if ($id !== 0) {
			$entry = Database::queryFirst('SELECT transportid, title, description, data
				FROM notification_backend
				WHERE transportid = :id', ['id' => $id]);
			if ($entry === false) {
				Message::addError('invalid-transport-id', $id);
				Util::redirect('?do=eventlog&show=transports&section=transports');
			}
			$entry['data'] = json_decode($entry['data'], true);
			$entry[($entry['data']['type'] ?? '') . '_selected'] = 'selected';
			$entry[($entry['data']['http-method'] ?? '') . '_selected'] = 'selected';
			$entry[($entry['data']['http-post-format'] ?? '') . '_selected'] = 'selected';
		} else {
			$entry = ['transportid' => $id, 'data' => [], 'backends' => []];
		}
		$entry['users'] = [];
		foreach (Database::queryAll("SELECT userid, login, fullname, email FROM user ORDER BY login") as $row) {
			$row['disabled'] = strpos($row['email'], '@') ? '' : 'disabled';
			$row['selected'] = in_array($row['userid'], $entry['data']['mail-users'] ?? []) ? 'selected' : '';
			$entry['users'][] = $row;
		}
		$entry['mailconfigs'] = [];
		foreach (Database::queryAll("SELECT configid, host, port, senderaddress FROM mail_config") as $row) {
			$row['selected'] = $row['configid'] == ($entry['data']['mail-config-id'] ?? []) ? 'selected' : '';
			$entry['mailconfigs'][] = $row;
		}
		foreach (Database::queryAll("SELECT transportid, title FROM notification_backend") as $row) {
			$row['selected'] = in_array($row['transportid'], ($entry['data']['group-list'] ?? [])) ? 'selected' : '';
			$entry['backends'][] = $row;
		}
		Module::isAvailable('bootstrap_multiselect');
		$entry['rules'] = Database::queryAll("SELECT sf.ruleid, sf.title,
       		IF(sfxb.transportid IS NULL, '', 'selected') AS selected
				FROM notification_rule sf
    			LEFT JOIN notification_rule_x_transport sfxb ON (sf.ruleid = sfxb.ruleid AND sfxb.transportid = :id)",
			['id' => $id]);
		Render::addTemplate('page-filters-edit-transport', $entry);
	}

}