Jump to content

I need a HOOK to create a Random Domain when ordering Web Hosting


Craft
Go to solution Solved by leemahoney3,

Recommended Posts

Hello,

I would like to Skip registering a domain name while ordering web hosting.

But as you know choosing a Domain name is required for the (createAccount) module to run, so I need to create a custom hook to generate a Random Domain name for the (createAccount) module when the customer checks out the order.

 

 

RequireDomain.png

Skip.png

Edited by Craft
Link to comment
Share on other sites

21 minutes ago, leemahoney3 said:

A random subdomain e.g. random.yourdomain.com or just a completely random domain e.g. randomdomain.com ?

Completely random domain, for example (jhsdfbuhasd.random)

I just want to pass any domain to the (createAccount) module to create the order on WHMCS + cPanel

Then the customer can ask to change the domain later to a real one!

Edited by Craft
Link to comment
Share on other sites

9 hours ago, Craft said:

Completely random domain, for example (jhsdfbuhasd.random)

I just want to pass any domain to the (createAccount) module to create the order on WHMCS + cPanel

Then the customer can ask to change the domain later to a real one!

Okay cool, I'll try put together a hook later if someone else doesn't in the meantime.

Link to comment
Share on other sites

Sorry for the delay, not sure if this is what you want?

It runs when you press the Create button (or if automatic setup on it'll run just before the module create happens) and generates a random, unique .com domain (you can configure the length and extension and the beginning of the hook)

<?php

use WHMCS\Service\Service;
use Illuminate\Support\Str;

add_hook('PreModuleCreate', 1, static function (array $params) {
    $extension = '.com';
    $domainLength = 15;

    $params = $params['params'];
    $serviceId = $params['serviceid'];
    $domain = $params['domain'];

    if (!empty($domain)) {
        return;
    }

    $unique = false;
    while (!$unique) {
        $domain = Str::lower(Str::random($domainLength)) . $extension;
        $check = Service::where('domain', $domain)->whereIn('domainstatus', ['Active', 'Suspended'])->count();
        if ($check === 0) {
            $unique = true;
        }
    }

    try {
        $service = Service::findOrFail($serviceId);
        $service->domain = $domain;
        $service->save();
    } catch (Throwable $e) {
        return ['abortcmd' => true];
    }

    return ['domain' => $domain];
});

 

Edited by leemahoney3
Link to comment
Share on other sites

34 minutes ago, leemahoney3 said:

Sorry for the delay, not sure if this is what you want?

It runs when you press the Create button (or if automatic setup on it'll run just before the module create happens) and generates a random, unique .com domain (you can configure the length and extension and the beginning of the hook)

<?php

use WHMCS\Service\Service;
use Illuminate\Support\Str;

add_hook('PreModuleCreate', 1, static function (array $params) {
    $extension = '.com';
    $domainLength = 15;

    $params = $params['params'];
    $serviceId = $params['serviceid'];
    $domain = $params['domain'];

    if (!empty($domain)) {
        return;
    }

    $unique = false;
    while (!$unique) {
        $domain = Str::lower(Str::random($domainLength)) . $extension;
        $check = Service::where('domain', $domain)->whereIn('domainstatus', ['Active', 'Suspended'])->count();
        if ($check === 0) {
            $unique = true;
        }
    }

    try {
        $service = Service::findOrFail($serviceId);
        $service->domain = $domain;
        $service->save();
    } catch (Throwable $e) {
        return ['abortcmd' => true];
    }

    return ['domain' => $domain];
});

 

That's awesome and what I need exactly 🙂

Just a few small modifications if you don't mind.

1. Could you please generate the string just letters without numbers?

2. The username is a required parameter same as the domain, could you please copy the generated domain to the username variable and then pass both parameters?

Username Required.png

Link to comment
Share on other sites

13 hours ago, Craft said:

hi @leemahoney3

sorry for annoying you, but I just want to make sure that you saw my previous comment if you can help more 🙂

 

Sorry! Only came across it now. I wasn't sure of the module you were using so purposely left out the username. Added in below and removed numbers from domain and username.

<?php

use WHMCS\Service\Service;
use Illuminate\Support\Str;

add_hook('PreModuleCreate', 1, static function (array $params) {
    $extension = '.com';
    $domainLength = 15;
  	$usernameLength = 8;

    $params = $params['params'];
    $serviceId = $params['serviceid'];
    $domain = $params['domain'];

    if (!empty($domain)) {
        return;
    }

    $uniqueDomain = false;
    while (!$uniqueDomain) {
        $domain = genRandomStr($domainLength) . $extension;
        $check = Service::where('domain', $domain)->whereIn('domainstatus', ['Active', 'Suspended'])->count();
        if ($check === 0) {
            $uniqueDomain = true;
        }
    }
  
    $uniqueUsername = false;
    while (!$uniqueUsername) {
        $username = genRandomStr($usernameLength);
        $check = Service::where('username', $username)->whereIn('domainstatus', ['Active', 'Suspended'])->count();
        if ($check === 0) {
            $uniqueUsername = true;
        }
    }

    try {
        $service = Service::findOrFail($serviceId);
        $service->domain = $domain;
      	$service->username = $username;
        $service->save();
    } catch (Throwable $e) {
        return ['abortcmd' => true];
    }

    return ['domain' => $domain, 'username' => $username];
});

function genRandomStr($length = 8) {
    $chars = 'abcdefghijklmnopqrstuvwxyz';
    $charsLength = strlen($characters);
    $str = '';
    for ($i = 0; $i < $length; $i++) {
        $str .= $chars[rand(0, $charsLength - 1)];
    }
    return $str;
}

 

Link to comment
Share on other sites

11 hours ago, leemahoney3 said:
    $uniqueDomain = false;
    while (!$uniqueDomain) {
        $domain = genRandomStr($domainLength) . $extension;
        $check = Service::where('domain', $domain)->whereIn('domainstatus', ['Active', 'Suspended'])->count();
        if ($check === 0) {
            $uniqueDomain = true;
        }
    }
  
    $uniqueUsername = false;
    while (!$uniqueUsername) {
        $username = genRandomStr($usernameLength);
        $check = Service::where('username', $username)->whereIn('domainstatus', ['Active', 'Suspended'])->count();
        if ($check === 0) {
            $uniqueUsername = true;
        }
    }

 

@leemahoney3

Now it works fine, thank you 🙂

A small question please, Does the mentioned code above generate a unique Domain and Username? which are not taken by any other customer?

Link to comment
Share on other sites

  • Solution
On 11/1/2023 at 9:30 AM, Craft said:

 

@leemahoney3

Now it works fine, thank you 🙂

A small question please, Does the mentioned code above generate a unique Domain and Username? which are not taken by any other customer?

Yes it checks against Active and Suspended services to ensure its unique 🙂 If it's not, it'll keep regenerating until it is. If you need more statuses, you can add them to the whereIn part of the query builder (e.g. to add Cancelled services also)

->whereIn('domainstatus', ['Active', 'Suspended', 'Cancelled'])

Better yet you could add these to the start of the hook for easier editing. I've also removed 

use Illuminate\Support\Str;

As it's no longer relevant.

<?php

use WHMCS\Service\Service;

add_hook('PreModuleCreate', 1, static function (array $params) {
    $extension = '.com';
    $domainLength = 15;
  	$usernameLength = 8;
  	$applicableStatuses = ['Active', 'Suspended', 'Cancelled'];

    $params = $params['params'];
    $serviceId = $params['serviceid'];
    $domain = $params['domain'];

    if (!empty($domain)) {
        return;
    }

    $uniqueDomain = false;
    while (!$uniqueDomain) {
        $domain = genRandomStr($domainLength) . $extension;
        $check = Service::where('domain', $domain)->whereIn('domainstatus', $applicableStatuses)->count();
        if ($check === 0) {
            $uniqueDomain = true;
        }
    }
  
    $uniqueUsername = false;
    while (!$uniqueUsername) {
        $username = genRandomStr($usernameLength);
        $check = Service::where('username', $username)->whereIn('domainstatus', $applicableStatuses)->count();
        if ($check === 0) {
            $uniqueUsername = true;
        }
    }

    try {
        $service = Service::findOrFail($serviceId);
        $service->domain = $domain;
      	$service->username = $username;
        $service->save();
    } catch (Throwable $e) {
        return ['abortcmd' => true];
    }

    return ['domain' => $domain, 'username' => $username];
});

function genRandomStr($length = 8) {
    $chars = 'abcdefghijklmnopqrstuvwxyz';
    $charsLength = strlen($chars);
    $str = '';
    for ($i = 0; $i < $length; $i++) {
        $str .= $chars[rand(0, $charsLength - 1)];
    }
    return $str;
}

 

Edited by leemahoney3
Link to comment
Share on other sites

  • 10 months later...

I want the subdomain to be domain.com and to be created randomly and not to enter the subdomain

Thank you for helping me

 

Because I did this for the subdomain, and of course I checked the mandatory domain, it gave this error in the log section

Module Create Failed - Service ID: 263 - Error: No user name supplied: "username" is a required argument. - User ID: 1

 

@leemahoney3

Edited by sadeght
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated