Completed
Push — master ( 7fb371...314972 )
by Joe
02:54
created

Cloudlinux::status()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
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
		else
109
			error_log($text);
110 1
	}
111
112
	/**
113
	 * Return system information about several Cloudlinux services
114
	 *
115
	 * @return array array of system information
116
	 */
117 1
	public function status() {
118 1
		$this->response = $this->getcurlpage($this->restUrl.'status.json');
119 1
		return json_decode($this->response, true);
120
	}
121
122
	/**
123
	 * Will return information about what kind of license types are available for registration and what types are already used by current account.
124
	 * @param string $ipAddress ip address to check
125
	 * @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
126
	 */
127 1
	public function availability($ipAddress) {
128 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/availability.json?ip='.$ipAddress.'&token='.$this->authToken());
129 1
		return json_decode($this->response, true);
130
	}
131
132
	/**
133
	 * Check if IP license is registered by any customer.
134
	 *
135
	 * @param string $ipAddress ip address to check
136
	 * @return false|array Will return list of registered license types or empty list if provided IP is not registered yet.
137
	 */
138 1
	public function check($ipAddress) {
139 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/check.json?ip='.$ipAddress.'&token='.$this->authToken());
140 1
		$response = json_decode($this->response, true);
141 1
		if ($response['success'] == 1)
142 1
			return $response['data'];
143
		else
144 1
			return false;
145
	}
146
147
	/**
148
	 * Check if IP license was registered by any customer. Arguments:
149
	 *
150
	 * @param string $ipAddress ip address to remove
151
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
152
	 * @throws XmlRpcException for critical errors
153
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
154
	 */
155 1
	public function isLicensed($ipAddress, $checkAll = true) {
156 1
		if ($this->apiType == 'xml')
157 1
			return $this->xmlIsLicensed($ipAddress, $checkAll);
158
		else
159 1
			return $this->check($ipAddress);
160
	}
161
	/**
162
	 * Check if IP license was registered by any customer. Arguments:
163
	 *
164
	 * @throws XmlRpcException for critical errors
165
	 * @param string $ipAddress ip address to remove
166
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
167
	 * @return false|array (list<int>): List of registered license types or empty list if no license found
168
	 */
169 2
	public function xmlIsLicensed($ipAddress, $checkAll = true) {
170 2
		$xmlClient = $this->xmlClient;
171
		try {
172 2
			$this->response = $xmlClient->is_licensed($this->authToken(), $ipAddress, $checkAll);
173 1
		} catch (\Exception $e) {
174 1
			$this->log('error', 'Caught exception code: ' . $e->getCode());
175 1
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
176 1
			return false;
177
		}
178 1
		return $this->response;
179
	}
180
181
	/**
182
	 * Will register IP based license for authorized user.
183
	 *
184
	 * @param string $ipAddress ip address to registger
185
	 * @param int $type IP license type (1,2 or 16)
186
	 * @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
187
	 */
188 1
	public function register($ipAddress, $type) {
189 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/register.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken());
190 1
		return json_decode($this->response, true);
191
	}
192
193
	/**
194
	 * Will remove IP based license from authorized user licenses.
195
	 *
196
	 * @param string $ipAddress ip address to remove licenses on
197
	 * @param int $type optional license type. If empty, will remove licenses with all types
198
	 * @return bool
199
	 */
200 1
	public function restRemove($ipAddress, $type = 0) {
201 1
		if ($type != 0)
202 1
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken());
203
		else
204 1
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&token='.$this->authToken());
205 1
		return json_decode($this->response, true);
206
	}
207
208
	/**
209
	 * Will remove IP based license from authorized user licenses.
210
	 *
211
	 * @param string $ipAddress ip address to remove licenses on
212
	 * @param int $type optional license type. If empty or 0, will remove licenses with all types
213
	 * @return bool|int
214
	 */
215 1
	public function remove($ipAddress, $type = 0) {
216 1
		if ($this->apiType == 'xml')
217 1
			return $this->removeLicense($ipAddress, $type);
218
		else
219 1
			return $this->restRemove($ipAddress, $type);
220
	}
221
222
	/**
223
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
224
	 * or
225
	 * Remove IP licenses with specified type for customer. Also un­registers from CLN server associated with IP.
226
	 * @param string         $ipAddress   ip address to remove
227
	 * @param int $type optional parameter to specify the type of license to remove (1,2, or 16) or 0 for all
228
	 * @return bool|int 0 on success, -1 on error, Error will be returned also if account have no licenses for provided IP.
229
	 */
230 1
	public function removeLicense($ipAddress, $type = 0) {
231 1
		$this->log('info', "Calling CLoudLinux->xmlClient->removeLicense({$this->authToken()}, {$ipAddress}, {$type})", __LINE__, __FILE__);
232
		try {
233 1
			$this->response = $this->xmlClient->remove_license($this->authToken(), $ipAddress, $type);
234 1
		} catch (\Exception $e) {
235 1
			$this->log('error', 'Caught exception code: ' . $e->getCode());
236 1
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
237 1
			return false;
238
		}
239
		return $this->response;
240
	}
241
242
	/**
243
	 * Return all IP licenses owned by authorized user.
244
	 *
245
	 * The normal response will look something like:
246
	 * 	[
247
	 * 		'success': true,
248
	 * 		'data': [
249
	 * 			[
250
	 * 				'created': '2017-05-05T16:19-0400',
251
	 * 				'ip': '66.45.240.186',
252
	 * 				'registered': true,
253
	 * 				'type': 1
254
	 * 			], [
255
	 * 				'created': '2016-10-14T10:42-0400',
256
	 * 				'ip': '131.153.38.228',
257
	 * 				'registered': false,
258
	 * 				'type': 1
259
	 * 			],
260
	 *  .....
261
	 *
262
	 * @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
263
	 */
264 1
	public function restList() {
265 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/list.json?token=' . $this->authToken());
266 1
		return json_decode($this->response, true);
267
	}
268
269
	/**
270
	 * Return list of all IP licenses owned by authorized user
271
	 *
272
	 * @throws XmlRpcException for critical errors
273
	 * @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
274
	 */
275 1
	public function reconcile() {
276 1
		$this->response = $this->xmlClient->reconcile($this->authToken());
277 1
		return $this->response;
278
	}
279
280
	/**
281
	 * alias function to get a list of licenses
282
	 *
283
	 * @return false|array
284
	 */
285 1
	public function licenseList() {
286 1
		if ($this->apiType == 'rest')
287 1
			return $this->restList();
288
		else
289 1
			return $this->reconcile();
290
	}
291
292
	/**
293
	 * Register new IP license.
294
	 *
295
	 * @param string $ipAddress IP Address
296
	 * @param integer $type license type (1,2 or 16)
297
	 * @throws XmlRpcException for critical errors
298
	 * @return false|integer 0 on success, -1 on error
299
	 */
300 1
	public function license($ipAddress, $type) {
301 1
		$type = (int)$type;
302 1
		$xmlClient = $this->xmlClient;
303
		try {
304 1
			$this->log('error', 'Calling License(' . $this->authToken() . ',' . $ipAddress . ',' . $type . ')');
305 1
			return $this->response = $xmlClient->license($this->authToken(), $ipAddress, $type);
306 1
		} catch (\Exception $e) {
307 1
			$this->log('error', 'Caught exception code: ' . $e->getCode());
308 1
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
309 1
			return false;
310
		}
311
	}
312
}
313
314