Test Failed
Push — master ( 815ce0...287534 )
by Joe
07:08
created

Cloudlinux::reconcile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
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 2017
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        $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 is registered by any customer.
134
	 *
135
	 * @param string $ipAddress ip address to check
136
	 * @return FALSE|array Will return list of registered license types or empty list if provided IP is not registered yet.
137
	 */
138
	public function check($ipAddress) {
139
		$this->response = $this->getcurlpage($this->restUrl.'ipl/check.json?ip='.$ipAddress.'&token='.$this->authToken());
140
		$response = json_decode($this->response, TRUE);
141
		if ($response['success'] == 1)
142
			return $response['data'];
143
		else
144
			return FALSE;
145
	}
146
147
	/**
148
	 * Check if IP license was registered by any customer. Arguments:
149
	 *
150
	 * @param string $ipAddress ip address to remove
151
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
152
	 * @throws XmlRpcException for critical errors
153
	 * @return FALSE|array (list<int>): List of registered license types or empty list if no license found
154
	 */
155
	public function isLicensed($ipAddress, $checkAll = TRUE) {
156
		if ($this->apiType == 'xml')
157
			return $this->xmlIsLicensed($ipAddress, $checkAll);
158
		else
159
			return $this->check($ipAddress);
160
	}
161
	/**
162
	 * Check if IP license was registered by any customer. Arguments:
163
	 *
164
	 * @throws XmlRpcException for critical errors
165
	 * @param string $ipAddress ip address to remove
166
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
167
	 * @return FALSE|array (list<int>): List of registered license types or empty list if no license found
168
	 */
169
	public function xmlIsLicensed($ipAddress, $checkAll = TRUE) {
170
		$xmlClient = $this->xmlClient;
171
		try {
172
			return $this->response = $xmlClient->is_licensed($this->authToken(), $ipAddress, $checkAll);
173
		} catch (\Exception $e) {
174
			$this->log('error', 'Caught exception code: '.$e->getCode(), __LINE__, __FILE__);
175
			$this->log('error', 'Caught exception message: '.$e->getMessage(), __LINE__, __FILE__);
176
			return FALSE;
177
		}
178
	}
179
180
	/**
181
	 * Will register IP based license for authorized user.
182
	 *
183
	 * @param string $ipAddress ip address to registger
184
	 * @param int $type IP license type (1,2 or 16)
185
	 * @return array On success response returns information about created or already registered license.   ip(string)    type(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
186
	 */
187
	public function register($ipAddress, $type) {
188
		$this->response = $this->getcurlpage($this->restUrl.'ipl/register.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken());
189
		return json_decode($this->response, TRUE);
190
	}
191
192
	/**
193
	 * Will remove IP based license from authorized user licenses.
194
	 *
195
	 * @param string $ipAddress ip address to remove licenses on
196
	 * @param int $type optional license type. If empty, will remove licenses with all types
197
	 * @return bool
198
	 */
199
	public function restRemove($ipAddress, $type = 0) {
200
		if ($type != 0)
201
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken());
202
		else
203
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&token='.$this->authToken());
204
		return json_decode($this->response, TRUE);
205
	}
206
207
	/**
208
	 * Will remove IP based license from authorized user licenses.
209
	 *
210
	 * @param string $ipAddress ip address to remove licenses on
211
	 * @param int $type optional license type. If empty or 0, will remove licenses with all types
212
	 * @return bool|int
213
	 */
214
	public function remove($ipAddress, $type = 0) {
215
		if ($this->apiType == 'xml')
216
			return $this->removeLicense($ipAddress, $type);
217
		else
218
			return $this->restRemove($ipAddress, $type);
219
	}
220
221
	/**
222
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
223
	 * or
224
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
225
	 * @param string         $ipAddress   ip address to remove
226
	 * @param int $type optional parameter to specify the type of license to remove (1,2, or 16) or 0 for all
227
	 * @return bool|int 0 on success, -1 on error, Error will be returned also if account have no licenses for provided IP.
228
	 */
229
	public function removeLicense($ipAddress, $type = 0) {
230
		$this->log('info', "Calling CloudLinux->xmlClient->remove_license({$this->authToken()}, {$ipAddress}, {$type})", __LINE__, __FILE__);
231
		try {
232
			return $this->response = $this->xmlClient->remove_license($this->authToken(), $ipAddress, $type);
233
		} catch (\Exception $e) {
234
			$this->log('error', 'Caught exception code: '.$e->getCode(), __LINE__, __FILE__);
235
			$this->log('error', 'Caught exception message: '.$e->getMessage(), __LINE__, __FILE__);
236
			return FALSE;
237
		}
238
	}
239
240
	/**
241
	 * Return all IP licenses owned by authorized user.
242
	 *
243
	 * The normal response will look something like:
244
	 * 	[
245
	 * 		'success': TRUE,
246
	 * 		'data': [
247
	 * 			[
248
	 * 				'created': '2017-05-05T16:19-0400',
249
	 * 				'ip': '66.45.240.186',
250
	 * 				'registered': TRUE,
251
	 * 				'type': 1
252
	 * 			], [
253
	 * 				'created': '2016-10-14T10:42-0400',
254
	 * 				'ip': '131.153.38.228',
255
	 * 				'registered': FALSE,
256
	 * 				'type': 1
257
	 * 			],
258
	 *  .....
259
	 *
260
	 * @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
261
	 */
262
	public function restList() {
263
		$this->response = $this->getcurlpage($this->restUrl.'ipl/list.json?token='.$this->authToken());
264
		return json_decode($this->response, TRUE);
265
	}
266
267
	/**
268
	 * Return list of all IP licenses owned by authorized user
269
	 *
270
	 * @throws XmlRpcException for critical errors
271
	 * @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
272
	 */
273
	public function reconcile() {
274
		$this->response = $this->xmlClient->reconcile($this->authToken());
275
		return $this->response;
276
	}
277
278
	/**
279
	 * alias function to get a list of licenses
280
	 *
281
	 * @return array|FALSE
282
	 * @throws \Detain\Cloudlinux\XmlRpcException
283
	 */
284
	public function licenseList() {
285
		if ($this->apiType == 'rest')
286
			return $this->restList();
287
		else
288
			return $this->reconcile();
289
	}
290
291
	/**
292
	 * Register new IP license.
293
	 *
294
	 * @param string $ipAddress IP Address
295
	 * @param integer $type license type (1,2 or 16)
296
	 * @throws XmlRpcException for critical errors
297
	 * @return FALSE|integer 0 on success, -1 on error
298
	 */
299
	public function license($ipAddress, $type) {
300
		$type = (int) $type;
301
		$xmlClient = $this->xmlClient;
302
		try {
303
			$this->log('error', 'Calling License('.$this->authToken().','.$ipAddress.','.$type.')', __LINE__, __FILE__);
304
			return $this->response = $xmlClient->license($this->authToken(), $ipAddress, $type);
305
		} catch (\Exception $e) {
306
			$this->log('error', 'Caught exception code: '.$e->getCode(), __LINE__, __FILE__);
307
			$this->log('error', 'Caught exception message: '.$e->getMessage(), __LINE__, __FILE__);
308
			return FALSE;
309
		}
310
	}
311
}
312
313