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.

postkillacct script

Discussion in 'cPanel Developers' started by lakridserne, May 23, 2011.

  1. lakridserne

    lakridserne Member

    Joined:
    Dec 3, 2009
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Hi,

    How can I use the variables cPanel pass to /scripts/postkillacct in PHP? I've made my /scripts/postkillacct written in PHP by doing

    #!/usr/bin/php

    on the top of the file.

    Also, how can I pass things in the same way as cPanel does it manually on the command line (for troubleshooting purposes I would like to know this)?

    Thanks a lot in advance.
     
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi lakridserne,

    In your script you will want to parse $argv (php manual reference). You can find an example of how to parse $argv into an associative array in the cPanel & WHM Script Hooks#PHP documentation.

    To emulate the call that cPanel performs you would do something like this on the commandline
    Code:
    /scripts/postkillacct user dave killdns 1 
    In the example above the cPanel account "dave" was terminated and DNS records were removed in the process

    The postkillacct only sends two key/value pairs: 'user' and 'killdns'. If you need other information related to the account, you will need to utilized the prekillacct hook instead. I seem to recall there being a thread somewhere concerning prekillacct that gathers info and then postkillacct to trigger the extra functionality.

    Best Regards,
    -DavidN
     
  3. lakridserne

    lakridserne Member

    Joined:
    Dec 3, 2009
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Thanks a lot David. This response was very helpful. Part of it I did figure out, but the code to emulate a user being deleted was especially very helpful. In fact I only need the username, so I can delete a line from a database. Thanks for it.
     
  4. lakridserne

    lakridserne Member

    Joined:
    Dec 3, 2009
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Can you please help me debug this code?

    PHP:
    #!/usr/bin/php
    <?php
      $user 
    $argv[2];

      require(
    '/home/backup/db.inc.php');
      
    $db = new DB;
      
    $conn $db->connect();

      
    // Put together the query and execute it
      
    $query "DELETE FROM access WHERE cpuser='$user'";
      
    $db->query($query,$conn);
    ?>
     
  5. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    If I make certain assumptions about db.inc.php and the database schema, I would have to say the script is fine. But honestly, I haven't a clue what they do (or more matter of fact, what they might be doing wrong) or how your script should treat them (semantically).

    What kind of error do you get when you test run the script? (ie "/scripts/postkillacct user dave" )

    Just a quick pointer or two: Since this script will be executed as a system call by the cPanel process, you need to make sure that the script is executable (ie 0700 or something executable but secure). And also, your shebang line is set for "/usr/bin/php" which is usually fine, but on cPanel systems, that particular PHP binary is actually compiled for cgi NOT cli. For casual use, it probably doesn't make too much difference, but I'd strongly consider switching it to "/usr/local/bin/php".

    -DavidN
     
  6. lakridserne

    lakridserne Member

    Joined:
    Dec 3, 2009
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Thanks for the prompt response.

    I tried changing the path to php and the permissions on the file, but it still doesn't work. It works when I run the script directly from command line, but not when I just terminate the account via cPanel.
     
  7. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    My guess is that $argv[2] is not the variable that you need. Let me explain:

    What the binary is really doing is something similar to the following pseudo code:
    Code:
    %DATA = ( 'user'=>'dave', 'killdns'=>1 );
    system( '/scripts/postkillacct', %DATA );
    
    The hash, %DATA, is being passed as an argument to the Perl function system(). system() unwinds the hash and places the key/value pairs as arguments to the first system() argument, '/scripts/postkillacct'. Thus you get something like:
    Code:
    /scripts/postkillacct user dave killdns 1
    However, unlike some other languages (like PHP**), Perl doesn't kept track of the ordering of the key/value pairs...so that example above could just as likely produce:
    Code:
    /scripts/postkillacct killdns 1 user dave
    So, I would recommend that you parse the entirety of $argv into an associate array, like the example in our documentation, an use the 'user' key. Doing so would ensure that your script is passing the correct value to the query statement.

    Regards,
    -DavidN

    **Technically speaking, I seem to recall that PHP doesn't guarantee that associative array key/value pair will be in the FIFO ordering, but in practice it usually is.
     
  8. lakridserne

    lakridserne Member

    Joined:
    Dec 3, 2009
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Wow - with this my script now works like a charm. Thanks a lot for your kind help. My script now looks like the following (so others can use it if they find it... :)):

    PHP:
    #!/usr/local/bin/php
    <?php
      
    function argv2array ($argv) {
        
    $opts = array();
        
    $argv0 array_shift($argv);

        while(
    count($argv)) {
          
    $key array_shift($argv);
          
    $value array_shift($argv);
          
    $opts[$key] = $value;
        }
        return 
    $opts;
      }

      
    $opts argv2array($argv);

      
    $user $opts['user'];

      require(
    '/home/backup/db.inc.php');
      
    $db = new DB;
      
    $conn $db->connect();

      
    // Put together the query and execute it
      
    $query "DELETE FROM access WHERE cpuser='$user'";
      
    $db->query($query,$conn);
    ?>
     
Loading...

Share This Page