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.

186 lines
5.4 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
  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. // create checkbox for disable / enabled
  67. $checkbox = self::createVhostCheckbox($vhost);
  68. // adding checkbox
  69. if ($vhost['enabled']) {
  70. $checkbox->setChecked(true);
  71. }
  72. $builder
  73. ->setTitle('Nginx > '.$vhost['domain'])
  74. ->setGoBackButtonText('Back')
  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. // input domain for confirmation
  82. $result = $menu->askText()
  83. ->setPromptText('Enter the domain name as confirmation: '.$vhost['domain'].' / [ESC] for Cancel')
  84. ->ask();
  85. // if result matching delete vhost an close menu
  86. if ($result->fetch() === $vhost['domain']) {
  87. unlink('/etc/nginx/sites-available/'.$vhost['file']);
  88. $menu->confirm($vhost['domain'].' is deleted!')->display('OK!');
  89. // get
  90. $parent = $menu->getParent();
  91. $menu->closeThis();
  92. // remove current vhost from mainmenu
  93. $parent->removeItem($parent->getSelectedItem());
  94. $parent->open();
  95. // cancel input
  96. } else if (empty($result->fetch())) {
  97. $menu->flash('Cancel')->display();
  98. // if domain not matching
  99. } else {
  100. $menu->flash('Not matching! Domain not deleted!')->display();
  101. }
  102. }, false, $vhost['enabled'])
  103. ->addLineBreak('-')
  104. ->addMenuItem($checkbox)
  105. ->addLineBreak('-');
  106. $submenu = $builder->build();
  107. $submenu->setParent($mainmenu);
  108. $submenu->setStyle($mainmenu->getStyle());
  109. // create MenuMenuItem
  110. $item = new MenuMenuItem(
  111. $vhost['domain'],
  112. $submenu,
  113. $builder->isMenuDisabled()
  114. );
  115. $item->getStyle()->setSelectedMarker("\xF0\x9D\x8C\xA1 ")->setUnselectedMarker(' ');
  116. // show item extra if domain is enabled
  117. $item->getStyle()->setItemExtra('[ enabled ]');
  118. if ($vhost['enabled']) {
  119. $item->showItemExtra();
  120. }
  121. return $item;
  122. }
  123. /**
  124. *
  125. * @param [type] $vhost
  126. * @return [type]
  127. */
  128. private function createVhostCheckbox($vhost)
  129. {
  130. // create checkbox for enabled / disabled
  131. $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($vhost) {
  132. // check status
  133. if ($menu->getSelectedItem()->getChecked()) {
  134. symlink('/etc/nginx/sites-available/'.$vhost['file'], '/etc/nginx/sites-enabled/'.$vhost['file']);
  135. $status = 'enabled';
  136. } else {
  137. unlink('/etc/nginx/sites-enabled/'.$vhost['file']);
  138. $status = 'disabled';
  139. }
  140. // restart
  141. exec('service nginx restart');
  142. exec('service nginx status', $output);
  143. if (strpos(implode(' ', $output), 'active') !== false) {
  144. $menu->confirm($vhost['domain'].' is '.$status.'!')->display('OK!');
  145. } else {
  146. $menu->confirm('Error! Something not working!')->display('OK!');
  147. }
  148. });
  149. return $checkbox;
  150. }
  151. }