AccountController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace application\controllers;
  3. class AccountController {
  4. public static function create($data): void {
  5. $username = $data['username'];
  6. $domain = $data['domain'];
  7. $adminName = $data['admin_name'] ?? '';
  8. $adminPassword = $data['admin_password'];
  9. $webDir = "/home/$username/$domain";
  10. $placeholderdir = "/var/www/catchall";
  11. $configTemplate = '/etc/apache2/site-config.in';
  12. $configFile = "/etc/apache2/sites-enabled/$domain.conf";
  13. if (empty($username) || empty($domain) || empty($adminName) || empty($adminPassword)) {
  14. error_log("deploy: ERROR: No username, domain, admin_name or admin_password provided");
  15. http_response_code(400);
  16. error_log("deploy: ERROR: UserName 1 " . $username);
  17. error_log("deploy: ERROR: Domain 1 " . $domain);
  18. error_log("deploy: ERROR: UserName 2 " . $data['username'] );
  19. error_log("deploy: ERROR: Domain 2 " . $data['domain'] );
  20. error_log("deploy: ERROR: AdminName " . $adminName);
  21. error_log("deploy: ERROR: AdminPasswd" . $adminPassword);
  22. error_log(print_r($data,true));
  23. echo json_encode(['error' => 'Missing required parameters']);
  24. return;
  25. }
  26. error_log(" Creating user " . $username . " DebugMode: " . $GLOBALS['debug']);
  27. $userExisted = false;
  28. if( strpos(file_get_contents("/etc/passwd"),$username) !== false) {
  29. $userExisted = true;
  30. }
  31. if ($userExisted != true) {
  32. // Create user without login access
  33. if ($GLOBALS['debug'] == true) {error_log("Adding User: " . $username); }
  34. exec("sudo /usr/sbin/useradd -m -k -M -s /usr/sbin/nologin $username 2>&1", $userOutput, $userReturnCode);
  35. if ($userReturnCode !== 0) {
  36. error_log("deploy: ERROR: Useradd for $username failed, details => " . implode("\n", $userOutput));
  37. http_response_code(500);
  38. echo json_encode(['error' => 'Failed to create user', 'details' => implode("\n", $userOutput)]);
  39. return;
  40. }
  41. }
  42. if ($GLOBALS['debug'] == true) { error_log("Creating logdir for : " . $username); }
  43. if (is_dir("/home/$username/logs") != true) {
  44. exec("sudo /usr/bin/mkdir -p /home/$username/logs 2>&1", $mkdirOutput, $mkdirReturnCode);
  45. if ($mkdirReturnCode !== 0) {
  46. error_log("deploy: ERROR: Failed to create log directory for $username failed, details => " . implode("\n", $mkdirOutput));
  47. http_response_code(500);
  48. echo json_encode(['error' => 'Failed to create logs dir', 'details' => implode("\n", $mkdirOutput)]);
  49. return;
  50. }
  51. }
  52. if ($GLOBALS['debug'] == true) { error_log("Creating backup dir for : " . $username); }
  53. if (is_dir("/home/$username/backups") != true) {
  54. exec("sudo /usr/bin/mkdir -p /home/$username/backups 2>&1", $mkdirOutput, $mkdirReturnCode);
  55. if ($mkdirReturnCode !== 0) {
  56. error_log("deploy: ERROR: mkdir /home/$username/backups failed, details => " . implode("\n", $mkdirOutput));
  57. http_response_code(500);
  58. echo json_encode(['error' => 'Failed to create backups dir', 'details' => implode("\n", $mkdirOutput)]);
  59. return;
  60. }
  61. }
  62. if ($GLOBALS['debug'] == true) { error_log("Chown homedir: " . $username); }
  63. exec("sudo /usr/bin/chown $username:$username /home/$username -R 2>&1", $chownOutput, $chownReturnCode);
  64. if ($chownReturnCode !== 0) {
  65. error_log("deploy: ERROR: chown on /home/$username failed, details => " . implode("\n", $chownOutput));
  66. http_response_code(500);
  67. echo json_encode(['error' => 'Failed to chown backups dir', 'details' => implode("\n", $chownOutput)]);
  68. return;
  69. }
  70. // Create PHP-FPM User
  71. // /etc/php/8.2/fpm/user.in
  72. if ($userExisted != true) {
  73. if ($GLOBALS['debug'] == true) {error_log("Writing PHP-FPM Config for : " . $username); }
  74. $phpContent = file_get_contents("/etc/php/8.2/fpm/user.in");
  75. $phpContent = str_replace("USERNAME", $username, $phpContent);
  76. file_put_contents("/etc/php/8.2/fpm/pool.d/" . $username . ".conf", $phpContent);
  77. if ($GLOBALS['debug'] == true) {error_log("Restarting PHP-FPM : " . $username);}
  78. exec('sudo /usr/bin/systemctl reload php8.2-fpm', $phpOutput, $phpReturnCode);
  79. if ($phpReturnCode !== 0) {
  80. error_log("deploy: ERROR: PHP-FPM reload error, details => " . implode("\n", $phpOutput));
  81. http_response_code(500);
  82. echo json_encode(['error' => 'Failed to reload PHP-FPM', 'details' => implode("\n", $phpOutput)]);
  83. return;
  84. }
  85. }
  86. echo json_encode(['success' => 'User crreation successfully','details' => '']);
  87. }
  88. public static function terminate($data): void {
  89. $username = $data['username'] ?? '';
  90. $domain = $data['domain'] ?? '';
  91. if (empty($username)) {
  92. error_log("terminate: ERROR: No username provided");
  93. http_response_code(400);
  94. error_log(print_r($data,true));
  95. echo json_encode(['error' => 'Missing username']);
  96. return;
  97. }
  98. if( strpos(file_get_contents("/etc/passwd"),$username) == false) {
  99. error_log("terminate: ERROR: User $username does not exist");
  100. http_response_code(400);
  101. error_log(print_r($data,true));
  102. echo json_encode(['error' => 'Unknown user']);
  103. return;
  104. }
  105. // remove PHP-FPM User
  106. if ($GLOBALS['debug'] == true) {error_log("Removing PHP-FPM Config for : " . $username); }
  107. $configName = "/etc/php/8.2/fpm/pool.d/" . $username . ".conf";
  108. exec("sudo /usr/bin/rm -f $configName 2>&1", $userOutput, $userReturnCode);
  109. if ($GLOBALS['debug'] == true) {error_log("Restarting PHP-FPM : " . $username);}
  110. exec('sudo /usr/bin/systemctl reload php8.2-fpm', $phpOutput, $phpReturnCode);
  111. if ($phpReturnCode !== 0) {
  112. error_log("deploy: ERROR: PHP-FPM reload error, details => " . implode("\n", $phpOutput));
  113. http_response_code(500);
  114. echo json_encode(['error' => 'Failed to reload PHP-FPM', 'details' => implode("\n", $phpOutput)]);
  115. return;
  116. }
  117. if(!empty($domain)) {
  118. $configFile = "/etc/apache2/sites-enabled/$domain.conf";
  119. if ($GLOBALS['debug'] == true) { error_log("Remove config of user : " . $username); }
  120. exec("sudo /usr/bin/rm -f $configFile 2>&1", $userOutput, $userReturnCode);
  121. exec("sudo /usr/bin/certbot delete --cert-name $domain --non-interactive 2>&1", $output, $returnCode);
  122. if ($returnCode !== 0) {
  123. error_log("deploy: ERROR: certbot failed to delete certificate on $domain, details => " . implode("\n", $output));
  124. http_response_code(500);
  125. echo json_encode(['error' => 'Certbot failed', 'details' => implode("\n", $output)]);
  126. return;
  127. }
  128. exec('sudo /usr/bin/systemctl reload apache2 2>&1', $apacheOutput, $apacheReturnCode);
  129. if ($GLOBALS['debug'] == true) { error_log("Restarting Apache"); }
  130. if ($apacheReturnCode !== 0) {
  131. error_log("deploy: ERROR: Apache Reload error, details => " . implode("\n", $apacheOutput));
  132. http_response_code(500);
  133. echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $apacheOutput)]);
  134. return;
  135. }
  136. }
  137. // Remove user and files
  138. if ($GLOBALS['debug'] == true) {error_log("Remove User: " . $username); }
  139. exec("sudo /usr/sbin/userdel -r -f $username 2>&1", $userOutput, $userReturnCode);
  140. if ($userReturnCode !== 0) {
  141. error_log("deploy: ERROR: Userdel for $username failed, details => " . implode("\n", $userOutput));
  142. http_response_code(500);
  143. echo json_encode(['error' => 'Failed to remove user', 'details' => implode("\n", $userOutput)]);
  144. return;
  145. }
  146. echo json_encode(['success' => 'Removing user ' . $username . ' successfully']);
  147. }
  148. }