OpenSource CLI-App to install and handle stuff related to Web-Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Factories;
  3. use PhpSchool\CliMenu\Builder\CliMenuBuilder;
  4. use PhpSchool\CliMenu\CliMenu;
  5. use PhpSchool\CliMenu\MenuItem\CheckboxItem;
  6. use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
  7. use PhpSchool\CliMenu\Style\SelectableStyle;
  8. use PhpSchool\CliMenu\Style\ItemStyle;
  9. class NginxVhostFactory
  10. {
  11. /**
  12. * adding Vhosts behind index
  13. *
  14. * @param [type] $mainmenu
  15. * @param [type] $vhosts
  16. * @param [type] $vhostIndex
  17. */
  18. public function addVhosts($mainmenu, $vhosts, $insertAfterIndex)
  19. {
  20. // get items
  21. $items = $mainmenu->getItems();
  22. // new items
  23. $newItems = [];
  24. // lastIndex
  25. $insertUntilIndex = 0;
  26. // get all items before
  27. foreach($items as $index => $item) {
  28. if ($index < $insertAfterIndex) {
  29. $newItems[] = $item;
  30. } else {
  31. break;
  32. }
  33. }
  34. // check for linebreak-object
  35. foreach($items as $index => $item) {
  36. if ($index >= $insertAfterIndex && get_class($item) === 'PhpSchool\CliMenu\MenuItem\LineBreakItem') {
  37. $insertUntilIndex = $index;
  38. break;
  39. }
  40. }
  41. // add submenu for each vhost
  42. foreach($vhosts as $vhost) {
  43. $newItems[] = self::createVhostSubmenu($vhost, $mainmenu);
  44. }
  45. // fillup last items from mainmenu
  46. foreach($items as $index => $item) {
  47. if ($index >= $insertUntilIndex) {
  48. $newItems[] = $item;
  49. }
  50. }
  51. $mainmenu->setItems($newItems);
  52. return $mainmenu;
  53. }
  54. /**
  55. *
  56. *
  57. * @param [type] $vhost
  58. * @param [type] $mainmenu
  59. * @return [type]
  60. */
  61. private function createVhostSubmenu($vhost, $mainmenu)
  62. {
  63. $builder = CliMenuBuilder::newSubMenu($mainmenu->getTerminal());
  64. // create checkbox for disable / enabled
  65. $checkbox = self::createVhostCheckbox($vhost);
  66. // if vhost is enabled
  67. if ($vhost['enabled']) {
  68. $checkbox->setChecked();
  69. }
  70. $builder
  71. ->setTitle('Nginx > '.$vhost['domain'])
  72. ->setGoBackButtonText('Back')
  73. // edit configuration
  74. ->addItem('edit', function(CliMenu $menu) use ($vhost) {
  75. system('nano /etc/nginx/sites-available/'.$vhost['file'].' > `tty`');
  76. })
  77. // delete configuration
  78. ->addItem('delete', function(CliMenu $menu) use ($vhost) {
  79. if ($vhost['enabled'] === true) {
  80. $menu->flash('Please disable first!')->display();
  81. } else {
  82. // input domain for confirmation
  83. $result = $menu->askText()
  84. ->setPromptText('Enter the domain name as confirmation: '.$vhost['domain'].' / [ESC] for Cancel')
  85. ->ask();
  86. // if result matching delete vhost an close menu
  87. if ($result->fetch() === $vhost['domain']) {
  88. unlink('/etc/nginx/sites-available/'.$vhost['file']);
  89. $menu->confirm($vhost['domain'].' is deleted!')->display('OK!');
  90. // get
  91. $parent = $menu->getParent();
  92. $menu->closeThis();
  93. // remove current vhost from mainmenu
  94. $parent->removeItem($parent->getSelectedItem());
  95. $parent->open();
  96. // cancel input
  97. } else if (empty($result->fetch())) {
  98. $menu->flash('Cancel')->display();
  99. // if domain not matching
  100. } else {
  101. $menu->flash('Not matching! Domain not deleted!')->display();
  102. }
  103. }
  104. })
  105. ->addLineBreak('-')
  106. ->addMenuItem($checkbox)
  107. ->addLineBreak('-');
  108. $submenu = $builder->build();
  109. $submenu->setParent($mainmenu);
  110. $submenu->setStyle($mainmenu->getStyle());
  111. // create MenuMenuItem
  112. $item = new MenuMenuItem(
  113. $vhost['domain'],
  114. $submenu,
  115. $builder->isMenuDisabled()
  116. );
  117. $item->getStyle()->setSelectedMarker("\xF0\x9D\x8C\xA1 ")->setUnselectedMarker(' ');
  118. // show item extra if domain is enabled
  119. $item->getStyle()->setItemExtra('[ enabled ]');
  120. if ($vhost['enabled']) {
  121. $item->showItemExtra();
  122. }
  123. return $item;
  124. }
  125. /**
  126. *
  127. * @param [type] $vhost
  128. * @return [type]
  129. */
  130. private function createVhostCheckbox($vhost)
  131. {
  132. // create checkbox for enabled / disabled
  133. $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($vhost) {
  134. // check status
  135. if ($menu->getSelectedItem()->getChecked()) {
  136. symlink('/etc/nginx/sites-available/'.$vhost['file'], '/etc/nginx/sites-enabled/'.$vhost['file']);
  137. exec('nginx -c /etc/nginx/nginx.conf -t 2>&1', $output);
  138. if ($output) {
  139. unlink('/etc/nginx/sites-enabled/'.$vhost['file']);
  140. $menu->confirm('Error! Configuration not Working!')->display('OK!');
  141. // @TODO: find a way to show logs, https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp/issues/15
  142. // implode(PHP_EOL, $output);
  143. $vhost['enabled'] = false;
  144. $status = 'disabled';
  145. } else {
  146. // restart
  147. exec('service nginx restart');
  148. $menu->confirm('Success! ')->display('OK!');
  149. $vhost['enabled'] = true;
  150. $status = 'enabled';
  151. }
  152. } else {
  153. unlink('/etc/nginx/sites-enabled/'.$vhost['file']);
  154. $vhost['enabled'] = false;
  155. $status = 'disabled';
  156. }
  157. if ($vhost['enabled']) {
  158. $menu->getSelectedItem()->setChecked();
  159. } else {
  160. $menu->getSelectedItem()->setUnchecked();
  161. }
  162. $menu->redraw();
  163. });
  164. return $checkbox;
  165. }
  166. }