Intercept and modify data with hook

kdub

Active Member
Apr 19, 2019
29
8
3
California
cPanel Access Level
Root Administrator
cPanel provides action hooks to trigger functions for cPanel events. As far as I understand, the data the system passes to the hook is essentially read-only as the hook has no way to modify values and return them to the system.

I'm looking for filter hook functionality akin to wordpress filter hooks that will allow me to intercept and modify data as it is processed—for example to automatically append a string to a domain name when creating a zone record, before the zone is actually created.

I suppose the alternative is to use a post action hook to go back and manually update the record through the api. But this is essentially twice the work. It would be so much easier to just hand a modified value back and let cpanel complete the task it was already doing.

Any suggestions for accomplishing something like this are much appreciated.
 

kdub

Active Member
Apr 19, 2019
29
8
3
California
cPanel Access Level
Root Administrator
I guess I answered my own question. I followed the guide Tutorial - Create a Standardized Hook - Developer Documentation - cPanel Documentation to write a hook as a perl module to test. Turns out you can easily modify the existing key values or add values for unset keys that are supported by the cpanel script.
Code:
# $data is a referenced hash so accessing them is fairly straight forward
# for example the key 'domain' inside the nested hash 'args' inside $data
$data->{'args'}->{'domain'} = 'domain.tld';
Do scripts provide the same capabilities or is it only modules?

For some reason what's not working is the alternate_logfile so I can output logs to a file other than the default error_log. The below code should work but info is still logged only to error_log. The file exists. I don't understand.
Code:
my $logger = Cpanel::Logger->new( { 'alternate_logfile' => '/usr/local/cpanel/logs/myhook_log' } );
 
  • Like
Reactions: cPanelLauren

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,260
463
Hello @kdub,

Our Integration Developers should be able to better answer your question about this topic. Can you send this question via email to [email protected] and let us know the outcome once you receive an answer?

Thank you.