-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcpanelapi.php
More file actions
372 lines (330 loc) · 10.8 KB
/
Copy pathcpanelapi.php
File metadata and controls
372 lines (330 loc) · 10.8 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* PHP class to handle connections with cPanel's UAPI and API2 specifically through cURL requests as seamlessly and simply as possible.
*
* For documentation on cPanel's UAPI:
*
* @see https://documentation.cpanel.net/display/SDK/Guide+to+UAPI
* For documentation on cPanel's API2:
* @see https://documentation.cpanel.net/display/SDK/Guide+to+cPanel+API+2
*
* Please use UAPI where possible, only use API2 where the equivalent doesn't exist for UAPI
*
* @author Trurl McByte <trurl@mcbyte.net>
* @copyright 2016 Trurl McByte
* @license license.txt The MIT License (MIT)
*
* @link https://github.com/TrurlMcByte/cpanel-UAPI-php-class
*
* Fork from:
*
* @author N1ghteyes - www.source-control.co.uk
* @copyright 2016 N1ghteyes
* @license license.txt The MIT License (MIT)
*
* @link https://github.com/N1ghteyes/cpanel-UAPI-php-class
*/
/**
* Class cPanelAPI.
*/
class cpanelapi
{
public $version = '1.1';
public $server;
private $maxredirect = 0;
private $ssl = 1;
private $port = 2083;
private $scope = '';
private $api;
private $auth;
private $user;
private $pass;
private $secret;
private $type;
private $session;
private $apipath;
public $method;
private $requestUrl;
private $last_answer;
/**
* we emulate a browser here since some websites detect
* us as a bot and don't let us do our job.
*/
public $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)'.
' Gecko/20041107 Firefox/1.0';
/**
* @param $user
* @param $pass
* @param $server
* @param $api (optional) api type 'uapi' ot 'api2' (by default is 'uapi')
* @param $ssl (optional) use SSL (by default is TRUE)
* @param $port (optional) use SSL (by default is TRUE)
* @param $maxredirect (optional) Number of redirects to make, typically 0 is fine. on some shared setups this will need to be increased (defaul 0)
*
* @return cpanelAPI $this self object
*/
public function __construct($user, $pass, $server, $api = 'uapi', $ssl = true, $port = null, $maxredirect = 0)
{
$this->user = $user;
$this->pass = $pass;
$this->ssl = $ssl;
if ($port === null) {
$this->port = $this->ssl ? 2083 : 2082;
}
$this->server = $server;
$this->maxredirect = $maxredirect;
return $this->setApi($api);
}
/**
* @param string $api 'uapi' ot 'api2'
*
* @return cpanelAPI $this self object
*/
public function setApi($api)
{
$this->api = $api;
$this->setApiPath();
return $this;
}
/**
* bit crazy.
*/
public function __get($name)
{
if ($name === 'API2') {
return $this->setApi('api2');
}
if ($name === 'UAPI' || $name === 'UAPI2') {
return $this->setApi('uapi');
}
$this->scope($name);
return new cpanelapimethod($this, $name);
}
public function __invoke()
{
if (!empty($this->apipath) && !empty($this->scope) && !empty($this->method) && func_num_args() > 0) {
return $this->APIcall($this->method, func_get_arg(0));
}
return $this;
}
/**
* Magic __call method, will translate all function calls to object to API requests.
*
* @param $name - name of the function
* @param $arguments - an array of arguments
*
* @throws Exception
* @return array report array
*
*/
public function __call($name, $arguments)
{
if (method_exists($this, $name)) {
return call_user_func_array(array($this, $name), $arguments);
}
if ($name === 'same' && !empty($this->method)) {
$name = $this->method;
} else {
$this->method = $name;
}
if (count($arguments) < 1 || !is_array($arguments[0])) {
$arguments[0] = (count($arguments) > 0 && is_object($arguments[0])) ? ((array) $arguments[0]) : array();
}
return $this->APIcall($name, $arguments[0]);
}
public function getLastRequest()
{
return $this->requestUrl;
}
/**
* set the scope to the module we want to use. NOTE: this IS case sensitive.
*
* @param $scope
*
* @return cpanelAPI $this self object
*/
public function scope($scope)
{
$this->scope = $scope;
return $this;
}
private function setApiPath()
{
switch ($this->api) {
case 'uapi' :
$this->apipath = '/execute/';
break;
case 'api2':
$this->apipath = '/json-api/cpanel/';
break;
default:
throw new Exception('$this->api is not set or is incorrectly set. The only available options are \'uapi\' or \'api2\'');
}
}
/**
* @param $name
* @param $arguments
*
* @throws Exception
* @return bool|mixed
*
*/
private function APIcall($name, $arguments)
{
$this->auth = base64_encode($this->user.':'.$this->pass);
$this->type = $this->ssl == 1 ? 'https://' : 'http://';
$this->requestUrl = $this->type.$this->server.':'.$this->port.$this->apipath;
switch ($this->api) {
case 'uapi':
$this->requestUrl .= ($this->scope != '' ? $this->scope.'/' : '').$name.'?';
break;
case 'api2':
if ($this->scope == '') {
throw new Exception('Scope must be set.');
}
$this->requestUrl .= '?cpanel_jsonapi_user='.$this->user.'&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module='.$this->scope.'&cpanel_jsonapi_func='.$name.'&';
break;
default:
throw new Exception('$this->api is not set or is incorrectly set. The only available options are \'uapi\' or \'api2\'');
}
foreach ($arguments as $key => $value) {
$this->requestUrl .= $key.'='.$value.'&';
}
$this->last_query = (object) array('error' => null, 'api' => $this->api, 'scope' => $this->scope, 'method' => $name, 'args' => $arguments, 'reply' => null);
$this->last_query->reply = $this->curl_request($this->requestUrl);
if ($this->last_query->reply['errno'] === 0) {
$this->last_answer = json_decode($this->last_query->reply['content']);
if (json_last_error() !== JSON_ERROR_NONE) {
$this->last_query->error = 'JSON ERROR: '.$this->last_query->json_error;
} else {
unset($this->last_query->reply);
}
} else {
$this->last_query->error = $this->last_query->reply['errmsg'];
}
if (is_object($this->last_answer)) {
$this->last_answer->__query = $this->last_query;
} else {
return (object) array('__query' => $this->last_query);
}
return $this->last_answer;
}
/**
* @param $url
*
* @return bool|mixed
*/
private function curl_request($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$this->auth));
curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = $this->curl_exec_follow($ch, $this->maxredirect);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch);
curl_close($ch);
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
/**
* @param $ch
* @param null $maxredirect
*
* @return bool|mixed
*/
private function curl_exec_follow($ch, &$maxredirect = null)
{
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
$mr = $maxredirect === null ? 5 : intval($maxredirect);
if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
if ($mr > 0) {
$original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$newurl = $original_url;
$rch = curl_copy_handle($ch);
curl_setopt($rch, CURLOPT_HEADER, true);
curl_setopt($rch, CURLOPT_NOBODY, true);
curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
do {
curl_setopt($rch, CURLOPT_URL, $newurl);
$header = curl_exec($rch);
if (curl_errno($rch)) {
$code = 0;
} else {
$code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
if ($code == 301 || $code == 302) {
preg_match('/Location:(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches));
// if no scheme is present then the new url is a
// relative path and thus needs some extra care
if (!preg_match('/^https?:/i', $newurl)) {
$newurl = $original_url.$newurl;
}
} else {
$code = 0;
}
}
} while ($code && --$mr);
curl_close($rch);
if (!$mr) {
if ($maxredirect === null) {
trigger_error('Too many redirects.', E_USER_WARNING);
} else {
$maxredirect = 0;
}
return false;
}
curl_setopt($ch, CURLOPT_URL, $newurl);
}
}
return curl_exec($ch);
}
}
/**
* Pseudo API class.
*/
class cpanelapimethod
{
public $base = null;
public $scope = '';
public $method = '';
public function __construct(cpanelapi &$base, $scope)
{
$this->base = &$base;
$this->scope = $scope;
return $this;
}
public function __get($method)
{
$this->base->scope($this->scope);
$this->method = $method;
$this->base->method = $this->method;
return $this;
}
public function __call($method, $arguments)
{
$this->base->scope($this->scope);
if ($method !== 'same') {
$this->method = $method;
$this->base->method = $this->method;
}
if (empty($this->method) && !(empty($this->base->method))) {
$this->method = $this->base->method;
}
assert(!empty($this->method));
return call_user_func_array(array($this->base, $this->method), $arguments);
}
}