The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Create a subdomain on account creation

Discussion in 'cPanel Developers' started by DanH42, Jul 15, 2014.

  1. DanH42

    DanH42 Active Member

    Joined:
    Sep 11, 2011
    Messages:
    35
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Bloomington, IL
    cPanel Access Level:
    Root Administrator
    I've like to be able to automatically create a subdomain of an account's primary domain when the account is created. The name of the subdomain will not change between accounts; it'll always be subdomain.<domain.tld>. I've found the documentation for both WHM hooks and the PHP API very confusing, so any pointers would be great.

    I'd prefer to use PHP to do all this, since I'm unfamiliar with Perl. That said, if it's incredibly simpler to do in Perl, I'd be open to that route as well.
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    675
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  3. DanH42

    DanH42 Active Member

    Joined:
    Sep 11, 2011
    Messages:
    35
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Bloomington, IL
    cPanel Access Level:
    Root Administrator
    The hook API seems straightforward enough; I think all I need to do is run "sudo /usr/local/cpanel/bin/manage_hooks add script /scripts/myscript --category Whostmgr --event Accounts::Create" to get my script called and passed details when an account is created.

    But dealing with the API has been another story. There's references to UAPI and LiveAPI and cpphp and phpcp and API 1 vs 2 requests, and it's around there I start getting lost. The examples in the CpanelInc/xmlapi-php repo don't do exactly what I need (not that I'd expect them to), and while I think SubDomain::addsubdomain is what I'm after, there's an API 1 and an API 2 version, and I'm unclear on which one I want, or exactly how to call it.

    I think I'm close, but I'm having trouble getting all the pieces put together.
     
  4. DanH42

    DanH42 Active Member

    Joined:
    Sep 11, 2011
    Messages:
    35
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Bloomington, IL
    cPanel Access Level:
    Root Administrator
    Update: after a fair amount of trial-and-error, I got a working solution.

    First, the command in the post above that I thought would add the proper hook gave me an error. I managed to get it to run without errors and print a status message, but my script still wasn't triggered on account creation. After looking at the logs generated while creating an account, I noticed cPanel always calls a script at /usr/local/cpanel/scripts/postwwwacct, but no file exists there. I symlinked my script to that location, and it got called properly after an account was created. There's also a corresponding "prewwwacct" script that's called just before creating the new account that some might find useful.

    The documentation wasn't specific, but I assumed account information would be passed in as a JSON or XML string on stdin. That was not the case. Instead, it looks like the information is passed via command-line arguments something like this:
    Code:
    # /usr/local/cpanel/scripts/postwwwacct locale en dkim 1 max_defer_fail_percentage unlimited (...)
    That's sort of strange, but I worked out the following PHP code to give me an associative array:
    PHP:
    $args = array();
    for(
    $i 1$i count($_SERVER['argv']); $i += 2){
        if(isset(
    $_SERVER['argv'][$i 1]))
            
    $args[$_SERVER['argv'][$i]] = $_SERVER['argv'][$i 1];
    }
    This gives you a variable called $args that lets you look up properties like username, domain, password, etc.

    What I needed to do was create a subdomain called "staging.<domain.tld>" with its content directory in ~user/staging_html for every new account. Ultimately, this was the code I worked out (using the xmlapi.php Michael linked to):

    PHP:
    #!/usr/local/cpanel/3rdparty/bin/php
    <?php
    include 'xmlapi.php';

    $args = array();
    for(
    $i 1$i count($_SERVER['argv']); $i += 2){
        if(isset(
    $_SERVER['argv'][$i 1]))
            
    $args[$_SERVER['argv'][$i]] = $_SERVER['argv'][$i 1];
    }

    if(isset(
    $args['user']) && isset($args['domain'])){
        
    $xmlapi = new xmlapi("127.0.0.1");
        
    $xmlapi->hash_auth("root""REMOTE ACCESS KEY FOR ROOT");

        
    $params = array("staging"$args['domain'], 00"staging_html");
        
    $xmlapi->api1_query($args['user'], "SubDomain""addsubdomain"$params);
    }
    ?>
     
  5. grayloon

    grayloon Well-Known Member

    Joined:
    Oct 31, 2007
    Messages:
    98
    Likes Received:
    2
    Trophy Points:
    8
    Location:
    Evansville, IN
    cPanel Access Level:
    Root Administrator
    Twitter:
    I want to do something similar. I need to add about 60 subdomains to a single account. I want to do this on-demand. Could someone guide me to the correct API to use for this?
     
  6. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    675
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Are the previous posts to this thread helpful?

    Thank you.
     
Loading...

Share This Page