1 <?php
2
3 4 5 6 7 8 9
10
11 namespace Omines\DirectAdmin\Objects\Users;
12
13 use Omines\DirectAdmin\Context\ResellerContext;
14 use Omines\DirectAdmin\Context\UserContext;
15 use Omines\DirectAdmin\DirectAdmin;
16 use Omines\DirectAdmin\DirectAdminException;
17 use Omines\DirectAdmin\Objects\Database;
18 use Omines\DirectAdmin\Objects\Domain;
19 use Omines\DirectAdmin\Objects\BaseObject;
20 use Omines\DirectAdmin\Utility\Conversion;
21
22 23 24 25 26
27 class User extends BaseObject
28 {
29 const CACHE_CONFIG = 'config';
30 const CACHE_DATABASES = 'databases';
31 const CACHE_USAGE = 'usage';
32
33
34 private $domains;
35
36 37 38 39 40 41 42
43 public function __construct($name, UserContext $context, $config = null)
44 {
45 parent::__construct($name, $context);
46 if (isset($config)) {
47 $this->setCache(self::CACHE_CONFIG, $config);
48 }
49 }
50
51 52 53
54 public function clearCache()
55 {
56 unset($this->domains);
57 parent::clearCache();
58 }
59
60 61 62 63 64 65 66 67
68 public function createDatabase($name, $username, $password = null)
69 {
70 $db = Database::create($this->getSelfManagedUser(), $name, $username, $password);
71 $this->clearCache();
72 return $db;
73 }
74
75 76 77 78 79 80 81 82 83 84 85
86 public function createDomain($domainName, $bandwidthLimit = null, $diskLimit = null, $ssl = null, $php = null, $cgi = null)
87 {
88 $domain = Domain::create($this->getSelfManagedUser(), $domainName, $bandwidthLimit, $diskLimit, $ssl, $php, $cgi);
89 $this->clearCache();
90 return $domain;
91 }
92
93 94 95
96 public function getUsername()
97 {
98 return $this->getName();
99 }
100
101 102 103 104 105
106 public function getBandwidthLimit()
107 {
108 return floatval($this->getConfig('bandwidth')) ?: null;
109 }
110
111 112 113 114 115
116 public function getBandwidthUsage()
117 {
118 return floatval($this->getUsage('bandwidth'));
119 }
120
121 122 123 124 125
126 public function getDatabaseLimit()
127 {
128 return intval($this->getConfig('mysql')) ?: null;
129 }
130
131 132 133 134 135
136 public function getDatabaseUsage()
137 {
138 return intval($this->getUsage('mysql'));
139 }
140
141 142 143 144 145
146 public function getDiskLimit()
147 {
148 return floatval($this->getConfig('quota')) ?: null;
149 }
150
151 152 153 154 155
156 public function getDiskUsage()
157 {
158 return floatval($this->getUsage('quota'));
159 }
160
161 162 163
164 public function getDefaultDomain()
165 {
166 if (empty($name = $this->getConfig('domain'))) {
167 return null;
168 }
169 return $this->getDomain($name);
170 }
171
172 173 174 175 176
177 public function getDomainLimit()
178 {
179 return intval($this->getConfig('vdomains')) ?: null;
180 }
181
182 183 184 185 186
187 public function getDomainUsage()
188 {
189 return intval($this->getUsage('vdomains'));
190 }
191
192 193 194 195 196
197 public function isSuspended()
198 {
199 return $this->getConfig('suspended') === 'ON';
200 }
201
202 203 204
205 public function getDatabases()
206 {
207 return $this->getCache(self::CACHE_DATABASES, function () {
208 $databases = [];
209 foreach ($this->getSelfManagedContext()->invokeApiGet('DATABASES') as $fullName) {
210 list($user, $db) = explode('_', $fullName, 2);
211 if ($this->getUsername() != $user) {
212 throw new DirectAdminException('Username incorrect on database ' . $fullName);
213 }
214 $databases[$db] = new Database($db, $this, $this->getSelfManagedContext());
215 }
216 return $databases;
217 });
218 }
219
220 221 222 223
224 public function getDomain($domainName)
225 {
226 if (!isset($this->domains)) {
227 $this->getDomains();
228 }
229 return isset($this->domains[$domainName]) ? $this->domains[$domainName] : null;
230 }
231
232 233 234
235 public function getDomains()
236 {
237 if (!isset($this->domains)) {
238 if (!$this->isSelfManaged()) {
239 $this->domains = $this->impersonate()->getDomains();
240 } else {
241 $this->domains = BaseObject::toRichObjectArray($this->getContext()->invokeApiGet('ADDITIONAL_DOMAINS'), Domain::class, $this->getContext());
242 }
243 }
244 return $this->domains;
245 }
246
247 248 249
250 public function getType()
251 {
252 return $this->getConfig('usertype');
253 }
254
255 256 257
258 public function hasCGI()
259 {
260 return Conversion::toBool($this->getConfig('cgi'));
261 }
262
263 264 265
266 public function hasPHP()
267 {
268 return Conversion::toBool($this->getConfig('php'));
269 }
270
271 272 273
274 public function hasSSL()
275 {
276 return Conversion::toBool($this->getConfig('ssl'));
277 }
278
279 280 281
282 public function impersonate()
283 {
284
285 if (!($context = $this->getContext()) instanceof ResellerContext) {
286 throw new DirectAdminException('You need to be at least a reseller to impersonate');
287 }
288 return $context->impersonateUser($this->getUsername());
289 }
290
291 292 293 294 295 296 297
298 public function modifyConfig(array $newConfig)
299 {
300 $this->getContext()->invokeApiPost('MODIFY_USER', array_merge(
301 $this->loadConfig(),
302 Conversion::processUnlimitedOptions($newConfig),
303 ['action' => 'customize', 'user' => $this->getUsername()]
304 ));
305 $this->clearCache();
306 }
307
308 309 310
311 public function setAllowCatchall($newValue)
312 {
313 $this->modifyConfig(['catchall' => Conversion::onOff($newValue)]);
314 }
315
316 317 318
319 public function setBandwidthLimit($newValue)
320 {
321 $this->modifyConfig(['bandwidth' => isset($newValue) ? floatval($newValue) : null]);
322 }
323
324 325 326
327 public function setDiskLimit($newValue)
328 {
329 $this->modifyConfig(['quota' => isset($newValue) ? floatval($newValue) : null]);
330 }
331
332 333 334
335 public function setDomainLimit($newValue)
336 {
337 $this->modifyConfig(['vdomains' => isset($newValue) ? intval($newValue) : null]);
338 }
339
340 341 342 343 344 345 346 347
348 public static function fromConfig($config, UserContext $context)
349 {
350 $name = $config['username'];
351 switch ($config['usertype']) {
352 case DirectAdmin::ACCOUNT_TYPE_USER:
353 return new self($name, $context, $config);
354 case DirectAdmin::ACCOUNT_TYPE_RESELLER:
355 return new Reseller($name, $context, $config);
356 case DirectAdmin::ACCOUNT_TYPE_ADMIN:
357 return new Admin($name, $context, $config);
358 default:
359 throw new DirectAdminException("Unknown user type '$config[usertype]'");
360 }
361 }
362
363 364 365 366 367 368
369 private function getConfig($item)
370 {
371 return $this->getCacheItem(self::CACHE_CONFIG, $item, function () {
372 return $this->loadConfig();
373 });
374 }
375
376 377 378 379 380 381
382 private function getUsage($item)
383 {
384 return $this->getCacheItem(self::CACHE_USAGE, $item, function () {
385 return $this->getContext()->invokeApiGet('SHOW_USER_USAGE', ['user' => $this->getUsername()]);
386 });
387 }
388
389 390 391
392 protected function getSelfManagedContext()
393 {
394 return $this->isSelfManaged() ? $this->getContext() : $this->impersonate();
395 }
396
397 398 399
400 protected function getSelfManagedUser()
401 {
402 return $this->isSelfManaged() ? $this : $this->impersonate()->getContextUser();
403 }
404
405 406 407
408 protected function isSelfManaged()
409 {
410 return $this->getUsername() === $this->getContext()->getUsername();
411 }
412
413 414 415 416 417
418 private function loadConfig()
419 {
420 return $this->getContext()->invokeApiGet('SHOW_USER_CONFIG', ['user' => $this->getUsername()]);
421 }
422 }
423