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
|
1 |
|
public function setopt($option, $value) { |
19
|
1 |
|
$this->opts[$option] = $value; |
20
|
1 |
|
} |
21
|
|
|
|
22
|
1 |
|
public function setCredentials($user, $pass) { |
23
|
1 |
|
$this->setopt(CURLOPT_USERPWD, $user.":".$pass); |
24
|
1 |
|
} |
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
|
|
|
if (!isset($result[str_replace('.cgi', '', $function)])) |
59
|
|
|
myadmin_log('licenses', 'warning', json_encode($result), __LINE__, __FILE__); |
60
|
|
|
$result = $result[str_replace('.cgi', '', $function)]; |
61
|
|
|
$result = $this->formatResult($result); |
62
|
|
|
return $result; |
63
|
|
|
} else { |
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* formats the response |
70
|
|
|
* @param array $result the result array to format |
71
|
|
|
* @return array the rormatted arrray |
72
|
|
|
*/ |
73
|
|
|
private function formatResult($result) { |
74
|
|
|
if (is_array($result)) { |
75
|
|
|
foreach ($result as $key => $value) { |
76
|
|
|
if (is_array($value)) { |
77
|
|
|
if (isset($value['attr']) && is_array($value['attr'])) { |
78
|
|
|
$result[$key] = $value['attr']; |
79
|
|
|
} else { |
80
|
|
|
$result[$key] = $this->formatResult($value); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function validateID($id) { |
89
|
|
|
if (preg_match("/^(L|P|G)?\d*$/", $id)) { |
90
|
|
|
return 1; |
91
|
|
|
} else { |
92
|
|
|
return 0; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function validateIP($ipAddress) { |
97
|
|
|
return preg_match("/^\d*\.\d*\.\d*\.\d*$/", $ipAddress); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function reactivateLicense($args) { |
|
|
|
|
101
|
|
|
if (!array_key_exists('liscid', $args)) { |
102
|
|
|
error_log("cpanelLicensing::reactivateLicense requires that the argument array contains element liscid"); |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
if (!$this->validateID($args['liscid'])) { |
106
|
|
|
error_log("The liscid passed to cpanelLicenseing::reactivateLicense was invalid"); |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
return $this->get("XMLlicenseReActivate.cgi", $args); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function expireLicense($args) { |
|
|
|
|
113
|
|
|
if (!array_key_exists("liscid", $args)) { |
114
|
|
|
error_log("cPanelLicensing::expireLicense requires that liscid elements exists in the array passed to it"); |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
if (!$this->validateID($args['liscid'])) { |
118
|
|
|
error_log("the liscense ID passed to cpanelLicensing::expireLiscense was invalid"); |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
return $this->get("XMLlicenseExpire.cgi", $args); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
View Code Duplication |
public function extendOnetimeUpdates($args) { |
|
|
|
|
125
|
|
|
if (!array_key_exists("ip", $args)) { |
126
|
|
|
error_log("cpanelLicensing::extendOnetimeUpdates requires that the element ip exists in the array is passed to it"); |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
if (!$this->validateIP($args['ip'])) { |
130
|
|
|
error_log("cpanelLicensing::extendOnetimeUpdates was passed an invalid ip"); |
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
return $this->get("XMLonetimeext.cgi", $args); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
public function changeip($args) { |
|
|
|
|
137
|
|
|
if (!array_key_exists("oldip", $args) || !array_key_exists("newip", $args)) { |
138
|
|
|
error_log("cpanelLicensing::changeip requires that oldip and newip elements exist in the array passed to it"); |
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
if (!$this->validateIP($args["newip"])) { |
142
|
|
|
error_log("the newip passed to cpanelLicensing::changeip was invalid"); |
143
|
|
|
return; |
144
|
|
|
} |
145
|
|
|
return $this->get("XMLtransfer.cgi", $args); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
public function requestTransfer($args) { |
|
|
|
|
149
|
|
|
if (!array_key_exists("ip", $args) || !array_key_exists("groupid", $args) || !array_key_exists("packagegroup", $args)) { |
150
|
|
|
error_log("cpanelLicensing::requestTransfer requires that ip, groupid and packageid elements exist in the array passed to it"); |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
if (!$this->validateID($args["groupid"])) { |
154
|
|
|
error_log("The groupid passed to cpanelLicensing::requestTransfer is invalid"); |
155
|
|
|
return; |
156
|
|
|
} |
157
|
|
|
if (!$this->validateID($args["packageid"])) { |
158
|
|
|
error_log("The package id passed to cpanelLicensing::requestTransfer is invalid"); |
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
if (!$this->validateIP($args['ip'])) { |
162
|
|
|
error_log("the ip passed to cpanelLicensing::requestTransfer was invalid"); |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
return $this->get("XMLtransferRequest.cgi", $args); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
View Code Duplication |
public function activateLicense($args) { |
|
|
|
|
169
|
|
|
if (!array_key_exists("ip", $args) || !array_key_exists("groupid", $args) || !array_key_exists("packageid", $args)) { |
170
|
|
|
error_log("cpanelLicensing::activateLicense requires that ip, groupid and packageid elements exist in the array passed to it"); |
171
|
|
|
return; |
172
|
|
|
} |
173
|
|
|
if (!$this->validateID($args['groupid'])) { |
174
|
|
|
error_log("cpanelLicensing::acivateLicense was passed an invalid groupid"); |
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
if (!$this->validateIP($args['ip'])) { |
178
|
|
|
error_log("cpanelLicensing::activateLicense was passed an invalid IP"); |
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
if (!$this->validateID($args['packageid'])) { |
182
|
|
|
error_log("cpanelLicensing::activateLicense was passed an invalid packageid"); |
183
|
|
|
return; |
184
|
|
|
} |
185
|
|
|
$args['legal'] = 1; |
186
|
|
|
return $this->get("XMLlicenseAdd.cgi", $args); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function addPickupPass($args) { |
190
|
|
|
if (!array_key_exists("pickup", $args)) { |
191
|
|
|
error_log("cPanelLicensing::addPickupPass requires a pickup param"); |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
return $this->get("XMLaddPickupPass.cgi", $args); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function registerAuth($args) { |
198
|
|
|
if (!array_key_exists("user", $args)) { |
199
|
|
|
error_log("cPanelLicensing::registerAuth requires a user param"); |
200
|
|
|
return; |
201
|
|
|
} |
202
|
|
|
if (!array_key_exists("pickup", $args)) { |
203
|
|
|
error_log("cPanelLicensing::registerAuth requires a pickup param"); |
204
|
|
|
return; |
205
|
|
|
} |
206
|
|
|
if (!array_key_exists("service", $args)) { |
207
|
|
|
error_log("cPanelLicensing::registerAuth requires a service param"); |
208
|
|
|
return; |
209
|
|
|
} |
210
|
|
|
$response = $this->get("XMLregisterAuth.cgi", $args); |
211
|
|
|
if ($this->format == "simplexml") { |
212
|
|
|
$this->setCredentials($args["user"], $response["key"]); |
213
|
|
|
} |
214
|
|
|
return $response; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function fetchGroups() { |
218
|
|
|
return $this->get("XMLgroupInfo.cgi"); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
View Code Duplication |
public function fetchLicenseRiskData($args) { |
|
|
|
|
222
|
|
|
if (!array_key_exists("ip", $args)) { |
223
|
|
|
error_log("cpanelLicensing::fetchLicenseRiskData requires that ip exists as an element in the array is passed to it"); |
224
|
|
|
return; |
225
|
|
|
} |
226
|
|
|
if (!$this->validateIP($args['ip'])) { |
227
|
|
|
error_log("cpanelLicensing::fetchLicenseRiskData was passed an invalid ip"); |
228
|
|
|
return; |
229
|
|
|
} |
230
|
|
|
return $this->get("XMLsecverify.cgi", $args); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
View Code Duplication |
public function fetchLicenseRaw($args) { |
|
|
|
|
234
|
|
|
$args = array_merge(array("all" => 1), $args); |
235
|
|
|
if (!array_key_exists("ip", $args)) { |
236
|
|
|
error_log("cpanelLicesning::fetchLicenseRaw requires that ip exists as an element in the array is passed to it"); |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
if (!$this->validateIP($args['ip'])) { |
240
|
|
|
error_log("cpanelLicensing::fetchLicenseRaw was passed an invalid ip"); |
241
|
|
|
return; |
242
|
|
|
} |
243
|
|
|
return $this->get("XMLRawlookup.cgi", $args); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
View Code Duplication |
public function fetchLicenseId($args) { |
|
|
|
|
247
|
|
|
$args = array_merge(array("all" => 1), $args); |
248
|
|
|
if (!array_key_exists('ip', $args)) { |
249
|
|
|
error_log("cpanelLicensing::getLicenseId requires that an IP is passed to it"); |
250
|
|
|
return; |
251
|
|
|
} |
252
|
|
|
if (!$this->validateIP($args['ip'])) { |
253
|
|
|
error_log("cpanelLicensing::fetchLicenseId was passed an invalid ip"); |
254
|
|
|
return; |
255
|
|
|
} |
256
|
|
|
return $this->get("XMLlookup.cgi", $args); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function fetchPackages() { |
260
|
|
|
return $this->get("XMLpackageInfo.cgi"); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function fetchLicenses() { |
264
|
|
|
return $this->get("XMLlicenseInfo.cgi"); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function fetchExpiredLicenses() { |
268
|
|
|
return $this->get("XMLlicenseInfo.cgi", array("expired" => '1')); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function findKey($search, $xmlObj) { |
272
|
|
|
$xmlObj = (array) $xmlObj; |
273
|
|
|
if (array_key_exists("packages", $xmlObj)) { |
274
|
|
|
$type = "packages"; |
275
|
|
|
} else if (array_key_exists("groups", $xmlObj)) { |
276
|
|
|
$type = "groups"; |
277
|
|
|
} else { |
278
|
|
|
error_log("cPanelLicensing::findKey with an object that is not groups or packages"); |
279
|
|
|
return; |
280
|
|
|
} |
281
|
|
|
foreach ((array) $xmlObj[$type] as $element) { |
282
|
|
|
foreach ((array) $element as $key => $value) { |
283
|
|
|
if ((string) $value == $search) { |
284
|
|
|
return (string) $key; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
error_log("Could not find $type that matches $search"); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
|
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.