Passed
Push — master ( 0a9663...29b356 )
by Joe
02:36
created

Cloudlinux::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 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
	 * @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
	public function __construct($login, $key, $apiType = 'rest') {
57
		$this->login = $login;
58
		$this->key = $key;
59
		$this->apiType = $apiType
60
		if (!isset($GLOBALS['HTTP_RAW_POST_DATA']))
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_IF
Loading history...
61
			$GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents('php://input');
62
		//if ($this->apiType == 'xml') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
			include_once('XML/RPC2/Client.php');
64
			$this->xmlOptions['prefix'] = $this->prefix;
65
			$this->xmlOptions['encoding'] = $this->encoding;
66
			$this->xmlOptions['sslverify'] = $this->sslverify;
67
			$this->xmlClient = \XML_RPC2_Client::create($this->xmlUrl, $this->xmlOptions);
68
		//} elseif ($this->apiType == 'rest') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
			//$this->restOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslverify;
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
		//}
71
	}
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 $options
80
	 * @return string the webpage
81
	 */
82
	public function getcurlpage($url, $postfields = '', $options = '') {
83
		//myadmin_log('myadmin', 'info', "Get Url Page $url", __LINE__, __FILE__);
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
		$agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2790.0 Safari/537.36';
85
		$curl = curl_init($url);
86
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
87
		curl_setopt($curl, CURLOPT_USERAGENT, $agent);
88
		if (is_array($postfields) || $postfields != '') {
89
			if (is_array($postfields)) {
90
				$postdata = [];
91
				foreach ($postfields as $field => $value) {
92
					$postdata[] = $field . '=' . urlencode($value);
93
				}
94
				curl_setopt($curl, CURLOPT_POSTFIELDS, implode('&', $postdata));
95
			} else {
96
				curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
97
			}
98
		}
99
		if (is_array($options) && sizeof($options) > 0) {
100
			foreach ($options as $key => $value) {
101
				curl_setopt($curl, $key, $value);
102
			}
103
		}
104
		$tmp = curl_exec($curl);
105
		curl_close($curl);
106
		$ret = $tmp;
107
		return $ret;
108
	}
109
110
	public function log($level, $text, $line = '', $file = '') {
111
		if (function_exists('myadmin_log'))
112
			myadmin_log('cloudlinux', $level, $text, $line, $file);
113
		else
114
			error_log($text);
115
	}
116
117
	/**
118
	 * Return system information about several Cloudlinux services
119
	 *
120
	 * @return array array of system information
121
	 */
122
	public function status() {
123
		$this->response = $this->getcurlpage($this->restUrl.'status.json', '', $this->restOptions);
124
		return json_decode($this->response, true);
125
	}
126
127
	/**
128
	 * Will return information about what kind of license types are available for registration and what types are already used by current account.
129
	 * @param string $ipAddress ip address to check
130
	 * @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
131
	 */
132
	public function availability($ipAddress) {
133
		$this->response = $this->getcurlpage($this->restUrl.'ipl/availability.json?ip='.$ipAddress.'&token='.$this->authToken(), '', $this->restOptions);
134
		return json_decode($this->response, true);
135
	}
136
137
	/**
138
	 * Check if IP license is registered by any customer.
139
	 *
140
	 * @param string $ipAddress ip address to check
141
	 * @return array Will return list of registered license types or empty list if provided IP is not registered yet.
142
	 */
143
	public function check($ipAddress) {
144
		$this->response = $this->getcurlpage($this->restUrl.'ipl/check.json?ip='.$ipAddress.'&token='.$this->authToken(), '', $this->restOptions);
145
		$response = json_decode($this->response, true);
146
		if ($response['success'] == 1)
147
			return $response['data'];
148
		else
149
			return false;
150
	}
151
152
	/**
153
	 * Will register IP based license for authorized user.
154
	 *
155
	 * @param string $ipAddress ip address to registger
156
	 * @param int $type IP license type (1,2 or 16)
157
	 * @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
158
	 */
159
	public function register($ipAddress, $type) {
160
		$this->response = $this->getcurlpage($this->restUrl.'ipl/register.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken(), '', $this->restOptions);
161
		return json_decode($this->response, true);
162
	}
163
164
	/**
165
	 * Will remove IP based license from authorized user licenses.
166
	 *
167
	 * @param string $ipAddress ip address to remove licenses on
168
	 * @param int $type optional license type. If empty, will remove licenses with all types
169
	 * @return bool
170
	 */
171
	public function restRemove($ipAddress, $type = 0) {
172
		if ($type != 0)
173
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&type='.$type.'&token='.$this->authToken(), '', $this->restOptions);
174
		else
175
			$this->response = $this->getcurlpage($this->restUrl.'ipl/remove.json?ip='.$ipAddress.'&token='.$this->authToken(), '', $this->restOptions);
176
		return json_decode($this->response, true);
177
	}
178
179
	/**
180
	 * Will remove IP based license from authorized user licenses.
181
	 *
182
	 * @param string $ipAddress ip address to remove licenses on
183
	 * @param int $type optional license type. If empty or 0, will remove licenses with all types
184
	 * @return bool
185
	 */
186
	public function remove($ipAddress, $type = 0) {
187
		if ($this->apiType == 'xml')
188
			return $this->removeLicense($ipAddress, $type);
189
		else
190
			return $this->restRemove($ipAddress, $type);
191
	}
192
193
	/**
194
	 * Return all IP licenses owned by authorized user.
195
	 *
196
	 * @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
197
	 */
198
	public function restList() {
199
		$this->response = $this->getcurlpage($this->restUrl.'ipl/list.json?token='.$this->uthToken(), '', $this->restOptions);
200
		return json_decode($this->response, true);
201
	}
202
203
	/**
204
	 * automatic authToken generator
205
	 *
206
	 * @return string the authToken
207
	 */
208
	public function authToken() {
209
		$time = time();
210
		try {
211
			return $this->login . '|' . $time . '|' . sha1($this->key . $time);
212
		} catch (Exception $e) {
213
			$this->log('error', 'Caught exception code: ' . $e->getCode());
214
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
215
			return false;
216
		}
217
	}
218
219
	/**
220
	 * Register new IP license.
221
	 *
222
	 * @param string $ipAddress IP Address
223
	 * @param integer $type license type (1,2 or 16)
224
	 * @throws XmlRpcException for critical errors
225
	 * @return integer 0 on success, -1 on error
226
	 */
227
	public function license($ipAddress, $type) {
228
		$type = (int)$type;
229
		try {
230
			$this->log('error', 'Calling License(' . $this->authToken() . ',' . $ipAddress . ',' . $type . ')');
231
			$this->response = $this->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 bool|false|int $type optional parameter to specify the type of license to remove (1,2, or 16)
247
	 * @return integer 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 = false) {
250
		$this->log('info', "Calling CLoudLinux->xmlClient->removeLicense({$this->authToken()}, {$ipAddress}, {$type})", __LINE__, __FILE__);
251
		try {
252
			if ($type === false)
253
				return $this->response = $this->remove($ipAddress);
254
			else
255
				return $this->response = $this->remove($ipAddress, $type);
256
		} catch (Exception $e) {
257
			$this->log('error', 'Caught exception code: ' . $e->getCode());
258
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
259
			return false;
260
		}
261
	}
262
263
	/**
264
	 * Check if IP license was registered by any customer. Arguments:
265
	 *
266
	 * @param string $ipAddress ip address to remove
267
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
268
	 * @throws XmlRpcException for critical errors
269
	 * @return array (list<int>): List of registered license types or empty list if no license found
270
	 */
271
	public function isLicensed($ipAddress, $checkAll = true) {
272
		if ($this->apiType == 'xml')
273
			return $this->xmlIsLicensed($ipAddress, $checkAll);
274
		else
275
			return $this->check($ipAddress, $checkAll);
276
	}
277
	/**
278
	 * Check if IP license was registered by any customer. Arguments:
279
	 *
280
	 * @param string $ipAddress ip address to remove
281
	 * @param bool $checkAll True will search for any type of license. False ­ only for types 1 or 2
282
	 * @throws XmlRpcException for critical errors
283
	 * @return array (list<int>): List of registered license types or empty list if no license found
284
	 */
285
	public function xmlIsLicensed($ipAddress, $checkAll = true) {
286
		try {
287
			return $this->response = $this->xmlClient->isLicensed($this->authToken(), $ipAddress, $checkAll);
288
		} catch (Exception $e) {
289
			$this->log('error', 'Caught exception code: ' . $e->getCode());
290
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
291
			return false;
292
		}
293
	}
294
295
	/**
296
	 * @return bool|mixed
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 array (list<structure>): List of structures or empty list. Each structure contains keys:
313
	 * 	IP(string)
314
	 * 	TYPE(int) ­ license type
315
	 * 	REGISTERED(boolean) ­ True if server was registered in CLN with this license
316
	 */
317
	public function reconcile() {
318
		try {
319
			return $this->response = $this->xmlClient->reconcile($this->authToken());
320
		} catch (Exception $e) {
321
			$this->log('error', 'Caught exception code: ' . $e->getCode());
322
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
323
			return false;
324
		}
325
	}
326
327
	/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
328
	public function getKeyInfo($Key) {
329
	$this->response = $this->xml->__call('partner10.getKeyInfo', array(
330
	$this->AuthInfo(),
331
	$Key,
332
	));
333
	return $this->response;
334
	}
335
	*/
336
}
337
338