Completed
Branch master (825399)
by Joe
04:27 queued 02:00
created

Cloudlinux::availability()   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 1
crap 2
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
	 * getcurlpage()
75
	 * gets a webpage via curl and returns the response.
76
	 * also it sets a mozilla type agent.
77
	 * @param string $url        the url of the page you want
78
	 * @param string $postfields postfields in the format of "v1=10&v2=20&v3=30"
79
	 * @param string|array $options
80
	 * @return string the webpage
81
	 */
82 1
	public function getcurlpage($url, $postfields = '', $options = '') {
83 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';
84 1
		$curl = curl_init($url);
85 1
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
86 1
		curl_setopt($curl, CURLOPT_USERAGENT, $agent);
87 1
		if (is_array($postfields) || $postfields != '') {
88
			if (is_array($postfields)) {
89
				$postdata = [];
90
				foreach ($postfields as $field => $value) {
91
					$postdata[] = $field . '=' . urlencode($value);
92
				}
93
				curl_setopt($curl, CURLOPT_POSTFIELDS, implode('&', $postdata));
94
			} else {
95
				curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
96
			}
97
		}
98 1
		if (is_array($options) && sizeof($options) > 0) {
99
			foreach ($options as $key => $value) {
100
				curl_setopt($curl, $key, $value);
101
			}
102
		}
103 1
		$tmp = curl_exec($curl);
104 1
		curl_close($curl);
105 1
		$ret = $tmp;
106 1
		return $ret;
107
	}
108
109
	public function log($level, $text, $line = '', $file = '') {
110
		if (function_exists('myadmin_log'))
111
			myadmin_log('cloudlinux', $level, $text, $line, $file);
112
		else
113
			error_log($text);
114
	}
115
116
	/**
117
	 * Return system information about several Cloudlinux services
118
	 *
119
	 * @return array array of system information
120
	 */
121 1
	public function status() {
122 1
		$this->response = $this->getcurlpage($this->restUrl.'status.json', '', $this->restOptions);
123 1
		return json_decode($this->response, true);
124
	}
125
126
	/**
127
	 * Will return information about what kind of license types are available for registration and what types are already used by current account.
128
	 * @param string $ipAddress ip address to check
129
	 * @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
130
	 */
131
	public function availability($ipAddress) {
132
		$this->response = $this->getcurlpage($this->restUrl.'ipl/availability.json?ip='.$ipAddress.'&token='.$this->authToken(), '', $this->restOptions);
133
		return json_decode($this->response, true);
134
	}
135
136
	/**
137
	 * Check if IP license is registered by any customer.
138
	 *
139
	 * @param string $ipAddress ip address to check
140
	 * @return false|array Will return list of registered license types or empty list if provided IP is not registered yet.
141
	 */
142
	public function check($ipAddress) {
143
		$this->response = $this->getcurlpage($this->restUrl.'ipl/check.json?ip='.$ipAddress.'&token='.$this->authToken(), '', $this->restOptions);
144
		$response = json_decode($this->response, true);
145
		if ($response['success'] == 1)
146
			return $response['data'];
147
		else
148
			return false;
149
	}
150
151
	/**
152
	 * Will register IP based license for authorized user.
153
	 *
154
	 * @param string $ipAddress ip address to registger
155
	 * @param int $type IP license type (1,2 or 16)
156
	 * @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
157
	 */
158
	public function register($ipAddress, $type) {
159
		$this->response = $this->getcurlpage($this->restUrl.'ipl/register.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken(), '', $this->restOptions);
160
		return json_decode($this->response, true);
161
	}
162
163
	/**
164
	 * Will remove IP based license from authorized user licenses.
165
	 *
166
	 * @param string $ipAddress ip address to remove licenses on
167
	 * @param int $type optional license type. If empty, will remove licenses with all types
168
	 * @return bool
169
	 */
170
	public function restRemove($ipAddress, $type = 0) {
171
		if ($type != 0)
172
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken(), '', $this->restOptions);
173
		else
174
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&token='.$this->authToken(), '', $this->restOptions);
175
		return json_decode($this->response, true);
176
	}
177
178
	/**
179
	 * Will remove IP based license from authorized user licenses.
180
	 *
181
	 * @param string $ipAddress ip address to remove licenses on
182
	 * @param int $type optional license type. If empty or 0, will remove licenses with all types
183
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
184
	 */
185
	public function remove($ipAddress, $type = 0) {
186
		if ($this->apiType == 'xml')
187
			return $this->removeLicense($ipAddress, $type);
188
		else
189
			return $this->restRemove($ipAddress, $type);
190
	}
191
192
	/**
193
	 * Return all IP licenses owned by authorized user.
194
	 *
195
	 * @return 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
196
	 */
197
	public function restList() {
198
		$this->response = $this->getcurlpage($this->restUrl.'ipl/list.json?token='.$this->authToken(), '', $this->restOptions);
199
		return json_decode($this->response, true);
200
	}
201
202
	/**
203
	 * automatic authToken generator
204
	 *
205
	 * @return false|string the authToken
206
	 */
207
	public function authToken() {
208
		$time = time();
209
		try {
210
			return $this->login . '|' . $time . '|' . sha1($this->key . $time);
211
		} catch (\Exception $e) {
212
			$this->log('error', 'Caught exception code: ' . $e->getCode());
213
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
214
			return false;
215
		}
216
	}
217
218
	/**
219
	 * Register new IP license.
220
	 *
221
	 * @param string $ipAddress IP Address
222
	 * @param integer $type license type (1,2 or 16)
223
	 * @throws XmlRpcException for critical errors
224
	 * @return false|integer 0 on success, -1 on error
225
	 */
226
	public function license($ipAddress, $type) {
227
		$type = (int)$type;
228
		$xmlClient = $this->xmlClient;
229
		try {
230
			$this->log('error', 'Calling License(' . $this->authToken() . ',' . $ipAddress . ',' . $type . ')');
231
			$this->response = $xmlClient->license($this->authToken(), $ipAddress, $type);
232
			$this->log('error', 'Response: ' . var_export($this->response, true));
233
			return $this->response;
234
		} catch (\Exception $e) {
235
			$this->log('error', 'Caught exception code: ' . $e->getCode());
236
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
237
			return false;
238
		}
239
	}
240
241
	/**
242
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
243
	 * or
244
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
245
	 * @param string         $ipAddress   ip address to remove
246
	 * @param int $type optional parameter to specify the type of license to remove (1,2, or 16) or 0 for all
247
	 * @return bool|int 0 on success, -1 on error, Error will be returned also if account have no licenses for provided IP.
248
	 */
249
	public function removeLicense($ipAddress, $type = 0) {
250
		$this->log('info', "Calling CLoudLinux->xmlClient->removeLicense({$this->authToken()}, {$ipAddress}, {$type})", __LINE__, __FILE__);
251
		try {
252
			$this->response = $this->remove($ipAddress, $type);
253
		} catch (\Exception $e) {
254
			$this->log('error', 'Caught exception code: ' . $e->getCode());
255
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
256
			return false;
257
		}
258
		return $this->response;
259
	}
260
261
	/**
262
	 * Check if IP license was registered by any customer. Arguments:
263
	 *
264
	 * @param string $ipAddress ip address to remove
265
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
266
	 * @throws XmlRpcException for critical errors
267
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
268
	 */
269
	public function isLicensed($ipAddress, $checkAll = true) {
270
		if ($this->apiType == 'xml')
271
			return $this->xmlIsLicensed($ipAddress, $checkAll);
272
		else
273
			return $this->check($ipAddress);
274
	}
275
	/**
276
	 * Check if IP license was registered by any customer. Arguments:
277
	 *
278
	 * @throws XmlRpcException for critical errors
279
	 * @param string $ipAddress ip address to remove
280
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
281
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
282
	 */
283 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...
284
		$xmlClient = $this->xmlClient;
285
		try {
286
			$this->response = $xmlClient->isLicensed($this->authToken(), $ipAddress, $checkAll);
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
		return $this->response;
293
	}
294
295
	/**
296
	 * @return false|array
297
	 */
298
	public function licenseList() {
299
		try {
300
			return json_decode($this->getcurlpage($this->restUrl.'ipl/list.json?token=' . $this->authToken()));
301
		} catch (\Exception $e) {
302
			$this->log('error', 'Caught exception code: ' . $e->getCode());
303
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
304
			return false;
305
		}
306
	}
307
308
	/**
309
	 * Return list of all IP licenses owned by authorized user
310
	 *
311
	 * @throws XmlRpcException for critical errors
312
	 * @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
313
	 */
314 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...
315
		$xmlClient = $this->xmlClient;
316
		try {
317
			$this->response = $xmlClient->reconcile($this->authToken());
318
		} catch (\Exception $e) {
319
			$this->log('error', 'Caught exception code: ' . $e->getCode());
320
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
321
			return false;
322
		}
323
		return $this->reseponse;
0 ignored issues
show
Bug introduced by
The property reseponse does not seem to exist. Did you mean response?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
324
	}
325
}
326
327