AppWizard.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hestia\WebApp;
  4. use Hestia\System\HestiaApp;
  5. class AppWizard {
  6. private $domain;
  7. private $appsetup;
  8. private $appcontext;
  9. private $formNamespace = 'webapp';
  10. private $errors;
  11. private $database_config = [
  12. 'database_create' => ['type'=>'boolean', 'value'=>false],
  13. 'database_name' => ['type'=>'text', 'placeholder' => 'auto'],
  14. 'database_user' => ['type'=>'text', 'placeholder' => 'auto'],
  15. 'database_password' => ['type'=>'password', 'placeholder' => 'auto'],
  16. ];
  17. public function __construct(InstallerInterface $app, string $domain, HestiaApp $context)
  18. {
  19. $this->domain = $domain;
  20. $this->appcontext = $context;
  21. if (!$this->appcontext->userOwnsDomain($domain)) {
  22. throw new \Exception("User does not have access to domain [$domain]");
  23. }
  24. $this->appsetup = $app;
  25. }
  26. public function getStatus()
  27. {
  28. return $this->errors;
  29. }
  30. public function isDomainRootClean()
  31. {
  32. $this->appcontext->runUser('v-run-cli-cmd', [ "ls", $this->appsetup->getDocRoot() ], $status);
  33. if($status->code !== 0) {
  34. throw new \Exception("Cannot list domain files");
  35. }
  36. $files = $status->raw;
  37. if( count($files) > 2) {
  38. return false;
  39. }
  40. foreach($files as $file) {
  41. if ( !in_array($file,['index.html','robots.txt']) ) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. public function formNs()
  48. {
  49. return $this->formNamespace;
  50. }
  51. public function getOptions()
  52. {
  53. $options = $this->appsetup->getOptions();
  54. if ($this->appsetup->withDatabase()) {
  55. $options = array_merge($options, $this->database_config);
  56. }
  57. return $options;
  58. }
  59. public function filterOptions(array $options)
  60. {
  61. $filteredoptions = [];
  62. array_walk($options, function($value, $key) use(&$filteredoptions) {
  63. if (strpos($key, $this->formNs().'_')===0) {
  64. $option = str_replace($this->formNs().'_','',$key);
  65. $filteredoptions[$option] = $value;
  66. }
  67. });
  68. return $filteredoptions;
  69. }
  70. public function execute(array $options)
  71. {
  72. $options = $this->filterOptions($options);
  73. $random_num = (string)random_int(10000, 99999);
  74. if ($this->appsetup->withDatabase() && !empty($options['database_create'])) {
  75. if(empty($options['database_name'])) {
  76. $options['database_name'] = $random_num;
  77. }
  78. if(empty($options['database_user'])) {
  79. $options['database_user'] = $random_num;
  80. }
  81. if(empty($options['database_password'])) {
  82. $options['database_password'] = bin2hex(random_bytes(10));
  83. }
  84. if(!$this->appcontext->databaseAdd($options['database_name'], $options['database_user'], $options['database_password'])) {
  85. $this->errors[] = "Error adding database";
  86. return false;
  87. }
  88. }
  89. if(empty($this->errors)) {
  90. return $this->appsetup->install($options);
  91. }
  92. }
  93. }