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.

cPanel hook functions

Discussion in 'cPanel Developers' started by NixTree, May 16, 2012.

  1. NixTree

    NixTree Well-Known Member

    Joined:
    Aug 19, 2010
    Messages:
    386
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Gods Own Country
    cPanel Access Level:
    Root Administrator
    Hello,

    I am trying to write a cPanel hook function to collect the information of addon domains. I believe I have to put the code in /usr/local/cpanel/hooks/addondomain/addaddondomain and I was able to do some basic XML-API calls ( like applist, listaccts etc ) with my code. Now I would like to know, when a user try to add an addon domain "newaddon.com", how the domain name is passed to the hook function, so that it can collect the information necessary regarding the new addon domain and write it to a file? I write the code in python!

    Any help will be highly appreciated!

    Thank you,
    Nibin.
     
  2. NixTree

    NixTree Well-Known Member

    Joined:
    Aug 19, 2010
    Messages:
    386
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Gods Own Country
    cPanel Access Level:
    Root Administrator
    Hello,

    Anybody?? I would like to know what arguements can be passed to the hook functions and how; if possible give some examples!

    Thank you,
    Nibin.
     
  3. 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
    I wouldn't use /usr/local/cpanel/hooks if I were you. I'd use the new Standardized Hooks system as it will be maintained and updated the most:

    Introduction to Standardized Hooks

    To use the same hook, you create an install file like so:

    Code:
    #!/usr/bin/perl
    
    use strict;
    
    print << "EOM";
    	[
    		{
    			"namespace": "Cpanel",
    			"function": "Api2::AddonDomain::addaddondomain",
    			"stage": "post",
    			"hook": "/location/of/your/hook/code"
    		}
    	]
    EOM
    
    Then run:

    Code:
    /usr/local/cpanel/bin/manage_hooks add script /path/to/install/file
    
    To see what data the hook outputs, make the hook code output the data:
    Code:
    #!/usr/bin/perl 
    use IO::Select;
    use JSON::Syck;
    use Data::Dumper;
    
    get_passed_data();
    
    sub get_passed_data {
        my $raw_data   = '';
        my $input_data = {};
        my $selects    = IO::Select->new();
        $selects->add( \*STDIN );
        if ( $selects->can_read(.1) ) {
            while (<STDIN>) {
                $raw_data .= $_;
            }
            $input_data = JSON::Syck::Load($raw_data);
        }
        print STDERR "HOOK DATA: " . Data::Dumper::Dumper($input_data);
        return $input_data;
    } 
    

    The above code will print the hook data as a JSON object dump to /usr/local/cpanel/logs/error_log when the hook is run.
     
  4. NixTree

    NixTree Well-Known Member

    Joined:
    Aug 19, 2010
    Messages:
    386
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Gods Own Country
    cPanel Access Level:
    Root Administrator
    Hello Dave,

    That was very helpful. Even though I found how to get my tasks completed with the hook functions, it seems the method you mentioned is the best with 11.32+.

    One final question; where I can find the possible values of the following.

    "namespace": "Cpanel",
    "function": "Api2::AddonDomain::addaddondomain",

    Thanks again :)

    Thank you,
    Nibin.
     
    #4 NixTree, May 18, 2012
    Last edited: May 18, 2012
  5. NixTree

    NixTree Well-Known Member

    Joined:
    Aug 19, 2010
    Messages:
    386
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Gods Own Country
    cPanel Access Level:
    Root Administrator
    Hello,

    Ok, found the necessary docs. When I tried this to call AddonDomain::deladdondomain , it didn't work the dump the following error in the cPanel error log.

    ======
    Cpanel::Api2::Exec::api2_exec('AddonDomain', 'deladdondomain', HASH(0x367fc20), HASH(0x1af5310)) called at cpanel line 631
    main::real_cpexectag('<?cp AddonDomain::deladdondomain(% %,result,reason) domain=$RAW_FORM{\'domain\'},subdomain=$RAW_FORM{\'subdomain\'} ?>') called at cpanel line 3937
    main::dotag_finished_headers(1) called at cpanel line 3768
    main::cpanel_parseblock('<cpanel setvar="headerimg=../images/topsubdomains.gif">\x0A<cpanel setvar="dprefix=../">\x0A<cpanel Branding="include(stdheader.html)">\x0A\x0A<style type="text/css">\x0A<?cp Branding::spritelist(.spriteicon_img_mini {float:left;margin-right:5px;background: url\\{leftparenthesis}"%"\\{rightparenthesis};} %,@spriteurl,images::#icon-${img}_mini {background-position\\{colon}0 -${cssposition}px;width\\{colon}${width}px;height\\{colon}${height}px; }:) imgtype=icon,subtype=img,method=scale_60percent,format=png,img=addondomain ?>\x0A</style>\x0A\x0A<div class="body-content">\x0A\x0A<div class="h1Title"><div class="spriteicon_img_mini" id="icon-addondomain_mini"></div><cpanel langprint="INDXAddonDomains"></div>\x0A\x0A<h2><cpanel langprint="AODRemoval"></h2>\x0A\x0A<!--\x0A<?cp AddonDomain::deladdondomain(% %,result,reason) domain=$RAW_FORM{\'domain\'},subdomain=$RAW_FORM{\'subdomain\'} ?>\x0A-->\x0A<cpanelif !$CPERROR{\'addondomain\'}>\x0A <div class="success_details">\x0A\x09 <cpanel Locale="maketext(\'The addon domain \xE2\x80\x9C[_1]\xE2\x80\x9D has been removed.\', $FORM{\'domain\'})">\x0A </div>\x0A <cpanel Ftp="delftp($FORM{\'domain\'})">\x0A <cpanelif !$CPERROR{\'subdomain\'}>\x0A <div class="success_details">\x0A <cpanel Locale="maketext(\'The linked subdomain "\')"> <cpanel sprint="$FORM{\'subdomain\'},_,0"> <cpanel Locale="maketext(\'" has been removed.\')">\x0A </div>\x0A </cpanelif>\x0A\x0A <cpanelif $CPERROR{\'subdomain\'}>\x0A <div class="error_details">\x0A <cpanel print="$CPERROR{\'subdomain\'}">\x0A </div>\x0A </cpanelif>\x0A</cpanelif>\x0A\x0A<cpanelif $CPERROR{\'addondomain\'}>\x0A <div class="error_details">\x0A\x09 <cpanel print="$CPERROR{\'addondomain\'}">\x0A </div>\x0A</cpanelif>\x0A\x0A<div class="return-link"><a href="index.html">&larr; <cptext "Go Back"></a></div>\x0A\x0A</div><!-- end body-content -->\x0A\x0A<cpanel Branding="include(stdfooter.html)">\x0A') called at cpanel line 3683
    main::cpanel_parse(GLOB(0x3232b20)) called at cpanel line 5096
    main::run_standard_mode() called at cpanel line 426
    ========

    But the same code worked, when I used it as a cPanel hook function! Any difference when we write these two scripts!

    Thank you,
    Nibin.
     
Loading...

Share This Page