1 <?php
2
3 4 5 6 7 8 9
10
11 namespace Omines\DirectAdmin\Objects;
12
13 use Omines\DirectAdmin\Context\UserContext;
14 use Omines\DirectAdmin\DirectAdminException;
15 use Omines\DirectAdmin\Objects\Domains\Subdomain;
16 use Omines\DirectAdmin\Objects\Email\Forwarder;
17 use Omines\DirectAdmin\Objects\Email\Mailbox;
18 use Omines\DirectAdmin\Objects\Users\User;
19 use Omines\DirectAdmin\Utility\Conversion;
20
21 22 23 24 25
26 class Domain extends BaseObject
27 {
28 const CACHE_FORWARDERS = 'forwarders';
29 const CACHE_MAILBOXES = 'mailboxes';
30 const CACHE_SUBDOMAINS = 'subdomains';
31
32 const CATCHALL_BLACKHOLE = ':blackhole:';
33 const CATCHALL_FAIL = ':fail:';
34
35
36 private $domainName;
37
38
39 private $owner;
40
41
42 private $aliases;
43
44
45 private $pointers;
46
47
48 private $bandwidthUsed;
49
50
51 private $bandwidthLimit;
52
53
54 private $diskUsage;
55
56 57 58 59 60 61 62
63 public function __construct($name, UserContext $context, $config)
64 {
65 parent::__construct($name, $context);
66 $this->setConfig($context, is_array($config) ? $config : \GuzzleHttp\Psr7\parse_query($config));
67 }
68
69 70 71 72 73 74 75 76 77 78 79 80
81 public static function create(User $user, $domainName, $bandwidthLimit = null, $diskLimit = null, $ssl = null, $php = null, $cgi = null)
82 {
83 $options = [
84 'action' => 'create',
85 'domain' => $domainName,
86 (isset($bandwidthLimit) ? 'bandwidth' : 'ubandwidth') => $bandwidthLimit,
87 (isset($diskLimit) ? 'quota' : 'uquota') => $diskLimit,
88 'ssl' => Conversion::onOff($ssl, $user->hasSSL()),
89 'php' => Conversion::onOff($php, $user->hasPHP()),
90 'cgi' => Conversion::onOff($cgi, $user->hasCGI()),
91 ];
92 $user->getContext()->invokeApiPost('DOMAIN', $options);
93 $config = $user->getContext()->invokeApiGet('ADDITIONAL_DOMAINS');
94 return new self($domainName, $user->getContext(), $config[$domainName]);
95 }
96
97 98 99 100 101 102 103
104 public function createForwarder($prefix, $recipients)
105 {
106 return Forwarder::create($this, $prefix, $recipients);
107 }
108
109 110 111 112 113 114 115 116 117
118 public function createMailbox($prefix, $password, $quota = null, $sendLimit = null)
119 {
120 return Mailbox::create($this, $prefix, $password, $quota, $sendLimit);
121 }
122
123 124 125 126 127 128
129 public function createPointer($domain, $alias = false)
130 {
131 $parameters = [
132 'domain' => $this->domainName,
133 'from' => $domain,
134 'action' => 'add',
135 ];
136 if ($alias) {
137 $parameters['alias'] = 'yes';
138 $list = &$this->aliases;
139 } else {
140 $list = &$this->pointers;
141 }
142 $this->getContext()->invokeApiPost('DOMAIN_POINTER', $parameters);
143 $list[] = $domain;
144 $list = array_unique($list);
145 }
146
147 148 149 150 151 152
153 public function createSubdomain($prefix)
154 {
155 return Subdomain::create($this, $prefix);
156 }
157
158 159 160
161 public function delete()
162 {
163 $this->getContext()->invokeApiPost('DOMAIN', [
164 'delete' => true,
165 'confirmed' => true,
166 'select0' => $this->domainName,
167 ]);
168 $this->owner->clearCache();
169 }
170
171 172 173
174 public function getAliases()
175 {
176 return $this->aliases;
177 }
178
179 180 181
182 public function getBandwidthUsed()
183 {
184 return $this->bandwidthUsed;
185 }
186
187 188 189
190 public function getBandwidthLimit()
191 {
192 return $this->bandwidthLimit;
193 }
194
195 196 197
198 public function getCatchall()
199 {
200 $value = $this->getContext()->invokeApiGet('EMAIL_CATCH_ALL', ['domain' => $this->domainName]);
201 return isset($value['value']) ? $value['value'] : null;
202 }
203
204 205 206
207 public function getDiskUsage()
208 {
209 return $this->diskUsage;
210 }
211
212 213 214
215 public function getDomainName()
216 {
217 return $this->domainName;
218 }
219
220 221 222 223 224
225 public function getDomainNames()
226 {
227 return $this->getCache('domainNames', function () {
228 $list = array_merge($this->aliases, $this->pointers, [$this->getDomainName()]);
229 sort($list);
230 return $list;
231 });
232 }
233
234 235 236
237 public function getForwarders()
238 {
239 return $this->getCache(self::CACHE_FORWARDERS, function () {
240 $forwarders = $this->getContext()->invokeApiGet('EMAIL_FORWARDERS', [
241 'domain' => $this->getDomainName(),
242 ]);
243 return DomainObject::toDomainObjectArray($forwarders, Forwarder::class, $this);
244 });
245 }
246
247 248 249
250 public function getMailboxes()
251 {
252 return $this->getCache(self::CACHE_MAILBOXES, function () {
253 $boxes = $this->getContext()->invokeApiGet('POP', [
254 'domain' => $this->getDomainName(),
255 'action' => 'full_list',
256 ]);
257 return DomainObject::toDomainObjectArray($boxes, Mailbox::class, $this);
258 });
259 }
260
261 262 263
264 public function getOwner()
265 {
266 return $this->owner;
267 }
268
269 270 271
272 public function getPointers()
273 {
274 return $this->pointers;
275 }
276
277 278 279
280 public function getSubdomains()
281 {
282 return $this->getCache(self::CACHE_SUBDOMAINS, function () {
283 $subs = $this->getContext()->invokeApiGet('SUBDOMAINS', ['domain' => $this->getDomainName()]);
284 $subs = array_combine($subs, $subs);
285 return DomainObject::toDomainObjectArray($subs, Subdomain::class, $this);
286 });
287 }
288
289 290 291 292 293 294 295 296 297
298 public function invokePost($command, $action, $parameters = [], $clearCache = true)
299 {
300 $response = $this->getContext()->invokeApiPost($command, array_merge([
301 'action' => $action,
302 'domain' => $this->domainName,
303 ], $parameters));
304 if ($clearCache) {
305 $this->clearCache();
306 }
307 return $response;
308 }
309
310 311 312
313 public function setCatchall($newValue)
314 {
315 $parameters = array_merge(['domain' => $this->domainName, 'update' => 'Update'],
316 (empty($newValue) || $newValue[0] == ':') ? ['catch' => $newValue] : ['catch' => 'address', 'value' => $newValue]);
317 $this->getContext()->invokeApiPost('EMAIL_CATCH_ALL', $parameters);
318 }
319
320 321 322 323 324
325 public function __toString()
326 {
327 return $this->getDomainName();
328 }
329
330 331 332 333 334 335
336 private function setConfig(UserContext $context, array $config)
337 {
338 $this->domainName = $config['domain'];
339
340
341 if ($config['username'] === $context->getUsername()) {
342 $this->owner = $context->getContextUser();
343 } else {
344 throw new DirectAdminException('Could not determine relationship between context user and domain');
345 }
346
347
348 $bandwidths = array_map('trim', explode('/', $config['bandwidth']));
349 $this->bandwidthUsed = floatval($bandwidths[0]);
350 $this->bandwidthLimit = ctype_alpha($bandwidths[1]) ? null : floatval($bandwidths[1]);
351 $this->diskUsage = floatval($config['quota']);
352
353 $this->aliases = array_filter(explode('|', $config['alias_pointers']));
354 $this->pointers = array_filter(explode('|', $config['pointers']));
355 }
356 }
357