SiteController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace application\controllers;
  3. class SiteController {
  4. public static function init($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(" Starting function init for " . $username . " and " . $domain . " DebugMode: " . $GLOBALS['debug']);
  27. if ($GLOBALS['debug'] == true) { error_log("Creating Webdir ($webDir) for : " . $username); }
  28. exec("sudo /usr/bin/mkdir -p $webDir 2>&1", $mkdirOutput, $mkdirReturnCode);
  29. if ($mkdirReturnCode !== 0) {
  30. error_log("init: ERROR: Create Webdir $webDir for $username failed, details => ". implode("\n", $mkdirOutput));
  31. http_response_code(500);
  32. echo json_encode(['error' => 'Failed to create web dir', 'details' => implode("\n", $mkdirOutput)]);
  33. return;
  34. }
  35. if ($GLOBALS['debug'] == true) { error_log("Chown webdir: " . $username); }
  36. exec("sudo /usr/bin/chown $username:$username $webDir -R 2>&1", $chownOutput, $chownReturnCode);
  37. if ($chownReturnCode !== 0) {
  38. error_log("deploy: ERROR: chown on $webDir failed, details => " . implode("\n", $chownOutput));
  39. http_response_code(500);
  40. echo json_encode(['error' => 'Failed to chown webdir', 'details' => implode("\n", $chownOutput)]);
  41. return;
  42. }
  43. if ($GLOBALS['debug'] == true) { error_log("Reading Apache Config Template /etc/apache2/site-config.in"); }
  44. $configContent = file_get_contents($configTemplate);
  45. if ($GLOBALS['debug'] == true) { error_log("Replace config settings in Apache Config Template"); }
  46. $configContent = str_replace(['DOCUMENTROOT', 'SERVERNAME','USERNAME', 'DOMAINNAME', 'SERVERALIAS'], [$webDir, $domain, $username, $domain, "" ], $configContent);
  47. if ($GLOBALS['debug'] == true) { error_log("Running Certbot for Domain " . $domain); }
  48. exec("sudo /usr/bin/certbot certonly --webroot -w /etc/apache2/letsencrypt -d $domain --non-interactive --agree-tos --email admin@$domain 2>&1", $output, $returnCode);
  49. if ($returnCode !== 0) {
  50. error_log("deploy: ERROR: certbot failed to create certificate on $domain, details => " . implode("\n", $output));
  51. http_response_code(500);
  52. echo json_encode(['error' => 'Certbot failed', 'details' => implode("\n", $output)]);
  53. return;
  54. }
  55. if ($GLOBALS['debug'] == true) { error_log("Replace sslsettings in in Apache Config Template"); }
  56. $configContent = str_replace('DOMAINNAME', $domain, $configContent);
  57. if ($GLOBALS['debug'] == true) { error_log("Writing apache config file to " . $configFile); }
  58. if (file_put_contents($configFile, $configContent) != true) {
  59. error_log("deploy: ERROR: while writing apache config");
  60. echo json_encode(['error' => 'Failed to write Apache config', 'details' => []]);
  61. }
  62. exec('sudo /usr/bin/systemctl reload apache2 2>&1', $apacheOutput, $apacheReturnCode);
  63. if ($GLOBALS['debug'] == true) { error_log("Restarting Apache"); }
  64. if ($apacheReturnCode !== 0) {
  65. error_log("deploy: ERROR: Apache Reload error, details => " . implode("\n", $apacheOutput));
  66. http_response_code(500);
  67. echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $apacheOutput)]);
  68. return;
  69. }
  70. if ($GLOBALS['debug'] == true) { error_log("Install Placeholder on : " . $webDir); }
  71. exec("sudo /usr/bin/cp -rf $placeholderdir/* $webDir/. 2>&1", $mkdirOutput, $cpReturnCode);
  72. if ($cpReturnCode !== 0) {
  73. error_log("deploy: ERROR: Copy placeholder for $username failed, details => ". implode("\n", $mkdirOutput));
  74. http_response_code(500);
  75. echo json_encode(['error' => 'Failed to copy placeholder', 'details' => implode("\n", $mkdirOutput)]);
  76. return;
  77. }
  78. echo json_encode(['success' => 'Development site deployed successfully','details' => '']);
  79. }
  80. public static function deploy($data): void {
  81. $username = $data['username'] ?? '';
  82. $domain = $data['domain'] ?? '';
  83. $webFileUrl = $data['zip'] ?? '';
  84. $webDir = "/home/$username/$domain";
  85. error_log("deploy: DEBUG: " . print_r($data, true));
  86. if (empty($username) || empty($domain) || empty($webFileUrl)) {
  87. http_response_code(400);
  88. echo json_encode(['error' => 'Missing required parameter']);
  89. return;
  90. }
  91. $parts = explode('/', $webFileUrl);
  92. $webFile = $parts[count($parts) - 1];
  93. $tmpFile = sys_get_temp_dir() . '/' . $webFile;
  94. if ($GLOBALS['debug'] == true) { error_log("Deploy new site on : " . $webDir); }
  95. // download file
  96. $options = array(
  97. CURLOPT_FILE => fopen($tmpFile, 'w'),
  98. CURLOPT_URL => $webFileUrl,
  99. CURLOPT_TIMEOUT => 28800 // 8 hours timeout
  100. );
  101. $ch = curl_init();
  102. curl_setopt_array($ch, $options);
  103. curl_exec($ch);
  104. $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  105. $error = curl_error($ch);
  106. if ($status != 200) {
  107. http_response_code($status);
  108. echo json_encode(['error' => 'Failed to download archive', 'details' => $error]);
  109. return;
  110. }
  111. curl_close($ch);
  112. // unzip file
  113. exec("sudo /usr/bin/unzip -q -o $tmpFile -d $webDir/ 2>&1", $unzipOutput, $unzipReturnCode);
  114. if ($unzipReturnCode !== 0) {
  115. error_log("deploy: ERROR: deplyoment of $domain failed, details => ". implode("\n", $unzipOutput));
  116. http_response_code(500);
  117. echo json_encode(['error' => 'Failed to copy placeholder', 'details' => implode("\n", $unzipOutput)]);
  118. return;
  119. }
  120. // set ownership
  121. if ($GLOBALS['debug'] == true) { error_log("Chown webdir: " . $domain); }
  122. exec("sudo /usr/bin/chown $username:$username /home/$username/$domain -R 2>&1", $chownOutput, $chownReturnCode);
  123. if ($chownReturnCode !== 0) {
  124. error_log("deploy: ERROR: chown on /home/$username/$domain failed, details => " . implode("\n", $chownOutput));
  125. http_response_code(500);
  126. echo json_encode(['error' => 'Failed to chown backups dir', 'details' => implode("\n", $chownOutput)]);
  127. return;
  128. }
  129. // remove tmpfile
  130. if ($GLOBALS['debug'] == true) { error_log("Remove tmpdile: " . $tmpFile); }
  131. exec("sudo /usr/bin/rm -f $tmpFile 2>&1", $rmOutput, $rmReturnCode);
  132. if ($rmReturnCode !== 0) {
  133. error_log("deploy: ERROR: could not remove $tmpFile, details => " . implode("\n", $rmOutput));
  134. http_response_code(500);
  135. echo json_encode(['error' => 'Failed to chown backups dir', 'details' => implode("\n", $rmOutput)]);
  136. return;
  137. }
  138. // echo json_encode(['success' => 'Site deployed successfully','details' => '']);
  139. }
  140. public static function revert($data): void {
  141. $username = $data['username'] ?? '';
  142. $domain = $data['domain'] ?? '';
  143. $webDir = "/home/$username/$domain";
  144. $placeholderdir = "/var/www/catchall";
  145. exec("sudo /usr/bin/rm -rf $webDir 2>&1", $rmOutput, $rmReturnCode);
  146. if ($rmReturnCode !== 0) {
  147. error_log("revert: error on rm $webDir -r, details => " . implode("\n", $rmOutput));
  148. http_response_code(500);
  149. echo json_encode(['error' => 'Failed to remove webdir dir', 'details' => implode("\n", $rmOutput)]);
  150. return;
  151. }
  152. exec("sudo /usr/bin/mkdir $webDir 2>&1", $mkdirOutput, $mkdirReturnCode);
  153. error_log("revert: error on mkdir $webDir, details => " . implode("\n", $mkdirOutput));
  154. if ($mkdirReturnCode !== 0) {
  155. http_response_code(500);
  156. echo json_encode(['error' => 'Failed to create web dir', 'details' => implode("\n", $mkdirOutput)]);
  157. return;
  158. }
  159. if ($GLOBALS['debug'] == true) { error_log("Install Placeholder on : " . $webDir); }
  160. exec("sudo /usr/bin/cp -rf $placeholderdir/* $webDir/. 2>&1", $mkdirOutput, $cpReturnCode);
  161. if ($cpReturnCode !== 0) {
  162. error_log("deploy: ERROR: Copy placeholder for $username failed, details => ". implode("\n", $mkdirOutput));
  163. http_response_code(500);
  164. echo json_encode(['error' => 'Failed to copy placeholder', 'details' => implode("\n", $mkdirOutput)]);
  165. return;
  166. }
  167. if ($GLOBALS['debug'] == true) { error_log("Install Placeholder on : " . $webDir); }
  168. exec("sudo /usr/bin/cp -rf $placeholderdir/* $webDir/. 2>&1", $mkdirOutput, $cpReturnCode);
  169. if ($cpReturnCode !== 0) {
  170. error_log("deploy: ERROR: Copy placeholder for $username failed, details => ". implode("\n", $mkdirOutput));
  171. http_response_code(500);
  172. echo json_encode(['error' => 'Failed to copy placeholder', 'details' => implode("\n", $mkdirOutput)]);
  173. return;
  174. }
  175. if ($GLOBALS['debug'] == true) { error_log("Change ownership on : " . $webDir); }
  176. exec("sudo /usr/bin/chown $username:$username $webDir 2>&1", $chownOutput, $chownReturnCode);
  177. if ($chownReturnCode !== 0) {
  178. error_log("revert: chown error on $webDir, details => " . implode("\n", $chownOutput));
  179. http_response_code(500);
  180. echo json_encode(['error' => 'Failed to chown web dir', 'details' => implode("\n", $chownOutput)]);
  181. return;
  182. }
  183. echo json_encode(['success' => 'Development site deployed successfully']);
  184. }
  185. public static function disable($data): void {
  186. $domain = $data['domain'] ?? '';
  187. if (empty($domain)) {
  188. http_response_code(400);
  189. echo json_encode(['error' => 'Missing required parameter: domainname']);
  190. return;
  191. }
  192. $enabledConfig = "/etc/apache2/sites-enabled/$domain.conf";
  193. $availableConfig = "/etc/apache2/sites-available/$domain.conf";
  194. if (!file_exists($enabledConfig)) {
  195. http_response_code(404);
  196. echo json_encode(['error' => 'Configuration file not found']);
  197. return;
  198. }
  199. if (!rename($enabledConfig, $availableConfig)) {
  200. http_response_code(500);
  201. echo json_encode(['error' => 'Failed to move configuration file']);
  202. return;
  203. }
  204. exec('systemctl reload apache2', $output, $returnCode);
  205. if ($returnCode !== 0) {
  206. http_response_code(500);
  207. echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $output)]);
  208. return;
  209. }
  210. echo json_encode(['success' => 'Production site disabled successfully']);
  211. }
  212. public static function enable($data): void {
  213. $domain = $data['domain'] ?? '';
  214. if (empty($domain)) {
  215. http_response_code(400);
  216. echo json_encode(['error' => 'Missing required parameter: domainname']);
  217. return;
  218. }
  219. $availableConfig = "/etc/apache2/sites-available/$domain.conf";
  220. $enabledConfig = "/etc/apache2/sites-enabled/$domain.conf";
  221. if (!file_exists($availableConfig)) {
  222. http_response_code(404);
  223. echo json_encode(['error' => 'Configuration file not found']);
  224. return;
  225. }
  226. if (!rename($availableConfig, $enabledConfig)) {
  227. http_response_code(500);
  228. echo json_encode(['error' => 'Failed to move configuration file']);
  229. return;
  230. }
  231. exec('systemctl reload apache2', $output, $returnCode);
  232. if ($returnCode !== 0) {
  233. http_response_code(500);
  234. echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $output)]);
  235. return;
  236. }
  237. echo json_encode(['success' => 'Production site enabled successfully']);
  238. }
  239. public static function isenabled($data): void {
  240. $domain = $data['domain'] ?? '';
  241. if (file_exists("/etc/apache2/sites-enabled/$domain.conf")) {
  242. echo json_encode(['isenabled' => 'YES']);
  243. } else {
  244. echo json_encode(['isenabled' => 'NO']);
  245. }
  246. }
  247. public static function migrate($data): void {
  248. $username = $data['username'] ?? '';
  249. $domain = $data['domain'] ?? '';
  250. $adminName = $data['admin_name'] ?? '';
  251. $adminPassword = $data['admin_password'] ?? '';
  252. if (empty($username) || empty($domain) || empty($adminName) || empty($adminPassword)) {
  253. http_response_code(400);
  254. echo json_encode(['error' => 'Missing required parameters']);
  255. return;
  256. }
  257. // Deployment from dev to prod
  258. exec("sudo /usr/bin/cp -a /home/$username/dev.$domain /home/$username/$domain",$cpOutput,$cpReturnCode);
  259. if ($cpReturnCode !== 0) {
  260. http_response_code(500);
  261. echo json_encode(['error' => 'Failed to copy dev installation into prod directory', 'details' => implode("\n", $cpOutput)]);
  262. return;
  263. }
  264. echo json_encode(['success' => 'Production site successfully from dev']);
  265. }
  266. public static function undeploy($data): void {
  267. $username = $data['username'] ?? '';
  268. $domain = $data['domain'] ?? '';
  269. if (empty($username)) {
  270. error_log("Undeploy: ERROR: No username provided");
  271. http_response_code(400);
  272. error_log(print_r($data,true));
  273. echo json_encode(['error' => 'Missing username']);
  274. return;
  275. }
  276. if( strpos(file_get_contents("/etc/passwd"),$username) == false) {
  277. error_log("Undeploy: ERROR: User $username does not exist");
  278. http_response_code(400);
  279. error_log(print_r($data,true));
  280. echo json_encode(['error' => 'Unknown user']);
  281. return;
  282. }
  283. if(!empty($domain)) {
  284. $enableConfigFile = "/etc/apache2/sites-enabled/$domain.conf";
  285. $disableConfigFile = "/etc/apache2/sites-available/$domain.conf";
  286. if ($GLOBALS['debug'] == true) { error_log("Remove config of user : " . $username); }
  287. exec("sudo /usr/bin/rm -f $enableConfigFile 2>&1", $userOutput, $userReturnCode);
  288. exec("sudo /usr/bin/rm -f $disableConfigFile 2>&1", $userOutput, $userReturnCode);
  289. exec("sudo /usr/bin/certbot delete --cert-name $domain --non-interactive 2>&1", $output, $returnCode);
  290. if ($returnCode !== 0) {
  291. error_log("Undeploy: ERROR: certbot failed to delete certificate on $domain, details => " . implode("\n", $output));
  292. http_response_code(500);
  293. echo json_encode(['error' => 'Certbot failed', 'details' => implode("\n", $output)]);
  294. return;
  295. }
  296. exec('sudo /usr/bin/systemctl reload apache2 2>&1', $apacheOutput, $apacheReturnCode);
  297. if ($GLOBALS['debug'] == true) { error_log("Restarting Apache"); }
  298. if ($apacheReturnCode !== 0) {
  299. error_log("Undeploy: ERROR: Apache Reload error, details => " . implode("\n", $apacheOutput));
  300. http_response_code(500);
  301. echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $apacheOutput)]);
  302. return;
  303. }
  304. }
  305. echo json_encode(['success' => 'Undeploy site prod.' . $domain . ' successfully']);
  306. }
  307. }