Skip to content

Commit b929453

Browse files
committed
updated cpanel webhosting code so catchs all the exceptions
1 parent 0123a25 commit b929453

1 file changed

Lines changed: 126 additions & 38 deletions

File tree

src/Plugin.php

Lines changed: 126 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,14 @@ function_requirements('whm_api');
146146
$password = generateRandomString(10, 2, 2, 2, 1);
147147
$options['password'] = $password;
148148
myadmin_log(self::$module, 'info', "Trying Password {$options['password']}", __LINE__, __FILE__, self::$module, $serviceClass->getId());
149-
$response = $whm->xmlapi_query('createacct', $options);
149+
try {
150+
$response = $whm->xmlapi_query('createacct', $options);
151+
} catch (\Exception $e) {
152+
$event['success'] = false;
153+
myadmin_log('cpanel', 'error', 'Caught Exception from createacct call: '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
154+
$event->stopPropagation();
155+
return;
156+
}
150157
request_log(self::$module, $serviceClass->getCustid(), __FUNCTION__, 'cpanel', 'createacct', $options, $response, $serviceClass->getId());
151158
myadmin_log(self::$module, 'info', 'Response: '.str_replace('\n', "\n", $response), __LINE__, __FILE__, self::$module, $serviceClass->getId());
152159
$response = json_decode($response, true);
@@ -159,7 +166,14 @@ function_requirements('whm_api');
159166
$username = mb_substr($username, 1);
160167
$options['username'] = $username;
161168
myadmin_log(self::$module, 'info', 'Trying Username '.$options['username'], __LINE__, __FILE__, self::$module, $serviceClass->getId());
162-
$response = $whm->xmlapi_query('createacct', $options);
169+
try {
170+
$response = $whm->xmlapi_query('createacct', $options);
171+
} catch (\Exception $e) {
172+
$event['success'] = false;
173+
myadmin_log('cpanel', 'error', 'Caught Exception from initial createacct call: '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
174+
$event->stopPropagation();
175+
return;
176+
}
163177
request_log(self::$module, $serviceClass->getCustid(), __FUNCTION__, 'cpanel', 'createacct', $options, $response, $serviceClass->getId());
164178
myadmin_log(self::$module, 'info', 'Response: '.str_replace('\n', "\n", $response), __LINE__, __FILE__, self::$module, $serviceClass->getId());
165179
$response = json_decode($response, true);
@@ -176,7 +190,14 @@ function_requirements('whm_api');
176190
$username = mb_substr($username, 1);
177191
$options['username'] = $username;
178192
myadmin_log(self::$module, 'info', 'Trying Username '.$options['username'], __LINE__, __FILE__, self::$module, $serviceClass->getId());
179-
$response = $whm->xmlapi_query('createacct', $options);
193+
try {
194+
$response = $whm->xmlapi_query('createacct', $options);
195+
} catch (\Exception $e) {
196+
$event['success'] = false;
197+
myadmin_log('cpanel', 'error', 'Caught Exception from initial createacct call: '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
198+
$event->stopPropagation();
199+
return;
200+
}
180201
request_log(self::$module, $serviceClass->getCustid(), __FUNCTION__, 'cpanel', 'createacct', $options, $response, $serviceClass->getId());
181202
myadmin_log(self::$module, 'info', 'Response: '.str_replace('\n', "\n", $response), __LINE__, __FILE__, self::$module, $serviceClass->getId());
182203
$response = json_decode($response, true);
@@ -186,15 +207,36 @@ function_requirements('whm_api');
186207
$event['success'] = true;
187208
$ip = $response['result'][0]['options']['ip'];
188209
if (isset($options['bwlimit']) && $options['bwlimit'] != 'unlimited') {
189-
$response3 = $whm->limitbw($username, $options['bwlimit']);
210+
try {
211+
$response3 = $whm->limitbw($username, $options['bwlimit']);
212+
} catch (\Exception $e) {
213+
$event['success'] = false;
214+
myadmin_log('cpanel', 'error', 'Caught Exception from limitbw call: '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
215+
$event->stopPropagation();
216+
return;
217+
}
190218
request_log(self::$module, $serviceClass->getCustid(), __FUNCTION__, 'cpanel', 'limitbw', ['username' => $username, 'options' => $options['bwlimit']], $response3, $serviceClass->getId());
191219
myadmin_log(self::$module, 'info', 'Response: '.str_replace('\n', "\n", strip_tags($response3)), __LINE__, __FILE__, self::$module, $serviceClass->getId());
192220
}
193221
if ($reseller === true) {
194-
$response2 = $whm->setupreseller($username, false);
222+
try {
223+
$response2 = $whm->setupreseller($username, false);
224+
} catch (\Exception $e) {
225+
$event['success'] = false;
226+
myadmin_log('cpanel', 'error', 'Caught Exception from setupreseller call: '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
227+
$event->stopPropagation();
228+
return;
229+
}
195230
request_log(self::$module, $serviceClass->getCustid(), __FUNCTION__, 'cpanel', 'setupreseller', ['username' => $username], $response2, $serviceClass->getId());
196231
myadmin_log(self::$module, 'info', "Response: {$response2}", __LINE__, __FILE__, self::$module, $serviceClass->getId());
197-
$response3 = $whm->listacls();
232+
try {
233+
$response3 = $whm->listacls();
234+
} catch (\Exception $e) {
235+
$event['success'] = false;
236+
myadmin_log('cpanel', 'error', 'Caught Exception from listacls call: '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
237+
$event->stopPropagation();
238+
return;
239+
}
198240

199241
$acls = json_decode($response3, true);
200242
request_log(self::$module, $serviceClass->getCustid(), __FUNCTION__, 'cpanel', 'listacls', [], $response, $serviceClass->getId());
@@ -243,23 +285,44 @@ function_requirements('whm_api');
243285
'acl-upgrade-account' => 1, // Allow the reseller to upgrade and downgrade accounts.
244286
'acllist' => 'reseller'
245287
];
246-
$response = $whm->saveacllist($acl);
288+
try {
289+
$response = $whm->saveacllist($acl);
290+
} catch (\Exception $e) {
291+
$event['success'] = false;
292+
myadmin_log('cpanel', 'error', 'Caught Exception from saveacllist call: '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
293+
$event->stopPropagation();
294+
return;
295+
}
247296
myadmin_log(self::$module, 'info', str_replace('\n', "\n", json_encode($response)), __LINE__, __FILE__, self::$module, $serviceClass->getId());
248297
request_log(self::$module, $serviceClass->getCustid(), __FUNCTION__, 'cpanel', 'saveacllist', $acl, $response, $serviceClass->getId());
249298
myadmin_log(self::$module, 'info', 'Reseller ACL Created', __LINE__, __FILE__, self::$module, $serviceClass->getId());
250299
} else {
251300
myadmin_log(self::$module, 'info', 'Reseller ACL Exists', __LINE__, __FILE__, self::$module, $serviceClass->getId());
252301
}
253302
$request = ['reseller' => $username, 'acllist' => 'reseller'];
254-
$response = $whm->setacls($request);
303+
try {
304+
$response = $whm->setacls($request);
305+
} catch (\Exception $e) {
306+
$event['success'] = false;
307+
myadmin_log('cpanel', 'error', 'Caught Exception from setacls call: '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
308+
$event->stopPropagation();
309+
return;
310+
}
255311
myadmin_log(self::$module, 'info', str_replace('\n', "\n", json_encode($response)), __LINE__, __FILE__, self::$module, $serviceClass->getId());
256312
request_log(self::$module, $serviceClass->getCustid(), __FUNCTION__, 'cpanel', 'setacls', $request, $response, $serviceClass->getId());
257313
myadmin_log(self::$module, 'info', 'Reseller assigned to ACL', __LINE__, __FILE__, self::$module, $serviceClass->getId());
258-
$whm->setresellerlimits([
259-
'user' => $username,
260-
'enable_account_limit' => 1,
261-
'account_limit' => $account_limit,
262-
]);
314+
try {
315+
$whm->setresellerlimits([
316+
'user' => $username,
317+
'enable_account_limit' => 1,
318+
'account_limit' => $account_limit,
319+
]);
320+
} catch (\Exception $e) {
321+
$event['success'] = false;
322+
myadmin_log('cpanel', 'error', 'Caught Exception from setresellerlimits call: '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
323+
$event->stopPropagation();
324+
return;
325+
}
263326
}
264327
$db = get_module_db(self::$module);
265328
$username = $db->real_escape($username);
@@ -298,7 +361,14 @@ function_requirements('whm_api');
298361
function_requirements('add_dns_record');
299362
$response = add_dns_record(14426, 'wh'.$serviceClass->getId(), $ip, 'A', 86400, 0, true);
300363
myadmin_log(self::$module, 'info', 'Response: '.str_replace('\n', "\n", json_encode($response)), __LINE__, __FILE__, self::$module, $serviceClass->getId());
301-
$response = $whm->park($options['username'], 'wh'.$serviceClass->getId().'.ispot.cc', '');
364+
try {
365+
$response = $whm->park($options['username'], 'wh'.$serviceClass->getId().'.ispot.cc', '');
366+
} catch (\Exception $e) {
367+
$event['success'] = false;
368+
myadmin_log('cpanel', 'error', 'Caught Exception from park call: '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
369+
$event->stopPropagation();
370+
return;
371+
}
302372
myadmin_log(self::$module, 'info', 'Response: '.str_replace('\n', "\n", json_encode($response)), __LINE__, __FILE__, self::$module, $serviceClass->getId());
303373
$event['success'] = true;
304374
} else {
@@ -323,16 +393,16 @@ public static function getReactivate(GenericEvent $event)
323393
$ip = $serverdata[$settings['PREFIX'].'_ip'];
324394
function_requirements('whm_api');
325395
$user = 'root';
326-
$whm = new \xmlapi($ip);
327-
//$whm->set_debug('true');
328-
$whm->set_port('2087');
329-
$whm->set_protocol('https');
330-
$whm->set_output('json');
331-
$whm->set_auth_type('hash');
332-
$whm->set_user($user);
333-
$whm->set_hash($hash);
334-
//$whm = whm_api('faith.interserver.net');
335396
try {
397+
$whm = new \xmlapi($ip);
398+
//$whm->set_debug('true');
399+
$whm->set_port('2087');
400+
$whm->set_protocol('https');
401+
$whm->set_output('json');
402+
$whm->set_auth_type('hash');
403+
$whm->set_user($user);
404+
$whm->set_hash($hash);
405+
//$whm = whm_api('faith.interserver.net');
336406
if (in_array('reseller', explode(',', $event['field1']))) {
337407
$response = json_decode($whm->unsuspendreseller($serviceClass->getUsername()), true);
338408
} else {
@@ -364,16 +434,16 @@ public static function getDeactivate(GenericEvent $event)
364434
$ip = $serverdata[$settings['PREFIX'].'_ip'];
365435
function_requirements('whm_api');
366436
$user = 'root';
367-
$whm = new \xmlapi($ip);
368-
//$whm->set_debug('true');
369-
$whm->set_port('2087');
370-
$whm->set_protocol('https');
371-
$whm->set_output('json');
372-
$whm->set_auth_type('hash');
373-
$whm->set_user($user);
374-
$whm->set_hash($hash);
375-
//$whm = whm_api('faith.interserver.net');
376437
try {
438+
$whm = new \xmlapi($ip);
439+
//$whm->set_debug('true');
440+
$whm->set_port('2087');
441+
$whm->set_protocol('https');
442+
$whm->set_output('json');
443+
$whm->set_auth_type('hash');
444+
$whm->set_user($user);
445+
$whm->set_hash($hash);
446+
//$whm = whm_api('faith.interserver.net');
377447
if (in_array('reseller', explode(',', $event['field1']))) {
378448
$response = json_decode($whm->suspendreseller($serviceClass->getUsername(), 'Canceled Service'), true);
379449
} else {
@@ -416,22 +486,40 @@ function_requirements('whm_api');
416486
$whm->set_hash($hash);
417487
//$whm = whm_api('faith.interserver.net');
418488
if (trim($serviceClass->getUsername()) != '') {
419-
if (in_array('reseller', explode(',', $event['field1']))) {
420-
$response = json_decode($whm->terminatereseller($serviceClass->getUsername(), true), true);
421-
} else {
422-
$response = json_decode($whm->removeacct($serviceClass->getUsername(), false), true);
489+
try {
490+
if (in_array('reseller', explode(',', $event['field1']))) {
491+
$response = json_decode($whm->terminatereseller($serviceClass->getUsername(), true), true);
492+
} else {
493+
$response = json_decode($whm->removeacct($serviceClass->getUsername(), false), true);
494+
}
495+
} catch (\Exception $e) {
496+
$event['success'] = false;
497+
myadmin_log('cpanel', 'error', 'removeacct('.$serviceClass->getUsername().') tossed exception '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
498+
add_output('Caught exception: '.$e->getMessage().'<br>');
423499
}
500+
424501
myadmin_log(self::$module, 'info', str_replace('\n', "\n", json_encode($response)), __LINE__, __FILE__, self::$module, $serviceClass->getId());
425502
} else {
426503
myadmin_log(self::$module, 'info', "Skipping WHMAPI/Server Removal for {$serviceClass->getHostname()} because username is blank", __LINE__, __FILE__, self::$module, $serviceClass->getId());
427504
}
428-
$dnsr = json_decode($whm->dumpzone($serviceClass->getHostname()), true);
505+
try {
506+
$dnsr = json_decode($whm->dumpzone($serviceClass->getHostname()), true);
507+
} catch (\Exception $e) {
508+
myadmin_log('cpanel', 'error', 'dumpzone('.$serviceClass->getHostname().') tossed exception '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
509+
add_output('Caught exception: '.$e->getMessage().'<br>');
510+
}
511+
429512
if ($dnsr['result'][0]['status'] == 1) {
430513
$db = get_module_db(self::$module);
431514
$db->query("select * from {$settings['TABLE']} where {$settings['PREFIX']}_hostname='{$serviceClass->getHostname()}' and {$settings['PREFIX']}_id != {$serviceClass->getId()} and {$settings['PREFIX']}_status = 'active'", __LINE__, __FILE__);
432515
if ($db->num_rows() == 0) {
433516
myadmin_log(self::$module, 'info', "Removing Hanging DNS entry for {$serviceClass->getHostname()}", __LINE__, __FILE__, self::$module, $serviceClass->getId());
434-
$whm->killdns($serviceClass->getHostname());
517+
try {
518+
$whm->killdns($serviceClass->getHostname());
519+
} catch (\Exception $e) {
520+
myadmin_log('cpanel', 'error', 'killdns('.$serviceClass->getHostname().') tossed exception '.$e->getMessage(), __LINE__, __FILE__, self::$module, $serviceClass->getId());
521+
add_output('Caught exception: '.$e->getMessage().'<br>');
522+
}
435523
} else {
436524
myadmin_log(self::$module, 'info', "Skipping Removing DNS entry for {$serviceClass->getHostname()} because other non deleted sites w/ the same hostname exist", __LINE__, __FILE__, self::$module, $serviceClass->getId());
437525
}

0 commit comments

Comments
 (0)