Passed
Push — master ( 335a7b...04d2b9 )
by Joe
01:52
created

Cpanel::setCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Detain\Cpanel;
4
5
class Cpanel {
6
	public $format;
7
	public $curl;
8
	public $opts;
9
10
	public function __construct($user, $pass) {
11
	$this->opts = [];
12
		$this->format = "simplexml";
13
		$this->setCredentials($user, $pass);
14
		$this->setopt(CURLOPT_RETURNTRANSFER, 1);
15
		$this->setopt(CURLOPT_USERAGENT, 'cPanel Licensing Agent (php) 3.5');
16
	}
17
18
	public function setopt($option, $value) {
19
		$this->opts[$option] = $value;
20
	}
21
22
	public function setCredentials($user, $pass) {
23
		$this->setopt(CURLOPT_USERPWD, $user.":".$pass);
24
	}
25
26
	public function setFormat($format) {
27
		if ($format != "xml" && $format != "json" && $format != "yaml" && $format != "simplexml") {
28
			error_log("setFormat requires that the format is xml, json, yaml or simplexml");
29
			return;
30
		} else {
31
			$this->format = $format;
32
		}
33
	}
34
35
	private function get($function, $args = array()) {
36
		if (!$function) {
37
			error_log("cPanelLicensing::get requires that a function is defined");
38
			return;
39
		}
40
		if ($this->format != "simplexml") {
41
			$args['output'] = $this->format;
42
		}
43
		$query = "https://manage2.cpanel.net/".$function."?".http_build_query($args);
44
		$this->setopt(CURLOPT_URL, $query);
45
		$this->curl = curl_init();
46
		foreach ($this->opts as $option => $value) {
47
			curl_setopt($this->curl, $option, $value);
48
		}
49
		$result = curl_exec($this->curl);
50
		curl_close($this->curl);
51
		if ($result == FALSE) {
52
			error_log("cPanelLicensing::get failed: \"".curl_error($this->curl)."\"");
53
			return;
54
		}
55
		if ($this->format == "simplexml") {
56
			function_requirements('xml2array');
57
			$result = xml2array($result, 1, 'attribute');
58
			$result = $result[str_replace('.cgi', '', $function)];
59
			$result = $this->formatResult($result);			
60
			return $result;
61
		} else {
62
			return $result;
63
		}
64
	}
65
66
	/**
67
	 * formats the response
68
	 * @param array $result the result array to format
69
	 * @return array the rormatted arrray
70
	 */
71
	private function formatResult($result) {
72
		if (is_array($result)) {
73
			foreach ($result as $key => $value) {
74
				if (is_array($value)) {
75
					if (isset($value['attr']) && is_array($value['attr'])) {
76
						$result[$key] = $value['attr'];
77
					} else {
78
						$result[$key] = $this->formatResult($value);
79
					}
80
				}
81
			}
82
		}
83
		return $result;
84
	}
85
86
	private function validateID($id) {
87
		if (preg_match("/^(L|P|G)?\d*$/", $id)) {
88
			return 1;
89
		} else {
90
			return 0;
91
		}
92
	}
93
94
	private function validateIP($ipAddress) {
95
		return preg_match("/^\d*\.\d*\.\d*\.\d*$/", $ipAddress);
96
	}
97
98 View Code Duplication
	public function reactivateLicense($args) {
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...
99
		if (!array_key_exists('liscid', $args)) {
100
			error_log("cpanelLicensing::reactivateLicense requires that the argument array contains element liscid");
101
			return;
102
		}
103
		if (!$this->validateID($args['liscid'])) {
104
			error_log("The liscid passed to cpanelLicenseing::reactivateLicense was invalid");
105
			return;
106
		}
107
		return $this->get("XMLlicenseReActivate.cgi", $args);
108
	}
109
110 View Code Duplication
	public function expireLicense($args) {
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...
111
		if (!array_key_exists("liscid", $args)) {
112
			error_log("cPanelLicensing::expireLicense requires that liscid elements exists in the array passed to it");
113
			return;
114
		}
115
		if (!$this->validateID($args['liscid'])) {
116
			error_log("the liscense ID passed to cpanelLicensing::expireLiscense was invalid");
117
			return;
118
		}
119
		return $this->get("XMLlicenseExpire.cgi", $args);
120
	}
121
122 View Code Duplication
	public function extendOnetimeUpdates($args) {
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...
123
		if (!array_key_exists("ip", $args)) {
124
			error_log("cpanelLicensing::extendOnetimeUpdates requires that the element ip exists in the array is passed to it");
125
			return;
126
		}
127
		if (!$this->validateIP($args['ip'])) {
128
			error_log("cpanelLicensing::extendOnetimeUpdates was passed an invalid ip");
129
			return;
130
		}
131
		return $this->get("XMLonetimeext.cgi", $args);
132
	}
133
134 View Code Duplication
	public function changeip($args) {
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...
135
		if (!array_key_exists("oldip", $args) || !array_key_exists("newip", $args)) {
136
			error_log("cpanelLicensing::changeip requires that oldip and newip elements exist in the array passed to it");
137
			return;
138
		}
139
		if (!$this->validateIP($args["newip"])) {
140
			error_log("the newip passed to cpanelLicensing::changeip was invalid");
141
			return;
142
		}
143
		return $this->get("XMLtransfer.cgi", $args);
144
	}
145
146 View Code Duplication
	public function requestTransfer($args) {
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...
147
		if (!array_key_exists("ip", $args) || !array_key_exists("groupid", $args) || !array_key_exists("packagegroup", $args)) {
148
			error_log("cpanelLicensing::requestTransfer requires that ip, groupid and packageid elements exist in the array passed to it");
149
			return;
150
		}
151
		if (!$this->validateID($args["groupid"])) {
152
			error_log("The groupid passed to cpanelLicensing::requestTransfer is invalid");
153
			return;
154
		}
155
		if (!$this->validateID($args["packageid"])) {
156
			error_log("The package id passed to cpanelLicensing::requestTransfer is invalid");
157
			return;
158
		}
159
		if (!$this->validateIP($args['ip'])) {
160
			error_log("the ip passed to cpanelLicensing::requestTransfer was invalid");
161
			return;
162
		}
163
		return $this->get("XMLtransferRequest.cgi", $args);
164
	}
165
166 View Code Duplication
	public function activateLicense($args) {
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...
167
		if (!array_key_exists("ip", $args) || !array_key_exists("groupid", $args) || !array_key_exists("packageid", $args)) {
168
			error_log("cpanelLicensing::activateLicense requires that ip, groupid and packageid elements exist in the array passed to it");
169
			return;
170
		}
171
		if (!$this->validateID($args['groupid'])) {
172
			error_log("cpanelLicensing::acivateLicense was passed an invalid groupid");
173
			return;
174
		}
175
		if (!$this->validateIP($args['ip'])) {
176
			error_log("cpanelLicensing::activateLicense was passed an invalid IP");
177
			return;
178
		}
179
		if (!$this->validateID($args['packageid'])) {
180
			error_log("cpanelLicensing::activateLicense was passed an invalid packageid");
181
			return;
182
		}
183
		$args['legal'] = 1;
184
		return $this->get("XMLlicenseAdd.cgi", $args);
185
	}
186
187
	public function addPickupPass($args) {
188
		if (!array_key_exists("pickup", $args)) {
189
			error_log("cPanelLicensing::addPickupPass requires a pickup param");
190
			return;
191
		}
192
		return $this->get("XMLaddPickupPass.cgi", $args);
193
	}
194
195
	public function registerAuth($args) {
196
		if (!array_key_exists("user", $args)) {
197
			error_log("cPanelLicensing::registerAuth requires a user param");
198
			return;
199
		}
200
		if (!array_key_exists("pickup", $args)) {
201
			error_log("cPanelLicensing::registerAuth requires a pickup param");
202
			return;
203
		}
204
		if (!array_key_exists("service", $args)) {
205
			error_log("cPanelLicensing::registerAuth requires a service param");
206
			return;
207
		}
208
		$response = $this->get("XMLregisterAuth.cgi", $args);
209
		if ($this->format == "simplexml") {
210
			$this->setCredentials($args["user"], $response["key"]);
211
		}
212
		return $response;
213
	}
214
215
	public function fetchGroups() {
216
		return $this->get("XMLgroupInfo.cgi");
217
	}
218
219 View Code Duplication
	public function fetchLicenseRiskData($args) {
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...
220
		if (!array_key_exists("ip", $args)) {
221
			error_log("cpanelLicensing::fetchLicenseRiskData requires that ip exists as an element in the array is passed to it");
222
			return;
223
		}
224
		if (!$this->validateIP($args['ip'])) {
225
			error_log("cpanelLicensing::fetchLicenseRiskData was passed an invalid ip");
226
			return;
227
		}
228
		return $this->get("XMLsecverify.cgi", $args);
229
	}
230
231 View Code Duplication
	public function fetchLicenseRaw($args) {
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...
232
		$args = array_merge(array("all" => 1), $args);
233
		if (!array_key_exists("ip", $args)) {
234
			error_log("cpanelLicesning::fetchLicenseRaw requires that ip exists as an element in the array is passed to it");
235
			return;
236
		}
237
		if (!$this->validateIP($args['ip'])) {
238
			error_log("cpanelLicensing::fetchLicenseRaw was passed an invalid ip");
239
			return;
240
		}
241
		return $this->get("XMLRawlookup.cgi", $args);
242
	}
243
244 View Code Duplication
	public function fetchLicenseId($args) {
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...
245
		$args = array_merge(array("all" => 1), $args);
246
		if (!array_key_exists('ip', $args)) {
247
			error_log("cpanelLicensing::getLicenseId requires that an IP is passed to it");
248
			return;
249
		}
250
		if (!$this->validateIP($args['ip'])) {
251
			error_log("cpanelLicensing::fetchLicenseId was passed an invalid ip");
252
			return;
253
		}
254
		return $this->get("XMLlookup.cgi", $args);
255
	}
256
257
	public function fetchPackages() {
258
		return $this->get("XMLpackageInfo.cgi");
259
	}
260
261
	public function fetchLicenses() {
262
		return $this->get("XMLlicenseInfo.cgi");
263
	}
264
265
	public function fetchExpiredLicenses() {
266
		return $this->get("XMLlicenseInfo.cgi", array("expired" => '1'));
267
	}
268
269
	public function findKey($search, $xmlObj) {
270
		$xmlObj = (array) $xmlObj;
271
		if (array_key_exists("packages", $xmlObj)) {
272
			$type = "packages";
273
		} else if (array_key_exists("groups", $xmlObj)) {
274
			$type = "groups";
275
		} else {
276
			error_log("cPanelLicensing::findKey with an object that is not groups or packages");
277
			return;
278
		}
279
		foreach ((array) $xmlObj[$type] as $element) {
280
			foreach ((array) $element as $key => $value) {
281
				if ((string) $value == $search) {
282
					return (string) $key;
283
				}
284
			}
285
		}
286
		error_log("Could not find $type that matches $search");
287
	}
288
}
289
290