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.

250 lines
7.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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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\Commands;
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use LaravelZero\Framework\Commands\Command;
  5. use Illuminate\Support\Facades\File;
  6. use App\Facades\Install;
  7. use App\Facades\NginxVhost;
  8. use App\Facades\NginxVhostFactory;
  9. use App\Facades\TerminalHelper;
  10. use App\Facades\Menus\StylesFactory;
  11. use App\Helpers\NginxTemplateHelper;
  12. use App\Menus\Nginx\NginxVhostGoBackAction;
  13. use App\BladeFile;
  14. use PhpSchool\CliMenu\Builder\CliMenuBuilder;
  15. use PhpSchool\CliMenu\CliMenu;
  16. use PhpSchool\CliMenu\MenuItem\CheckboxItem;
  17. use PhpSchool\CliMenu\Style\SelectableStyle;
  18. use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
  19. use Respect\Validation\Validator as v;
  20. use Respect\Validation\Exceptions\NestedValidationException;
  21. use Log;
  22. use Closure;
  23. /**
  24. * Add and Edit Configurations of Vhosts from Nginx
  25. *
  26. * Using php-school/cli-menu
  27. *
  28. *
  29. *
  30. */
  31. class NginxVhostsCommand extends Command
  32. {
  33. // path templates
  34. const TEMPLATES_DIR = '/resources/nginx/templates';
  35. // index for vhosts
  36. const VHOST_INDEX = 0;
  37. /**
  38. * The signature of the command.
  39. *
  40. * @var string
  41. */
  42. protected $signature = 'nginx:vhosts';
  43. /**
  44. * The description of the command.
  45. *
  46. * @var string
  47. */
  48. protected $description = 'Create and Manage Nginx Vhosts';
  49. /**
  50. *
  51. *
  52. *
  53. */
  54. private function selectTemplate()
  55. {
  56. $menu = function(CliMenuBuilder $builder)
  57. {
  58. // create blade
  59. $bladeFile = new BladeFile(self::TEMPLATES_DIR);
  60. $builder
  61. ->setTitle('Nginx > Add')
  62. ->setGoBackButtonText('Back');
  63. $nginxTemplateHelper = new NginxTemplateHelper();
  64. foreach($nginxTemplateHelper->find() as $template) {
  65. $submenuCallable = $this->createConfiguration($template, $bladeFile);
  66. $builder
  67. ->addSubMenu($template['name'], $submenuCallable);
  68. }
  69. $builder->addLineBreak('-');
  70. };
  71. return $menu;
  72. }
  73. /**
  74. * create Configuration, add for each template a submenu
  75. *
  76. *
  77. * @param array $template
  78. * @param Blade $blade
  79. * @return CliMenuBuilder
  80. */
  81. private function createConfiguration($template, $bladeFile)
  82. {
  83. $menu = function(CliMenuBuilder $builder) use ($template, $bladeFile)
  84. {
  85. $configuration = [
  86. 'domain' => '',
  87. 'root' => '',
  88. 'index' => 'index.php',
  89. 'ssl' => true,
  90. 'redirect_www' => true
  91. ];
  92. exec('find /lib/systemd/system/ -name "php[0-9\.]*-fpm.service"', $files);
  93. // create checkbox for ssl
  94. $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) {
  95. $configuration['ssl'] = $menu->getSelectedItem()->getChecked();
  96. });
  97. $checkboxSSL->setChecked($configuration['ssl']);
  98. // create checkbox for redirect from www
  99. $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) use (&$configuration) {
  100. $configuration['redirect_www'] = $menu->getSelectedItem()->getChecked();
  101. });
  102. $checkboxRedirect->setChecked($configuration['redirect_www']);
  103. $builder
  104. ->setTitle('Nginx > Add > '.$template['name'])
  105. ->setGoBackButtonText('Cancel')
  106. // input domain
  107. ->addItem('domain: -', function(CliMenu $menu) use (&$configuration) {
  108. $result = $menu->askText()->ask();
  109. $configuration['domain'] = $result->fetch();
  110. $menu->getSelectedItem()->setText('domain: '.$result->fetch());
  111. $menu->redraw();
  112. })
  113. // input root
  114. ->addItem('root: -', function(CliMenu $menu) use (&$configuration) {
  115. $result = $menu->askText()->ask();
  116. $configuration['root'] = $result->fetch();
  117. $menu->getSelectedItem()->setText('root: '.$result->fetch());
  118. $menu->redraw();
  119. })
  120. // input index
  121. ->addItem('index: '.$configuration['index'], function(CliMenu $menu) use (&$configuration) {
  122. $result = $menu->askText()->ask();
  123. $configuration['index'] = $result->fetch();
  124. $menu->getSelectedItem()->setText('index: '.$result->fetch());
  125. $menu->redraw();
  126. })
  127. ->addLineBreak('-');
  128. foreach($files as $index => $file) {
  129. $file = str_replace('/lib/systemd/system/', '', $file);
  130. $file = str_replace('.service', '', $file);
  131. $builder->addRadioItem($file, function(CliMenu $menu) use (&$configuration) {
  132. $configuration['phpFpm'] = $menu->getSelectedItem()->getText();
  133. });
  134. }
  135. $builder
  136. ->addLineBreak('-')
  137. // options
  138. ->addMenuItem($checkboxSSL)
  139. ->addMenuItem($checkboxRedirect)
  140. ->addLineBreak('-')
  141. // create
  142. ->addItem('Create', function(CliMenu $menu) use (&$configuration, $template, $bladeFile) {
  143. $data = $configuration;
  144. // add directory for validator to check if file exits
  145. $data['index'] = $data['root'].'/'.$data['index'];
  146. $validator = v::key('domain', v::domain(false))
  147. ->key('root', v::directory())
  148. ->key('index', v::file())
  149. ->key('phpFpm', v::notEmpty());
  150. try {
  151. $validator->assert($data);
  152. } catch(NestedValidationException $exception) {
  153. $errors = $exception->getMessages();
  154. }
  155. if (isset($errors)) {
  156. TerminalHelper::confirmArray($menu, $errors);
  157. } else {
  158. // create filename
  159. $filename = $configuration['domain'].'.conf';
  160. // write configuration to file
  161. $bladeFile->put($template['name'], '/etc/nginx/sites-available/'.$filename, $configuration);
  162. $menu->confirm('Success!')->display('Ok!');
  163. // invoke action
  164. $action = new NginxVhostGoBackAction();
  165. is_callable($action($menu));
  166. }
  167. })
  168. ->addLineBreak('-');
  169. };
  170. return $menu;
  171. }
  172. /**
  173. * let it rain
  174. *
  175. *
  176. */
  177. public function handle()
  178. {
  179. $submenuSelectTemplate = $this->selectTemplate();
  180. // create menu
  181. $builder = $this->menu('Nginx')
  182. // add Submenu for select templates
  183. ->addLineBreak('-')
  184. ->addSubMenu('Add', $submenuSelectTemplate);
  185. $builder->addLineBreak('-');
  186. // apperance
  187. $builder = StylesFactory::setMenuStyles($builder);
  188. $mainmenu = $builder->build();
  189. // remove first item
  190. $items = $mainmenu->getItems();
  191. unset($items[0]);
  192. $mainmenu->setItems($items);
  193. // adding current vhosts
  194. $mainmenu = NginxVhostFactory::addVhosts($mainmenu, NginxVhost::find(), self::VHOST_INDEX);
  195. $mainmenu->open();
  196. }
  197. }