Cpanel::setopt()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 5
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Detain\Cpanel;
4
5
require_once __DIR__.'/../../../workerman/statistics/Applications/Statistics/Clients/StatisticClient.php';
6
7
/**
8
 * Class Cpanel
9
 *
10
 * @package Detain\Cpanel
11
 */
12
class Cpanel
13
{
14
	public $format;
15
	public $curl;
16
	public $opts;
17
18
	/**
19
	* Cpanel constructor.
20
	*
21
	* @param $user
22
	* @param $pass
23
	*/
24
	public function __construct($user, $pass)
25
	{
26
		$this->opts = [];
27
		$this->format = 'simplexml';
28
		$this->setCredentials($user, $pass);
29
		$this->setopt(CURLOPT_RETURNTRANSFER, 1);
30
		$this->setopt(CURLOPT_USERAGENT, 'cPanel Licensing Agent (php) 3.5');
31
	}
32 1
33 1
	/**
34 1
	* @param $option
35
	* @param $value
36
	*/
37
	public function setopt($option, $value)
38
	{
39
		$this->opts[$option] = $value;
40 1
	}
41 1
42 1
	/**
43
	* sets the login credentials
44
	* @param string $user the username
45
	* @param string $pass the password
46
	*/
47
	public function setCredentials($user, $pass)
48
	{
49
		$this->setopt(CURLOPT_USERPWD, $user.':'.$pass);
50
	}
51
52
	/**
53
	* sets the output format
54
	*
55
	* @param string $format can be any of xml,json,yaml,simplexml
56
	*/
57
	public function setFormat($format)
58
	{
59
		if ($format != 'xml' && $format != 'json' && $format != 'yaml' && $format != 'simplexml') {
60
			error_log('setFormat requires that the format is xml, json, yaml or simplexml');
61
			return;
62
		} else {
63
			$this->format = $format;
64
		}
65
	}
66
67
	/**
68
	* @param string $function
69
	* @param array $args
70
	* @return array|mixed|void
71
	*/
72
	private function get($function, $args = [])
73
	{
74
		if (!$function) {
75
			error_log('cPanelLicensing::get requires that a function is defined');
76
			return;
77
		}
78
		if ($this->format != 'simplexml') {
79
			$args['output'] = $this->format;
80
		}
81
		\StatisticClient::tick('CPanel', str_replace('XML', '', $function));
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...
82
		$query = 'https://manage2.cpanel.net/'.$function.'?'.http_build_query($args);
83
		$this->setopt(CURLOPT_URL, $query);
84
		$this->curl = curl_init();
85
		foreach ($this->opts as $option => $value) {
86
			curl_setopt($this->curl, $option, $value);
87
		}
88
		$result = curl_exec($this->curl);
89
		curl_close($this->curl);
90
		if ($result == false) {
91
			$errno = curl_errno($this->curl);
92
			$error = curl_error($this->curl);
93
			error_log('cPanelLicensing::get failed with error #'.$errno.' "'.$error.'"');
94
			\StatisticClient::report('CPanel', str_replace('XML', '', $function), false, $errno, $error, STATISTICS_SERVER);
0 ignored issues
show
Bug introduced by
The constant Detain\Cpanel\STATISTICS_SERVER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
95
			return;
96
		}
97
		\StatisticClient::report('CPanel', str_replace('XML', '', $function), true, 0, '', STATISTICS_SERVER);
98
		if ($this->format == 'simplexml') {
99
			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

99
			/** @scrutinizer ignore-call */ function_requirements('xml2array');
Loading history...
100
			$result = xml2array($result, 1, 'attribute');
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

100
			$result = /** @scrutinizer ignore-call */ xml2array($result, 1, 'attribute');
Loading history...
101
			if (!isset($result[str_replace('.cgi', '', $function)])) {
102
				myadmin_log('licenses', 'warning', json_encode($result), __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

102
				/** @scrutinizer ignore-call */ myadmin_log('licenses', 'warning', json_encode($result), __LINE__, __FILE__);
Loading history...
103
			}
104
			$result = $result[str_replace('.cgi', '', $function)];
105
			$result = $this->formatResult($result);
106
			return $result;
107
		} else {
108
			return $result;
109
		}
110
	}
111
112
	/**
113
	* formats the response
114
	* @param array $result the result array to format
115
	* @return array the rormatted arrray
116
	*/
117
	private function formatResult($result)
118
	{
119
		if (is_array($result)) {
0 ignored issues
show
introduced by
The condition is_array($result) is always true.
Loading history...
120
			foreach ($result as $key => $value) {
121
				if (is_array($value)) {
122
					if (isset($value['attr']) && is_array($value['attr'])) {
123
						$result[$key] = $value['attr'];
124
					} else {
125
						$result[$key] = $this->formatResult($value);
126
					}
127
				}
128
			}
129
		}
130
		return $result;
131
	}
132
133
	/**
134
	* @param $id
135
	* @return int
136
	*/
137
	private function validateID($id)
138
	{
139
		if (preg_match("/^(L|P|G)?\d*$/", $id)) {
140
			return 1;
141
		} else {
142
			return 0;
143
		}
144
	}
145
146
	/**
147
	* @param $ipAddress
148
	* @return int
149
	*/
150
	private function validateIP($ipAddress)
151
	{
152
		return preg_match("/^\d*\.\d*\.\d*\.\d*$/", $ipAddress);
153
	}
154
155
	/**
156
	* @param $args
157
	* @return array|mixed|void
158
	*/
159
	public function reactivateLicense($args)
160
	{
161
		if (!array_key_exists('liscid', $args)) {
162
			error_log('cpanelLicensing::reactivateLicense requires that the argument array contains element liscid');
163
			return;
164
		}
165
		if (!$this->validateID($args['liscid'])) {
166
			error_log('The liscid passed to cpanelLicenseing::reactivateLicense was invalid');
167
			return;
168
		}
169
		return $this->get('XMLlicenseReActivate.cgi', $args);
170
	}
171
172
	/**
173
	* @param $args
174
	* @return array|mixed|void
175
	*/
176
	public function expireLicense($args)
177
	{
178
		if (!array_key_exists('liscid', $args)) {
179
			error_log('cPanelLicensing::expireLicense requires that liscid elements exists in the array passed to it');
180
			return;
181
		}
182
		if (!$this->validateID($args['liscid'])) {
183
			error_log('the liscense ID passed to cpanelLicensing::expireLiscense was invalid');
184
			return;
185
		}
186
		return $this->get('XMLlicenseExpire.cgi', $args);
187
	}
188
189
	/**
190
	* @param $args
191
	* @return array|mixed|void
192
	*/
193
	public function extendOnetimeUpdates($args)
194
	{
195
		if (!array_key_exists('ip', $args)) {
196
			error_log('cpanelLicensing::extendOnetimeUpdates requires that the element ip exists in the array is passed to it');
197
			return;
198
		}
199
		if (!$this->validateIP($args['ip'])) {
200
			error_log('cpanelLicensing::extendOnetimeUpdates was passed an invalid ip');
201
			return;
202
		}
203
		return $this->get('XMLonetimeext.cgi', $args);
204
	}
205
206
	/**
207
	* @param $args
208
	* @return array|mixed|void
209
	*/
210
	public function changeip($args)
211
	{
212
		if (!array_key_exists('oldip', $args) || !array_key_exists('newip', $args)) {
213
			error_log('cpanelLicensing::changeip requires that oldip and newip elements exist in the array passed to it');
214
			return;
215
		}
216
		if (!$this->validateIP($args['newip'])) {
217
			error_log('the newip passed to cpanelLicensing::changeip was invalid');
218
			return;
219
		}
220
		return $this->get('XMLtransfer.cgi', $args);
221
	}
222
223
	/**
224
	* @param $args
225
	* @return array|mixed|void
226
	*/
227
	public function requestTransfer($args)
228
	{
229
		if (!array_key_exists('ip', $args) || !array_key_exists('groupid', $args) || !array_key_exists('packagegroup', $args)) {
230
			error_log('cpanelLicensing::requestTransfer requires that ip, groupid and packageid elements exist in the array passed to it');
231
			return;
232
		}
233
		if (!$this->validateID($args['groupid'])) {
234
			error_log('The groupid passed to cpanelLicensing::requestTransfer is invalid');
235
			return;
236
		}
237
		if (!$this->validateID($args['packageid'])) {
238
			error_log('The package id passed to cpanelLicensing::requestTransfer is invalid');
239
			return;
240
		}
241
		if (!$this->validateIP($args['ip'])) {
242
			error_log('the ip passed to cpanelLicensing::requestTransfer was invalid');
243
			return;
244
		}
245
		return $this->get('XMLtransferRequest.cgi', $args);
246
	}
247
248
	/**
249
	* @param $args
250
	* @return array|mixed|void
251
	*/
252
	public function activateLicense($args)
253
	{
254
		if (!array_key_exists('ip', $args) || !array_key_exists('groupid', $args) || !array_key_exists('packageid', $args)) {
255
			error_log('cpanelLicensing::activateLicense requires that ip, groupid and packageid elements exist in the array passed to it');
256
			return;
257
		}
258
		if (!$this->validateID($args['groupid'])) {
259
			error_log('cpanelLicensing::acivateLicense was passed an invalid groupid');
260
			return;
261
		}
262
		if (!$this->validateIP($args['ip'])) {
263
			error_log('cpanelLicensing::activateLicense was passed an invalid IP');
264
			return;
265
		}
266
		if (!$this->validateID($args['packageid'])) {
267
			error_log('cpanelLicensing::activateLicense was passed an invalid packageid');
268
			return;
269
		}
270
		$args['legal'] = 1;
271
		return $this->get('XMLlicenseAdd.cgi', $args);
272
	}
273
274
	/**
275
	* @param $args
276
	* @return array|mixed|void
277
	*/
278
	public function addPickupPass($args)
279
	{
280
		if (!array_key_exists('pickup', $args)) {
281
			error_log('cPanelLicensing::addPickupPass requires a pickup param');
282
			return;
283
		}
284
		return $this->get('XMLaddPickupPass.cgi', $args);
285
	}
286
287
	/**
288
	* @param $args
289
	* @return array|mixed|void
290
	*/
291
	public function registerAuth($args)
292
	{
293
		if (!array_key_exists('user', $args)) {
294
			error_log('cPanelLicensing::registerAuth requires a user param');
295
			return;
296
		}
297
		if (!array_key_exists('pickup', $args)) {
298
			error_log('cPanelLicensing::registerAuth requires a pickup param');
299
			return;
300
		}
301
		if (!array_key_exists('service', $args)) {
302
			error_log('cPanelLicensing::registerAuth requires a service param');
303
			return;
304
		}
305
		$response = $this->get('XMLregisterAuth.cgi', $args);
306
		if ($this->format == 'simplexml') {
307
			$this->setCredentials($args['user'], $response['key']);
308
		}
309
		return $response;
310
	}
311
312
	/**
313
	* return s a list of groups
314
	*
315
	* @return array|mixed|void
316
	*/
317
	public function fetchGroups()
318
	{
319
		return $this->get('XMLgroupInfo.cgi');
320
	}
321
322
	/**
323
	* fetches license risk data
324
	*
325
	* @param array $args
326
	* @return array|mixed|void
327
	*/
328
	public function fetchLicenseRiskData($args = [])
329
	{
330
		if (!array_key_exists('ip', $args)) {
331
			error_log('cpanelLicensing::fetchLicenseRiskData requires that ip exists as an element in the array is passed to it');
332
			return;
333
		}
334
		if (!$this->validateIP($args['ip'])) {
335
			error_log('cpanelLicensing::fetchLicenseRiskData was passed an invalid ip');
336
			return;
337
		}
338
		return $this->get('XMLsecverify.cgi', $args);
339
	}
340
341
	/**
342
	* gets raw license information
343
	*
344
	* @param array $args optional array of arguments
345
	* @return array|mixed|void
346
	*/
347
	public function fetchLicenseRaw($args = [])
348
	{
349
		$args = array_merge(['all' => 1], $args);
350
		if (!array_key_exists('ip', $args)) {
351
			error_log('cpanelLicesning::fetchLicenseRaw requires that ip exists as an element in the array is passed to it');
352
			return;
353
		}
354
		if (!$this->validateIP($args['ip'])) {
355
			error_log('cpanelLicensing::fetchLicenseRaw was passed an invalid ip');
356
			return;
357
		}
358
		return $this->get('XMLRawlookup.cgi', $args);
359
	}
360
361
	/**
362
	* gets license information
363
	*
364
	* @param array $args optional array of arguments
365
	* @return array|mixed|void
366
	*/
367
	public function fetchLicenseId($args = [])
368
	{
369
		$args = array_merge(['all' => 1], $args);
370
		if (!array_key_exists('ip', $args)) {
371
			error_log('cpanelLicensing::getLicenseId requires that an IP is passed to it');
372
			return;
373
		}
374
		if (!$this->validateIP($args['ip'])) {
375
			error_log('cpanelLicensing::fetchLicenseId was passed an invalid ip');
376
			return;
377
		}
378
		return $this->get('XMLlookup.cgi', $args);
379
	}
380
381
	/**
382
	* @return array|mixed|void
383
	*/
384
	public function fetchPackages($expand = false)
385
	{
386
		return $this->get('XMLpackageInfo.cgi', ($expand === true ? ['expand' => 1] : []));
387
	}
388
389
	/**
390
	* gets a list of licenses
391
	*
392
	* @param array $args optional arguments
393
	* @return array|mixed|void
394
	*/
395
	public function fetchLicenses($args = [])
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

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

395
	public function fetchLicenses(/** @scrutinizer ignore-unused */ $args = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
396
	{
397
		return $this->get('XMLlicenseInfo.cgi');
398
	}
399
400
	/**
401
	* return sa list of expired licenses
402
	* @return array|mixed|void
403
	*/
404
	public function fetchExpiredLicenses()
405
	{
406
		return $this->fetchLicenses(['expired' => '1']);
407
	}
408
409
	/**
410
	* @param $search
411
	* @param $xmlObj
412
	* @return string|void
413
	*/
414
	public function findKey($search, $xmlObj)
415
	{
416
		$xmlObj = (array) $xmlObj;
417
		if (array_key_exists('packages', $xmlObj)) {
418
			$type = 'packages';
419
		} elseif (array_key_exists('groups', $xmlObj)) {
420
			$type = 'groups';
421
		} else {
422
			error_log('cPanelLicensing::findKey with an object that is not groups or packages');
423
			return;
424
		}
425
		foreach ((array) $xmlObj[$type] as $element) {
426
			foreach ((array) $element as $key => $value) {
427
				if ((string) $value == $search) {
428
					return (string) $key;
429
				}
430
			}
431
		}
432
		error_log("Could not find $type that matches $search");
433
	}
434
}
435