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.

Get WHM username API

Discussion in 'cPanel Developers' started by dabihsss, Mar 31, 2010.

  1. dabihsss

    dabihsss Registered

    Joined:
    Mar 30, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Hi,

    please guide me how to get the whm username ( logged in user) through API?

    Thank you
     
    #1 dabihsss, Mar 31, 2010
    Last edited: Mar 31, 2010
  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
    WHM username? Making an API call

    Hi dabihsss,

    1) WHM username: Do you mean Reseller? A cPanel account which has access to provision accounts and system resources via WHM? or are you looking for any cPanel account on the system?

    2) Are you looking to use API1/API2 or the XML-API? If your code will be on another machine, then you have to use the XML-API. If your code is on the same system, you can use cPanel tags which call API1/API2. You could also poll the loopback interface with the XML-API.

    3) Are you scripting in Perl, PHP, something else?

    I suggest you take a look at our developer's documentation if you haven't already:
    Developer Resources

    There are examples of how to make the various API queries. You'll find all the XML-API, API1, and API2 functions there; you can call them either in an HTTP request (via XML-API) or within files in certain cPanel directories (for native API1/API2 calls).

    I really recommend using XML-API because you can make queries for API1/API2 functions in addition to some functions only available via the XML-API.

    Best Regards,
    -Dave
     
  3. dabihsss

    dabihsss Registered

    Joined:
    Mar 30, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Hi Dave,

    Thanks for the repy.

    The reseller's user name that currently logged in , i got it from your developer pages ($user = $_ENV['REMOTE_USER'];).

    I am developing a WHM plugin in PHP.

    Thank you once again.
     
  4. 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
    WHM Plugin in PHP

    Have you taken a look at the example PHP WHM Plugin?
    http://sdk.cpanel.net/examples/php_whmaddon_outline.cgi_example
    --I've included the example below for easy reference

    Couple of things you'll need to be sure to do:

    1) naming - you must pick a name for your script that will be used by WHM. Say if desire your addon to be know as "My Super Addon" then you should probably name your file addon_mysuperaddon.cgi.

    2)First line of script should be
    Code:
    #!/usr/bin/php-cgi
    (obvious but I'm just being thorough ;) )

    3)Second line is parse by WHM so that you plugin is dynamically rendered in the WHM interface:
    Code:
    #WHMADDON:mysuperaddon:My Super Addon
    maps the human readable name to the filename

    4)Wrap your PHP code in normal PHP tags
    Code:
    <?php ..... ?>
    (I know, the example uses short tags...well, I didn't write the example, and not every example is perfect :p

    5)Make sure to set the proper permissions on your cgi (again, obvious, but sometimes we all benefit from a sanity check)


    So...to get back to your question about API calls with WHM Plugins. In PHP, you'd have to include all your own code(or our example xmlapi client class) for making the queries to the loopback; making native calls, like a .cpphp is not possible.
    If you were writing in Perl, you *might* have access to native API calls, but I'd have to write a sample test; the WHM Plugin documents don't explicitly state if they're available for inclusion.

    Hope this helps,
    -Dave

    reference example: WHM Plugin in PHP
    NOTE: probably shouldn't use PHP short tags
    Code:
    #!/usr/bin/php-cgi
    <?
    #WHMADDON:test:PHP Test App
    if (!checkacl("all")) {
    	print "Access Denied";
    } else {
    	print "you have access!";
    }
    
    function checkacl ($acl) {
    	$user = $_ENV['REMOTE_USER'];
    
    	if ($user == "root") {
    		return 1;
    	}
    
    	$reseller = file_get_contents("/var/cpanel/resellers");
    	foreach ( split( "\n", $reseller ) as $line ) {
    		if ( preg_match( "/^$user:/", $line) ) {
    			$line = preg_replace( "/^$user:/", "", $line);
    			foreach ( split(",", $line )  as $perm ) {
    				if ( $perm == "all" || $perm == $acl ) {
    					return 1;
    				}
    			}
    		}
    	}
    	return 0;
    }
    
    ?>
    
     
Loading...

Share This Page