blob: b7292357337922c150fcba6ee13746b922699c4a [file] [log] [blame]
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001<?php
2function customize($_action, $_item, $_data = null) {
3 global $redis;
4 global $lang;
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01005 global $LOGO_LIMITS;
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01006
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01007 switch ($_action) {
8 case 'add':
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +01009 // disable functionality when demo mode is enabled
10 if ($GLOBALS["DEMO_MODE"]) {
11 $_SESSION['return'][] = array(
12 'type' => 'danger',
13 'log' => array(__FUNCTION__, $_action, $_item, $_data),
14 'msg' => 'demo_mode_enabled'
15 );
16 return false;
17 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010018 if ($_SESSION['mailcow_cc_role'] != "admin") {
19 $_SESSION['return'][] = array(
20 'type' => 'danger',
21 'log' => array(__FUNCTION__, $_action, $_item, $_data),
22 'msg' => 'access_denied'
23 );
24 return false;
25 }
26 switch ($_item) {
27 case 'main_logo':
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010028 case 'main_logo_dark':
29 if (in_array($_data[$_item]['type'], array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png', 'image/svg+xml'))) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010030 try {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010031 if (file_exists($_data[$_item]['tmp_name']) !== true) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010032 $_SESSION['return'][] = array(
33 'type' => 'danger',
34 'log' => array(__FUNCTION__, $_action, $_item, $_data),
35 'msg' => 'img_tmp_missing'
36 );
37 return false;
38 }
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +010039 if ($_data[$_item]['size'] > $LOGO_LIMITS['max_size']) {
40 $_SESSION['return'][] = array(
41 'type' => 'danger',
42 'log' => array(__FUNCTION__, $_action, $_item, $_data),
43 'msg' => 'img_size_exceeded'
44 );
45 return false;
46 }
47 list($width, $height) = getimagesize($_data[$_item]['tmp_name']);
48 if ($width > $LOGO_LIMITS['max_width'] || $height > $LOGO_LIMITS['max_height']) {
49 $_SESSION['return'][] = array(
50 'type' => 'danger',
51 'log' => array(__FUNCTION__, $_action, $_item, $_data),
52 'msg' => 'img_dimensions_exceeded'
53 );
54 return false;
55 }
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010056 $image = new Imagick($_data[$_item]['tmp_name']);
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010057 if ($image->valid() !== true) {
58 $_SESSION['return'][] = array(
59 'type' => 'danger',
60 'log' => array(__FUNCTION__, $_action, $_item, $_data),
61 'msg' => 'img_invalid'
62 );
63 return false;
64 }
65 $image->destroy();
66 }
67 catch (ImagickException $e) {
68 $_SESSION['return'][] = array(
69 'type' => 'danger',
70 'log' => array(__FUNCTION__, $_action, $_item, $_data),
71 'msg' => 'img_invalid'
72 );
73 return false;
74 }
75 }
76 else {
77 $_SESSION['return'][] = array(
78 'type' => 'danger',
79 'log' => array(__FUNCTION__, $_action, $_item, $_data),
80 'msg' => 'invalid_mime_type'
81 );
82 return false;
83 }
84 try {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +010085 $redis->Set(strtoupper($_item), 'data:' . $_data[$_item]['type'] . ';base64,' . base64_encode(file_get_contents($_data[$_item]['tmp_name'])));
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010086 }
87 catch (RedisException $e) {
88 $_SESSION['return'][] = array(
89 'type' => 'danger',
90 'log' => array(__FUNCTION__, $_action, $_item, $_data),
91 'msg' => array('redis_error', $e)
92 );
93 return false;
94 }
95 $_SESSION['return'][] = array(
96 'type' => 'success',
97 'log' => array(__FUNCTION__, $_action, $_item, $_data),
98 'msg' => 'upload_success'
99 );
100 break;
101 }
102 break;
103 case 'edit':
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100104 // disable functionality when demo mode is enabled
105 if ($GLOBALS["DEMO_MODE"]) {
106 $_SESSION['return'][] = array(
107 'type' => 'danger',
108 'log' => array(__FUNCTION__, $_action, $_item, $_data),
109 'msg' => 'demo_mode_enabled'
110 );
111 return false;
112 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100113 if ($_SESSION['mailcow_cc_role'] != "admin") {
114 $_SESSION['return'][] = array(
115 'type' => 'danger',
116 'log' => array(__FUNCTION__, $_action, $_item, $_data),
117 'msg' => 'access_denied'
118 );
119 return false;
120 }
121 switch ($_item) {
122 case 'app_links':
123 $apps = (array)$_data['app'];
124 $links = (array)$_data['href'];
125 $out = array();
126 if (count($apps) == count($links)) {
127 for ($i = 0; $i < count($apps); $i++) {
128 $out[] = array($apps[$i] => $links[$i]);
129 }
130 try {
131 $redis->set('APP_LINKS', json_encode($out));
132 }
133 catch (RedisException $e) {
134 $_SESSION['return'][] = array(
135 'type' => 'danger',
136 'log' => array(__FUNCTION__, $_action, $_item, $_data),
137 'msg' => array('redis_error', $e)
138 );
139 return false;
140 }
141 }
142 $_SESSION['return'][] = array(
143 'type' => 'success',
144 'log' => array(__FUNCTION__, $_action, $_item, $_data),
145 'msg' => 'app_links'
146 );
147 break;
148 case 'ui_texts':
149 $title_name = $_data['title_name'];
150 $main_name = $_data['main_name'];
151 $apps_name = $_data['apps_name'];
152 $help_text = $_data['help_text'];
153 $ui_footer = $_data['ui_footer'];
154 $ui_announcement_text = $_data['ui_announcement_text'];
155 $ui_announcement_type = (in_array($_data['ui_announcement_type'], array('info', 'warning', 'danger'))) ? $_data['ui_announcement_type'] : false;
156 $ui_announcement_active = (!empty($_data['ui_announcement_active']) ? 1 : 0);
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100157
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100158 try {
159 $redis->set('TITLE_NAME', htmlspecialchars($title_name));
160 $redis->set('MAIN_NAME', htmlspecialchars($main_name));
161 $redis->set('APPS_NAME', htmlspecialchars($apps_name));
162 $redis->set('HELP_TEXT', $help_text);
163 $redis->set('UI_FOOTER', $ui_footer);
164 $redis->set('UI_ANNOUNCEMENT_TEXT', $ui_announcement_text);
165 $redis->set('UI_ANNOUNCEMENT_TYPE', $ui_announcement_type);
166 $redis->set('UI_ANNOUNCEMENT_ACTIVE', $ui_announcement_active);
167 }
168 catch (RedisException $e) {
169 $_SESSION['return'][] = array(
170 'type' => 'danger',
171 'log' => array(__FUNCTION__, $_action, $_item, $_data),
172 'msg' => array('redis_error', $e)
173 );
174 return false;
175 }
176 $_SESSION['return'][] = array(
177 'type' => 'success',
178 'log' => array(__FUNCTION__, $_action, $_item, $_data),
179 'msg' => 'ui_texts'
180 );
181 break;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100182 case 'ip_check':
183 $ip_check = ($_data['ip_check_opt_in'] == "1") ? 1 : 0;
184 try {
185 $redis->set('IP_CHECK', $ip_check);
186 }
187 catch (RedisException $e) {
188 $_SESSION['return'][] = array(
189 'type' => 'danger',
190 'log' => array(__FUNCTION__, $_action, $_item, $_data),
191 'msg' => array('redis_error', $e)
192 );
193 return false;
194 }
195 $_SESSION['return'][] = array(
196 'type' => 'success',
197 'log' => array(__FUNCTION__, $_action, $_item, $_data),
198 'msg' => 'ip_check_opt_in_modified'
199 );
200 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100201 }
202 break;
203 case 'delete':
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100204 // disable functionality when demo mode is enabled
205 if ($GLOBALS["DEMO_MODE"]) {
206 $_SESSION['return'][] = array(
207 'type' => 'danger',
208 'log' => array(__FUNCTION__, $_action, $_item, $_data),
209 'msg' => 'demo_mode_enabled'
210 );
211 return false;
212 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100213 if ($_SESSION['mailcow_cc_role'] != "admin") {
214 $_SESSION['return'][] = array(
215 'type' => 'danger',
216 'log' => array(__FUNCTION__, $_action, $_item, $_data),
217 'msg' => 'access_denied'
218 );
219 return false;
220 }
221 switch ($_item) {
222 case 'main_logo':
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100223 case 'main_logo_dark':
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100224 try {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100225 if ($redis->del(strtoupper($_item))) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100226 $_SESSION['return'][] = array(
227 'type' => 'success',
228 'log' => array(__FUNCTION__, $_action, $_item, $_data),
229 'msg' => 'reset_main_logo'
230 );
231 return true;
232 }
233 }
234 catch (RedisException $e) {
235 $_SESSION['return'][] = array(
236 'type' => 'danger',
237 'log' => array(__FUNCTION__, $_action, $_item, $_data),
238 'msg' => array('redis_error', $e)
239 );
240 return false;
241 }
242 break;
243 }
244 break;
245 case 'get':
246 switch ($_item) {
247 case 'app_links':
248 try {
249 $app_links = json_decode($redis->get('APP_LINKS'), true);
250 }
251 catch (RedisException $e) {
252 $_SESSION['return'][] = array(
253 'type' => 'danger',
254 'log' => array(__FUNCTION__, $_action, $_item, $_data),
255 'msg' => array('redis_error', $e)
256 );
257 return false;
258 }
259 return ($app_links) ? $app_links : false;
260 break;
261 case 'main_logo':
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100262 case 'main_logo_dark':
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100263 try {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100264 return $redis->get(strtoupper($_item));
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100265 }
266 catch (RedisException $e) {
267 $_SESSION['return'][] = array(
268 'type' => 'danger',
269 'log' => array(__FUNCTION__, $_action, $_item, $_data),
270 'msg' => array('redis_error', $e)
271 );
272 return false;
273 }
274 break;
275 case 'ui_texts':
276 try {
277 $data['title_name'] = ($title_name = $redis->get('TITLE_NAME')) ? $title_name : 'mailcow UI';
278 $data['main_name'] = ($main_name = $redis->get('MAIN_NAME')) ? $main_name : 'mailcow UI';
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200279 $data['apps_name'] = ($apps_name = $redis->get('APPS_NAME')) ? $apps_name : $lang['header']['apps'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100280 $data['help_text'] = ($help_text = $redis->get('HELP_TEXT')) ? $help_text : false;
281 if (!empty($redis->get('UI_IMPRESS'))) {
282 $redis->set('UI_FOOTER', $redis->get('UI_IMPRESS'));
283 $redis->del('UI_IMPRESS');
284 }
285 $data['ui_footer'] = ($ui_footer = $redis->get('UI_FOOTER')) ? $ui_footer : false;
286 $data['ui_announcement_text'] = ($ui_announcement_text = $redis->get('UI_ANNOUNCEMENT_TEXT')) ? $ui_announcement_text : false;
287 $data['ui_announcement_type'] = ($ui_announcement_type = $redis->get('UI_ANNOUNCEMENT_TYPE')) ? $ui_announcement_type : false;
288 $data['ui_announcement_active'] = ($redis->get('UI_ANNOUNCEMENT_ACTIVE') == 1) ? 1 : 0;
289 return $data;
290 }
291 catch (RedisException $e) {
292 $_SESSION['return'][] = array(
293 'type' => 'danger',
294 'log' => array(__FUNCTION__, $_action, $_item, $_data),
295 'msg' => array('redis_error', $e)
296 );
297 return false;
298 }
299 break;
300 case 'main_logo_specs':
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100301 case 'main_logo_dark_specs':
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100302 try {
303 $image = new Imagick();
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100304 if($_item == 'main_logo_specs') {
305 $img_data = explode('base64,', customize('get', 'main_logo'));
306 } else {
307 $img_data = explode('base64,', customize('get', 'main_logo_dark'));
308 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100309 if ($img_data[1]) {
310 $image->readImageBlob(base64_decode($img_data[1]));
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100311 return $image->identifyImage();
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100312 }
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100313 return false;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100314 }
315 catch (ImagickException $e) {
316 $_SESSION['return'][] = array(
317 'type' => 'danger',
318 'log' => array(__FUNCTION__, $_action, $_item, $_data),
319 'msg' => 'imagick_exception'
320 );
321 return false;
322 }
323 break;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100324 case 'ip_check':
325 try {
326 $ip_check = ($ip_check = $redis->get('IP_CHECK')) ? $ip_check : 0;
327 return $ip_check;
328 }
329 catch (RedisException $e) {
330 $_SESSION['return'][] = array(
331 'type' => 'danger',
332 'log' => array(__FUNCTION__, $_action, $_item, $_data),
333 'msg' => array('redis_error', $e)
334 );
335 return false;
336 }
337 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100338 }
339 break;
340 }
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100341}