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\Context;
12
13 use Omines\DirectAdmin\Objects\BaseObject;
14 use Omines\DirectAdmin\Objects\Users\User;
15
16 /**
17 * Context for reseller functions.
18 *
19 * @author Niels Keurentjes <niels.keurentjes@omines.com>
20 */
21 class ResellerContext extends UserContext
22 {
23 /**
24 * Creates a new user on the server.
25 *
26 * @param string $username Login for the new user
27 * @param string $password Password for the new user
28 * @param string $email Email for the new user
29 * @param string $domain Default domain for the new user
30 * @param string $ip IP for the user
31 * @param string|array $package Either a package name or an array of options for custom
32 * @return User Newly created user
33 * @url http://www.directadmin.com/api.html#create for options to use.
34 */
35 public function createUser($username, $password, $email, $domain, $ip, $package = [])
36 {
37 $options = array_merge(
38 ['ip' => $ip, 'domain' => $domain],
39 is_array($package) ? $package : ['package' => $package]
40 );
41 return $this->createAccount($username, $password, $email, $options, 'ACCOUNT_USER', User::class);
42 }
43
44 /**
45 * Internal helper function for creating new accounts.
46 *
47 * @param string $username Login for the new user
48 * @param string $password Password for the new user
49 * @param string $email Email for the new user
50 * @param array $options List of DA account options to apply
51 * @param string $endpoint API endpoint to invoke
52 * @param string $returnType Class name that should wrap the resulting account
53 * @return object An instance of the type specified in $returnType
54 */
55 protected function createAccount($username, $password, $email, $options, $endpoint, $returnType)
56 {
57 $this->invokeApiPost($endpoint, array_merge($options, [
58 'action' => 'create',
59 'add' => 'Submit',
60 'email' => $email,
61 'passwd' => $password,
62 'passwd2' => $password,
63 'username' => $username,
64 ]));
65 return new $returnType($username, $this);
66 }
67
68 /**
69 * Deletes a single account.
70 *
71 * @param string $username Account to delete
72 */
73 public function deleteAccount($username)
74 {
75 $this->deleteAccounts([$username]);
76 }
77
78 /**
79 * Deletes multiple accounts.
80 *
81 * @param string[] $usernames Accounts to delete
82 */
83 public function deleteAccounts(array $usernames)
84 {
85 $options = ['confirmed' => 'Confirm', 'delete' => 'yes'];
86 foreach (array_values($usernames) as $idx => $username) {
87 $options["select{$idx}"] = $username;
88 }
89 $this->invokeApiPost('SELECT_USERS', $options);
90 }
91
92 /**
93 * Suspends a single account.
94 *
95 * @param string $username Account to delete
96 */
97 public function suspendAccount($username)
98 {
99 $this->suspendAccounts([$username]);
100 }
101
102 /**
103 * Unsuspends a single account.
104 *
105 * @param string $username Account to delete
106 */
107 public function unsuspendAccount($username)
108 {
109 $this->suspendAccounts([$username], false);
110 }
111
112 /**
113 * Suspends (or unsuspends) multiple accounts.
114 *
115 * @param string[] $usernames Accounts to delete
116 * @param bool $suspend (true - suspend, false - unsuspend)
117 */
118 public function suspendAccounts(array $usernames, $suspend = true)
119 {
120 $options = ['suspend' => $suspend ? 'Suspend' : 'Unsuspend'];
121 foreach (array_values($usernames) as $idx => $username) {
122 $options['select' . $idx] = $username;
123 }
124 $this->invokeApiPost('SELECT_USERS', $options);
125 }
126
127 /**
128 * Unsuspends multiple accounts.
129 *
130 * @param string[] $usernames Accounts to delete
131 */
132 public function unsuspendAccounts(array $usernames)
133 {
134 $this->suspendAccounts($usernames, false);
135 }
136
137 /**
138 * Returns all IPs available to this reseller.
139 *
140 * @return array List of IPs as strings
141 */
142 public function getIPs()
143 {
144 return $this->invokeApiGet('SHOW_RESELLER_IPS');
145 }
146
147 /**
148 * Returns a single user by name.
149 *
150 * @param string $username
151 * @return User|null
152 */
153 public function getUser($username)
154 {
155 $resellers = $this->getUsers();
156 return isset($resellers[$username]) ? $resellers[$username] : null;
157 }
158
159 /**
160 * Returns all users for this reseller.
161 *
162 * @return User[] Associative array of users
163 */
164 public function getUsers()
165 {
166 return BaseObject::toObjectArray($this->invokeApiGet('SHOW_USERS'), User::class, $this);
167 }
168
169 /**
170 * Impersonates a user, allowing the reseller/admin to act on their behalf.
171 *
172 * @param string $username Login of the account to impersonate
173 * @param bool $validate Whether to check the user exists and is a user
174 * @return UserContext
175 */
176 public function impersonateUser($username, $validate = false)
177 {
178 return new UserContext($this->getConnection()->loginAs($username), $validate);
179 }
180 }
181