Skip to content

Commit

Permalink
Merge pull request #184 from openprovider/provisioning-module
Browse files Browse the repository at this point in the history
premium dns
  • Loading branch information
sibgatulin-danila authored Sep 1, 2021
2 parents bb82ec8 + 90514df commit a2f54ad
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use OpenProvider\API\APITools;
use OpenProvider\API\DomainTransfer;
use OpenProvider\API\DomainRegistration;
use OpenProvider\WhmcsRegistrar\enums\DatabaseTable;
use OpenProvider\WhmcsRegistrar\src\PremiumDomain;
use OpenProvider\WhmcsRegistrar\src\Handle;
use OpenProvider\WhmcsRegistrar\src\AdditionalFields;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use WeDevelopCoffee\wPower\Controllers\BaseController;
use WeDevelopCoffee\wPower\Core\Core;
use WHMCS\Database\Capsule;

/**
* Class DomainController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ class DomainRegistration extends \OpenProvider\API\AutoloadConstructor
* @var string
*/
public $billingHandle = null;



/**
* @var array
*/
public $nameServers = null;

/**
Expand Down Expand Up @@ -92,5 +94,4 @@ class DomainRegistration extends \OpenProvider\API\AutoloadConstructor
* @var boolean
*/
public $isDnssecEnabled;

}
2 changes: 2 additions & 0 deletions modules/registrars/openprovider/enums/DatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ class DatabaseTable
const Currencies = 'tblcurrencies';

const Configuration = 'tblconfiguration';

const Hosting = 'tblhosting';
}
28 changes: 28 additions & 0 deletions modules/servers/openprovidersectigodns/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use OpenProvider\API\ApiV1;
use OpenProvider\Logger;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;

const OPENPROVIDER_HOST = 'https://api.openprovider.eu';

function getApi($username, $password): ?ApiV1
{
$api = new ApiV1(new Logger(), new CamelCaseToSnakeCaseNameConverter, new idna_convert);
$api->getConfiguration()->setHost(OPENPROVIDER_HOST);

$tokenRequest = $api->call('generateAuthTokenRequest', [
'username' => $username,
'password' => $password
]);

if ($tokenRequest->getCode() != 0) {
return null;
}

$token = $tokenRequest->getData()['token'];

$api->getConfiguration()->setToken($token);

return $api;
}
24 changes: 24 additions & 0 deletions modules/servers/openprovidersectigodns/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Example:
* 'domain.com' => ['domain', 'com']
*
* @param string $domain
* @return array domain array ['domain name', 'domain extension']
*/
function getDomainArrayFromDomain(string $domain): array
{
$domainArray = explode('.', $domain);
if (count($domainArray) < 2) {
throw new \Exception('Domain name has no tld.');
}

$domainSld = explode('.', $domain)[0];
$domainTld = substr(str_replace($domainSld, '', $domain), 1);

return [
'name' => $domainSld,
'extension' => $domainTld
];
}
34 changes: 34 additions & 0 deletions modules/servers/openprovidersectigodns/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use WHMCS\Database\Capsule;

include_once 'api.php';

add_hook('ShoppingCartValidateDomain', 1, function($vars) {
if ($vars['domainoption'] != 'owndomain') {
return;
}

$productId = $_REQUEST['pid'];
if (!$productId) {
return 'You have no product id in query parameters: "pid" does not exist!';
}

$productRow = Capsule::table('tblproducts')
->where('id', $productId)
->first();

$api = getApi($productRow->configoption1, $productRow->configoption2);

if (is_null($api)) {
return 'Something was wrong! Check your credentials and try again';
}

$domainRequest = $api->call('searchDomainRequest', [
'fullName' => sprintf('%s.%s', $vars['sld'], $vars['tld'])
]);

if ($domainRequest->getCode() != 0 || count($domainRequest->getData()['results']) == 0) {
return 'This domain not found in Openprovider or something went wrong!';
}
});
131 changes: 131 additions & 0 deletions modules/servers/openprovidersectigodns/openprovidersectigodns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}

include_once 'api.php';
include_once 'functions.php';

const CREATE_DNS_ZONE_TYPE = 'master';

const SUCCESS_MESSAGE = 'success';

const MODULE_NAME = 'Openprovider-premiumDNS';
const API_VERSION = '1.1'; // Use API Version 1.1
const REQUIRES_SERVER = true; // Set true if module requires a server to work
const DEFAULT_NON_SSL_PORT = '1111'; // Default Non-SSL Connection Port
const DEFAULT_SSL_PORT = '1112'; // Default SSL Connection Port
const SERVICE_SINGLE_SIGN_ON_LABEL = 'Login to Panel as User';
const ADMIN_SINGLE_SIGN_ON_LABEL = 'Login to Panel as Admin';

const CONFIG_OPTION_LOGIN_NAME = 'Login';
const CONFIG_OPTION_LOGIN_DESCRIPTION = 'Enter Openprovider login';

const CONFIG_OPTION_PASSWORD_NAME = 'Password';
const CONFIG_OPTION_PASSWORD_DESCRIPTION = 'Enter Openprovider password';

/**
* Define module related meta data.
*
* Values returned here are used to determine module related abilities and
* settings.
*
* @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
*
* @return array
*/
function openprovidersectigodns_MetaData()
{
return array(
'DisplayName' => MODULE_NAME,
'APIVersion' => API_VERSION,
'RequiresServer' => REQUIRES_SERVER,
'DefaultNonSSLPort' => DEFAULT_NON_SSL_PORT,
'DefaultSSLPort' => DEFAULT_SSL_PORT,
'ServiceSingleSignOnLabel' => SERVICE_SINGLE_SIGN_ON_LABEL,
'AdminSingleSignOnLabel' => ADMIN_SINGLE_SIGN_ON_LABEL,
);
}

function openprovidersectigodns_ConfigOptions()
{
return [
// a text field type allows for single line text input
CONFIG_OPTION_LOGIN_NAME => [
'Type' => 'text',
'Description' => CONFIG_OPTION_LOGIN_DESCRIPTION,
'SimpleMode' => true,
],
// a password field type allows for masked text input
CONFIG_OPTION_PASSWORD_NAME => [
'Type' => 'password',
'Description' => CONFIG_OPTION_PASSWORD_DESCRIPTION,
'SimpleMode' => true,
],
];
}

/**
* Provision a new instance of a product/service.
*
* Attempt to provision a new instance of a given product/service. This is
* called any time provisioning is requested inside of WHMCS. Depending upon the
* configuration, this can be any of:
* * When a new order is placed
* * When an invoice for a new order is paid
* * Upon manual request by an admin user
*
* @param array $params common module parameters
*
* @see https://developers.whmcs.com/provisioning-modules/module-parameters/
*
* @return string "success" or an error message
*/
function openprovidersectigodns_CreateAccount(array $params)
{
$api = getApi($params['configoption1'], $params['configoption2']);

if (is_null($api)) {
return 'provisioning module cannot configure api. Maybe your credentials are incorrect.';
}

// get dns zone if exist
$dnsZoneResponse = $api->call('retrieveZoneDnsRequest', [
'name' => $params['domain'],
]);

// if zone exists
if ($dnsZoneResponse->getCode() == 0) {
$modifyZoneResponse = $api->call('modifyZoneDnsRequest', [
'name' => $params['domain'],
'premiumDNS' => true,
]);

if ($modifyZoneResponse->getCode() != 0) {
return $modifyZoneResponse->getMessage();
}

return SUCCESS_MESSAGE;
}

// if zone does not exist
try {
$domainArray = getDomainArrayFromDomain($params['domain']);
} catch (Exception $e) {
return $e->getMessage();
}

$createDnsZoneResponse = $api->call('createZoneDnsRequest', [
'domain' => $domainArray,
'records' => [],
'type' => CREATE_DNS_ZONE_TYPE,
'premiumDNS' => true,
]);

if ($createDnsZoneResponse->getCode() != 0) {
return $createDnsZoneResponse->getMessage();
}

return SUCCESS_MESSAGE;
}

0 comments on commit a2f54ad

Please sign in to comment.