Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubashar Ahmad committed Jan 13, 2023
1 parent dbca95a commit 35fff90
Showing 1 changed file with 69 additions and 16 deletions.
85 changes: 69 additions & 16 deletions src/Cpanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function createSubdomain($subdomain, $username = '', $subdomain_dir = '',
$result = $this->api2_query($username, 'SubDomain', 'addsubdomain', array(
'domain' => $subdomain,
'rootdomain' => $domain,
'dir' => '/public_html/' . $subdomain_dir,
'dir' => $subdomain_dir,
'disallowdot' => 1
)
);
Expand Down Expand Up @@ -238,10 +238,12 @@ public function createdb(string $db_name)
return array('reason' => $msg, 'result' => 0);
}

$name_length = 54 - strlen($this->username);
$name_length = 63;
if (!$this->hasUsernamePrefixed($db_name)) {
$name_length = $name_length - (strlen($this->username) + 1);
}

$db_name = str_replace($this->username . '_', '', $this->slug($db_name, '_'));
$database_name = $this->username . "_" . $db_name;
$database_name = $this->fixName($db_name);

if (strlen($db_name) > $name_length || strlen($db_name) < 4) {
return array('reason' => 'Database name should be greater than 4 and less than ' . $name_length . ' characters.', 'result' => 0);
Expand All @@ -252,6 +254,20 @@ public function createdb(string $db_name)
return $this->returnResult($result);
}

public function deletedb($db_name)
{
if (!isset($db_name) || empty($db_name)) {
$msg = "database name is required.";
return array('reason' => $msg, 'result' => 0);
}

$db_name = $this->fixName($db_name);

$result = $this->api2_query($this->username, "MysqlFE", "deletedb", array('db' => $db_name));

return $this->returnResult($result);
}

/**
* @param string $db_user
* @return array|mixed
Expand All @@ -264,7 +280,7 @@ public function checkdbuser(string $db_user)
return array('reason' => $msg, 'result' => 0);
}

$dbuser = $this->username . '_' . ($db_user ? str_replace($this->username . '_', '', $db_user) : "myadmin");
$dbuser = $this->fixName(($db_user ?: 'myadmin'));

$user = $this->api2_query($this->username, "MysqlFE", "dbuserexists", array('dbuser' => $dbuser));

Expand All @@ -289,12 +305,15 @@ public function createdbuser($db_user, $db_pass)
return array('reason' => 'Please sent database username and password.', 'result' => '0');
}

$user_length = 16 - strlen($this->username);
$db_user = str_replace($this->username . '_', '', $this->slug($db_user, '_'));
$dbuser = $this->username . "_" . $db_user;
$name_length = 32;
if (!$this->hasUsernamePrefixed($db_user)) {
$name_length = $name_length - (strlen($this->username) + 1);
}

$dbuser = $this->fixName($db_user);

if (strlen($db_user) > $user_length || strlen($db_user) < 4) {
return array('reason' => 'Database username should be greater than 4 and less than ' . $user_length . ' characters.', 'result' => 0);
if (strlen($db_user) > $name_length || strlen($db_user) < 4) {
return array('reason' => 'Database username should be greater than 4 and less than ' . $name_length . ' characters.', 'result' => 0);
}

$validate = $this->checkPassword($db_pass);
Expand Down Expand Up @@ -325,7 +344,7 @@ public function createdbuser($db_user, $db_pass)
* @param string $privileges
* @return array|mixed
*/
protected function setdbuser(string $db_name, string $db_user, string $privileges = '')
public function setdbuser(string $db_name, string $db_user, string $privileges = '')
{

if (!isset($db_name) || !isset($db_user)) {
Expand All @@ -334,8 +353,8 @@ protected function setdbuser(string $db_name, string $db_user, string $privilege
return array('reason' => $msg, 'result' => 0);
}

$dbname = $this->username . "_" . str_replace($this->username . '_', '', $db_name);
$dbuser = $this->username . '_' . ($db_user ? str_replace($this->username . '_', '', $db_user) : "myadmin"); //be careful this can only have a maximum of 7 characters
$dbname = $this->fixName($db_name);
$dbuser = $this->fixName($db_user);

if (is_array($privileges)) {
$privileges = implode(',', $privileges);
Expand Down Expand Up @@ -369,6 +388,19 @@ public function accountDetials(string $username = '')
return $this->accountsummary($username);
}

public function createEmailAccount($email, $password, $quota = 500, $main_domain = '')
{
$result = $this->api2_query($this->username, 'Email', 'addpop', [
'domain' => $main_domain,
'email' => $email,
'password' => $password,
'quota' => $quota
]
);

return $this->returnResult($result);
}

/**
* @param $result
* @return array|mixed
Expand All @@ -393,8 +425,8 @@ protected function returnResult($result)
if (isset($result['data'])) {
$data = $result['data'];
if (is_array($data)) {
$reason = (string)$data['reason'];
$status = (string)$data['result'];
$reason = (string)(is_array($data['reason']) ? implode(', ', $data['reason']) : $data['reason']);
$result = (string)(is_array($data['result']) ? array_shift($data['result']) : $data['result']);

if (mb_strpos($reason, ')') !== false) {
$reason = ltrim(strstr($reason, ')'), ') ');
Expand All @@ -404,7 +436,7 @@ protected function returnResult($result)
$reason = trim(strstr($reason, ' at ', true));
}

return array('reason' => $reason, 'result' => (int)$status);
return array('reason' => $reason, 'result' => (int)$result);
} else {
if (isset($result['func'])) {
$function = $result['func'];
Expand Down Expand Up @@ -482,4 +514,25 @@ protected function checkPassword($pwd)

return '';
}


/**
* @param $name
* @return bool
*/
protected function hasUsernamePrefixed($name): bool
{
if (substr(strtolower($name), 0, strlen($this->username)) == strtolower($this->username)) {
return true;
}
return false;
}

protected function fixName($name)
{
if (!$this->hasUsernamePrefixed($name)) {
$name = $this->username . "_" . $name;
}
return $name;
}
}

0 comments on commit 35fff90

Please sign in to comment.