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.

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