andre 8 месяцев назад
Родитель
Сommit
17be78fd4c
1 измененных файлов с 60 добавлено и 0 удалено
  1. 60 0
      controllers/QuotaController.php

+ 60 - 0
controllers/QuotaController.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace application\controllers;
+
+class QuotaController {
+    public static function getQuota($data): void {
+        $username =  $data['username'];
+
+        if (empty($username)) {
+            http_response_code(400);
+            echo json_encode(['error' => 'Missing required parameter: username']);
+            return;
+        }
+
+        // get quota
+        if ($GLOBALS['debug'] == true) {error_log("Get Quota for User: " . $username); }
+        exec("sudo quota -p --hide-device -w $username | tail -n +3 | column -t -n quota -N blocks,soft,hard,grace,inodes,isoft,ihard,igrace -H 5 -J 2>&1", $userOutput, $userReturnCode);
+        if ($userReturnCode !== 0) {
+            error_log("getquota: ERROR: Getting Quota for $username failed, details => " . implode("\n", $userOutput));
+            http_response_code(500);
+            echo json_encode(['error' => 'Failed to get user quota', 'details' => implode("\n", $userOutput)]);
+            return;
+        }
+        echo $userOutput;
+    }
+    public static function getStats($data): void {
+        // get all quotas
+        if ($GLOBALS['debug'] == true) {error_log("Getting Quota Stats"); }
+        exec("sudo xfs_quota -x -c 'report -u' /home | tail -n +7 | column -t -n quota -N user,used,soft,hard,limit,grace -H 5 -J 2>&1", $userOutput, $userReturnCode);
+        if ($userReturnCode !== 0) {
+            error_log("getstats: ERROR: Getting Quota Stats failed, details => " . implode("\n", $userOutput));
+            http_response_code(500);
+            echo json_encode(['error' => 'Failed to get quota stats', 'details' => implode("\n", $userOutput)]);
+            return;
+        }
+        echo $userOutput;
+    }
+    public static function setQuota($data): void {
+        $username = $data['username'];
+        $quota = $data['quota'];
+        if (empty($username)) {
+            http_response_code(400);
+            echo json_encode(['error' => 'Missing required parameter: username']);
+            return;
+        }
+        if (empty($quota)) {
+            $quota = 0;
+        }
+        // set quota
+        if ($GLOBALS['debug'] == true) {error_log("Set Limit of $quota for $username"); }
+        exec("sudo xfs_quota -x -c 'limit -u bsoft=$quota bhard=$quota $username' /home 2>&1", $userOutput, $userReturnCode);
+        if ($userReturnCode !== 0) {
+            error_log("getstats: ERROR: Set Quota of $quota for $username failed, details => " . implode("\n", $userOutput));
+            http_response_code(500);
+            echo json_encode(['error' => 'Failed to set quota', 'details' => implode("\n", $userOutput)]);
+            return;
+        }
+        echo json_encode(['success' => 'Set Quota successfully']);
+    }
+}