Completed
Push — master ( 505b34...576cb8 )
by Joe
02:48
created

Cloudlinux::licenseList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2.0625
1
<?php
2
/**
3
 * Cloudlinux Functionality
4
 *
5
 * API Documentation at: .. ill fill this in later from forum posts
6
 *
7
 * Last Changed: $LastChangedDate: 2017-05-26 04:36:01 -0400 (Fri, 26 May 2017) $
8
 * @author detain
9
 * @version $Revision: 24803 $
10
 * @copyright 2017
11
 * @package MyAdmin
12
 * @category Licenses
13
 */
14
15
namespace Detain\Cloudlinux;
16
17
/**
18
 * Cloudlinux Licensing Class
19
 *
20
 * XMLRPC Exception codes:
21
 * 	1 ­ Internal (unknown) Error
22
 * 	10 ­ Not authorized
23
 * 	30 ­ Invalid call arguments
24
 * 	40 ­ Invalid IP format
25
 *
26
 * @link https://cln.cloudlinux.com/clweb/downloads/cloudlinux-xmlrpc-api.pdf XML API Documentation
27
 * @link https://cln.cloudlinux.com/clweb/downloads/cloudlinux-rest-api.pdf REST API Documentation
28
 *
29
 * @access public
30
 */
31
class Cloudlinux
32
{
33
	private $login = '';
34
	private $key = '';
35
	public $prefix = 'registration.';
36
	public $encoding = 'utf-8'; // utf-8 / UTF-8
37
	public $apiType = 'rest';
38
	public $sslverify = false;
39
	public $xmlOptions = [];
40
	public $xmlUrl = 'https://cln.cloudlinux.com/clweb/xmlrpc';
41
	public $restUrl = 'https://cln.cloudlinux.com/api/';
42
	public $restOptions = [];
43
	/**
44
	 * @var \XML_RPC2_Client
45
	 */
46
	public $xmlClient;
47
	public $response;
48
49
	/**
50
	 * Cloudlinux::__construct()
51
	 *
52
	 * @param string $login API Login Name
53
	 * @param string $key API Key
54
	 * @param string $apiType API type to use, can be 'rest' or 'xml'
55
	 */
56 1
	public function __construct($login, $key, $apiType = 'rest') {
57 1
		$this->login = $login;
58 1
		$this->key = $key;
59 1
		$this->apiType = $apiType;
60 1
		$limitType = false;
61 1
		if ($limitType === false || $this->apiType == 'xml') {
62 1
			include_once('XML/RPC2/Client.php');
63 1
			$this->xmlOptions['prefix'] = $this->prefix;
64 1
			$this->xmlOptions['encoding'] = $this->encoding;
65 1
			$this->xmlOptions['sslverify'] = $this->sslverify;
66 1
			$this->xmlClient = \XML_RPC2_Client::create($this->xmlUrl, $this->xmlOptions);
67
		}
68 1
		if ($limitType === false || $this->apiType == 'rest') {
69 1
			$this->restOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslverify;
70
		}
71 1
	}
72
73
	/**
74
	 * automatic authToken generator
75
	 *
76
	 * @return false|string the authToken
77
	 */
78 1
	public function authToken() {
79 1
		$time = time();
80 1
		return $this->login . '|' . $time . '|' . sha1($this->key . $time);
81
	}
82
83
	/**
84
	 * getcurlpage()
85
	 * gets a webpage via curl and returns the response.
86
	 * also it sets a mozilla type agent.
87
	 * @param string $url        the url of the page you want
88
	 * @param string $postfields postfields in the format of "v1=10&v2=20&v3=30"
0 ignored issues
show
Bug introduced by
There is no parameter named $postfields. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
89
	 * @param string|array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
90
	 * @return string the webpage
91
	 */
92 1
	public function getcurlpage($url) {
93 1
		$agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2790.0 Safari/537.36';
94 1
		$curl = curl_init($url);
95 1
		$options = $this->restOptions;
96 1
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
97 1
		curl_setopt($curl, CURLOPT_USERAGENT, $agent);
98 1
		if (is_array($options) && sizeof($options) > 0)
99 1
			foreach ($options as $key => $value)
100 1
				curl_setopt($curl, $key, $value);
101 1
		$return = curl_exec($curl);
102 1
		curl_close($curl);
103 1
		return $return;
104
	}
105
106
	public function log($level, $text, $line = '', $file = '') {
107
		if (function_exists('myadmin_log'))
108
			myadmin_log('cloudlinux', $level, $text, $line, $file);
109
		else
110
			error_log($text);
111
	}
112
113
	/**
114
	 * Return system information about several Cloudlinux services
115
	 *
116
	 * @return array array of system information
117
	 */
118 1
	public function status() {
119 1
		$this->response = $this->getcurlpage($this->restUrl.'status.json');
120 1
		return json_decode($this->response, true);
121
	}
122
123
	/**
124
	 * Will return information about what kind of license types are available for registration and what types are already used by current account.
125
	 * @param string $ipAddress ip address to check
126
	 * @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
127
	 */
128 1
	public function availability($ipAddress) {
129 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/availability.json?ip='.$ipAddress.'&token='.$this->authToken());
130 1
		return json_decode($this->response, true);
131
	}
132
133
	/**
134
	 * Check if IP license is registered by any customer.
135
	 *
136
	 * @param string $ipAddress ip address to check
137
	 * @return false|array Will return list of registered license types or empty list if provided IP is not registered yet.
138
	 */
139 1
	public function check($ipAddress) {
140 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/check.json?ip='.$ipAddress.'&token='.$this->authToken());
141 1
		$response = json_decode($this->response, true);
142 1
		if ($response['success'] == 1)
143 1
			return $response['data'];
144
		else
145
			return false;
146
	}
147
148
	/**
149
	 * Check if IP license was registered by any customer. Arguments:
150
	 *
151
	 * @param string $ipAddress ip address to remove
152
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
153
	 * @throws XmlRpcException for critical errors
154
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
155
	 */
156 1
	public function isLicensed($ipAddress, $checkAll = true) {
157 1
		if ($this->apiType == 'xml')
158
			return $this->xmlIsLicensed($ipAddress, $checkAll);
159
		else
160 1
			return $this->check($ipAddress);
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 1 View Code Duplication
	public function xmlIsLicensed($ipAddress, $checkAll = true) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171 1
		$xmlClient = $this->xmlClient;
172
		try {
173 1
			$this->response = $xmlClient->is_licensed($this->authToken(), $ipAddress, $checkAll);
174
		} catch (\Exception $e) {
175
			$this->log('error', 'Caught exception code: ' . $e->getCode());
176
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
177
			return false;
178
		}
179 1
		return $this->response;
180
	}
181
182
	/**
183
	 * Will register IP based license for authorized user.
184
	 *
185
	 * @param string $ipAddress ip address to registger
186
	 * @param int $type IP license type (1,2 or 16)
187
	 * @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
188
	 */
189
	public function register($ipAddress, $type) {
190
		$this->response = $this->getcurlpage($this->restUrl.'ipl/register.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken());
191
		return json_decode($this->response, true);
192
	}
193
194
	/**
195
	 * Will remove IP based license from authorized user licenses.
196
	 *
197
	 * @param string $ipAddress ip address to remove licenses on
198
	 * @param int $type optional license type. If empty, will remove licenses with all types
199
	 * @return bool
200
	 */
201
	public function restRemove($ipAddress, $type = 0) {
202
		if ($type != 0)
203
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken());
204
		else
205
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&token='.$this->authToken());
206
		return json_decode($this->response, true);
207
	}
208
209
	/**
210
	 * Will remove IP based license from authorized user licenses.
211
	 *
212
	 * @param string $ipAddress ip address to remove licenses on
213
	 * @param int $type optional license type. If empty or 0, will remove licenses with all types
214
	 * @return bool|int
215
	 */
216
	public function remove($ipAddress, $type = 0) {
217
		if ($this->apiType == 'xml')
218
			return $this->removeLicense($ipAddress, $type);
219
		else
220
			return $this->restRemove($ipAddress, $type);
221
	}
222
223
	/**
224
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
225
	 * or
226
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
227
	 * @param string         $ipAddress   ip address to remove
228
	 * @param int $type optional parameter to specify the type of license to remove (1,2, or 16) or 0 for all
229
	 * @return bool|int 0 on success, -1 on error, Error will be returned also if account have no licenses for provided IP.
230
	 */
231
	public function removeLicense($ipAddress, $type = 0) {
232
		$this->log('info', "Calling CLoudLinux->xmlClient->removeLicense({$this->authToken()}, {$ipAddress}, {$type})", __LINE__, __FILE__);
233
		try {
234
			$this->response = $this->remove($ipAddress, $type);
235
		} catch (\Exception $e) {
236
			$this->log('error', 'Caught exception code: ' . $e->getCode());
237
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
238
			return false;
239
		}
240
		return $this->response;
241
	}
242
243
	/**
244
	 * Return all IP licenses owned by authorized user.
245
	 *
246
	 * The normal response will look something like:
247
	 * 	[
248
	 * 		'success': true,
249
	 * 		'data': [
250
	 * 			[
251
	 * 				'created': '2017-05-05T16:19-0400',
252
	 * 				'ip': '66.45.240.186',
253
	 * 				'registered': true,
254
	 * 				'type': 1
255
	 * 			], [
256
	 * 				'created': '2016-10-14T10:42-0400',
257
	 * 				'ip': '131.153.38.228',
258
	 * 				'registered': false,
259
	 * 				'type': 1
260
	 * 			],
261
	 *  .....
262
	 *
263
	 * @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
264
	 */
265 1
	public function restList() {
266
		try {
267 1
			$this->response = $this->getcurlpage($this->restUrl.'ipl/list.json?token=' . $this->authToken());
268
		} catch (\Exception $e) {
269
			$this->log('error', 'Caught exception code: ' . $e->getCode());
270
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
271
			return false;
272
		}
273 1
		return json_decode($this->response, true);
274
	}
275
276
	/**
277
	 * Return list of all IP licenses owned by authorized user
278
	 *
279
	 * @throws XmlRpcException for critical errors
280
	 * @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
281
	 */
282 1 View Code Duplication
	public function reconcile() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
283 1
		$xmlClient = $this->xmlClient;
284
		try {
285 1
			$this->response = $xmlClient->reconcile($this->authToken());
286
		} catch (\Exception $e) {
287
			$this->log('error', 'Caught exception code: ' . $e->getCode());
288
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
289
			return false;
290
		}
291 1
		return $this->response;
292
	}
293
294
	/**
295
	 * alias function to get a list of licenses
296
	 *
297
	 * @return false|array
298
	 */
299 1
	public function licenseList() {
300 1
		if ($this->apiType == 'rest')
301 1
			return $this->restList();
302
		else
303
			return $this->reconcile();
304
	}
305
306
	/**
307
	 * Register new IP license.
308
	 *
309
	 * @param string $ipAddress IP Address
310
	 * @param integer $type license type (1,2 or 16)
311
	 * @throws XmlRpcException for critical errors
312
	 * @return false|integer 0 on success, -1 on error
313
	 */
314
	public function license($ipAddress, $type) {
315
		$type = (int)$type;
316
		$xmlClient = $this->xmlClient;
317
		try {
318
			$this->log('error', 'Calling License(' . $this->authToken() . ',' . $ipAddress . ',' . $type . ')');
319
			$this->response = $xmlClient->license($this->authToken(), $ipAddress, $type);
320
			$this->log('error', 'Response: ' . var_export($this->response, true));
321
			return $this->response;
322
		} catch (\Exception $e) {
323
			$this->log('error', 'Caught exception code: ' . $e->getCode());
324
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
325
			return false;
326
		}
327
	}
328
}
329
330