This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
DirectAdmin.php
170 lines (153 loc) · 5.69 KB
/
DirectAdmin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/*
* DirectAdmin API Client
* (c) Omines Internetbureau B.V. - https://omines.nl/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Omines\DirectAdmin;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException;
use Omines\DirectAdmin\Context\AdminContext;
use Omines\DirectAdmin\Context\ResellerContext;
use Omines\DirectAdmin\Context\UserContext;
use Omines\DirectAdmin\Utility\Conversion;
/**
* DirectAdmin API main class, encapsulating a specific account connection to a single server.
*
* @author Niels Keurentjes <niels.keurentjes@omines.com>
*/
class DirectAdmin
{
const ACCOUNT_TYPE_ADMIN = 'admin';
const ACCOUNT_TYPE_RESELLER = 'reseller';
const ACCOUNT_TYPE_USER = 'user';
/** @var string */
private $authenticatedUser;
/** @var string */
private $username;
/** @var string */
private $password;
/** @var string */
private $baseUrl;
/** @var Client */
private $connection;
/**
* Connects to DirectAdmin with an admin account.
*
* @param string $url The base URL of the DirectAdmin server
* @param string $username The username of the account
* @param string $password The password of the account
* @param bool $validate Whether to ensure the account exists and is of the correct type
* @return AdminContext
*/
public static function connectAdmin($url, $username, $password, $validate = false)
{
return new AdminContext(new self($url, $username, $password), $validate);
}
/**
* Connects to DirectAdmin with a reseller account.
*
* @param string $url The base URL of the DirectAdmin server
* @param string $username The username of the account
* @param string $password The password of the account
* @param bool $validate Whether to ensure the account exists and is of the correct type
* @return ResellerContext
*/
public static function connectReseller($url, $username, $password, $validate = false)
{
return new ResellerContext(new self($url, $username, $password), $validate);
}
/**
* Connects to DirectAdmin with a user account.
*
* @param string $url The base URL of the DirectAdmin server
* @param string $username The username of the account
* @param string $password The password of the account
* @param bool $validate Whether to ensure the account exists and is of the correct type
* @return UserContext
*/
public static function connectUser($url, $username, $password, $validate = false)
{
return new UserContext(new self($url, $username, $password), $validate);
}
/**
* Creates a connection wrapper to DirectAdmin as the specified account.
*
* @param string $url The base URL of the DirectAdmin server
* @param string $username The username of the account
* @param string $password The password of the account
*/
protected function __construct($url, $username, $password)
{
$accounts = explode('|', $username);
$this->authenticatedUser = current($accounts);
$this->username = end($accounts);
$this->password = $password;
$this->baseUrl = rtrim($url, '/') . '/';
$this->connection = new Client([
'base_uri' => $this->baseUrl,
'auth' => [$username, $password],
]);
}
/**
* Returns the username behind the current connection.
*
* @return string Currently logged in user's username
*/
public function getUsername()
{
return $this->username;
}
/**
* Invokes the DirectAdmin API with specific options.
*
* @param string $method HTTP method to use (ie. GET or POST)
* @param string $command DirectAdmin API command to invoke
* @param array $options Guzzle options to use for the call
* @return array The unvalidated response
* @throws DirectAdminException If anything went wrong on the network level
*/
public function invokeApi($method, $command, $options = [])
{
$result = $this->rawRequest($method, '/CMD_API_' . $command, $options);
if (!empty($result['error'])) {
throw new DirectAdminException("$method to $command failed: $result[details] ($result[text])");
}
return Conversion::sanitizeArray($result);
}
/**
* Returns a clone of the connection logged in as a managed user or reseller.
*
* @param string $username
* @return DirectAdmin
*/
public function loginAs($username)
{
// DirectAdmin format is to just pipe the accounts together under the master password
return new self($this->baseUrl, $this->authenticatedUser . "|{$username}", $this->password);
}
/**
* Sends a raw request to DirectAdmin.
*
* @param string $method
* @param string $uri
* @param array $options
* @return array
*/
public function rawRequest($method, $uri, $options)
{
try {
$response = $this->connection->request($method, $uri, $options);
if ($response->getHeader('Content-Type')[0] == 'text/html') {
throw new DirectAdminException(sprintf('DirectAdmin API returned text/html to %s %s containing "%s"', $method, $uri, strip_tags($response->getBody()->getContents())));
}
$body = $response->getBody()->getContents();
return Conversion::responseToArray($body);
} catch (TransferException $exception) {
// Rethrow anything that causes a network issue
throw new DirectAdminException(sprintf('%s request to %s failed', $method, $uri), 0, $exception);
}
}
}