1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Cloudlinux Functionality |
4
|
|
|
* |
5
|
|
|
* API Documentation at: .. ill fill this in later from forum posts |
6
|
|
|
* |
7
|
|
|
* @author Joe Huss <[email protected]> |
8
|
|
|
* @copyright 2018 |
9
|
|
|
* @package MyAdmin |
10
|
|
|
* @category Licenses |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Detain\Cloudlinux; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Cloudlinux Licensing Class |
17
|
|
|
* |
18
|
|
|
* XMLRPC Exception codes: |
19
|
|
|
* 1 Internal (unknown) Error |
20
|
|
|
* 10 Not authorized |
21
|
|
|
* 30 Invalid call arguments |
22
|
|
|
* 40 Invalid IP format |
23
|
|
|
* |
24
|
|
|
* @link https://cln.cloudlinux.com/clweb/downloads/cloudlinux-xmlrpc-api.pdf XML API Documentation |
25
|
|
|
* @link https://cln.cloudlinux.com/clweb/downloads/cloudlinux-rest-api.pdf REST API Documentation |
26
|
|
|
* |
27
|
|
|
* @access public |
28
|
|
|
*/ |
29
|
|
|
class Cloudlinux { |
30
|
|
|
private $login = ''; |
31
|
|
|
private $key = ''; |
32
|
|
|
public $prefix = 'registration.'; |
33
|
|
|
public $encoding = 'utf-8'; // utf-8 / UTF-8 |
34
|
|
|
public $apiType = 'rest'; |
35
|
|
|
public $sslverify = FALSE; |
36
|
|
|
public $xmlOptions = []; |
37
|
|
|
public $xmlUrl = 'https://cln.cloudlinux.com/clweb/xmlrpc'; |
38
|
|
|
public $restUrl = 'https://cln.cloudlinux.com/api/'; |
39
|
|
|
public $restOptions = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \XML_RPC2_Client |
43
|
|
|
*/ |
44
|
|
|
public $xmlClient; |
45
|
|
|
public $response; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Cloudlinux::__construct() |
49
|
|
|
* |
50
|
|
|
* @param string $login API Login Name |
51
|
|
|
* @param string $key API Key |
52
|
|
|
* @param string $apiType API type to use, can be 'rest' or 'xml' |
53
|
|
|
*/ |
54
|
|
|
public function __construct($login, $key, $apiType = 'rest') { |
55
|
|
|
$this->login = $login; |
56
|
|
|
$this->key = $key; |
57
|
|
|
$this->apiType = $apiType; |
58
|
|
|
$limitType = FALSE; |
59
|
|
|
if ($limitType === FALSE || $this->apiType == 'xml') { |
|
|
|
|
60
|
|
|
include_once 'XML/RPC2/Client.php'; |
61
|
|
|
$this->xmlOptions['prefix'] = $this->prefix; |
62
|
|
|
$this->xmlOptions['encoding'] = $this->encoding; |
63
|
|
|
$this->xmlOptions['sslverify'] = $this->sslverify; |
64
|
|
|
$this->xmlClient = \XML_RPC2_Client::create($this->xmlUrl, $this->xmlOptions); |
65
|
|
|
} |
66
|
|
|
if ($limitType === FALSE || $this->apiType == 'rest') |
|
|
|
|
67
|
|
|
$this->restOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslverify; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* automatic authToken generator |
72
|
|
|
* |
73
|
|
|
* @return FALSE|string the authToken |
74
|
|
|
*/ |
75
|
|
|
public function authToken() { |
76
|
|
|
$time = time(); |
77
|
|
|
return $this->login.'|'.$time.'|'.sha1($this->key.$time); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* getcurlpage() |
82
|
|
|
* gets a webpage via curl and returns the response. |
83
|
|
|
* also it sets a mozilla type agent. |
84
|
|
|
* @param string $url the url of the page you want |
85
|
|
|
* @return string the webpage |
86
|
|
|
*/ |
87
|
|
|
public function getcurlpage($url) { |
88
|
|
|
$agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2790.0 Safari/537.36'; |
89
|
|
|
$curl = curl_init($url); |
90
|
|
|
$options = $this->restOptions; |
91
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
92
|
|
|
curl_setopt($curl, CURLOPT_USERAGENT, $agent); |
93
|
|
|
if (is_array($options) && count($options) > 0) |
94
|
|
|
foreach ($options as $key => $value) |
95
|
|
|
curl_setopt($curl, $key, $value); |
96
|
|
|
$return = curl_exec($curl); |
97
|
|
|
curl_close($curl); |
98
|
|
|
return $return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $level |
103
|
|
|
* @param $text |
104
|
|
|
* @param string $line |
105
|
|
|
* @param string $file |
106
|
|
|
*/ |
107
|
|
|
public function log($level, $text, $line = '', $file = '') { |
108
|
|
|
if (function_exists('myadmin_log')) |
109
|
|
|
myadmin_log('cloudlinux', $level, $text, $line, $file); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Return system information about several Cloudlinux services |
114
|
|
|
* |
115
|
|
|
* @return array array of system information |
116
|
|
|
*/ |
117
|
|
|
public function status() { |
118
|
|
|
$this->response = $this->getcurlpage($this->restUrl.'status.json'); |
119
|
|
|
return json_decode($this->response, TRUE); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Will return information about what kind of license types are available for registration and what types are already used by current account. |
124
|
|
|
* @param string $ipAddress ip address to check |
125
|
|
|
* @return array returns an array with available(int[]) list of types that can be used to register new IP license, and owned(int[]) list of types that already registered(owned) by this account |
126
|
|
|
*/ |
127
|
|
|
public function availability($ipAddress) { |
128
|
|
|
$this->response = $this->getcurlpage($this->restUrl.'ipl/availability.json?ip='.$ipAddress.'&token='.$this->authToken()); |
129
|
|
|
return json_decode($this->response, TRUE); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check if IP license was registered by any customer. Arguments: |
134
|
|
|
* |
135
|
|
|
* @param string $ipAddress ip address to remove |
136
|
|
|
* @param bool $checkAll True will search for any type of license. False only for types 1 or 2 |
137
|
|
|
* @throws XmlRpcException for critical errors |
138
|
|
|
* @return FALSE|array (list<int>): List of registered license types or empty list if no license found |
139
|
|
|
*/ |
140
|
|
|
public function isLicensed($ipAddress, $checkAll = TRUE) { |
141
|
|
|
if ($this->apiType == 'xml') |
142
|
|
|
return $this->xmlIsLicensed($ipAddress, $checkAll); |
143
|
|
|
else |
144
|
|
|
return $this->check($ipAddress); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Check if IP license is registered by any customer. |
149
|
|
|
* |
150
|
|
|
* @param string $ipAddress ip address to check |
151
|
|
|
* @return FALSE|array Will return list of registered license types or empty list if provided IP is not registered yet. |
152
|
|
|
*/ |
153
|
|
|
public function check($ipAddress) { |
154
|
|
|
$this->response = $this->getcurlpage($this->restUrl.'ipl/check.json?ip='.$ipAddress.'&token='.$this->authToken()); |
155
|
|
|
$response = json_decode($this->response, TRUE); |
156
|
|
|
if ($response['success'] == 1) |
157
|
|
|
return $response['data']; |
158
|
|
|
else |
159
|
|
|
return FALSE; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Check if IP license was registered by any customer. Arguments: |
164
|
|
|
* |
165
|
|
|
* @throws XmlRpcException for critical errors |
166
|
|
|
* @param string $ipAddress ip address to remove |
167
|
|
|
* @param bool $checkAll True will search for any type of license. False only for types 1 or 2 |
168
|
|
|
* @return FALSE|array (list<int>): List of registered license types or empty list if no license found |
169
|
|
|
*/ |
170
|
|
|
public function xmlIsLicensed($ipAddress, $checkAll = TRUE) { |
171
|
|
|
$xmlClient = $this->xmlClient; |
172
|
|
|
try { |
173
|
|
|
return $this->response = $xmlClient->is_licensed($this->authToken(), $ipAddress, $checkAll); |
174
|
|
|
} catch (\Exception $e) { |
175
|
|
|
$this->log('error', 'Caught exception code: '.$e->getCode(), __LINE__, __FILE__); |
176
|
|
|
$this->log('error', 'Caught exception message: '.$e->getMessage(), __LINE__, __FILE__); |
177
|
|
|
return FALSE; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Will remove IP based license from authorized user licenses. |
183
|
|
|
* |
184
|
|
|
* @param string $ipAddress ip address to remove licenses on |
185
|
|
|
* @param int $type optional license type. If empty or 0, will remove licenses with all types |
186
|
|
|
* @return bool|int |
187
|
|
|
*/ |
188
|
|
|
public function remove($ipAddress, $type = 0) { |
189
|
|
|
if ($this->apiType == 'xml') |
190
|
|
|
return $this->removeLicense($ipAddress, $type); |
191
|
|
|
else |
192
|
|
|
return $this->restRemove($ipAddress, $type); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Will remove IP based license from authorized user licenses. |
197
|
|
|
* |
198
|
|
|
* @param string $ipAddress ip address to remove licenses on |
199
|
|
|
* @param int $type optional license type. If empty, will remove licenses with all types |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
public function restRemove($ipAddress, $type = 0) { |
203
|
|
|
if ($type != 0) |
204
|
|
|
$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken()); |
205
|
|
|
else |
206
|
|
|
$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&token='.$this->authToken()); |
207
|
|
|
return json_decode($this->response, TRUE); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Remove IP licenses with specified type for customer. Also unregisters from CLN server associated with IP. |
212
|
|
|
* or |
213
|
|
|
* Remove IP licenses with specified type for customer. Also unregisters from CLN server associated with IP. |
214
|
|
|
* @param string $ipAddress ip address to remove |
215
|
|
|
* @param int $type optional parameter to specify the type of license to remove (1,2, or 16) or 0 for all |
216
|
|
|
* @return bool|int 0 on success, -1 on error, Error will be returned also if account have no licenses for provided IP. |
217
|
|
|
*/ |
218
|
|
|
public function removeLicense($ipAddress, $type = 0) { |
219
|
|
|
$this->log('info', "Calling CloudLinux->xmlClient->remove_license({$this->authToken()}, {$ipAddress}, {$type})", __LINE__, __FILE__); |
220
|
|
|
try { |
221
|
|
|
return $this->response = $this->xmlClient->remove_license($this->authToken(), $ipAddress, $type); |
222
|
|
|
} catch (\Exception $e) { |
223
|
|
|
$this->log('error', 'Caught exception code: '.$e->getCode(), __LINE__, __FILE__); |
224
|
|
|
$this->log('error', 'Caught exception message: '.$e->getMessage(), __LINE__, __FILE__); |
225
|
|
|
return FALSE; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* alias function to get a list of licenses |
231
|
|
|
* |
232
|
|
|
* @return array|FALSE |
233
|
|
|
* @throws \Detain\Cloudlinux\XmlRpcException |
234
|
|
|
*/ |
235
|
|
|
public function licenseList() { |
236
|
|
|
if ($this->apiType == 'rest') |
237
|
|
|
return $this->restList(); |
238
|
|
|
else |
239
|
|
|
return $this->reconcile(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Return all IP licenses owned by authorized user. |
244
|
|
|
* |
245
|
|
|
* The normal response will look something like: |
246
|
|
|
* [ |
247
|
|
|
* 'success': TRUE, |
248
|
|
|
* 'data': [ |
249
|
|
|
* [ |
250
|
|
|
* 'created': '2017-05-05T16:19-0400', |
251
|
|
|
* 'ip': '66.45.240.186', |
252
|
|
|
* 'registered': TRUE, |
253
|
|
|
* 'type': 1 |
254
|
|
|
* ], [ |
255
|
|
|
* 'created': '2016-10-14T10:42-0400', |
256
|
|
|
* 'ip': '131.153.38.228', |
257
|
|
|
* 'registered': FALSE, |
258
|
|
|
* 'type': 1 |
259
|
|
|
* ], |
260
|
|
|
* ..... |
261
|
|
|
* |
262
|
|
|
* @return FALSE|array an array of licenses each one containing these fields: ip(string) ype(int) license type (1,2,16) registered(boolean) TRUE if server was registered in CLN with this license (CLN licenses only). created(string) license creation time |
263
|
|
|
*/ |
264
|
|
|
public function restList() { |
265
|
|
|
$this->response = $this->getcurlpage($this->restUrl.'ipl/list.json?token='.$this->authToken()); |
266
|
|
|
return json_decode($this->response, TRUE); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Return list of all IP licenses owned by authorized user |
271
|
|
|
* |
272
|
|
|
* @throws XmlRpcException for critical errors |
273
|
|
|
* @return FALSE|array (list<structure>): List of structures or empty list. Each structure contains keys: IP(string) TYPE(int) license type REGISTERED(boolean) True if server was registered in CLN with this license |
274
|
|
|
*/ |
275
|
|
|
public function reconcile() { |
276
|
|
|
$this->response = $this->xmlClient->reconcile($this->authToken()); |
277
|
|
|
return $this->response; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Register new IP license. |
282
|
|
|
* |
283
|
|
|
* @param string $ipAddress IP Address |
284
|
|
|
* @param integer $type license type (1,2 or 16) |
285
|
|
|
* @return bool|mixed whether or not it was successfull |
286
|
|
|
*/ |
287
|
|
|
public function license($ipAddress, $type) { |
288
|
|
|
return $this->apiType == 'rest' ? $this->register($ipAddress, $type) : $this->reconcile($ipAddress, $type); |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Will register IP based license for authorized user. |
293
|
|
|
* |
294
|
|
|
* @param string $ipAddress ip address to registger |
295
|
|
|
* @param int $type IP license type (1,2 or 16) |
296
|
|
|
* @return bool|array true/false with normal response otherwise returns response |
297
|
|
|
*/ |
298
|
|
|
public function register($ipAddress, $type) { |
299
|
|
|
$this->response = $this->getcurlpage($this->restUrl.'ipl/register.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken()); |
300
|
|
|
$return = json_decode($this->response, TRUE); |
301
|
|
|
return isset($return['registered']) ? $return['registered'] : $return; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Register new IP license. |
306
|
|
|
* |
307
|
|
|
* @param string $ipAddress IP Address |
308
|
|
|
* @param integer $type license type (1,2 or 16) |
309
|
|
|
* @throws XmlRpcException for critical errors |
310
|
|
|
* @return FALSE|integer 0 on success, -1 on error |
311
|
|
|
*/ |
312
|
|
|
public function xmlLicense($ipAddress, $type) { |
313
|
|
|
$type = (int) $type; |
314
|
|
|
$xmlClient = $this->xmlClient; |
315
|
|
|
try { |
316
|
|
|
$this->log('error', 'Calling License('.$this->authToken().','.$ipAddress.','.$type.')', __LINE__, __FILE__); |
317
|
|
|
$this->response = $xmlClient->license($this->authToken(), $ipAddress, $type); |
318
|
|
|
} catch (\Exception $e) { |
319
|
|
|
$this->log('error', 'Caught exception code: '.$e->getCode(), __LINE__, __FILE__); |
320
|
|
|
$this->log('error', 'Caught exception message: '.$e->getMessage(), __LINE__, __FILE__); |
321
|
|
|
return FALSE; |
322
|
|
|
} |
323
|
|
|
if ($this->response == -1) |
324
|
|
|
return FALSE; |
325
|
|
|
elseif ($this->response == 0) |
326
|
|
|
return TRUE; |
327
|
|
|
else |
328
|
|
|
return $this->response; |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
|