Completed
Push — master ( 3810fc...e58c12 )
by Joe
02:55
created

Cloudlinux::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 6
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 || $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 || $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
		try {
81 1
			return $this->login . '|' . $time . '|' . sha1($this->key . $time);
82
		} catch (\Exception $e) {
83
			$this->log('error', 'Caught exception code: ' . $e->getCode());
84
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
85
			return false;
86
		}
87
	}
88
89
	/**
90
	 * getcurlpage()
91
	 * gets a webpage via curl and returns the response.
92
	 * also it sets a mozilla type agent.
93
	 * @param string $url        the url of the page you want
94
	 * @param string $postfields postfields in the format of "v1=10&v2=20&v3=30"
95
	 * @param string|array $options
96
	 * @return string the webpage
97
	 */
98 1
	public function getcurlpage($url, $postfields = '', $options = '') {
99 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';
100 1
		$curl = curl_init($url);
101 1
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
102 1
		curl_setopt($curl, CURLOPT_USERAGENT, $agent);
103 1
		if (is_array($postfields) || $postfields != '') {
104
			if (is_array($postfields)) {
105
				$postdata = [];
106
				foreach ($postfields as $field => $value) {
107
					$postdata[] = $field . '=' . urlencode($value);
108
				}
109
				curl_setopt($curl, CURLOPT_POSTFIELDS, implode('&', $postdata));
110
			} else {
111
				curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
112
			}
113
		}
114 1
		if (is_array($options) && sizeof($options) > 0) {
115
			foreach ($options as $key => $value) {
116
				curl_setopt($curl, $key, $value);
117
			}
118
		}
119 1
		$tmp = curl_exec($curl);
120 1
		curl_close($curl);
121 1
		$ret = $tmp;
122 1
		return $ret;
123
	}
124
125
	public function log($level, $text, $line = '', $file = '') {
126
		if (function_exists('myadmin_log'))
127
			myadmin_log('cloudlinux', $level, $text, $line, $file);
128
		else
129
			error_log($text);
130
	}
131
132
	/**
133
	 * Return system information about several Cloudlinux services
134
	 *
135
	 * @return array array of system information
136
	 */
137 1
	public function status() {
138 1
		$this->response = $this->getcurlpage($this->restUrl.'status.json', '', $this->restOptions);
139 1
		return json_decode($this->response, true);
140
	}
141
142
	/**
143
	 * Will return information about what kind of license types are available for registration and what types are already used by current account.
144
	 * @param string $ipAddress ip address to check
145
	 * @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
146
	 */
147 1
	public function availability($ipAddress) {
148 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/availability.json?ip='.$ipAddress.'&token='.$this->authToken(), '', $this->restOptions);
149 1
		return json_decode($this->response, true);
150
	}
151
152
	/**
153
	 * Check if IP license is registered by any customer.
154
	 *
155
	 * @param string $ipAddress ip address to check
156
	 * @return false|array Will return list of registered license types or empty list if provided IP is not registered yet.
157
	 */
158 1
	public function check($ipAddress) {
159 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/check.json?ip='.$ipAddress.'&token='.$this->authToken(), '', $this->restOptions);
160 1
		$response = json_decode($this->response, true);
161 1
		if ($response['success'] == 1)
162 1
			return $response['data'];
163
		else
164
			return false;
165
	}
166
167
	/**
168
	 * Check if IP license was registered by any customer. Arguments:
169
	 *
170
	 * @param string $ipAddress ip address to remove
171
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
172
	 * @throws XmlRpcException for critical errors
173
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
174
	 */
175 1
	public function isLicensed($ipAddress, $checkAll = true) {
176 1
		if ($this->apiType == 'xml')
177
			return $this->xmlIsLicensed($ipAddress, $checkAll);
178
		else
179 1
			return $this->check($ipAddress);
180
	}
181
	/**
182
	 * Check if IP license was registered by any customer. Arguments:
183
	 *
184
	 * @throws XmlRpcException for critical errors
185
	 * @param string $ipAddress ip address to remove
186
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
187
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
188
	 */
189 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...
190 1
		$xmlClient = $this->xmlClient;
191
		try {
192 1
			$this->response = $xmlClient->is_licensed($this->authToken(), $ipAddress, $checkAll);
193
		} catch (\Exception $e) {
194
			$this->log('error', 'Caught exception code: ' . $e->getCode());
195
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
196
			return false;
197
		}
198 1
		return $this->response;
199
	}
200
201
	/**
202
	 * Will register IP based license for authorized user.
203
	 *
204
	 * @param string $ipAddress ip address to registger
205
	 * @param int $type IP license type (1,2 or 16)
206
	 * @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
207
	 */
208
	public function register($ipAddress, $type) {
209
		$this->response = $this->getcurlpage($this->restUrl.'ipl/register.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken(), '', $this->restOptions);
210
		return json_decode($this->response, true);
211
	}
212
213
	/**
214
	 * Will remove IP based license from authorized user licenses.
215
	 *
216
	 * @param string $ipAddress ip address to remove licenses on
217
	 * @param int $type optional license type. If empty, will remove licenses with all types
218
	 * @return bool
219
	 */
220
	public function restRemove($ipAddress, $type = 0) {
221
		if ($type != 0)
222
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken(), '', $this->restOptions);
223
		else
224
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&token='.$this->authToken(), '', $this->restOptions);
225
		return json_decode($this->response, true);
226
	}
227
228
	/**
229
	 * Will remove IP based license from authorized user licenses.
230
	 *
231
	 * @param string $ipAddress ip address to remove licenses on
232
	 * @param int $type optional license type. If empty or 0, will remove licenses with all types
233
	 * @return bool|int
234
	 */
235
	public function remove($ipAddress, $type = 0) {
236
		if ($this->apiType == 'xml')
237
			return $this->removeLicense($ipAddress, $type);
238
		else
239
			return $this->restRemove($ipAddress, $type);
240
	}
241
242
	/**
243
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
244
	 * or
245
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
246
	 * @param string         $ipAddress   ip address to remove
247
	 * @param int $type optional parameter to specify the type of license to remove (1,2, or 16) or 0 for all
248
	 * @return bool|int 0 on success, -1 on error, Error will be returned also if account have no licenses for provided IP.
249
	 */
250
	public function removeLicense($ipAddress, $type = 0) {
251
		$this->log('info', "Calling CLoudLinux->xmlClient->removeLicense({$this->authToken()}, {$ipAddress}, {$type})", __LINE__, __FILE__);
252
		try {
253
			$this->response = $this->remove($ipAddress, $type);
254
		} catch (\Exception $e) {
255
			$this->log('error', 'Caught exception code: ' . $e->getCode());
256
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
257
			return false;
258
		}
259
		return $this->response;
260
	}
261
262
	/**
263
	 * Return all IP licenses owned by authorized user.
264
	 *
265
	 * The normal response will look something like:
266
	 * 	[
267
	 * 		'success': true,
268
	 * 		'data': [
269
	 * 			[
270
	 * 				'created': '2017-05-05T16:19-0400',
271
	 * 				'ip': '66.45.240.186',
272
	 * 				'registered': true,
273
	 * 				'type': 1
274
	 * 			], [
275
	 * 				'created': '2016-10-14T10:42-0400',
276
	 * 				'ip': '131.153.38.228',
277
	 * 				'registered': false,
278
	 * 				'type': 1
279
	 * 			],
280
	 *  .....
281
	 *
282
	 * @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
283
	 */
284 1
	public function restList() {
285
		try {
286 1
			$this->response = $this->getcurlpage($this->restUrl.'ipl/list.json?token=' . $this->authToken(), '', $this->restOptions);
287
		} catch (\Exception $e) {
288
			$this->log('error', 'Caught exception code: ' . $e->getCode());
289
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
290
			return false;
291
		}
292 1
		return json_decode($this->response, true);
293
	}
294
295
	/**
296
	 * alias function to get a list of licenses
297
	 *
298
	 * @return false|array
299
	 */
300 1
	public function licenseList() {
301 1
		return $this->restList();
302
	}
303
304
	/**
305
	 * Return list of all IP licenses owned by authorized user
306
	 *
307
	 * @throws XmlRpcException for critical errors
308
	 * @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
309
	 */
310 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...
311
		$xmlClient = $this->xmlClient;
312
		try {
313
			$this->response = $xmlClient->reconcile($this->authToken());
314
		} catch (\Exception $e) {
315
			$this->log('error', 'Caught exception code: ' . $e->getCode());
316
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
317
			return false;
318
		}
319
		return $this->response;
320
	}
321
322
	/**
323
	 * Register new IP license.
324
	 *
325
	 * @param string $ipAddress IP Address
326
	 * @param integer $type license type (1,2 or 16)
327
	 * @throws XmlRpcException for critical errors
328
	 * @return false|integer 0 on success, -1 on error
329
	 */
330
	public function license($ipAddress, $type) {
331
		$type = (int)$type;
332
		$xmlClient = $this->xmlClient;
333
		try {
334
			$this->log('error', 'Calling License(' . $this->authToken() . ',' . $ipAddress . ',' . $type . ')');
335
			$this->response = $xmlClient->license($this->authToken(), $ipAddress, $type);
336
			$this->log('error', 'Response: ' . var_export($this->response, true));
337
			return $this->response;
338
		} catch (\Exception $e) {
339
			$this->log('error', 'Caught exception code: ' . $e->getCode());
340
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
341
			return false;
342
		}
343
	}
344
}
345
346