Skip to content

Commit

Permalink
CHG: General code improvement and fixes
Browse files Browse the repository at this point in the history
CHG: Disabled Usage Updates (needs a full rework)
NEW: Added support for Cancellation Hold
FIX: Updated hook code for WHMCS v8 compatability
  • Loading branch information
tsiedsma committed Sep 30, 2020
1 parent 9944ed9 commit 4ac6777
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 79 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Stripped down the provisioning module, you only need to specify the plan to provision instead of all plan options
### Fixed
- All domains are made lower case prior to making API calls
- All domains are made lower case prior to making API calls

## [1.0.9] - 2020-09-30
### Added
- Added support for Cancellation Hold
### Changed
- General code improvement and fixes
- Disabled Usage Updates (needs a full rework)
### FIXED
- Updated hook code for WHMCS v8 compatability
49 changes: 43 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ By Troy Siedsma (Lithium Hosting)

## Requirements:
WHMCS v7.1+
ApisCP must be on version 3.1
https://forums.apnscp.com/t/apnscp-3-1-development-thread/68/5?u=msaladna
ApisCP must be on version 3.2.5

## Configuring
- Create Plans in ApisCP
Expand All @@ -21,8 +20,22 @@ https://forums.apnscp.com/t/apnscp-3-1-development-thread/68/5?u=msaladna

Edit clientareaproductdetails and add the following after the last {if} before the first <div>
```smarty
{if $apiscp_banned}
{include file="$template/includes/alert.tpl" type="error" title="IP Ban Notice" msg="We found your IP Address in the Blacklist on in the Panel. We've taken the liberty of removing the IP ban and added your IP to your accounts whitelist.<br>Your IP was detected in the following Jails:<ul>{foreach from=$apiscp_jails item=jail}<li>{$jail}</li>{/foreach}"}
{if $apisVars['is_banned']}
<div class="alert alert-danger">
<h3>IP Ban Notice</h3>
We found your IP Address in the Blacklist in the Panel. <br>Your IP was detected in the following Jails:
<ul>
{foreach from=$apisVars['jails'] item=jail}
<li>{$jail}</li>
{/foreach}
</ul>
We've removed the ban on your IP but any additional suspicious activity may result in banning your IP Again.
{if $apisVars['rampart_enabled']}
<br>
You should also be sure to add your IP {$apisVars['ip']} to the Whitelist
<a href="clientarea.php?action=productdetails&amp;id={$serviceid}&amp;dosinglesignon=1&amp;app=whitelist" target="_blank" title="Panel Whitelist" class="alert-link">here</a>.
{/if}
</div>
{/if}
```

Expand All @@ -36,7 +49,27 @@ Edit clientareaproductdetails and add the following after the last {if} before t
- Changing Plans
- SSO from WHMCS for Client and Admin
- SSO with custom links to different apps
- Automatically unban and whitelist a user's IP
- Automatically unban a user's IP
- Cancellation Hold

## Cancellation Hold
This feature allows you to defer termination of cancelled and terminated accounts for 30 days.
To enable, uncomment lines 255 and 256 in apnscp.php and comment out lines 249 and 250

Comment:
```php
$opts['force'] = 'true';
$client->admin_delete_site($site_domain, $opts);
```
By commenting out that section, you are preventing account deletion.

Uncomment:
```php
$opts['reason'] = 'Customer Requested Cancellation';
$client->admin_deactivate_site($site_domain, $opts);
```
By uncommenting that section, you are forcing account suspension. This means the site will effectively be suspended until the automated process purges the account from the server.
To enable that feature, uncomment the last action hook block in hooks.php

## Summary
The ApisCP provisioning module for WHMCS allows you to integrate your billing system with your server management panel so new user accounts will be automatically provisioned, suspended and terminated as needed. Users can change their password as well as use the Single Sign-On (SSO) feature to seamlessly transition from WHMCS to ApisCP.
Expand All @@ -46,4 +79,8 @@ This product is licensed under the GPL v3 (see LICENSE file). Basically, you ca
This is meant to be free for the benefit of the community. Help us by improving with Pull Requests!

## Contributing
Submit a PR and have fun!
Submit a PR and have fun!
I am a developer by hobby, not profession so don't judge me and I won't judge you :P

## Need Help?
Join us in the [ApisCP Discord](https://discord.gg/5bQr3Dm) in the #whmcs channel!
120 changes: 74 additions & 46 deletions modules/servers/apnscp/apnscp.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ function apnscp_ConfigOptions()
return [
'apnscp Plan' => [
'Type' => 'dropdown',
'Options' => $plans,
'Default' => 'basic',
'Description' => 'Choose a plan (auto populated)',
'Description' => 'Choose a plan (auto populated)<br>Format: &lt;server&gt; - &lt;plan&gt;',
'Loader' => 'apnscp_getPlans',
'SimpleMode' => true,
],
];
}
Expand Down Expand Up @@ -120,7 +121,7 @@ function apnscp_CreateAccount(array $params)
logModuleCall(
'apnscp',
__FUNCTION__,
['params' => $params, 'options' => $opts],
['params' => $params, 'options' => $opts, 'CommandString' => $cliCommand],
$e->getMessage(),
$e->getTraceAsString()
);
Expand Down Expand Up @@ -150,12 +151,14 @@ function apnscp_SuspendAccount(array $params)
$apnscp_apikey = $params['serverpassword'];
$site_domain = strtolower($params['domain']);

$opts['reason'] = $params['suspendreason'];

try
{
$adminId = \session_id();
$client = Connector::create_client($apnscp_apikey, $apnscp_apiendpoint, $adminId);

$client->admin_deactivate_site($site_domain);
$client->admin_deactivate_site($site_domain, $opts);
}
catch (Exception $e)
{
Expand Down Expand Up @@ -235,15 +238,22 @@ function apnscp_TerminateAccount(array $params)
$apnscp_apikey = $params['serverpassword'];
$site_domain = strtolower($params['domain']);

$opts = [];
$opts['force'] = 'true';

try
{
$adminId = \session_id();
$client = Connector::create_client($apnscp_apikey, $apnscp_apiendpoint, $adminId);

/*
* To disable cancellation hold, uncomment these lines
*/
$opts['force'] = 'true';
$client->admin_delete_site($site_domain, $opts);
############################################################################################
/*
* To enable cancellation hold, uncomment these lines
*/
// $opts['reason'] = 'Customer Requested Cancellation';
// $client->admin_deactivate_site($site_domain, $opts);
}
catch (Exception $e)
{
Expand Down Expand Up @@ -399,6 +409,7 @@ function apnscp_ServiceSingleSignOn(array $params)
'phpmyadmin',
'webapps',
'terminal',
'whitelist',
];

try
Expand Down Expand Up @@ -463,25 +474,34 @@ function apnscp_ServiceSingleSignOn(array $params)
function apnscp_ClientArea(array $params)
{
return [
'overrideDisplayTitle' => ucfirst($params['domain']),
'overrideDisplayTitle' => $params['domain'],
'tabOverviewReplacementTemplate' => 'overview.tpl',
];
}

function apnscp_getPlans()
{
$server = DB::table('tblservers')->where('type', 'apnscp')->first();
$apnscp_apiendpoint = ($server->secure === 'on' ? 'https' : 'http') . '://' . $server->hostname . ':' . ($server->secure === 'on' ? '2083' : '2082');
$apnscp_apikey = decrypt($server->password);
$servers = DB::table('tblservers')->where('type', 'apnscp')->get();
$adminId = \session_id();

try
{
$adminId = \session_id();
$client = Connector::create_client($apnscp_apikey, $apnscp_apiendpoint, $adminId);
foreach ($servers as $server)
{
$apnscp_apiendpoint = ($server->secure === 'on' ? 'https' : 'http') . '://' . $server->hostname . ':' . ($server->secure === 'on' ? '2083' : '2082');
$apnscp_apikey = decrypt($server->password);

$client = Connector::create_client($apnscp_apikey, $apnscp_apiendpoint, $adminId);

$plans = $client->admin_list_plans();
$plans = $client->admin_list_plans();

foreach ($plans as $plan)
{
$return[ $plan ] = $server->name . ' - ' . $plan;
}
}

return array_combine($plans, $plans);
return $return;
}
catch (Exception $e)
{
Expand All @@ -490,36 +510,44 @@ function apnscp_getPlans()
}
}

function apnscp_UsageUpdate($params)
{
$apnscp_apiendpoint = $params['serverhttpprefix'] . '://' . $params['serverhostname'] . ':' . $params['serverport'];
$apnscp_apikey = $params['serverpassword'];
$serverid = $params['serverid'];
$stats = [];

# Run connection to retrieve usage for all domains/accounts on $serverid
$adminId = \session_id();
$adminClient = Connector::create_client($apnscp_apikey, $apnscp_apiendpoint, $adminId);
$domains = $adminClient->admin_get_domains();
foreach ($domains as $domain)
{
$session_id = $adminClient->admin_hijack($domain);
$userClient = Connector::create_client($apnscp_apikey, $apnscp_apiendpoint, $session_id);
$bw = $userClient->site_get_bandwidth_usage();
$quota = $userClient->site_get_account_quota();

$stats[] = ['domain' => $domain, 'bw' => $bw, 'disk' => $quota];
}
/**
* This is a hot mess on servers with a large number of accounts.
* Disabled Usage Stats, takes way too long to run. Every single domain requires 3 API calls to Hijack, get BW and get Disk usage
* Multiplied by hundreds of accounts per server and many servers, this takes too long.
* Enable only if absolutely necessary. Please feel free to update and submit a PR in the repo. This definitely needs a rework and optimization!
*/

# Now loop through results and update DB
foreach ($stats AS $values)
{
update_query('tblhosting', [
'diskusage' => $values['disk']['qused'] / 1024,
'disklimit' => $values['disk']['qhard'] / 1024,
'bwusage' => ($values['bw']['used'] / 1024) / 1024,
'bwlimit' => ($values['bw']['total'] / 1024) / 1024,
'lastupdate' => 'now()',
], ['server' => $serverid, 'domain' => $values['domain']]);
}
}
//function apnscp_UsageUpdate($params)
//{
// $apnscp_apiendpoint = $params['serverhttpprefix'] . '://' . $params['serverhostname'] . ':' . $params['serverport'];
// $apnscp_apikey = $params['serverpassword'];
// $serverid = $params['serverid'];
// $stats = [];
//
// # Run connection to retrieve usage for all domains/accounts on $serverid
// $adminId = \session_id();
// $adminClient = Connector::create_client($apnscp_apikey, $apnscp_apiendpoint, $adminId);
// $domains = $adminClient->admin_get_domains();
// foreach ($domains as $domain)
// {
// $session_id = $adminClient->admin_hijack($domain);
// $userClient = Connector::create_client($apnscp_apikey, $apnscp_apiendpoint, $session_id);
// $bw = $userClient->site_get_bandwidth_usage();
// $quota = $userClient->site_get_account_quota();
//
// $stats[] = ['domain' => $domain, 'bw' => $bw, 'disk' => $quota];
// }
//
// # Now loop through results and update DB
// foreach ($stats AS $values)
// {
// update_query('tblhosting', [
// 'diskusage' => $values['disk']['qused'] / 1024,
// 'disklimit' => $values['disk']['qhard'] / 1024,
// 'bwusage' => ($values['bw']['used'] / 1024) / 1024,
// 'bwlimit' => ($values['bw']['total'] / 1024) / 1024,
// 'lastupdate' => 'now()',
// ], ['server' => $serverid, 'domain' => $values['domain']]);
// }
//}
Loading

0 comments on commit 4ac6777

Please sign in to comment.