AppWizard.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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'=>true],
  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. $config = $this->appsetup -> getConfig();
  55. $options = array_merge($options, array('php_version' => ['type' => 'select', 'value' => $this->appcontext->getCurrentBackendTemplate($this -> domain), 'options' => $this->appcontext->getSupportedPHP($config['server']['php']['supported'])]));
  56. if ($this->appsetup->withDatabase()) {
  57. $options = array_merge($options, $this->database_config);
  58. }
  59. return $options;
  60. }
  61. public function filterOptions(array $options)
  62. {
  63. $filteredoptions = [];
  64. array_walk($options, function($value, $key) use(&$filteredoptions) {
  65. if (strpos($key, $this->formNs().'_')===0) {
  66. $option = str_replace($this->formNs().'_','',$key);
  67. $filteredoptions[$option] = $value;
  68. }
  69. });
  70. return $filteredoptions;
  71. }
  72. public function execute(array $options)
  73. {
  74. $options = $this->filterOptions($options);
  75. $random_num = (string)random_int(10000, 99999);
  76. if ($this->appsetup->withDatabase() && !empty($options['database_create'])) {
  77. if(empty($options['database_name'])) {
  78. $options['database_name'] = $random_num;
  79. }
  80. if(empty($options['database_user'])) {
  81. $options['database_user'] = $random_num;
  82. }
  83. if(empty($options['database_password'])) {
  84. $options['database_password'] = bin2hex(random_bytes(10));
  85. }
  86. if(!$this->appcontext->checkDatabaseLimit()) {
  87. $this->errors[] = _('Unable to add databse! Limit reached!');
  88. return false;
  89. }
  90. if(!$this->appcontext->databaseAdd($options['database_name'], $options['database_user'], $options['database_password'])) {
  91. $this->errors[] = "Error adding database";
  92. return false;
  93. }
  94. }
  95. if(empty($this->errors)) {
  96. return $this->appsetup->install($options);
  97. }
  98. }
  99. }