mirror of
https://github.com/miguel456/php-http-virtualmin-api
synced 2023-07-28 01:52:31 +00:00
initial commit
This commit is contained in:
37
src/Managers/Account/AccountModifier.php
Normal file
37
src/Managers/Account/AccountModifier.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Account;
|
||||
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
|
||||
/*
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class AccountModifier {
|
||||
|
||||
public function deleteAccount(HttpClient $httpClient, string $domain, string $username) : bool {
|
||||
$httpClient->queryStringBuilder()->addParameter("program", "delete-user");
|
||||
$httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$httpClient->queryStringBuilder()->addParameter("user", $username);
|
||||
|
||||
return $httpClient->sendRequest();
|
||||
}
|
||||
|
||||
public function disableAccount(HttpClient $httpClient, string $domain, string $username, string $accountType) : bool {
|
||||
$httpClient->queryStringBuilder()->addParameter("program", "modify-user");
|
||||
$httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$httpClient->queryStringBuilder()->addParameter("user", $username);
|
||||
$httpClient->queryStringBuilder()->addParameter("disable-{$accountType}");
|
||||
|
||||
return $httpClient->sendRequest();
|
||||
}
|
||||
|
||||
public function enableAccount(HttpClient $httpClient, string $domain, string $username, string $accountType) : bool {
|
||||
$httpClient->queryStringBuilder()->addParameter("program", "modify-user");
|
||||
$httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$httpClient->queryStringBuilder()->addParameter("user", $username);
|
||||
$httpClient->queryStringBuilder()->addParameter("enable-{$accountType}");
|
||||
|
||||
return $httpClient->sendRequest();
|
||||
}
|
||||
|
||||
}
|
130
src/Managers/Account/Email/EmailManager.php
Normal file
130
src/Managers/Account/Email/EmailManager.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Account\Email;
|
||||
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
use Nilemin\Virtualmin\Entities\Account;
|
||||
use Nilemin\Virtualmin\Managers\Account\AccountModifier;
|
||||
use Nilemin\Virtualmin\Managers\BaseManager;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class EmailManager extends BaseManager implements EmailManagerInterface {
|
||||
|
||||
/**
|
||||
* EmailAccountManager constructor.
|
||||
*
|
||||
* @param HttpClient $httpClient
|
||||
*/
|
||||
public function __construct(HttpClient $httpClient) {
|
||||
parent::__construct($httpClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates email account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param string $realname
|
||||
* @param int|null $quota Disk quota for the email account in MB.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function createEmailAccount(string $domain, string $username, string $password, string $realname, int $quota = null) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "create-user");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("user", $username);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("pass", $password);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("real", $realname);
|
||||
if ($quota !== null) {
|
||||
$quota *= 1000;
|
||||
$this->httpClient->queryStringBuilder()->addParameter("quota", $quota);
|
||||
}
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes email account quota.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
* @param int $quota
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function changeEmailAccountQuota(string $domain, string $username, int $quota) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "modify-user");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("user", $username);
|
||||
$quota *= 1000;
|
||||
$this->httpClient->queryStringBuilder()->addParameter("quota", $quota);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes email account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function deleteEmailAccount(string $domain, string $username) : bool {
|
||||
$handler = new AccountModifier($this->httpClient,$domain, $username);
|
||||
return $handler->deleteAccount($domain, $username);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables email account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function disableEmailAccount(string $domain, string $username) : bool {
|
||||
$handler = new AccountModifier($this->httpClient, $domain, $username);
|
||||
return $handler->disableAccount($domain, $username, "email");
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables email account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function enableEmailAccount(string $domain, string $username) : bool {
|
||||
$handler = new AccountModifier($this->httpClient, $domain, $username);
|
||||
return $handler->enableAccount($domain, $username, "email");
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all email accounts of given domain.
|
||||
*
|
||||
* @param $domain
|
||||
* @return array Array containing \stdClass instances.
|
||||
*/
|
||||
public function fetchEmailAccounts($domain) {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "list-users");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
|
||||
return $this->filterEmailAccounts($this->httpClient->getResponseMessage()->data);
|
||||
}
|
||||
|
||||
private function filterEmailAccounts(array $accounts) {
|
||||
$emailAccounts = [];
|
||||
foreach ($accounts as $account) {
|
||||
if ($account->values->login_permissions[0] === "Email only") {
|
||||
$emailAccounts[] = new Account($account);
|
||||
}
|
||||
}
|
||||
return $emailAccounts;
|
||||
}
|
||||
}
|
68
src/Managers/Account/Email/EmailManagerInterface.php
Normal file
68
src/Managers/Account/Email/EmailManagerInterface.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Account\Email;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
interface EmailManagerInterface {
|
||||
/**
|
||||
* Creates email account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param string $realname
|
||||
* @param int|null $quota Disk quota for the email account in MB.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function createEmailAccount(string $domain, string $username, string $password, string $realname, int $quota = null) : bool;
|
||||
|
||||
/**
|
||||
* Changes email account quota.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
* @param int $quota
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function changeEmailAccountQuota(string $domain, string $username, int $quota) : bool;
|
||||
|
||||
/**
|
||||
* Deletes email account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function deleteEmailAccount(string $domain, string $username) : bool;
|
||||
|
||||
/**
|
||||
* Disables email account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function disableEmailAccount(string $domain, string $username) : bool;
|
||||
|
||||
/**
|
||||
* Enables email account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function enableEmailAccount(string $domain, string $username) : bool;
|
||||
|
||||
/**
|
||||
* Retrieves all email accounts of given domain.
|
||||
*
|
||||
* @param $domain
|
||||
* @return array Array containing \stdClass instances.
|
||||
*/
|
||||
public function fetchEmailAccounts($domain);
|
||||
}
|
108
src/Managers/Account/Ftp/FtpManager.php
Normal file
108
src/Managers/Account/Ftp/FtpManager.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Account\Ftp;
|
||||
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
use Nilemin\Virtualmin\Entities\Account;
|
||||
use Nilemin\Virtualmin\Managers\Account\AccountModifier;
|
||||
use Nilemin\Virtualmin\Managers\BaseManager;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class FtpManager extends BaseManager implements FtpManagerInterface {
|
||||
|
||||
/**
|
||||
* FtpAccountManager constructor.
|
||||
*
|
||||
* @param HttpClient $httpClient
|
||||
*/
|
||||
public function __construct(HttpClient $httpClient) {
|
||||
parent::__construct($httpClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates ftp account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function createFtpAccount(string $domain, string $username, string $password) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "create-user");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("user", $username);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("pass", $password);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("web");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("ftp");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("noemail");
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes ftp account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function deleteFtpAccount(string $domain, string $username) : bool {
|
||||
$handler = new AccountModifier($this->httpClient,$domain, $username);
|
||||
return $handler->deleteAccount($domain, $username);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables ftp account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function disableFtpAccount(string $domain, string $username) : bool {
|
||||
$handler = new AccountModifier($this->httpClient, $domain, $username);
|
||||
return $handler->disableAccount($domain, $username, "ftp");
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables ftp account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function enableFtpAccount(string $domain, string $username) : bool {
|
||||
$handler = new AccountModifier($this->httpClient, $domain, $username);
|
||||
return $handler->enableAccount($domain, $username, "ftp");
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all ftp accounts of given domain.
|
||||
*
|
||||
* @param $domain
|
||||
* @return array Array containing \stdClass instances.
|
||||
*/
|
||||
public function fetchFtpAccounts($domain) : array {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "list-users");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
|
||||
return $this->filterFtpAccount($this->httpClient->getResponseMessage()->data);
|
||||
}
|
||||
|
||||
private function filterFtpAccount(array $accounts) {
|
||||
$ftpAccounts = [];
|
||||
foreach ($accounts as $account) {
|
||||
if($account->values->ftp_access[0] === "Yes") {
|
||||
$ftpAccounts[] = new Account($account);
|
||||
}
|
||||
}
|
||||
return $ftpAccounts;
|
||||
}
|
||||
}
|
59
src/Managers/Account/Ftp/FtpManagerInterface.php
Normal file
59
src/Managers/Account/Ftp/FtpManagerInterface.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Nilemin\Virtualmin\Managers\Account\Ftp;
|
||||
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
interface FtpManagerInterface {
|
||||
/**
|
||||
* Creates ftp account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function createFtpAccount(string $domain, string $username, string $password) : bool;
|
||||
|
||||
/**
|
||||
* Deletes ftp account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function deleteFtpAccount(string $domain, string $username) : bool;
|
||||
|
||||
/**
|
||||
* Disables ftp account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function disableFtpAccount(string $domain, string $username) : bool;
|
||||
|
||||
/**
|
||||
* Enables ftp account.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function enableFtpAccount(string $domain, string $username) : bool;
|
||||
|
||||
/**
|
||||
* Retrieves all ftp accounts of given domain.
|
||||
*
|
||||
* @param $domain
|
||||
*
|
||||
* @return array Array containing \stdClass instances.
|
||||
*/
|
||||
public function fetchFtpAccounts($domain) : array;
|
||||
}
|
Reference in New Issue
Block a user