summaryrefslogtreecommitdiffstats
path: root/modules-available/eventlog/pages/mailconfigs.inc.php
blob: 0ba711042e15c5e7a24a83df9b315f88fb11dba0 (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
<?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);
	}

}