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.

postwwwacct returns empty data?

Discussion in 'cPanel Developers' started by Phelms215, Nov 16, 2011.

  1. Phelms215

    Phelms215 Member

    Joined:
    Aug 27, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Hey,

    I have a script setup which is below it gets called when postwwwacct runs.. for some reason it is empty? am i missing something ?

    PHP:
            $i 0;
        
    $opts = array();
        
    $argv0 array_shift($argv);
        
            while(
    count($argv)) {
                
    $key array_shift($argv);
                
    $value array_shift($argv);
                
    $opts[$key] = $value;
                
    $i++;
            }
            
        
    mail('testemail''num'$i);
            
            
            
    $data json_encode($opts);
            
        
    mail('testemail''acct2'$data);
    ?>
    the 'num' email returns 0 so it never even hit the while and data returns [] as the array is empty..

    thanks in advance!!
     
  2. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    There's some interesting sample code for extracting command-line parameters into associative arrays and discussion of argc and argv not being superglobals on this page: PHP: Command line usage - Manual
     
  3. Phelms215

    Phelms215 Member

    Joined:
    Aug 27, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Thanks, I looked into that but have one question..what would I actually pass through to that parseArgs function? $argv or something else ? or nothing

    also I am not running this as normal

    #/usr/bin/php-cli -q
    <?PHP
    // code here..
    ?>

    I am actually doing php-cli -q </path/tofile.php> as the first option returns an error of an invalid path, could that be causing this somehow?
     
  4. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    I would assume that since $argv is the argument, you would pass whatever contains argv as the parameter.

    To clarify: is this script /scripts/postwwwacct or is this script being called by a script named postwwwacct in /scripts/
     
  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
    the '-q' flag only suppresses header output and would have nothing to do with arguments passed to the script. If you know you're using a CLI SAPI then you likely don't need the '-q' because headers are not send by default; only with the CGI SAPI.

    you're example script will work if it is the postwwwacct. As documented in Script Hooks, the postwwwacct script receives the new user data as arguments.

    so, you should be able to test your postwwwacct like
    Code:
    /usr/local/cpanel/scripts/postwwwacct user yourname domain mydomain.tld arraykey arrayvalue
    for instance, i placed your code in a test file named forum.php and made the following changes (shorter and no mail)
    PHP:
    #!/usr/bin/php-cli -q
    <?
    $i = 0; 
    $opts = array(); 
    $argv0 = array_shift($argv); 
         
    while(count($argv)) { 
        $opts[array_shift($argv)] = array_shift($argv); 
        $i++; 

    echo "num: $i";    
    var_dump($opts);   
    ?>
    now you can test on the command line
    Code:
    root@044 [~/junk]# chmod 755 forum.php 
    root@044 [~/junk]# ./forum.php 
    num: 0
    array(0) {
    }
    root@044 [~/junk]# ./forum.php one test two more
    num: 2array(2) {
      ["one"]=>
      string(4) "test"
      ["two"]=>
      string(4) "more"
    }
    root@044
    ------------------------
    now, if you're calling other scripts from postwwwacct, it depends on the language that postwwwacct is written in as to how you should prepare the execution arguments in there so that your PHP script (aka your snippet) gets the expected input.

    Likely whatever language postwwwwacct is written it has a system or exec or backticks. Those shell execution functions usually just like a command prompt, you simply would string your arguments behind the script name (just as if you were performing the manual test above).

    That said, there are several caveats that come with using shell execution functions. Please read their documentation and search around for best practices.


    Regards,
    -DavidN
     
  6. Phelms215

    Phelms215 Member

    Joined:
    Aug 27, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Hello,


    Thanks so much for your reply - sorry it took so long for me to respond.. I did what you said and still having a little trouble.

    /usr/local/cpanel/scripts/postwwwacct
    PHP:
    #!/usr/bin/php-cli  
    <? 
    $i = 0;  
    $opts = array();  
    $argv0 = array_shift($argv);  
          
    while(count($argv)) {  
        $opts[array_shift($argv)] = array_shift($argv);  
        $i++;  
    }  
    echo "num: $i";     
    var_dump($opts);    
    ?>
    Result from test
    Code:
      
    root@server1 [~]# /scripts/postwwwacct one two three four
    array(0) {
    }
    
    .. now it works when I put that script in a test.php file and outputs perfectly but from postwwwacct nothing shows up?
     
    #6 Phelms215, Nov 24, 2011
    Last edited: Nov 24, 2011
  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
    hmm, that's very peculiar. You should not only have the the array dump but also the echo statement in you output...

    couple of things to verify/consider:
    • ensure that postwwwacct is executable (likely already is, but just to make sure)
    • use long form PHP tags…namely '<?php' instead of '<?' (again, likely not the problem but worth doing)
    • verify that /usr/bin/php-cli exists; consider using /usr/bin/php instead since it's more common
    • add additional debug statements; piece you script together until you find the problem
    • verify $argv and $argc are available: "$ini_value = ini_get ('register_argc_argv');var_dump($ini_value);" php.net:regist_argc_argv
    • try running postwwwacct as a script to a PHP binary. This will cause the shebang to be ignored, but forces the script to be handled directly by the PHP binary without shell interpretation: "php -f /usr/local/cpanel/scripts/postwwwacct one two three four"

    Regards,
    -DavidN
     
Loading...

Share This Page