This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
libyouhosting.php
359 lines (316 loc) · 11.6 KB
/
libyouhosting.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/**
* Class YouHosting
* A PHP wrapper around the official API provided by YouHosting. Requires a SuperVIP account.
*/
class YouHosting {
private $config = array(
'continueIfNoResponse' => false,
);
private $host = "https://rest.main-hosting.com";
/**
* Create a new YouHosting object
* @param string $apikey your YouHosting API key
* @param array $config an array with configuration values: {continueIfNoResponse: true/false}
*/
public function __construct($apikey, $config = array()){
$this->apikey = $apikey;
$this->config = array_merge($this->config, $config);
}
/**
* Perform a GET request
* @param $url string the URL to request
* @param $params array (optional) GET values to pass
* @return mixed the result json data
* @throws YouHostingException if the connection fails, this exception is thrown
*/
private function get($url, $params = null){
do {
$ch = curl_init($this->host . $url . (!empty($params) ? "?".http_build_query($params) : ""));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "reseller:" . $this->apikey);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$return = curl_exec($ch);
curl_close($ch);
if(empty($return) && !$this->config['continueIfNoResponse']){
throw new YouHostingException(array('code' => 0, 'message' => 'Empty response from YouHosting'));
}
} while (empty($return) && $this->config['continueIfNoResponse']);
$data = json_decode($return);
if(!empty($data->error)){
throw new YouHostingException($data->error);
}
return $data->result;
}
/**
* Perform a POST request
* @param $url string the URL to request
* @param $data array (optional) post data to add
* @return mixed the result json data
* @throws YouHostingException if the connection fails, this exception is thrown
*/
private function post($url, $data = null){
do {
$ch = curl_init($this->host.$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
if(!empty($data)){
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));
}
curl_setopt($ch, CURLOPT_USERPWD, "reseller:".$this->apikey);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$return = curl_exec($ch);
curl_close($ch);
if(empty($return) && !$this->config['continueIfNoResponse']){
throw new YouHostingException(array('code' => 0, 'message' => 'Empty response from YouHosting'));
}
} while (empty($return) && $this->config['continueIfNoResponse']);
$data = json_decode($return);
if(!empty($data->error)){
throw new YouHostingException($data->error);
}
return $data->result;
}
/**
* Perform a DELETE request
* @param $url string the URL to request
* @return mixed the result json data
* @throws YouHostingException if the connection fails, this exception is thrown
*/
private function delete($url){
do{
$ch = curl_init($this->host.$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_USERPWD, "reseller:".$this->apikey);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$return = curl_exec($ch);
curl_close($ch);
if(empty($return) && !$this->config['continueIfNoResponse']){
throw new YouHostingException(array('code' => 0, 'message' => 'Empty response from YouHosting'));
}
} while (empty($return) && $this->config['continueIfNoResponse']);
$data = json_decode($return);
if(!empty($data->error)){
throw new YouHostingException($data->error);
}
return $data->result;
}
/**
* Checks the connection to the API server
* @return bool whether the connection was successful
* @throws YouHostingException
*/
public function ping(){
return $this->get("/ping") == "pong";
}
/**
* Create a new client
* @param $data array an array of data, containing {first_name, email, password, captcha_id}
* @return int the ID of the new client
* @throws YouHostingException
*/
public function clientCreate($data){
return $this->post("/v1/client", $data)->id;
}
/**
* List all clients of the reseller
*
* @param int $page (optional) if you don't want to start at page one (to resume a partially completed pull)
* @return array an array of clients
* @throws YouHostingException
*/
public function clientList($page = 1){
return $this->get("/v1/client/list?page=".$page);
}
/**
* Get the details of a client
* @param $client_id int the ID of the client
* @return array
* @throws YouHostingException
*/
public function clientGet($client_id){
return $this->get("/v1/client/".$client_id);
}
/**
* Get a one time URL to login to the client's account
* @param $client_id
* @return string
* @throws YouHostingException
*/
public function clientLoginUrl($client_id){
return $this->get("/v1/client/".$client_id."/login-url");
}
/**
* Check if a domain is available to use
* @param string $type "domain" or "subdomain"
* @param string $domain if type is "domain", then this is the domain to check. if type is "subdomain", this is the master domain
* @param string $subdomain if the type is "subdomain", this is the string the client would like to use as his own subdomain
* @return bool whether the domain is available
* @throws YouHostingException
*/
public function accountCheck($type, $domain, $subdomain = ""){
$postdata = array(
'type' => $type,
'domain' => $domain,
);
if(!empty($subdomain)){
$postdata['subdomain'] = $subdomain;
}
return $this->get("/v1/account/check", $postdata);
}
/**
* Create a new account
* @param $data array account data {client_id, captcha_id, plan_id, type (see accountCheck), domain (see accountCheck), subdomain (see accountCheck), password, client_ip }
* @return mixed
* @throws YouHostingException
*/
public function accountCreate($data){
return $this->post("/v1/account", $data);
}
/**
* Get a list of account data
*
* WARNING: the connection limiter may break this when used with a larger reseller account. I'm still looking for a way to work around this in a user friendly fashion.
* If you have any suggestions, send them to [email protected]
*
* @param int $page (optional) if you don't want to start at page one (to resume a partially completed pull)
* @param int $client_id (optional) if you want to get the accounts for a specific client, you can specify a client id
* @return array a list of accounts
* @throws YouHostingException
*/
public function accountList($page = 1, $client_id = null){
$totalPages = PHP_INT_MAX;
$accounts = array();
for($i = $page; $i <= $totalPages; $i++){
$url = "/v1/account/list?page=".$i;
if(!empty($client_id)){
$url .= "&client_id=".$client_id;
}
$data = $this->get($url);
$totalPages = $data->pages;
$accounts = array_merge($accounts, $data->list);
}
return $accounts;
}
/**
* get the details of an account
* @param $account_id int an account id
* @return array
* @throws YouHostingException
*/
public function accountGet($account_id){
return $this->get("/v1/account/".$account_id);
}
/**
* suspend an account
* @param $account_id int
* @return bool whether the action was successful
* @throws YouHostingException
*/
public function accountSuspend($account_id){
return $this->post("/v1/account/".$account_id."/suspend", array(
'id' => $account_id
));
}
/**
* unsuspend an account
* @param $account_id int
* @return bool whether the action was successful
* @throws YouHostingException
*/
public function accountUnsuspend($account_id){
return $this->post("/v1/account/".$account_id."/unsuspend", array(
'id' => $account_id
));
}
/**
* Get a one time URL to login to the account
* @param $account_id int
* @return string
* @throws YouHostingException
*/
public function accountLoginUrl($account_id){
return $this->get("/v1/account/".$account_id."/login-url");
}
/**
* delete the account
*
* WARNING: last time I tested this, it didn't do anything regardless of the status of the account.
* Also, you cannot delete free accounts which have been created through the API (THANKS youhosting)
*
* @param $account_id
* @return bool
* @throws YouHostingException
*/
public function accountDelete($account_id){
return $this->delete("/v1/account/".$account_id);
}
/**
* Get a new captcha
* @param string $client_ip the IP of the requester
* @return array a response array containing an id and url
* @throws YouHostingException
*/
public function captchaGet($client_ip){
return $this->post("/v1/captcha", array(
'client_ip' => $client_ip,
));
}
/**
* Solve the captcha
* @param int $captcha_id the ID of the captcha
* @param string $solution the solution provided by the user
* @param string $client_ip the IP of the requester
* @return bool whether the answer is correct or not
* @throws YouHostingException
*/
public function captchaSolve($captcha_id, $solution, $client_ip){
$result = $this->post("/v1/captcha/".$captcha_id, array(
'id' => $captcha_id,
'solution' => $solution,
'client_ip' => $client_ip
));
return $result->solved;
}
/**
* List the domains for which clients can create subdomains
* @return array an assoc array of accountid=>domain
* @throws YouHostingException
*/
public function subdomains(){
return $this->get("/v1/settings/subdomains");
}
/**
* List the plans for this reseller account
* @return array with json objects containing id, name and type (paid/free)
* @throws YouHostingException
*/
public function plans(){
return $this->get("/v1/settings/plans");
}
/**
* Get the nameservers
* @return array an array with ns1, ..., ns4 and ip1, ... ip4
* @throws YouHostingException
*/
public function nameservers(){
return $this->get("/v1/settings/nameservers");
}
}
/**
* Class YouHostingException
* An exception class for the YouHosting wrapper
*/
class YouHostingException extends Exception {
public function __construct($error){
if(is_array($error)){
parent::__construct($error['message'], $error['code']);
} else {
parent::__construct($error->message, $error->code);
}
}
}