LiteSpeed::resetParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * LiteSpeed Licensing
4
 * @author Joe Huss <[email protected]>
5
 * @copyright 2019
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
	{
38
		$this->login = $login;
39
		$this->password = $password;
40
		$this->resetParams();
41
		function_requirements('xml2array');
0 ignored issues
show
Bug introduced by
The function function_requirements was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
		/** @scrutinizer ignore-call */ 
42
  function_requirements('xml2array');
Loading history...
42
	}
43
44
	public function resetParams()
45
	{
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
	{
57
		return $this->req('Ping');
58
	}
59
60
	/**
61
	 * LiteSpeed Order License Return
62
	 *     If any errors occur during this process, <result> will contain "error" and <message>
63
	 *     will contain a detailed description:
64
	 *         <LiteSpeed_eService>
65
	 *             <action>Order</action>
66
	 *             <result>error</result>
67
	 *             <message>Invalid cpu!</message>
68
	 *         </LiteSpeed_eService>
69
	 *     If the transaction cannot be completed, <result> will be "incomplete". For example, if
70
	 *     payment method is "credit", but there is not enough credit in your account, or if payment method
71
	 *     is "creditcard", but the charge cannot go through, then the transaction will not be completed
72
	 *     and <result> will display "incomplete". <license_id> and <invoice> will be provided. You will
73
	 *     need to login to LiteSpeed’s online store and pay the invoice to finish the order.
74
	 *         If payment method is "credit", but not enough credit is available to your account:
75
	 *             <LiteSpeed_eService>
76
	 *                 <action>Order</action>
77
	 *                     <license_id>6066</license_id>
78
	 *                 <license_type>WS_L_V</license_type>
79
	 *                 <invoice_id>12466</invoice_id>
80
	 *                 <result>incomplete</result>
81
	 *                 <message>need to pay invoice first</message>
82
	 *             </LiteSpeed_eService>
83
	 *         If payment method is "creditcard", but the attempted credit card payment failed:
84
	 *             <LiteSpeed_eService>
85
	 *                 <action>Order</action>
86
	 *                 <license_id>9329</license_id>
87
	 *                 <license_type>WS_L_V</license_type>
88
	 *                 <invoice_id>20568</invoice_id>
89
	 *                 <result>incomplete</result>
90
	 *                 <message>need to pay invoice first, credit card payment failed</message>
91
	 *             </LiteSpeed_eService>
92
	 *     If the transaction is successful, which should happen for the majority of cases, you will get a
93
	 *     serial number back. You can parse the message to get the serial number and create your own script
94
	 *     for installation. You will still receive the same confirmation email and serial number emails as
95
	 *     if you ordered online. There will be no <invoice_id> if the charge was paid with credit.
96
	 *         <LiteSpeed_eService>
97
	 *             <action>Order</action>
98
	 *             < license_id>6067</ license_id>
99
	 *             <license_type>WS_L_V</license_type>
100
	 *             <invoice_id>12466</invoice_id>
101
	 *             < license_serial>gv06-kXsU-SHBr-pL4N</license_serial>
102
	 *             <result>success</result>
103
	 *             <message>new order automatically accepted</message>
104
	 *         </LiteSpeed_eService>
105
	 *
106
	 */
107
108
	/**
109
	 * Order a LiteSpeed License
110
	 *
111
	 * @param mixed $product  Product type. Available values: "LSWS" or "LSLB".
112
	 * @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.
113
	 * @param mixed $period  Renewal period. Available values: "monthly", "yearly", "owned".
114
	 * @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".
115
	 * @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.
116
	 * @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.
117
	 * @return array array with the output result. see above for description of output.
118
	 *         array (
119
	 *             'LiteSpeed_eService' => array (
120
	 *                 'action' => 'Order',
121
	 *                 'license_id' => '36514',
122
	 *                 'license_type' => 'WS_L_1',
123
	 *                 'invoice_id' => '86300',
124
	 *                 'result' => 'incomplete',
125
	 *                 'message' => 'Invoice 86300 not paid. ',
126
	 *             ),
127
	 *         )
128
	 */
129
	public function order($product, $cpu = false, $period = 'monthly', $payment = 'credit', $cvv = false, $promocode = false)
130
	{
131
		if (!in_array($product, $this->validProducts)) {
132
			return ['error' => 'Invalid Product'];
133
		}
134
		if ($product == 'LSWS' && !in_array($cpu, $this->validCpu)) {
135
			return ['error' => 'Invalid CPU'];
136
		}
137
		if (!in_array($period, $this->validPeriod)) {
138
			return ['error' => 'Invalid Billing Period'];
139
		}
140
		if (!in_array($payment, $this->validPayment)) {
141
			return ['error' => 'Invalid Payment Method'];
142
		}
143
		$this->params['order_product'] = $product;
144
		if ($product != 'LSLB') {
145
			$this->params['order_cpu'] = $cpu;
146
		}
147
		$this->params['order_period'] = $period;
148
		$this->params['order_payment'] = $payment;
149
		if ($cvv !== false) {
150
			$this->params['order_cvv'] = $cvv;
151
		}
152
		if ($promocode !== false) {
153
			$this->params['order_promocode'] = $promocode;
154
		}
155
		return $this->req('Order');
156
	}
157
158
	/**
159
	 * @param bool   $serial
160
	 * @param bool   $ipAddress
161
	 * @param string $now
162
	 * @param bool   $reason
163
	 * @return mixed
164
	 */
165
	public function cancel($serial = false, $ipAddress = false, $now = 'Y', $reason = false)
166
	{
167
		$this->params['license_serial'] = $serial;
168
		$this->params['server_ip'] = $ipAddress;
169
		$this->params['cancel_now'] = $now;
170
		$this->params['cancel_reason'] = $reason;
171
		return $this->req('Cancel');
172
	}
173
174
	/**
175
	 * @param $serial
176
	 * @param $ipAddress
177
	 * @return mixed
178
	 */
179
	public function release($serial, $ipAddress)
180
	{
181
		$this->params['license_serial'] = $serial;
182
		$this->params['server_ip'] = $ipAddress;
183
		return $this->req('ReleaseLicense');
184
	}
185
186
	/**
187
	 * Suspend a license.   This is a tool to temporarily suspend a particular user's license in special cases,
188
	 *  like nonpayment or policy violation. The web server checks in with the license server at least once
189
	 *  every 24 hours. It will shut down when it sees the license has been suspended. As a consequence, your
190
	 *  client's web site will go down. Please note, though, that this license will continue to appear on
191
	 *  your invoices. Once the issue is resolved, you can use an "unsuspend" action to reactivate the license;
192
	 *  or you can request cancellation to permanently cancel it. Only requesting cancellation will take the
193
	 *  license off your future invoices.
194
	 *
195
	 * @param mixed $serial optional (if you specify IP , but this is preferred) serial of the license
196
	 * @param mixed $ipAddress optional (if you specify serial) ip of the license, specifying bothserial and ip gives extra validation
197
	 * @param mixed $reason optional reason for suspend/unsuspend
198
	 * @return mixed
199
	 */
200
	public function suspend($serial = false, $ipAddress = false, $reason = false)
201
	{
202
		if ($serial !== false) {
203
			$this->params['license_serial'] = $serial;
204
		}
205
		if ($ipAddress !== false) {
206
			$this->params['server_ip'] = $ipAddress;
207
		}
208
		if ($reason !== false) {
209
			$this->params['reason'] = $reason;
210
		}
211
		return $this->req('Suspend');
212
	}
213
214
	/**
215
	 * Unsuspend a license.
216
	 *
217
	 * @param mixed $serial optional (if you specify IP , but this is preferred) serial of the license
218
	 * @param mixed $ipAddress optional (if you specify serial) ip of the license, specifying bothserial and ip gives extra validation
219
	 * @param mixed $reason optional reason for suspend/unsuspend
220
	 * @return mixed
221
	 */
222
	public function unsuspend($serial = false, $ipAddress = false, $reason = false)
223
	{
224
		if ($serial !== false) {
225
			$this->params['license_serial'] = $serial;
226
		}
227
		if ($ipAddress !== false) {
228
			$this->params['server_ip'] = $ipAddress;
229
		}
230
		if ($reason !== false) {
231
			$this->params['reason'] = $reason;
232
		}
233
		return $this->req('Unsuspend');
234
	}
235
236
	/**
237
	 * @param bool   $serial
238
	 * @param bool   $ipAddress
239
	 * @param        $cpu
240
	 * @param string $payment
241
	 * @param bool   $cvv
242
	 * @return array|mixed
243
	 */
244
	public function upgrade($serial = false, $ipAddress = false, $cpu, $payment = 'credit', $cvv = false)
245
	{
246
		if ($serial !== false) {
247
			$this->params['license_serial'] = $serial;
248
		}
249
		if ($ipAddress !== false) {
250
			$this->params['server_ip'] = $ipAddress;
251
		}
252
		if (!in_array($cpu, $this->validCpu)) {
253
			return ['error' => 'Invalid CPU'];
254
		}
255
		if (!in_array($payment, $this->validPayment)) {
256
			return ['error' => 'Invalid Payment Method'];
257
		}
258
		$this->params['upgrade_cpu'] = $cpu;
259
		$this->params['order_payment'] = $payment;
260
		if ($cvv !== false) {
261
			$this->params['order_cvv'] = $cvv;
262
		}
263
		return $this->req('Upgrade');
264
	}
265
266
	/**
267
	 * @param $field
268
	 * @return mixed
269
	 */
270
	public function query($field)
271
	{
272
		/**
273
		 * query_field – Currently supported values:
274
		 *         "AllActiveLicenses"
275
		 *         "LicenseDetail_IP:IP Address"
276
		 *     LicenseDetail_IP:xx.xxx.xxx.xxx (Please replace "IP Address" above with the IP address
277
		 *     you would like to look up. No space between tag, colon and IP address.)
278
		 *     If there is more than one active license associated with the query IP, "error" will be
279
		 *     returned.
280
		 *         "LicenseDetail_Serial:Serial Number" (Since Dec 16, 2011)
281
		 *     LicenseDetail_Serial:ccccccccccccc (Please replace "Serial Number" above with the
282
		 *     serial number you would like to look up. No space between tag, colon and serial
283
		 *     number.)
284
		 *     If there is no active license with the query serial number (including licenses that have
285
		 *     been canceled or terminated), "error" will be returned.
286
		 *     (If you have a specific function you'd like to see us implement, let us know. We'll do our
287
		 *     best to make the system more useful.)
288
		 */
289
		$this->params['query_field'] = $field;
290
		return $this->req('Query');
291
	}
292
293
	/**
294
	 * sets whether or not to use POST for the request or GET (false)
295
	 *
296
	 * @param mixed $post TRUE for POST , FALSE for GET requests        *
297
	 */
298
	public function usePost($post = true)
299
	{
300
		$this->usePost = $post;
301
	}
302
303
	/**
304
	 * performs a request to LiteSpeed
305
	 *
306
	 * @param string $action Can be one of Ping, Order, Cancel, ReleaseLicense, Suspend, Unsuspend, Upgrade, or Query
307
	 * @return mixed
308
	 */
309
	public function req($action)
310
	{
311
		require_once __DIR__.'/../../../workerman/statistics/Applications/Statistics/Clients/StatisticClient.php';
312
		
313
		$this->params['eService_action'] = rawurlencode($action);
314
		// Set the curl parameters.
315
		$ch = curl_init();
316
		$url = $this->url;
317
		if ($this->usePost !== false) {
318
			curl_setopt($ch, CURLOPT_POST, true);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

318
			curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_POST, true);
Loading history...
319
			$pstring = '';
320
			foreach ($this->params as $param => $value) {
321
				$pstring .= '&'.$param.'='.$value.'';
322
			}
323
			$pstring = mb_substr($pstring, 1);
324
			curl_setopt($ch, CURLOPT_POSTFIELDS, $pstring);
325
		} else {
326
			curl_setopt($ch, CURLOPT_POST, false);
327
			$pstring = '';
328
			foreach ($this->params as $param => $value) {
329
				$pstring .= '&'.$param.'='.$value.'';
330
			}
331
			$pstring = mb_substr($pstring, 1);
332
			$url .= '?'.$pstring;
333
		}
334
		myadmin_log('licenses', 'info', "LiteSpeed URL: $url\npstring: $pstring\n", __LINE__, __FILE__);
0 ignored issues
show
Bug introduced by
The function myadmin_log was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

334
		/** @scrutinizer ignore-call */ 
335
  myadmin_log('licenses', 'info', "LiteSpeed URL: $url\npstring: $pstring\n", __LINE__, __FILE__);
Loading history...
335
		curl_setopt($ch, CURLOPT_URL, $url);
336
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
337
		// Get response from the server.
338
		\StatisticClient::tick('LiteSpeed', $action);
0 ignored issues
show
Bug introduced by
The type StatisticClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
339
		$this->rawResponse = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

339
		$this->rawResponse = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
340
		if (!$this->rawResponse) {
341
			$error = 'There was some error in connecting to LiteSpeed. This may be because of no internet connectivity at your end.';
342
			$this->error[] = $error;
0 ignored issues
show
Bug Best Practice introduced by
The property error does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
343
			\StatisticClient::report('LiteSpeed', $action, false, 1, $error, STATISTICS_SERVER);
0 ignored issues
show
Bug introduced by
The constant Detain\LiteSpeed\STATISTICS_SERVER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
344
			return false;
345
		}
346
347
		// Extract the response details.
348
		$this->response = xml2array($this->rawResponse);
0 ignored issues
show
Bug introduced by
The function xml2array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

348
		$this->response = /** @scrutinizer ignore-call */ xml2array($this->rawResponse);
Loading history...
349
		myadmin_log('licenses', 'info', 'LiteSpeed Response '.var_export($this->response, true), __LINE__, __FILE__);
350
		if (empty($this->response['error'])) {
351
			unset($this->response['error']);
352
			\StatisticClient::report('LiteSpeed', $action, true, 0, '', STATISTICS_SERVER);
353
			return $this->response;
354
		} else {
355
			$this->error = array_merge($this->error, $this->response['error']);
356
			\StatisticClient::report('LiteSpeed', $action, false, 1, $this->response['error'], STATISTICS_SERVER);
357
			return false;
358
		}
359
	}
360
361
	/**
362
	 * displays the response
363
	 *
364
	 * @param mixed $response the response from an a function/api command
365
	 * @return void
366
	 */
367
	public function displayResponse($response)
368
	{
369
		if (empty($response)) {
370
			$response = $this->error;
371
		}
372
		echo '<pre>'.json_encode($response, JSON_PRETTY_PRINT).'</pre>';
373
	}
374
}
375