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.

Finding an account's primary domain.

Discussion in 'cPanel Developers' started by That1Swede, Mar 11, 2010.

  1. That1Swede

    That1Swede Member

    Joined:
    Jul 23, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Houston, Texas
    Is there a reliable way to retrieve an account's primary domain name via the XML API and only the cPanel account's login details? I've looked the API1/2 modules over and nothing stands out as a winner.
     
  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 That1Swede,

    You should be able to retrieve the primary domain by using 'listaccts'

    Code:
    $searchtype = 'user';
    $searchforme = 'dave'; //my username is 'dave' and my primary domain is dave.test
    print $xmlapi->listaccts($searchtype, $searchforme);
    
    will produce something like:
    Code:
    <listaccts>
      <acct>
        <disklimit>unlimited</disklimit>
        <diskused>0M</diskused>
        <domain>dave.test</domain>
        <email>dave@cpanel.test</email>
        <ip>192.168.1.1</ip>
        <maxaddons>*unknown*</maxaddons>
        <maxftp>unlimited</maxftp>
        <maxlst>unlimited</maxlst>
        <maxparked>*unknown*</maxparked>
        <maxpop>unlimited</maxpop>
        <maxsql>unlimited</maxsql>
        <maxsub>unlimited</maxsub>
        <owner>masterowner</owner>
        <partition>home</partition>
        <plan>default</plan>
        <shell>/bin/bash</shell>
        <startdate>10 Mar 12 07:08</startdate>
        <suspended>0</suspended>
        <suspendreason>not suspended</suspendreason>
        <suspendtime></suspendtime>
        <theme>x3</theme>
        <unix_startdate>1268399296</unix_startdate>
        <user>dave</user>
      </acct>
      <status>1</status>
      <statusmsg>Ok</statusmsg>
    </listaccts>
    
    from there you can grep the domain node.

    If you mean by "primary" the "owner's" domain, then there's no direct function for that. You'll need to create you own iterative code block to check the 'owner' tag to see if it matches 'root' or the username. Either case, you will have queried and received the authoritative user (and their domain info!).

    Hope that helps,
    -Dave
     
  3. That1Swede

    That1Swede Member

    Joined:
    Jul 23, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Houston, Texas
    I considered listaccts however with only cPanel access to the account (and not WHM access) it's not an option in this case. I was hoping for an API1/2 call of sorts.

    So, with only the login details for a cPanel account is there a way to reliable fetch the account's main domain (ie. the domain name the cPanel account was created with)?
     
  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
    Finding account's primary domain with cPanel credentials

    That1Swede,

    I'm sorry, I misunderstood that cPanel credentials were the limiting factor.

    To my knowledge there's no direct way to do what you want. However, there are a few indirect ways to obtain that information.

    1) write your own cpanel module that answers api calls:
    WritingCpanelModules < DeveloperResources/ApiBasics < TWiki

    2) create a cPanel branding page (or modify an existing one) that contains/returns an ExpVar
    Code:
    <cpanel print=$CPDATA{'DOMAIN'} >
    This first way is much better but would require some knowledge of Perl. The second way is kind of a shift in methodology. That's to say, if you do create a page with this tag, you'd have to retrieve that page directly.
    Additionally, $CPDATA{'DOMAIN'} only contains the immediate user's domain, and there's no knowledge of a parent or owner account (I was unclear if that matters).
    Lastly, you'd need root to implement either #1 or #2

    3) along the lines of #2, you could just login and parse the index.html page. If you're using the x3 theme: use cURL to login and fetch index.html. Then create a DOM object of html and use XPath and search for a table with id="stats", grab the second cell of the first row
    Code:
    <table id="stats" cellpadding="0" cellspacing="0" width="100%">
    		<tbody><tr class="row-even">
    			<td class="stats_left">Main Domain</td>
    			[COLOR="DarkGreen"]<td class="stats_right"><b>therewillbecake.net</b></td>[/COLOR]
    		</tr>.....
    
    Hope this helps. If I can think of anything else, I'll be sure to post.

    -Dave
     
  5. That1Swede

    That1Swede Member

    Joined:
    Jul 23, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Houston, Texas
    Thanks for the suggestions, as I won't have root access to the servers that the script will be interfacing with I won't be able to go the module or branding route. I was hoping I wouldn't have to scrape HTML to get the information (as the account could be using any random theme and thus it'd be a bit of 'hit and miss').

    The 'listaddondomains' function in the 'AddonDomain' module can be used to grab the info provided that there is at least one addon domain set up under the account as it returns a 'rootdomain' entry for each addon domain that is set to the account's primary domain.

    Any chance of having the account's primary domain added to the 'StatsBar' module's 'stat' function in some way in the future, perhaps? ;P
     
  6. 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
    Finding account's primary domain with cPanel credentials

    That1Swede,

    My colleague, Matt, and I got solid answer for you!
    Code:
    http://yourserver:2082/xml-api/cpanel?cpanel_xmlapi_apiversion=1&cpanel_xmlapi_module=print&arg-0=DOMAIN
    
    result:
    Code:
    <?xml version="1.0" ?>
    <cpanelresult>
      <command>print</command>
      <type>internal</type>
      <source>internal</source>
      <apiversion>1</apiversion>
      <data>
        <result>dave.com</result>
      </data>
      <event>
        <result>1</result>
      </event>
    </cpanelresult>
    
    
    What we're doing is making a call to the <cpanel print="" > module. This hits the same code as in my #2 suggestion.

    Hope that works for you!

    One note, if you're using our XMLAPI.php class, the api1_query() explicitly requires a module and function be specified for creating the request URL. The example URL only as the module, cause this module only has a single funciton (internally). You will have to modify the php wrapper class for this usage case...that is, like I said, if you're using it. Otherwise, just structure your query like the example above within your own code:)

    Best Regards,
    -Dave

    PS. I agree about the StatsBar; had investigated that possibility previously too. I'll certainly make note to the powers that be;)
     
  7. That1Swede

    That1Swede Member

    Joined:
    Jul 23, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Houston, Texas
    Dave,

    Excellent. Much thanks for that. :)
     
Loading...

Share This Page