@@ -34,13 +34,15 @@ discard block |
||
| 34 | 34 | public function __construct(DirectAdmin $connection, $validate = false) |
| 35 | 35 | { |
| 36 | 36 | parent::__construct($connection); |
| 37 | - if ($validate) { |
|
| 37 | + if ($validate) |
|
| 38 | + { |
|
| 38 | 39 | $classMap = [ |
| 39 | 40 | DirectAdmin::ACCOUNT_TYPE_ADMIN => AdminContext::class, |
| 40 | 41 | DirectAdmin::ACCOUNT_TYPE_RESELLER => ResellerContext::class, |
| 41 | 42 | DirectAdmin::ACCOUNT_TYPE_USER => self::class, |
| 42 | 43 | ]; |
| 43 | - if ($classMap[$this->getType()] != get_class($this)) { |
|
| 44 | + if ($classMap[$this->getType()] != get_class($this)) |
|
| 45 | + { |
|
| 44 | 46 | throw new DirectAdminException('Validation mismatch on context construction'); |
| 45 | 47 | } |
| 46 | 48 | } |
@@ -63,7 +65,8 @@ discard block |
||
| 63 | 65 | */ |
| 64 | 66 | public function getContextUser() |
| 65 | 67 | { |
| 66 | - if (!isset($this->user)) { |
|
| 68 | + if (!isset($this->user)) |
|
| 69 | + { |
|
| 67 | 70 | $this->user = User::fromConfig($this->invokeGet('SHOW_USER_CONFIG'), $this); |
| 68 | 71 | } |
| 69 | 72 | return $this->user; |
@@ -43,7 +43,8 @@ discard block |
||
| 43 | 43 | public function __construct($name, UserContext $context, $config = null) |
| 44 | 44 | { |
| 45 | 45 | parent::__construct($name, $context); |
| 46 | - if (isset($config)) { |
|
| 46 | + if (isset($config)) |
|
| 47 | + { |
|
| 47 | 48 | $this->setCache(self::CACHE_CONFIG, $config); |
| 48 | 49 | } |
| 49 | 50 | } |
@@ -163,7 +164,8 @@ discard block |
||
| 163 | 164 | */ |
| 164 | 165 | public function getDefaultDomain() |
| 165 | 166 | { |
| 166 | - if (empty($name = $this->getConfig('domain'))) { |
|
| 167 | + if (empty($name = $this->getConfig('domain'))) |
|
| 168 | + { |
|
| 167 | 169 | return null; |
| 168 | 170 | } |
| 169 | 171 | return $this->getDomain($name); |
@@ -206,9 +208,11 @@ discard block |
||
| 206 | 208 | { |
| 207 | 209 | return $this->getCache(self::CACHE_DATABASES, function () { |
| 208 | 210 | $databases = []; |
| 209 | - foreach ($this->getSelfManagedContext()->invokeGet('DATABASES') as $fullName) { |
|
| 211 | + foreach ($this->getSelfManagedContext()->invokeGet('DATABASES') as $fullName) |
|
| 212 | + { |
|
| 210 | 213 | list($user, $db) = explode('_', $fullName, 2); |
| 211 | - if ($this->getUsername() != $user) { |
|
| 214 | + if ($this->getUsername() != $user) |
|
| 215 | + { |
|
| 212 | 216 | throw new DirectAdminException('Username incorrect on database ' . $fullName); |
| 213 | 217 | } |
| 214 | 218 | $databases[$db] = new Database($db, $this, $this->getSelfManagedContext()); |
@@ -223,7 +227,8 @@ discard block |
||
| 223 | 227 | */ |
| 224 | 228 | public function getDomain($domainName) |
| 225 | 229 | { |
| 226 | - if (!isset($this->domains)) { |
|
| 230 | + if (!isset($this->domains)) |
|
| 231 | + { |
|
| 227 | 232 | $this->getDomains(); |
| 228 | 233 | } |
| 229 | 234 | return isset($this->domains[$domainName]) ? $this->domains[$domainName] : null; |
@@ -234,10 +239,14 @@ discard block |
||
| 234 | 239 | */ |
| 235 | 240 | public function getDomains() |
| 236 | 241 | { |
| 237 | - if (!isset($this->domains)) { |
|
| 238 | - if (!$this->isSelfManaged()) { |
|
| 242 | + if (!isset($this->domains)) |
|
| 243 | + { |
|
| 244 | + if (!$this->isSelfManaged()) |
|
| 245 | + { |
|
| 239 | 246 | $this->domains = $this->impersonate()->getDomains(); |
| 240 | - } else { |
|
| 247 | + } |
|
| 248 | + else |
|
| 249 | + { |
|
| 241 | 250 | $this->domains = Object::toRichObjectArray($this->getContext()->invokeGet('ADDITIONAL_DOMAINS'), Domain::class, $this->getContext()); |
| 242 | 251 | } |
| 243 | 252 | } |
@@ -282,7 +291,8 @@ discard block |
||
| 282 | 291 | public function impersonate() |
| 283 | 292 | { |
| 284 | 293 | /** @var ResellerContext $context */ |
| 285 | - if (!($context = $this->getContext()) instanceof ResellerContext) { |
|
| 294 | + if (!($context = $this->getContext()) instanceof ResellerContext) |
|
| 295 | + { |
|
| 286 | 296 | throw new DirectAdminException('You need to be at least a reseller to impersonate'); |
| 287 | 297 | } |
| 288 | 298 | return $context->impersonateUser($this->getUsername()); |
@@ -348,7 +358,8 @@ discard block |
||
| 348 | 358 | public static function fromConfig($config, UserContext $context) |
| 349 | 359 | { |
| 350 | 360 | $name = $config['username']; |
| 351 | - switch ($config['usertype']) { |
|
| 361 | + switch ($config['usertype']) |
|
| 362 | + { |
|
| 352 | 363 | case DirectAdmin::ACCOUNT_TYPE_USER: |
| 353 | 364 | return new self($name, $context, $config); |
| 354 | 365 | case DirectAdmin::ACCOUNT_TYPE_RESELLER: |
@@ -35,7 +35,8 @@ |
||
| 35 | 35 | public function impersonate() |
| 36 | 36 | { |
| 37 | 37 | /** @var AdminContext $context */ |
| 38 | - if (!($context = $this->getContext()) instanceof AdminContext) { |
|
| 38 | + if (!($context = $this->getContext()) instanceof AdminContext) |
|
| 39 | + { |
|
| 39 | 40 | throw new DirectAdminException('You need to be an admin to impersonate another admin'); |
| 40 | 41 | } |
| 41 | 42 | return $context->impersonateAdmin($this->getUsername()); |
@@ -31,7 +31,8 @@ |
||
| 31 | 31 | public function __construct($prefix, Domain $domain, $config = null) |
| 32 | 32 | { |
| 33 | 33 | parent::__construct($prefix, $domain); |
| 34 | - if (isset($config)) { |
|
| 34 | + if (isset($config)) |
|
| 35 | + { |
|
| 35 | 36 | $this->setCache(self::CACHE_DATA, is_string($config) ? \GuzzleHttp\Psr7\parse_query($config) : $config); |
| 36 | 37 | } |
| 37 | 38 | } |
@@ -57,9 +57,12 @@ |
||
| 57 | 57 | 'action' => 'create', |
| 58 | 58 | 'name' => $name, |
| 59 | 59 | ]; |
| 60 | - if (!empty($password)) { |
|
| 60 | + if (!empty($password)) |
|
| 61 | + { |
|
| 61 | 62 | $options += ['user' => $username, 'passwd' => $password, 'passwd2' => $password]; |
| 62 | - } else { |
|
| 63 | + } |
|
| 64 | + else |
|
| 65 | + { |
|
| 63 | 66 | $options += ['userlist' => $username]; |
| 64 | 67 | } |
| 65 | 68 | $user->getContext()->invokePost('DATABASES', $options); |
@@ -39,7 +39,8 @@ discard block |
||
| 39 | 39 | { |
| 40 | 40 | $ukey = "u{$key}"; |
| 41 | 41 | unset($options[$ukey]); |
| 42 | - if (array_key_exists($key, $options) && ($options[$key] === 'unlimited' || !isset($options[$key]))) { |
|
| 42 | + if (array_key_exists($key, $options) && ($options[$key] === 'unlimited' || !isset($options[$key]))) |
|
| 43 | + { |
|
| 43 | 44 | $options[$ukey] = 'ON'; |
| 44 | 45 | } |
| 45 | 46 | } |
@@ -53,7 +54,8 @@ discard block |
||
| 53 | 54 | public static function processUnlimitedOptions(array $options) |
| 54 | 55 | { |
| 55 | 56 | foreach (['bandwidth', 'domainptr', 'ftp', 'mysql', 'nemailf', 'nemailml', 'nemailr', 'nemails', |
| 56 | - 'nsubdomains', 'quota', 'vdomains', ] as $key) { |
|
| 57 | + 'nsubdomains', 'quota', 'vdomains', ] as $key) |
|
| 58 | + { |
|
| 57 | 59 | self::processUnlimitedOption($options, $key); |
| 58 | 60 | } |
| 59 | 61 | return $options; |
@@ -81,7 +83,8 @@ discard block |
||
| 81 | 83 | */ |
| 82 | 84 | public static function sanitizeArray($result) |
| 83 | 85 | { |
| 84 | - if (count($result) == 1 && isset($result['list[]'])) { |
|
| 86 | + if (count($result) == 1 && isset($result['list[]'])) |
|
| 87 | + { |
|
| 85 | 88 | $result = $result['list[]']; |
| 86 | 89 | } |
| 87 | 90 | return is_array($result) ? $result : [$result]; |
@@ -127,7 +127,8 @@ discard block |
||
| 127 | 127 | public function invoke($method, $command, $options = []) |
| 128 | 128 | { |
| 129 | 129 | $result = $this->rawRequest($method, '/CMD_API_' . $command, $options); |
| 130 | - if (!empty($result['error'])) { |
|
| 130 | + if (!empty($result['error'])) |
|
| 131 | + { |
|
| 131 | 132 | throw new DirectAdminException("$method to $command failed: $result[details] ($result[text])"); |
| 132 | 133 | } |
| 133 | 134 | return Conversion::sanitizeArray($result); |
@@ -155,14 +156,18 @@ discard block |
||
| 155 | 156 | */ |
| 156 | 157 | private function rawRequest($method, $uri, $options) |
| 157 | 158 | { |
| 158 | - try { |
|
| 159 | + try |
|
| 160 | + { |
|
| 159 | 161 | $response = $this->connection->request($method, $uri, $options); |
| 160 | - if ($response->getHeader('Content-Type')[0] == 'text/html') { |
|
| 162 | + if ($response->getHeader('Content-Type')[0] == 'text/html') |
|
| 163 | + { |
|
| 161 | 164 | throw new DirectAdminException("DirectAdmin API returned a text/html body. Requested {$uri} via {$method}. Responded: " . strip_tags($response->getBody()->getContents())); |
| 162 | 165 | } |
| 163 | 166 | $body = $response->getBody()->getContents(); |
| 164 | 167 | return Conversion::responseToArray($body); |
| 165 | - } catch (TransferException $exception) { |
|
| 168 | + } |
|
| 169 | + catch (TransferException $exception) |
|
| 170 | + { |
|
| 166 | 171 | // Rethrow anything that causes a network issue |
| 167 | 172 | throw new DirectAdminException("Request to {$uri} using {$method} failed", 0, $exception); |
| 168 | 173 | } |
@@ -56,7 +56,8 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | protected function getCache($key, $default) |
| 58 | 58 | { |
| 59 | - if (!isset($this->cache[$key])) { |
|
| 59 | + if (!isset($this->cache[$key])) |
|
| 60 | + { |
|
| 60 | 61 | $this->cache[$key] = is_callable($default) ? $default() : $default; |
| 61 | 62 | } |
| 62 | 63 | return $this->cache[$key]; |
@@ -73,10 +74,12 @@ discard block |
||
| 73 | 74 | */ |
| 74 | 75 | protected function getCacheItem($key, $item, $defaultKey, $defaultItem = null) |
| 75 | 76 | { |
| 76 | - if (empty($cache = $this->getCache($key, $defaultKey))) { |
|
| 77 | + if (empty($cache = $this->getCache($key, $defaultKey))) |
|
| 78 | + { |
|
| 77 | 79 | return $defaultItem; |
| 78 | 80 | } |
| 79 | - if (!is_array($cache)) { |
|
| 81 | + if (!is_array($cache)) |
|
| 82 | + { |
|
| 80 | 83 | throw new DirectAdminException("Cache item $key is not an array"); |
| 81 | 84 | } |
| 82 | 85 | return isset($cache[$item]) ? $cache[$item] : $defaultItem; |
@@ -35,7 +35,8 @@ |
||
| 35 | 35 | public function impersonate() |
| 36 | 36 | { |
| 37 | 37 | /** @var AdminContext $context */ |
| 38 | - if (!($context = $this->getContext()) instanceof AdminContext) { |
|
| 38 | + if (!($context = $this->getContext()) instanceof AdminContext) |
|
| 39 | + { |
|
| 39 | 40 | throw new DirectAdminException('You need to be an admin to impersonate another admin'); |
| 40 | 41 | } |
| 41 | 42 | return $context->impersonateAdmin($this->getUsername()); |