Augusto Will

Active Member
Sep 9, 2011
43
6
58
cPanel Access Level
Root Administrator
I need to run a php script when user creates an addon domain in cpanel.
If I use Api2::AddonDomain::addaddondomain, the script is called when a user go to cpanel and creates a new addon domain? Or only if the addon is created via API?
My manage_hooks list show this:

Cpanel:
Api2::AddonDomain::addaddondomain:
stage: post
escalateprivs: 0
weight: 200
id: DVq_gNCtDh8hdYV6PLtoaYjl
exectype: script
hook: /var/cpanel/ax/ohs.php

But if i go to cpanel and add a new add-on, the script is not called. Why?
How is the correct way to do that?

Thank you.
 

sparek-3

Well-Known Member
Aug 10, 2002
2,042
230
368
cPanel Access Level
Root Administrator
I'm actually interested in this too. Haven't been able to get it to work, but I'm really new to scripting hooks, so I blew it off as not knowing enough.

It doesn't appear that anything is passed to the script in $argv when the script is run. At least that's what I found.

I added the code:

PHP:
ob_start();
print_r($argv);
$message = ob_get_contents();
ob_end_clean();
mail("[email protected]","Addon Hook", $message);
at the top of the hook script, and I don't get the expected results.
 

Augusto Will

Active Member
Sep 9, 2011
43
6
58
cPanel Access Level
Root Administrator
yeah, i put an file_put_contents('/hookcont.txt', $contents); in the script, no results. :)


sparek-3, try this:


Code:
function get_passed_data() {
    $raw_data;
    $stdin_fh = fopen('php://stdin', 'r');
    if ( is_resource($stdin_fh) ) {
        stream_set_blocking($stdin_fh, 0);
        while ( ($line = fgets( $stdin_fh, 1024 )) !== false ) {
            $raw_data .= trim($line);
        }
        fclose($stdin_fh);
    }
    if ($raw_data) {
        $input_data = json_decode($raw_data, true);
    } else {
        $input_data = array('context'=>array(),'data'=>array(), 'hook'=>array());
    }


    return $input_data;
	
}


$input = get_passed_data();
$contents = var_export($input, true);
file_put_contents('/hookcont.txt',  $contents);
mail("[email protected]","Addon Hook",  $contents);
 
Last edited:

sparek-3

Well-Known Member
Aug 10, 2002
2,042
230
368
cPanel Access Level
Root Administrator
Wow! Thanks! That was immensely helpful. Actually solved my problem. Never occurred to me that the script was being treated as a pipe. I guess that's in the documentation some where. Thanks for giving me the heads up on that. My use was for a pre hook instead of a post hook.

I don't guess that really helps you though.

Have you tried setting the debughooks variable in /var/cpanel/cpanel.config and tailing the /usr/local/cpanel/logs/error_log while creating an addon domain?

There is information on this at:

https://documentation.cpanel.net/display/SDK/Guide+to+Standardized+Hooks+-+Debug+Mode

Not sure if it will really help you, but that's all I've got.

Thanks again for your help in solving my problem.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,904
2,218
463
I need some help with API2 here.... The API2 only execute the hook if the event is triggered by scripts using the API or even a user using cPanel trigger the API2 events in the same way? Thank you.
Hello :)

Yes, the hookable events should apply to actions performed in the cPanel/WHM UI as well as the command line. Feel free to open a support ticket if this is not functioning as expected so we can take a closer look. You can post the ticket number here so we can update this thread with the outcome.

Thank you.