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.

API not working after cpanel upgrade:11.16.0-RELEASE

Discussion in 'General Discussion' started by shaily903, Feb 15, 2008.

  1. shaily903

    shaily903 Registered

    Joined:
    Feb 4, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    hi

    We have the below code working before cpanel upgrade. This code use to fetch us the data through cpanel. Please let us know the changes which needs to be made on our side to fix this code as per new cpanel.

    <?php
    class HTTP
    {
    function HTTP($host, $username, $password, $port = 2082, $ssl = '', $theme = 'sg')
    {
    $this->ssl = $ssl ? 'ssl://' : '';
    $this->username = $username;
    $this->password = $password;
    $this->theme = $theme;
    $this->auth = base64_encode($username . ':' . $password);
    $this->port = $port;
    $this->host = $host;
    $this->path = '/frontend/' . $theme . '/';
    }

    function getData($url, $data = '')
    {
    $url = $this->path . $url;
    if(is_array($data))
    {
    $url = $url . '?';
    foreach($data as $key=>$value)
    {
    $url .= urlencode($key) . '=' . urlencode($value) . '&';
    }
    $url = substr($url, 0, -1);
    }
    $response = '';
    $fp = fsockopen($this->ssl . $this->host, $this->port);
    if(!$fp)
    {
    return false;
    }
    $out = 'GET ' . $url . ' HTTP/1.0' . "\r\n";
    $out .= 'Authorization: Basic ' . $this->auth . "\r\n";
    $out .= 'Connection: Close' . "\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp))
    {
    $response .= @fgets($fp);
    }
    fclose($fp);
    return $response;
    }
    }

    /**
    * Functions to manipulate cPanel
    */
    class cPanel
    {
    /**
    * Creates an object to manipulate cPanel
    * @param string $host cPanel host without leading http://
    * @param string $username cPanel username
    * @param string $password cPanel password
    * @param int $port cPanel port, default to 2082. Change to 2083 if using SSL
    * @param bool $ssl False for http (default), true for SSL (requires OpenSSL)
    * @param string $theme cPanel theme, (forward compatibility- 'sg' theme currently required)
    * @return cPanel
    */
    function cPanel($host, $username, $password, $port = 2082, $ssl = false, $theme = 'sg')
    {
    $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
    }

    /**
    * Change cPanel's password
    *
    * Returns true on success or false on failure.
    * The cPanel object is no longer usable after changing the password.
    * @param string $password new password
    * @return bool
    */
    function setPassword($password)
    {
    $data['oldpass'] = $this->HTTP->password;
    $data['newpass'] = $password;
    $response = $this->HTTP->getData('passwd/changepass.html', $data);
    if(strpos($response, 'has been') && !strpos($response, 'could not'))
    {
    return true;
    }
    return false;
    }

    /**
    * Retrieve contact email address.
    *
    * Returns the contact email address listed in cPanel.
    * @return string
    */
    function getContactEmail()
    {
    $email = array();
    preg_match('/email" value="(.*)"/', $this->HTTP->getData('contact/index.html'), $email);
    return $email[1];
    }

    /**
    * Modify contact email address
    *
    * Returns true on success or false on failure.
    * @param string new contact email address
    * @return string
    */
    function setContactEmail($email)
    {
    $data['email'] = $email;
    $response = $this->HTTP->getData('contact/saveemail.html', $data);
    if(strpos($response, 'has been'))
    {
    return true;
    }
    return false;
    }

    /**
    * List all domains in the cPanel account
    *
    * Returns a numerically-indexed array on success or false on failure.
    * @return array
    */
    function listDomains()
    {
    $domainList = array();
    preg_match_all('/<option value="([^"]*)/', $this->HTTP->getData('mail/addpop2.html'), $domainList);
    if(count($domainList[1]) > 0)
    {
    return $domainList[1];
    }
    return false;
    }

    /**
    * List all POP3 email accounts
    *
    * Returns a numerically-indexed array on success or false on failure.
    * @return array
    */
    function listMailAccounts()
    {
    $accountList = array();
    preg_match_all('/\?acct=([^"]*)/', $this->HTTP->getData('mail/pops.html'), $accountList);
    if(count($accountList[1]) > 0)
    {
    return $accountList[1];
    }
    return false;
    }

    /**
     
  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
    Looks like you're doing a lot of things without using APIs. You may wish to switch over to using APIs so you wont need to worry much about cPanel upgrades breaking your scripts in the future.

    Relevant API function calls you may want to use instead:

    - Change Password: XML API passwd - http://www.cpanel.net/plugins/xmlapi/passwd.html
    - Get Contact Email: XML API listaccts - http://www.cpanel.net/plugins/xmlapi/listaccts.html
    - Set Contact Email: API2 CustInfo::savecontactinfo (1)
    - List all domains for an account: You could use the XML API listaccts function to get the primary domain, then use API2 Park::listaddondomains (2) for addon domains and for parked domains use Park::listparkeddomains http://www.cpanel.net/plugins/api2/Cpanel::Park::listparkeddomains.pod.html
    - List all POP3 accounts: API2 Email::listpopswithdisk - http://www.cpanel.net/plugins/api2/Cpanel::Email::listpopswithdisk.pod.html

    Note that API2 functions can be called via the XML API. More documentation at: http://www.cPanel.net/plugins/devel

    (1) = Not yet documented. Look at /usr/local/cpanel/base/frontend/x3/contact/savemailinclude.html to see how it is implemented in the X3 theme.

    (2) = Not yet documented. Look at /usr/local/cpanel/base/frontend/x3/addon/index.html to see how it is implemented in the X3 theme.

    Of course, you could always simply look at the contents of the appropriate files in /usr/local/cpanel/base/frontend/x3 to see how things are handled by the X3 theme. However as X3 itself is merely a graphical interface to the APIs, you may wish to interact directly with the APIs rather than using the X3 as an interface to the APIs.
     
  3. apple99

    apple99 Registered

    Joined:
    Apr 1, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    I have exactly the same problem

    How did you get on with this issue ?

    We have to force upgrade from theme x to x3 since x didn't allow us to do all the the things we wanted to do with mapping domains.

    Like you, most of our code, which is basically identical to yours has broke.

    And as usual we are on tight deadlines to finish off the current project.

    To be blunt, the documentation for PHP use for X3 seems to be a bit hard to locate. We don't really want to use XML. We need a library of simple PHP calls which mainly perform the following functions which fit into our existing framework.

    Find out what email addresses exist on the server. ***
    Find our what domains exist on the server.
    Add a domain
    Add an email address
    Delete a domain
    Delete an email address.

    Hope you can provide me with some feedback. I would appreciate this very much.

    Ed
    Ed Hollingshead
    sales@mediatradingservices.co.uk
     
  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
    If you do not desire to use XML, then there are no APIs available for you to use to achieve the functionality you desire outside of cPanel (or a shell environment) itself.

    There are plenty of PHP code samples on how to use the XML-API around the forum, I recommend performing a forum search.
     
Loading...

Share This Page