Skip to content

Commit

Permalink
Allow domain check on global whois or ISPConfig database #24
Browse files Browse the repository at this point in the history
  • Loading branch information
ole committed Oct 26, 2019
1 parent 507e738 commit a949754
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 23 deletions.
30 changes: 24 additions & 6 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class WPISPConfig3
Login with your account on http://#HOSTNAME#:8080",
'default_domain' => 'yourdomain.tld',
'sender_name' => 'Your Sevice name',
'domain_check_global' => 1,
'domain_check_expiration' => 600,
'user_roles' => ['customer', 'subscriber']
];

Expand Down Expand Up @@ -159,8 +161,14 @@ private function onUpdateSettings()
if (get_magic_quotes_gpc()) {
$_POST = array_map('stripslashes_deep', $_POST);
}

self::$OPTIONS = $_POST;

foreach (self::$OPTIONS as $k => &$v) {
if (isset($_POST[$k])) {
$v = $_POST[$k];
} elseif (in_array($k, ['domain_check_global', 'skip_ssl', 'confirm'])) {
$v = !empty($_POST[$k]) ? 1 : 0;
}
}

if ($this->update_options()) {
?><div class="updated"><p> <?php _e('Settings saved', 'wp-ispconfig3');?></p></div><?php
Expand Down Expand Up @@ -213,6 +221,13 @@ public function DisplaySettings()
self::getField('default_domain', 'Default Domain');
self::getField('sender_name', 'Sender name');
?>
<h3><?php _e('Domain Check') ?></h3>
<p>Decide how WP-ISPConfig 3 checks for domain availability.<br />You can either validate against ISPConfig domains or use the whois command to check for free domains</p>
<p>To test the domain check, use the shortcode "[Ispconfig submit_url='...']" on your favorite front page</p>
<?php
self::getField('domain_check_global', 'Global domain check with <strong>whois</strong><br /><i>Unhook this to validate against ISPConfig domains only</i>', 'checkbox');
self::getField('domain_check_expiration', 'ISPConfig domain name cache expiration (in seconds)', 'number');
?>
<h3><?php _e('User Mapping') ?></h3>
<p>Choose the below WordPress user roles to match the clients stored in ISPConfig3</p>
<?php
Expand Down Expand Up @@ -308,9 +323,12 @@ public static function getField($name, $title, $type = 'text', $args = [])
*/
protected function load_options()
{
$opt = get_option(self :: OPTION_KEY);
if (!empty($opt)) {
self::$OPTIONS = $opt;
$opt = get_option(self::OPTION_KEY, []);

foreach (self::$OPTIONS as $k => &$v) {
if (isset($opt[$k])) {
$v = $opt[$k];
}
}
}

Expand All @@ -321,7 +339,7 @@ protected function load_options()
*/
public function update_options()
{
return update_option(self :: OPTION_KEY, self::$OPTIONS);
return update_option(self::OPTION_KEY, self::$OPTIONS);
}

/**
Expand Down
54 changes: 47 additions & 7 deletions ispconfig-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,52 @@ public function DisplayWrapper($attr, $content)
return $data;
}

public static function isDomainAvailable($dom)
public function IsDomainAvailable($dom)
{
$result = shell_exec("whois $dom");
if (WPISPConfig3::$OPTIONS['domain_check_global']) {
$result = shell_exec("whois $dom");

if (preg_match("/^(No whois server is known|This TLD has no whois server)/m", $result)) {
return -1;
} elseif (preg_match("/^(Status: AVAILABLE|Status: free|NOT FOUND|" . $dom . " no match|No match for \"(.*?)\"\.)$/im", $result)) {
return 1;
if (preg_match("/^(No whois server is known|This TLD has no whois server)/m", $result)) {
return -1;
}

return preg_match("/^(Status: AVAILABLE|Status: free|NOT FOUND|" . $dom . " no match|No match for \"(.*?)\"\.)$/im", $result) ? 1: 0;
} else {
$cacheExpiration = intval(WPISPConfig3::$OPTIONS['domain_check_expiration']); // 10 minutes caching the domains
$cachedDomains = get_option('ispconfig_cached_domains', ['expires' => 0]);

if ($cachedDomains['expires'] < time()) {
$all_client_ids = $this->GetAllClientIds();

$groupIds = [];

foreach ($all_client_ids as $id) {
$groupIds[] = $this->GetGroupIdByClientId($id);
}

$result = $this->GetClientSitesByGroupIds($groupIds);

$domains = array_map(function ($item) {
return $item['domain'];
}, $result);

update_option('ispconfig_cached_domains', ['domains' => $domains,'expires' => time() + $cacheExpiration]);
} else {
$domains = $cachedDomains['domains'];
}

return in_array($dom, $domains) ? 0 : 1;
}
}

return 0;
public function GetAllClientIds()
{
return $this->soap->client_get_all($this->session_id);
}

public function GetGroupIdByClientId($clientId)
{
return $this->soap->client_get_groupid($this->session_id, $clientId);
}

/**
Expand Down Expand Up @@ -129,6 +164,11 @@ public function GetClientSites($user_name)
return $this->soap->client_get_sites_by_user($this->session_id, $client['userid'], $client['default_group']);
}

public function GetClientSitesByGroupIds($groupIds = [])
{
return $this->soap->client_get_sites_by_user($this->session_id, 0, implode(',', $groupIds));
}

public function GetClientDatabases($user_name)
{
$client = $this->GetClientByUser($user_name);
Expand Down
47 changes: 37 additions & 10 deletions ispconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public function AJAX_ispconfig_whois_callback()
{
$dom = strtolower($_POST['domain']);

$ok = self::isDomainAvailable($dom);
$this->withSoap();

$ok = $this->IsDomainAvailable($dom);

$this->closeSoap();

$result = ['text' => '', 'class' => ''];

Expand Down Expand Up @@ -72,23 +76,46 @@ public function Display($attr, $content = null)
$result .= '<div class="ispconfig-msg ispconfig-msg-error">The parameter \'submit_url\' is missing</div>';
}

$input_attr = ['id' => 'txtDomain'];
$input_attr = ['id' => 'txtDomain', 'placeholder' => 'E.g. yourdomain.net'];
?>
<script>
if($ === undefined) {
var $ = jQuery;
}

function toggleCheck() {
$('#submit').hide();
$('#check').prop('disabled', false);
$('#check').show();
}

function toggleSubmit() {
$('#submit').show();
$('#check').hide();
}

function checkDomain() {
var domain = jQuery('#txtDomain').val();
jQuery.post(ajaxurl, { action: 'ispconfig_whois', 'domain': domain }, null, 'json').done(function(resp){
jQuery('.ispconfig-box').html('<div class="ispconfig-msg ' + resp.class + '">' + resp.text + '</div>');
var domain = $('#txtDomain').val();

$('#check').prop('disabled', true);

$.post(ajaxurl, { action: 'ispconfig_whois', 'domain': domain }, null, 'json').done(function(resp){
$('.ispconfig-box').html('<div class="ispconfig-msg ' + resp.class + '">' + resp.text + '</div>');

if (resp.class !== 'ispconfig-msg-error') {
jQuery('#submit').show();
jQuery('#check').hide();
toggleSubmit();
} else {
jQuery('#submit').hide();
jQuery('#check').show();
toggleCheck();
}
});
}

$(function() {
$('#txtDomain').focus(function() {
toggleCheck();
$(this).select();
});
});
</script>
<div class="ispconfig-box"></div>
<div>&nbsp;</div>
Expand All @@ -99,7 +126,7 @@ function checkDomain() {
<input id="submit" type="submit" value="Continue" style="display: none" />
</form>
<?php if (current_user_can('administrator')) : ?>
<div style="font-size: 90%; margin-top: .5em">
<div style="font-size: 80%; margin-top: .5em">
ADMIN NOTICE: <a href="https://github.com/ole1986/wp-ispconfig3/wiki/Extending-wp-ispconfig3-with-custom-shortcodes" target="_blank">Learn more about custom shortcodes for WP-ISPConfig3</a>
</div>
<?php endif; ?>
Expand Down

1 comment on commit a949754

@MachineITSvcs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Good job!

Please sign in to comment.