One of the most flexible ways of working within cPanel is by creating CGI applications.  Of course, this isn’t the most efficient method, however sometimes efficiency isn’t your best bet when you’re in a rush.  You can use a standard CGI script within cPanel, you are limited by our APIs, such as what perl version to use.

First thing to note, is that these CGI scripts  have to be placed in /usr/local/cpanel/base/3rdparty/ with normal CGI permissions (have to be executable by the user, cannot be globally writable, etc).  Once placed here it can be accessed via $IP:2083/3rdparty/scriptnamed.cgi.  If you place them anywhere else within the cPanel document root, they will be offered up for download rather than executable.

When these CGI scripts are executed, they are executed as the user, so limited permissions apply.

What if you want to actually access cPanel’s APIs from within this CGI script?  What do you do?  There are a few different ways of handling this.  First you can use the JSON API if you just want to handle the interaction via javascript (you’re already authenticated, why not?).  The other option is using the Cpanel::XML module to make XML-API calls without the HTTP interface at all.  With the introduction of XML API’s Fast Mode we included a function called cpanel_fast_exec.  This is essentially a perl interface into API1 and API2 that can be accessed outside of cPanel.  The data passed into this is the same as the parameters used inside of fast mode.  For example, if you wanted to get a list of email accounts, you would call the following:
my $xml = Cpanel::XML::cpanel_exec_fast(
        {
                'cpanel_xmlapi_module' => 'Email',
                'cpanel_xmlapi_func' => 'listpopswithdisk',
        }
);

$xml = XMLin($xml);

Now $xml is a hash reference containing all the information returned by the call.

To download the example of this in use, click here.