blob: bdc23b0836149c46688e7ba83ec02f7735c5a9f5 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2function rsettings($_action, $_data = null) {
3 global $pdo;
4 global $lang;
5 $_data_log = $_data;
6 switch ($_action) {
7 case 'add':
8 if ($_SESSION['mailcow_cc_role'] != "admin") {
9 $_SESSION['return'][] = array(
10 'type' => 'danger',
11 'log' => array(__FUNCTION__, $_action, $_data_log),
12 'msg' => 'access_denied'
13 );
14 return false;
15 }
16 $content = $_data['content'];
17 $desc = $_data['desc'];
18 $active = intval($_data['active']);
19 if (empty($content)) {
20 $_SESSION['return'][] = array(
21 'type' => 'danger',
22 'log' => array(__FUNCTION__, $_action, $_data_log),
23 'msg' => 'map_content_empty'
24 );
25 return false;
26 }
27 $stmt = $pdo->prepare("INSERT INTO `settingsmap` (`content`, `desc`, `active`)
28 VALUES (:content, :desc, :active)");
29 $stmt->execute(array(
30 ':content' => $content,
31 ':desc' => $desc,
32 ':active' => $active
33 ));
34 $_SESSION['return'][] = array(
35 'type' => 'success',
36 'log' => array(__FUNCTION__, $_action, $_data_log),
37 'msg' => 'settings_map_added'
38 );
39 break;
40 case 'edit':
41 if ($_SESSION['mailcow_cc_role'] != "admin") {
42 $_SESSION['return'][] = array(
43 'type' => 'danger',
44 'log' => array(__FUNCTION__, $_action, $_data_log),
45 'msg' => 'access_denied'
46 );
47 return false;
48 }
49 $ids = (array)$_data['id'];
50 foreach ($ids as $id) {
51 $is_now = rsettings('details', $id);
52 if (!empty($is_now)) {
53 $content = (!empty($_data['content'])) ? $_data['content'] : $is_now['content'];
54 $desc = (!empty($_data['desc'])) ? $_data['desc'] : $is_now['desc'];
55 $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
56 }
57 else {
58 $_SESSION['return'][] = array(
59 'type' => 'danger',
60 'log' => array(__FUNCTION__, $_action, $_data_log),
61 'msg' => array('settings_map_invalid', $id)
62 );
63 continue;
64 }
65 $content = trim($content);
66 $stmt = $pdo->prepare("UPDATE `settingsmap` SET
67 `content` = :content,
68 `desc` = :desc,
69 `active` = :active
70 WHERE `id` = :id");
71 $stmt->execute(array(
72 ':content' => $content,
73 ':desc' => $desc,
74 ':active' => $active,
75 ':id' => $id
76 ));
77 $_SESSION['return'][] = array(
78 'type' => 'success',
79 'log' => array(__FUNCTION__, $_action, $_data_log),
80 'msg' => array('object_modified', htmlspecialchars($ids))
81 );
82 }
83 break;
84 case 'delete':
85 if ($_SESSION['mailcow_cc_role'] != "admin") {
86 $_SESSION['return'][] = array(
87 'type' => 'danger',
88 'log' => array(__FUNCTION__, $_action, $_data_log),
89 'msg' => 'access_denied'
90 );
91 return false;
92 }
93 $ids = (array)$_data['id'];
94 foreach ($ids as $id) {
95 $stmt = $pdo->prepare("DELETE FROM `settingsmap` WHERE `id`= :id");
96 $stmt->execute(array(':id' => $id));
97 $_SESSION['return'][] = array(
98 'type' => 'success',
99 'log' => array(__FUNCTION__, $_action, $_data_log),
100 'msg' => array('settings_map_removed', htmlspecialchars($id))
101 );
102 }
103 break;
104 case 'get':
105 if ($_SESSION['mailcow_cc_role'] != "admin") {
106 return false;
107 }
108 $settingsmaps = array();
109 $stmt = $pdo->query("SELECT `id`, `desc`, `active` FROM `settingsmap`");
110 $settingsmaps = $stmt->fetchAll(PDO::FETCH_ASSOC);
111 return $settingsmaps;
112 break;
113 case 'details':
114 if ($_SESSION['mailcow_cc_role'] != "admin" || !isset($_data)) {
115 return false;
116 }
117 $settingsmapdata = array();
118 $stmt = $pdo->prepare("SELECT `id`,
119 `desc`,
120 `content`,
121 `active`
122 FROM `settingsmap`
123 WHERE `id` = :id");
124 $stmt->execute(array(':id' => $_data));
125 $settingsmapdata = $stmt->fetch(PDO::FETCH_ASSOC);
126 return $settingsmapdata;
127 break;
128 }
129}
130function rspamd_maps($_action, $_data = null) {
131 global $pdo;
132 global $lang;
133 global $RSPAMD_MAPS;
134 $_data_log = $_data;
135 switch ($_action) {
136 case 'edit':
137 if ($_SESSION['mailcow_cc_role'] != "admin") {
138 $_SESSION['return'][] = array(
139 'type' => 'danger',
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200140 'log' => array(__FUNCTION__, $_action, '-'),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100141 'msg' => 'access_denied'
142 );
143 return false;
144 }
145 $maps = (array)$_data['map'];
146 foreach ($maps as $map) {
147 foreach ($RSPAMD_MAPS as $rspamd_map_type) {
148 if (!in_array($map, $rspamd_map_type)) {
149 $_SESSION['return'][] = array(
150 'type' => 'danger',
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200151 'log' => array(__FUNCTION__, $_action, '-'),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100152 'msg' => array('global_map_invalid', $map)
153 );
154 continue;
155 }
156 }
157 try {
158 if (file_exists('/rspamd_custom_maps/' . $map)) {
159 $map_content = trim($_data['rspamd_map_data']);
160 $map_handle = fopen('/rspamd_custom_maps/' . $map, 'w');
161 if (!$map_handle) {
162 throw new Exception($lang['danger']['file_open_error']);
163 }
164 fwrite($map_handle, $map_content . PHP_EOL);
165 fclose($map_handle);
166 sleep(1.5);
167 touch('/rspamd_custom_maps/' . $map);
168 }
169 }
170 catch (Exception $e) {
171 $_SESSION['return'][] = array(
172 'type' => 'danger',
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200173 'log' => array(__FUNCTION__, $_action, '-'),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100174 'msg' => array('global_map_write_error', htmlspecialchars($map), htmlspecialchars($e->getMessage()))
175 );
176 continue;
177 }
178 $_SESSION['return'][] = array(
179 'type' => 'success',
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200180 'log' => array(__FUNCTION__, $_action, '-'),
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100181 'msg' => array('object_modified', htmlspecialchars($map))
182 );
183 }
184 break;
185 }
186}
187function rspamd_actions() {
188 if (isset($_SESSION["mailcow_cc_role"]) && $_SESSION["mailcow_cc_role"] == "admin") {
189 $curl = curl_init();
190 curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
191 curl_setopt($curl, CURLOPT_URL,"http://rspamd/stat");
192 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
193 $data = curl_exec($curl);
194 if ($data) {
195 $return = array();
196 $stats_array = json_decode($data, true)['actions'];
197 $stats_array['soft reject'] = $stats_array['soft reject'] + $stats_array['greylist'];
198 unset($stats_array['greylist']);
199 foreach ($stats_array as $action => $count) {
200 $return[] = array($action, $count);
201 }
202 return $return;
203 }
204 else {
205 return false;
206 }
207 }
208 else {
209 return false;
210 }
211}