如何使用单点登录将用户登录到cpanel?

时间:2017-03-26 12:57:11

标签: php cpanel whm cpanel-xmlapi

我正在尝试在我的网络应用上实现这个确切的功能,正如whm所使用的那样。 Please check this image

我为单点登录api阅读了documentation,并提出了以下代码:

<?php

// This can also be the reseller who owns the cPanel user.
$whmusername = "resellerusername";
$whmpassword = "abctesting";


// The user on whose behalf the API call runs.
$cpanel_user = "normaluser"; //under reseller

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

$curl = curl_init();                                     // Create Curl Object.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);       // Allow self-signed certificates...
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);       // and certificates that don't match the hostname.
curl_setopt($curl, CURLOPT_HEADER, false);               // Do not include header in output
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);        // Return contents of transfer on curl_exec.
$header[0] = "Authorization: Basic " . base64_encode($whmusername.":".$whmpassword) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);         // Set the username and password.
curl_setopt($curl, CURLOPT_URL, $query);                 // Execute the query.
$result = curl_exec($curl);
if ($result == false) {
    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
                                                    // log error if curl exec fails
}


$decoded_response = json_decode( $result, true );
print_r($decoded_response);

$session_url = $decoded_response['data']['url'];
$cookie_jar = 'cookie.txt';

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.
}

$session_url = preg_replace( '{/login(?:/)??.*}', '', $session_url );  // make $session_url = https://10.0.0.1/$session_key

$query = "$session_url/execute/Ftp/list_ftp";

curl_setopt($curl, CURLOPT_URL, $query);  // Change the query url to use the UAPI call.
$result = curl_exec($curl);               // Execute the UAPI call.
if ($result == false) {
    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
                                                    // log error if curl exec fails
}

curl_close($curl);

print $result;

?>

我得到这个作为输出:

Array
(
    [data] => Array
        (
            [url] => https://domainname.com:2083/cpsess5326278746/login/?session=normaluser%3azRtR0RzLZ5owYZin%3acreate_user_session%2c4597fa33ff7ce68f3fdab84d9f3a51a1
            [session] => normaluser:zRtR0RzLZ5owYZin:create_user_session,4597fa33ff7ce68f3fdab84d9f3a51a1
            [expires] => 1490532538
            [cp_security_token] => /cpsess5326278746
            [service] => cpaneld
        )

    [metadata] => Array
        (
            [result] => 1
            [command] => create_user_session
            [version] => 1
            [reason] => Created session
        )

)
<br />
<b>Warning</b>:  curl_setopt(): You must pass either an object or an array with the CURLOPT_HTTPHEADER argument in <b>C:\xampp\htdocs\cpanel-api\open-cpanel.php</b> on line <b>34</b><br />
{"messages":null,"data":[{"homedir":"/home/normaluser","type":"main","user":"normaluser"},{"type":"logaccess","homedir":"/usr/local/apache/domlogs/normaluser","user":"normaluser_logs"}],"errors":null,"status":1,"metadata":{"transformed":1}}

但是当我在浏览器https://domainname.com:2083/cpsess5326278746/login/?session=normaluser%3azRtR0RzLZ5owYZin%3acreate_user_session%2c4597fa33ff7ce68f3fdab84d9f3a51a1中使用此网址时,

它不会将我登录到cPanel。

我注意到whm重定向页面有以下用于登录用户的html代码:

<html slick-uniqueid="3"><head><meta http-equiv="refresh" content="2;URL=https://domainname.com:2083/cpsess7055670446/login/?session=normaluser:7PMD2WWAjnQc_cDL,e691a31623f55cf37ee32a63a390fb08"></head><body>
</body></html>

那么如何生成像whm生成的url并将用户登录到cPanel(即:打开带有normaluser登录的cpanel)?

1 个答案:

答案 0 :(得分:0)

您可以尝试一下。对我有用。

arr_a = np.random.randint(low = 0,high = 10,size = 10)
arr_b = np.random.randint(low = 0,high = 10,size = 11)

out:
arr_a : [4 8 6 3 3 7 8 6 0 2]
arr_b : [8 8 9 6 1 6 1 5 8 3 1]

list_c = []
for i,j in zip(range(len(arr_a)),range(len(arr_b))):
    list_c.append((arr_a[i]-arr_b[i],arr_a[i]-arr_b[i+1]))

out:
list_c : [(-4, -4), (0, -1), (-3, 0), (-3, 2), (2, -3), (1, 6), (7, 3), (1, -2), (-8, -3), (-1, 1)]