json_api.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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. }
  60. break;
  61. case "get":
  62. switch ($category) {
  63. case "domain":
  64. switch ($object) {
  65. case "all":
  66. $domains = mailbox('get', 'domains');
  67. if (!empty($domains)) {
  68. foreach ($domains as $domain) {
  69. if ($details = mailbox('get', 'domain_details', $domain)) {
  70. $data[] = $details;
  71. }
  72. else {
  73. continue;
  74. }
  75. }
  76. if (!isset($data) || empty($data)) {
  77. echo '{}';
  78. }
  79. else {
  80. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  81. }
  82. }
  83. else {
  84. echo '{}';
  85. }
  86. break;
  87. default:
  88. $data = mailbox('get', 'domain_details', $object);
  89. if (!isset($data) || empty($data)) {
  90. echo '{}';
  91. }
  92. else {
  93. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  94. }
  95. break;
  96. }
  97. break;
  98. case "logs":
  99. switch ($object) {
  100. case "dovecot":
  101. if (isset($extra) && !empty($extra)) {
  102. $extra = intval($extra);
  103. $logs = get_logs('dovecot-mailcow', $extra);
  104. }
  105. else {
  106. $logs = get_logs('dovecot-mailcow', -1);
  107. }
  108. if (isset($logs) && !empty($logs)) {
  109. echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  110. }
  111. else {
  112. echo '{}';
  113. }
  114. break;
  115. case "postfix":
  116. if (isset($extra) && !empty($extra)) {
  117. $extra = intval($extra);
  118. $logs = get_logs('postfix-mailcow', $extra);
  119. }
  120. else {
  121. $logs = get_logs('postfix-mailcow', -1);
  122. }
  123. if (isset($logs) && !empty($logs)) {
  124. echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  125. }
  126. else {
  127. echo '{}';
  128. }
  129. break;
  130. case "sogo":
  131. if (isset($extra) && !empty($extra)) {
  132. $extra = intval($extra);
  133. $logs = get_logs('sogo-mailcow', $extra);
  134. }
  135. else {
  136. $logs = get_logs('sogo-mailcow', -1);
  137. }
  138. if (isset($logs) && !empty($logs)) {
  139. echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  140. }
  141. else {
  142. echo '{}';
  143. }
  144. break;
  145. case "rspamd-history":
  146. $logs = get_logs('rspamd-history');
  147. if (isset($logs) && !empty($logs)) {
  148. echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  149. }
  150. else {
  151. echo '{}';
  152. }
  153. break;
  154. }
  155. break;
  156. case "mailbox":
  157. switch ($object) {
  158. case "all":
  159. $domains = mailbox('get', 'domains');
  160. if (!empty($domains)) {
  161. foreach ($domains as $domain) {
  162. $mailboxes = mailbox('get', 'mailboxes', $domain);
  163. if (!empty($mailboxes)) {
  164. foreach ($mailboxes as $mailbox) {
  165. if ($details = mailbox('get', 'mailbox_details', $mailbox)) {
  166. $data[] = $details;
  167. }
  168. else {
  169. continue;
  170. }
  171. }
  172. }
  173. }
  174. if (!isset($data) || empty($data)) {
  175. echo '{}';
  176. }
  177. else {
  178. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  179. }
  180. }
  181. else {
  182. echo '{}';
  183. }
  184. break;
  185. default:
  186. $data = mailbox('get', 'mailbox_details', $object);
  187. if (!isset($data) || empty($data)) {
  188. echo '{}';
  189. }
  190. else {
  191. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  192. }
  193. break;
  194. }
  195. break;
  196. case "syncjobs":
  197. switch ($object) {
  198. default:
  199. $data = mailbox('get', 'syncjobs', $object);
  200. if (!isset($data) || empty($data)) {
  201. echo '{}';
  202. }
  203. else {
  204. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  205. }
  206. break;
  207. }
  208. break;
  209. case "policy_wl_mailbox":
  210. switch ($object) {
  211. default:
  212. $data = policy('get', 'mailbox', $object)['whitelist'];
  213. if (!isset($data) || empty($data)) {
  214. echo '{}';
  215. }
  216. else {
  217. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  218. }
  219. break;
  220. }
  221. break;
  222. case "policy_bl_mailbox":
  223. switch ($object) {
  224. default:
  225. $data = policy('get', 'mailbox', $object)['blacklist'];
  226. if (!isset($data) || empty($data)) {
  227. echo '{}';
  228. }
  229. else {
  230. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  231. }
  232. break;
  233. }
  234. break;
  235. case "policy_wl_domain":
  236. switch ($object) {
  237. default:
  238. $data = policy('get', 'domain', $object)['whitelist'];
  239. if (!isset($data) || empty($data)) {
  240. echo '{}';
  241. }
  242. else {
  243. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  244. }
  245. break;
  246. }
  247. break;
  248. case "policy_bl_domain":
  249. switch ($object) {
  250. default:
  251. $data = policy('get', 'domain', $object)['blacklist'];
  252. if (!isset($data) || empty($data)) {
  253. echo '{}';
  254. }
  255. else {
  256. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  257. }
  258. break;
  259. }
  260. break;
  261. case "time_limited_aliases":
  262. switch ($object) {
  263. default:
  264. $data = mailbox('get', 'time_limited_aliases', $object);
  265. if (!isset($data) || empty($data)) {
  266. echo '{}';
  267. }
  268. else {
  269. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  270. }
  271. break;
  272. }
  273. break;
  274. case "resource":
  275. switch ($object) {
  276. case "all":
  277. $domains = mailbox('get', 'domains');
  278. if (!empty($domains)) {
  279. foreach ($domains as $domain) {
  280. $resources = mailbox('get', 'resources', $domain);
  281. if (!empty($resources)) {
  282. foreach ($resources as $resource) {
  283. if ($details = mailbox('get', 'resource_details', $resource)) {
  284. $data[] = $details;
  285. }
  286. else {
  287. continue;
  288. }
  289. }
  290. }
  291. }
  292. if (!isset($data) || empty($data)) {
  293. echo '{}';
  294. }
  295. else {
  296. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  297. }
  298. }
  299. else {
  300. echo '{}';
  301. }
  302. break;
  303. default:
  304. $data = mailbox('get', 'resource_details', $object);
  305. if (!isset($data) || empty($data)) {
  306. echo '{}';
  307. }
  308. else {
  309. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  310. }
  311. break;
  312. }
  313. break;
  314. case "fwdhost":
  315. switch ($object) {
  316. case "all":
  317. $fwdhosts = get_forwarding_hosts();
  318. if (!empty($fwdhosts)) {
  319. foreach ($fwdhosts as $fwdhost) {
  320. if ($details = get_forwarding_host_details($fwdhost)) {
  321. $data[] = $details;
  322. }
  323. else {
  324. continue;
  325. }
  326. }
  327. }
  328. if (!isset($data) || empty($data)) {
  329. echo '{}';
  330. }
  331. else {
  332. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  333. }
  334. break;
  335. default:
  336. $data = get_forwarding_host_details($object);
  337. if (!isset($data) || empty($data)) {
  338. echo '{}';
  339. }
  340. else {
  341. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  342. }
  343. break;
  344. }
  345. break;
  346. case "alias-domain":
  347. switch ($object) {
  348. case "all":
  349. $domains = mailbox('get', 'domains');
  350. if (!empty($domains)) {
  351. foreach ($domains as $domain) {
  352. $alias_domains = mailbox('get', 'alias_domains', $domain);
  353. if (!empty($alias_domains)) {
  354. foreach ($alias_domains as $alias_domain) {
  355. if ($details = mailbox('get', 'alias_domain_details', $alias_domain)) {
  356. $data[] = $details;
  357. }
  358. else {
  359. continue;
  360. }
  361. }
  362. }
  363. }
  364. if (!isset($data) || empty($data)) {
  365. echo '{}';
  366. }
  367. else {
  368. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  369. }
  370. }
  371. else {
  372. echo '{}';
  373. }
  374. break;
  375. default:
  376. $data = mailbox('get', 'alias_domains', $object);
  377. if (!isset($data) || empty($data)) {
  378. echo '{}';
  379. }
  380. else {
  381. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  382. }
  383. break;
  384. }
  385. break;
  386. case "alias":
  387. switch ($object) {
  388. case "all":
  389. $domains = array_merge(mailbox('get', 'domains'),mailbox('get', 'alias_domains'));
  390. if (!empty($domains)) {
  391. foreach ($domains as $domain) {
  392. $aliases = mailbox('get', 'aliases', $domain);
  393. if (!empty($aliases)) {
  394. foreach ($aliases as $alias) {
  395. if ($details = mailbox('get', 'alias_details', $alias)) {
  396. $data[] = $details;
  397. }
  398. else {
  399. continue;
  400. }
  401. }
  402. }
  403. }
  404. if (!isset($data) || empty($data)) {
  405. echo '{}';
  406. }
  407. else {
  408. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  409. }
  410. }
  411. else {
  412. echo '{}';
  413. }
  414. break;
  415. default:
  416. $data = mailbox('get', 'alias_details', $object);
  417. if (!isset($data) || empty($data)) {
  418. echo '{}';
  419. }
  420. else {
  421. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  422. }
  423. break;
  424. }
  425. break;
  426. case "domain-admin":
  427. switch ($object) {
  428. case "all":
  429. $domain_admins = get_domain_admins();
  430. if (!empty($domain_admins)) {
  431. foreach ($domain_admins as $domain_admin) {
  432. if ($details = get_domain_admin_details($domain_admin)) {
  433. $data[] = $details;
  434. }
  435. else {
  436. continue;
  437. }
  438. }
  439. if (!isset($data) || empty($data)) {
  440. echo '{}';
  441. }
  442. else {
  443. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  444. }
  445. }
  446. else {
  447. echo '{}';
  448. }
  449. break;
  450. default:
  451. $data = get_domain_admin_details($object);
  452. if (!isset($data) || empty($data)) {
  453. echo '{}';
  454. }
  455. else {
  456. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  457. }
  458. break;
  459. }
  460. break;
  461. case "u2f-registration":
  462. header('Content-Type: application/javascript');
  463. if (($_SESSION["mailcow_cc_role"] == "admin" || $_SESSION["mailcow_cc_role"] == "domainadmin") && $_SESSION["mailcow_cc_username"] == $object) {
  464. $data = $u2f->getRegisterData(get_u2f_registrations($object));
  465. list($req, $sigs) = $data;
  466. $_SESSION['regReq'] = json_encode($req);
  467. echo 'var req = ' . json_encode($req) . '; var sigs = ' . json_encode($sigs) . ';';
  468. }
  469. else {
  470. return;
  471. }
  472. break;
  473. case "u2f-authentication":
  474. header('Content-Type: application/javascript');
  475. if (isset($_SESSION['pending_mailcow_cc_username']) && $_SESSION['pending_mailcow_cc_username'] == $object) {
  476. $reqs = json_encode($u2f->getAuthenticateData(get_u2f_registrations($object)));
  477. $_SESSION['authReq'] = $reqs;
  478. echo 'var req = ' . $reqs . ';';
  479. }
  480. else {
  481. return;
  482. }
  483. break;
  484. default:
  485. echo '{}';
  486. break;
  487. }
  488. break;
  489. case "delete":
  490. switch ($category) {
  491. case "alias":
  492. if (isset($_POST['items'])) {
  493. $items = (array)json_decode($_POST['items'], true);
  494. if (is_array($items)) {
  495. if (mailbox('delete', 'alias', array('address' => $items)) === false) {
  496. if (isset($_SESSION['return'])) {
  497. echo json_encode($_SESSION['return']);
  498. }
  499. else {
  500. echo json_encode(array(
  501. 'type' => 'error',
  502. 'msg' => 'Deletion of items/s failed'
  503. ));
  504. }
  505. }
  506. else {
  507. if (isset($_SESSION['return'])) {
  508. echo json_encode($_SESSION['return']);
  509. }
  510. else {
  511. echo json_encode(array(
  512. 'type' => 'success',
  513. 'msg' => 'Task completed'
  514. ));
  515. }
  516. }
  517. }
  518. else {
  519. echo json_encode(array(
  520. 'type' => 'error',
  521. 'msg' => 'Cannot find address array in post data'
  522. ));
  523. }
  524. }
  525. else {
  526. echo json_encode(array(
  527. 'type' => 'error',
  528. 'msg' => 'Cannot find items in post data'
  529. ));
  530. }
  531. break;
  532. case "syncjob":
  533. if (isset($_POST['items'])) {
  534. $items = (array)json_decode($_POST['items'], true);
  535. if (is_array($items)) {
  536. if (mailbox('delete', 'syncjob', array('id' => $items)) === false) {
  537. if (isset($_SESSION['return'])) {
  538. echo json_encode($_SESSION['return']);
  539. }
  540. else {
  541. echo json_encode(array(
  542. 'type' => 'error',
  543. 'msg' => 'Deletion of items/s failed'
  544. ));
  545. }
  546. }
  547. else {
  548. if (isset($_SESSION['return'])) {
  549. echo json_encode($_SESSION['return']);
  550. }
  551. else {
  552. echo json_encode(array(
  553. 'type' => 'success',
  554. 'msg' => 'Task completed'
  555. ));
  556. }
  557. }
  558. }
  559. else {
  560. echo json_encode(array(
  561. 'type' => 'error',
  562. 'msg' => 'Cannot find id array in post data'
  563. ));
  564. }
  565. }
  566. else {
  567. echo json_encode(array(
  568. 'type' => 'error',
  569. 'msg' => 'Cannot find items in post data'
  570. ));
  571. }
  572. break;
  573. case "fwdhost":
  574. if (isset($_POST['items'])) {
  575. $items = (array)json_decode($_POST['items'], true);
  576. if (is_array($items)) {
  577. if (delete_forwarding_host(array('forwardinghost' => $items)) === false) {
  578. if (isset($_SESSION['return'])) {
  579. echo json_encode($_SESSION['return']);
  580. }
  581. else {
  582. echo json_encode(array(
  583. 'type' => 'error',
  584. 'msg' => 'Deletion of items/s failed'
  585. ));
  586. }
  587. }
  588. else {
  589. if (isset($_SESSION['return'])) {
  590. echo json_encode($_SESSION['return']);
  591. }
  592. else {
  593. echo json_encode(array(
  594. 'type' => 'success',
  595. 'msg' => 'Task completed'
  596. ));
  597. }
  598. }
  599. }
  600. else {
  601. echo json_encode(array(
  602. 'type' => 'error',
  603. 'msg' => 'Cannot find forwardinghost array in post data'
  604. ));
  605. }
  606. }
  607. else {
  608. echo json_encode(array(
  609. 'type' => 'error',
  610. 'msg' => 'Cannot find items in post data'
  611. ));
  612. }
  613. break;
  614. case "dkim":
  615. if (isset($_POST['items'])) {
  616. $items = (array)json_decode($_POST['items'], true);
  617. if (is_array($items)) {
  618. if (dkim_delete_key(array('domains' => $items)) === false) {
  619. if (isset($_SESSION['return'])) {
  620. echo json_encode($_SESSION['return']);
  621. }
  622. else {
  623. echo json_encode(array(
  624. 'type' => 'error',
  625. 'msg' => 'Deletion of items/s failed'
  626. ));
  627. }
  628. }
  629. else {
  630. if (isset($_SESSION['return'])) {
  631. echo json_encode($_SESSION['return']);
  632. }
  633. else {
  634. echo json_encode(array(
  635. 'type' => 'success',
  636. 'msg' => 'Task completed'
  637. ));
  638. }
  639. }
  640. }
  641. else {
  642. echo json_encode(array(
  643. 'type' => 'error',
  644. 'msg' => 'Cannot find domains array in post data'
  645. ));
  646. }
  647. }
  648. else {
  649. echo json_encode(array(
  650. 'type' => 'error',
  651. 'msg' => 'Cannot find items in post data'
  652. ));
  653. }
  654. break;
  655. case "domain":
  656. if (isset($_POST['items'])) {
  657. $items = (array)json_decode($_POST['items'], true);
  658. if (is_array($items)) {
  659. if (mailbox('delete', 'domain', array('domain' => $items)) === false) {
  660. if (isset($_SESSION['return'])) {
  661. echo json_encode($_SESSION['return']);
  662. }
  663. else {
  664. echo json_encode(array(
  665. 'type' => 'error',
  666. 'msg' => 'Task failed'
  667. ));
  668. }
  669. }
  670. else {
  671. if (isset($_SESSION['return'])) {
  672. echo json_encode($_SESSION['return']);
  673. }
  674. else {
  675. echo json_encode(array(
  676. 'type' => 'success',
  677. 'msg' => 'Task completed'
  678. ));
  679. }
  680. }
  681. }
  682. else {
  683. echo json_encode(array(
  684. 'type' => 'error',
  685. 'msg' => 'Cannot find domain array in post data'
  686. ));
  687. }
  688. }
  689. else {
  690. echo json_encode(array(
  691. 'type' => 'error',
  692. 'msg' => 'Cannot find items in post data'
  693. ));
  694. }
  695. break;
  696. case "alias-domain":
  697. if (isset($_POST['items'])) {
  698. $items = (array)json_decode($_POST['items'], true);
  699. if (is_array($items)) {
  700. if (mailbox('delete', 'alias_domain', array('alias_domain' => $items)) === false) {
  701. if (isset($_SESSION['return'])) {
  702. echo json_encode($_SESSION['return']);
  703. }
  704. else {
  705. echo json_encode(array(
  706. 'type' => 'error',
  707. 'msg' => 'Task failed'
  708. ));
  709. }
  710. }
  711. else {
  712. if (isset($_SESSION['return'])) {
  713. echo json_encode($_SESSION['return']);
  714. }
  715. else {
  716. echo json_encode(array(
  717. 'type' => 'success',
  718. 'msg' => 'Task completed'
  719. ));
  720. }
  721. }
  722. }
  723. else {
  724. echo json_encode(array(
  725. 'type' => 'error',
  726. 'msg' => 'Cannot find alias_domain array in post data'
  727. ));
  728. }
  729. }
  730. else {
  731. echo json_encode(array(
  732. 'type' => 'error',
  733. 'msg' => 'Cannot find items in post data'
  734. ));
  735. }
  736. break;
  737. case "mailbox":
  738. if (isset($_POST['items'])) {
  739. $items = (array)json_decode($_POST['items'], true);
  740. if (is_array($items)) {
  741. if (mailbox('delete', 'mailbox', array('username' => $items)) === false) {
  742. if (isset($_SESSION['return'])) {
  743. echo json_encode($_SESSION['return']);
  744. }
  745. else {
  746. echo json_encode(array(
  747. 'type' => 'error',
  748. 'msg' => 'Task failed'
  749. ));
  750. }
  751. }
  752. else {
  753. if (isset($_SESSION['return'])) {
  754. echo json_encode($_SESSION['return']);
  755. }
  756. else {
  757. echo json_encode(array(
  758. 'type' => 'success',
  759. 'msg' => 'Task completed'
  760. ));
  761. }
  762. }
  763. }
  764. else {
  765. echo json_encode(array(
  766. 'type' => 'error',
  767. 'msg' => 'Cannot find username array in post data'
  768. ));
  769. }
  770. }
  771. else {
  772. echo json_encode(array(
  773. 'type' => 'error',
  774. 'msg' => 'Cannot find items in post data'
  775. ));
  776. }
  777. break;
  778. case "resource":
  779. if (isset($_POST['items'])) {
  780. $items = (array)json_decode($_POST['items'], true);
  781. if (is_array($items)) {
  782. if (mailbox('delete', 'resource', array('name' => $items)) === false) {
  783. if (isset($_SESSION['return'])) {
  784. echo json_encode($_SESSION['return']);
  785. }
  786. else {
  787. echo json_encode(array(
  788. 'type' => 'error',
  789. 'msg' => 'Task failed'
  790. ));
  791. }
  792. }
  793. else {
  794. if (isset($_SESSION['return'])) {
  795. echo json_encode($_SESSION['return']);
  796. }
  797. else {
  798. echo json_encode(array(
  799. 'type' => 'success',
  800. 'msg' => 'Task completed'
  801. ));
  802. }
  803. }
  804. }
  805. else {
  806. echo json_encode(array(
  807. 'type' => 'error',
  808. 'msg' => 'Cannot find name array in post data'
  809. ));
  810. }
  811. }
  812. else {
  813. echo json_encode(array(
  814. 'type' => 'error',
  815. 'msg' => 'Cannot find items in post data'
  816. ));
  817. }
  818. break;
  819. case "policy_mailbox":
  820. if (isset($_POST['items'])) {
  821. $items = (array)json_decode($_POST['items'], true);
  822. if (is_array($items)) {
  823. if (policy('delete', 'mailbox', array('prefid' => $items)) === false) {
  824. if (isset($_SESSION['return'])) {
  825. echo json_encode($_SESSION['return']);
  826. }
  827. else {
  828. echo json_encode(array(
  829. 'type' => 'error',
  830. 'msg' => 'Task failed'
  831. ));
  832. }
  833. }
  834. else {
  835. if (isset($_SESSION['return'])) {
  836. echo json_encode($_SESSION['return']);
  837. }
  838. else {
  839. echo json_encode(array(
  840. 'type' => 'success',
  841. 'msg' => 'Task completed'
  842. ));
  843. }
  844. }
  845. }
  846. else {
  847. echo json_encode(array(
  848. 'type' => 'error',
  849. 'msg' => 'Cannot find name array in post data'
  850. ));
  851. }
  852. }
  853. else {
  854. echo json_encode(array(
  855. 'type' => 'error',
  856. 'msg' => 'Cannot find items in post data'
  857. ));
  858. }
  859. break;
  860. case "time_limited_alias":
  861. if (isset($_POST['items'])) {
  862. $items = (array)json_decode($_POST['items'], true);
  863. if (is_array($items)) {
  864. if (mailbox('delete', 'time_limited_alias', array('address' => $items)) === false) {
  865. if (isset($_SESSION['return'])) {
  866. echo json_encode($_SESSION['return']);
  867. }
  868. else {
  869. echo json_encode(array(
  870. 'type' => 'error',
  871. 'msg' => 'Task failed'
  872. ));
  873. }
  874. }
  875. else {
  876. if (isset($_SESSION['return'])) {
  877. echo json_encode($_SESSION['return']);
  878. }
  879. else {
  880. echo json_encode(array(
  881. 'type' => 'success',
  882. 'msg' => 'Task completed'
  883. ));
  884. }
  885. }
  886. }
  887. else {
  888. echo json_encode(array(
  889. 'type' => 'error',
  890. 'msg' => 'Cannot find name array in post data'
  891. ));
  892. }
  893. }
  894. else {
  895. echo json_encode(array(
  896. 'type' => 'error',
  897. 'msg' => 'Cannot find items in post data'
  898. ));
  899. }
  900. break;
  901. case "eas_cache":
  902. if (isset($_POST['items'])) {
  903. $items = (array)json_decode($_POST['items'], true);
  904. if (is_array($items)) {
  905. if (mailbox('delete', 'eas_cache', array('username' => $items)) === false) {
  906. if (isset($_SESSION['return'])) {
  907. echo json_encode($_SESSION['return']);
  908. }
  909. else {
  910. echo json_encode(array(
  911. 'type' => 'error',
  912. 'msg' => 'Task failed'
  913. ));
  914. }
  915. }
  916. else {
  917. if (isset($_SESSION['return'])) {
  918. echo json_encode($_SESSION['return']);
  919. }
  920. else {
  921. echo json_encode(array(
  922. 'type' => 'success',
  923. 'msg' => 'Task completed'
  924. ));
  925. }
  926. }
  927. }
  928. else {
  929. echo json_encode(array(
  930. 'type' => 'error',
  931. 'msg' => 'Cannot find name array in post data'
  932. ));
  933. }
  934. }
  935. else {
  936. echo json_encode(array(
  937. 'type' => 'error',
  938. 'msg' => 'Cannot find items in post data'
  939. ));
  940. }
  941. break;
  942. }
  943. break;
  944. case "edit":
  945. switch ($category) {
  946. case "alias":
  947. if (isset($_POST['items']) && isset($_POST['attr'])) {
  948. $items = (array)json_decode($_POST['items'], true);
  949. $attr = (array)json_decode($_POST['attr'], true);
  950. $postarray = array_merge(array('address' => $items), $attr);
  951. if (is_array($postarray['address'])) {
  952. if (mailbox('edit', 'alias', $postarray) === false) {
  953. if (isset($_SESSION['return'])) {
  954. echo json_encode($_SESSION['return']);
  955. }
  956. else {
  957. echo json_encode(array(
  958. 'type' => 'error',
  959. 'msg' => 'Edit failed'
  960. ));
  961. }
  962. exit();
  963. }
  964. else {
  965. if (isset($_SESSION['return'])) {
  966. echo json_encode($_SESSION['return']);
  967. }
  968. else {
  969. echo json_encode(array(
  970. 'type' => 'success',
  971. 'msg' => 'Task completed'
  972. ));
  973. }
  974. }
  975. }
  976. else {
  977. echo json_encode(array(
  978. 'type' => 'error',
  979. 'msg' => 'Incomplete post data'
  980. ));
  981. }
  982. }
  983. else {
  984. echo json_encode(array(
  985. 'type' => 'error',
  986. 'msg' => 'Incomplete post data'
  987. ));
  988. }
  989. break;
  990. case "delimiter_action":
  991. if (isset($_POST['items']) && isset($_POST['attr'])) {
  992. $items = (array)json_decode($_POST['items'], true);
  993. $attr = (array)json_decode($_POST['attr'], true);
  994. $postarray = array_merge(array('username' => $items), $attr);
  995. if (is_array($postarray['username'])) {
  996. if (mailbox('edit', 'delimiter_action', $postarray) === false) {
  997. if (isset($_SESSION['return'])) {
  998. echo json_encode($_SESSION['return']);
  999. }
  1000. else {
  1001. echo json_encode(array(
  1002. 'type' => 'error',
  1003. 'msg' => 'Edit failed'
  1004. ));
  1005. }
  1006. exit();
  1007. }
  1008. else {
  1009. if (isset($_SESSION['return'])) {
  1010. echo json_encode($_SESSION['return']);
  1011. }
  1012. else {
  1013. echo json_encode(array(
  1014. 'type' => 'success',
  1015. 'msg' => 'Task completed'
  1016. ));
  1017. }
  1018. }
  1019. }
  1020. else {
  1021. echo json_encode(array(
  1022. 'type' => 'error',
  1023. 'msg' => 'Incomplete post data'
  1024. ));
  1025. }
  1026. }
  1027. else {
  1028. echo json_encode(array(
  1029. 'type' => 'error',
  1030. 'msg' => 'Incomplete post data'
  1031. ));
  1032. }
  1033. break;
  1034. case "tls_policy":
  1035. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1036. $items = (array)json_decode($_POST['items'], true);
  1037. $attr = (array)json_decode($_POST['attr'], true);
  1038. $postarray = array_merge(array('username' => $items), $attr);
  1039. if (is_array($postarray['username'])) {
  1040. if (mailbox('edit', 'tls_policy', $postarray) === false) {
  1041. if (isset($_SESSION['return'])) {
  1042. echo json_encode($_SESSION['return']);
  1043. }
  1044. else {
  1045. echo json_encode(array(
  1046. 'type' => 'error',
  1047. 'msg' => 'Edit failed'
  1048. ));
  1049. }
  1050. exit();
  1051. }
  1052. else {
  1053. if (isset($_SESSION['return'])) {
  1054. echo json_encode($_SESSION['return']);
  1055. }
  1056. else {
  1057. echo json_encode(array(
  1058. 'type' => 'success',
  1059. 'msg' => 'Task completed'
  1060. ));
  1061. }
  1062. }
  1063. }
  1064. else {
  1065. echo json_encode(array(
  1066. 'type' => 'error',
  1067. 'msg' => 'Incomplete post data'
  1068. ));
  1069. }
  1070. }
  1071. else {
  1072. echo json_encode(array(
  1073. 'type' => 'error',
  1074. 'msg' => 'Incomplete post data'
  1075. ));
  1076. }
  1077. break;
  1078. case "time_limited_alias":
  1079. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1080. $items = (array)json_decode($_POST['items'], true);
  1081. $attr = (array)json_decode($_POST['attr'], true);
  1082. $postarray = array_merge(array('address' => $items), $attr);
  1083. if (is_array($postarray['address'])) {
  1084. if (mailbox('edit', 'time_limited_alias', $postarray) === false) {
  1085. if (isset($_SESSION['return'])) {
  1086. echo json_encode($_SESSION['return']);
  1087. }
  1088. else {
  1089. echo json_encode(array(
  1090. 'type' => 'error',
  1091. 'msg' => 'Edit failed'
  1092. ));
  1093. }
  1094. exit();
  1095. }
  1096. else {
  1097. if (isset($_SESSION['return'])) {
  1098. echo json_encode($_SESSION['return']);
  1099. }
  1100. else {
  1101. echo json_encode(array(
  1102. 'type' => 'success',
  1103. 'msg' => 'Task completed'
  1104. ));
  1105. }
  1106. }
  1107. }
  1108. else {
  1109. echo json_encode(array(
  1110. 'type' => 'error',
  1111. 'msg' => 'Incomplete post data'
  1112. ));
  1113. }
  1114. }
  1115. else {
  1116. echo json_encode(array(
  1117. 'type' => 'error',
  1118. 'msg' => 'Incomplete post data'
  1119. ));
  1120. }
  1121. break;
  1122. case "mailbox":
  1123. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1124. $items = (array)json_decode($_POST['items'], true);
  1125. $attr = (array)json_decode($_POST['attr'], true);
  1126. $postarray = array_merge(array('username' => $items), $attr);
  1127. if (is_array($postarray['username'])) {
  1128. if (mailbox('edit', 'mailbox', $postarray) === false) {
  1129. if (isset($_SESSION['return'])) {
  1130. echo json_encode($_SESSION['return']);
  1131. }
  1132. else {
  1133. echo json_encode(array(
  1134. 'type' => 'error',
  1135. 'msg' => 'Edit failed'
  1136. ));
  1137. }
  1138. exit();
  1139. }
  1140. else {
  1141. if (isset($_SESSION['return'])) {
  1142. echo json_encode($_SESSION['return']);
  1143. }
  1144. else {
  1145. echo json_encode(array(
  1146. 'type' => 'success',
  1147. 'msg' => 'Task completed'
  1148. ));
  1149. }
  1150. }
  1151. }
  1152. else {
  1153. echo json_encode(array(
  1154. 'type' => 'error',
  1155. 'msg' => 'Incomplete post data'
  1156. ));
  1157. }
  1158. }
  1159. else {
  1160. echo json_encode(array(
  1161. 'type' => 'error',
  1162. 'msg' => 'Incomplete post data'
  1163. ));
  1164. }
  1165. break;
  1166. case "syncjob":
  1167. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1168. $items = (array)json_decode($_POST['items'], true);
  1169. $attr = (array)json_decode($_POST['attr'], true);
  1170. $postarray = array_merge(array('id' => $items), $attr);
  1171. if (is_array($postarray['id'])) {
  1172. if (mailbox('edit', 'syncjob', $postarray) === false) {
  1173. if (isset($_SESSION['return'])) {
  1174. echo json_encode($_SESSION['return']);
  1175. }
  1176. else {
  1177. echo json_encode(array(
  1178. 'type' => 'error',
  1179. 'msg' => 'Edit failed'
  1180. ));
  1181. }
  1182. exit();
  1183. }
  1184. else {
  1185. if (isset($_SESSION['return'])) {
  1186. echo json_encode($_SESSION['return']);
  1187. }
  1188. else {
  1189. echo json_encode(array(
  1190. 'type' => 'success',
  1191. 'msg' => 'Task completed'
  1192. ));
  1193. }
  1194. }
  1195. }
  1196. else {
  1197. echo json_encode(array(
  1198. 'type' => 'error',
  1199. 'msg' => 'Incomplete post data'
  1200. ));
  1201. }
  1202. }
  1203. else {
  1204. echo json_encode(array(
  1205. 'type' => 'error',
  1206. 'msg' => 'Incomplete post data'
  1207. ));
  1208. }
  1209. break;
  1210. case "resource":
  1211. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1212. $items = (array)json_decode($_POST['items'], true);
  1213. $attr = (array)json_decode($_POST['attr'], true);
  1214. $postarray = array_merge(array('name' => $items), $attr);
  1215. if (is_array($postarray['name'])) {
  1216. if (mailbox('edit', 'resource', $postarray) === 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' => 'Edit failed'
  1224. ));
  1225. }
  1226. exit();
  1227. }
  1228. else {
  1229. if (isset($_SESSION['return'])) {
  1230. echo json_encode($_SESSION['return']);
  1231. }
  1232. else {
  1233. echo json_encode(array(
  1234. 'type' => 'success',
  1235. 'msg' => 'Task completed'
  1236. ));
  1237. }
  1238. }
  1239. }
  1240. else {
  1241. echo json_encode(array(
  1242. 'type' => 'error',
  1243. 'msg' => 'Incomplete post data'
  1244. ));
  1245. }
  1246. }
  1247. else {
  1248. echo json_encode(array(
  1249. 'type' => 'error',
  1250. 'msg' => 'Incomplete post data'
  1251. ));
  1252. }
  1253. break;
  1254. case "domain":
  1255. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1256. $items = (array)json_decode($_POST['items'], true);
  1257. $attr = (array)json_decode($_POST['attr'], true);
  1258. $postarray = array_merge(array('domain' => $items), $attr);
  1259. if (is_array($postarray['domain'])) {
  1260. if (mailbox('edit', 'domain', $postarray) === false) {
  1261. if (isset($_SESSION['return'])) {
  1262. echo json_encode($_SESSION['return']);
  1263. }
  1264. else {
  1265. echo json_encode(array(
  1266. 'type' => 'error',
  1267. 'msg' => 'Edit failed'
  1268. ));
  1269. }
  1270. exit();
  1271. }
  1272. else {
  1273. if (isset($_SESSION['return'])) {
  1274. echo json_encode($_SESSION['return']);
  1275. }
  1276. else {
  1277. echo json_encode(array(
  1278. 'type' => 'success',
  1279. 'msg' => 'Task completed'
  1280. ));
  1281. }
  1282. }
  1283. }
  1284. else {
  1285. echo json_encode(array(
  1286. 'type' => 'error',
  1287. 'msg' => 'Incomplete post data'
  1288. ));
  1289. }
  1290. }
  1291. else {
  1292. echo json_encode(array(
  1293. 'type' => 'error',
  1294. 'msg' => 'Incomplete post data'
  1295. ));
  1296. }
  1297. break;
  1298. case "alias-domain":
  1299. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1300. $items = (array)json_decode($_POST['items'], true);
  1301. $attr = (array)json_decode($_POST['attr'], true);
  1302. $postarray = array_merge(array('alias_domain' => $items), $attr);
  1303. if (is_array($postarray['alias_domain'])) {
  1304. if (mailbox('edit', 'alias_domain', $postarray) === false) {
  1305. if (isset($_SESSION['return'])) {
  1306. echo json_encode($_SESSION['return']);
  1307. }
  1308. else {
  1309. echo json_encode(array(
  1310. 'type' => 'error',
  1311. 'msg' => 'Edit failed'
  1312. ));
  1313. }
  1314. exit();
  1315. }
  1316. else {
  1317. if (isset($_SESSION['return'])) {
  1318. echo json_encode($_SESSION['return']);
  1319. }
  1320. else {
  1321. echo json_encode(array(
  1322. 'type' => 'success',
  1323. 'msg' => 'Task completed'
  1324. ));
  1325. }
  1326. }
  1327. }
  1328. else {
  1329. echo json_encode(array(
  1330. 'type' => 'error',
  1331. 'msg' => 'Incomplete post data'
  1332. ));
  1333. }
  1334. }
  1335. else {
  1336. echo json_encode(array(
  1337. 'type' => 'error',
  1338. 'msg' => 'Incomplete post data'
  1339. ));
  1340. }
  1341. break;
  1342. case "spam_score":
  1343. if (isset($_POST['items']) && isset($_POST['attr'])) {
  1344. $items = (array)json_decode($_POST['items'], true);
  1345. $attr = (array)json_decode($_POST['attr'], true);
  1346. $postarray = array_merge(array('username' => $items), $attr);
  1347. if (is_array($postarray['username'])) {
  1348. if (mailbox('edit', 'spam_score', $postarray) === false) {
  1349. if (isset($_SESSION['return'])) {
  1350. echo json_encode($_SESSION['return']);
  1351. }
  1352. else {
  1353. echo json_encode(array(
  1354. 'type' => 'error',
  1355. 'msg' => 'Edit failed'
  1356. ));
  1357. }
  1358. exit();
  1359. }
  1360. else {
  1361. if (isset($_SESSION['return'])) {
  1362. echo json_encode($_SESSION['return']);
  1363. }
  1364. else {
  1365. echo json_encode(array(
  1366. 'type' => 'success',
  1367. 'msg' => 'Task completed'
  1368. ));
  1369. }
  1370. }
  1371. }
  1372. else {
  1373. echo json_encode(array(
  1374. 'type' => 'error',
  1375. 'msg' => 'Incomplete post data'
  1376. ));
  1377. }
  1378. }
  1379. else {
  1380. echo json_encode(array(
  1381. 'type' => 'error',
  1382. 'msg' => 'Incomplete post data'
  1383. ));
  1384. }
  1385. break;
  1386. }
  1387. break;
  1388. }
  1389. }
  1390. }