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:
commit
ea2a332d97
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.idea
|
||||
vendor
|
11
LICENSE
Normal file
11
LICENSE
Normal file
@ -0,0 +1,11 @@
|
||||
License
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
168
README.md
Normal file
168
README.md
Normal file
@ -0,0 +1,168 @@
|
||||
# php-virtualmin-remote-api
|
||||
|
||||
> **Note:** This is not a full featured implementation of the API.
|
||||
However it is really easy to add new functionalities to the library if needed.
|
||||
|
||||
### Requirements
|
||||
`PHP >= 7.0`
|
||||
|
||||
### Install
|
||||
|
||||
Composer
|
||||
|
||||
```javascript
|
||||
{
|
||||
"require": {
|
||||
"nilemin/php-http-virtualmin-api": ">=1.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### API examples
|
||||
|
||||
```php
|
||||
use Nilemin\Virtualmin\DNS\DNSRecord;
|
||||
use Nilemin\Virtualmin\Managers\DNS\DNSRecordTypes;
|
||||
use Nilemin\Virtualmin\Managers\Server\ServerTypes;
|
||||
use Nilemin\Virtualmin\Managers\SSL\CSRInfo;
|
||||
use Nilemin\Virtualmin\Virtualmin;
|
||||
use Nilet\Components\Configuration\Config;
|
||||
use Nilet\Components\FileSystem\Directory;
|
||||
|
||||
require_once "vendor/autoload.php";
|
||||
|
||||
$config = new Nilet\Components\Configuration\Config();
|
||||
$config->setConfigFolder(new Directory(__DIR__ . "/config"));
|
||||
$api = new VirtualminApi("domain.com", 1221, "root", "password", $config);
|
||||
|
||||
$domain = "domain.com";
|
||||
```
|
||||
|
||||
Email and Ftp accounts
|
||||
```
|
||||
$emailManager = $api->createEmailAccountManager();
|
||||
$ftpManager = $api->createFtpAccountManager();
|
||||
$emailManager->createEmailAccount($domain, "emailaccount1", "123", "Real name", 5);
|
||||
$ftpManager->createFtpAccount($domain, "onlyftp4", "123");
|
||||
|
||||
$emailManager->changeEmailAccountQuota($domain, "emailaccount1", 10);
|
||||
|
||||
$emailManager->disableEmailAccount($domain, "emailaccount1");
|
||||
$ftpManager->disableFtpAccount($domain, "onlyftp1");
|
||||
|
||||
$emailManager->enableEmailAccount($domain, "emailaccount1");
|
||||
$ftpManager->enableFtpAccount($domain, "onlyftp1");
|
||||
|
||||
$emailManager->fetchEmailAccounts($domain);
|
||||
$ftpManager->fetchFtpAccounts($domain);
|
||||
|
||||
$emailManager->deleteEmailAccount($domain, "emailaccount1");
|
||||
$ftpManager->deleteFtpAccount($domain, "onlyftp1");
|
||||
```
|
||||
|
||||
Database
|
||||
```php
|
||||
$dBManager = $api->createDatabaseManager();
|
||||
$dBManager->createDatabase($domain, "mysqldbtest");
|
||||
$dBManager->deleteDatabase($domain, "mysqldbtest");
|
||||
|
||||
$dBManager->fetchDatabasesNames($domain);
|
||||
$dBManager->fetchDatabases($domain);
|
||||
|
||||
$dBManager->grantDatabaseAccess($domain, "emailaccount1", "test2");
|
||||
$dBManager->grantDatabaseAccess($domain, "onlyftp1", "test1");
|
||||
|
||||
$dBManager->removeDatabaseAccess($domain, "emailaccount1", "test2");
|
||||
$dBManager->removeDatabaseAccess($domain, "onlyftp1", "test1");
|
||||
```
|
||||
|
||||
Virtual server
|
||||
```php
|
||||
$vsManager = $api->createVirtualServerManager();
|
||||
$vsManager->changeServerName($domain, "newdomain.org");
|
||||
$vsManager->changeAdminPassword($domain, "7777");
|
||||
|
||||
$options = [
|
||||
"dns",
|
||||
"mail"
|
||||
];
|
||||
$vsManager->createSubServer("sub1.domain.org", $domain, "Sub 1", $options);
|
||||
$vsManager->deleteServer("sub1.domain.org");
|
||||
|
||||
$vsManager->fetchAddonServers("domain");
|
||||
$vsManager->fetchSubServers("domain");
|
||||
$vsManager->fetchSubServersNames($domain, "domain");
|
||||
$vsManager->fetchAddonServersNames($domain, "domain");
|
||||
$vsManager->fetchServer($domain, ServerTypes::TOP_LEVEL_SERVER);
|
||||
|
||||
$vsManager->createServerAlias("domain-alias.org", "domain.org", "");
|
||||
$vsManager->fetchAliasServers("domain");
|
||||
|
||||
$vsManager->addServerRedirect("domain.org", "/", "http://domain-redirect.org");
|
||||
$vsManager->addServerRedirect("domain.org", "/asas", "http://domain-redirect-asas.org");
|
||||
|
||||
$vsManager->fetchServerRedirects($domain);
|
||||
|
||||
$vsManager->deleteServerRedirect($domain, "/");
|
||||
$vsManager->deleteServerRedirect($domain, "/asas");
|
||||
```
|
||||
|
||||
DNS
|
||||
```php
|
||||
$dnsManager = $api->createDnsManager();
|
||||
$dnsManager->addSpfHostnames($domain, ["domain-spf-test.org", "192.168.1"]);
|
||||
$dnsManager->deleteSpfHostnames($domain, ["domain-spf-test.org", "192.168.1"]);
|
||||
|
||||
$dnsRecords = [
|
||||
new DNSRecord("TXT-test1", DNSRecordTypes::TXT, "TXT test value", 666),
|
||||
new DNSRecord("TXT-test2", DNSRecordTypes::TXT, "TXT test value", 777),
|
||||
new DNSRecord("TXT-test4", DNSRecordTypes::TXT, "TXT test value", 777)
|
||||
];
|
||||
$dnsManager->deleteDnsRecords($domain, $dnsRecords);
|
||||
$dnsManager->addDnsRecords($domain, $dnsRecords);
|
||||
$dnsManager->fetchDNSRecords($domain);
|
||||
$dnsManager->fetchSPFOptions($domain);
|
||||
```
|
||||
|
||||
SSL
|
||||
```php
|
||||
$sslManager = $api->createSslManager();
|
||||
$csrInfo = new CSRInfo();
|
||||
$csrInfo->setOrganization("Organization")
|
||||
->setOrganizationUnit("unit")
|
||||
->setCountryCode("dk")
|
||||
->setState("MidtJylland")
|
||||
->setCity("Viborg")
|
||||
->setEmail("test@email.com")
|
||||
->setDomainName($domain);
|
||||
echo $sslManager->generateCSR($csrInfo);
|
||||
echo $sslManager->getCSRContent($domain);
|
||||
```
|
||||
|
||||
PHP
|
||||
```php
|
||||
$phpManager = $api->createPhpManager();
|
||||
$phpManager->fetchPHPDirectories($domain);
|
||||
$phpManager->fetchInstalledPHPVersions();
|
||||
|
||||
$phpManager->addPHPDirectory($domain, "test_php_dr2", "7.0");
|
||||
$phpManager->addPHPDirectory($domain, "test_php_dr3", "5.6");
|
||||
|
||||
$phpManager->deletePHPDirectory($domain, "test_php_dr2");
|
||||
$phpManager->deletePHPDirectory($domain, "test_php_dr3");
|
||||
|
||||
$phpManager->fetchPHPIniVars($domain, "7.0", $config->get("config")["userModifiableIniVars"]);
|
||||
$phpManager->modifyPHPIniVars($domain, "7.0", ["max_execution_time" => 60, "display_errors" => 0]);
|
||||
```
|
||||
|
||||
Script installers
|
||||
```php
|
||||
$scriptManager = $api->createScriptsManager();
|
||||
$scriptManager->installCMS($domain, $config->get("config")["scripts"]["cms"]["wordpress"]);
|
||||
$scriptManager->installPHPMyAdmin($domain, $config->get("config")["scripts"]["dbPanels"]["phpmyadmin"]);
|
||||
|
||||
$scriptManager->deleteScript($domain, "phpmyadmin");
|
||||
|
||||
$scriptManager->fetchInstalledScripts($domain);
|
||||
$scriptManager->fetchInstalledScript($domain, "wordpress");
|
||||
```
|
30
composer.json
Normal file
30
composer.json
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "nilemin/php-http-virtualmin-api",
|
||||
"description": "PHP implementation of Virtualmin's remote api",
|
||||
"license": "MIT",
|
||||
"support": {
|
||||
"issues": "https://github.com/tsvetelintsonev/php-http-virtualmin-api/issues",
|
||||
"source": "https://github.com/tsvetelintsonev/php-http-virtualmin-api"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Tsvetelin Tsonev",
|
||||
"email": "github.tsonev@yahoo.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.0",
|
||||
"niletphp/dependency-container": ">=1.0",
|
||||
"niletphp/configuration": ">=1.0",
|
||||
"niletphp/file-system": ">=1.0",
|
||||
"guzzlehttp/guzzle" : "6.2.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~5.7"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Nilemin\\Virtualmin\\": "src/"
|
||||
}
|
||||
}
|
||||
}
|
148
example.php
Normal file
148
example.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
use Nilemin\Virtualmin\DNS\DNSRecord;
|
||||
use Nilemin\Virtualmin\Managers\DNS\DNSRecordTypes;
|
||||
use Nilemin\Virtualmin\Managers\Server\ServerTypes;
|
||||
use Nilemin\Virtualmin\Managers\SSL\CSRInfo;
|
||||
use Nilemin\Virtualmin\Virtualmin;
|
||||
use Nilet\Components\Configuration\Config;
|
||||
use Nilet\Components\FileSystem\Directory;
|
||||
|
||||
require_once "vendor/autoload.php";
|
||||
|
||||
$config = new Config();
|
||||
$config->setConfigDir(new Directory(__DIR__ . "/config"));
|
||||
$api = new Virtualmin("domain.com", 1221, "root", "password", $config);
|
||||
|
||||
$domain = "domain.com";
|
||||
|
||||
|
||||
|
||||
// Email and Ftp accounts API
|
||||
$emailManager = $api->createEmailManager();
|
||||
$ftpManager = $api->createFtpManager();
|
||||
$emailManager->createEmailAccount($domain, "emailaccount1", "123", "Real name", 5);
|
||||
$ftpManager->createFtpAccount($domain, "onlyftp4", "123");
|
||||
|
||||
$emailManager->changeEmailAccountQuota($domain, "emailaccount1", 10);
|
||||
|
||||
$emailManager->disableEmailAccount($domain, "emailaccount1");
|
||||
$ftpManager->disableFtpAccount($domain, "onlyftp1");
|
||||
|
||||
$emailManager->enableEmailAccount($domain, "emailaccount1");
|
||||
$ftpManager->enableFtpAccount($domain, "onlyftp1");
|
||||
|
||||
$emailManager->fetchEmailAccounts($domain);
|
||||
$ftpManager->fetchFtpAccounts($domain);
|
||||
|
||||
$emailManager->deleteEmailAccount($domain, "emailaccount1");
|
||||
$ftpManager->deleteFtpAccount($domain, "onlyftp1");
|
||||
|
||||
|
||||
|
||||
// Database management API
|
||||
$dBManager = $api->createDatabaseManager();
|
||||
$dBManager->createDatabase($domain, "mysqldbtest");
|
||||
$dBManager->deleteDatabase($domain, "mysqldbtest");
|
||||
|
||||
$dBManager->fetchDatabasesNames($domain);
|
||||
$dBManager->fetchDatabases($domain);
|
||||
|
||||
$dBManager->grantDatabaseAccess($domain, "emailaccount1", "test2");
|
||||
$dBManager->grantDatabaseAccess($domain, "onlyftp1", "test1");
|
||||
|
||||
$dBManager->removeDatabaseAccess($domain, "emailaccount1", "test2");
|
||||
$dBManager->removeDatabaseAccess($domain, "onlyftp1", "test1");
|
||||
|
||||
|
||||
|
||||
// Virtual server API
|
||||
$vsManager = $api->createVirtualServerManager();
|
||||
$vsManager->changeServerName($domain, "newdomain.org");
|
||||
$vsManager->changeAdminPassword($domain, "7777");
|
||||
|
||||
$options = [
|
||||
"dns",
|
||||
"mail"
|
||||
];
|
||||
$vsManager->createSubServer("sub1.domain.org", $domain, "Sub 1", $options);
|
||||
$vsManager->deleteServer("sub1.domain.org");
|
||||
|
||||
$vsManager->fetchAddonServers("domain");
|
||||
$vsManager->fetchSubServers("domain");
|
||||
$vsManager->fetchSubServersNames($domain, "domain");
|
||||
$vsManager->fetchAddonServersNames($domain, "domain");
|
||||
$vsManager->fetchServer($domain, ServerTypes::TOP_LEVEL_SERVER);
|
||||
|
||||
$vsManager->createServerAlias("domain-alias.org", "domain.org", "");
|
||||
$vsManager->fetchAliasServers("domain");
|
||||
|
||||
$vsManager->addServerRedirect("domain.org", "/", "http://domain-redirect.org");
|
||||
$vsManager->addServerRedirect("domain.org", "/asas", "http://domain-redirect-asas.org");
|
||||
|
||||
$vsManager->fetchServerRedirects($domain);
|
||||
|
||||
$vsManager->deleteServerRedirect($domain, "/");
|
||||
$vsManager->deleteServerRedirect($domain, "/asas");
|
||||
|
||||
|
||||
|
||||
// DNS API
|
||||
$dnsManager = $api->createDnsManager();
|
||||
$dnsManager->addSpfHostnames($domain, ["domain-spf-test.org", "192.168.1"]);
|
||||
$dnsManager->deleteSpfHostnames($domain, ["domain-spf-test.org", "192.168.1"]);
|
||||
|
||||
$dnsRecords = [
|
||||
new DNSRecord("TXT-test1", DNSRecordTypes::TXT, "TXT test value", 666),
|
||||
new DNSRecord("TXT-test2", DNSRecordTypes::TXT, "TXT test value", 777),
|
||||
new DNSRecord("TXT-test4", DNSRecordTypes::TXT, "TXT test value", 777)
|
||||
];
|
||||
$dnsManager->deleteDnsRecords($domain, $dnsRecords);
|
||||
$dnsManager->addDnsRecords($domain, $dnsRecords);
|
||||
$dnsManager->fetchDNSRecords($domain);
|
||||
$dnsManager->fetchSPFOptions($domain);
|
||||
|
||||
|
||||
|
||||
// SSL API
|
||||
$sslManager = $api->createSslManager();
|
||||
$csrInfo = new CSRInfo();
|
||||
$csrInfo->setOrganization("Organization")
|
||||
->setOrganizationUnit("unit")
|
||||
->setCountryCode("dk")
|
||||
->setState("MidtJylland")
|
||||
->setCity("Viborg")
|
||||
->setEmail("test@email.com")
|
||||
->setDomainName($domain);
|
||||
echo $sslManager->generateCSR($csrInfo);
|
||||
echo $sslManager->getCSRContent($domain);
|
||||
|
||||
|
||||
|
||||
// PHP management API
|
||||
$phpManager = $api->createPhpManager();
|
||||
$phpManager->fetchPHPDirectories($domain);
|
||||
$phpManager->fetchInstalledPHPVersions();
|
||||
|
||||
$phpManager->addPHPDirectory($domain, "test_php_dr2", "7.0");
|
||||
$phpManager->addPHPDirectory($domain, "test_php_dr3", "5.6");
|
||||
|
||||
$phpManager->deletePHPDirectory($domain, "test_php_dr2");
|
||||
$phpManager->deletePHPDirectory($domain, "test_php_dr3");
|
||||
|
||||
$phpManager->fetchPHPIniVars($domain, "7.0", $config->get("config")["userModifiableIniVars"]);
|
||||
$phpManager->modifyPHPIniVars($domain, "7.0", ["max_execution_time" => 60, "display_errors" => 0]);
|
||||
|
||||
|
||||
|
||||
// Script installers API
|
||||
$scriptManager = $api->createScriptsManager();
|
||||
$scriptManager->installCMS($domain, $config->get("config")["scripts"]["cms"]["wordpress"]);
|
||||
$scriptManager->installPHPMyAdmin($domain, $config->get("config")["scripts"]["dbPanels"]["phpmyadmin"]);
|
||||
|
||||
$scriptManager->deleteScript($domain, "phpmyadmin");
|
||||
|
||||
$scriptManager->fetchInstalledScripts($domain);
|
||||
$scriptManager->fetchInstalledScript($domain, "wordpress");
|
85
src/Entities/Account.php
Normal file
85
src/Entities/Account.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Entities;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class Account {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var \stdClass
|
||||
*/
|
||||
protected $account;
|
||||
|
||||
public function __construct(\stdClass $account) {
|
||||
$this->name = $account->name;
|
||||
$this->account = $account->values;
|
||||
}
|
||||
|
||||
public function getUnixUsername() {
|
||||
return $this->account->unix_username[0];
|
||||
}
|
||||
|
||||
public function getMailStorageType() {
|
||||
return $this->account->mail_storage_type[0];
|
||||
}
|
||||
|
||||
public function getUserType() {
|
||||
return $this->account->user_type[0];
|
||||
}
|
||||
|
||||
public function getDisabled() {
|
||||
return $this->account->disabled[0];
|
||||
}
|
||||
|
||||
public function getFtpAccess() {
|
||||
return $this->account->ftp_access[0];
|
||||
}
|
||||
|
||||
public function getDomain() {
|
||||
return $this->account->domain[0];
|
||||
}
|
||||
|
||||
public function getHomeByteQuota() {
|
||||
return $this->account->home_byte_quota[0];
|
||||
}
|
||||
|
||||
public function getHomeByteQuotaUsed() {
|
||||
return $this->account->home_byte_quota_used[0];
|
||||
}
|
||||
|
||||
public function getHomeQuota() {
|
||||
return $this->account->home_quota[0];
|
||||
}
|
||||
|
||||
public function getHomeQuotaUsed() {
|
||||
return $this->account->home_quota_used[0];
|
||||
}
|
||||
|
||||
public function getHomeDirectory() {
|
||||
return $this->account->home_directory[0];
|
||||
}
|
||||
|
||||
public function getEmailAddress() {
|
||||
return $this->account->email_address[0];
|
||||
}
|
||||
|
||||
public function getRealName() {
|
||||
return $this->account->real_name[0];
|
||||
}
|
||||
|
||||
public function getLoginPermissions() {
|
||||
return $this->account->login_permissions[0];
|
||||
}
|
||||
|
||||
public function getMailLocation() {
|
||||
return $this->account->mail_location[0];
|
||||
}
|
||||
|
||||
|
||||
}
|
28
src/Entities/Database.php
Normal file
28
src/Entities/Database.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Entities;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class Database extends Entity {
|
||||
|
||||
public function __construct(\stdClass $object) {
|
||||
parent::__construct($object);
|
||||
}
|
||||
|
||||
public function getTables() : array {
|
||||
return $this->object->tables;
|
||||
}
|
||||
|
||||
public function getByteSize() : string {
|
||||
return $this->object->byte_size[0];
|
||||
}
|
||||
|
||||
public function getType() : string {
|
||||
return $this->object->type[0];
|
||||
}
|
||||
|
||||
public function getSize() : string {
|
||||
return $this->object->size[0];
|
||||
}
|
||||
}
|
28
src/Entities/Entity.php
Normal file
28
src/Entities/Entity.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Entities;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class Entity {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var \stdClass
|
||||
*/
|
||||
protected $object;
|
||||
|
||||
public function __construct(\stdClass $object) {
|
||||
$this->name = $object->name;
|
||||
$this->object = $object->values;
|
||||
}
|
||||
|
||||
public function getName() : string {
|
||||
return $this->object->name;
|
||||
}
|
||||
|
||||
}
|
29
src/Entities/PHPDirectory.php
Normal file
29
src/Entities/PHPDirectory.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Entities;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class PHPDirectory extends Entity {
|
||||
|
||||
public function __construct(\stdClass $object) {
|
||||
parent::__construct($object);
|
||||
}
|
||||
|
||||
public function isWebRootDirectory() {
|
||||
return $this->object->web_root_directory[0] === "YES";
|
||||
}
|
||||
|
||||
public function getFullPHPVersion() {
|
||||
return $this->object->full_version[0];
|
||||
}
|
||||
|
||||
public function getPHPVersion() {
|
||||
return $this->object->php_version[0];
|
||||
}
|
||||
|
||||
public function getPHPExecutionMode() {
|
||||
return $this->object->execution_mode[0];
|
||||
}
|
||||
|
||||
}
|
125
src/Entities/Server.php
Normal file
125
src/Entities/Server.php
Normal file
@ -0,0 +1,125 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Entities;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class Server extends SubServer {
|
||||
|
||||
public function __construct(\stdClass $sever) {
|
||||
parent::__construct($sever);
|
||||
}
|
||||
|
||||
public function getUserQuota() {
|
||||
return $this->server->user_quota[0];
|
||||
}
|
||||
|
||||
public function getUserByteQuotaUsed() {
|
||||
return $this->server->user_byte_quota_used[0];
|
||||
}
|
||||
|
||||
public function getUserQuotaUsed() {
|
||||
return $this->server->user_quota_used[0];
|
||||
}
|
||||
|
||||
public function getUserBlockQuota() {
|
||||
return $this->server->user_block_quota[0];
|
||||
}
|
||||
|
||||
public function getUserBlockQuotaUsed() {
|
||||
return $this->server->user_block_quota_used[0];
|
||||
}
|
||||
|
||||
public function getServerQuota() {
|
||||
return $this->server->server_quota[0];
|
||||
}
|
||||
|
||||
public function getServerQuotaUsed() {
|
||||
return $this->server->server_quota_used[0];
|
||||
}
|
||||
|
||||
public function getServerBlockQuota() {
|
||||
return $this->server->server_block_quota[0];
|
||||
}
|
||||
|
||||
public function getServerBlockQuotaUsed() {
|
||||
return $this->server->server_block_quota_used[0];
|
||||
}
|
||||
|
||||
public function getServerByteQuotaUsed() {
|
||||
return $this->server->server_byte_quota_used[0];
|
||||
}
|
||||
|
||||
public function getBandwidthByteLimit() {
|
||||
return $this->server->bandwidth_byte_limit[0];
|
||||
}
|
||||
|
||||
public function getBandwidthUsage() {
|
||||
return $this->server->bandwidth_usage[0];
|
||||
}
|
||||
|
||||
public function getBandwidthByteUsage() {
|
||||
return $this->server->bandwidth_byte_usage[0];
|
||||
}
|
||||
|
||||
public function getBandwidthStart() {
|
||||
return $this->server->bandwidth_start[0];
|
||||
}
|
||||
|
||||
|
||||
public function getBandwidthLimit() {
|
||||
return $this->server->bandwidth_limit[0];
|
||||
}
|
||||
|
||||
public function getEditCapabilities() {
|
||||
return $this->server->edit_capabilities[0];
|
||||
}
|
||||
|
||||
public function getAllowedMysqlHosts() {
|
||||
return $this->server->allowed_mysql_hosts[0];
|
||||
}
|
||||
|
||||
public function getAllowedFeatures() {
|
||||
return $this->server->allowed_features[0];
|
||||
}
|
||||
|
||||
public function getMaximumSubServers() {
|
||||
return $this->server->{"maximum_sub-servers"}[0];
|
||||
}
|
||||
|
||||
public function getMaximumMailboxes() {
|
||||
return $this->server->maximum_mailboxes[0];
|
||||
}
|
||||
|
||||
public function getReadOnlyMode() {
|
||||
return $this->server->{"read-only_mode"}[0];
|
||||
}
|
||||
|
||||
public function getMaximumNonAliasServers() {
|
||||
return $this->server->{"maximum_non-alias_servers"}[0];
|
||||
}
|
||||
|
||||
public function getSubServersInheritIpAddress() {
|
||||
return $this->server->{"sub-servers_inherit_ip_address"}[0];
|
||||
}
|
||||
|
||||
public function getMaximumProcesses() {
|
||||
return $this->server->maximum_processes[0];
|
||||
}
|
||||
|
||||
public function getMaximumDatabases() {
|
||||
return $this->server->maximum_databases[0];
|
||||
}
|
||||
|
||||
public function getMaximumAliasServers() {
|
||||
return $this->server->maximum_alias_servers[0];
|
||||
}
|
||||
|
||||
public function getLoginPermissions() {
|
||||
return $this->server->login_permissions[0];
|
||||
}
|
||||
|
||||
public function getMaximumAliases() {
|
||||
return $this->server->maximum_aliases[0];
|
||||
}
|
||||
}
|
17
src/Entities/ServerRedirect.php
Normal file
17
src/Entities/ServerRedirect.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Entities;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class ServerRedirect extends Entity {
|
||||
|
||||
public function __construct(\stdClass $object) {
|
||||
parent::__construct($object);
|
||||
}
|
||||
|
||||
public function getDestination() {
|
||||
return $this->object->destination[0];
|
||||
}
|
||||
|
||||
}
|
164
src/Entities/SubServer.php
Normal file
164
src/Entities/SubServer.php
Normal file
@ -0,0 +1,164 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Entities;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class SubServer extends Entity {
|
||||
|
||||
public function __construct(\stdClass $object) {
|
||||
parent::__construct($object);
|
||||
}
|
||||
|
||||
public function getPHPMaxExecutionTime() {
|
||||
return $this->object->php_max_execution_time[0];
|
||||
}
|
||||
|
||||
public function getPasswordForMysql() {
|
||||
return $this->object->password_for_mysql[0];
|
||||
}
|
||||
|
||||
public function getSpamassassinClient() {
|
||||
return $this->object->spamassassin_client[0];
|
||||
}
|
||||
|
||||
public function getDatabasesSize() {
|
||||
return $this->object->databases_size[0];
|
||||
}
|
||||
|
||||
public function getCgiDirectory() {
|
||||
return $this->object->cgi_directory[0];
|
||||
}
|
||||
|
||||
public function getCreatedOn() {
|
||||
return $this->object->created_on[0];
|
||||
}
|
||||
|
||||
public function getPHPVersion() {
|
||||
return $this->object->php_version[0];
|
||||
}
|
||||
|
||||
public function getPlan() {
|
||||
return $this->object->plan[0];
|
||||
}
|
||||
|
||||
public function getPlanId() {
|
||||
return $this->object->plan_id[0];
|
||||
}
|
||||
|
||||
public function getSPF_DNS_Record_Status() {
|
||||
return $this->object->spf_dns_record[0];
|
||||
}
|
||||
|
||||
public function getHomeDirectory() {
|
||||
return $this->object->home_directory[0];
|
||||
}
|
||||
|
||||
public function getErrorLog() {
|
||||
return $this->object->error_log[0];
|
||||
}
|
||||
|
||||
public function getTemplate() {
|
||||
return $this->object->template[0];
|
||||
}
|
||||
|
||||
public function getPHPExecutionMode() {
|
||||
return $this->object->php_execution_mode[0];
|
||||
}
|
||||
|
||||
public function getDescription() {
|
||||
return $this->object->description[0];
|
||||
}
|
||||
|
||||
public function getUsername() {
|
||||
return $this->object->username[0];
|
||||
}
|
||||
|
||||
public function getHtmlDirectory() {
|
||||
return $this->object->html_directory[0];
|
||||
}
|
||||
|
||||
public function getSpamClearingPolicy() {
|
||||
return $this->object->spam_clearing_policy[0];
|
||||
}
|
||||
|
||||
public function getVirusDelivery() {
|
||||
return $this->object->virus_delivery[0];
|
||||
}
|
||||
|
||||
public function getDatabasesCount() {
|
||||
return $this->object->databases_count[0];
|
||||
}
|
||||
|
||||
public function getType() {
|
||||
return $this->object->type[0];
|
||||
}
|
||||
|
||||
public function getContactAddress() {
|
||||
return $this->object->contact_address[0];
|
||||
}
|
||||
|
||||
public function getParentDomainName() {
|
||||
return $this->object->parent_domain[0];
|
||||
}
|
||||
|
||||
public function getDatabasesByteSize() {
|
||||
return $this->object->databases_byte_size[0];
|
||||
}
|
||||
|
||||
public function getIPAddress() {
|
||||
return $this->object->ip_address[0];
|
||||
}
|
||||
|
||||
public function getTrashClearingPolicy() {
|
||||
return $this->object->trash_clearing_policy[0];
|
||||
}
|
||||
|
||||
public function getRubyExecutionMode() {
|
||||
return $this->object->ruby_execution_mode[0];
|
||||
}
|
||||
|
||||
public function getTemplateId() {
|
||||
return $this->object->template_id[0];
|
||||
}
|
||||
|
||||
public function getGroupName() {
|
||||
return $this->object->group_name[0];
|
||||
}
|
||||
|
||||
public function getFeatures() {
|
||||
return $this->object->features[0];
|
||||
}
|
||||
|
||||
public function hasFeature(string $feature) {
|
||||
return strpos($this->getFeatures(), strtolower($feature)) !== false;
|
||||
}
|
||||
|
||||
public function getDMARC_DNS_Record_Status() {
|
||||
return $this->object->dmarc_dns_record[0];
|
||||
}
|
||||
|
||||
public function getAccessLog() {
|
||||
return $this->object->access_log[0];
|
||||
}
|
||||
|
||||
public function getUsernameForMysql() {
|
||||
return $this->object->username_for_mysql[0];
|
||||
}
|
||||
|
||||
public function getGroupId() {
|
||||
return $this->object->group_id[0];
|
||||
}
|
||||
|
||||
public function getContactEmail() {
|
||||
return $this->object->contact_email[0];
|
||||
}
|
||||
|
||||
public function getSSLKeyFile() {
|
||||
return $this->object->ssl_key_file[0];
|
||||
}
|
||||
|
||||
public function getSSLCertFile() {
|
||||
return $this->object->ssl_cert_file[0];
|
||||
}
|
||||
}
|
131
src/Http/HttpClient.php
Normal file
131
src/Http/HttpClient.php
Normal file
@ -0,0 +1,131 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Http;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class HttpClient implements HttpClientInterface {
|
||||
|
||||
/**
|
||||
* Virtualmin port
|
||||
* @var int
|
||||
*/
|
||||
private $port;
|
||||
|
||||
/**
|
||||
* Root user name.
|
||||
* @var string
|
||||
*/
|
||||
private $rootName;
|
||||
|
||||
/**
|
||||
* Root user password.
|
||||
* @var string
|
||||
*/
|
||||
private $rootPassword;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $virtualminRemoteApiPath = "virtual-server/remote.cgi";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $cloudminRemoteApi = "server-manager/remote.cgi";
|
||||
|
||||
/**
|
||||
* Virtualmin url.
|
||||
* @var string
|
||||
*/
|
||||
private $url;
|
||||
|
||||
/**
|
||||
* Guzzle HTTP Client.
|
||||
* @var \GuzzleHttp\Client
|
||||
*/
|
||||
private $client = null;
|
||||
|
||||
/**
|
||||
* The response returned from Guzzle.
|
||||
* @var \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
private $response;
|
||||
|
||||
/**
|
||||
* The response message type - json, xml
|
||||
* @var string
|
||||
*/
|
||||
private $responseType = "json";
|
||||
|
||||
/**
|
||||
* Response message returned by Virtualmin.
|
||||
* @var \stdClass
|
||||
*/
|
||||
private $responseMessage;
|
||||
|
||||
/**
|
||||
* The query builder.
|
||||
* @var QueryStringBuilder
|
||||
*/
|
||||
private $queryStringBuilder;
|
||||
|
||||
/**
|
||||
* HttpClient constructor.
|
||||
* @param string $url Virualmins URL.
|
||||
* @param int $port Virualmins port.
|
||||
* @param string $rootName Root user name.
|
||||
* @param string $rootPassword Root user password.
|
||||
* @param string $responseType Response format - json, xml.
|
||||
*/
|
||||
public function __construct(string $url, int $port, string $rootName, string $rootPassword, string $responseType) {
|
||||
$this->client = new \GuzzleHttp\Client(array('curl' => array(CURLOPT_SSL_VERIFYPEER => false)));
|
||||
$this->queryStringBuilder = new QueryStringBuilder();
|
||||
$this->rootName = $rootName;
|
||||
$this->rootPassword = $rootPassword;
|
||||
$this->port = $port;
|
||||
$this->url = "https://{$rootName}:{$rootPassword}@{$url}:{$this->port}/{$this->virtualminRemoteApiPath}";
|
||||
$this->responseType = $responseType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends request to Virtualmin's remote api.
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function sendRequest() : bool {
|
||||
$this->dispatch();
|
||||
return $this->processResponse();
|
||||
}
|
||||
|
||||
private function buildUrl() : string {
|
||||
return $this->url . "?" . $this->queryStringBuilder->createQueryString() . "&" . $this->responseType . "=1";
|
||||
}
|
||||
|
||||
private function dispatch() {
|
||||
$url = $this->buildUrl();
|
||||
$this->response = $this->client->get($url);
|
||||
}
|
||||
|
||||
private function processResponse() {
|
||||
$this->responseMessage = json_decode($this->response->getBody()->getContents());
|
||||
// TODO delete var_dump.
|
||||
var_dump($this->responseMessage);
|
||||
if ($this->responseMessage->status == "success") return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the response message.
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function getResponseMessage() {
|
||||
return $this->responseMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return QueryStringBuilder
|
||||
*/
|
||||
public function queryStringBuilder() : QueryStringBuilder {
|
||||
return $this->queryStringBuilder;
|
||||
}
|
||||
}
|
26
src/Http/HttpClientInterface.php
Normal file
26
src/Http/HttpClientInterface.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Http;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
interface HttpClientInterface {
|
||||
/**
|
||||
* Sends request to Virtualmin's remote api.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function sendRequest() : bool;
|
||||
|
||||
/**
|
||||
* Retrieves the response message.
|
||||
*
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function getResponseMessage();
|
||||
|
||||
/**
|
||||
* @return QueryStringBuilder
|
||||
*/
|
||||
public function queryStringBuilder() : QueryStringBuilder;
|
||||
}
|
25
src/Http/QueryStringBuilder.php
Normal file
25
src/Http/QueryStringBuilder.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Http;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class QueryStringBuilder implements QueryStringBuilderInterface {
|
||||
|
||||
private $queryString;
|
||||
|
||||
public function addParameter(string $name, string $value = "") {
|
||||
$this->queryString .= $name . "=" . $value . "&";
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes the query string.
|
||||
* @return string
|
||||
*/
|
||||
public function createQueryString(): string {
|
||||
$string = rtrim($this->queryString, "&");
|
||||
$this->queryString = "";
|
||||
return $string;
|
||||
}
|
||||
|
||||
}
|
23
src/Http/QueryStringBuilderInterface.php
Normal file
23
src/Http/QueryStringBuilderInterface.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Http;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
interface QueryStringBuilderInterface {
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function addParameter(string $name, string $value = "");
|
||||
|
||||
/**
|
||||
* Flushes the query string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function createQueryString() : string;
|
||||
}
|
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;
|
||||
}
|
26
src/Managers/BaseManager.php
Normal file
26
src/Managers/BaseManager.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace Nilemin\Manager;
|
||||
|
||||
use Nilemin\Virtualmin\Http\HttpClientInterface;
|
||||
use Nilet\Components\Configuration\Config;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
abstract class BaseManager {
|
||||
|
||||
/**
|
||||
* @var HttpClientInterface
|
||||
*/
|
||||
protected $httpClient;
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
public function __construct(HttpClientInterface $httpClient, Config $config = null) {
|
||||
$this->httpClient = $httpClient;
|
||||
$this->config = $config;
|
||||
}
|
||||
}
|
21
src/Managers/Cron/CronManager.php
Normal file
21
src/Managers/Cron/CronManager.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Cron;
|
||||
|
||||
use Nilemin\Virtualmin\BaseManager;
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class CronManager extends BaseManager implements CronManagerInterface, CronManagerInterface {
|
||||
|
||||
/**
|
||||
* CronManager constructor.
|
||||
*
|
||||
* @param HttpClient $httpClient
|
||||
*/
|
||||
public function __construct(HttpClient $httpClient) {
|
||||
parent::__construct($httpClient);
|
||||
}
|
||||
|
||||
}
|
8
src/Managers/Cron/CronManagerInterface.php
Normal file
8
src/Managers/Cron/CronManagerInterface.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Cron;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
interface CronManagerInterface {
|
||||
}
|
220
src/Managers/DNS/DNSManager.php
Normal file
220
src/Managers/DNS/DNSManager.php
Normal file
@ -0,0 +1,220 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\DNS;
|
||||
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
use Nilemin\Manager\BaseManager;
|
||||
use Nilet\Components\Configuration\Config;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class DNSManager extends BaseManager implements DNSManagerInterface {
|
||||
|
||||
private $dnsRecordTypes = [
|
||||
DNSRecordTypes::A => "A - IPv4 Address",
|
||||
DNSRecordTypes::AAAA => "AAAA - IPv6 Address",
|
||||
DNSRecordTypes::CNAME => "CNAME - Name Alias",
|
||||
DNSRecordTypes::MX => "MX - Mail Server",
|
||||
DNSRecordTypes::NS => "NS - Name Server",
|
||||
DNSRecordTypes::PTR => "PTR - Reverse Address",
|
||||
DNSRecordTypes::SRV => "SRV - Service record",
|
||||
DNSRecordTypes::TXT => "TXT - Text"
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $ttlUnits = [
|
||||
"s", // seconds
|
||||
"m", // minutes
|
||||
"h", // hours
|
||||
"d", // days
|
||||
"w" // weeks
|
||||
];
|
||||
|
||||
/**
|
||||
* DNSManager constructor.
|
||||
*
|
||||
* @param HttpClient $httpClient
|
||||
* @param Config $config
|
||||
*/
|
||||
public function __construct(HttpClient $httpClient, Config $config) {
|
||||
parent::__construct($httpClient, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a dns records to a given domain.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param array $dnsRecords DNSRecord instances.
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function addDnsRecords(string $domain, array $dnsRecords) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "modify-dns");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
foreach ($dnsRecords as $record) {
|
||||
/* @var $record DNSRecord */
|
||||
$this->httpClient->queryStringBuilder()->addParameter("add-record-with-ttl", (string)$record);
|
||||
}
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a dns records to a given domain.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param array $dnsRecords DNSRecord instances.
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function deleteDnsRecords(string $domain, array $dnsRecords) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "modify-dns");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
foreach ($dnsRecords as $record) {
|
||||
/* @var $record DNSRecord */
|
||||
$this->httpClient->queryStringBuilder()->addParameter("remove-record", $record->getName() . " " . $record->getType());
|
||||
}
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds SPF (Sender Policy Framework) host names option to a given domain.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param array $hostnames Host names e.g ["domain.com", "192.168.1", etc.]
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function addSpfHostnames(string $domain, array $hostnames) : bool {
|
||||
return $this->handleSpfHostnames($domain, $hostnames, "add");
|
||||
}
|
||||
|
||||
/**
|
||||
* * Deletes SPF (Sender Policy Framework) host names from a given domain.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param array $hostnames Host names e.g ["domain.com", "192.168.1", etc.]
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function deleteSpfHostnames(string $domain, array $hostnames) : bool {
|
||||
return $this->handleSpfHostnames($domain, $hostnames, "remove");
|
||||
}
|
||||
|
||||
private function handleSpfHostnames(string $domain, array $hostnames, string $action) {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "modify-dns");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
foreach ($hostnames as $hostname) {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("spf-{$action}-a", $hostname);
|
||||
}
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all domain DNS records.
|
||||
*
|
||||
* @param string $domain
|
||||
* @return array
|
||||
*/
|
||||
public function fetchDNSRecords(string $domain) : array {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "get-dns");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
return $this->filterDNSRecords($this->httpClient->getResponseMessage()->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves record's SPF options
|
||||
*
|
||||
* @param string $domain
|
||||
* @return array Array with SPF options if any, empty array otherwise.
|
||||
*/
|
||||
public function fetchSPFOptions(string $domain) : array {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "get-dns");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
return $this->filterSPFOptions($this->httpClient->getResponseMessage()->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters DNS records. Remove NSEC, DNSSEC, DNSKEY, RRSIG etc.
|
||||
*
|
||||
* @param array $dnsRecords
|
||||
* @return array
|
||||
*/
|
||||
private function filterDNSRecords(array $dnsRecords) : array {
|
||||
$filteredRecords = [];
|
||||
foreach ($dnsRecords as $record) {
|
||||
if (isset($this->dnsRecordTypes[$record->values->type[0]]) && !$this->isDKIMRecord($record)) {
|
||||
$value = trim(implode(" ", $record->values->value));
|
||||
$ttl = $this->processTtl($record->values->ttl[0]);
|
||||
$filteredRecords[] = new DNSRecord($record->name, $record->values->type[0], $value, $ttl["value"], $ttl["unit"]);
|
||||
}
|
||||
}
|
||||
return $filteredRecords;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts record's ttl value and unit.
|
||||
*
|
||||
* @param $ttl
|
||||
* @return array
|
||||
*/
|
||||
private function processTtl($ttl) : array {
|
||||
$ttlData = [];
|
||||
$ttl = isset($ttl) ? $ttl : $this->config->get("config")["defaultTtl"];
|
||||
$ttlData["value"] = substr($ttl, 0, strlen($ttl) - 1);
|
||||
$ttlData["unit"] = substr($ttl, -1);
|
||||
return $ttlData;
|
||||
}
|
||||
|
||||
private function filterSPFOptions(array $dnsRecords) : array {
|
||||
foreach ($dnsRecords as $record) {
|
||||
if ($this->isSPFRecord($record)) {
|
||||
return $this->extractSPFOptions($record->values->value[0]);
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
private function extractSPFOptions(string $value) : array {
|
||||
$matches = [];
|
||||
preg_match_all('/a:([^\s]+)/i', $value, $matches);
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a given DNS record is DomainKeys Identified Mail.
|
||||
*
|
||||
* @param \stdClass $record
|
||||
* @return bool
|
||||
*/
|
||||
private function isDKIMRecord(\stdClass $record) : bool {
|
||||
return strpos($record->name, "_domainkey") !== false;
|
||||
}
|
||||
|
||||
private function isSPFRecord(\stdClass $record) : bool {
|
||||
return $record->values->type[0] === "SPF";
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves DNS record types.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDNSRecordTypes() : array {
|
||||
return $this->dnsRecordTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ttl units. ["s", "m", "h", "d", w]
|
||||
* Respectively seconds, minutes, hours, days, weeks
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTtlUnits() : array {
|
||||
return $this->ttlUnits;
|
||||
}
|
||||
|
||||
}
|
71
src/Managers/DNS/DNSManagerInterface.php
Normal file
71
src/Managers/DNS/DNSManagerInterface.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\DNS;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
interface DNSManagerInterface {
|
||||
/**
|
||||
* Adds a dns records to a given domain.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param array $dnsRecords DNSRecord instances.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function addDnsRecords(string $domain, array $dnsRecords) : bool;
|
||||
|
||||
/**
|
||||
* Deletes a dns records to a given domain.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param array $dnsRecords DNSRecord instances.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function deleteDnsRecords(string $domain, array $dnsRecords) : bool;
|
||||
|
||||
/**
|
||||
* Adds SPF (Sender Privacy Framework) host names option to a given domain.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param array $hostnames Host names e.g ["domain.com", "192.168.1", etc.]
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function addSpfHostnames(string $domain, array $hostnames) : bool;
|
||||
|
||||
/**
|
||||
* * Deletes SPF (Sender Privacy Framework) host names from a given domain.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param array $hostnames Host names e.g ["domain.com", "192.168.1", etc.]
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function deleteSpfHostnames(string $domain, array $hostnames) : bool;
|
||||
|
||||
/**
|
||||
* Retrieves all domain DNS records.
|
||||
*
|
||||
* @param string $domain
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fetchDNSRecords(string $domain) : array;
|
||||
|
||||
/**
|
||||
* Retrieves DNS record types.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDNSRecordTypes() : array;
|
||||
|
||||
/**
|
||||
* Ttl units. ["s", "m", "h", "d", w]
|
||||
* Respectively seconds, minutes, hours, days, weeks
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTtlUnits() : array;
|
||||
}
|
120
src/Managers/DNS/DNSRecord.php
Normal file
120
src/Managers/DNS/DNSRecord.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\DNS;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class DNSRecord {
|
||||
|
||||
/**
|
||||
* Name of the dns record.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* Type of the dns record.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $type;
|
||||
|
||||
/**
|
||||
* Value of the dns record.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* Time to live in seconds.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $ttl = 300;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $ttlUnit = "m";
|
||||
|
||||
/**
|
||||
* DNSRecord constructor.
|
||||
*
|
||||
* @param string $name Dns record name.
|
||||
* @param string $type DNSRecordTypes constant.
|
||||
* @param string $value Dns record value.
|
||||
* @param int $ttl Time to live in seconds.
|
||||
* @param string $ttlUnit. "s" for seconds, "m" for minutes, "h" for hours, "d" for days, "w" for weeks.
|
||||
*/
|
||||
public function __construct(string $name, string $type, string $value, int $ttl = null, string $ttlUnit = null) {
|
||||
$this->name = $this->normalizeName($name);
|
||||
$this->type = $type;
|
||||
$this->value = $value;
|
||||
if($ttl !== null) {
|
||||
$this->ttl = $ttl;
|
||||
}
|
||||
if($ttlUnit !== null) {
|
||||
$this->ttlUnit = $ttlUnit;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes/extracts record's name.
|
||||
* Virtualmin returns subdomain DNS records with names consisting of subdomain name, domain name and a "." suffix.
|
||||
* We are interested only in the subdomain name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
private function normalizeName(string $name) : string {
|
||||
if(substr_count($name, ".") > 2) {
|
||||
return substr($name, 0, strrpos(substr($name, 0, strrpos($name, ".", -2)), "."));
|
||||
}
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates string with the following format - "name type value",
|
||||
* which is ready for use when adding dns records.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() {
|
||||
return trim($this->name . " " . $this->type . " " . $this->ttl . $this->ttlUnit . " " . $this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getValue(): string {
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTtl(): int {
|
||||
return $this->ttl;
|
||||
}
|
||||
|
||||
public function getTtlUnit() : string {
|
||||
return $this->ttlUnit;
|
||||
}
|
||||
|
||||
}
|
48
src/Managers/DNS/DNSRecordTypes.php
Normal file
48
src/Managers/DNS/DNSRecordTypes.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\DNS;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class DNSRecordTypes {
|
||||
|
||||
/**
|
||||
* IPv4 Address
|
||||
*/
|
||||
const A = "A";
|
||||
|
||||
/**
|
||||
* IPv6 Address
|
||||
*/
|
||||
const AAAA = "AAAA";
|
||||
|
||||
/**
|
||||
* Name Alias
|
||||
*/
|
||||
const CNAME = "CNAME";
|
||||
|
||||
/**
|
||||
* Name Server
|
||||
*/
|
||||
const NS = "NS";
|
||||
|
||||
/**
|
||||
* Mail Server
|
||||
*/
|
||||
const MX = "MX";
|
||||
|
||||
/**
|
||||
* Text
|
||||
*/
|
||||
const TXT = "TXT";
|
||||
|
||||
/**
|
||||
* Reverse address
|
||||
*/
|
||||
const PTR = "PTR";
|
||||
|
||||
/**
|
||||
* Service Record
|
||||
*/
|
||||
const SRV = "SRV";
|
||||
}
|
138
src/Managers/Database/DatabaseManager.php
Normal file
138
src/Managers/Database/DatabaseManager.php
Normal file
@ -0,0 +1,138 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Database;
|
||||
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
use Nilemin\Manager\BaseManager;
|
||||
use Nilemin\Virtualmin\Entities\Database;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class DatabaseManager extends BaseManager implements DatabaseManagerInterface {
|
||||
|
||||
/**
|
||||
* DatabaseManager constructor.
|
||||
*
|
||||
* @param HttpClient $httpClient
|
||||
*/
|
||||
public function __construct(HttpClient $httpClient) {
|
||||
parent::__construct($httpClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grants database access to user.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
* @param $database
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function grantDatabaseAccess(string $domain, string $username, $database) : bool {
|
||||
return $this->handleDatabaseAccess($domain, $username, $database, "add");
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes database user access.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
* @param $database
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function removeDatabaseAccess(string $domain, string $username, $database) : bool {
|
||||
return $this->handleDatabaseAccess($domain, $username, $database, "remove");
|
||||
}
|
||||
|
||||
private function handleDatabaseAccess(string $domain, string $username, $database, $action) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "modify-user");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("user", $username);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("{$action}-mysql", $database);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a MySQL database for a given virtual server.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $database Database name
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function createDatabase(string $domain, string $database
|
||||
) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "create-database");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("name", $database);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("type", "mysql");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("opt", "charset utf8");
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a MySQL database.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $database Database name.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteDatabase(string $domain, string $database) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "delete-database");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("name", $database);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("type", "mysql");
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all MySQL databases names for a given domain.
|
||||
*
|
||||
* @param $domain
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetchDatabasesNames($domain) {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "list-databases");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("type", "mysql");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("name-only");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
|
||||
$data = $this->httpClient->getResponseMessage()->data;
|
||||
$names = [];
|
||||
foreach ($data as $item) {
|
||||
$names[] = $item->name;
|
||||
}
|
||||
return $names;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all MySQL databases for a given domain.
|
||||
*
|
||||
* @param $domain
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetchDatabases($domain) {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "list-databases");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("type", "mysql");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
|
||||
$data = $this->httpClient->getResponseMessage()->data;
|
||||
$databases = [];
|
||||
foreach ($data as $db) {
|
||||
$databases[] = new Database($db);
|
||||
}
|
||||
return $databases;
|
||||
}
|
||||
}
|
69
src/Managers/Database/DatabaseManagerInterface.php
Normal file
69
src/Managers/Database/DatabaseManagerInterface.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Nilemin\Virtualmin\Managers\Database;
|
||||
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
interface DatabaseManagerInterface {
|
||||
/**
|
||||
* Grants database access to user.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
* @param $database
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function grantDatabaseAccess(string $domain, string $username, $database) : bool;
|
||||
|
||||
/**
|
||||
* Removes database user access.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $username
|
||||
* @param $database
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function removeDatabaseAccess(string $domain, string $username, $database) : bool;
|
||||
|
||||
/**
|
||||
* Creates a MySQL database for a given virtual server.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $databaseName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function createDatabase(string $domain, string $databaseName) : bool;
|
||||
|
||||
/**
|
||||
* Deletes a MySQL database.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $databaseName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteDatabase(string $domain, string $databaseName) : bool;
|
||||
|
||||
/**
|
||||
* Retrieves all MySQL databases names for a given domain.
|
||||
*
|
||||
* @param $domain
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetchDatabasesNames($domain);
|
||||
|
||||
/**
|
||||
* Retrieves all MySQL databases for a given domain.
|
||||
*
|
||||
* @param $domain
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetchDatabases($domain);
|
||||
}
|
133
src/Managers/PHP/PHPManager.php
Normal file
133
src/Managers/PHP/PHPManager.php
Normal file
@ -0,0 +1,133 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\PHP;
|
||||
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
use Nilemin\Manager\BaseManager;
|
||||
use Nilemin\Virtualmin\Entities\PHPDirectory;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class PHPManager extends BaseManager implements PHPManagerInterface, PHPManagerInterface {
|
||||
|
||||
public function __construct(HttpClient $httpClient) {
|
||||
parent::__construct($httpClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all directories in which a specific version of PHP has been activated.
|
||||
*
|
||||
* @param string $domain
|
||||
* @return array Array of PHPDirectory instances.
|
||||
*/
|
||||
public function fetchPHPDirectories(string $domain) : array {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "list-php-directories");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
$data = $this->httpClient->getResponseMessage()->data;
|
||||
$directories = [];
|
||||
foreach ($data as $directoryInfo) {
|
||||
$directories[] = new PHPDirectory($directoryInfo);
|
||||
}
|
||||
return $directories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all PHP installed versions.
|
||||
*
|
||||
* @return array Array of \stdClass
|
||||
*/
|
||||
public function fetchInstalledPHPVersions() : array {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "list-php-versions");
|
||||
$queryBuilder->addParameter("name-only");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
|
||||
return $this->httpClient->getResponseMessage()->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the version of PHP to run in given directory.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $directory Relative path to website root directory.
|
||||
* @param string $phpVersion
|
||||
* @return bool
|
||||
*/
|
||||
public function addPHPDirectory(string $domain, string $directory, string $phpVersion) : bool {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "set-php-directory");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("dir", $directory);
|
||||
$queryBuilder->addParameter("version", $phpVersion);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove any custom version of PHP for a given directory.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $directory Relative path to website root directory.
|
||||
* @return bool
|
||||
*/
|
||||
public function deletePHPDirectory(string $domain, string $directory) {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "delete-php-directory");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("dir", $directory);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all PHP variables for a given domain.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param string $phpVersion PHP version.
|
||||
* @param array $phpIniVars PHP variable names.
|
||||
* @return array
|
||||
*/
|
||||
public function fetchPHPIniVars(string $domain, string $phpVersion, array $phpIniVars) : array {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "list-php-ini");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("php-version", $phpVersion);
|
||||
foreach ($phpIniVars as $iniVar) {
|
||||
$queryBuilder->addParameter("ini-name", $iniVar);
|
||||
}
|
||||
$this->httpClient->sendRequest();
|
||||
// Parse the ugly response from Virtualmin ...
|
||||
$data = get_object_vars($this->httpClient->getResponseMessage()->data[0]->values);
|
||||
$phpVars = [];
|
||||
foreach ($data as $key => $value) {
|
||||
$phpVars[$key] = $value[0];
|
||||
}
|
||||
return $phpVars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies PHP variables for a given domain.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $phpVersion
|
||||
* @param array $phpIniVars
|
||||
* @return bool
|
||||
*/
|
||||
public function modifyPHPIniVars(string $domain, string $phpVersion, array $phpIniVars) : bool {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "modify-php-ini");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("php-version", $phpVersion);
|
||||
foreach ($phpIniVars as $key => $value) {
|
||||
$queryBuilder->addParameter("ini-name", $key);
|
||||
$queryBuilder->addParameter("ini-value", $value);
|
||||
}
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
}
|
66
src/Managers/PHP/PHPManagerInterface.php
Normal file
66
src/Managers/PHP/PHPManagerInterface.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\PHP;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
interface PHPManagerInterface {
|
||||
/**
|
||||
* Retrieves all directories in which a specific version of PHP has been activated.
|
||||
*
|
||||
* @param string $domain
|
||||
*
|
||||
* @return array Array of PHPDirectory instances.
|
||||
*/
|
||||
public function fetchPHPDirectories(string $domain) : array;
|
||||
|
||||
/**
|
||||
* Retrieves all PHP installed versions.
|
||||
*
|
||||
* @return array Array of \stdClass
|
||||
*/
|
||||
public function fetchInstalledPHPVersions() : array;
|
||||
|
||||
/**
|
||||
* Sets the version of PHP to run in given directory.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $directory Relative path to website root directory.
|
||||
* @param string $phpVersion
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function addPHPDirectory(string $domain, string $directory, string $phpVersion) : bool;
|
||||
|
||||
/**
|
||||
* Remove any custom version of PHP for a given directory.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $directory Relative path to website root directory.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deletePHPDirectory(string $domain, string $directory);
|
||||
|
||||
/**
|
||||
* Retrieves all PHP variables for a given domain.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param string $phpVersion PHP version.
|
||||
* @param array $phpIniVars PHP variable names.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fetchPHPIniVars(string $domain, string $phpVersion, array $phpIniVars) : array;
|
||||
|
||||
/**
|
||||
* Modifies PHP variables for a given domain.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $phpVersion
|
||||
* @param array $phpIniVars
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function modifyPHPIniVars(string $domain, string $phpVersion, array $phpIniVars) : bool;
|
||||
}
|
157
src/Managers/SSL/CSRInfo.php
Normal file
157
src/Managers/SSL/CSRInfo.php
Normal file
@ -0,0 +1,157 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\SSL;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class CSRInfo {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $organization;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $organizationUnit;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $countryCode;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $city;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $email;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $domainName;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOrganization(): string {
|
||||
return $this->organization;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $organization
|
||||
* @return $this
|
||||
*/
|
||||
public function setOrganization(string $organization) {
|
||||
$this->organization = $organization;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOrganizationUnit(): string {
|
||||
return $this->organizationUnit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $organizationUnit
|
||||
* @return $this
|
||||
*/
|
||||
public function setOrganizationUnit(string $organizationUnit) {
|
||||
$this->organizationUnit = $organizationUnit;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCountryCode(): string {
|
||||
return $this->countryCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $countryCode
|
||||
* @return $this
|
||||
*/
|
||||
public function setCountryCode(string $countryCode) {
|
||||
$this->countryCode = $countryCode;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getState(): string {
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $state
|
||||
* @return $this
|
||||
*/
|
||||
public function setState(string $state) {
|
||||
$this->state = $state;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCity(): string {
|
||||
return $this->city;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $city
|
||||
* @return $this
|
||||
*/
|
||||
public function setCity(string $city) {
|
||||
$this->city = $city;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail(): string {
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @return $this
|
||||
*/
|
||||
public function setEmail(string $email) {
|
||||
$this->email = $email;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDomainName(): string {
|
||||
return $this->domainName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $domainName
|
||||
* @return $this
|
||||
*/
|
||||
public function setDomainName(string $domainName) {
|
||||
$this->domainName = $domainName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
79
src/Managers/SSL/SSLManager.php
Normal file
79
src/Managers/SSL/SSLManager.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\SSL;
|
||||
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
use Nilemin\Manager\BaseManager;
|
||||
use Nilet\Components\FileSystem\IFile;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class SSLManager extends BaseManager implements SSLManagerInterface {
|
||||
|
||||
public function __construct(HttpClient $httpClient) {
|
||||
parent::__construct($httpClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a Certificate Signing Request file and RSA(Rivest-Shamir-Adleman) private key for a given domain.
|
||||
*
|
||||
* @param CSRInfo $csrInfo CSR information - organization, org. unit, country, city etc.
|
||||
* @return string CSR file content.
|
||||
*/
|
||||
public function generateCSR(CSRInfo $csrInfo) {
|
||||
$domain = $csrInfo->getDomainName();
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "generate-cert");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("csr");
|
||||
$queryBuilder->addParameter("o", $csrInfo->getOrganization());
|
||||
$queryBuilder->addParameter("ou", $csrInfo->getOrganizationUnit());
|
||||
$queryBuilder->addParameter("c", $csrInfo->getCountryCode());
|
||||
$queryBuilder->addParameter("st", $csrInfo->getState());
|
||||
$queryBuilder->addParameter("l", $csrInfo->getCity());
|
||||
$queryBuilder->addParameter("email", $csrInfo->getEmail());
|
||||
$queryBuilder->addParameter("cn", $domain);
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
return $this->getCSRContent($domain);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves CSR content of a given domain.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @return string
|
||||
*/
|
||||
public function getCSRContent(string $domain) : string {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "list-certs");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("csr");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
$data = array_slice($this->httpClient->getResponseMessage()->data, 1);
|
||||
$csr = "";
|
||||
foreach ($data as $csrLine) {
|
||||
$csr .= $csrLine->name.PHP_EOL;
|
||||
}
|
||||
return $csr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs a signed SSL certificate fpr a given domain.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param IFile $file
|
||||
* @return bool
|
||||
*/
|
||||
public function installSSL(string $domain, IFile $file) : bool {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "install-cert");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("cert", $file->getRealPath());
|
||||
$queryBuilder->addParameter("use-newkey");
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
}
|
34
src/Managers/SSL/SSLManagerInterface.php
Normal file
34
src/Managers/SSL/SSLManagerInterface.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\SSL;
|
||||
use Nilet\Components\FileSystem\IFile;
|
||||
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
interface SSLManagerInterface {
|
||||
/**
|
||||
* Generates a Certificate Signing Request file and RSA(Rivest-Shamir-Adleman) private key for a given domain.
|
||||
*
|
||||
* @param CSRInfo $csrInfo CSR information - organization, org. unit, country, city etc.
|
||||
* @return string CSR file content.
|
||||
*/
|
||||
public function generateCSR(CSRInfo $csrInfo);
|
||||
|
||||
/**
|
||||
* Retrieves CSR content of a given domain.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @return string
|
||||
*/
|
||||
public function getCSRContent(string $domain) : string;
|
||||
|
||||
/**
|
||||
* Installs a signed SSL certificate fpr a given domain.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param IFile $file
|
||||
* @return bool
|
||||
*/
|
||||
public function installSSL(string $domain, IFile $file) : bool;
|
||||
}
|
122
src/Managers/Scripts/ScriptsManager.php
Normal file
122
src/Managers/Scripts/ScriptsManager.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Scripts;
|
||||
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
use Nilemin\Manager\BaseManager;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class ScriptsManager extends BaseManager implements ScriptsManagerInterface {
|
||||
|
||||
/**
|
||||
* ScriptsManager constructor.
|
||||
*
|
||||
* @param HttpClient $httpClient
|
||||
*/
|
||||
public function __construct(HttpClient $httpClient) {
|
||||
parent::__construct($httpClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs a third party CMS software under a given domain.
|
||||
*
|
||||
* @param string $domain Domain name
|
||||
* @param array $sctiptInfo
|
||||
* "wordpress" => [
|
||||
* // scripts short name
|
||||
* "scriptName" => "wordpress",
|
||||
* "dir" => "wordpress",
|
||||
* "dbName" => "wordpress"
|
||||
* ]
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function installCMS(string $domain, array $sctiptInfo) : bool {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "install-script");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("type", $sctiptInfo["scriptName"]);
|
||||
$queryBuilder->addParameter("version", "latest");
|
||||
$queryBuilder->addParameter("path", "/".trim($sctiptInfo["dir"], "/"));
|
||||
$queryBuilder->addParameter("newdb");
|
||||
$queryBuilder->addParameter("db", "mysql {$sctiptInfo["dbName"]}");
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs PHPMyAdmin.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param array $sctiptInfo
|
||||
* "phpmyadmin" => [
|
||||
* "scriptName" => "phpmyadmin",
|
||||
* "dir" => "rdbms"
|
||||
* ]
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function installPHPMyAdmin(string $domain, array $sctiptInfo) : bool {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "install-script");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("type", $sctiptInfo["scriptName"]);
|
||||
$queryBuilder->addParameter("version", "latest");
|
||||
$queryBuilder->addParameter("path", "/".trim($sctiptInfo["dir"], "/"));
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes/uninstalls a given script.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param string $type Scripts short name.
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function deleteScript(string $domain, string $type) : bool {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "delete-script");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("type", $type);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
public function fetchInstalledScripts(string $domain) {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "list-scripts");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
$data = $this->httpClient->getResponseMessage()->data;
|
||||
$scripts = [];
|
||||
foreach ($data as $item) {
|
||||
$scriptData = get_object_vars($item->values);
|
||||
foreach ($scriptData as $key => $value) {
|
||||
$scriptData[$key] = $value[0];
|
||||
}
|
||||
$scripts[] = $scriptData;
|
||||
}
|
||||
return $scripts;
|
||||
}
|
||||
|
||||
public function fetchInstalledScript(string $domain, string $type) {
|
||||
$queryBuilder = $this->httpClient->queryStringBuilder();
|
||||
$queryBuilder->addParameter("program", "list-scripts");
|
||||
$queryBuilder->addParameter("domain", $domain);
|
||||
$queryBuilder->addParameter("type", $type);
|
||||
$queryBuilder->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
$data = get_object_vars($this->httpClient->getResponseMessage()->data[0]->values);
|
||||
$scriptData = [];
|
||||
foreach ($data as $key => $value) {
|
||||
$scriptData[$key] = $value[0];
|
||||
}
|
||||
return $scriptData;
|
||||
}
|
||||
|
||||
}
|
51
src/Managers/Scripts/ScriptsManagerInterface.php
Normal file
51
src/Managers/Scripts/ScriptsManagerInterface.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Scripts;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
interface ScriptsManagerInterface {
|
||||
/**
|
||||
* Installs a third party CMS software under a given domain.
|
||||
*
|
||||
* @param string $domain Domain name
|
||||
* @param array $sctiptInfo
|
||||
* "wordpress" => [
|
||||
* // scripts short name
|
||||
* "scriptName" => "wordpress",
|
||||
* "dir" => "wordpress",
|
||||
* "dbName" => "wordpress"
|
||||
* ]
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function installCMS(string $domain, array $sctiptInfo) : bool;
|
||||
|
||||
/**
|
||||
* Installs PHPMyAdmin.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param array $sctiptInfo
|
||||
* "phpmyadmin" => [
|
||||
* "scriptName" => "phpmyadmin",
|
||||
* "dir" => "rdbms"
|
||||
* ]
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function installPHPMyAdmin(string $domain, array $sctiptInfo) : bool;
|
||||
|
||||
/**
|
||||
* Deletes/uninstalls a given script.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param string $type Scripts short name.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function deleteScript(string $domain, string $type) : bool;
|
||||
|
||||
public function fetchInstalledScripts(string $domain);
|
||||
|
||||
public function fetchInstalledScript(string $domain, string $type);
|
||||
}
|
12
src/Managers/Server/ServerTypes.php
Normal file
12
src/Managers/Server/ServerTypes.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Server;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class ServerTypes {
|
||||
|
||||
const TOP_LEVEL_SERVER = "topLevelServer";
|
||||
const SUB_SERVER = "subServer";
|
||||
|
||||
}
|
13
src/Managers/Server/UnknownVirtualServerTypeException.php
Normal file
13
src/Managers/Server/UnknownVirtualServerTypeException.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Server;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class UnknownVirtualServerTypeException extends \Exception {
|
||||
|
||||
public function __construct($message) {
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
}
|
395
src/Managers/Server/VirtualServerManager.php
Normal file
395
src/Managers/Server/VirtualServerManager.php
Normal file
@ -0,0 +1,395 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Server;
|
||||
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
use Nilemin\Manager\BaseManager;
|
||||
use Nilemin\Virtualmin\Entities\Server;
|
||||
use Nilemin\Virtualmin\Entities\ServerRedirect;
|
||||
use Nilemin\Virtualmin\Entities\SubServer;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class VirtualServerManager extends BaseManager implements VirtualServerManagerInterface {
|
||||
|
||||
/**
|
||||
* VirtualServerManager constructor.
|
||||
*
|
||||
* @param HttpClient $httpClient
|
||||
*/
|
||||
public function __construct(HttpClient $httpClient) {
|
||||
parent::__construct($httpClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes virtual server (domain) name
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $newDomain
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function changeServerName(string $domain, string $newDomain) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "modify-domain");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("newdomain", $newDomain);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes virtual server admin password.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $pass
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function changeAdminPassword(string $domain, string $pass) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "modify-domain");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("pass", $pass);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new Virtual Sub Server.
|
||||
*
|
||||
* @param string $domain Name of the new sub server.
|
||||
* @param string $parentDomain Name of the parent server.
|
||||
* @param string $description Sub server description.
|
||||
* @param array $options Array containing additional domain options e.g mail, ssl, mysql, spam, virus, status, plan, template etc.
|
||||
* For full list of the available options visit ({@link https://www.virtualmin.com/documentation/developer/cli/create_domain})
|
||||
* Note! "dir" and "web" options are added by default.
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function createSubServer(string $domain, string $parentDomain, string $description, array $options = []) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "create-domain");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("parent", $parentDomain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("dir");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("web");
|
||||
foreach ($options as $key => $value) {
|
||||
if (is_numeric($key)) {
|
||||
$this->httpClient->queryStringBuilder()->addParameter($value);
|
||||
} else {
|
||||
$this->httpClient->queryStringBuilder()->addParameter($key, $value);
|
||||
}
|
||||
}
|
||||
$this->httpClient->queryStringBuilder()->addParameter("desc", $description);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates Virtual Server alias.
|
||||
*
|
||||
* @param string $domain Name of the new alias server.
|
||||
* @param string $alias Name of the target server.
|
||||
* @param string $description Sub server description.
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function createServerAlias(string $domain, string $alias, string $description) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "create-domain");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("alias", $alias);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("web");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("dns");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("desc", $description);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a given sub server.
|
||||
*
|
||||
* @param $domain Domain name.
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function deleteServer($domain) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "delete-domain");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieving single virtual server.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $type ServerTypes::TOP_LEVEL_SERVER, ServerTypes::SUB_SERVER
|
||||
* @return Server|SubServer
|
||||
* @throws UnknownVirtualServerTypeException
|
||||
*/
|
||||
public function fetchServer(string $domain, string $type) {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "list-domains");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
$data = $this->httpClient->getResponseMessage()->data;
|
||||
$dataCount = count($data);
|
||||
if ($dataCount > 1) {
|
||||
$values = (array)$data[0]->values;
|
||||
for($i = 1; $i < $dataCount; $i++) {
|
||||
$values = array_merge($values, (array)$data[$i]->values);
|
||||
}
|
||||
$data[0]->values = (object)$values;
|
||||
}
|
||||
if ($type === ServerTypes::TOP_LEVEL_SERVER) {
|
||||
return new Server($data[0]);
|
||||
} else if ($type === ServerTypes::SUB_SERVER) {
|
||||
return new SubServer($data[0]);
|
||||
} else {
|
||||
throw new UnknownVirtualServerTypeException("Unknown server type : {$type}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (addon domains) owned by a given user.
|
||||
*
|
||||
* @param string $user
|
||||
* @return array Array containing SubServer instances.
|
||||
*/
|
||||
public function fetchAddonServers(string $user) : array {
|
||||
return $this->filterAddonServers($this->fetchUserSubServers($user, "subserver"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (sub domains) owned by a given user.
|
||||
* They should not be mistaken with entries created with the sub domain option in Virtualmin.
|
||||
* These are just ordinary sub servers with prefixed name
|
||||
* that is the same with the accounts primary domain name.
|
||||
*
|
||||
* @param string $user
|
||||
* @return array Array containing SubServer instances.
|
||||
*/
|
||||
public function fetchSubServers(string $user) : array {
|
||||
return $this->filterSubServers($this->fetchUserSubServers($user, "subserver"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (sub domains) names owned by a given user.
|
||||
* They should not be mistaken with entries created with the sub domain option in Virtualmin.
|
||||
* These are just ordinary sub servers with prefixed name
|
||||
* that is the same with the accounts primary domain name.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $user
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fetchSubServersNames(string $domain, string $user) {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "list-domains");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("user", $user);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("name-only");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("subserver");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("no-alias");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("no-reseller");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
$data = $this->httpClient->getResponseMessage()->data;
|
||||
return $this->filterSubServersNames($domain, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (addon domains) names owned by a given user.
|
||||
* They should not be mistaken with entries created with the sub domain option in Virtualmin.
|
||||
* These are just ordinary sub servers with prefixed name
|
||||
* that is the same with the accounts primary domain name.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $user
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAddonServersNames(string $domain, string $user) {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "list-domains");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("user", $user);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("name-only");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("subserver");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("no-alias");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("no-reseller");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
$data = $this->httpClient->getResponseMessage()->data;
|
||||
return $this->filterAddonServersNames($domain, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (alias domains) owned by a given user.
|
||||
*
|
||||
* @param string $user
|
||||
* @return array Array containing SubServer instances.
|
||||
*/
|
||||
public function fetchAliasServers(string $user) : array {
|
||||
$servers = [];
|
||||
$result = $this->fetchUserSubServers($user, "alias");
|
||||
foreach ($result as $server) {
|
||||
$servers[] = new SubServer($server);
|
||||
}
|
||||
return $servers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a Virtual server redirect records.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param string $path Domain path to redirect from.
|
||||
* @param string $destination Redirect's destination.
|
||||
* @return bool
|
||||
*/
|
||||
public function addServerRedirect(string $domain, string $path, string $destination) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "create-redirect");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("path", $path);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("redirect", $destination);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a Virtual server redirect record.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param $path $redirect Redirect path.
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function deleteServerRedirect(string $domain, string $path) : bool {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "delete-redirect");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("path", $path);
|
||||
|
||||
return $this->httpClient->sendRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all redirects belonging to a given domain.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @return array Array containing ServerRedirect instances.
|
||||
*/
|
||||
public function fetchServerRedirects(string $domain) : array {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "list-redirects");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("domain", $domain);
|
||||
$this->httpClient->queryStringBuilder()->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
$redirects = [];
|
||||
$data = $this->httpClient->getResponseMessage()->data;
|
||||
foreach ($data as $redirect) {
|
||||
$redirects[] = new ServerRedirect($redirect);
|
||||
}
|
||||
return $redirects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (sub domains, addon domains or alias) owned by a given user.
|
||||
*
|
||||
* @param string $user User. The name of the user who owns the sub servers.
|
||||
* @param string $type ServerTypes::TOP_LEVEL_SERVER or ServerTypes::SUB_SERVER
|
||||
* @return array Array containing \stdClass instances.
|
||||
*/
|
||||
private function fetchUserSubServers(string $user, string $type) : array {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("program", "list-domains");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("user", $user);
|
||||
$this->httpClient->queryStringBuilder()->addParameter($type);
|
||||
if ($type !== "alias") {
|
||||
$this->httpClient->queryStringBuilder()->addParameter("no-alias");
|
||||
}
|
||||
$this->httpClient->queryStringBuilder()->addParameter("no-reseller");
|
||||
$this->httpClient->queryStringBuilder()->addParameter("multiline");
|
||||
|
||||
$this->httpClient->sendRequest();
|
||||
return $this->httpClient->getResponseMessage()->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all addon domains from a virtual servers array;
|
||||
*
|
||||
* @param array $subServers Addon domains.
|
||||
*
|
||||
* @return array Array containing SubServer instances.
|
||||
*/
|
||||
private function filterAddonServers(array $subServers) : array {
|
||||
$servers = [];
|
||||
foreach ($subServers as $subServer) {
|
||||
if ($this->isSubServer($subServer) && !$this->isSubDomain($subServer)) {
|
||||
$servers[] = new SubServer($subServer);
|
||||
}
|
||||
}
|
||||
return $servers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all sub domains from the virtual servers array;
|
||||
*
|
||||
* @param array $subServers Sub domains.
|
||||
*
|
||||
* @return array Array containing SubServer instances.
|
||||
*/
|
||||
private function filterSubServers(array $subServers) : array {
|
||||
$servers = [];
|
||||
foreach ($subServers as $subServer) {
|
||||
if ($this->isSubServer($subServer) && $this->isSubDomain($subServer)) {
|
||||
$servers[] = new SubServer($subServer);
|
||||
}
|
||||
}
|
||||
return $servers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all sub domains names.
|
||||
*
|
||||
* @param string $domain Top level server (domain) name.
|
||||
* @param array $subServers Sub domains names.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function filterSubServersNames(string $domain, array $subServers) : array {
|
||||
$serverNames = [];
|
||||
foreach ($subServers as $subServer) {
|
||||
if (strpos($subServer->name, $domain) !== false)
|
||||
$serverNames[] = $subServer->name;
|
||||
}
|
||||
return $serverNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all addon domains names.
|
||||
*
|
||||
* @param string $domain Top level server (domain) name.
|
||||
* @param array $subServers Sub domains names.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function filterAddonServersNames(string $domain, array $subServers) : array {
|
||||
$serverNames = [];
|
||||
foreach ($subServers as $subServer) {
|
||||
if (strpos($subServer->name, $domain) === false)
|
||||
$serverNames[] = $subServer->name;
|
||||
}
|
||||
return $serverNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a given sub server is "sub domain".
|
||||
*
|
||||
* @param \stdClass $subServer
|
||||
*
|
||||
* @return bool TRUE if the sub server is "sub domain".
|
||||
*/
|
||||
private function isSubDomain(\stdClass $subServer) : bool {
|
||||
return strpos($subServer->name, $subServer->values->parent_domain[0]) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a given server entry is a sub server.
|
||||
* Virtualmi's list-domains command used with subserver parameter returns an associative array containing all sub servers and their ssl information
|
||||
* as a sub server \stdClass entry. We need to filter these out by "type" checking each entry.
|
||||
*
|
||||
* @param \stdClass $subServer
|
||||
* @return bool
|
||||
*/
|
||||
private function isSubServer(\stdClass $subServer) : bool {
|
||||
return ($subServer->values->type[0] === "Sub-server");
|
||||
}
|
||||
}
|
148
src/Managers/Server/VirtualServerManagerInterface.php
Normal file
148
src/Managers/Server/VirtualServerManagerInterface.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin\Managers\Server;
|
||||
|
||||
use Nilemin\Virtualmin\Entities\Server;
|
||||
use Nilemin\Virtualmin\Entities\SubServer;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
interface VirtualServerManagerInterface {
|
||||
/**
|
||||
* Changes virtual server (domain) name
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $newDomain
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function changeServerName(string $domain, string $newDomain) : bool;
|
||||
|
||||
/**
|
||||
* Changes virtual server admin password.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $pass
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function changeAdminPassword(string $domain, string $pass) : bool;
|
||||
|
||||
/**
|
||||
* Creates new Virtual Sub Server.
|
||||
*
|
||||
* @param string $domain Name of the new sub server.
|
||||
* @param string $parentDomain Name of the parent server.
|
||||
* @param string $description Sub server description.
|
||||
* @param array $options Array containing additional domain options e.g mail, ssl, mysql, spam, virus, status, plan, template etc.
|
||||
* For full list of the available options visit ({@link https://www.virtualmin.com/documentation/developer/cli/create_domain})
|
||||
* Note! "dir" and "web" options are added by default.
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function createSubServer(string $domain, string $parentDomain, string $description, array $options = []) : bool;
|
||||
|
||||
/**
|
||||
* Creates Virtual Server alias.
|
||||
*
|
||||
* @param string $domain Name of the new alias server.
|
||||
* @param string $alias Name of the target server.
|
||||
* @param string $description Sub server description.
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function createServerAlias(string $domain, string $alias, string $description) : bool;
|
||||
|
||||
/**
|
||||
* Deletes a given sub server.
|
||||
*
|
||||
* @param $domain Domain name.
|
||||
* @return bool TRUE on success, FALSE otherwise
|
||||
*/
|
||||
public function deleteServer($domain) : bool;
|
||||
|
||||
/**
|
||||
* Retrieving single virtual server.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $type ServerTypes::TOP_LEVEL_SERVER or ServerTypes::SUB_SERVER
|
||||
* @return Server|SubServer
|
||||
* @throws UnknownVirtualServerTypeException
|
||||
*/
|
||||
public function fetchServer(string $domain, string $type);
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (addon domains) owned by a given user.
|
||||
*
|
||||
* @param string $user
|
||||
* @return array Array containing SubServer instances.
|
||||
*/
|
||||
public function fetchAddonServers(string $user) : array;
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (sub domains) owned by a given user.
|
||||
* They should not be mistaken with entries created with the sub domain option in Virtualmin.
|
||||
* These are just ordinary sub servers with prefixed name
|
||||
* that is the same with the accounts primary domain name.
|
||||
*
|
||||
* @param string $user
|
||||
* @return array Array containing SubServer instances.
|
||||
*/
|
||||
public function fetchSubServers(string $user) : array;
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (sub domains) names owned by a given user.
|
||||
* They should not be mistaken with entries created with the sub domain option in Virtualmin.
|
||||
* These are just ordinary sub servers with prefixed name
|
||||
* that is the same with the accounts primary domain name.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $user
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fetchSubServersNames(string $domain, string $user);
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (addon domains) names owned by a given user.
|
||||
* They should not be mistaken with entries created with the sub domain option in Virtualmin.
|
||||
* These are just ordinary sub servers with prefixed name
|
||||
* that is the same with the accounts primary domain name.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $user
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAddonServersNames(string $domain, string $user);
|
||||
|
||||
/**
|
||||
* Retrieves all sub servers (alias domains) owned by a given user.
|
||||
*
|
||||
* @param string $user
|
||||
* @return array Array containing SubServer instances.
|
||||
*/
|
||||
public function fetchAliasServers(string $user) : array;
|
||||
|
||||
/**
|
||||
* Adds a Virtual server redirect records.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param string $path Domain path to redirect from.
|
||||
* @param string $destination Redirect's destination.
|
||||
* @return bool
|
||||
*/
|
||||
public function addServerRedirect(string $domain, string $path, string $destination) : bool;
|
||||
|
||||
/**
|
||||
* Deletes a Virtual server redirect record.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @param $path $redirect Redirect path.
|
||||
* @return bool TRUE on success, FALSE otherwise.
|
||||
*/
|
||||
public function deleteServerRedirect(string $domain, string $path) : bool;
|
||||
|
||||
/**
|
||||
* Retrieves all redirects belonging to a given domain.
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
* @return array Array containing ServerRedirect instances.
|
||||
*/
|
||||
public function fetchServerRedirects(string $domain) : array;
|
||||
}
|
135
src/Virtualmin.php
Normal file
135
src/Virtualmin.php
Normal file
@ -0,0 +1,135 @@
|
||||
<?php
|
||||
namespace Nilemin\Virtualmin;
|
||||
|
||||
use Nilemin\Virtualmin\Accounts\Email\EmailAccountManager;
|
||||
use Nilemin\Virtualmin\Managers\Account\Email\EmailAccountBaseManager;
|
||||
use Nilemin\Virtualmin\Managers\Account\Ftp\FtpManager;
|
||||
use Nilemin\Virtualmin\Managers\Database\DatabaseManager;
|
||||
use Nilemin\Virtualmin\Managers\PHP\PHPManager;
|
||||
use Nilemin\Virtualmin\Managers\Server\VirtualServerManager;
|
||||
use Nilemin\Virtualmin\Managers\DNS\DNSManager;
|
||||
use Nilemin\Virtualmin\Managers\SSL\SSLManager;
|
||||
use Nilemin\Virtualmin\Managers\Scripts\ScriptsManager;
|
||||
use Nilemin\Virtualmin\Managers\Cron\CronManager;
|
||||
use Nilemin\Virtualmin\Http\HttpClient;
|
||||
use Nilet\Components\Configuration\Config;
|
||||
use Nilet\Components\Container\DependencyContainer;
|
||||
|
||||
/**
|
||||
* @author Tsvetelin Tsonev <github.tsonev@yahoo.com>
|
||||
*/
|
||||
class Virtualmin {
|
||||
|
||||
/**
|
||||
* Http client.
|
||||
* @var HttpClient
|
||||
*/
|
||||
private $httpClient = null;
|
||||
|
||||
private $dc = null;
|
||||
|
||||
/**
|
||||
* VirtualminApi new instance.
|
||||
*
|
||||
* @param string $url Virualmins URL.
|
||||
* @param int $port Virualmins port.
|
||||
* @param string $rootName Root user name.
|
||||
* @param string $rootPassword Root user password.
|
||||
* @param Config $config
|
||||
*/
|
||||
public function __construct(string $url, int $port, string $rootName, string $rootPassword, Config $config) {
|
||||
$this->httpClient = new HttpClient($url, $port, $rootName, $rootPassword, "json");
|
||||
$this->dc = new DependencyContainer();
|
||||
$this->dc->instance(Config::class, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the email account manager.
|
||||
*
|
||||
* @return EmailAccountBaseManager
|
||||
*/
|
||||
public function createEmailManager() : EmailManager {
|
||||
return $this->dc->create(EmailManager::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the ftp account manager.
|
||||
*
|
||||
* @return FtpManager
|
||||
*/
|
||||
public function createFtpManager() : FtpManager {
|
||||
return $this->dc->create(FtpManager::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the database manager.
|
||||
*
|
||||
* @return DatabaseManager
|
||||
*/
|
||||
public function createDatabaseManager() : DatabaseManager {
|
||||
return $this->dc->create(DatabaseManager::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the virtual server manager.
|
||||
*
|
||||
* @return VirtualServerManager
|
||||
*/
|
||||
public function createVirtualServerManager() : VirtualServerManager {
|
||||
return $this->dc->create(VirtualServerManager::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the domain manager.
|
||||
*
|
||||
* @return DNSManager
|
||||
*/
|
||||
public function createDnsManager() : DNSManager {
|
||||
return $this->dc->create(DNSManager::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the SSL manager.
|
||||
*
|
||||
* @return SSLManager
|
||||
*/
|
||||
public function createSslManager(): SSLManager {
|
||||
return $this->dc->create(SSLManager::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the PHP manager.
|
||||
*
|
||||
* @return PHPManager
|
||||
*/
|
||||
public function createPhpManager(): PHPManager {
|
||||
return $this->dc->create(PHPManager::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the scripts manager.
|
||||
*
|
||||
* @return ScriptsManager
|
||||
*/
|
||||
public function createScriptsManager(): ScriptsManager {
|
||||
return $this->dc->create(ScriptsManager::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the cron manager.
|
||||
*
|
||||
* @return ScriptsManager
|
||||
*/
|
||||
public function createCronManager(): ScriptsManager {
|
||||
return $this->dc->create(CronManager::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves Virtualmin's response message.
|
||||
*
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function getResponseMessage() : \stdClass {
|
||||
return $this->httpClient->getResponseMessage();
|
||||
}
|
||||
}
|
49
src/config/config.php
Normal file
49
src/config/config.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
return [
|
||||
/*
|
||||
***********************************************************************************
|
||||
* DNS configuration
|
||||
***********************************************************************************
|
||||
|
|
||||
*/
|
||||
"defaultTtl" => "6h",
|
||||
|
||||
/*
|
||||
***********************************************************************************
|
||||
* PHP configuration
|
||||
***********************************************************************************
|
||||
|
|
||||
*/
|
||||
"userModifiableIniVars" => [
|
||||
"max_execution_time",
|
||||
"display_errors"
|
||||
],
|
||||
|
||||
/*
|
||||
***********************************************************************************
|
||||
* Script installers configuration
|
||||
***********************************************************************************
|
||||
|
|
||||
*/
|
||||
"scripts" => [
|
||||
"supportedScripts" => [
|
||||
"wordpress",
|
||||
"phpmyadmin"
|
||||
],
|
||||
"cms" => [
|
||||
"wordpress" => [
|
||||
// scripts short name
|
||||
"scriptName" => "wordpress",
|
||||
"dir" => "wordpress",
|
||||
"dbName" => "wordpress"
|
||||
]
|
||||
],
|
||||
"dbPanels" => [
|
||||
"phpmyadmin" => [
|
||||
"scriptName" => "phpmyadmin",
|
||||
"dir" => "rdbms"
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
Loading…
Reference in New Issue
Block a user