Passed
Push — master ( 3ce015...babf90 )
by Joe
02:04
created

LiteSpeed::order()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 20
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

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