<?php
class SubPage
{
public static function doPreprocess()
{
if (Request::isPost()) {
User::assertPermission('filter.transport.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§ion=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.transport.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.transport.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§ion=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);
}
}