functions.customize.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. function customize($_action, $_item, $_data = null) {
  3. global $redis;
  4. global $lang;
  5. global $LOGO_LIMITS;
  6. switch ($_action) {
  7. case 'add':
  8. // disable functionality when demo mode is enabled
  9. if ($GLOBALS["DEMO_MODE"]) {
  10. $_SESSION['return'][] = array(
  11. 'type' => 'danger',
  12. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  13. 'msg' => 'demo_mode_enabled'
  14. );
  15. return false;
  16. }
  17. if ($_SESSION['mailcow_cc_role'] != "admin") {
  18. $_SESSION['return'][] = array(
  19. 'type' => 'danger',
  20. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  21. 'msg' => 'access_denied'
  22. );
  23. return false;
  24. }
  25. switch ($_item) {
  26. case 'main_logo':
  27. case 'main_logo_dark':
  28. if (in_array($_data[$_item]['type'], array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png', 'image/svg+xml'))) {
  29. try {
  30. if (file_exists($_data[$_item]['tmp_name']) !== true) {
  31. $_SESSION['return'][] = array(
  32. 'type' => 'danger',
  33. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  34. 'msg' => 'img_tmp_missing'
  35. );
  36. return false;
  37. }
  38. if ($_data[$_item]['size'] > $LOGO_LIMITS['max_size']) {
  39. $_SESSION['return'][] = array(
  40. 'type' => 'danger',
  41. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  42. 'msg' => 'img_size_exceeded'
  43. );
  44. return false;
  45. }
  46. list($width, $height) = getimagesize($_data[$_item]['tmp_name']);
  47. if ($width > $LOGO_LIMITS['max_width'] || $height > $LOGO_LIMITS['max_height']) {
  48. $_SESSION['return'][] = array(
  49. 'type' => 'danger',
  50. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  51. 'msg' => 'img_dimensions_exceeded'
  52. );
  53. return false;
  54. }
  55. $image = new Imagick($_data[$_item]['tmp_name']);
  56. if ($image->valid() !== true) {
  57. $_SESSION['return'][] = array(
  58. 'type' => 'danger',
  59. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  60. 'msg' => 'img_invalid'
  61. );
  62. return false;
  63. }
  64. $image->destroy();
  65. }
  66. catch (ImagickException $e) {
  67. $_SESSION['return'][] = array(
  68. 'type' => 'danger',
  69. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  70. 'msg' => 'img_invalid'
  71. );
  72. return false;
  73. }
  74. }
  75. else {
  76. $_SESSION['return'][] = array(
  77. 'type' => 'danger',
  78. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  79. 'msg' => 'invalid_mime_type'
  80. );
  81. return false;
  82. }
  83. try {
  84. $redis->Set(strtoupper($_item), 'data:' . $_data[$_item]['type'] . ';base64,' . base64_encode(file_get_contents($_data[$_item]['tmp_name'])));
  85. }
  86. catch (RedisException $e) {
  87. $_SESSION['return'][] = array(
  88. 'type' => 'danger',
  89. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  90. 'msg' => array('redis_error', $e)
  91. );
  92. return false;
  93. }
  94. $_SESSION['return'][] = array(
  95. 'type' => 'success',
  96. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  97. 'msg' => 'upload_success'
  98. );
  99. break;
  100. }
  101. break;
  102. case 'edit':
  103. // disable functionality when demo mode is enabled
  104. if ($GLOBALS["DEMO_MODE"]) {
  105. $_SESSION['return'][] = array(
  106. 'type' => 'danger',
  107. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  108. 'msg' => 'demo_mode_enabled'
  109. );
  110. return false;
  111. }
  112. if ($_SESSION['mailcow_cc_role'] != "admin") {
  113. $_SESSION['return'][] = array(
  114. 'type' => 'danger',
  115. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  116. 'msg' => 'access_denied'
  117. );
  118. return false;
  119. }
  120. switch ($_item) {
  121. case 'app_links':
  122. $apps = (array)$_data['app'];
  123. $links = (array)$_data['href'];
  124. $user_links = (array)$_data['user_href'];
  125. $hide = (array)$_data['hide'];
  126. $out = array();
  127. if (count($apps) == count($links) && count($apps) == count($user_links) && count($apps) == count($hide)) {
  128. for ($i = 0; $i < count($apps); $i++) {
  129. $out[] = array($apps[$i] => array(
  130. 'link' => $links[$i],
  131. 'user_link' => $user_links[$i],
  132. 'hide' => ($hide[$i] === '0' || $hide[$i] === 0) ? false : true
  133. ));
  134. }
  135. try {
  136. $redis->set('APP_LINKS', json_encode($out));
  137. }
  138. catch (RedisException $e) {
  139. $_SESSION['return'][] = array(
  140. 'type' => 'danger',
  141. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  142. 'msg' => array('redis_error', $e)
  143. );
  144. return false;
  145. }
  146. }
  147. $_SESSION['return'][] = array(
  148. 'type' => 'success',
  149. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  150. 'msg' => 'app_links'
  151. );
  152. break;
  153. case 'ui_texts':
  154. $title_name = $_data['title_name'];
  155. $main_name = $_data['main_name'];
  156. $apps_name = $_data['apps_name'];
  157. $help_text = $_data['help_text'];
  158. $ui_footer = $_data['ui_footer'];
  159. $ui_announcement_text = $_data['ui_announcement_text'];
  160. $ui_announcement_type = (in_array($_data['ui_announcement_type'], array('info', 'warning', 'danger'))) ? $_data['ui_announcement_type'] : false;
  161. $ui_announcement_active = (!empty($_data['ui_announcement_active']) ? 1 : 0);
  162. try {
  163. $redis->set('TITLE_NAME', htmlspecialchars($title_name));
  164. $redis->set('MAIN_NAME', htmlspecialchars($main_name));
  165. $redis->set('APPS_NAME', htmlspecialchars($apps_name));
  166. $redis->set('HELP_TEXT', $help_text);
  167. $redis->set('UI_FOOTER', $ui_footer);
  168. $redis->set('UI_ANNOUNCEMENT_TEXT', $ui_announcement_text);
  169. $redis->set('UI_ANNOUNCEMENT_TYPE', $ui_announcement_type);
  170. $redis->set('UI_ANNOUNCEMENT_ACTIVE', $ui_announcement_active);
  171. }
  172. catch (RedisException $e) {
  173. $_SESSION['return'][] = array(
  174. 'type' => 'danger',
  175. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  176. 'msg' => array('redis_error', $e)
  177. );
  178. return false;
  179. }
  180. $_SESSION['return'][] = array(
  181. 'type' => 'success',
  182. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  183. 'msg' => 'ui_texts'
  184. );
  185. break;
  186. case 'ip_check':
  187. $ip_check = ($_data['ip_check_opt_in'] == "1") ? 1 : 0;
  188. try {
  189. $redis->set('IP_CHECK', $ip_check);
  190. }
  191. catch (RedisException $e) {
  192. $_SESSION['return'][] = array(
  193. 'type' => 'danger',
  194. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  195. 'msg' => array('redis_error', $e)
  196. );
  197. return false;
  198. }
  199. $_SESSION['return'][] = array(
  200. 'type' => 'success',
  201. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  202. 'msg' => 'ip_check_opt_in_modified'
  203. );
  204. break;
  205. }
  206. break;
  207. case 'delete':
  208. // disable functionality when demo mode is enabled
  209. if ($GLOBALS["DEMO_MODE"]) {
  210. $_SESSION['return'][] = array(
  211. 'type' => 'danger',
  212. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  213. 'msg' => 'demo_mode_enabled'
  214. );
  215. return false;
  216. }
  217. if ($_SESSION['mailcow_cc_role'] != "admin") {
  218. $_SESSION['return'][] = array(
  219. 'type' => 'danger',
  220. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  221. 'msg' => 'access_denied'
  222. );
  223. return false;
  224. }
  225. switch ($_item) {
  226. case 'main_logo':
  227. case 'main_logo_dark':
  228. try {
  229. if ($redis->del(strtoupper($_item))) {
  230. $_SESSION['return'][] = array(
  231. 'type' => 'success',
  232. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  233. 'msg' => 'reset_main_logo'
  234. );
  235. return true;
  236. }
  237. }
  238. catch (RedisException $e) {
  239. $_SESSION['return'][] = array(
  240. 'type' => 'danger',
  241. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  242. 'msg' => array('redis_error', $e)
  243. );
  244. return false;
  245. }
  246. break;
  247. }
  248. break;
  249. case 'get':
  250. switch ($_item) {
  251. case 'app_links':
  252. try {
  253. $app_links = json_decode($redis->get('APP_LINKS'), true);
  254. }
  255. catch (RedisException $e) {
  256. $_SESSION['return'][] = array(
  257. 'type' => 'danger',
  258. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  259. 'msg' => array('redis_error', $e)
  260. );
  261. return false;
  262. }
  263. if (empty($app_links)){
  264. return false;
  265. }
  266. foreach($app_links as $key => $value){
  267. foreach($value as $app => $details){
  268. if (empty($details['user_link']) || empty($_SESSION['mailcow_cc_username'])){
  269. $app_links[$key][$app]['user_link'] = $app_links[$key][$app]['link'];
  270. } else {
  271. $app_links[$key][$app]['user_link'] = str_replace('%u', $_SESSION['mailcow_cc_username'], $app_links[$key][$app]['user_link']);
  272. }
  273. }
  274. }
  275. return $app_links;
  276. break;
  277. case 'main_logo':
  278. case 'main_logo_dark':
  279. try {
  280. return $redis->get(strtoupper($_item));
  281. }
  282. catch (RedisException $e) {
  283. $_SESSION['return'][] = array(
  284. 'type' => 'danger',
  285. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  286. 'msg' => array('redis_error', $e)
  287. );
  288. return false;
  289. }
  290. break;
  291. case 'ui_texts':
  292. try {
  293. $data['title_name'] = ($title_name = $redis->get('TITLE_NAME')) ? $title_name : 'mailcow UI';
  294. $data['main_name'] = ($main_name = $redis->get('MAIN_NAME')) ? $main_name : 'mailcow UI';
  295. $data['apps_name'] = ($apps_name = $redis->get('APPS_NAME')) ? $apps_name : $lang['header']['apps'];
  296. $data['help_text'] = ($help_text = $redis->get('HELP_TEXT')) ? $help_text : false;
  297. if (!empty($redis->get('UI_IMPRESS'))) {
  298. $redis->set('UI_FOOTER', $redis->get('UI_IMPRESS'));
  299. $redis->del('UI_IMPRESS');
  300. }
  301. $data['ui_footer'] = ($ui_footer = $redis->get('UI_FOOTER')) ? $ui_footer : false;
  302. $data['ui_announcement_text'] = ($ui_announcement_text = $redis->get('UI_ANNOUNCEMENT_TEXT')) ? $ui_announcement_text : false;
  303. $data['ui_announcement_type'] = ($ui_announcement_type = $redis->get('UI_ANNOUNCEMENT_TYPE')) ? $ui_announcement_type : false;
  304. $data['ui_announcement_active'] = ($redis->get('UI_ANNOUNCEMENT_ACTIVE') == 1) ? 1 : 0;
  305. return $data;
  306. }
  307. catch (RedisException $e) {
  308. $_SESSION['return'][] = array(
  309. 'type' => 'danger',
  310. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  311. 'msg' => array('redis_error', $e)
  312. );
  313. return false;
  314. }
  315. break;
  316. case 'main_logo_specs':
  317. case 'main_logo_dark_specs':
  318. try {
  319. $image = new Imagick();
  320. if($_item == 'main_logo_specs') {
  321. $img_data = explode('base64,', customize('get', 'main_logo'));
  322. } else {
  323. $img_data = explode('base64,', customize('get', 'main_logo_dark'));
  324. }
  325. if ($img_data[1]) {
  326. $image->readImageBlob(base64_decode($img_data[1]));
  327. return $image->identifyImage();
  328. }
  329. return false;
  330. }
  331. catch (ImagickException $e) {
  332. $_SESSION['return'][] = array(
  333. 'type' => 'danger',
  334. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  335. 'msg' => 'imagick_exception'
  336. );
  337. return false;
  338. }
  339. break;
  340. case 'ip_check':
  341. try {
  342. $ip_check = ($ip_check = $redis->get('IP_CHECK')) ? $ip_check : 0;
  343. return $ip_check;
  344. }
  345. catch (RedisException $e) {
  346. $_SESSION['return'][] = array(
  347. 'type' => 'danger',
  348. 'log' => array(__FUNCTION__, $_action, $_item, $_data),
  349. 'msg' => array('redis_error', $e)
  350. );
  351. return false;
  352. }
  353. break;
  354. }
  355. break;
  356. }
  357. }