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.

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