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.

Authentification on WHM with PHP

Discussion in 'cPanel Developers' started by skkeeper, Feb 5, 2011.

  1. skkeeper

    skkeeper Registered

    Joined:
    Feb 5, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    I'm trying to create a simple script to authentificate users on WHM with the Remote Access Key. I've tried to setup the headers since I don't really want to use curl for that.

    This is what I got so far, but it just shows me the login page for some reason.

    PHP:
    <?php
    $hash 
    "24159f07cb9a7fcb5ce78e1999e1b9e4
    e98d0f34799e06c04856815503ba48b1
    9ef14d56aca4007b426e68de676a23dc
    0d4232f779ecc6d9d98615fb44a5ca54
    <- missing part ->
    1f8140eacc2887ebf99490d995bba280
    a85b1b3ce8f1de6f79efdd1a9622f77
    f50c7f9ea4e5928f3c86bfad6fc07de0
    adbe2fd8664f68cf9c4051f69129e586
    ca7dd6b7488431f9d08d389266260766
    9a61eda588133f77c48873c14d5a6264"
    ;        # Set up the Hash
    $hash str_replace(array("\r""\n"),"",$hash);

    $user "johndoe";
    $servername '192.168.1.1'# Fake Ip btw :D

    header("Authorization: WHM $user:$hash\r\n");
    header("Location: https://$servername:2087/");

    ?>
    Any help would be apreciated :)
     
  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 skkeeper,

    You might want to take a look at our PHP XML-API client class. It can take care of the authentication for you. You can configure it to use fopen instead of cURL if you like.

    Here's the code within xmlapi.php that we use for fopen:
    PHP:
    // set the hash method
        
    public function set_hash$hash ) {
            
    $this->auth_type 'hash';
            
    $this->auth preg_replace("/(\n|\r|\s)/"''$hash);
        }

    // extract the hash for use in WHM
    //  this code block is within the generic xmlapi_query function that all
    //  function calls use
            
    if ( $this->auth_type == 'hash' ) {
                
    $authstr 'Authorization: WHM ' $this->user ':' $this->auth "\r\n";
            } elseif (
    $this->auth_type == 'pass' ) {
                
    $authstr 'Authorization: Basic ' base64_encode($this->user .':'$this->auth) . "\r\n";
            } else {
                throw new 
    Exception('invalid auth_type set');
            }

    // The send and receive method for fopen
    //  used internally by xmlapi_query if $this->http_client is set to 'fopen'
        
    private function fopen_query$url$postdata$authstr ) {
            if ( !(
    ini_get('allow_url_fopen') ) ) {
                throw new 
    Exception('fopen_query called on system without allow_url_fopen enabled in php.ini');
            }

            
    $opts = array(
                
    'http' => array(
                    
    'allow_self_signed' => true,
                    
    'method' => 'POST',
                    
    'header' => $authstr .
                        
    "Content-Type: application/x-www-form-urlencoded\r\n" .
                        
    "Content-Length: " strlen($postdata) . "\r\n" .
                        
    "\r\n" $postdata
            
    )
            );
            
    $context stream_context_create($opts);
            return 
    file_get_contents($urlfalse$context);
        }
    If you use this PHP XML-API class you might make the following script
    PHP:
    <?php
    include("xmlapi.php");

    $ip "10.0.0.1";
    $root_hash "24159f07cb9a7fcb5ce78e1999e1b9e4 
    e98d0f34799e06c04856815503ba48b1 
    9ef14d56aca4007b426e68de676a23dc 
    0d4232f779ecc6d9d98615fb44a5ca54 
    <- missing part -> 
    1f8140eacc2887ebf99490d995bba280 
    a85b1b3ce8f1de6f79efdd1a9622f77 
    f50c7f9ea4e5928f3c86bfad6fc07de0 
    adbe2fd8664f68cf9c4051f69129e586 
    ca7dd6b7488431f9d08d389266260766 
    9a61eda588133f77c48873c14d5a6264"
    ;;

    $xmlapi = new xmlapi($ip);
    $xmlapi->set_http_client('fopen');
    $xmlapi->set_hash($root_hash);

    // for debugging
    //$xmlapi->set_debug(1);

    // simple call to the XML-API funciton "version" which
    //  returns the version info of you cPanel/WHM server
    $xmlapi->version();
    Best Regards,
    -DavidN
     
    #2 cPanelDavidN, Feb 7, 2011
    Last edited: Feb 8, 2011
  3. mayaboys

    mayaboys Member

    Joined:
    Jan 22, 2009
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Hello,

    Why not just use SOAP for the identification and use and very simple API-XML?

    Good day
     
  4. jason_85

    jason_85 Member

    Joined:
    Feb 8, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    I'm new to all of this, but this works for me (I use the XML API):
    PHP:
    $url url('http://' $host':' $port .'/xml-api/cpanel', array('query' => $query));

    $headers['Authorization'] = 'Basic ' base64_encode($user ':' $pass);

    $response drupal_http_request($url$headers);
    I'm using the Drupal abstraction layer but the $headers['Authorization'] part is standard php. $user and $pass are strings, no hash.
     
    #4 jason_85, Feb 10, 2011
    Last edited: Feb 10, 2011
Loading...

Share This Page