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.

158 lines
4.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
  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. class NginxVhostFactory
  8. {
  9. /**
  10. * adding Vhosts behind index
  11. *
  12. * @param [type] $mainmenu
  13. * @param [type] $vhosts
  14. * @param [type] $vhostIndex
  15. */
  16. public function addVhosts($mainmenu, $vhosts, $vhostIndex)
  17. {
  18. // get items
  19. $items = $mainmenu->getItems();
  20. // new items
  21. $newItems = [];
  22. foreach($items as $index => $item) {
  23. if ($index <= $vhostIndex) {
  24. $newItems[] = $item;
  25. } else {
  26. break;
  27. }
  28. }
  29. // check for linebreak-object
  30. foreach($items as $index => $item) {
  31. if ($index > $vhostIndex && get_class($item) === 'PhpSchool\CliMenu\MenuItem\LineBreakItem') {
  32. $startIndex = $index;
  33. break;
  34. }
  35. }
  36. // add submenu for each vhost
  37. foreach($vhosts as $vhost) {
  38. $newItems[] = self::createVhostSubmenu($vhost, $mainmenu);
  39. }
  40. // fillup last items from mainmenu
  41. foreach($items as $index => $item) {
  42. if ($index >= $startIndex) {
  43. $newItems[] = $item;
  44. }
  45. }
  46. $mainmenu->setItems($newItems);
  47. return $mainmenu;
  48. }
  49. /**
  50. *
  51. *
  52. * @param [type] $vhost
  53. * @param [type] $mainmenu
  54. * @return [type]
  55. */
  56. private function createVhostSubmenu($vhost, $mainmenu)
  57. {
  58. $builder = CliMenuBuilder::newSubMenu($mainmenu->getTerminal());
  59. // create checkbox for disable / enabled
  60. $checkbox = self::createVhostCheckbox($vhost);
  61. // adding checkbox
  62. if ($vhost['enabled']) {
  63. $checkbox->setChecked(true);
  64. }
  65. $builder
  66. ->setTitle('Nginx > '.$vhost['domain'])
  67. ->setGoBackButtonText('Back')
  68. ->addLineBreak('')
  69. ->addItem('edit', function(CliMenu $menu) use ($vhost) {
  70. system('nano /etc/nginx/sites-available/'.$vhost['file'].' > `tty`');
  71. })
  72. ->addItem('delete', function(CliMenu $menu) use ($vhost) {
  73. if (file_exists('/etc/nginx/sites-enabled/'.$vhost['file'])) {
  74. $menu->confirm('Error! Please disable '.$vhost['domain'].' first!')->display('OK!');
  75. } else {
  76. unlink('/etc/nginx/sites-available/'.$vhost['file']);
  77. $menu->confirm($vhost['domain'].' is deleted!')->display('OK!');
  78. // get
  79. $parent = $menu->getParent();
  80. $menu->closeThis();
  81. // remove current vhost from mainmenu
  82. $parent->removeItem($parent->getSelectedItem());
  83. $parent->open();
  84. }
  85. })
  86. ->addLineBreak('-')
  87. ->addMenuItem($checkbox)
  88. ->addLineBreak('-');
  89. $submenu = $builder->build();
  90. $submenu->setParent($mainmenu);
  91. $submenu->setStyle($mainmenu->getStyle());
  92. $item = new MenuMenuItem(
  93. $vhost['domain'],
  94. $submenu,
  95. $builder->isMenuDisabled()
  96. );
  97. // show item extra if domain is enabled
  98. $item->getStyle()->setItemExtra('[ enabled ]');
  99. if ($vhost['enabled']) {
  100. $item->showItemExtra();
  101. }
  102. return $item;
  103. }
  104. /**
  105. *
  106. * @param [type] $vhost
  107. * @return [type]
  108. */
  109. private function createVhostCheckbox($vhost)
  110. {
  111. // create checkbox for enabled / disabled
  112. $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($vhost) {
  113. // check status
  114. if ($menu->getSelectedItem()->getChecked()) {
  115. symlink('/etc/nginx/sites-available/'.$vhost['file'], '/etc/nginx/sites-enabled/'.$vhost['file']);
  116. $status = 'enabled';
  117. } else {
  118. unlink('/etc/nginx/sites-enabled/'.$vhost['file']);
  119. $status = 'disabled';
  120. }
  121. // restart
  122. exec('service nginx restart');
  123. exec('service nginx status', $output);
  124. if (strpos(implode(' ', $output), 'active') !== false) {
  125. $menu->confirm($vhost['domain'].' is '.$status.'!')->display('OK!');
  126. } else {
  127. $menu->confirm('Error! Something not working!')->display('OK!');
  128. }
  129. });
  130. return $checkbox;
  131. }
  132. }