Cpanel UAPI remote login stoped working

Jaro747

Member
Jan 23, 2018
15
1
3
Polska
cPanel Access Level
Root Administrator
Hello

I have simple plugin with autologin to remote server from server A to server B


I dont understand why this script stoped working . Any one can help me



PHP:
<?php
//echo "testing";

// Instantiate the CPANEL object.
require_once "/usr/local/cpanel/php/cpanel.php";
 
// Connect to cPanel - only do this once.
$cpanel = new CPANEL();
 
$user = $cpanel->uapi('Variables','get_user_information',array('name' => 'user',));
//print_r ($user); display table

$cpanel_user = $user["cpanelresult"]["result"]["data"]["user"];
// get cp username from table


?>

<?php
$whmusername = "whmusername";
# The contents of /root/.accesshash
$hash = "whmapikey";

$query = "https://hostname:2087/json-api/create_user_session?api.version=1&user=$cpanel_user&service=cpaneld";

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

$header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'","",$hash);
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
curl_setopt($curl, CURLOPT_URL, $query);

$result = curl_exec($curl);

if ($result == false) {
 
    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
}


$decoded_response = json_decode( $result, true );
$session_url = $decoded_response['data']['url'];
$cookie_jar = 'cookie.txt';
//print_r($session_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, null);             // Unset the authentication header.
curl_setopt($curl, CURLOPT_COOKIESESSION, true);          // Initiate a new cookie session.
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);       // Set the cookie jar.
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);      // Set the cookie file.
curl_setopt($curl, CURLOPT_URL, $session_url);            // Set the query url to the session login url.

$result = curl_exec($curl);                               // Execute the session login call.
if ($result == false) {
    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
                                                    // Log an error if curl_exec fails.
}


curl_close($curl);


echo "<script>";
echo "window.location.href= '$session_url';";
echo "</script>";
echo "<noscript>";
echo '<a href="'.$session_url.'">go to remote server</a>';
echo "</noscript>";



?>
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,296
1,252
313
Houston