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.

Adding custom dns-entries on sub-domain creation

Discussion in 'cPanel Developers' started by mhofer, May 19, 2013.

  1. mhofer

    mhofer Member

    Joined:
    May 10, 2013
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hi

    Because we were not satisfied with the default proxy-subdomain creation of cpanel/whm, we try to create our own.
    For domains / parked domains / addon-domains this was no problem by editing the Zone-Template files.

    Unfortunately I could not find something similar for subdomains... that's why i'm trying to create something manually.

    I tried it with the CustomEventHandler.pm as follows:
    Code:
    package Cpanel::CustomEventHandler;
    
    # cpanel12 - CustomEventHandler.pm                      Copyright(c) 2012 cPanel, Inc.
    #                                                           All rights Reserved.
    # copyright@cpanel.net                                         http://cpanel.net
    # This code is subject to the cPanel license. Unauthorized copying is prohibited
    
    use strict;
    use Cpanel::Logger ();
    
    # api_version = api_version
    # type = pre,post
    # module = Cpanel::<modname>
    # event = the event ie addpop
    # cfg ref is a hash of the conf variables passed to the event. If its a legacy event, the hash keys are numbered, newer events have names.
    # dataref = the data returned from the event (post events only)
    sub event {
        my ( $api_version, $type, $module, $func, $cfgref, $dataref ) = @_;
        ## UAPI note: $api_version will be '3', and the method signature becomes the similar:
        ##   my ( $api_version, $type, $module, $func, $args, $result ) = @_;
    
        if ($module eq 'subdomain') {
            if ($func eq 'addsubdomain') {
                if ($type eq 'post') {
                    my $hdata = $cfgref->[0];
                    my $sub = $hdata->{'domain'};
                    my $domain = $hdata->{'rootdomain'};
                    #require Cpanel::API
                    Cpanel::API::execute( 'ZoneEdit', 'add_zone_record', { domain => $domain, name => 'autoconfig.' .  $sub, type => 'CNAME', cname => $domain . '.' } );
                }
            }
        }
    
        return 1;
    }
    
    1;
    
    when i create a subdomain in cpanel, it gets created normally and this script has no effect.

    Can someone help me figure out why? Is there an easier solution to accomplish this?

    Following should be done:
    - user creates subdomain
    - system creates several custom dns-entries based on this subdomain
    ยจ
    Greetings
    Marcel
     
  2. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    Marcel,
    Don't use CustomEventHandler.pm, it's an old system and there can only be one CustomEventHandler.pm so you run the risk of it getting overwritten by some 3rd party plugin that hasn't moved past that hook yet.

    Instead, use cPanel's Standardized Hooks (Introduction to Standardized Hooks). Here's an example of adding a hook for Subdomain::addsubdomain():


    Hook code:

    Code:
    #!/usr/bin/perl
    
    package ScriptHook;
    
    sub describe {
        my $hooks = {
            'hook'      => 'ScriptHook::changedns',
            'function'  => 'Api2::Subdomain::addsubdomain',
            'exectype'  => 'module',
            'namespace' => 'Cpanel',
            'stage'     => 'post'
        };
        return $hooks;
    }
    
    sub changedns {
        #require Cpanel::API
        Cpanel::API::execute( 'ZoneEdit', 'add_zone_record', { domain => $domain, name => 'autoconfig.' .  $sub, type => 'CNAME', cname => $domain . '.' } );
    }
    
    1;
    

    Here's how to add the module hook:

    Code:
    $mkdir /var/cpanel/perl5
    $mkdir /var/cpanel/perl5/lib
    $cp ScriptHook.pm /var/cpanel/perl5/lib/
    $/usr/local/cpanel/bin/manage_hooks add module ScriptHook
    
     
Loading...

Share This Page