|
| 1 | +<?php |
| 2 | + |
| 3 | +abstract class BaseSetup { |
| 4 | + public function getConfig($section=null) { |
| 5 | + return (!empty($section))? $this->config[$section] : $this->config; |
| 6 | + } |
| 7 | + |
| 8 | + public function getOptions() { |
| 9 | + return $this->getConfig('form'); |
| 10 | + } |
| 11 | + |
| 12 | + public function withDatabase() { |
| 13 | + return ($this->getConfig('database') === true); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +class WordpressSetup extends BaseSetup { |
| 19 | + protected $config = [ |
| 20 | + 'form' => [ |
| 21 | + 'protocol' => [ |
| 22 | + 'type' => 'select', |
| 23 | + 'options' => ['http','ion','https'], |
| 24 | + 'value' => 'ion', |
| 25 | + ], |
| 26 | + 'subdir' => ['type'=>'text', 'value'=>'/'], |
| 27 | + 'site_name' => ['type'=>'text', 'value'=>'Wordpress Blog'], |
| 28 | + 'site_description' => ['value'=>'Another wordpresss site'], |
| 29 | + 'wordpress_account_username' => ['value'=>'wpadmin'], |
| 30 | + 'wordpress_account_password' => 'password', |
| 31 | + ], |
| 32 | + 'database' => true, |
| 33 | + 'url' => 'https://wordpress.org/wordpress-5.2.2.tar.gz' |
| 34 | + ]; |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +class HestiaApp { |
| 39 | + |
| 40 | + public function run($cmd, $args, &$return_code=null) { |
| 41 | + |
| 42 | + $cli_script = HESTIA_CMD . '/' . basename($cmd); |
| 43 | + $cli_arguments = ''; |
| 44 | + |
| 45 | + if (!empty($args) && is_array($args)) { |
| 46 | + foreach ($args as $arg) { |
| 47 | + $cli_arguments .= escapeshellarg($arg) . ' '; |
| 48 | + } |
| 49 | + } else { |
| 50 | + $cli_arguments = escapeshellarg($args); |
| 51 | + } |
| 52 | + |
| 53 | + exec ($cli_script . ' ' . $cli_arguments, $output, $return_code); |
| 54 | + |
| 55 | + $result['code'] = $return_code; |
| 56 | + $result['raw'] = $output; |
| 57 | + $result['text'] = implode( PHP_EOL, $result['raw']); |
| 58 | + $result['json'] = json_decode($result['text'], true); |
| 59 | + |
| 60 | + return (object)$result; |
| 61 | + } |
| 62 | + |
| 63 | + public function realuser() { |
| 64 | + // Logged in user |
| 65 | + return $_SESSION['user']; |
| 66 | + } |
| 67 | + |
| 68 | + public function user() { |
| 69 | + // Effective user |
| 70 | + if ($this->realuser() == 'admin' && !empty($_SESSION['look'])) { |
| 71 | + return $_SESSION['look']; |
| 72 | + } |
| 73 | + return $this->realuser(); |
| 74 | + } |
| 75 | + |
| 76 | + public function userOwnsDomain($domain) { |
| 77 | + $status = null; |
| 78 | + $this->run('v-list-web-domain', [$this->user(), $domain, 'json'], $status); |
| 79 | + return ($status === 0); |
| 80 | + } |
| 81 | + |
| 82 | + public function databaseAdd($dbname,$dbuser,$dbpass) { |
| 83 | + $v_password = tempnam("/tmp","vst"); |
| 84 | + $fp = fopen($v_password, "w"); |
| 85 | + fwrite($fp, $dbpass."\n"); |
| 86 | + fclose($fp); |
| 87 | + $this->run('v-add-database', [$this->user(), $dbname, $dbuser, $v_password], $status); |
| 88 | + unlink($v_password); |
| 89 | + return ($status === 0); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +class AppInstaller { |
| 95 | + |
| 96 | + private $domain; |
| 97 | + private $appsetup; |
| 98 | + private $appcontext; |
| 99 | + private $formNamespace = 'webapp'; |
| 100 | + private $errors; |
| 101 | + |
| 102 | + private $database_config = [ |
| 103 | + 'database_name' => 'text', |
| 104 | + 'database_user' => 'text', |
| 105 | + 'database_password' => 'password', |
| 106 | + ]; |
| 107 | + |
| 108 | + public function __construct($app, $domain, $context) { |
| 109 | + $this->domain = $domain; |
| 110 | + $this->appcontext = $context; |
| 111 | + |
| 112 | + if (!$this->appcontext->userOwnsDomain($domain)) { |
| 113 | + throw new Exception("User does not have access to domain [$domain]"); |
| 114 | + } |
| 115 | + |
| 116 | + $appclass = ucfirst($app).'Setup'; |
| 117 | + if (class_exists($appclass)) { |
| 118 | + $this->appsetup = new $appclass(); |
| 119 | + } |
| 120 | + |
| 121 | + if (!$this->appsetup) { |
| 122 | + throw new Exception( "Application [".ucfirst($app)."] does not have a installer" ); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public function getStatus() { |
| 127 | + return $this->errors; |
| 128 | + } |
| 129 | + |
| 130 | + public function formNs() { |
| 131 | + return $this->formNamespace; |
| 132 | + } |
| 133 | + |
| 134 | + public function getOptions() { |
| 135 | + if(!$this->appsetup) return; |
| 136 | + |
| 137 | + $options = $this->appsetup->getOptions(); |
| 138 | + if ($this->appsetup->withDatabase()) { |
| 139 | + $options = array_merge($options, $this->database_config); |
| 140 | + } |
| 141 | + return $options; |
| 142 | + } |
| 143 | + |
| 144 | + public function filterOptions($options) |
| 145 | + { |
| 146 | + $filteredoptions = []; |
| 147 | + array_walk($options, function($value, $key) use(&$filteredoptions) { |
| 148 | + if (strpos($key, $this->formNs().'_')===0) { |
| 149 | + $option = str_replace($this->formNs().'_','',$key); |
| 150 | + $filteredoptions[$option] = $value; |
| 151 | + } |
| 152 | + }); |
| 153 | + return $filteredoptions; |
| 154 | + } |
| 155 | + |
| 156 | + public function execute($options) { |
| 157 | + if (!$this->appsetup) return; |
| 158 | + |
| 159 | + $options = $this->filterOptions($options); |
| 160 | + |
| 161 | + $random_num = random_int(10000, 99999); |
| 162 | + if ($this->appsetup->withDatabase()) { |
| 163 | + |
| 164 | + if(empty($options['database_name'])) { |
| 165 | + $options['database_name'] = $random_num; |
| 166 | + } |
| 167 | + |
| 168 | + if(empty($options['database_user'])) { |
| 169 | + $options['database_user'] = $random_num; |
| 170 | + } |
| 171 | + |
| 172 | + if(empty($options['database_password'])) { |
| 173 | + $options['database_password'] = bin2hex(random_bytes(10)); |
| 174 | + } |
| 175 | + |
| 176 | + if(!$this->appcontext->databaseAdd($options['database_name'], $options['database_user'], $options['database_password'])){ |
| 177 | + $this->errors[] = "Error adding database"; |
| 178 | + return false; |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments