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.

268 lines
8.1 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
  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 PhpSchool\CliMenu\Builder\CliMenuBuilder;
  10. use PhpSchool\CliMenu\CliMenu;
  11. use PhpSchool\CliMenu\MenuItem\CheckboxItem;
  12. use PhpSchool\CliMenu\Style\SelectableStyle;
  13. use Log;
  14. use Closure;
  15. class NginxVhostsCommand extends Command
  16. {
  17. // path templates
  18. const TEMPLATES_DIR = '/resources/nginx/templates';
  19. /**
  20. * The signature of the command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'nginx:vhosts';
  25. /**
  26. * The description of the command.
  27. *
  28. * @var string
  29. */
  30. protected $description = 'Create and Manage Nginx Vhosts';
  31. /**
  32. *
  33. *
  34. *
  35. */
  36. private function getTemplates($prefix = '')
  37. {
  38. // templates
  39. $templates = [];
  40. // getting templates
  41. $files = scandir(base_path().self::TEMPLATES_DIR);
  42. foreach($files as $file) {
  43. // create filepath
  44. $filepath = base_path().self::TEMPLATES_DIR.'/'.$file;
  45. // getting info
  46. $pathinfo = pathinfo($filepath);
  47. // if extension isset and php
  48. if (isset($pathinfo['extension']) && $pathinfo['extension'] === 'php') {
  49. $name = str_replace('.blade.php', '', $file);
  50. $templates[] = [
  51. 'name' => $name,
  52. 'file' => $file,
  53. 'filepath' => $filepath
  54. ];
  55. }
  56. }
  57. return $templates;
  58. }
  59. /**
  60. *
  61. *
  62. *
  63. */
  64. private function selectTemplate()
  65. {
  66. // getting templates
  67. $templates = $this->getTemplates();
  68. $menu = function(CliMenuBuilder $builder) use ($templates)
  69. {
  70. // writing configuration
  71. $blade = new Blade(base_path().self::TEMPLATES_DIR, base_path().'/storage/cache');
  72. $builder
  73. ->setTitle('Nginx > add')
  74. ->setGoBackButtonText('Back');
  75. foreach($templates as $template) {
  76. $submenuCallable = $this->createConfiguration($template, $blade);
  77. $builder
  78. ->addSubMenu($template['name'], $submenuCallable);
  79. }
  80. $builder->addLineBreak('-');
  81. };
  82. return $menu;
  83. }
  84. /**
  85. *
  86. *
  87. * @param [type] $template [description]
  88. * @param [type] $blade [description]
  89. * @return [type] [description]
  90. */
  91. private function createConfiguration($template, $blade)
  92. {
  93. $menu = function(CliMenuBuilder $builder) use ($template, $blade)
  94. {
  95. $configuration = [
  96. 'domain' => '',
  97. 'root' => '',
  98. 'index' => 'index.php',
  99. 'ssl' => true,
  100. 'redirect_www' => true
  101. ];
  102. // create checkbox for ssl
  103. $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) {
  104. $configuration['ssl'] = $menu->getSelectedItem()->getChecked();
  105. });
  106. $checkboxSSL->setChecked($configuration['ssl']);
  107. // create checkbox for redirect from www
  108. $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) use (&$configuration) {
  109. $configuration['redirect_www'] = $menu->getSelectedItem()->getChecked();
  110. });
  111. $checkboxRedirect->setChecked($configuration['redirect_www']);
  112. $builder
  113. ->setTitle('Nginx Vhosts > add > '.$template['name'])
  114. ->addItem('domain', function(CliMenu $menu) use (&$configuration) {
  115. $result = $menu->askText()->ask();
  116. $configuration['domain'] = $result->fetch();
  117. $menu->getSelectedItem()->setText('domain -> '.$result->fetch());
  118. $menu->redraw();
  119. })
  120. ->addItem('root', function(CliMenu $menu) use (&$configuration) {
  121. $result = $menu->askText()->ask();
  122. $configuration['root'] = $result->fetch();
  123. $menu->getSelectedItem()->setText('root -> '.$result->fetch());
  124. $menu->redraw();
  125. })
  126. ->addItem($configuration['index'], function(CliMenu $menu) use (&$configuration) {
  127. $result = $menu->askText()->ask();
  128. $configuration['index'] = $result->fetch();
  129. $menu->getSelectedItem()->setText($result->fetch());
  130. $menu->redraw();
  131. })
  132. ->addLineBreak('-')
  133. ->addMenuItem($checkboxSSL)
  134. ->addMenuItem($checkboxRedirect)
  135. ->addLineBreak('-')
  136. ->addItem('save', function() use (&$configuration, $template, $blade) {
  137. $content = $blade->render($template['name'], $configuration);
  138. file_put_contents('/etc/nginx/sites-available/'.$configuration['domain'].'.conf', $content);
  139. })
  140. ->addLineBreak('-');
  141. };
  142. return $menu;
  143. }
  144. /**
  145. * create submenu for vhost
  146. *
  147. *
  148. * @param array $vhost
  149. * @return CliMenuBuilder
  150. *
  151. */
  152. private function vhost($vhost)
  153. {
  154. $menu = function(CliMenuBuilder $builder) use ($vhost)
  155. {
  156. $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($vhost) {
  157. // check status
  158. if ($menu->getSelectedItem()->getChecked()) {
  159. symlink('/etc/nginx/sites-available/'.$vhost['file'], '/etc/nginx/sites-enabled/'.$vhost['file']);
  160. $status = 'enabled';
  161. } else {
  162. unlink('/etc/nginx/sites-enabled/'.$vhost['file']);
  163. $status = 'disabled';
  164. }
  165. // restart
  166. exec('service nginx restart');
  167. exec('service nginx status', $output);
  168. if (strpos(implode(' ', $output), 'active') !== false) {
  169. $menu->confirm($vhost['domain'].' is '.$status.'!')->display('OK!');
  170. } else {
  171. $menu->confirm('Error! Something not working!')->display('OK!');
  172. }
  173. });
  174. // adding checkbox
  175. if ($vhost['enabled']) {
  176. $checkbox->setChecked(true);
  177. }
  178. $builder
  179. ->setTitle('Nginx > '.$vhost['domain'])
  180. ->setGoBackButtonText('Back')
  181. ->addItem('edit', function(CliMenu $menu) use ($vhost) {
  182. system('nano /etc/nginx/sites-available/'.$vhost['file'].' > `tty`');
  183. })
  184. ->addItem('delete', function(CliMenu $menu) use ($vhost) {
  185. if (file_exists('/etc/nginx/sites-enabled/'.$vhost['file'])) {
  186. $menu->confirm('Error! Please disable '.$vhost['domain'].' first!')->display('OK!');
  187. } else {
  188. unlink('/etc/nginx/sites-available/'.$vhost['file']);
  189. $menu->confirm($vhost['domain'].' is deleted!')->display('OK!');
  190. }
  191. })
  192. ->addLineBreak('-')
  193. ->addMenuItem($checkbox)
  194. ->addLineBreak('-');
  195. };
  196. return $menu;
  197. }
  198. /**
  199. * let it rain
  200. *
  201. *
  202. */
  203. public function handle()
  204. {
  205. $submenuSelectTemplate = $this->selectTemplate();
  206. // create menu
  207. $main = $this->menu('Nginx')
  208. // add Submenu for select templates
  209. ->addSubMenu('add', $submenuSelectTemplate)
  210. ->addLineBreak('-');
  211. // getting vhosts
  212. $vhosts = NginxVhost::find();
  213. // add submenu for each vhost
  214. foreach($vhosts as $vhost) {
  215. $submenuEditVhost = $this->vhost($vhost);
  216. $main->addSubMenu($vhost['domain'], $submenuEditVhost);
  217. }
  218. $main->addLineBreak('-');
  219. $main->open();
  220. }
  221. }