Completed
Push — master ( 314972...b6fe6c )
by Joe
02:47
created

Cloudlinux::license()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 3
nop 2
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
	/**
45
	 * @var \XML_RPC2_Client
46
	 */
47
	public $xmlClient;
48
	public $response;
49
50
	/**
51
	 * Cloudlinux::__construct()
52
	 *
53
	 * @param string $login API Login Name
54
	 * @param string $key API Key
55
	 * @param string $apiType API type to use, can be 'rest' or 'xml'
56
	 */
57 1
	public function __construct($login, $key, $apiType = 'rest') {
58 1
		$this->login = $login;
59 1
		$this->key = $key;
60 1
		$this->apiType = $apiType;
61 1
		$limitType = false;
62 1
		if ($limitType === false || $this->apiType == 'xml') {
63 1
			include_once('XML/RPC2/Client.php');
64 1
			$this->xmlOptions['prefix'] = $this->prefix;
65 1
			$this->xmlOptions['encoding'] = $this->encoding;
66 1
			$this->xmlOptions['sslverify'] = $this->sslverify;
67 1
			$this->xmlClient = \XML_RPC2_Client::create($this->xmlUrl, $this->xmlOptions);
68
		}
69 1
		if ($limitType === false || $this->apiType == 'rest') {
70 1
			$this->restOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslverify;
71
		}
72 1
	}
73
74
	/**
75
	 * automatic authToken generator
76
	 *
77
	 * @return false|string the authToken
78
	 */
79 1
	public function authToken() {
80 1
		$time = time();
81 1
		return $this->login . '|' . $time . '|' . sha1($this->key . $time);
82
	}
83
84
	/**
85
	 * getcurlpage()
86
	 * gets a webpage via curl and returns the response.
87
	 * also it sets a mozilla type agent.
88
	 * @param string $url        the url of the page you want
89
	 * @return string the webpage
90
	 */
91 1
	public function getcurlpage($url) {
92 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';
93 1
		$curl = curl_init($url);
94 1
		$options = $this->restOptions;
95 1
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
96 1
		curl_setopt($curl, CURLOPT_USERAGENT, $agent);
97 1
		if (is_array($options) && sizeof($options) > 0)
98 1
			foreach ($options as $key => $value)
99 1
				curl_setopt($curl, $key, $value);
100 1
		$return = curl_exec($curl);
101 1
		curl_close($curl);
102 1
		return $return;
103
	}
104
105 1
	public function log($level, $text, $line = '', $file = '') {
106 1
		if (function_exists('myadmin_log'))
107 1
			myadmin_log('cloudlinux', $level, $text, $line, $file);
108 1
	}
109
110
	/**
111
	 * Return system information about several Cloudlinux services
112
	 *
113
	 * @return array array of system information
114
	 */
115 1
	public function status() {
116 1
		$this->response = $this->getcurlpage($this->restUrl.'status.json');
117 1
		return json_decode($this->response, true);
118
	}
119
120
	/**
121
	 * Will return information about what kind of license types are available for registration and what types are already used by current account.
122
	 * @param string $ipAddress ip address to check
123
	 * @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
124
	 */
125 1
	public function availability($ipAddress) {
126 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/availability.json?ip='.$ipAddress.'&token='.$this->authToken());
127 1
		return json_decode($this->response, true);
128
	}
129
130
	/**
131
	 * Check if IP license is registered by any customer.
132
	 *
133
	 * @param string $ipAddress ip address to check
134
	 * @return false|array Will return list of registered license types or empty list if provided IP is not registered yet.
135
	 */
136 1
	public function check($ipAddress) {
137 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/check.json?ip='.$ipAddress.'&token='.$this->authToken());
138 1
		$response = json_decode($this->response, true);
139 1
		if ($response['success'] == 1)
140 1
			return $response['data'];
141
		else
142 1
			return false;
143
	}
144
145
	/**
146
	 * Check if IP license was registered by any customer. Arguments:
147
	 *
148
	 * @param string $ipAddress ip address to remove
149
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
150
	 * @throws XmlRpcException for critical errors
151
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
152
	 */
153 1
	public function isLicensed($ipAddress, $checkAll = true) {
154 1
		if ($this->apiType == 'xml')
155 1
			return $this->xmlIsLicensed($ipAddress, $checkAll);
156
		else
157 1
			return $this->check($ipAddress);
158
	}
159
	/**
160
	 * Check if IP license was registered by any customer. Arguments:
161
	 *
162
	 * @throws XmlRpcException for critical errors
163
	 * @param string $ipAddress ip address to remove
164
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
165
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
166
	 */
167 2
	public function xmlIsLicensed($ipAddress, $checkAll = true) {
168 2
		$xmlClient = $this->xmlClient;
169
		try {
170 2
			return $this->response = $xmlClient->is_licensed($this->authToken(), $ipAddress, $checkAll);
171 1
		} catch (\Exception $e) {
172 1
			$this->log('error', 'Caught exception code: ' . $e->getCode());
173 1
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
174 1
			return false;
175
		}
176
	}
177
178
	/**
179
	 * Will register IP based license for authorized user.
180
	 *
181
	 * @param string $ipAddress ip address to registger
182
	 * @param int $type IP license type (1,2 or 16)
183
	 * @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
184
	 */
185 1
	public function register($ipAddress, $type) {
186 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/register.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken());
187 1
		return json_decode($this->response, true);
188
	}
189
190
	/**
191
	 * Will remove IP based license from authorized user licenses.
192
	 *
193
	 * @param string $ipAddress ip address to remove licenses on
194
	 * @param int $type optional license type. If empty, will remove licenses with all types
195
	 * @return bool
196
	 */
197 1
	public function restRemove($ipAddress, $type = 0) {
198 1
		if ($type != 0)
199 1
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken());
200
		else
201 1
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&token='.$this->authToken());
202 1
		return json_decode($this->response, true);
203
	}
204
205
	/**
206
	 * Will remove IP based license from authorized user licenses.
207
	 *
208
	 * @param string $ipAddress ip address to remove licenses on
209
	 * @param int $type optional license type. If empty or 0, will remove licenses with all types
210
	 * @return bool|int
211
	 */
212 1
	public function remove($ipAddress, $type = 0) {
213 1
		if ($this->apiType == 'xml')
214 1
			return $this->removeLicense($ipAddress, $type);
215
		else
216 1
			return $this->restRemove($ipAddress, $type);
217
	}
218
219
	/**
220
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
221
	 * or
222
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
223
	 * @param string         $ipAddress   ip address to remove
224
	 * @param int $type optional parameter to specify the type of license to remove (1,2, or 16) or 0 for all
225
	 * @return bool|int 0 on success, -1 on error, Error will be returned also if account have no licenses for provided IP.
226
	 */
227 1
	public function removeLicense($ipAddress, $type = 0) {
228 1
		$this->log('info', "Calling CLoudLinux->xmlClient->removeLicense({$this->authToken()}, {$ipAddress}, {$type})", __LINE__, __FILE__);
229
		try {
230 1
			return $this->response = $this->xmlClient->remove_license($this->authToken(), $ipAddress, $type);
231 1
		} catch (\Exception $e) {
232 1
			$this->log('error', 'Caught exception code: ' . $e->getCode());
233 1
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
234 1
			return false;
235
		}
236
	}
237
238
	/**
239
	 * Return all IP licenses owned by authorized user.
240
	 *
241
	 * The normal response will look something like:
242
	 * 	[
243
	 * 		'success': true,
244
	 * 		'data': [
245
	 * 			[
246
	 * 				'created': '2017-05-05T16:19-0400',
247
	 * 				'ip': '66.45.240.186',
248
	 * 				'registered': true,
249
	 * 				'type': 1
250
	 * 			], [
251
	 * 				'created': '2016-10-14T10:42-0400',
252
	 * 				'ip': '131.153.38.228',
253
	 * 				'registered': false,
254
	 * 				'type': 1
255
	 * 			],
256
	 *  .....
257
	 *
258
	 * @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
259
	 */
260 1
	public function restList() {
261 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/list.json?token=' . $this->authToken());
262 1
		return json_decode($this->response, true);
263
	}
264
265
	/**
266
	 * Return list of all IP licenses owned by authorized user
267
	 *
268
	 * @throws XmlRpcException for critical errors
269
	 * @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
270
	 */
271 1
	public function reconcile() {
272 1
		$this->response = $this->xmlClient->reconcile($this->authToken());
273 1
		return $this->response;
274
	}
275
276
	/**
277
	 * alias function to get a list of licenses
278
	 *
279
	 * @return false|array
280
	 */
281 1
	public function licenseList() {
282 1
		if ($this->apiType == 'rest')
283 1
			return $this->restList();
284
		else
285 1
			return $this->reconcile();
286
	}
287
288
	/**
289
	 * Register new IP license.
290
	 *
291
	 * @param string $ipAddress IP Address
292
	 * @param integer $type license type (1,2 or 16)
293
	 * @throws XmlRpcException for critical errors
294
	 * @return false|integer 0 on success, -1 on error
295
	 */
296 1
	public function license($ipAddress, $type) {
297 1
		$type = (int)$type;
298 1
		$xmlClient = $this->xmlClient;
299
		try {
300 1
			$this->log('error', 'Calling License(' . $this->authToken() . ',' . $ipAddress . ',' . $type . ')');
301 1
			return $this->response = $xmlClient->license($this->authToken(), $ipAddress, $type);
302 1
		} catch (\Exception $e) {
303 1
			$this->log('error', 'Caught exception code: ' . $e->getCode());
304 1
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
305 1
			return false;
306
		}
307
	}
308
}
309
310