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.

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