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.

Redirect to WHM using a php script

Discussion in 'cPanel Developers' started by vimalkumar_k, Jan 10, 2013.

  1. vimalkumar_k

    vimalkumar_k Member

    Joined:
    Feb 24, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    1
    Hi,

    I am trying to write a php script which when executed from the browser, will redirect and login to the WHM and am looking forward to some help regarding the same.

    I have read through a similar post (old, though).. http://forums.cpanel.net/f42/help-whm-login-script-php-161349.html

    My scenario:
    Trying using curl to execute json-api, like applist works:

    PHP:
            $api "applist";
            
    $query "https://$ip:2087/json-api/$api";

            
    $curl curl_init();
            
    curl_setopt($curlCURLOPT_SSL_VERIFYHOST,0);
            
    curl_setopt($curlCURLOPT_SSL_VERIFYPEER,0);
            
    curl_setopt($curlCURLOPT_RETURNTRANSFER,1);
            
    $header[0] = "Authorization: WHM $whmusername:" preg_replace("'(\r|\n)'","",$remotekey); // remotekey stored in db as TEXT
            
    curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
            
    curl_setopt($curlCURLOPT_URL$query);
            
    $result curl_exec($curl);
            
    curl_close($curl);
            if (
    $result == false) {
                return 
    FALSE// error
            
    }
            
            return 
    json_decode($resulttrue);
    But trying to get the same idea executed via php header() is failing for me:

    PHP:
                $srv_info = array(
                    
    "ip" => $row->s_ip,
                    
    "username" => $row->user_name,
                    
    "remotekey" => $row->remote_key
                
    );
                
                
    $ip $srv_info["ip"];
                
    $whmusername $srv_info["username"];
                
    $remotekey $srv_info["remotekey"];
                
                
    header("Authorization: WHM $whmusername:" preg_replace("'(\r|\n)'","",$remotekey));
                
    header("Location: http://" $ip ":2086/");

                
    // Also tried:

                // header("Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'","",$remotekey) . "\r\n");
                // header("Location: https://" . $ip . ":2087/");
    Using the header() stuff, even json calls fail, say if I change to:

    PHP:
                header("Authorization: WHM $whmusername:" preg_replace("'(\r|\n)'","",$remotekey));
                
    header("Location: http://" $ip ":2086/json-api/applist");
    .. and when I load this php script, it redirects to ../applist with the following error:

    Code:
    {"cpanelresult":{"apiversion":"2","error":"Access denied","data":{"reason":"Access denied","result":"0"},"type":"text"}}
    I am sure the remotekey is corret, as curl api calls (first code block) function fine for me.

    I played around with the code from http://forums.cpanel.net/f42/help-whm-login-script-php-161349.html but without success. I always end up with the WHM login screen, or its login prompt.

    What to do? Help..!! :confused:
     
    #1 vimalkumar_k, Jan 10, 2013
    Last edited: Jan 10, 2013
  2. vimalkumar_k

    vimalkumar_k Member

    Joined:
    Feb 24, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    1
    Digging further, this PHP implementation of LogMeIn works fine and seems to serve my purpose: https://gist.github.com/4440574

    But I would prefer authenticating using remote key only.. Is there any way of tweaking the logmein to post remotekey to <whm>/login/ url.. and then generate url with session id and redirect page, as done in logmein?
     
  3. 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
    Sorry, but no. Authentication done with a remote accesshash key will not provide the server or client mechanics necessary for an interactive session, such as those required for a browser session. Remote accesshashes for programmatic API request actions.

    Regards,
    -DavidN
     
  4. vimalkumar_k

    vimalkumar_k Member

    Joined:
    Feb 24, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    1
    David,

    Thank you for your clarification. Much appreciated.
     
Loading...

Share This Page