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.

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