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.

Hook for after domain/email creation/deletion

Discussion in 'cPanel Developers' started by MACscr, Oct 15, 2013.

  1. MACscr

    MACscr Well-Known Member

    Joined:
    Sep 30, 2003
    Messages:
    190
    Likes Received:
    1
    Trophy Points:
    18
    cPanel Access Level:
    Root Administrator
    I need to run a particular script any time an account is created or deleted, but this also needs to any domain setup for Domain Forwarding. I think pretty much Addon Domains and Parked Domains can be used for this.

    I am basically looking to use such a hook for syncing domains with my external spam filter.

    I do need to create an email filter every time an email account is created. I know there is an api for creating the filter, but I dont know if there are hooks that can be used to trigger it when an email account is created.
     
  2. MACscr

    MACscr Well-Known Member

    Joined:
    Sep 30, 2003
    Messages:
    190
    Likes Received:
    1
    Trophy Points:
    18
    cPanel Access Level:
    Root Administrator
    Sweet, just figured out part of it. I can place a script in /usr/local/cpanel/hooks/email/, such as addpop, then run:

    /usr/local/cpanel/bin/register_hooks

    And now whenever an email account is created, my script runs.

    Now to see if i can do something like this for addon domains, etc.
     
  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
    Check out the Standardized Hooks documentation: cPanel Functions

    You can use a hook on any API1/API2 call and other events. With standardized hooks, you can place your hook script anywhere so you're not limited to just one hook per event (there can only be one /usr/local/cpanel/hooks/email/addpop, etc).
     
  4. MACscr

    MACscr Well-Known Member

    Joined:
    Sep 30, 2003
    Messages:
    190
    Likes Received:
    1
    Trophy Points:
    18
    cPanel Access Level:
    Root Administrator
    hmm, i have no idea how those Standardized Hooks work. Seems to be limited info on it. Might be easier for me to grasp if i was using perl and knew it better, but I am using php.
     
  5. MACscr

    MACscr Well-Known Member

    Joined:
    Sep 30, 2003
    Messages:
    190
    Likes Received:
    1
    Trophy Points:
    18
    cPanel Access Level:
    Root Administrator
    Dave,

    I was able to get hooks to register using the method you described, but they dont appear to run. I have tried it with the full path or the just the file name (as it says to do), but I get no error in /usr/local/cpanel/logs/error_log and no indication that they have ran (i even tried to have them write to the temp folder to see if they were ran). Any suggestions?

    PHP:
    # /usr/local/cpanel/bin/manage_hooks list
    Cpanel:
        
    Email::addpop:
            
    stagepost
            escalateprivs
    0
            weight
    100
            id
    4CTB3SjGIqegeMUJrt51bWV2
            exectype
    script
            hook
    hooks.php
            
    --
            
    stagepost
            escalateprivs
    0
            weight
    200
            id
    xmJIw8rzXBWuZB8yPVRnjWBA
            exectype
    script
            hook
    : /usr/local/cpanel/base/frontend/x3/mymx/hooks.php
        Email
    ::addforward:
            
    stagepost
            escalateprivs
    0
            weight
    100
            id
    Snq3pzwzK6uuBYGvu4jvsciJ
            exectype
    script
            hook
    hooks.php
            
    --
            
    stagepost
            escalateprivs
    0
            weight
    200
            id
    FqYnm0fpM9B4k2yfub1kK1pM
            exectype
    script
            hook
    : /usr/local/cpanel/base/frontend/x3/mymx/hooks.php
     
  6. 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
    Two things to check. First, you'll need to make sure the PHP files are 0755 as they are being run from the command line via the hook code.

    Second, you can enable debugging by adding the following line to /var/cpanel/cpanel.config:

    Code:
    debughooks=2
    
    This will show you what's happening when the hook fires. You can also set debughooks=3 to see every hookable event in real time to make sure Email::addpop and Email::addforward are being run.


    Another note is about file location. /usr/local/cpanel/base/frontend/x3/mymx/hooks.php is inside /usr/local/cpanel/base/frontend and as such, the code will be able to be run if someone goes to https://hostname:2083/frontend/x3/mymx/hooks.php. If you don't want that to be possible, you'll need to move the hooks outside of the cPanel document root (/usr/local/cpanel/base/frontend).
     
  7. MACscr

    MACscr Well-Known Member

    Joined:
    Sep 30, 2003
    Messages:
    190
    Likes Received:
    1
    Trophy Points:
    18
    cPanel Access Level:
    Root Administrator
    Dave,

    Where is the proper place to store a cpanel plugin in general? /usr/local/cpanel/base/frontend/mypluginname?
     
  8. 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
    Unfortunately, there's no real best practices for this but here are different areas of the filesystem that are useful:

    /usr/local/cpanel/base/frontend - cPanel document root. NOTE: user will always be redirected to their theme document root. code here will not be accessible

    /usr/local/cpanel/base/frontend/$theme - theme document root. Code here runs under cPanel's webserver (cpsrvd) and has access to built-in cPanel variables. Code is only accessible for users of $theme

    /usr/local/cpanel/base/3rdparty - used to store 3rd party scripts. Code here executes without extra help from cpsrvd. Code is accessible by users of any theme

    This makes things a mess because if you want to take advantage of built-in API functionality, you have to place files in a specific theme (or all themes, hoping no one installs a new theme after your plugin is installed). If you want them accessible to all themes by placing them in /3rdparty, then you don't get privileges to execute API and such.
     
Loading...

Share This Page