<?php
class SubPage
{
public static function doPreprocess()
{
if (Request::isPost()) {
User::assertPermission('filter.mailconfigs.edit');
$action = Request::post('action');
if ($action === 'save-mailconfig') {
self::saveMailconfig();
} elseif ($action === 'delete-mailconfig') {
self::deleteMailconfig();
} else {
Message::addError('main.invalid-action', $action);
}
Util::redirect('?do=eventlog&show=mailconfigs');
}
}
private static function saveMailconfig()
{
User::assertPermission('filter.mailconfigs.edit');
$id = Request::post('id', Request::REQUIRED, 'int');
$data = [
'host' => Request::post('host', Request::REQUIRED, 'string'),
'port' => Request::post('port', Request::REQUIRED, 'int'),
'ssl' => Request::post('ssl', Request::REQUIRED, 'string'),
'senderaddress' => Request::post('senderaddress', Request::REQUIRED, 'string'),
'replyto' => Request::post('replyto', '', 'string'),
'username' => Request::post('username', '', 'string'),
'password' => Request::post('password', '', 'string'),
];
if ($id === 0) {
// NEW
Database::exec("INSERT INTO mail_config (host, port, `ssl`, senderaddress, replyto, username, password)
VALUES (:host, :port, :ssl, :senderaddress, :replyto, :username, :password)", $data);
} else {
// UPDATE
$data['configid'] = $id;
Database::exec("UPDATE mail_config SET host = :host, port = :port, `ssl` = :ssl,
senderaddress = :senderaddress, replyto = :replyto, username = :username, password = :password
WHERE configid = :configid", $data);
}
Message::addSuccess("event-mailconfig-saved", $id);
Util::redirect('?do=eventlog&show=mailconfigs');
}
private static function deleteMailconfig()
{
User::assertPermission('filter.mailconfigs.edit');
$id = Request::post('id', Request::REQUIRED, 'int');
Database::exec("DELETE FROM mail_config WHERE configid = :id", ['id' => $id]);
}
/*
*
*/
public static function doRender()
{
User::assertPermission('filter.mailconfigs.view');
$id = Request::get('id', null, 'int');
if ($id !== null) {
self::showMailconfigEditor($id);
} else {
// LIST
$data = [];
$data['configs'] = Database::queryAll('SELECT configid, host, port, `ssl`, senderaddress, replyto
FROM mail_config
ORDER BY host');
Render::addTemplate('page-filters-mailconfigs', $data);
}
}
/**
* @param int $id Config to edit. If id is 0, a new config will be created.
*/
private static function showMailconfigEditor(int $id)
{
User::assertPermission('filter.mailconfigs.edit');
if ($id !== 0) {
// EDIT
$data = Database::queryFirst('SELECT configid, host, port, `ssl`, senderaddress, replyto,
username, password
FROM mail_config
WHERE configid = :id', ['id' => $id]);
if ($data === false) {
Message::addError('invalid-mailconfig-id', $id);
Util::redirect('?do=eventlog&show=mailconfigs');
}
} else {
$data = ['configid' => 0];
}
Render::addTemplate('page-filters-edit-mailconfig', $data);
}
}