Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

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.

Custom UAPI Module - cPanel Variables

Discussion in 'cPanel Developers' started by maxcell, Jun 26, 2017.

Tags:
  1. maxcell

    maxcell Member

    Joined:
    Aug 18, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    51
    I'm working on a custom UAPI module that will be called from WHM API 1. So far I've been able to perform basic functions and return the information I need. However I'm stuck trying to figure out how to properly retrieve the home direction of the current user.

    I found that I could access the CPDATA variables like:
    Code:
    my $user = $Cpanel::CPDATA{'USER'};
    However, that only provides variables from the /var/cpanel/users/user file which does not include the home dir.

    I need to be able to access the home direction of the current user and I'm having trouble finding the proper way to do that.

    I'm calling my custom UAPI function with:

    Code:
    example.com:2087/json-api/cpanel?cpanel_jsonapi_user=myuser&cpanel_jsonapi_module=WPAPI&cpanel_jsonapi_func=myfunction&cpanel_jsonapi_apiversion=3
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,658
    Likes Received:
    1,425
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
  3. maxcell

    maxcell Member

    Joined:
    Aug 18, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    51
    Michael,
    Thanks for the info.. I tried those functions but I kept getting errors.. I must not be using them properly.

    I'm getting this error: Can't call method "uapi" on an undefined value at /usr/local/cpanel/Cpanel/API/WPAPI.pm line 45.\n"

    I have included the LiveAPI
    Code:
    use Cpanel::LiveAPI          ();
    so in my code:
    Code:
    sub function {
     
        # $Cpanel::CPDATA pulls data from /var/cpanel/users/user file
        my $user = $Cpanel::CPDATA{'USER'};
    
        my $cpliveapi = Cpanel::LiveAPI->new(); # Connect to cPanel - only do this once.
        # Get domain user data.
        my $get_userdata = $cpliveapi->uapi(
            'DomainInfo', 'domains_data',
            {
                'format'    => 'hash',
            }
        );
    ....
    
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,658
    Likes Received:
    1,425
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

    The LiveAPI System provides environments for applications in the cPanel interface. Is your custom application running within cPanel? If not, try testing this UAPI function via the command line first to verify it works. EX:

    Code:
    uapi --user=username DomainInfo domains_data format=hash
    Thank you.
     
  5. maxcell

    maxcell Member

    Joined:
    Aug 18, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    51
    Running that command in your example above in the command prompt returns the correct info..

    I am writing a UAPI custom module that will be accessed via the WHM API 1..

    So I execute the functions of this module by calling the API from:
    myserver.com:2087/json-api/cpanel?cpanel_jsonapi_user=myuser&cpanel_jsonapi_module=WPAPI&cpanel_jsonapi_func=myfunction&cpanel_jsonapi_apiversion=3

    Maybe I'm not using it like it was intended?

    What I'm trying to accomplish is to have our billing system perform various custom tasks.. So that's why I'm using the WHM API 1 to call this custom uapi module. Since the billing system has the cPanel API token for the root user, I want to use it to be able to perform things like install and manage different web applications for sites.

    Is there a better way to go about accomplishing this?

    Thanks!
     
  6. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,658
    Likes Received:
    1,425
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    You can find the working example on the following document:

    Use WHM API to Call cPanel API and UAPI - Software Development Kit - cPanel Documentation

    Based on the example in the above document, the URL in your example should be:

    Code:
    https://$host.tld:2087/$cpsess1234567/json-api/cpanel?api.version=1&cpanel_jsonapi_user=$username&cpanel_jsonapi_module=DomainInfo&cpanel_jsonapi_func=domains_data&cpanel_jsonapi_apiversion=3&format=hash
    Upon testing, this URL works well on a test system. Could you verify if the above example works on your server?

    Thank you.
     
  7. maxcell

    maxcell Member

    Joined:
    Aug 18, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    51
    I wasn't having trouble with the API call, that is working just fine and returning data.

    What I'm having trouble is getting the home dir info using
    Code:
    sub function {
     
        # $Cpanel::CPDATA pulls data from /var/cpanel/users/user file
        my $user = $Cpanel::CPDATA{'USER'};
    
        my $cpliveapi = Cpanel::LiveAPI->new(); # Connect to cPanel - only do this once.
        # Get domain user data.
        my $get_userdata = $cpliveapi->uapi(
            'DomainInfo', 'domains_data',
            {
                'format'    => 'hash',
            }
        );
    ....
    I'm getting this error: Can't call method "uapi" on an undefined value at /usr/local/cpanel/Cpanel/API/WPAPI.pm line 45.\n"

    Thanks for your input and assistance!
     
  8. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,658
    Likes Received:
    1,425
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Could you provide some more information about how you are setting up the LiveAPI script? For instance, where do you call it from? Is it uploaded to a website, or as part of a plugin you open in the cPanel interface?

    Thank you.
     
  9. maxcell

    maxcell Member

    Joined:
    Aug 18, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    51
    Here's a simple example:

    UAPI module MyExample.pm (/usr/local/cpanel/Cpanel/API/MyExample.pm)

    Code:
    package Cpanel::API::MyExample;
    
    use strict;
    
    our $VERSION = '1.0';
    
    # Cpanel Dependencies
    use Cpanel                   ();
    use Cpanel::API              ();
    use Cpanel::Locale           ();
    use Cpanel::Logger           ();
    use Cpanel::LiveAPI          ();
    
    # Globals
    my $logger;
    my $locale;
     
    # Functions go here.
    sub getUserInfo {
        # $Cpanel::CPDATA pulls data from /var/cpanel/users/user file
        my $user = $Cpanel::CPDATA{'USER'};
    
        my ( $args, $result ) = @_;
      
        # Build the results.
        my $success = "true";
    
        if ($success) {
            $result->data("$user");
            return 1;
        }
        else {
            return 0;
        }
    }
    
    
    1;
    
    I call it with WPAPI (using the API token in the headers):
    https://myserver.com:2087/json-api/cpanel?cpanel_jsonapi_user=myuser&cpanel_jsonapi_module=MyExample&cpanel_jsonapi_func=getUserInfo&cpanel_jsonapi_apiversion=3

    This works.. I get this response:
    Code:
    {
       "module":"MyExample",
       "result":{
          "metadata":{
    
          },
          "status":1,
          "errors":null,
          "data":"myuser",
          "messages":null
       },
       "func":"getUserInfo",
       "apiversion":3
    }
    I need to be able to find user's Home Directory. When I add the LiveAPI code mentioned earlier in this thread, that's when I get the error: Can't call method "uapi" on an undefined value at /usr/local/cpanel/Cpanel/API/WPAPI.pm line 45.\n"

    Am I not able to use that uapi function that way? If so, how could I get additional user information like their home directory?

    Thanks!
     
  10. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,658
    Likes Received:
    1,425
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
  11. maxcell

    maxcell Member

    Joined:
    Aug 18, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    51
    cPanelMichael,
    Thanks for you suggestions!

    Are there any other ways to be able to write custom functions for the WHM API?

    I was hoping to use WHM/cPanel's existing API and add some custom functionality but it looks like I'm going to need to create my own custom API separate from cPanel/WHM.
     
  12. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,658
    Likes Received:
    1,425
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Could you provide a specific example of what you'd like to do?

    Thanks.
     
  13. maxcell

    maxcell Member

    Joined:
    Aug 18, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    51
    I decided to change my approach a little bit. Instead I'm going to create a cPanel php live plugin and I'll use single signon to automatically login and take the user to my cPanel plugin logged in as the user.

    I created an API2 module that my cPanel plugin calls. But I'm getting some permissions denied errors when I have it download some files to /var/mysoftware

    What would be the best way to allow my cPanel plugin to download some files to /var/mysoftware (which will be used by other users using this plugin). I have it check if the software hasn't been loaded or if it is outdated, then it downloads to that directory.
     
  14. maxcell

    maxcell Member

    Joined:
    Aug 18, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    51
    Just a little update, I get the permission denied errors if I try download with a shell command using system.. When using LWP::Simple, it returns 200 status but doesn't actually save the file to /var/mysoftware and no errors in the error log.

    I'm trying to figure out how to write to a /var or /opt/ directory using a UAPI module?
     
  15. maxcell

    maxcell Member

    Joined:
    Aug 18, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    51
    It may be helpful if I give you an example scenario with details of what I'm trying to accomplish.

    In this example, we'll have a cPanel plugin that will give the user some options for installing Wordpress. So when the user pull up my plugin they will be given a few options on some plugins and themes that will be auto-installed for them with their Wordpress installation. Once they select the desired options, the plugin will then install and customize the installation.

    So I have my cPanel plugin, Installer, which can be accessed through:
    https://my-server.com:2083/cpsessXXXXXX/frontend/paper_lantern/installer/index.live.php

    I created a custom UAPI module in: /usr/local/cpanel/Cpanel/API/WPAPI.pm
    which I'm accessing from installer/index.live.php using:

    Code:
    $function_result = $cpanel->uapi(
        'WPAPI', 'install',
        array(
            'user'     => $user,
            'domain'     => $domain,
            'homedir'     => $homedir,
            'documentroot'     => $documentroot,
             )
    );
    So the issue is that when /usr/local/cpanel/Cpanel/API/WPAPI.pm attempts to download something to /var/mysoftware it doesn't seem to have privileges? How do I accomplish that?

    Thanks!
     
    #15 maxcell, Jul 3, 2017
    Last edited by a moderator: Jul 4, 2017
  16. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,658
    Likes Received:
    1,425
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

    Do you notice any debugging data in the /home/$user/.cpanel/LiveAPI.log.rand when encountering the permissions error?

    Thank you.
     
  17. maxcell

    maxcell Member

    Joined:
    Aug 18, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    51
    No, the only files in that directory are:

    Code:
    ./   caches/      datastore/           live-engine-connector-6G3GSJ2ISf.sock=  live-engine-connector-Ohm6XdvRqO.sock=  live-engine-connector-ypeNFypAbt.sock=  nvdata.cache
    ../  contactinfo  email_accounts.json  live-engine-connector-mfeHRjXM7h.sock=  live-engine-connector-_pdrnrFVCW.sock=  nvdata/
     
  18. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,658
    Likes Received:
    1,425
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    I recommend sending an email to integration@cpanel.net so that one of our Integration Developers can take a closer look at the issue you are facing and determine if the action you are attempting to perform in your code is supported.

    Thank you.
     
Loading...

Share This Page