1 <?php
2
3 /*
4 * DirectAdmin API Client
5 * (c) Omines Internetbureau B.V. - https://omines.nl/
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 namespace Omines\DirectAdmin\Objects\Email;
12
13 use Omines\DirectAdmin\Objects\Domain;
14
15 /**
16 * Encapsulates a full mailbox with POP/IMAP/webmail access.
17 *
18 * @author Niels Keurentjes <niels.keurentjes@omines.com>
19 */
20 class Mailbox extends MailObject
21 {
22 const CACHE_DATA = 'mailbox';
23
24 /**
25 * Construct the object.
26 *
27 * @param string $prefix The part before the @ in the address
28 * @param Domain $domain The containing domain
29 * @param string|array|null $config URL encoded config string as returned by CMD_API_POP
30 */
31 public function __construct($prefix, Domain $domain, $config = null)
32 {
33 parent::__construct($prefix, $domain);
34 if (isset($config)) {
35 $this->setCache(self::CACHE_DATA, is_string($config) ? \GuzzleHttp\Psr7\parse_query($config) : $config);
36 }
37 }
38
39 /**
40 * Creates a new mailbox.
41 *
42 * @param Domain $domain Domain to add the account to
43 * @param string $prefix Prefix for the account
44 * @param string $password Password for the account
45 * @param int|null $quota Quota in megabytes, or zero/null for unlimited
46 * @param int|null $sendLimit Send limit, or 0 for unlimited, or null for system default
47 * @return Mailbox The created mailbox
48 */
49 public static function create(Domain $domain, $prefix, $password, $quota = null, $sendLimit = null)
50 {
51 $domain->invokePost('POP', 'create', [
52 'user' => $prefix,
53 'passwd' => $password,
54 'passwd2' => $password,
55 'quota' => intval($quota) ?: 0,
56 'limit' => isset($sendLimit) ? (intval($sendLimit) ?: 0) : null,
57 ]);
58 return new self($prefix, $domain);
59 }
60
61 /**
62 * Deletes the mailbox.
63 */
64 public function delete()
65 {
66 $this->invokeDelete('POP', 'user');
67 }
68
69 /**
70 * Reset the password for this mailbox.
71 *
72 * @param string $newPassword
73 */
74 public function setPassword($newPassword)
75 {
76 $this->invokePost('POP', 'modify', [
77 'user' => $this->getPrefix(),
78 'passwd' => $newPassword,
79 'passwd2' => $newPassword,
80 ], false);
81 }
82
83 /**
84 * Returns the disk quota in megabytes.
85 *
86 * @return float|null
87 */
88 public function getDiskLimit()
89 {
90 return floatval($this->getData('quota')) ?: null;
91 }
92
93 /**
94 * Returns the disk usage in megabytes.
95 *
96 * @return float
97 */
98 public function getDiskUsage()
99 {
100 return floatval($this->getData('usage'));
101 }
102
103 /**
104 * Return the amount of mails sent in the current period.
105 *
106 * @return int
107 */
108 public function getMailsSent()
109 {
110 return intval($this->getData('sent'));
111 }
112
113 /**
114 * Cache wrapper to keep mailbox stats up to date.
115 *
116 * @param string $key
117 * @return mixed
118 */
119 protected function getData($key)
120 {
121 return $this->getCacheItem(self::CACHE_DATA, $key, function () {
122 $result = $this->getContext()->invokeApiGet('POP', [
123 'domain' => $this->getDomainName(),
124 'action' => 'full_list',
125 ]);
126 return \GuzzleHttp\Psr7\parse_query($result[$this->getPrefix()]);
127 });
128 }
129 }
130