QuotaController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 /usr/bin/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);
  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. error_log("DEBUG: Getting Quota for $username => " . implode("", $userOutput));
  21. // echo json_decode(print_r($userOutput));
  22. }
  23. public static function getStats(): void {
  24. // get all quotas
  25. if ($GLOBALS['debug'] == true) {error_log("Getting Quota Stats"); }
  26. exec("sudo /usr/sbin/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);
  27. if ($userReturnCode !== 0) {
  28. error_log("getstats: ERROR: Getting Quota Stats failed, details => " . implode("\n", $userOutput));
  29. http_response_code(500);
  30. echo json_encode(['error' => 'Failed to get quota stats', 'details' => implode("\n", $userOutput)]);
  31. return;
  32. }
  33. echo $userOutput;
  34. }
  35. public static function setQuota($data): void {
  36. $username = $data['username'];
  37. $quota = $data['quota'];
  38. if (empty($username)) {
  39. http_response_code(400);
  40. echo json_encode(['error' => 'Missing required parameter: username']);
  41. return;
  42. }
  43. if (empty($quota)) {
  44. $quota = 0;
  45. }
  46. // set quota
  47. if ($GLOBALS['debug'] == true) {error_log("Set Limit of $quota for $username"); }
  48. exec("sudo xfs_quota -x -c 'limit -u bsoft=$quota bhard=$quota $username' /home 2>&1", $userOutput, $userReturnCode);
  49. if ($userReturnCode !== 0) {
  50. error_log("getstats: ERROR: Set Quota of $quota for $username failed, details => " . implode("\n", $userOutput));
  51. http_response_code(500);
  52. echo json_encode(['error' => 'Failed to set quota', 'details' => implode("\n", $userOutput)]);
  53. return;
  54. }
  55. echo json_encode(['success' => 'Set Quota successfully']);
  56. }
  57. }