Passed
Push — master ( 5a4945...c689df )
by Joe
02:26
created

LiteSpeed::order()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 27
cp 0
rs 4.909
c 0
b 0
f 0
cc 9
eloc 19
nc 12
nop 6
crap 90
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
/**
13
 * LiteSpeed
14
 *
15
 * @access public
16
 */
17
class LiteSpeed {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
The property $use_post is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $valid_products is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $valid_cpu is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $valid_period is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $valid_payment is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
18
19
	public $login = '';
20
	public $password = '';
21
	public $use_post = 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 $valid_products = ['LSWS', 'LSLB'];
27
	public $valid_cpu = ['1', '2', '4', '8', 'V', 'U'];
28
	public $valid_period = ['monthly', 'yearly', 'owned'];
29
	public $valid_payment = ['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->valid_products)) {
128
			return array('error' => 'Invalid Product');
129
		}
130
		if ($product == 'LSWS' && !in_array($cpu, $this->valid_cpu)) {
131
			return array('error' => 'Invalid CPU');
132
		}
133
		if (!in_array($period, $this->valid_period)) {
134
			return array('error' => 'Invalid Billing Period');
135
		}
136
		if (!in_array($payment, $this->valid_payment)) {
137
			return array('error' => 'Invalid Payment Method');
138
		}
139
		$this->params['order_product'] = $product;
140
		if ($product != 'LSLB') {
141
			$this->params['order_cpu'] = $cpu;
142
		}
143
		$this->params['order_period'] = $period;
144
		$this->params['order_payment'] = $payment;
145
		if ($cvv !== false) {
146
			$this->params['order_cvv'] = $cvv;
147
		}
148
		if ($promocode !== false) {
149
			$this->params['order_promocode'] = $promocode;
150
		}
151
		return $this->req('Order');
152
	}
153
154
	/**
155
	 * @param bool   $serial
156
	 * @param bool   $ip
157
	 * @param string $now
158
	 * @param bool   $reason
159
	 * @return mixed
160
	 */
161
	public function cancel($serial = false, $ip = false, $now = 'Y', $reason = false) {
162
		$this->params['license_serial'] = $serial;
163
		$this->params['server_ip'] = $ip;
164
		$this->params['cancel_now'] = $now;
165
		$this->params['cancel_reason'] = $reason;
166
		return $this->req('Cancel');
167
	}
168
169
	/**
170
	 * @param $serial
171
	 * @param $ip
172
	 * @return mixed
173
	 */
174
	public function release($serial, $ip) {
175
		$this->params['license_serial'] = $serial;
176
		$this->params['server_ip'] = $ip;
177
		return $this->req('ReleaseLicense');
178
	}
179
180
	/**
181
	 * Suspend a license.   This is a tool to temporarily suspend a particular user's license in special cases,
182
	 *  like nonpayment or policy violation. The web server checks in with the license server at least once
183
	 *  every 24 hours. It will shut down when it sees the license has been suspended. As a consequence, your
184
	 *  client's web site will go down. Please note, though, that this license will continue to appear on
185
	 *  your invoices. Once the issue is resolved, you can use an “unsuspend” action to reactivate the license;
186
	 *  or you can request cancellation to permanently cancel it. Only requesting cancellation will take the
187
	 *  license off your future invoices.
188
	 *
189
	 * @param mixed $serial optional (if you specify IP , but this is preferred) serial of the license
190
	 * @param mixed $ip optional (if you specify serial) ip of the license, specifying bothserial and ip gives extra validation
191
	 * @param mixed $reason optional reason for suspend/unsuspend
192
	 * @return mixed
193
	 */
194 View Code Duplication
	public function suspend($serial = false, $ip = 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...
195
		if ($serial !== false) {
196
			$this->params['license_serial'] = $serial;
197
		}
198
		if ($ip !== false) {
199
			$this->params['server_ip'] = $ip;
200
		}
201
		if ($reason !== false) {
202
			$this->params['reason'] = $reason;
203
		}
204
		return $this->req('Suspend');
205
	}
206
207
	/**
208
	 * Unsuspend a license.
209
	 *
210
	 * @param mixed $serial optional (if you specify IP , but this is preferred) serial of the license
211
	 * @param mixed $ip optional (if you specify serial) ip of the license, specifying bothserial and ip gives extra validation
212
	 * @param mixed $reason optional reason for suspend/unsuspend
213
	 * @return mixed
214
	 */
215 View Code Duplication
	public function unsuspend($serial = false, $ip = 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...
216
		if ($serial !== false) {
217
			$this->params['license_serial'] = $serial;
218
		}
219
		if ($ip !== false) {
220
			$this->params['server_ip'] = $ip;
221
		}
222
		if ($reason !== false) {
223
			$this->params['reason'] = $reason;
224
		}
225
		return $this->req('Unsuspend');
226
	}
227
228
	/**
229
	 * @param bool   $serial
230
	 * @param bool   $ip
231
	 * @param        $cpu
232
	 * @param string $payment
233
	 * @param bool   $cvv
234
	 * @return array|mixed
235
	 */
236
	public function upgrade($serial = false, $ip = false, $cpu, $payment = 'credit', $cvv = false) {
237
		if ($serial !== false) {
238
			$this->params['license_serial'] = $serial;
239
		}
240
		if ($ip !== false) {
241
			$this->params['server_ip'] = $ip;
242
		}
243
		if (!in_array($cpu, $this->valid_cpu)) {
244
			return array('error' => 'Invalid CPU');
245
		}
246
		if (!in_array($payment, $this->valid_payment)) {
247
			return array('error' => 'Invalid Payment Method');
248
		}
249
		$this->params['upgrade_cpu'] = $cpu;
250
		$this->params['order_payment'] = $payment;
251
		if ($cvv !== false) {
252
			$this->params['order_cvv'] = $cvv;
253
		}
254
		return $this->req('Upgrade');
255
	}
256
257
	/**
258
	 * @param $field
259
	 * @return mixed
260
	 */
261
	public function query($field) {
262
		/**
263
		 * query_field – Currently supported values:
264
		 *	 	“AllActiveLicenses”
265
		 * 		“LicenseDetail_IP:IP Address”
266
		 * 	LicenseDetail_IP:xx.xxx.xxx.xxx (Please replace “IP Address” above with the IP address
267
		 * 	you would like to look up. No space between tag, colon and IP address.)
268
		 * 	If there is more than one active license associated with the query IP, “error” will be
269
		 * 	returned.
270
		 * 		“LicenseDetail_Serial:Serial Number” (Since Dec 16, 2011)
271
		 * 	LicenseDetail_Serial:ccccccccccccc (Please replace “Serial Number” above with the
272
		 * 	serial number you would like to look up. No space between tag, colon and serial
273
		 * 	number.)
274
		 * 	If there is no active license with the query serial number (including licenses that have
275
		 * 	been canceled or terminated), “error” will be returned.
276
		 * 	(If you have a specific function you'd like to see us implement, let us know. We'll do our
277
		 * 	best to make the system more useful.)
278
		 */
279
		$this->params['query_field'] = $field;
280
		return $this->req('Query');
281
	}
282
283
	/**
284
	 * sets whether or not to use POST for the request or GET (false)
285
	 *
286
	 * @param mixed $post TRUE for POST , FALSE for GET requests		*
287
	 */
288
	public function use_post($post = true) {
289
		$this->use_post = $post;
290
	}
291
292
	/**
293
	 * performs a request to LiteSpeed
294
	 *
295
	 * @param string $action Can be one of Ping, Order, Cancel, ReleaseLicense, Suspend, Unsuspend, Upgrade, or Query
296
	 * @return mixed
297
	 */
298
	public function req($action) {
299
		$this->params['eService_action'] = rawurlencode($action);
300
		// Set the curl parameters.
301
		$ch = curl_init();
302
		$url = $this->url;
303
		if ($this->use_post !== false) {
304
			curl_setopt($ch, CURLOPT_POST, true);
305
			$pstring = '';
306 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...
307
				$pstring .= '&'.$param.'='.$value.'';
308
			}
309
			$pstring = mb_substr($pstring, 1);
310
			curl_setopt($ch, CURLOPT_POSTFIELDS, $pstring);
311
		} else {
312
			curl_setopt($ch, CURLOPT_POST, false);
313
			$pstring = '';
314 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...
315
				$pstring .= '&'.$param.'='.$value.'';
316
			}
317
			$pstring = mb_substr($pstring, 1);
318
			$url .= '?'.$pstring;
319
		}
320
		myadmin_log('licenses', 'info', "LiteSpeed URL: $url\npstring: $pstring\n", __LINE__, __FILE__);
321
		curl_setopt($ch, CURLOPT_URL, $url);
322
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
323
		// Get response from the server.
324
		$this->rawResponse = curl_exec($ch);
325
		if (!$this->rawResponse) {
326
			$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...
327
			return false;
328
		}
329
330
		// Extract the response details.
331
		$this->response = xml2array($this->rawResponse);
332
		myadmin_log('licenses', 'info', 'LiteSpeed Response '.var_export($this->response, true), __LINE__, __FILE__);
333
		if (empty($this->response['error'])) {
334
			unset($this->response['error']);
335
			return $this->response;
336
		} else {
337
			$this->error = array_merge($this->error, $this->response['error']);
338
			return false;
339
		}
340
341
	}
342
343
	/**
344
	 * LiteSpeed::r()
345
	 *
346
	 * @param mixed $r
347
	 * @return void
348
	 */
349
	public function r($r) {
350
		if (empty($r)) {
351
			$r = $this->error;
352
		}
353
		echo '<pre>';
354
		var_export($r);
355
		echo '</pre>';
356
	}
357
358
}
359