json_api.php 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  1. <?php
  2. /*
  3. edit/alias => POST data:
  4. {
  5. address: {a, b, c}, (where a, b, c represent alias addresses)
  6. active: 1 (0 or 1)
  7. }
  8. delete/alias => POST data:
  9. {
  10. address: {a, b, c}, (where a, b, c represent alias addresses)
  11. }
  12. */
  13. header('Content-Type: application/json');
  14. require_once 'inc/prerequisites.inc.php';
  15. error_reporting(0);
  16. if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_username'])) {
  17. if (isset($_GET['query'])) {
  18. $query = explode('/', $_GET['query']);
  19. $action = (isset($query[0])) ? $query[0] : null;
  20. $category = (isset($query[1])) ? $query[1] : null;
  21. $object = (isset($query[2])) ? $query[2] : null;
  22. $extra = (isset($query[3])) ? $query[3] : null;
  23. switch ($action) {
  24. case "add":
  25. switch ($category) {
  26. case "time_limited_alias":
  27. if (isset($_POST['attr'])) {
  28. $attr = (array)json_decode($_POST['attr'], true);
  29. if (mailbox('add', 'time_limited_alias', $attr) === false) {
  30. if (isset($_SESSION['return'])) {
  31. echo json_encode($_SESSION['return']);
  32. }
  33. else {
  34. echo json_encode(array(
  35. 'type' => 'error',
  36. 'msg' => 'Cannot add item'
  37. ));
  38. }
  39. }
  40. else {
  41. if (isset($_SESSION['return'])) {
  42. echo json_encode($_SESSION['return']);
  43. }
  44. else {
  45. echo json_encode(array(
  46. 'type' => 'success',
  47. 'msg' => 'Task completed'
  48. ));
  49. }
  50. }
  51. }
  52. else {
  53. echo json_encode(array(
  54. 'type' => 'error',
  55. 'msg' => 'Cannot find attributes in post data'
  56. ));
  57. }
  58. break;
  59. case "mailbox":
  60. if (isset($_POST['attr'])) {
  61. $attr = (array)json_decode($_POST['attr'], true);
  62. if (mailbox('add', 'mailbox', $attr) === false) {
  63. if (isset($_SESSION['return'])) {
  64. echo json_encode($_SESSION['return']);
  65. }
  66. else {
  67. echo json_encode(array(
  68. 'type' => 'error',
  69. 'msg' => 'Cannot add item'
  70. ));
  71. }
  72. }
  73. else {
  74. if (isset($_SESSION['return'])) {
  75. echo json_encode($_SESSION['return']);
  76. }
  77. else {
  78. echo json_encode(array(
  79. 'type' => 'success',
  80. 'msg' => 'Task completed'
  81. ));
  82. }
  83. }
  84. }
  85. else {
  86. echo json_encode(array(
  87. 'type' => 'error',
  88. 'msg' => 'Cannot find attributes in post data'
  89. ));
  90. }
  91. break;
  92. case "domain":
  93. if (isset($_POST['attr'])) {
  94. $attr = (array)json_decode($_POST['attr'], true);
  95. if (mailbox('add', 'domain', $attr) === false) {
  96. if (isset($_SESSION['return'])) {
  97. echo json_encode($_SESSION['return']);
  98. }
  99. else {
  100. echo json_encode(array(
  101. 'type' => 'error',
  102. 'msg' => 'Cannot add item'
  103. ));
  104. }
  105. }
  106. else {
  107. if (isset($_SESSION['return'])) {
  108. echo json_encode($_SESSION['return']);
  109. }
  110. else {
  111. echo json_encode(array(
  112. 'type' => 'success',
  113. 'msg' => 'Task completed'
  114. ));
  115. }
  116. }
  117. }
  118. else {
  119. echo json_encode(array(
  120. 'type' => 'error',
  121. 'msg' => 'Cannot find attributes in post data'
  122. ));
  123. }
  124. break;
  125. case "resource":
  126. if (isset($_POST['attr'])) {
  127. $attr = (array)json_decode($_POST['attr'], true);
  128. if (mailbox('add', 'resource', $attr) === false) {
  129. if (isset($_SESSION['return'])) {
  130. echo json_encode($_SESSION['return']);
  131. }
  132. else {
  133. echo json_encode(array(
  134. 'type' => 'error',
  135. 'msg' => 'Cannot add item'
  136. ));
  137. }
  138. }
  139. else {
  140. if (isset($_SESSION['return'])) {
  141. echo json_encode($_SESSION['return']);
  142. }
  143. else {
  144. echo json_encode(array(
  145. 'type' => 'success',
  146. 'msg' => 'Task completed'
  147. ));
  148. }
  149. }
  150. }
  151. else {
  152. echo json_encode(array(
  153. 'type' => 'error',
  154. 'msg' => 'Cannot find attributes in post data'
  155. ));
  156. }
  157. break;
  158. case "alias":
  159. if (isset($_POST['attr'])) {
  160. $attr = (array)json_decode($_POST['attr'], true);
  161. if (mailbox('add', 'alias', $attr) === false) {
  162. if (isset($_SESSION['return'])) {
  163. echo json_encode($_SESSION['return']);
  164. }
  165. else {
  166. echo json_encode(array(
  167. 'type' => 'error',
  168. 'msg' => 'Cannot add item'
  169. ));
  170. }
  171. }
  172. else {
  173. if (isset($_SESSION['return'])) {
  174. echo json_encode($_SESSION['return']);
  175. }
  176. else {
  177. echo json_encode(array(
  178. 'type' => 'success',
  179. 'msg' => 'Task completed'
  180. ));
  181. }
  182. }
  183. }
  184. else {
  185. echo json_encode(array(
  186. 'type' => 'error',
  187. 'msg' => 'Cannot find attributes in post data'
  188. ));
  189. }
  190. break;
  191. case "syncjob":
  192. if (isset($_POST['attr'])) {
  193. $attr = (array)json_decode($_POST['attr'], true);
  194. if (mailbox('add', 'syncjob', $attr) === false) {
  195. if (isset($_SESSION['return'])) {
  196. echo json_encode($_SESSION['return']);
  197. }
  198. else {
  199. echo json_encode(array(
  200. 'type' => 'error',
  201. 'msg' => 'Cannot add item'
  202. ));
  203. }
  204. }
  205. else {
  206. if (isset($_SESSION['return'])) {
  207. echo json_encode($_SESSION['return']);
  208. }
  209. else {
  210. echo json_encode(array(
  211. 'type' => 'success',
  212. 'msg' => 'Task completed'
  213. ));
  214. }
  215. }
  216. }
  217. else {
  218. echo json_encode(array(
  219. 'type' => 'error',
  220. 'msg' => 'Cannot find attributes in post data'
  221. ));
  222. }
  223. break;
  224. case "domain-policy":
  225. if (isset($_POST['attr'])) {
  226. $attr = (array)json_decode($_POST['attr'], true);
  227. if (policy('add', 'domain', $attr) === false) {
  228. if (isset($_SESSION['return'])) {
  229. echo json_encode($_SESSION['return']);
  230. }
  231. else {
  232. echo json_encode(array(
  233. 'type' => 'error',
  234. 'msg' => 'Cannot add item'
  235. ));
  236. }
  237. }
  238. else {
  239. if (isset($_SESSION['return'])) {
  240. echo json_encode($_SESSION['return']);
  241. }
  242. else {
  243. echo json_encode(array(
  244. 'type' => 'success',
  245. 'msg' => 'Task completed'
  246. ));
  247. }
  248. }
  249. }
  250. else {
  251. echo json_encode(array(
  252. 'type' => 'error',
  253. 'msg' => 'Cannot find attributes in post data'
  254. ));
  255. }
  256. break;
  257. case "mailbox-policy":
  258. if (isset($_POST['attr'])) {
  259. $attr = (array)json_decode($_POST['attr'], true);
  260. if (policy('add', 'mailbox', $attr) === false) {
  261. if (isset($_SESSION['return'])) {
  262. echo json_encode($_SESSION['return']);
  263. }
  264. else {
  265. echo json_encode(array(
  266. 'type' => 'error',
  267. 'msg' => 'Cannot add item'
  268. ));
  269. }
  270. }
  271. else {
  272. if (isset($_SESSION['return'])) {
  273. echo json_encode($_SESSION['return']);
  274. }
  275. else {
  276. echo json_encode(array(
  277. 'type' => 'success',
  278. 'msg' => 'Task completed'
  279. ));
  280. }
  281. }
  282. }
  283. else {
  284. echo json_encode(array(
  285. 'type' => 'error',
  286. 'msg' => 'Cannot find attributes in post data'
  287. ));
  288. }
  289. break;
  290. case "alias-domain":
  291. if (isset($_POST['attr'])) {
  292. $attr = (array)json_decode($_POST['attr'], true);
  293. if (mailbox('add', 'alias_domain', $attr) === false) {
  294. if (isset($_SESSION['return'])) {
  295. echo json_encode($_SESSION['return']);
  296. }
  297. else {
  298. echo json_encode(array(
  299. 'type' => 'error',
  300. 'msg' => 'Cannot add item'
  301. ));
  302. }
  303. }
  304. else {
  305. if (isset($_SESSION['return'])) {
  306. echo json_encode($_SESSION['return']);
  307. }
  308. else {
  309. echo json_encode(array(
  310. 'type' => 'success',
  311. 'msg' => 'Task completed'
  312. ));
  313. }
  314. }
  315. }
  316. else {
  317. echo json_encode(array(
  318. 'type' => 'error',
  319. 'msg' => 'Cannot find attributes in post data'
  320. ));
  321. }
  322. break;
  323. case "fwdhost":
  324. if (isset($_POST['attr'])) {
  325. $attr = (array)json_decode($_POST['attr'], true);
  326. if (fwdhost('add', $attr) === false) {
  327. if (isset($_SESSION['return'])) {
  328. echo json_encode($_SESSION['return']);
  329. }
  330. else {
  331. echo json_encode(array(
  332. 'type' => 'error',
  333. 'msg' => 'Cannot add item'
  334. ));
  335. }
  336. }
  337. else {
  338. if (isset($_SESSION['return'])) {
  339. echo json_encode($_SESSION['return']);
  340. }
  341. else {
  342. echo json_encode(array(
  343. 'type' => 'success',
  344. 'msg' => 'Task completed'
  345. ));
  346. }
  347. }
  348. }
  349. else {
  350. echo json_encode(array(
  351. 'type' => 'error',
  352. 'msg' => 'Cannot find attributes in post data'
  353. ));
  354. }
  355. break;
  356. case "dkim":
  357. if (isset($_POST['attr'])) {
  358. $attr = (array)json_decode($_POST['attr'], true);
  359. if (dkim('add', $attr) === false) {
  360. if (isset($_SESSION['return'])) {
  361. echo json_encode($_SESSION['return']);
  362. }
  363. else {
  364. echo json_encode(array(
  365. 'type' => 'error',
  366. 'msg' => 'Cannot add item'
  367. ));
  368. }
  369. }
  370. else {
  371. if (isset($_SESSION['return'])) {
  372. echo json_encode($_SESSION['return']);
  373. }
  374. else {
  375. echo json_encode(array(
  376. 'type' => 'success',
  377. 'msg' => 'Task completed'
  378. ));
  379. }
  380. }
  381. }
  382. else {
  383. echo json_encode(array(
  384. 'type' => 'error',
  385. 'msg' => 'Cannot find attributes in post data'
  386. ));
  387. }
  388. break;
  389. case "dkim_import":
  390. if (isset($_POST['attr'])) {
  391. $attr = (array)json_decode($_POST['attr'], true);
  392. if (dkim('import', $attr) === false) {
  393. if (isset($_SESSION['return'])) {
  394. echo json_encode($_SESSION['return']);
  395. }
  396. else {
  397. echo json_encode(array(
  398. 'type' => 'error',
  399. 'msg' => 'Cannot add item'
  400. ));
  401. }
  402. }
  403. else {
  404. if (isset($_SESSION['return'])) {
  405. echo json_encode($_SESSION['return']);
  406. }
  407. else {
  408. echo json_encode(array(
  409. 'type' => 'success',
  410. 'msg' => 'Task completed'
  411. ));
  412. }
  413. }
  414. }
  415. else {
  416. echo json_encode(array(
  417. 'type' => 'error',
  418. 'msg' => 'Cannot find attributes in post data'
  419. ));
  420. }
  421. break;
  422. case "domain-admin":
  423. if (isset($_POST['attr'])) {
  424. $attr = (array)json_decode($_POST['attr'], true);
  425. if (domain_admin('add', $attr) === false) {
  426. if (isset($_SESSION['return'])) {
  427. echo json_encode($_SESSION['return']);
  428. }
  429. else {
  430. echo json_encode(array(
  431. 'type' => 'error',
  432. 'msg' => 'Cannot add item'
  433. ));
  434. }
  435. }
  436. else {
  437. if (isset($_SESSION['return'])) {
  438. echo json_encode($_SESSION['return']);
  439. }
  440. else {
  441. echo json_encode(array(
  442. 'type' => 'success',
  443. 'msg' => 'Task completed'
  444. ));
  445. }
  446. }
  447. }
  448. else {
  449. echo json_encode(array(
  450. 'type' => 'error',
  451. 'msg' => 'Cannot find attributes in post data'
  452. ));
  453. }
  454. break;
  455. }
  456. break;
  457. case "get":
  458. switch ($category) {
  459. case "domain":
  460. switch ($object) {
  461. case "all":
  462. $domains = mailbox('get', 'domains');
  463. if (!empty($domains)) {
  464. foreach ($domains as $domain) {
  465. if ($details = mailbox('get', 'domain_details', $domain)) {
  466. $data[] = $details;
  467. }
  468. else {
  469. continue;
  470. }
  471. }
  472. if (!isset($data) || empty($data)) {
  473. echo '{}';
  474. }
  475. else {
  476. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  477. }
  478. }
  479. else {
  480. echo '{}';
  481. }
  482. break;
  483. default:
  484. $data = mailbox('get', 'domain_details', $object);
  485. if (!isset($data) || empty($data)) {
  486. echo '{}';
  487. }
  488. else {
  489. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  490. }
  491. break;
  492. }
  493. break;
  494. case "logs":
  495. switch ($object) {
  496. case "dovecot":
  497. if (isset($extra) && !empty($extra)) {
  498. $extra = intval($extra);
  499. $logs = get_logs('dovecot-mailcow', $extra);
  500. }
  501. else {
  502. $logs = get_logs('dovecot-mailcow', -1);
  503. }
  504. if (isset($logs) && !empty($logs)) {
  505. echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  506. }
  507. else {
  508. echo '{}';
  509. }
  510. break;
  511. case "fail2ban":
  512. if (isset($extra) && !empty($extra)) {
  513. $extra = intval($extra);
  514. $logs = get_logs('fail2ban-mailcow', $extra);
  515. }
  516. else {
  517. $logs = get_logs('fail2ban-mailcow', -1);
  518. }
  519. if (isset($logs) && !empty($logs)) {
  520. echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  521. }
  522. else {
  523. echo '{}';
  524. }
  525. break;
  526. case "postfix":
  527. if (isset($extra) && !empty($extra)) {
  528. $extra = intval($extra);
  529. $logs = get_logs('postfix-mailcow', $extra);
  530. }
  531. else {
  532. $logs = get_logs('postfix-mailcow', -1);
  533. }
  534. if (isset($logs) && !empty($logs)) {
  535. echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  536. }
  537. else {
  538. echo '{}';
  539. }
  540. break;
  541. case "sogo":
  542. if (isset($extra) && !empty($extra)) {
  543. $extra = intval($extra);
  544. $logs = get_logs('sogo-mailcow', $extra);
  545. }
  546. else {
  547. $logs = get_logs('sogo-mailcow', -1);
  548. }
  549. if (isset($logs) && !empty($logs)) {
  550. echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  551. }
  552. else {
  553. echo '{}';
  554. }
  555. break;
  556. case "rspamd-history":
  557. $logs = get_logs('rspamd-history');
  558. if (isset($logs) && !empty($logs)) {
  559. echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  560. }
  561. else {
  562. echo '{}';
  563. }
  564. break;
  565. }
  566. break;
  567. case "mailbox":
  568. switch ($object) {
  569. case "all":
  570. $domains = mailbox('get', 'domains');
  571. if (!empty($domains)) {
  572. foreach ($domains as $domain) {
  573. $mailboxes = mailbox('get', 'mailboxes', $domain);
  574. if (!empty($mailboxes)) {
  575. foreach ($mailboxes as $mailbox) {
  576. if ($details = mailbox('get', 'mailbox_details', $mailbox)) {
  577. $data[] = $details;
  578. }
  579. else {
  580. continue;
  581. }
  582. }
  583. }
  584. }
  585. if (!isset($data) || empty($data)) {
  586. echo '{}';
  587. }
  588. else {
  589. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  590. }
  591. }
  592. else {
  593. echo '{}';
  594. }
  595. break;
  596. default:
  597. $data = mailbox('get', 'mailbox_details', $object);
  598. if (!isset($data) || empty($data)) {
  599. echo '{}';
  600. }
  601. else {
  602. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  603. }
  604. break;
  605. }
  606. break;
  607. case "syncjobs":
  608. switch ($object) {
  609. default:
  610. $data = mailbox('get', 'syncjobs', $object);
  611. if (!isset($data) || empty($data)) {
  612. echo '{}';
  613. }
  614. else {
  615. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  616. }
  617. break;
  618. }
  619. break;
  620. case "policy_wl_mailbox":
  621. switch ($object) {
  622. default:
  623. $data = policy('get', 'mailbox', $object)['whitelist'];
  624. if (!isset($data) || empty($data)) {
  625. echo '{}';
  626. }
  627. else {
  628. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  629. }
  630. break;
  631. }
  632. break;
  633. case "policy_bl_mailbox":
  634. switch ($object) {
  635. default:
  636. $data = policy('get', 'mailbox', $object)['blacklist'];
  637. if (!isset($data) || empty($data)) {
  638. echo '{}';
  639. }
  640. else {
  641. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  642. }
  643. break;
  644. }
  645. break;
  646. case "policy_wl_domain":
  647. switch ($object) {
  648. default:
  649. $data = policy('get', 'domain', $object)['whitelist'];
  650. if (!isset($data) || empty($data)) {
  651. echo '{}';
  652. }
  653. else {
  654. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  655. }
  656. break;
  657. }
  658. break;
  659. case "policy_bl_domain":
  660. switch ($object) {
  661. default:
  662. $data = policy('get', 'domain', $object)['blacklist'];
  663. if (!isset($data) || empty($data)) {
  664. echo '{}';
  665. }
  666. else {
  667. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  668. }
  669. break;
  670. }
  671. break;
  672. case "time_limited_aliases":
  673. switch ($object) {
  674. default:
  675. $data = mailbox('get', 'time_limited_aliases', $object);
  676. if (!isset($data) || empty($data)) {
  677. echo '{}';
  678. }
  679. else {
  680. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  681. }
  682. break;
  683. }
  684. break;
  685. case "resource":
  686. switch ($object) {
  687. case "all":
  688. $domains = mailbox('get', 'domains');
  689. if (!empty($domains)) {
  690. foreach ($domains as $domain) {
  691. $resources = mailbox('get', 'resources', $domain);
  692. if (!empty($resources)) {
  693. foreach ($resources as $resource) {
  694. if ($details = mailbox('get', 'resource_details', $resource)) {
  695. $data[] = $details;
  696. }
  697. else {
  698. continue;
  699. }
  700. }
  701. }
  702. }
  703. if (!isset($data) || empty($data)) {
  704. echo '{}';
  705. }
  706. else {
  707. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  708. }
  709. }
  710. else {
  711. echo '{}';
  712. }
  713. break;
  714. default:
  715. $data = mailbox('get', 'resource_details', $object);
  716. if (!isset($data) || empty($data)) {
  717. echo '{}';
  718. }
  719. else {
  720. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  721. }
  722. break;
  723. }
  724. break;
  725. case "fwdhost":
  726. switch ($object) {
  727. case "all":
  728. $data = fwdhost('get');
  729. if (!isset($data) || empty($data)) {
  730. echo '{}';
  731. }
  732. else {
  733. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  734. }
  735. break;
  736. default:
  737. $data = fwdhost('details', $object);
  738. if (!isset($data) || empty($data)) {
  739. echo '{}';
  740. }
  741. else {
  742. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  743. }
  744. break;
  745. }
  746. break;
  747. case "alias-domain":
  748. switch ($object) {
  749. case "all":
  750. $alias_domains = mailbox('get', 'alias_domains');
  751. if (!empty($alias_domains)) {
  752. foreach ($alias_domains as $alias_domain) {
  753. if ($details = mailbox('get', 'alias_domain_details', $alias_domain)) {
  754. $data[] = $details;
  755. }
  756. else {
  757. continue;
  758. }
  759. }
  760. }
  761. if (!isset($data) || empty($data)) {
  762. echo '{}';
  763. }
  764. else {
  765. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  766. }
  767. break;
  768. default:
  769. $data = mailbox('get', 'alias_domain_details', $object);
  770. if (!isset($data) || empty($data)) {
  771. echo '{}';
  772. }
  773. else {
  774. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  775. }
  776. break;
  777. }
  778. break;
  779. case "alias":
  780. switch ($object) {
  781. case "all":
  782. $domains = array_merge(mailbox('get', 'domains'),mailbox('get', 'alias_domains'));
  783. if (!empty($domains)) {
  784. foreach ($domains as $domain) {
  785. $aliases = mailbox('get', 'aliases', $domain);
  786. if (!empty($aliases)) {
  787. foreach ($aliases as $alias) {
  788. if ($details = mailbox('get', 'alias_details', $alias)) {
  789. $data[] = $details;
  790. }
  791. else {
  792. continue;
  793. }
  794. }
  795. }
  796. }
  797. if (!isset($data) || empty($data)) {
  798. echo '{}';
  799. }
  800. else {
  801. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  802. }
  803. }
  804. else {
  805. echo '{}';
  806. }
  807. break;
  808. default:
  809. $data = mailbox('get', 'alias_details', $object);
  810. if (!isset($data) || empty($data)) {
  811. echo '{}';
  812. }
  813. else {
  814. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  815. }
  816. break;
  817. }
  818. break;
  819. case "domain-admin":
  820. switch ($object) {
  821. case "all":
  822. $domain_admins = domain_admin('get');
  823. if (!empty($domain_admins)) {
  824. foreach ($domain_admins as $domain_admin) {
  825. if ($details = domain_admin('details', $domain_admin)) {
  826. $data[] = $details;
  827. }
  828. else {
  829. continue;
  830. }
  831. }
  832. if (!isset($data) || empty($data)) {
  833. echo '{}';
  834. }
  835. else {
  836. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  837. }
  838. }
  839. else {
  840. echo '{}';
  841. }
  842. break;
  843. default:
  844. $data = domain_admin('details', $object);
  845. if (!isset($data) || empty($data)) {
  846. echo '{}';
  847. }
  848. else {
  849. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  850. }
  851. break;
  852. }
  853. break;
  854. case "u2f-registration":
  855. header('Content-Type: application/javascript');
  856. if (($_SESSION["mailcow_cc_role"] == "admin" || $_SESSION["mailcow_cc_role"] == "domainadmin") && $_SESSION["mailcow_cc_username"] == $object) {
  857. $data = $u2f->getRegisterData(get_u2f_registrations($object));
  858. list($req, $sigs) = $data;
  859. $_SESSION['regReq'] = json_encode($req);
  860. echo 'var req = ' . json_encode($req) . '; var sigs = ' . json_encode($sigs) . ';';
  861. }
  862. else {
  863. return;
  864. }
  865. break;
  866. case "u2f-authentication":
  867. header('Content-Type: application/javascript');
  868. if (isset($_SESSION['pending_mailcow_cc_username']) && $_SESSION['pending_mailcow_cc_username'] == $object) {
  869. $reqs = json_encode($u2f->getAuthenticateData(get_u2f_registrations($object)));
  870. $_SESSION['authReq'] = $reqs;
  871. echo 'var req = ' . $reqs . ';';
  872. }
  873. else {
  874. return;
  875. }
  876. break;
  877. default:
  878. echo '{}';
  879. break;
  880. }
  881. break;
  882. case "delete":
  883. switch ($category) {
  884. case "alias":
  885. if (isset($_POST['items'])) {
  886. $items = (array)json_decode($_POST['items'], true);
  887. if (is_array($items)) {
  888. if (mailbox('delete', 'alias', array('address' => $items)) === false) {
  889. if (isset($_SESSION['return'])) {
  890. echo json_encode($_SESSION['return']);
  891. }
  892. else {
  893. echo json_encode(array(
  894. 'type' => 'error',
  895. 'msg' => 'Deletion of items/s failed'
  896. ));
  897. }
  898. }
  899. else {
  900. if (isset($_SESSION['return'])) {
  901. echo json_encode($_SESSION['return']);
  902. }
  903. else {
  904. echo json_encode(array(
  905. 'type' => 'success',
  906. 'msg' => 'Task completed'
  907. ));
  908. }
  909. }
  910. }
  911. else {
  912. echo json_encode(array(
  913. 'type' => 'error',
  914. 'msg' => 'Cannot find address array in post data'
  915. ));
  916. }
  917. }
  918. else {
  919. echo json_encode(array(
  920. 'type' => 'error',
  921. 'msg' => 'Cannot find items in post data'
  922. ));
  923. }
  924. break;
  925. case "syncjob":
  926. if (isset($_POST['items'])) {
  927. $items = (array)json_decode($_POST['items'], true);
  928. if (is_array($items)) {
  929. if (mailbox('delete', 'syncjob', array('id' => $items)) === false) {
  930. if (isset($_SESSION['return'])) {
  931. echo json_encode($_SESSION['return']);
  932. }
  933. else {
  934. echo json_encode(array(
  935. 'type' => 'error',
  936. 'msg' => 'Deletion of items/s failed'
  937. ));
  938. }
  939. }
  940. else {
  941. if (isset($_SESSION['return'])) {
  942. echo json_encode($_SESSION['return']);
  943. }
  944. else {
  945. echo json_encode(array(
  946. 'type' => 'success',
  947. 'msg' => 'Task completed'
  948. ));
  949. }
  950. }
  951. }
  952. else {
  953. echo json_encode(array(
  954. 'type' => 'error',
  955. 'msg' => 'Cannot find id array in post data'
  956. ));
  957. }
  958. }
  959. else {
  960. echo json_encode(array(
  961. 'type' => 'error',
  962. 'msg' => 'Cannot find items in post data'
  963. ));
  964. }
  965. break;
  966. case "fwdhost":
  967. if (isset($_POST['items'])) {
  968. $items = (array)json_decode($_POST['items'], true);
  969. if (is_array($items)) {
  970. if (fwdhost('delete', array('forwardinghost' => $items)) === false) {
  971. if (isset($_SESSION['return'])) {
  972. echo json_encode($_SESSION['return']);
  973. }
  974. else {
  975. echo json_encode(array(
  976. 'type' => 'error',
  977. 'msg' => 'Deletion of items/s failed'
  978. ));
  979. }
  980. }
  981. else {
  982. if (isset($_SESSION['return'])) {
  983. echo json_encode($_SESSION['return']);
  984. }
  985. else {
  986. echo json_encode(array(
  987. 'type' => 'success',
  988. 'msg' => 'Task completed'
  989. ));
  990. }
  991. }
  992. }
  993. else {
  994. echo json_encode(array(
  995. 'type' => 'error',
  996. 'msg' => 'Cannot find forwardinghost array in post data'
  997. ));
  998. }
  999. }
  1000. else {
  1001. echo json_encode(array(
  1002. 'type' => 'error',
  1003. 'msg' => 'Cannot find items in post data'
  1004. ));
  1005. }
  1006. break;
  1007. case "dkim":
  1008. if (isset($_POST['items'])) {
  1009. $items = (array)json_decode($_POST['items'], true);
  1010. if (is_array($items)) {
  1011. if (dkim('delete', array('domains' => $items)) === false) {
  1012. if (isset($_SESSION['return'])) {
  1013. echo json_encode($_SESSION['return']);
  1014. }
  1015. else {
  1016. echo json_encode(array(
  1017. 'type' => 'error',
  1018. 'msg' => 'Deletion of items/s failed'
  1019. ));
  1020. }
  1021. }
  1022. else {
  1023. if (isset($_SESSION['return'])) {
  1024. echo json_encode($_SESSION['return']);
  1025. }
  1026. else {
  1027. echo json_encode(array(
  1028. 'type' => 'success',
  1029. 'msg' => 'Task completed'
  1030. ));
  1031. }
  1032. }
  1033. }
  1034. else {
  1035. echo json_encode(array(
  1036. 'type' => 'error',
  1037. 'msg' => 'Cannot find domains array in post data'
  1038. ));
  1039. }
  1040. }
  1041. else {
  1042. echo json_encode(array(
  1043. 'type' => 'error',
  1044. 'msg' => 'Cannot find items in post data'
  1045. ));
  1046. }
  1047. break;
  1048. case "domain":
  1049. if (isset($_POST['items'])) {
  1050. $items = (array)json_decode($_POST['items'], true);
  1051. if (is_array($items)) {
  1052. if (mailbox('delete', 'domain', array('domain' => $items)) === false) {
  1053. if (isset($_SESSION['return'])) {
  1054. echo json_encode($_SESSION['return']);
  1055. }
  1056. else {
  1057. echo json_encode(array(
  1058. 'type' => 'error',
  1059. 'msg' => 'Task failed'
  1060. ));
  1061. }
  1062. }
  1063. else {
  1064. if (isset($_SESSION['return'])) {
  1065. echo json_encode($_SESSION['return']);
  1066. }
  1067. else {
  1068. echo json_encode(array(
  1069. 'type' => 'success',
  1070. 'msg' => 'Task completed'
  1071. ));
  1072. }
  1073. }
  1074. }
  1075. else {
  1076. echo json_encode(array(
  1077. 'type' => 'error',
  1078. 'msg' => 'Cannot find domain array in post data'
  1079. ));
  1080. }
  1081. }
  1082. else {
  1083. echo json_encode(array(
  1084. 'type' => 'error',
  1085. 'msg' => 'Cannot find items in post data'
  1086. ));
  1087. }
  1088. break;
  1089. case "alias-domain":
  1090. if (isset($_POST['items'])) {
  1091. $items = (array)json_decode($_POST['items'], true);
  1092. if (is_array($items)) {
  1093. if (mailbox('delete', 'alias_domain', array('alias_domain' => $items)) === false) {
  1094. if (isset($_SESSION['return'])) {
  1095. echo json_encode($_SESSION['return']);
  1096. }
  1097. else {
  1098. echo json_encode(array(
  1099. 'type' => 'error',
  1100. 'msg' => 'Task failed'
  1101. ));
  1102. }
  1103. }
  1104. else {
  1105. if (isset($_SESSION['return'])) {
  1106. echo json_encode($_SESSION['return']);
  1107. }
  1108. else {
  1109. echo json_encode(array(
  1110. 'type' => 'success',
  1111. 'msg' => 'Task completed'
  1112. ));
  1113. }
  1114. }
  1115. }
  1116. else {
  1117. echo json_encode(array(
  1118. 'type' => 'error',
  1119. 'msg' => 'Cannot find alias_domain array in post data'
  1120. ));
  1121. }
  1122. }
  1123. else {
  1124. echo json_encode(array(
  1125. 'type' => 'error',
  1126. 'msg' => 'Cannot find items in post data'
  1127. ));
  1128. }
  1129. break;
  1130. case "mailbox":
  1131. if (isset($_POST['items'])) {
  1132. $items = (array)json_decode($_POST['items'], true);
  1133. if (is_array($items)) {
  1134. if (mailbox('delete', 'mailbox', array('username' => $items)) === false) {
  1135. if (isset($_SESSION['return'])) {
  1136. echo json_encode($_SESSION['return']);
  1137. }
  1138. else {
  1139. echo json_encode(array(
  1140. 'type' => 'error',
  1141. 'msg' => 'Task failed'
  1142. ));
  1143. }
  1144. }
  1145. else {
  1146. if (isset($_SESSION['return'])) {
  1147. echo json_encode($_SESSION['return']);
  1148. }
  1149. else {
  1150. echo json_encode(array(
  1151. 'type' => 'success',
  1152. 'msg' => 'Task completed'
  1153. ));
  1154. }
  1155. }
  1156. }
  1157. else {
  1158. echo json_encode(array(
  1159. 'type' => 'error',
  1160. 'msg' => 'Cannot find username array in post data'
  1161. ));
  1162. }
  1163. }
  1164. else {
  1165. echo json_encode(array(
  1166. 'type' => 'error',
  1167. 'msg' => 'Cannot find items in post data'
  1168. ));
  1169. }
  1170. break;
  1171. case "resource":
  1172. if (isset($_POST['items'])) {
  1173. $items = (array)json_decode($_POST['items'], true);
  1174. if (is_array($items)) {
  1175. if (mailbox('delete', 'resource', array('name' => $items)) === false) {
  1176. if (isset($_SESSION['return'])) {
  1177. echo json_encode($_SESSION['return']);
  1178. }
  1179. else {
  1180. echo json_encode(array(
  1181. 'type' => 'error',
  1182. 'msg' => 'Task failed'
  1183. ));
  1184. }
  1185. }
  1186. else {
  1187. if (isset($_SESSION['return'])) {
  1188. echo json_encode($_SESSION['return']);
  1189. }
  1190. else {
  1191. echo json_encode(array(
  1192. 'type' => 'success',
  1193. 'msg' => 'Task completed'
  1194. ));
  1195. }
  1196. }
  1197. }
  1198. else {
  1199. echo json_encode(array(
  1200. 'type' => 'error',
  1201. 'msg' => 'Cannot find name array in post data'
  1202. ));
  1203. }
  1204. }
  1205. else {
  1206. echo json_encode(array(
  1207. 'type' => 'error',
  1208. 'msg' => 'Cannot find items in post data'
  1209. ));
  1210. }
  1211. break;
  1212. case "mailbox-policy":
  1213. if (isset($_POST['items'])) {
  1214. $items = (array)json_decode($_POST['items'], true);
  1215. if (is_array($items)) {
  1216. if (policy('delete', 'mailbox', array('prefid' => $items)) === false) {
  1217. if (isset($_SESSION['return'])) {
  1218. echo json_encode($_SESSION['return']);
  1219. }
  1220. else {
  1221. echo json_encode(array(
  1222. 'type' => 'error',
  1223. 'msg' => 'Task failed'
  1224. ));
  1225. }
  1226. }
  1227. else {
  1228. if (isset($_SESSION['return'])) {
  1229. echo json_encode($_SESSION['return']);
  1230. }
  1231. else {
  1232. echo json_encode(array(
  1233. 'type' => 'success',
  1234. 'msg' => 'Task completed'
  1235. ));
  1236. }
  1237. }
  1238. }
  1239. else {
  1240. echo json_encode(array(
  1241. 'type' => 'error',
  1242. 'msg' => 'Cannot find name array in post data'
  1243. ));
  1244. }
  1245. }
  1246. else {
  1247. echo json_encode(array(
  1248. 'type' => 'error',
  1249. 'msg' => 'Cannot find items in post data'
  1250. ));
  1251. }
  1252. break;
  1253. case "domain-policy":
  1254. if (isset($_POST['items'])) {
  1255. $items = (array)json_decode($_POST['items'], true);
  1256. if (is_array($items)) {
  1257. if (policy('delete', 'domain', array('prefid' => $items)) === false) {
  1258. if (isset($_SESSION['return'])) {
  1259. echo json_encode($_SESSION['return']);
  1260. }
  1261. else {
  1262. echo json_encode(array(
  1263. 'type' => 'error',
  1264. 'msg' => 'Task failed'
  1265. ));
  1266. }
  1267. }
  1268. else {
  1269. if (isset($_SESSION['return'])) {
  1270. echo json_encode($_SESSION['return']);
  1271. }
  1272. else {
  1273. echo json_encode(array(
  1274. 'type' => 'success',
  1275. 'msg' => 'Task completed'
  1276. ));
  1277. }
  1278. }
  1279. }
  1280. else {
  1281. echo json_encode(array(
  1282. 'type' => 'error',
  1283. 'msg' => 'Cannot find name array in post data'
  1284. ));
  1285. }
  1286. }
  1287. else {
  1288. echo json_encode(array(
  1289. 'type' => 'error',
  1290. 'msg' => 'Cannot find items in post data'
  1291. ));
  1292. }
  1293. break;
  1294. case "time_limited_alias":
  1295. if (isset($_POST['items'])) {
  1296. $items = (array)json_decode($_POST['items'], true);
  1297. if (is_array($items)) {
  1298. if (mailbox('delete', 'time_limited_alias', array('address' => $items)) === false) {
  1299. if (isset($_SESSION['return'])) {
  1300. echo json_encode($_SESSION['return']);
  1301. }
  1302. else {
  1303. echo json_encode(array(
  1304. 'type' => 'error',
  1305. 'msg' => 'Task failed'
  1306. ));
  1307. }
  1308. }
  1309. else {
  1310. if (isset($_SESSION['return'])) {
  1311. echo json_encode($_SESSION['return']);
  1312. }
  1313. else {
  1314. echo json_encode(array(
  1315. 'type' => 'success',
  1316. 'msg' => 'Task completed'
  1317. ));
  1318. }
  1319. }
  1320. }
  1321. else {
  1322. echo json_encode(array(
  1323. 'type' => 'error',
  1324. 'msg' => 'Cannot find name array in post data'
  1325. ));
  1326. }
  1327. }
  1328. else {
  1329. echo json_encode(array(
  1330. 'type' => 'error',
  1331. 'msg' => 'Cannot find items in post data'
  1332. ));
  1333. }
  1334. break;
  1335. case "eas_cache":
  1336. if (isset($_POST['items'])) {
  1337. $items = (array)json_decode($_POST['items'], true);
  1338. if (is_array($items)) {
  1339. if (mailbox('delete', 'eas_cache', array('username' => $items)) === false) {
  1340. if (isset($_SESSION['return'])) {
  1341. echo json_encode($_SESSION['return']);
  1342. }
  1343. else {
  1344. echo json_encode(array(
  1345. 'type' => 'error',
  1346. 'msg' => 'Task failed'
  1347. ));
  1348. }
  1349. }
  1350. else {
  1351. if (isset($_SESSION['return'])) {
  1352. echo json_encode($_SESSION['return']);
  1353. }
  1354. else {
  1355. echo json_encode(array(
  1356. 'type' => 'success',
  1357. 'msg' => 'Task completed'
  1358. ));
  1359. }
  1360. }
  1361. }
  1362. else {
  1363. echo json_encode(array(
  1364. 'type' => 'error',
  1365. 'msg' => 'Cannot find name array in post data'
  1366. ));
  1367. }
  1368. }
  1369. else {
  1370. echo json_encode(array(
  1371. 'type' => 'error',
  1372. 'msg' => 'Cannot find items in post data'
  1373. ));
  1374. }
  1375. break;
  1376. case "domain-admin":
  1377. if (isset($_POST['items'])) {
  1378. $items = (array)json_decode($_POST['items'], true);
  1379. if (is_array($items)) {
  1380. if (domain_admin('delete', array('username' => $items)) === false) {
  1381. if (isset($_SESSION['return'])) {
  1382. echo json_encode($_SESSION['return']);
  1383. }
  1384. else {
  1385. echo json_encode(array(
  1386. 'type' => 'error',
  1387. 'msg' => 'Task failed'
  1388. ));
  1389. }
  1390. }
  1391. else {
  1392. if (isset($_SESSION['return'])) {
  1393. echo json_encode($_SESSION['return']);
  1394. }
  1395. else {
  1396. echo json_encode(array(
  1397. 'type' => 'success',
  1398. 'msg' => 'Task completed'
  1399. ));
  1400. }
  1401. }
  1402. }
  1403. else {
  1404. echo json_encode(array(
  1405. 'type' => 'error',
  1406. 'msg' => 'Cannot find name array in post data'
  1407. ));
  1408. }
  1409. }
  1410. else {
  1411. echo json_encode(array(
  1412. 'type' => 'error',
  1413. 'msg' => 'Cannot find items in post data'
  1414. ));
  1415. }
  1416. break;
  1417. }
  1418. break;
  1419. case "edit":
  1420. switch ($category) {
  1421. case "alias":
  1422. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1423. $items = (array)json_decode($_POST['items'], true);
  1424. $attr = (array)json_decode($_POST['attr'], true);
  1425. $postarray = array_merge(array('address' => $items), $attr);
  1426. if (is_array($postarray['address'])) {
  1427. if (mailbox('edit', 'alias', $postarray) === false) {
  1428. if (isset($_SESSION['return'])) {
  1429. echo json_encode($_SESSION['return']);
  1430. }
  1431. else {
  1432. echo json_encode(array(
  1433. 'type' => 'error',
  1434. 'msg' => 'Edit failed'
  1435. ));
  1436. }
  1437. exit();
  1438. }
  1439. else {
  1440. if (isset($_SESSION['return'])) {
  1441. echo json_encode($_SESSION['return']);
  1442. }
  1443. else {
  1444. echo json_encode(array(
  1445. 'type' => 'success',
  1446. 'msg' => 'Task completed'
  1447. ));
  1448. }
  1449. }
  1450. }
  1451. else {
  1452. echo json_encode(array(
  1453. 'type' => 'error',
  1454. 'msg' => 'Incomplete post data'
  1455. ));
  1456. }
  1457. }
  1458. else {
  1459. echo json_encode(array(
  1460. 'type' => 'error',
  1461. 'msg' => 'Incomplete post data'
  1462. ));
  1463. }
  1464. break;
  1465. case "delimiter_action":
  1466. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1467. $items = (array)json_decode($_POST['items'], true);
  1468. $attr = (array)json_decode($_POST['attr'], true);
  1469. $postarray = array_merge(array('username' => $items), $attr);
  1470. if (is_array($postarray['username'])) {
  1471. if (mailbox('edit', 'delimiter_action', $postarray) === false) {
  1472. if (isset($_SESSION['return'])) {
  1473. echo json_encode($_SESSION['return']);
  1474. }
  1475. else {
  1476. echo json_encode(array(
  1477. 'type' => 'error',
  1478. 'msg' => 'Edit failed'
  1479. ));
  1480. }
  1481. exit();
  1482. }
  1483. else {
  1484. if (isset($_SESSION['return'])) {
  1485. echo json_encode($_SESSION['return']);
  1486. }
  1487. else {
  1488. echo json_encode(array(
  1489. 'type' => 'success',
  1490. 'msg' => 'Task completed'
  1491. ));
  1492. }
  1493. }
  1494. }
  1495. else {
  1496. echo json_encode(array(
  1497. 'type' => 'error',
  1498. 'msg' => 'Incomplete post data'
  1499. ));
  1500. }
  1501. }
  1502. else {
  1503. echo json_encode(array(
  1504. 'type' => 'error',
  1505. 'msg' => 'Incomplete post data'
  1506. ));
  1507. }
  1508. break;
  1509. case "tls_policy":
  1510. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1511. $items = (array)json_decode($_POST['items'], true);
  1512. $attr = (array)json_decode($_POST['attr'], true);
  1513. $postarray = array_merge(array('username' => $items), $attr);
  1514. if (is_array($postarray['username'])) {
  1515. if (mailbox('edit', 'tls_policy', $postarray) === false) {
  1516. if (isset($_SESSION['return'])) {
  1517. echo json_encode($_SESSION['return']);
  1518. }
  1519. else {
  1520. echo json_encode(array(
  1521. 'type' => 'error',
  1522. 'msg' => 'Edit failed'
  1523. ));
  1524. }
  1525. exit();
  1526. }
  1527. else {
  1528. if (isset($_SESSION['return'])) {
  1529. echo json_encode($_SESSION['return']);
  1530. }
  1531. else {
  1532. echo json_encode(array(
  1533. 'type' => 'success',
  1534. 'msg' => 'Task completed'
  1535. ));
  1536. }
  1537. }
  1538. }
  1539. else {
  1540. echo json_encode(array(
  1541. 'type' => 'error',
  1542. 'msg' => 'Incomplete post data'
  1543. ));
  1544. }
  1545. }
  1546. else {
  1547. echo json_encode(array(
  1548. 'type' => 'error',
  1549. 'msg' => 'Incomplete post data'
  1550. ));
  1551. }
  1552. break;
  1553. case "time_limited_alias":
  1554. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1555. $items = (array)json_decode($_POST['items'], true);
  1556. $attr = (array)json_decode($_POST['attr'], true);
  1557. $postarray = array_merge(array('address' => $items), $attr);
  1558. if (is_array($postarray['address'])) {
  1559. if (mailbox('edit', 'time_limited_alias', $postarray) === false) {
  1560. if (isset($_SESSION['return'])) {
  1561. echo json_encode($_SESSION['return']);
  1562. }
  1563. else {
  1564. echo json_encode(array(
  1565. 'type' => 'error',
  1566. 'msg' => 'Edit failed'
  1567. ));
  1568. }
  1569. exit();
  1570. }
  1571. else {
  1572. if (isset($_SESSION['return'])) {
  1573. echo json_encode($_SESSION['return']);
  1574. }
  1575. else {
  1576. echo json_encode(array(
  1577. 'type' => 'success',
  1578. 'msg' => 'Task completed'
  1579. ));
  1580. }
  1581. }
  1582. }
  1583. else {
  1584. echo json_encode(array(
  1585. 'type' => 'error',
  1586. 'msg' => 'Incomplete post data'
  1587. ));
  1588. }
  1589. }
  1590. else {
  1591. echo json_encode(array(
  1592. 'type' => 'error',
  1593. 'msg' => 'Incomplete post data'
  1594. ));
  1595. }
  1596. break;
  1597. case "mailbox":
  1598. // sender_acl:0 removes all entries
  1599. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1600. $items = (array)json_decode($_POST['items'], true);
  1601. $attr = (array)json_decode($_POST['attr'], true);
  1602. $postarray = array_merge(array('username' => $items), $attr);
  1603. if (is_array($postarray['username'])) {
  1604. if (mailbox('edit', 'mailbox', $postarray) === false) {
  1605. if (isset($_SESSION['return'])) {
  1606. echo json_encode($_SESSION['return']);
  1607. }
  1608. else {
  1609. echo json_encode(array(
  1610. 'type' => 'error',
  1611. 'msg' => 'Edit failed'
  1612. ));
  1613. }
  1614. exit();
  1615. }
  1616. else {
  1617. if (isset($_SESSION['return'])) {
  1618. echo json_encode($_SESSION['return']);
  1619. }
  1620. else {
  1621. echo json_encode(array(
  1622. 'type' => 'success',
  1623. 'msg' => 'Task completed'
  1624. ));
  1625. }
  1626. }
  1627. }
  1628. else {
  1629. echo json_encode(array(
  1630. 'type' => 'error',
  1631. 'msg' => 'Incomplete post data'
  1632. ));
  1633. }
  1634. }
  1635. else {
  1636. echo json_encode(array(
  1637. 'type' => 'error',
  1638. 'msg' => 'Incomplete post data'
  1639. ));
  1640. }
  1641. break;
  1642. case "syncjob":
  1643. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1644. $items = (array)json_decode($_POST['items'], true);
  1645. $attr = (array)json_decode($_POST['attr'], true);
  1646. $postarray = array_merge(array('id' => $items), $attr);
  1647. if (is_array($postarray['id'])) {
  1648. if (mailbox('edit', 'syncjob', $postarray) === false) {
  1649. if (isset($_SESSION['return'])) {
  1650. echo json_encode($_SESSION['return']);
  1651. }
  1652. else {
  1653. echo json_encode(array(
  1654. 'type' => 'error',
  1655. 'msg' => 'Edit failed'
  1656. ));
  1657. }
  1658. exit();
  1659. }
  1660. else {
  1661. if (isset($_SESSION['return'])) {
  1662. echo json_encode($_SESSION['return']);
  1663. }
  1664. else {
  1665. echo json_encode(array(
  1666. 'type' => 'success',
  1667. 'msg' => 'Task completed'
  1668. ));
  1669. }
  1670. }
  1671. }
  1672. else {
  1673. echo json_encode(array(
  1674. 'type' => 'error',
  1675. 'msg' => 'Incomplete post data'
  1676. ));
  1677. }
  1678. }
  1679. else {
  1680. echo json_encode(array(
  1681. 'type' => 'error',
  1682. 'msg' => 'Incomplete post data'
  1683. ));
  1684. }
  1685. break;
  1686. case "resource":
  1687. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1688. $items = (array)json_decode($_POST['items'], true);
  1689. $attr = (array)json_decode($_POST['attr'], true);
  1690. $postarray = array_merge(array('name' => $items), $attr);
  1691. if (is_array($postarray['name'])) {
  1692. if (mailbox('edit', 'resource', $postarray) === false) {
  1693. if (isset($_SESSION['return'])) {
  1694. echo json_encode($_SESSION['return']);
  1695. }
  1696. else {
  1697. echo json_encode(array(
  1698. 'type' => 'error',
  1699. 'msg' => 'Edit failed'
  1700. ));
  1701. }
  1702. exit();
  1703. }
  1704. else {
  1705. if (isset($_SESSION['return'])) {
  1706. echo json_encode($_SESSION['return']);
  1707. }
  1708. else {
  1709. echo json_encode(array(
  1710. 'type' => 'success',
  1711. 'msg' => 'Task completed'
  1712. ));
  1713. }
  1714. }
  1715. }
  1716. else {
  1717. echo json_encode(array(
  1718. 'type' => 'error',
  1719. 'msg' => 'Incomplete post data'
  1720. ));
  1721. }
  1722. }
  1723. else {
  1724. echo json_encode(array(
  1725. 'type' => 'error',
  1726. 'msg' => 'Incomplete post data'
  1727. ));
  1728. }
  1729. break;
  1730. case "domain":
  1731. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1732. $items = (array)json_decode($_POST['items'], true);
  1733. $attr = (array)json_decode($_POST['attr'], true);
  1734. $postarray = array_merge(array('domain' => $items), $attr);
  1735. if (is_array($postarray['domain'])) {
  1736. if (mailbox('edit', 'domain', $postarray) === false) {
  1737. if (isset($_SESSION['return'])) {
  1738. echo json_encode($_SESSION['return']);
  1739. }
  1740. else {
  1741. echo json_encode(array(
  1742. 'type' => 'error',
  1743. 'msg' => 'Edit failed'
  1744. ));
  1745. }
  1746. exit();
  1747. }
  1748. else {
  1749. if (isset($_SESSION['return'])) {
  1750. echo json_encode($_SESSION['return']);
  1751. }
  1752. else {
  1753. echo json_encode(array(
  1754. 'type' => 'success',
  1755. 'msg' => 'Task completed'
  1756. ));
  1757. }
  1758. }
  1759. }
  1760. else {
  1761. echo json_encode(array(
  1762. 'type' => 'error',
  1763. 'msg' => 'Incomplete post data'
  1764. ));
  1765. }
  1766. }
  1767. else {
  1768. echo json_encode(array(
  1769. 'type' => 'error',
  1770. 'msg' => 'Incomplete post data'
  1771. ));
  1772. }
  1773. break;
  1774. case "domain-ratelimit":
  1775. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1776. $items = (array)json_decode($_POST['items'], true);
  1777. $attr = (array)json_decode($_POST['attr'], true);
  1778. $postarray = array_merge(array('domain' => $items), $attr);
  1779. if (is_array($postarray['domain'])) {
  1780. if (mailbox('edit', 'domain_ratelimit', $postarray) === false) {
  1781. if (isset($_SESSION['return'])) {
  1782. echo json_encode($_SESSION['return']);
  1783. }
  1784. else {
  1785. echo json_encode(array(
  1786. 'type' => 'error',
  1787. 'msg' => 'Edit failed'
  1788. ));
  1789. }
  1790. exit();
  1791. }
  1792. else {
  1793. if (isset($_SESSION['return'])) {
  1794. echo json_encode($_SESSION['return']);
  1795. }
  1796. else {
  1797. echo json_encode(array(
  1798. 'type' => 'success',
  1799. 'msg' => 'Task completed'
  1800. ));
  1801. }
  1802. }
  1803. }
  1804. else {
  1805. echo json_encode(array(
  1806. 'type' => 'error',
  1807. 'msg' => 'Incomplete post data'
  1808. ));
  1809. }
  1810. }
  1811. else {
  1812. echo json_encode(array(
  1813. 'type' => 'error',
  1814. 'msg' => 'Incomplete post data'
  1815. ));
  1816. }
  1817. break;
  1818. case "alias-domain":
  1819. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1820. $items = (array)json_decode($_POST['items'], true);
  1821. $attr = (array)json_decode($_POST['attr'], true);
  1822. $postarray = array_merge(array('alias_domain' => $items), $attr);
  1823. if (is_array($postarray['alias_domain'])) {
  1824. if (mailbox('edit', 'alias_domain', $postarray) === false) {
  1825. if (isset($_SESSION['return'])) {
  1826. echo json_encode($_SESSION['return']);
  1827. }
  1828. else {
  1829. echo json_encode(array(
  1830. 'type' => 'error',
  1831. 'msg' => 'Edit failed'
  1832. ));
  1833. }
  1834. exit();
  1835. }
  1836. else {
  1837. if (isset($_SESSION['return'])) {
  1838. echo json_encode($_SESSION['return']);
  1839. }
  1840. else {
  1841. echo json_encode(array(
  1842. 'type' => 'success',
  1843. 'msg' => 'Task completed'
  1844. ));
  1845. }
  1846. }
  1847. }
  1848. else {
  1849. echo json_encode(array(
  1850. 'type' => 'error',
  1851. 'msg' => 'Incomplete post data'
  1852. ));
  1853. }
  1854. }
  1855. else {
  1856. echo json_encode(array(
  1857. 'type' => 'error',
  1858. 'msg' => 'Incomplete post data'
  1859. ));
  1860. }
  1861. break;
  1862. case "spam-score":
  1863. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1864. $items = (array)json_decode($_POST['items'], true);
  1865. $attr = (array)json_decode($_POST['attr'], true);
  1866. $postarray = array_merge(array('username' => $items), $attr);
  1867. if (is_array($postarray['username'])) {
  1868. if (mailbox('edit', 'spam_score', $postarray) === false) {
  1869. if (isset($_SESSION['return'])) {
  1870. echo json_encode($_SESSION['return']);
  1871. }
  1872. else {
  1873. echo json_encode(array(
  1874. 'type' => 'error',
  1875. 'msg' => 'Edit failed'
  1876. ));
  1877. }
  1878. exit();
  1879. }
  1880. else {
  1881. if (isset($_SESSION['return'])) {
  1882. echo json_encode($_SESSION['return']);
  1883. }
  1884. else {
  1885. echo json_encode(array(
  1886. 'type' => 'success',
  1887. 'msg' => 'Task completed'
  1888. ));
  1889. }
  1890. }
  1891. }
  1892. else {
  1893. echo json_encode(array(
  1894. 'type' => 'error',
  1895. 'msg' => 'Incomplete post data'
  1896. ));
  1897. }
  1898. }
  1899. else {
  1900. echo json_encode(array(
  1901. 'type' => 'error',
  1902. 'msg' => 'Incomplete post data'
  1903. ));
  1904. }
  1905. break;
  1906. case "domain-admin":
  1907. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1908. $items = (array)json_decode($_POST['items'], true);
  1909. $attr = (array)json_decode($_POST['attr'], true);
  1910. $postarray = array_merge(array('username' => $items), $attr);
  1911. if (is_array($postarray['username'])) {
  1912. if (domain_admin('edit', $postarray) === false) {
  1913. if (isset($_SESSION['return'])) {
  1914. echo json_encode($_SESSION['return']);
  1915. }
  1916. else {
  1917. echo json_encode(array(
  1918. 'type' => 'error',
  1919. 'msg' => 'Edit failed'
  1920. ));
  1921. }
  1922. exit();
  1923. }
  1924. else {
  1925. if (isset($_SESSION['return'])) {
  1926. echo json_encode($_SESSION['return']);
  1927. }
  1928. else {
  1929. echo json_encode(array(
  1930. 'type' => 'success',
  1931. 'msg' => 'Task completed'
  1932. ));
  1933. }
  1934. }
  1935. }
  1936. else {
  1937. echo json_encode(array(
  1938. 'type' => 'error',
  1939. 'msg' => 'Incomplete post data'
  1940. ));
  1941. }
  1942. }
  1943. else {
  1944. echo json_encode(array(
  1945. 'type' => 'error',
  1946. 'msg' => 'Incomplete post data'
  1947. ));
  1948. }
  1949. break;
  1950. case "fwdhost":
  1951. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1952. $items = (array)json_decode($_POST['items'], true);
  1953. $attr = (array)json_decode($_POST['attr'], true);
  1954. $postarray = array_merge(array('fwdhost' => $items), $attr);
  1955. if (is_array($postarray['fwdhost'])) {
  1956. if (fwdhost('edit', $postarray) === false) {
  1957. if (isset($_SESSION['return'])) {
  1958. echo json_encode($_SESSION['return']);
  1959. }
  1960. else {
  1961. echo json_encode(array(
  1962. 'type' => 'error',
  1963. 'msg' => 'Edit failed'
  1964. ));
  1965. }
  1966. exit();
  1967. }
  1968. else {
  1969. if (isset($_SESSION['return'])) {
  1970. echo json_encode($_SESSION['return']);
  1971. }
  1972. else {
  1973. echo json_encode(array(
  1974. 'type' => 'success',
  1975. 'msg' => 'Task completed'
  1976. ));
  1977. }
  1978. }
  1979. }
  1980. else {
  1981. echo json_encode(array(
  1982. 'type' => 'error',
  1983. 'msg' => 'Incomplete post data'
  1984. ));
  1985. }
  1986. }
  1987. else {
  1988. echo json_encode(array(
  1989. 'type' => 'error',
  1990. 'msg' => 'Incomplete post data'
  1991. ));
  1992. }
  1993. break;
  1994. case "fail2ban":
  1995. // No items
  1996. if (isset($_POST['attr'])) {
  1997. $attr = (array)json_decode($_POST['attr'], true);
  1998. if (fail2ban('edit', $attr) === false) {
  1999. if (isset($_SESSION['return'])) {
  2000. echo json_encode($_SESSION['return']);
  2001. }
  2002. else {
  2003. echo json_encode(array(
  2004. 'type' => 'error',
  2005. 'msg' => 'Edit failed'
  2006. ));
  2007. }
  2008. exit();
  2009. }
  2010. else {
  2011. if (isset($_SESSION['return'])) {
  2012. echo json_encode($_SESSION['return']);
  2013. }
  2014. else {
  2015. echo json_encode(array(
  2016. 'type' => 'success',
  2017. 'msg' => 'Task completed'
  2018. ));
  2019. }
  2020. }
  2021. }
  2022. else {
  2023. echo json_encode(array(
  2024. 'type' => 'error',
  2025. 'msg' => 'Incomplete post data'
  2026. ));
  2027. }
  2028. break;
  2029. case "self":
  2030. // No items, logged-in user, users and domain admins
  2031. if ($_SESSION['mailcow_cc_role'] == "domainadmin") {
  2032. if (isset($_POST['attr'])) {
  2033. $attr = (array)json_decode($_POST['attr'], true);
  2034. if (domain_admin('edit', $attr) === false) {
  2035. if (isset($_SESSION['return'])) {
  2036. echo json_encode($_SESSION['return']);
  2037. }
  2038. else {
  2039. echo json_encode(array(
  2040. 'type' => 'error',
  2041. 'msg' => 'Edit failed'
  2042. ));
  2043. }
  2044. exit();
  2045. }
  2046. else {
  2047. if (isset($_SESSION['return'])) {
  2048. echo json_encode($_SESSION['return']);
  2049. }
  2050. else {
  2051. echo json_encode(array(
  2052. 'type' => 'success',
  2053. 'msg' => 'Task completed'
  2054. ));
  2055. }
  2056. }
  2057. }
  2058. else {
  2059. echo json_encode(array(
  2060. 'type' => 'error',
  2061. 'msg' => 'Incomplete post data'
  2062. ));
  2063. }
  2064. }
  2065. elseif ($_SESSION['mailcow_cc_role'] == "user") {
  2066. if (isset($_POST['attr'])) {
  2067. $attr = (array)json_decode($_POST['attr'], true);
  2068. if (edit_user_account($attr) === false) {
  2069. if (isset($_SESSION['return'])) {
  2070. echo json_encode($_SESSION['return']);
  2071. }
  2072. else {
  2073. echo json_encode(array(
  2074. 'type' => 'error',
  2075. 'msg' => 'Edit failed'
  2076. ));
  2077. }
  2078. exit();
  2079. }
  2080. else {
  2081. if (isset($_SESSION['return'])) {
  2082. echo json_encode($_SESSION['return']);
  2083. }
  2084. else {
  2085. echo json_encode(array(
  2086. 'type' => 'success',
  2087. 'msg' => 'Task completed'
  2088. ));
  2089. }
  2090. }
  2091. }
  2092. else {
  2093. echo json_encode(array(
  2094. 'type' => 'error',
  2095. 'msg' => 'Incomplete post data'
  2096. ));
  2097. }
  2098. }
  2099. elseif ($_SESSION['mailcow_cc_role'] == "admin") {
  2100. if (isset($_POST['attr'])) {
  2101. $attr = (array)json_decode($_POST['attr'], true);
  2102. if (edit_admin_account($attr) === false) {
  2103. if (isset($_SESSION['return'])) {
  2104. echo json_encode($_SESSION['return']);
  2105. }
  2106. else {
  2107. echo json_encode(array(
  2108. 'type' => 'error',
  2109. 'msg' => 'Edit failed'
  2110. ));
  2111. }
  2112. exit();
  2113. }
  2114. else {
  2115. if (isset($_SESSION['return'])) {
  2116. echo json_encode($_SESSION['return']);
  2117. }
  2118. else {
  2119. echo json_encode(array(
  2120. 'type' => 'success',
  2121. 'msg' => 'Task completed'
  2122. ));
  2123. }
  2124. }
  2125. }
  2126. else {
  2127. echo json_encode(array(
  2128. 'type' => 'error',
  2129. 'msg' => 'Incomplete post data'
  2130. ));
  2131. }
  2132. }
  2133. break;
  2134. }
  2135. break;
  2136. }
  2137. }
  2138. }