Test Failed
Branch master (1adabf)
by Joe
03:05
created

Cpanel::__construct()   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
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Detain\Cpanel;
4
/**
5
 * Class Cpanel
6
 *
7
 * @package Detain\Cpanel
8
 */
9
class Cpanel {
10
	public $format;
11
	public $curl;
12
	public $opts;
13
14
	/**
15
	 * Cpanel constructor.
16
	 *
17
	 * @param $user
18
	 * @param $pass
19
	 */
20
	public function __construct($user, $pass) {
21
	$this->opts = [];
22
		$this->format = 'simplexml';
23
		$this->setCredentials($user, $pass);
24
		$this->setopt(CURLOPT_RETURNTRANSFER, 1);
25
		$this->setopt(CURLOPT_USERAGENT, 'cPanel Licensing Agent (php) 3.5');
26
	}
27
28
	/**
29
	 * @param $option
30
	 * @param $value
31
	 */
32 1
	public function setopt($option, $value) {
33 1
		$this->opts[$option] = $value;
34 1
	}
35
36
	/**
37
	 * @param $user
38
	 * @param $pass
39
	 */
40 1
	public function setCredentials($user, $pass) {
41 1
		$this->setopt(CURLOPT_USERPWD, $user. ':' .$pass);
42 1
	}
43
44
	/**
45
	 * @param $format
46
	 */
47
	public function setFormat($format) {
48
		if ($format != 'xml' && $format != 'json' && $format != 'yaml' && $format != 'simplexml') {
49
			error_log('setFormat requires that the format is xml, json, yaml or simplexml');
50
			return;
51
		} else {
52
			$this->format = $format;
53
		}
54
	}
55
56
	/**
57
	 * @param       $function
58
	 * @param array $args
59
	 * @return array|mixed|void
60
	 */
61
	private function get($function, $args = []) {
62
		if (!$function) {
63
			error_log('cPanelLicensing::get requires that a function is defined');
64
			return;
65
		}
66
		if ($this->format != 'simplexml')
67
			$args['output'] = $this->format;
68
		$query = 'https://manage2.cpanel.net/' .$function. '?' .http_build_query($args);
69
		$this->setopt(CURLOPT_URL, $query);
70
		$this->curl = curl_init();
71
		foreach ($this->opts as $option => $value)
72
			curl_setopt($this->curl, $option, $value);
73
		$result = curl_exec($this->curl);
74
		curl_close($this->curl);
75
		if ($result == FALSE) {
76
			error_log('cPanelLicensing::get failed: "' .curl_error($this->curl). '"');
77
			return;
78
		}
79
		if ($this->format == 'simplexml') {
80
			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

80
			/** @scrutinizer ignore-call */ 
81
   function_requirements('xml2array');
Loading history...
81
			$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

81
			$result = /** @scrutinizer ignore-call */ xml2array($result, 1, 'attribute');
Loading history...
82
			if (!isset($result[str_replace('.cgi', '', $function)]))
83
				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

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