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.

309 lines
8.9 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
  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. $self = $this;
  101. $menu = function(CliMenuBuilder $builder) use ($template, $blade, $self)
  102. {
  103. $configuration = [
  104. 'domain' => '',
  105. 'root' => '',
  106. 'index' => 'index.php',
  107. 'ssl' => true,
  108. 'redirect_www' => true
  109. ];
  110. // create checkbox for ssl
  111. $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) {
  112. $configuration['ssl'] = $menu->getSelectedItem()->getChecked();
  113. });
  114. $checkboxSSL->setChecked($configuration['ssl']);
  115. // create checkbox for redirect from www
  116. $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) use (&$configuration) {
  117. $configuration['redirect_www'] = $menu->getSelectedItem()->getChecked();
  118. });
  119. $checkboxRedirect->setChecked($configuration['redirect_www']);
  120. $builder
  121. ->setTitle('Nginx Vhosts > add > '.$template['name'])
  122. ->addItem('domain', function(CliMenu $menu) use (&$configuration) {
  123. $result = $menu->askText()->ask();
  124. $configuration['domain'] = $result->fetch();
  125. $menu->getSelectedItem()->setText('domain -> '.$result->fetch());
  126. $menu->redraw();
  127. })
  128. ->addItem('root', function(CliMenu $menu) use (&$configuration) {
  129. $result = $menu->askText()->ask();
  130. $configuration['root'] = $result->fetch();
  131. $menu->getSelectedItem()->setText('root -> '.$result->fetch());
  132. $menu->redraw();
  133. })
  134. ->addItem($configuration['index'], function(CliMenu $menu) use (&$configuration) {
  135. $result = $menu->askText()->ask();
  136. $configuration['index'] = $result->fetch();
  137. $menu->getSelectedItem()->setText($result->fetch());
  138. $menu->redraw();
  139. })
  140. ->addLineBreak('-')
  141. ->addMenuItem($checkboxSSL)
  142. ->addMenuItem($checkboxRedirect)
  143. ->addLineBreak('-')
  144. ->addItem('save', function(CliMenu $menu) use (&$configuration, $template, $blade, $self) {
  145. $content = $blade->render($template['name'], $configuration);
  146. file_put_contents('/etc/nginx/sites-available/'.$configuration['domain'].'.conf', $content);
  147. $parent = $menu->getParent();
  148. $menu->closeThis();
  149. $mainmenu = $parent->getParent();
  150. $mainmenu->open();
  151. })
  152. ->addLineBreak('-');
  153. };
  154. return $menu;
  155. }
  156. /**
  157. *
  158. *
  159. *
  160. */
  161. /**
  162. * create submenu for vhost
  163. *
  164. *
  165. * @param array $vhost
  166. * @return CliMenuBuilder
  167. *
  168. */
  169. private function vhost($vhost)
  170. {
  171. $currentMenu = function(CliMenuBuilder $builder) use ($vhost)
  172. {
  173. $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($vhost) {
  174. // check status
  175. if ($menu->getSelectedItem()->getChecked()) {
  176. symlink('/etc/nginx/sites-available/'.$vhost['file'], '/etc/nginx/sites-enabled/'.$vhost['file']);
  177. $status = 'enabled';
  178. } else {
  179. unlink('/etc/nginx/sites-enabled/'.$vhost['file']);
  180. $status = 'disabled';
  181. }
  182. // restart
  183. exec('service nginx restart');
  184. exec('service nginx status', $output);
  185. if (strpos(implode(' ', $output), 'active') !== false) {
  186. $menu->confirm($vhost['domain'].' is '.$status.'!')->display('OK!');
  187. } else {
  188. $menu->confirm('Error! Something not working!')->display('OK!');
  189. }
  190. });
  191. // adding checkbox
  192. if ($vhost['enabled']) {
  193. $checkbox->setChecked(true);
  194. }
  195. $builder
  196. ->setTitle('Nginx > '.$vhost['domain'])
  197. ->setGoBackButtonText('Back')
  198. ->addItem('edit', function(CliMenu $menu) use ($vhost) {
  199. system('nano /etc/nginx/sites-available/'.$vhost['file'].' > `tty`');
  200. })
  201. ->addItem('delete', function(CliMenu $menu) use ($vhost) {
  202. if (file_exists('/etc/nginx/sites-enabled/'.$vhost['file'])) {
  203. $menu->confirm('Error! Please disable '.$vhost['domain'].' first!')->display('OK!');
  204. } else {
  205. unlink('/etc/nginx/sites-available/'.$vhost['file']);
  206. $menu->confirm($vhost['domain'].' is deleted!')->display('OK!');
  207. // get
  208. $parent = $menu->getParent();
  209. $menu->closeThis();
  210. // remove current vhost from mainmenu
  211. $parent->removeItem($parent->getSelectedItem());
  212. $parent->open();
  213. }
  214. })
  215. ->addLineBreak('-')
  216. ->addMenuItem($checkbox)
  217. ->addLineBreak('-');
  218. };
  219. return $currentMenu;
  220. }
  221. /**
  222. *
  223. *
  224. */
  225. protected function updateVhosts($menu)
  226. {
  227. // getting vhosts
  228. $vhosts = NginxVhost::find();
  229. // add submenu for each vhost
  230. foreach($vhosts as $vhost) {
  231. $submenuEditVhost = $this->vhost($vhost);
  232. $menu->addSubMenu($vhost['domain'], $submenuEditVhost);
  233. }
  234. return $menu;
  235. }
  236. /**
  237. * let it rain
  238. *
  239. *
  240. */
  241. public function handle()
  242. {
  243. $submenuSelectTemplate = $this->selectTemplate();
  244. // create menu
  245. $main = $this->menu('Nginx')
  246. // add Submenu for select templates
  247. ->addSubMenu('add', $submenuSelectTemplate)
  248. ->addLineBreak('-');
  249. $main = $this->updateVhosts($main);
  250. $main->addLineBreak('-');
  251. $main->open();
  252. }
  253. }