Skip to content

Update cpaneluapi.class.php for improving security #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions cpaneluapi.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class cpanelAPI
protected $scope; //String - Module we want to use
protected $api;
protected $auth;
protected $authType; // Authorization type 'Basic' to authenticate using account password or 'cpanel' to authenticate using cpanel api tokens to prevent leaking of account password
protected $pass;
protected $secret;
protected $type;
Expand All @@ -48,11 +49,12 @@ class cpanelAPI
protected $postData = '';
/**
* @param $user
* @param $pass
* @param $pass cpanel password or api token
* @param $server
* @param $secret
* @param $authType Basic/cpanel
*/
function __construct($user, $pass, $server, $secret = FALSE)
function __construct($user, $pass, $server, $secret = FALSE, $authType = 'Basic')
{
$this->user = $user;
$this->pass = $pass;
Expand All @@ -61,6 +63,7 @@ function __construct($user, $pass, $server, $secret = FALSE)
$this->secret = $secret;
$this->set2Fa();
}
$this->authType = $authType;
}

/**
Expand Down Expand Up @@ -151,6 +154,15 @@ public function __call($name, $arguments)
return json_decode($this->json);
}

/**
* @return string
**/

protected function getAuth()
{
return ($this->authType == 'cpanel' ? $this->user . ":" . $this->pass : base64_encode($this->user . ":" . $this->pass));
}

/**
* @param $name
* @param $arguments
Expand All @@ -159,7 +171,7 @@ public function __call($name, $arguments)
*/
protected function APIcall($name, $arguments)
{
$this->auth = base64_encode($this->user . ":" . $this->pass);
$this->auth = $this->getAuth();
$this->type = $this->ssl == 1 ? "https://" : "http://";
$this->requestUrl = $this->type . $this->server . ':' . $this->port . $this->method;
switch ($this->api) {
Expand Down Expand Up @@ -191,7 +203,7 @@ protected function APIcall($name, $arguments)
*/
protected function curl_request($url)
{
$httpHeaders = array("Authorization: Basic " . $this->auth);
$httpHeaders = array("Authorization: {$this->authType} " . $this->auth);
//If we have a token then send that with the request for 2FA.
if ($this->token) {
$httpHeaders[] = "X-CPANEL-OTP: " . $this->token;
Expand Down