NobGit
public nobgit read

NobMail

Based on mailcow: dockerized

Languages

Repository composition by tracked source files.

PHP
PHP 49% JavaScript 35% HTML 9% CSS 4% Shell 2% Python 1% Lua 0% Perl 0% Ruby 0% SCSS 0%
Create file Wiki Documentation
Clone
https://nobgit.com/orgs/nobgit/nobmail.git
ssh://[email protected]:2222/orgs/nobgit/nobmail.git

Commit

[Dovecot] Lz4 compression sometimes leads to strange EOF errors when replicating [Web] Cleanup JSON API

0a44ea1a
AndrĂ© <[email protected]> 8 years ago
data/conf/dovecot/dovecot.conf |    4 +-
 data/web/json_api.php          | 2706 ++++------------------------------------
 2 files changed, 215 insertions(+), 2495 deletions(-)

Diff

diff --git a/data/conf/dovecot/dovecot.conf b/data/conf/dovecot/dovecot.conf
index 6611e836..fc29c63f 100644
--- a/data/conf/dovecot/dovecot.conf
+++ b/data/conf/dovecot/dovecot.conf
@@ -285,8 +285,8 @@ plugin {
   #mail_crypt_global_private_key = </mail_crypt/ecprivkey.pem
   #mail_crypt_global_public_key = </mail_crypt/ecpubkey.pem
   #mail_crypt_save_version = 2
-  # Enable compression while saving, lz4 Dovecot v2.2.11+
-  zlib_save = lz4
+  # Enable compression while saving
+  zlib_save = gz
 }
 dict {
   sqlquota = mysql:/usr/local/etc/dovecot/sql/dovecot-dict-sql-quota.conf
diff --git a/data/web/json_api.php b/data/web/json_api.php
index 8fe0c22e..a19b6a2c 100644
--- a/data/web/json_api.php
+++ b/data/web/json_api.php
@@ -64,595 +64,100 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
     $object =     (isset($query[2])) ? $query[2] : null;
     $extra =      (isset($query[3])) ? $query[3] : null;
 
+    $request_incomplete = json_encode(array(
+    'type' => 'error',
+    'msg' => 'Cannot find attributes in post data'
+    ));
+
     switch ($action) {
       case "add":
+        function process_add_return($return) {
+          $generic_failure = json_encode(array(
+            'type' => 'error',
+            'msg' => 'Cannot add item'
+          ));
+          $generic_success = json_encode(array(
+            'type' => 'success',
+            'msg' => 'Task completed'
+          ));
+          if ($return === false) {
+            echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_failure;
+          }
+          else {
+            echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_success;
+          }
+        }
+        if (!isset($_POST['attr'])) {
+          echo $request_incomplete;
+          exit;
+        }
+        else {
+          $attr = (array)json_decode($_POST['attr'], true);
+        }
         switch ($category) {
           case "time_limited_alias":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (mailbox('add', 'time_limited_alias', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(mailbox('add', 'time_limited_alias', $attr));
           break;
           case "relayhost":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (relayhost('add', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(relayhost('add', $attr));
           break;
           case "mailbox":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (mailbox('add', 'mailbox', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(mailbox('add', 'mailbox', $attr));
           break;
           case "domain":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (mailbox('add', 'domain', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(mailbox('add', 'domain', $attr));
           break;
-          case "resource":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (mailbox('add', 'resource', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+          case "domain":
+            process_add_return(mailbox('add', 'resource', $attr));
           break;
           case "alias":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (mailbox('add', 'alias', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(mailbox('add', 'alias', $attr));
           break;
           case "filter":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (mailbox('add', 'filter', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(mailbox('add', 'filter', $attr));
           break;
           case "domain-policy":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (policy('add', 'domain', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(policy('add', 'domain', $attr));
           break;
           case "mailbox-policy":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (policy('add', 'mailbox', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(policy('add', 'mailbox', $attr));
           break;
           case "alias-domain":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (mailbox('add', 'alias_domain', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(mailbox('add', 'alias_domain', $attr));
           break;
           case "fwdhost":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (fwdhost('add', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(fwdhost('add', $attr));
           break;
           case "dkim":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (dkim('add', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(dkim('add', $attr));
           break;
           case "dkim_import":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (dkim('import', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(dkim('import', $attr));
           break;
           case "domain-admin":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (domain_admin('add', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(domain_admin('add', $attr));
           break;
           case "syncjob":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (mailbox('add', 'syncjob', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(mailbox('add', 'syncjob', $attr));
           break;
           case "bcc":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (bcc('add', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(bcc('add', $attr));
           break;
           case "recipient_map":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (recipient_map('add', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Cannot add item'
-                  ));
-                }
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find attributes in post data'
-              ));
-            }
+            process_add_return(recipient_map('add', $attr));
           break;
         }
       break;
       case "get":
+        function process_get_return($data) {
+          echo (!isset($data) || empty($data)) ? '{}' : json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
+        }
         switch ($category) {
           case "rspamd":
             switch ($object) {
-              case "actions":
-                $curl = curl_init();
-                curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/rspamd-sock/rspamd.sock');
-                curl_setopt($curl, CURLOPT_URL,"http://rspamd/stat");
-                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
-                $data = curl_exec($curl);
-                if ($data) {
-                  $return = array();
-                  $stats_array = json_decode($data, true)['actions'];
-                  $stats_array['soft reject'] = $stats_array['soft reject'] + $stats_array['greylist'];
-                  unset($stats_array['greylist']);
-                  foreach ($stats_array as $action => $count) {
-                    $return[] = array($action, $count);
-                  }
-                  echo json_encode($return, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
-                elseif (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
+              case "stat":
+                $data = file_get_contents('http://rspamd-mailcow:11334/stat');
+                process_get_return($data);
               break;
               case "graph":
                 switch ($extra) {
@@ -813,12 +318,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                       continue;
                     }
                   }
-                  if (!isset($data) || empty($data)) {
-                    echo '{}';
-                  }
-                  else {
-                    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                  }
+                  process_get_return($data);
                 }
                 else {
                   echo '{}';
@@ -827,12 +327,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
 
               default:
                 $data = mailbox('get', 'domain_details', $object);
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -850,12 +345,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                       continue;
                     }
                   }
-                  if (!isset($data) || empty($data)) {
-                    echo '{}';
-                  }
-                  else {
-                    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                  }
+                  process_get_return($data);
                 }
                 else {
                   echo '{}';
@@ -864,15 +354,38 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
 
               default:
                 $data = relayhost('details', $object);
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
+                process_get_return($data);
+              break;
+            }
+          break;
+
+          case "oauth2-client":
+            switch ($object) {
+              case "all":
+                $clients = oauth2('get', 'clients');
+                if (!empty($clients)) {
+                  foreach ($clients as $client) {
+                    if ($details = oauth2('details', 'client', $client)) {
+                      $data[] = $details;
+                    }
+                    else {
+                      continue;
+                    }
+                  }
+                  process_get_return($data);
                 }
                 else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
+                  echo '{}';
                 }
               break;
+
+              default:
+                $data = oauth2('details', 'client', $object);
+                process_get_return($data);
+              break;
             }
           break;
+
           case "logs":
             switch ($object) {
               case "dovecot":
@@ -884,12 +397,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                 else {
                   $logs = get_logs('dovecot-mailcow');
                 }
-                if (isset($logs) && !empty($logs)) {
-                  echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
-                else {
-                  echo '{}';
-                }
+                echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
               break;
               case "netfilter":
                 // 0 is first record, so empty is fine
@@ -900,12 +408,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                 else {
                   $logs = get_logs('netfilter-mailcow');
                 }
-                if (isset($logs) && !empty($logs)) {
-                  echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
-                else {
-                  echo '{}';
-                }
+                echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
               break;
               case "postfix":
                 // 0 is first record, so empty is fine
@@ -916,12 +419,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                 else {
                   $logs = get_logs('postfix-mailcow');
                 }
-                if (isset($logs) && !empty($logs)) {
-                  echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
-                else {
-                  echo '{}';
-                }
+                echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
               break;
               case "autodiscover":
                 // 0 is first record, so empty is fine
@@ -932,12 +430,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                 else {
                   $logs = get_logs('autodiscover-mailcow');
                 }
-                if (isset($logs) && !empty($logs)) {
-                  echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
-                else {
-                  echo '{}';
-                }
+                echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
               break;
               case "sogo":
                 // 0 is first record, so empty is fine
@@ -948,12 +441,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                 else {
                   $logs = get_logs('sogo-mailcow');
                 }
-                if (isset($logs) && !empty($logs)) {
-                  echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
-                else {
-                  echo '{}';
-                }
+                echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
               break;
               case "watchdog":
                 // 0 is first record, so empty is fine
@@ -964,12 +452,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                 else {
                   $logs = get_logs('watchdog-mailcow');
                 }
-                if (isset($logs) && !empty($logs)) {
-                  echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
-                else {
-                  echo '{}';
-                }
+                echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
               break;
               case "acme":
                 // 0 is first record, so empty is fine
@@ -980,12 +463,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                 else {
                   $logs = get_logs('acme-mailcow');
                 }
-                if (isset($logs) && !empty($logs)) {
-                  echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
-                else {
-                  echo '{}';
-                }
+                echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
               break;
               case "api":
                 // 0 is first record, so empty is fine
@@ -996,12 +474,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                 else {
                   $logs = get_logs('api-mailcow');
                 }
-                if (isset($logs) && !empty($logs)) {
-                  echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
-                else {
-                  echo '{}';
-                }
+                echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
               break;
               case "rspamd-history":
                 // 0 is first record, so empty is fine
@@ -1012,12 +485,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                 else {
                   $logs = get_logs('rspamd-history');
                 }
-                if (isset($logs) && !empty($logs)) {
-                  echo json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
-                else {
-                  echo '{}';
-                }
+                echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
               break;
             }
           break;
@@ -1039,12 +507,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                       }
                     }
                   }
-                  if (!isset($data) || empty($data)) {
-                    echo '{}';
-                  }
-                  else {
-                    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                  }
+                  process_get_return($data);
                 }
                 else {
                   echo '{}';
@@ -1053,12 +516,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
 
               default:
                 $data = mailbox('get', 'mailbox_details', $object);
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -1091,12 +549,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                       }
                     }
                   }
-                  if (!isset($data) || empty($data)) {
-                    echo '{}';
-                  }
-                  else {
-                    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                  }
+                  process_get_return($data);
                 }
                 else {
                   echo '{}';
@@ -1121,12 +574,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                     }
                   }
                 }
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -1137,12 +585,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                 $data[] = $sieve_filter;
               }
             }
-            if (!isset($data) || empty($data)) {
-              echo '{}';
-            }
-            else {
-              echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-            }
+            process_get_return($data);
           break;
           case "filters":
             switch ($object) {
@@ -1167,12 +610,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                       }
                     }
                   }
-                  if (!isset($data) || empty($data)) {
-                    echo '{}';
-                  }
-                  else {
-                    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                  }
+                  process_get_return($data);
                 }
                 else {
                   echo '{}';
@@ -1191,12 +629,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                     }
                   }
                 }
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -1214,24 +647,14 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                     }
                   }
                 }
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
               default:
                 $data = bcc('details', $object);
                 if (!empty($data)) {
                   $data[] = $details;
                 }
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -1249,24 +672,14 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                     }
                   }
                 }
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
               default:
                 $data = recipient_map('details', $object);
                 if (!empty($data)) {
                   $data[] = $details;
                 }
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -1274,12 +687,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
             switch ($object) {
               default:
                 $data = policy('get', 'mailbox', $object)['whitelist'];
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -1287,12 +695,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
             switch ($object) {
               default:
                 $data = policy('get', 'mailbox', $object)['blacklist'];
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -1300,12 +703,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
             switch ($object) {
               default:
                 $data = policy('get', 'domain', $object)['whitelist'];
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -1313,12 +711,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
             switch ($object) {
               default:
                 $data = policy('get', 'domain', $object)['blacklist'];
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -1326,12 +719,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
             switch ($object) {
               default:
                 $data = mailbox('get', 'time_limited_aliases', $object);
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
@@ -1353,12 +741,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                       }
                     }
                   }
-                  if (!isset($data) || empty($data)) {
-                    echo '{}';
-                  }
-                  else {
-                    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                  }
+                  process_get_return($data);
                 }
                 else {
                   echo '{}';
@@ -1366,34 +749,17 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
               break;
               default:
                 $data = mailbox('get', 'resource_details', $object);
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
             }
           break;
           case "fwdhost":
             switch ($object) {
               case "all":
-                $data = fwdhost('get');
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return(fwdhost('get'));
               break;
               default:
-                $data = fwdhost('details', $object);
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return(fwdhost('details', $object));
               break;
             }
           break;
@@ -1401,22 +767,10 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
             // "all" will not print details
             switch ($object) {
               case "all":
-                $data = quarantine('get');
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return(quarantine('get'));
               break;
               default:
-                $data = quarantine('details', $object);
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return(quarantine('details', $object));
               break;
             }
           break;
@@ -1434,28 +788,17 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                     }
                   }
                 }
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return($data);
               break;
               default:
-                $data = mailbox('get', 'alias_domain_details', $object);
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return(mailbox('get', 'alias_domain_details', $object));
               break;
             }
           break;
           case "alias":
             switch ($object) {
               case "all":
-                $domains = array_merge(mailbox('get', 'domains'),mailbox('get', 'alias_domains'));
+                $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
                 if (!empty($domains)) {
                   foreach ($domains as $domain) {
                     $aliases = mailbox('get', 'aliases', $domain);
@@ -1470,12 +813,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                       }
                     }
                   }
-                  if (!isset($data) || empty($data)) {
-                    echo '{}';
-                  }
-                  else {
-                    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                  }
+                  process_get_return($data);
                 }
                 else {
                   echo '{}';
@@ -1483,13 +821,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
               break;
 
               default:
-                $data = mailbox('get', 'alias_details', $object);
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return(mailbox('get', 'alias_details', $object));
               break;
             }
           break;
@@ -1506,12 +838,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
                       continue;
                     }
                   }
-                  if (!isset($data) || empty($data)) {
-                    echo '{}';
-                  }
-                  else {
-                    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                  }
+                  process_get_return($data);
                 }
                 else {
                   echo '{}';
@@ -1519,13 +846,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
               break;
 
               default:
-                $data = domain_admin('details', $object);
-                if (!isset($data) || empty($data)) {
-                  echo '{}';
-                }
-                else {
-                  echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-                }
+                process_get_return(domain_admin('details', $object));
               break;
             }
           break;
@@ -1571,1792 +892,191 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
         }
       break;
       case "delete":
+        function process_delete_return($return) {
+          $generic_failure = json_encode(array(
+            'type' => 'error',
+            'msg' => 'Cannot delete item'
+          ));
+          $generic_success = json_encode(array(
+            'type' => 'success',
+            'msg' => 'Task completed'
+          ));
+          if ($return === false) {
+            echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_failure;
+          }
+          else {
+            echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_success;
+          }
+        }
+        if (!isset($_POST['items'])) {
+          echo $request_incomplete;
+          exit;
+        }
+        else {
+          $items = (array)json_decode($_POST['items'], true);
+        }
         switch ($category) {
           case "alias":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (mailbox('delete', 'alias', array('address' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Deletion of items/s failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find address array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(mailbox('delete', 'alias', array('address' => $items)));
           break;
           case "relayhost":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (relayhost('delete', array('id' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Deletion of items/s failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find id array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(relayhost('delete', array('id' => $items)));
           break;
           case "syncjob":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (mailbox('delete', 'syncjob', array('id' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Deletion of items/s failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find id array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(mailbox('delete', 'syncjob', array('id' => $items)));
           break;
           case "filter":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (mailbox('delete', 'filter', array('id' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Deletion of items/s failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find id array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(mailbox('delete', 'filter', array('id' => $items)));
           break;
           case "qitem":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (quarantine('delete', array('id' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Deletion of items/s failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find id array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(quarantine('delete', array('id' => $items)));
           break;
           case "bcc":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (bcc('delete', array('id' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Deletion of items/s failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find id array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(bcc('delete', array('id' => $items)));
           break;
           case "recipient_map":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (recipient_map('delete', array('id' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Deletion of items/s failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find id array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(recipient_map('delete', array('id' => $items)));
           break;
           case "fwdhost":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (fwdhost('delete', array('forwardinghost' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Deletion of items/s failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find forwardinghost array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(fwdhost('delete', array('forwardinghost' => $items)));
           break;
           case "dkim":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (dkim('delete', array('domains' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Deletion of items/s failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find domains array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(dkim('delete', array('domains' => $items)));
           break;
           case "domain":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (mailbox('delete', 'domain', array('domain' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Task failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find domain array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            file_put_contents('/tmp/dssaa', $items);
+            process_delete_return(mailbox('delete', 'domain', array('domain' => $items)));
           break;
           case "alias-domain":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (mailbox('delete', 'alias_domain', array('alias_domain' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Task failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find alias_domain array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(mailbox('delete', 'alias_domain', array('alias_domain' => $items)));
           break;
           case "mailbox":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (mailbox('delete', 'mailbox', array('username' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Task failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find username array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(mailbox('delete', 'mailbox', array('username' => $items)));
           break;
           case "resource":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (mailbox('delete', 'resource', array('name' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Task failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find name array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(mailbox('delete', 'resource', array('name' => $items)));
           break;
           case "mailbox-policy":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (policy('delete', 'mailbox', array('prefid' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Task failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find name array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(policy('delete', 'mailbox', array('prefid' => $items)));
           break;
           case "domain-policy":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (policy('delete', 'domain', array('prefid' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Task failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find name array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(policy('delete', 'domain', array('prefid' => $items)));
           break;
           case "time_limited_alias":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (mailbox('delete', 'time_limited_alias', array('address' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Task failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find name array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(mailbox('delete', 'time_limited_alias', array('address' => $items)));
           break;
           case "eas_cache":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (mailbox('delete', 'eas_cache', array('username' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Task failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find name array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(mailbox('delete', 'eas_cache', array('username' => $items)));
           break;
           case "domain-admin":
-            if (isset($_POST['items'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              if (is_array($items)) {
-                if (domain_admin('delete', array('username' => $items)) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Task failed'
-                    ));
-                  }
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Cannot find name array in post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Cannot find items in post data'
-              ));
-            }
+            process_delete_return(domain_admin('delete', array('username' => $items)));
           break;
         }
       break;
       case "edit":
+        function process_edit_return($return) {
+          $generic_failure = json_encode(array(
+            'type' => 'error',
+            'msg' => 'Cannot edit item'
+          ));
+          $generic_success = json_encode(array(
+            'type' => 'success',
+            'msg' => 'Task completed'
+          ));
+          if ($return === false) {
+            echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_failure;
+          }
+          else {
+            echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_success;
+          }
+        }
+        if (!isset($_POST['attr'])) {
+          echo $request_incomplete;
+          exit;
+        }
+        else {
+          $attr = (array)json_decode($_POST['attr'], true);
+          $items = isset($_POST['items']) ? (array)json_decode($_POST['items'], true) : null;
+        }
         switch ($category) {
           case "bcc":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('id' => $items), $attr);
-              if (is_array($postarray['id'])) {
-                if (bcc('edit', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(bcc('edit', array_merge(array('id' => $items), $attr)));
+          break;
+          case "oauth2-client":
+            process_edit_return(oauth2('edit', 'client', array_merge(array('id' => $items), $attr)));
           break;
           case "recipient_map":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('id' => $items), $attr);
-              if (is_array($postarray['id'])) {
-                if (recipient_map('edit', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(recipient_map('edit', array_merge(array('id' => $items), $attr)));
           break;
           case "alias":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('address' => $items), $attr);
-              if (is_array($postarray['address'])) {
-                if (mailbox('edit', 'alias', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'alias', array_merge(array('address' => $items), $attr)));
           break;
           case "app_links":
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (is_array($attr)) {
-                if (customize('edit', 'app_links', $attr) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(customize('edit', 'app_links', $attr));
           break;
           case "relayhost":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('id' => $items), $attr);
-              if (is_array($postarray['id'])) {
-                if (relayhost('edit', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(relayhost('edit', array_merge(array('id' => $items), $attr)));
           break;
           case "delimiter_action":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('username' => $items), $attr);
-              if (is_array($postarray['username'])) {
-                if (mailbox('edit', 'delimiter_action', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'delimiter_action', array_merge(array('username' => $items), $attr)));
           break;
           case "tls_policy":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('username' => $items), $attr);
-              if (is_array($postarray['username'])) {
-                if (mailbox('edit', 'tls_policy', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'tls_policy', array_merge(array('username' => $items), $attr)));
           break;
           case "qitem":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('id' => $items), $attr);
-              if (is_array($postarray['id'])) {
-                if (quarantine('edit', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(quarantine('edit', array_merge(array('id' => $items), $attr)));
           break;
           case "quarantine":
-            // Edit settings, does not need IDs
-            if (isset($_POST['attr'])) {
-              $postarray = json_decode($_POST['attr'], true);
-              if (quarantine('edit', $postarray) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Edit failed'
-                  ));
-                }
-                exit();
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(quarantine('edit', $attr));
           break;
           case "time_limited_alias":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('address' => $items), $attr);
-              if (is_array($postarray['address'])) {
-                if (mailbox('edit', 'time_limited_alias', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'time_limited_alias', array_merge(array('address' => $items), $attr)));
           break;
           case "mailbox":
-            // sender_acl:0 removes all entries
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('username' => $items), $attr);
-              if (is_array($postarray['username'])) {
-                if (mailbox('edit', 'mailbox', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'mailbox', array_merge(array('username' => $items), $attr)));
           break;
           case "syncjob":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('id' => $items), $attr);
-              if (is_array($postarray['id'])) {
-                if (mailbox('edit', 'syncjob', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'syncjob', array_merge(array('id' => $items), $attr)));
           break;
           case "filter":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('id' => $items), $attr);
-              if (is_array($postarray['id'])) {
-                if (mailbox('edit', 'filter', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'filter', array_merge(array('id' => $items), $attr)));
           break;          
           case "resource":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('name' => $items), $attr);
-              if (is_array($postarray['name'])) {
-                if (mailbox('edit', 'resource', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'resource', array_merge(array('name' => $items), $attr)));
           break;
           case "domain":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('domain' => $items), $attr);
-              if (is_array($postarray['domain'])) {
-                if (mailbox('edit', 'domain', $postarray)) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'domain', array_merge(array('domain' => $items), $attr)));
           break;
           case "ratelimit":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('object' => $items), $attr);
-              if (is_array($postarray['object'])) {
-                if (mailbox('edit', 'ratelimit', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'ratelimit', array_merge(array('object' => $items), $attr)));
           break;
           case "alias-domain":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('alias_domain' => $items), $attr);
-              if (is_array($postarray['alias_domain'])) {
-                if (mailbox('edit', 'alias_domain', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'alias_domain', array_merge(array('alias_domain' => $items), $attr)));
           break;
           case "spam-score":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('username' => $items), $attr);
-              if (is_array($postarray['username'])) {
-                if (mailbox('edit', 'spam_score', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(mailbox('edit', 'spam_score', array_merge(array('username' => $items), $attr)));
           break;
           case "domain-admin":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('username' => $items), $attr);
-              if (is_array($postarray['username'])) {
-                if (domain_admin('edit', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(domain_admin('edit', array_merge(array('username' => $items), $attr)));
           break;
           case "fwdhost":
-            if (isset($_POST['items']) && isset($_POST['attr'])) {
-              $items = (array)json_decode($_POST['items'], true);
-              $attr = (array)json_decode($_POST['attr'], true);
-              $postarray = array_merge(array('fwdhost' => $items), $attr);
-              if (is_array($postarray['fwdhost'])) {
-                if (fwdhost('edit', $postarray) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(fwdhost('edit', array_merge(array('fwdhost' => $items), $attr)));
           break;
           case "fail2ban":
-            // No items
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (fail2ban('edit', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Edit failed'
-                  ));
-                }
-                exit();
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(fail2ban('edit', $attr));
           break;
           case "ui_texts":
-            // No items
-            if (isset($_POST['attr'])) {
-              $attr = (array)json_decode($_POST['attr'], true);
-              if (customize('edit', 'ui_texts', $attr) === false) {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'error',
-                    'msg' => 'Edit failed'
-                  ));
-                }
-                exit();
-              }
-              else {
-                if (isset($_SESSION['return'])) {
-                  echo json_encode($_SESSION['return']);
-                }
-                else {
-                  echo json_encode(array(
-                    'type' => 'success',
-                    'msg' => 'Task completed'
-                  ));
-                }
-              }
-            }
-            else {
-              echo json_encode(array(
-                'type' => 'error',
-                'msg' => 'Incomplete post data'
-              ));
-            }
+            process_edit_return(customize('edit', 'ui_texts', $attr));
           break;
           case "self":
-            // No items, logged-in user, users and domain admins
             if ($_SESSION['mailcow_cc_role'] == "domainadmin") {
-              if (isset($_POST['attr'])) {
-                $attr = (array)json_decode($_POST['attr'], true);
-                if (domain_admin('edit', $attr) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
+              process_edit_return(domain_admin('edit', $attr));
             }
             elseif ($_SESSION['mailcow_cc_role'] == "user") {
-              if (isset($_POST['attr'])) {
-                $attr = (array)json_decode($_POST['attr'], true);
-                if (edit_user_account($attr) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
+              process_edit_return(edit_user_account($attr));
             }
             elseif ($_SESSION['mailcow_cc_role'] == "admin") {
-              if (isset($_POST['attr'])) {
-                $attr = (array)json_decode($_POST['attr'], true);
-                if (edit_admin_account($attr) === false) {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'error',
-                      'msg' => 'Edit failed'
-                    ));
-                  }
-                  exit();
-                }
-                else {
-                  if (isset($_SESSION['return'])) {
-                    echo json_encode($_SESSION['return']);
-                  }
-                  else {
-                    echo json_encode(array(
-                      'type' => 'success',
-                      'msg' => 'Task completed'
-                    ));
-                  }
-                }
-              }
-              else {
-                echo json_encode(array(
-                  'type' => 'error',
-                  'msg' => 'Incomplete post data'
-                ));
-              }
+              process_edit_return(edit_admin_account($attr));
             }
           break;
         }