Completed
Push — master ( d73743...7fb371 )
by Joe
02:48
created

Cloudlinux::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 4
crap 6
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
	public function xmlIsLicensed($ipAddress, $checkAll = true) {
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 1
		$this->response = $this->remove($ipAddress, $type);
232 1
		return $this->response;
233
	}
234
235
	/**
236
	 * Return all IP licenses owned by authorized user.
237
	 *
238
	 * The normal response will look something like:
239
	 * 	[
240
	 * 		'success': true,
241
	 * 		'data': [
242
	 * 			[
243
	 * 				'created': '2017-05-05T16:19-0400',
244
	 * 				'ip': '66.45.240.186',
245
	 * 				'registered': true,
246
	 * 				'type': 1
247
	 * 			], [
248
	 * 				'created': '2016-10-14T10:42-0400',
249
	 * 				'ip': '131.153.38.228',
250
	 * 				'registered': false,
251
	 * 				'type': 1
252
	 * 			],
253
	 *  .....
254
	 *
255
	 * @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
256
	 */
257 1
	public function restList() {
258 1
		$this->response = $this->getcurlpage($this->restUrl.'ipl/list.json?token=' . $this->authToken());
259 1
		return json_decode($this->response, true);
260
	}
261
262
	/**
263
	 * Return list of all IP licenses owned by authorized user
264
	 *
265
	 * @throws XmlRpcException for critical errors
266
	 * @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
267
	 */
268 1
	public function reconcile() {
269 1
		$this->response = $this->xmlClient->reconcile($this->authToken());
270 1
		return $this->response;
271
	}
272
273
	/**
274
	 * alias function to get a list of licenses
275
	 *
276
	 * @return false|array
277
	 */
278 1
	public function licenseList() {
279 1
		if ($this->apiType == 'rest')
280 1
			return $this->restList();
281
		else
282 1
			return $this->reconcile();
283
	}
284
285
	/**
286
	 * Register new IP license.
287
	 *
288
	 * @param string $ipAddress IP Address
289
	 * @param integer $type license type (1,2 or 16)
290
	 * @throws XmlRpcException for critical errors
291
	 * @return false|integer 0 on success, -1 on error
292
	 */
293 1
	public function license($ipAddress, $type) {
294 1
		$type = (int)$type;
295 1
		$xmlClient = $this->xmlClient;
296
		try {
297 1
			$this->log('error', 'Calling License(' . $this->authToken() . ',' . $ipAddress . ',' . $type . ')');
298 1
			$this->response = $xmlClient->license($this->authToken(), $ipAddress, $type);
299 1
		} catch (\Exception $e) {
300 1
			$this->log('error', 'Caught exception code: ' . $e->getCode());
301 1
			$this->log('error', 'Caught exception message: ' . $e->getMessage());
302 1
			return false;
303
		}
304
		return $this->response;
305
	}
306
}
307
308