blob: 49a098ef348dc50e9ae581662c527f5f8cfdcd5e [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 Benkard97f12022026-10-23 08:46:40 +02006
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'];
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200125 $user_links = (array)$_data['user_href'];
126 $hide = (array)$_data['hide'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100127 $out = array();
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200128 if (count($apps) == count($links) && count($apps) == count($user_links) && count($apps) == count($hide)) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100129 for ($i = 0; $i < count($apps); $i++) {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200130 $out[] = array($apps[$i] => array(
131 'link' => $links[$i],
132 'user_link' => $user_links[$i],
133 'hide' => ($hide[$i] === '0' || $hide[$i] === 0) ? false : true
134 ));
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100135 }
136 try {
137 $redis->set('APP_LINKS', json_encode($out));
138 }
139 catch (RedisException $e) {
140 $_SESSION['return'][] = array(
141 'type' => 'danger',
142 'log' => array(__FUNCTION__, $_action, $_item, $_data),
143 'msg' => array('redis_error', $e)
144 );
145 return false;
146 }
147 }
148 $_SESSION['return'][] = array(
149 'type' => 'success',
150 'log' => array(__FUNCTION__, $_action, $_item, $_data),
151 'msg' => 'app_links'
152 );
153 break;
154 case 'ui_texts':
155 $title_name = $_data['title_name'];
156 $main_name = $_data['main_name'];
157 $apps_name = $_data['apps_name'];
158 $help_text = $_data['help_text'];
159 $ui_footer = $_data['ui_footer'];
160 $ui_announcement_text = $_data['ui_announcement_text'];
161 $ui_announcement_type = (in_array($_data['ui_announcement_type'], array('info', 'warning', 'danger'))) ? $_data['ui_announcement_type'] : false;
162 $ui_announcement_active = (!empty($_data['ui_announcement_active']) ? 1 : 0);
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100163
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100164 try {
165 $redis->set('TITLE_NAME', htmlspecialchars($title_name));
166 $redis->set('MAIN_NAME', htmlspecialchars($main_name));
167 $redis->set('APPS_NAME', htmlspecialchars($apps_name));
168 $redis->set('HELP_TEXT', $help_text);
169 $redis->set('UI_FOOTER', $ui_footer);
170 $redis->set('UI_ANNOUNCEMENT_TEXT', $ui_announcement_text);
171 $redis->set('UI_ANNOUNCEMENT_TYPE', $ui_announcement_type);
172 $redis->set('UI_ANNOUNCEMENT_ACTIVE', $ui_announcement_active);
173 }
174 catch (RedisException $e) {
175 $_SESSION['return'][] = array(
176 'type' => 'danger',
177 'log' => array(__FUNCTION__, $_action, $_item, $_data),
178 'msg' => array('redis_error', $e)
179 );
180 return false;
181 }
182 $_SESSION['return'][] = array(
183 'type' => 'success',
184 'log' => array(__FUNCTION__, $_action, $_item, $_data),
185 'msg' => 'ui_texts'
186 );
187 break;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100188 case 'ip_check':
189 $ip_check = ($_data['ip_check_opt_in'] == "1") ? 1 : 0;
190 try {
191 $redis->set('IP_CHECK', $ip_check);
192 }
193 catch (RedisException $e) {
194 $_SESSION['return'][] = array(
195 'type' => 'danger',
196 'log' => array(__FUNCTION__, $_action, $_item, $_data),
197 'msg' => array('redis_error', $e)
198 );
199 return false;
200 }
201 $_SESSION['return'][] = array(
202 'type' => 'success',
203 'log' => array(__FUNCTION__, $_action, $_item, $_data),
204 'msg' => 'ip_check_opt_in_modified'
205 );
206 break;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200207 case 'custom_login':
208 $hide_user_quicklink = ($_data['hide_user_quicklink'] == "1") ? 1 : 0;
209 $hide_domainadmin_quicklink = ($_data['hide_domainadmin_quicklink'] == "1") ? 1 : 0;
210 $hide_admin_quicklink = ($_data['hide_admin_quicklink'] == "1") ? 1 : 0;
211 $force_sso = ($_data['force_sso'] == "1") ? 1 : 0;
212
213 $custom_login = array(
214 "hide_user_quicklink" => $hide_user_quicklink,
215 "hide_domainadmin_quicklink" => $hide_domainadmin_quicklink,
216 "hide_admin_quicklink" => $hide_admin_quicklink,
217 "force_sso" => $force_sso,
218 );
219 try {
220 $redis->set('CUSTOM_LOGIN', json_encode($custom_login));
221 }
222 catch (RedisException $e) {
223 $_SESSION['return'][] = array(
224 'type' => 'danger',
225 'log' => array(__FUNCTION__, $_action, $_item, $_data),
226 'msg' => array('redis_error', $e)
227 );
228 return false;
229 }
230 $_SESSION['return'][] = array(
231 'type' => 'success',
232 'log' => array(__FUNCTION__, $_action, $_item, $_data),
233 'msg' => 'custom_login_modified'
234 );
235 break;
236 case 'ui_theme':
237 $ui_theme = is_array($_data['ui_theme']) ? (string)reset($_data['ui_theme']) : (string)$_data['ui_theme'];
238 if ($ui_theme !== '' && !array_key_exists($ui_theme, customize('get', 'ui_themes'))) {
239 $_SESSION['return'][] = array(
240 'type' => 'danger',
241 'log' => array(__FUNCTION__, $_action, $_item, $_data),
242 'msg' => 'ui_theme_invalid'
243 );
244 return false;
245 }
246 try {
247 if ($ui_theme === '') {
248 $redis->del('UI_THEME');
249 }
250 else {
251 $redis->set('UI_THEME', $ui_theme);
252 }
253 }
254 catch (RedisException $e) {
255 $_SESSION['return'][] = array(
256 'type' => 'danger',
257 'log' => array(__FUNCTION__, $_action, $_item, $_data),
258 'msg' => array('redis_error', $e)
259 );
260 return false;
261 }
262 $_SESSION['return'][] = array(
263 'type' => 'success',
264 'log' => array(__FUNCTION__, $_action, $_item, $_data),
265 'msg' => 'ui_theme_modified'
266 );
267 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100268 }
269 break;
270 case 'delete':
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100271 // disable functionality when demo mode is enabled
272 if ($GLOBALS["DEMO_MODE"]) {
273 $_SESSION['return'][] = array(
274 'type' => 'danger',
275 'log' => array(__FUNCTION__, $_action, $_item, $_data),
276 'msg' => 'demo_mode_enabled'
277 );
278 return false;
279 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100280 if ($_SESSION['mailcow_cc_role'] != "admin") {
281 $_SESSION['return'][] = array(
282 'type' => 'danger',
283 'log' => array(__FUNCTION__, $_action, $_item, $_data),
284 'msg' => 'access_denied'
285 );
286 return false;
287 }
288 switch ($_item) {
289 case 'main_logo':
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100290 case 'main_logo_dark':
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100291 try {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100292 if ($redis->del(strtoupper($_item))) {
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100293 $_SESSION['return'][] = array(
294 'type' => 'success',
295 'log' => array(__FUNCTION__, $_action, $_item, $_data),
296 'msg' => 'reset_main_logo'
297 );
298 return true;
299 }
300 }
301 catch (RedisException $e) {
302 $_SESSION['return'][] = array(
303 'type' => 'danger',
304 'log' => array(__FUNCTION__, $_action, $_item, $_data),
305 'msg' => array('redis_error', $e)
306 );
307 return false;
308 }
309 break;
310 }
311 break;
312 case 'get':
313 switch ($_item) {
314 case 'app_links':
315 try {
316 $app_links = json_decode($redis->get('APP_LINKS'), true);
317 }
318 catch (RedisException $e) {
319 $_SESSION['return'][] = array(
320 'type' => 'danger',
321 'log' => array(__FUNCTION__, $_action, $_item, $_data),
322 'msg' => array('redis_error', $e)
323 );
324 return false;
325 }
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200326
327 if (empty($app_links)){
328 return [];
329 }
330
331 // convert from old style
332 foreach($app_links as $i => $entry){
333 foreach($entry as $app => $link){
334 if (empty($link['link']) && empty($link['user_link'])){
335 $app_links[$i][$app] = array();
336 $app_links[$i][$app]['link'] = $link;
337 $app_links[$i][$app]['user_link'] = $link;
338 }
339 }
340 }
341
342 return $app_links;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100343 break;
344 case 'main_logo':
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100345 case 'main_logo_dark':
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100346 try {
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100347 return $redis->get(strtoupper($_item));
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100348 }
349 catch (RedisException $e) {
350 $_SESSION['return'][] = array(
351 'type' => 'danger',
352 'log' => array(__FUNCTION__, $_action, $_item, $_data),
353 'msg' => array('redis_error', $e)
354 );
355 return false;
356 }
357 break;
358 case 'ui_texts':
359 try {
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200360 $mailcow_hostname = strtolower(getenv("MAILCOW_HOSTNAME"));
361
362 $data['title_name'] = ($title_name = $redis->get('TITLE_NAME')) ? $title_name : "$mailcow_hostname - mail UI";
363 $data['main_name'] = ($main_name = $redis->get('MAIN_NAME')) ? $main_name : "$mailcow_hostname - mail UI";
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +0200364 $data['apps_name'] = ($apps_name = $redis->get('APPS_NAME')) ? $apps_name : $lang['header']['apps'];
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100365 $data['help_text'] = ($help_text = $redis->get('HELP_TEXT')) ? $help_text : false;
366 if (!empty($redis->get('UI_IMPRESS'))) {
367 $redis->set('UI_FOOTER', $redis->get('UI_IMPRESS'));
368 $redis->del('UI_IMPRESS');
369 }
370 $data['ui_footer'] = ($ui_footer = $redis->get('UI_FOOTER')) ? $ui_footer : false;
371 $data['ui_announcement_text'] = ($ui_announcement_text = $redis->get('UI_ANNOUNCEMENT_TEXT')) ? $ui_announcement_text : false;
372 $data['ui_announcement_type'] = ($ui_announcement_type = $redis->get('UI_ANNOUNCEMENT_TYPE')) ? $ui_announcement_type : false;
373 $data['ui_announcement_active'] = ($redis->get('UI_ANNOUNCEMENT_ACTIVE') == 1) ? 1 : 0;
374 return $data;
375 }
376 catch (RedisException $e) {
377 $_SESSION['return'][] = array(
378 'type' => 'danger',
379 'log' => array(__FUNCTION__, $_action, $_item, $_data),
380 'msg' => array('redis_error', $e)
381 );
382 return false;
383 }
384 break;
385 case 'main_logo_specs':
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100386 case 'main_logo_dark_specs':
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100387 try {
388 $image = new Imagick();
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100389 if($_item == 'main_logo_specs') {
390 $img_data = explode('base64,', customize('get', 'main_logo'));
391 } else {
392 $img_data = explode('base64,', customize('get', 'main_logo_dark'));
393 }
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100394 if ($img_data[1]) {
395 $image->readImageBlob(base64_decode($img_data[1]));
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100396 return $image->identifyImage();
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100397 }
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100398 return false;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100399 }
400 catch (ImagickException $e) {
401 $_SESSION['return'][] = array(
402 'type' => 'danger',
403 'log' => array(__FUNCTION__, $_action, $_item, $_data),
404 'msg' => 'imagick_exception'
405 );
406 return false;
407 }
408 break;
Matthias Andreas Benkardd63fe562023-11-18 13:18:30 +0100409 case 'ip_check':
410 try {
411 $ip_check = ($ip_check = $redis->get('IP_CHECK')) ? $ip_check : 0;
412 return $ip_check;
413 }
414 catch (RedisException $e) {
415 $_SESSION['return'][] = array(
416 'type' => 'danger',
417 'log' => array(__FUNCTION__, $_action, $_item, $_data),
418 'msg' => array('redis_error', $e)
419 );
420 return false;
421 }
422 break;
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200423 case 'custom_login':
424 try {
425 $custom_login = $redis->get('CUSTOM_LOGIN');
426 return $custom_login ? json_decode($custom_login, true) : array();
427 }
428 catch (RedisException $e) {
429 $_SESSION['return'][] = array(
430 'type' => 'danger',
431 'log' => array(__FUNCTION__, $_action, $_item, $_data),
432 'msg' => array('redis_error', $e)
433 );
434 return false;
435 }
436 break;
437 case 'ui_themes':
438 // theme folders in css/themes/<name>/ with a theme.css
439 $themes = array();
440 foreach (glob('/web/css/themes/*/theme.css') as $theme_css) {
441 $theme_dir = dirname($theme_css);
442 $theme = basename($theme_dir);
443 if (!preg_match('/^[a-zA-Z0-9_-]+$/', $theme)) {
444 continue;
445 }
446 $meta = json_decode(@file_get_contents($theme_dir . '/theme.json'), true);
447 $themes[$theme] = array(
448 'name' => (!empty($meta['name'])) ? $meta['name'] : $theme,
449 'description' => (!empty($meta['description'])) ? $meta['description'] : '',
450 'dark' => file_exists($theme_dir . '/dark.css')
451 );
452 }
453 ksort($themes);
454 return $themes;
455 break;
456 case 'ui_theme':
457 try {
458 $ui_theme = $redis->get('UI_THEME');
459 }
460 catch (RedisException $e) {
461 $_SESSION['return'][] = array(
462 'type' => 'danger',
463 'log' => array(__FUNCTION__, $_action, $_item, $_data),
464 'msg' => array('redis_error', $e)
465 );
466 return false;
467 }
468 if (empty($ui_theme) || !preg_match('/^[a-zA-Z0-9_-]+$/', $ui_theme) || !file_exists('/web/css/themes/' . $ui_theme . '/theme.css')) {
469 return '';
470 }
471 return $ui_theme;
472 break;
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +0100473 }
474 break;
475 }
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100476}