Completed
Push — master ( 13d70e...d73743 )
by Joe
02:47
created

Cloudlinux::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 13
nc 4
nop 3
crap 5
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
	 * @return string the webpage
89
	 */
90 1
	public function getcurlpage($url) {
91 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';
92 1
		$curl = curl_init($url);
93 1
		$options = $this->restOptions;
94 1
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
95 1
		curl_setopt($curl, CURLOPT_USERAGENT, $agent);
96 1
		if (is_array($options) && sizeof($options) > 0)
97 1
			foreach ($options as $key => $value)
98 1
				curl_setopt($curl, $key, $value);
99 1
		$return = curl_exec($curl);
100 1
		curl_close($curl);
101 1
		return $return;
102
	}
103
104
	public function log($level, $text, $line = '', $file = '') {
105
		if (function_exists('myadmin_log'))
106
			myadmin_log('cloudlinux', $level, $text, $line, $file);
107
		else
108
			error_log($text);
109
	}
110
111
	/**
112
	 * Return system information about several Cloudlinux services
113
	 *
114
	 * @return array array of system information
115
	 */
116 1
	public function status() {
117 1
		$this->response = $this->getcurlpage($this->restUrl.'status.json');
118 1
		return json_decode($this->response, true);
119
	}
120
121
	/**
122
	 * Will return information about what kind of license types are available for registration and what types are already used by current account.
123
	 * @param string $ipAddress ip address to check
124
	 * @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
125
	 */
126 1
	public function availability($ipAddress) {
127 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/availability.json?ip='.$ipAddress.'&token='.$this->authToken());
128 1
		return json_decode($this->response, true);
129
	}
130
131
	/**
132
	 * Check if IP license is registered by any customer.
133
	 *
134
	 * @param string $ipAddress ip address to check
135
	 * @return false|array Will return list of registered license types or empty list if provided IP is not registered yet.
136
	 */
137 1
	public function check($ipAddress) {
138 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/check.json?ip='.$ipAddress.'&token='.$this->authToken());
139 1
		$response = json_decode($this->response, true);
140 1
		if ($response['success'] == 1)
141 1
			return $response['data'];
142
		else
143
			return false;
144
	}
145
146
	/**
147
	 * Check if IP license was registered by any customer. Arguments:
148
	 *
149
	 * @param string $ipAddress ip address to remove
150
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
151
	 * @throws XmlRpcException for critical errors
152
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
153
	 */
154 1
	public function isLicensed($ipAddress, $checkAll = true) {
155 1
		if ($this->apiType == 'xml')
156
			return $this->xmlIsLicensed($ipAddress, $checkAll);
157
		else
158 1
			return $this->check($ipAddress);
159
	}
160
	/**
161
	 * Check if IP license was registered by any customer. Arguments:
162
	 *
163
	 * @throws XmlRpcException for critical errors
164
	 * @param string $ipAddress ip address to remove
165
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
166
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
167
	 */
168 2 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...
169 2
		$xmlClient = $this->xmlClient;
170
		try {
171 2
			$this->response = $xmlClient->is_licensed($this->authToken(), $ipAddress, $checkAll);
172 1
		} catch (\Exception $e) {
173 1
			$this->log('error', 'Caught exception code: ' . $e->getCode());
174 1
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
175 1
			return false;
176
		}
177 1
		return $this->response;
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 1
	public function register($ipAddress, $type) {
188 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/register.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken());
189 1
		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 1
	public function restRemove($ipAddress, $type = 0) {
200 1
		if ($type != 0)
201
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken());
202
		else
203 1
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&token='.$this->authToken());
204 1
		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 1
	public function remove($ipAddress, $type = 0) {
215 1
		if ($this->apiType == 'xml')
216
			return $this->removeLicense($ipAddress, $type);
217
		else
218 1
			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 1
	public function removeLicense($ipAddress, $type = 0) {
230 1
		$this->log('info', "Calling CLoudLinux->xmlClient->removeLicense({$this->authToken()}, {$ipAddress}, {$type})", __LINE__, __FILE__);
231
		try {
232 1
			$this->response = $this->remove($ipAddress, $type);
233
		} catch (\Exception $e) {
234
			$this->log('error', 'Caught exception code: ' . $e->getCode());
235
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
236
			return false;
237
		}
238 1
		return $this->response;
239
	}
240
241
	/**
242
	 * Return all IP licenses owned by authorized user.
243
	 *
244
	 * The normal response will look something like:
245
	 * 	[
246
	 * 		'success': true,
247
	 * 		'data': [
248
	 * 			[
249
	 * 				'created': '2017-05-05T16:19-0400',
250
	 * 				'ip': '66.45.240.186',
251
	 * 				'registered': true,
252
	 * 				'type': 1
253
	 * 			], [
254
	 * 				'created': '2016-10-14T10:42-0400',
255
	 * 				'ip': '131.153.38.228',
256
	 * 				'registered': false,
257
	 * 				'type': 1
258
	 * 			],
259
	 *  .....
260
	 *
261
	 * @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
262
	 */
263 1
	public function restList() {
264
		try {
265 1
			$this->response = $this->getcurlpage($this->restUrl.'ipl/list.json?token=' . $this->authToken());
266
		} catch (\Exception $e) {
267
			$this->log('error', 'Caught exception code: ' . $e->getCode());
268
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
269
			return false;
270
		}
271 1
		return json_decode($this->response, true);
272
	}
273
274
	/**
275
	 * Return list of all IP licenses owned by authorized user
276
	 *
277
	 * @throws XmlRpcException for critical errors
278
	 * @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
279
	 */
280 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...
281 1
		$xmlClient = $this->xmlClient;
282
		try {
283 1
			$this->response = $xmlClient->reconcile($this->authToken());
284
		} catch (\Exception $e) {
285
			$this->log('error', 'Caught exception code: ' . $e->getCode());
286
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
287
			return false;
288
		}
289 1
		return $this->response;
290
	}
291
292
	/**
293
	 * alias function to get a list of licenses
294
	 *
295
	 * @return false|array
296
	 */
297 1
	public function licenseList() {
298 1
		if ($this->apiType == 'rest')
299 1
			return $this->restList();
300
		else
301
			return $this->reconcile();
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 1
	public function license($ipAddress, $type) {
313 1
		$type = (int)$type;
314 1
		$xmlClient = $this->xmlClient;
315
		try {
316 1
			$this->log('error', 'Calling License(' . $this->authToken() . ',' . $ipAddress . ',' . $type . ')');
317 1
			$this->response = $xmlClient->license($this->authToken(), $ipAddress, $type);
318
			$this->log('error', 'Response: ' . var_export($this->response, true));
319
			return $this->response;
320 1
		} catch (\Exception $e) {
321 1
			$this->log('error', 'Caught exception code: ' . $e->getCode());
322 1
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
323 1
			return false;
324
		}
325
	}
326
}
327
328