Completed
Push — master ( e1a42d...a22432 )
by Joe
03:54
created

LiteSpeed::resetParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * LiteSpeed Licensing
4
 * Last Changed: $LastChangedDate: 2017-05-31 04:54:09 -0400 (Wed, 31 May 2017) $
5
 * @author detain
6
 * @version $Revision: 24934 $
7
 * @copyright 2017
8
 * @package MyAdmin
9
 * @category Licenses
10
 */
11
12
namespace Detain\LiteSpeed;
13
14
/**
15
 * LiteSpeed
16
 *
17
 * @access public
18
 */
19
class LiteSpeed {
20
21
	public $login = '';
22
	public $password = '';
23
	public $usePost = TRUE;
24
	public $url = 'https://store.litespeedtech.com/reseller/LiteSpeed_eService.php';
25
	public $version = '1.1';
26
	public $params = [];
27
	public $response = [];
28
	public $validProducts = ['LSWS', 'LSLB'];
29
	public $validCpu = ['1', '2', '4', '8', 'V', 'U'];
30
	public $validPeriod = ['monthly', 'yearly', 'owned'];
31
	public $validPayment = ['credit', 'creditcard'];
32
	public $rawResponse;
33
34
	/**
35
	 * @param $login
36
	 * @param $password
37
	 */
38
	public function __construct($login, $password) {
39
		$this->login = $login;
40
		$this->password = $password;
41
		$this->resetParams();
42
		function_requirements('xml2array');
43
	}
44
45
	public function resetParams() {
46
		$this->params = [];
47
		$this->params['litespeed_store_login'] = rawurlencode($this->login);
48
		$this->params['litespeed_store_pass'] = rawurlencode($this->password);
49
		$this->params['eService_version'] = rawurlencode($this->version);
50
	}
51
52
	/**
53
	 * @return mixed
54
	 */
55
	public function ping() {
56
		return $this->req('Ping');
57
	}
58
59
	/**
60
	 * LiteSpeed Order License Return
61
	 * 		* 	If any errors occur during this process, <result> will contain “error” and <message>
62
	 * 	will contain a detailed description:
63
	 * 		<LiteSpeed_eService>
64
	 * 			<action>Order</action>
65
	 * 			<result>error</result>
66
	 * 			<message>Invalid cpu!</message>
67
	 * 		</LiteSpeed_eService>
68
	 * 	If the transaction cannot be completed, <result> will be “incomplete”. For example, if
69
	 * 	payment method is “credit”, but there is not enough credit in your account, or if payment method
70
	 * 	is “creditcard”, but the charge cannot go through, then the transaction will not be completed
71
	 * 	and <result> will display “incomplete”. <license_id> and <invoice> will be provided. You will
72
	 * 	need to login to LiteSpeed’s online store and pay the invoice to finish the order.
73
	 * 		If payment method is “credit”, but not enough credit is available to your account:
74
	 * 			<LiteSpeed_eService>
75
	 * 				<action>Order</action>
76
	 * 					<license_id>6066</license_id>
77
	 * 				<license_type>WS_L_V</license_type>
78
	 * 				<invoice_id>12466</invoice_id>
79
	 * 				<result>incomplete</result>
80
	 * 				<message>need to pay invoice first</message>
81
	 * 			</LiteSpeed_eService>
82
	 * 		If payment method is “creditcard”, but the attempted credit card payment failed:
83
	 * 			<LiteSpeed_eService>
84
	 * 				<action>Order</action>
85
	 * 				<license_id>9329</license_id>
86
	 * 				<license_type>WS_L_V</license_type>
87
	 * 				<invoice_id>20568</invoice_id>
88
	 * 				<result>incomplete</result>
89
	 * 				<message>need to pay invoice first, credit card payment failed</message>
90
	 * 			</LiteSpeed_eService>
91
	 * 	If the transaction is successful, which should happen for the majority of cases, you will get a
92
	 * 	serial number back. You can parse the message to get the serial number and create your own script
93
	 * 	for installation. You will still receive the same confirmation email and serial number emails as
94
	 * 	if you ordered online. There will be no <invoice_id> if the charge was paid with credit.
95
	 * 		<LiteSpeed_eService>
96
	 * 			<action>Order</action>
97
	 * 			< license_id>6067</ license_id>
98
	 * 			<license_type>WS_L_V</license_type>
99
	 * 			<invoice_id>12466</invoice_id>
100
	 * 			< license_serial>gv06-kXsU-SHBr-pL4N</license_serial>
101
	 * 			<result>success</result>
102
	 * 			<message>new order automatically accepted</message>
103
	 * 		</LiteSpeed_eService>
104
	 *
105
	 */
106
107
	/**
108
	 * Order a LiteSpeed License
109
	 *
110
	 * @param mixed $product  Product type. Available values: “LSWS” or “LSLB”.
111
	 * @param mixed $cpu What kind of license. Available values: “1”: 1-CPU license, “2”: 2-CPU license,  “4”: 4-CPU license, “8”: 8-CPU license, “V”: VPS license, “U”: Ultra-VPS license (Available LSWS 4.2.2 and above.), If <order_product> is “LSLB”, <order_cpu> is not required.
112
	 * @param mixed $period  Renewal period. Available values: “monthly”, “yearly”, “owned”.
113
	 * @param mixed $payment Payment method. Available values: “credit”: Use account credit. User can utilize “Add funds” function to pre-deposit money, which will show up as account credit.      “creditcard”: Use credit card to pay. The credit card is pre-defined in the account.  If there is available credit in the account, credit will be applied first, even when the payment method is set to “creditcard”.
114
	 * @param mixed $cvv  (optional) Credit card security code. Try not to set this field. Only if your bank requires this (meaning that the transaction will fail without it) should you then supply this field. CVV code is not stored in the system, so if you need to set it, you have to set this field every time. Other information from your credit card will be taken from your user account.
115
	 * @param mixed $promocode  (optional) Promotional code. If you have a pre-assigned promotional code registered to your account, then you can set it here. Promotional codes are exclusive to each client. If your account is entitled to discounts at the invoice level, you do not need a promotional code.
116
	 * @return array array with the output result. see above for description of output.
117
	 * 		array (
118
	 * 			'LiteSpeed_eService' => array (
119
	 * 				'action' => 'Order',
120
	 * 				'license_id' => '36514',
121
	 * 				'license_type' => 'WS_L_1',
122
	 * 				'invoice_id' => '86300',
123
	 * 				'result' => 'incomplete',
124
	 * 				'message' => 'Invoice 86300 not paid. ',
125
	 * 			),
126
	 * 		)
127
	 */
128
	public function order($product, $cpu = FALSE, $period = 'monthly', $payment = 'credit', $cvv = FALSE, $promocode = FALSE) {
129
		if (!in_array($product, $this->validProducts)) {
130
			return array('error' => 'Invalid Product');
131
		}
132
		if ($product == 'LSWS' && !in_array($cpu, $this->validCpu)) {
133
			return array('error' => 'Invalid CPU');
134
		}
135
		if (!in_array($period, $this->validPeriod)) {
136
			return array('error' => 'Invalid Billing Period');
137
		}
138
		if (!in_array($payment, $this->validPayment)) {
139
			return array('error' => 'Invalid Payment Method');
140
		}
141
		$this->params['order_product'] = $product;
142
		if ($product != 'LSLB') {
143
			$this->params['order_cpu'] = $cpu;
144
		}
145
		$this->params['order_period'] = $period;
146
		$this->params['order_payment'] = $payment;
147
		if ($cvv !== FALSE) {
148
			$this->params['order_cvv'] = $cvv;
149
		}
150
		if ($promocode !== FALSE) {
151
			$this->params['order_promocode'] = $promocode;
152
		}
153
		return $this->req('Order');
154
	}
155
156
	/**
157
	 * @param bool   $serial
158
	 * @param bool   $ipAddress
159
	 * @param string $now
160
	 * @param bool   $reason
161
	 * @return mixed
162
	 */
163
	public function cancel($serial = FALSE, $ipAddress = FALSE, $now = 'Y', $reason = FALSE) {
164
		$this->params['license_serial'] = $serial;
165
		$this->params['server_ip'] = $ipAddress;
166
		$this->params['cancel_now'] = $now;
167
		$this->params['cancel_reason'] = $reason;
168
		return $this->req('Cancel');
169
	}
170
171
	/**
172
	 * @param $serial
173
	 * @param $ipAddress
174
	 * @return mixed
175
	 */
176
	public function release($serial, $ipAddress) {
177
		$this->params['license_serial'] = $serial;
178
		$this->params['server_ip'] = $ipAddress;
179
		return $this->req('ReleaseLicense');
180
	}
181
182
	/**
183
	 * Suspend a license.   This is a tool to temporarily suspend a particular user's license in special cases,
184
	 *  like nonpayment or policy violation. The web server checks in with the license server at least once
185
	 *  every 24 hours. It will shut down when it sees the license has been suspended. As a consequence, your
186
	 *  client's web site will go down. Please note, though, that this license will continue to appear on
187
	 *  your invoices. Once the issue is resolved, you can use an “unsuspend” action to reactivate the license;
188
	 *  or you can request cancellation to permanently cancel it. Only requesting cancellation will take the
189
	 *  license off your future invoices.
190
	 *
191
	 * @param mixed $serial optional (if you specify IP , but this is preferred) serial of the license
192
	 * @param mixed $ipAddress optional (if you specify serial) ip of the license, specifying bothserial and ip gives extra validation
193
	 * @param mixed $reason optional reason for suspend/unsuspend
194
	 * @return mixed
195
	 */
196 View Code Duplication
	public function suspend($serial = FALSE, $ipAddress = FALSE, $reason = FALSE) {
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...
197
		if ($serial !== FALSE) {
198
			$this->params['license_serial'] = $serial;
199
		}
200
		if ($ipAddress !== FALSE) {
201
			$this->params['server_ip'] = $ipAddress;
202
		}
203
		if ($reason !== FALSE) {
204
			$this->params['reason'] = $reason;
205
		}
206
		return $this->req('Suspend');
207
	}
208
209
	/**
210
	 * Unsuspend a license.
211
	 *
212
	 * @param mixed $serial optional (if you specify IP , but this is preferred) serial of the license
213
	 * @param mixed $ipAddress optional (if you specify serial) ip of the license, specifying bothserial and ip gives extra validation
214
	 * @param mixed $reason optional reason for suspend/unsuspend
215
	 * @return mixed
216
	 */
217 View Code Duplication
	public function unsuspend($serial = FALSE, $ipAddress = FALSE, $reason = FALSE) {
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...
218
		if ($serial !== FALSE) {
219
			$this->params['license_serial'] = $serial;
220
		}
221
		if ($ipAddress !== FALSE) {
222
			$this->params['server_ip'] = $ipAddress;
223
		}
224
		if ($reason !== FALSE) {
225
			$this->params['reason'] = $reason;
226
		}
227
		return $this->req('Unsuspend');
228
	}
229
230
	/**
231
	 * @param bool   $serial
232
	 * @param bool   $ipAddress
233
	 * @param        $cpu
234
	 * @param string $payment
235
	 * @param bool   $cvv
236
	 * @return array|mixed
237
	 */
238
	public function upgrade($serial = FALSE, $ipAddress = FALSE, $cpu, $payment = 'credit', $cvv = FALSE) {
239
		if ($serial !== FALSE) {
240
			$this->params['license_serial'] = $serial;
241
		}
242
		if ($ipAddress !== FALSE) {
243
			$this->params['server_ip'] = $ipAddress;
244
		}
245
		if (!in_array($cpu, $this->validCpu)) {
246
			return array('error' => 'Invalid CPU');
247
		}
248
		if (!in_array($payment, $this->validPayment)) {
249
			return array('error' => 'Invalid Payment Method');
250
		}
251
		$this->params['upgrade_cpu'] = $cpu;
252
		$this->params['order_payment'] = $payment;
253
		if ($cvv !== FALSE) {
254
			$this->params['order_cvv'] = $cvv;
255
		}
256
		return $this->req('Upgrade');
257
	}
258
259
	/**
260
	 * @param $field
261
	 * @return mixed
262
	 */
263
	public function query($field) {
264
		/**
265
		 * query_field – Currently supported values:
266
		 *	 	“AllActiveLicenses”
267
		 * 		“LicenseDetail_IP:IP Address”
268
		 * 	LicenseDetail_IP:xx.xxx.xxx.xxx (Please replace “IP Address” above with the IP address
269
		 * 	you would like to look up. No space between tag, colon and IP address.)
270
		 * 	If there is more than one active license associated with the query IP, “error” will be
271
		 * 	returned.
272
		 * 		“LicenseDetail_Serial:Serial Number” (Since Dec 16, 2011)
273
		 * 	LicenseDetail_Serial:ccccccccccccc (Please replace “Serial Number” above with the
274
		 * 	serial number you would like to look up. No space between tag, colon and serial
275
		 * 	number.)
276
		 * 	If there is no active license with the query serial number (including licenses that have
277
		 * 	been canceled or terminated), “error” will be returned.
278
		 * 	(If you have a specific function you'd like to see us implement, let us know. We'll do our
279
		 * 	best to make the system more useful.)
280
		 */
281
		$this->params['query_field'] = $field;
282
		return $this->req('Query');
283
	}
284
285
	/**
286
	 * sets whether or not to use POST for the request or GET (false)
287
	 *
288
	 * @param mixed $post TRUE for POST , FALSE for GET requests		*
289
	 */
290
	public function usePost($post = TRUE) {
291
		$this->usePost = $post;
292
	}
293
294
	/**
295
	 * performs a request to LiteSpeed
296
	 *
297
	 * @param string $action Can be one of Ping, Order, Cancel, ReleaseLicense, Suspend, Unsuspend, Upgrade, or Query
298
	 * @return mixed
299
	 */
300
	public function req($action) {
301
		$this->params['eService_action'] = rawurlencode($action);
302
		// Set the curl parameters.
303
		$ch = curl_init();
304
		$url = $this->url;
305
		if ($this->usePost !== FALSE) {
306
			curl_setopt($ch, CURLOPT_POST, TRUE);
307
			$pstring = '';
308 View Code Duplication
			foreach ($this->params as $param => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
309
				$pstring .= '&'.$param.'='.$value.'';
310
			}
311
			$pstring = mb_substr($pstring, 1);
312
			curl_setopt($ch, CURLOPT_POSTFIELDS, $pstring);
313
		} else {
314
			curl_setopt($ch, CURLOPT_POST, FALSE);
315
			$pstring = '';
316 View Code Duplication
			foreach ($this->params as $param => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
317
				$pstring .= '&'.$param.'='.$value.'';
318
			}
319
			$pstring = mb_substr($pstring, 1);
320
			$url .= '?'.$pstring;
321
		}
322
		myadmin_log('licenses', 'info', "LiteSpeed URL: $url\npstring: $pstring\n", __LINE__, __FILE__);
323
		curl_setopt($ch, CURLOPT_URL, $url);
324
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
325
		// Get response from the server.
326
		$this->rawResponse = curl_exec($ch);
327
		if (!$this->rawResponse) {
328
			$this->error[] = 'There was some error in connecting to Softaculous. This may be because of no internet connectivity at your end.';
0 ignored issues
show
Bug introduced by
The property error does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
329
			return FALSE;
330
		}
331
332
		// Extract the response details.
333
		$this->response = xml2array($this->rawResponse);
334
		myadmin_log('licenses', 'info', 'LiteSpeed Response '.var_export($this->response, TRUE), __LINE__, __FILE__);
335
		if (empty($this->response['error'])) {
336
			unset($this->response['error']);
337
			return $this->response;
338
		} else {
339
			$this->error = array_merge($this->error, $this->response['error']);
340
			return FALSE;
341
		}
342
343
	}
344
345
	/**
346
	 * displays the response
347
	 *
348
	 * @param mixed $response the response from an a function/api command
349
	 * @return void
350
	 */
351
	public function displayResponse($response) {
352
		if (empty($response))
353
			$response = $this->error;
354
		echo '<pre>'.json_encode($response, JSON_PRETTY_PRINT).'</pre>';
355
	}
356
357
}
358