QuotaController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace application\controllers;
  3. class QuotaController {
  4. public static function getQuota($data): void {
  5. $username = $data['username'];
  6. if (empty($username)) {
  7. http_response_code(400);
  8. echo json_encode(['error' => 'Missing required parameter: username']);
  9. return;
  10. }
  11. // get quota
  12. if ($GLOBALS['debug'] == true) {error_log("Get Quota for User: " . $username); }
  13. 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 2>&1", $userOutput, $userReturnCode);
  14. if ($userReturnCode !== 0) {
  15. error_log("getquota: ERROR: Getting Quota for $username failed, details => " . implode("\n", $userOutput));
  16. http_response_code(500);
  17. echo json_encode(['error' => 'Failed to get user quota', 'details' => implode("\n", $userOutput)]);
  18. return;
  19. }
  20. echo json_decode(print_r($userOutput));
  21. }
  22. public static function getStats(): void {
  23. // get all quotas
  24. if ($GLOBALS['debug'] == true) {error_log("Getting Quota Stats"); }
  25. 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);
  26. if ($userReturnCode !== 0) {
  27. error_log("getstats: ERROR: Getting Quota Stats failed, details => " . implode("\n", $userOutput));
  28. http_response_code(500);
  29. echo json_encode(['error' => 'Failed to get quota stats', 'details' => implode("\n", $userOutput)]);
  30. return;
  31. }
  32. echo $userOutput;
  33. }
  34. public static function setQuota($data): void {
  35. $username = $data['username'];
  36. $quota = $data['quota'];
  37. if (empty($username)) {
  38. http_response_code(400);
  39. echo json_encode(['error' => 'Missing required parameter: username']);
  40. return;
  41. }
  42. if (empty($quota)) {
  43. $quota = 0;
  44. }
  45. // set quota
  46. if ($GLOBALS['debug'] == true) {error_log("Set Limit of $quota for $username"); }
  47. exec("sudo xfs_quota -x -c 'limit -u bsoft=$quota bhard=$quota $username' /home 2>&1", $userOutput, $userReturnCode);
  48. if ($userReturnCode !== 0) {
  49. error_log("getstats: ERROR: Set Quota of $quota for $username failed, details => " . implode("\n", $userOutput));
  50. http_response_code(500);
  51. echo json_encode(['error' => 'Failed to set quota', 'details' => implode("\n", $userOutput)]);
  52. return;
  53. }
  54. echo json_encode(['success' => 'Set Quota successfully']);
  55. }
  56. }