#1717 Add "Port" option on add hosting server page (#1725)

* Add "Port" number on add hosting server page

* Set default value to ""

* Allow Directadmin to use configured port instead of 2222
This commit is contained in:
Jaap Marcus authored and GitHub committed 2023-10-16 00:36:23 -07:00
1 parent d7264541bd
commit c9685e6412
2 files changed
+71 -60

No files matched your search

+63 -58
View File
@@ -15,16 +15,16 @@ class Server_Manager_Directadmin extends Server_Manager
if(empty($this->_config['host'])) {
throw new Server_Exception('The ":server_manager" server manager is not fully configured. Please configure the :missing', [':server_manager' => 'DirectAdmin', ':missing' => 'hostname']);
}
if(empty($this->_config['username'])) {
throw new Server_Exception('The ":server_manager" server manager is not fully configured. Please configure the :missing', [':server_manager' => 'DirectAdmin', ':missing' => 'username']);
}
if(empty($this->_config['password'])) {
throw new Server_Exception('The ":server_manager" server manager is not fully configured. Please configure the :missing', [':server_manager' => 'DirectAdmin', ':missing' => 'password']);
}
}
public static function getForm()
{
return array(
@@ -51,13 +51,18 @@ class Server_Manager_Directadmin extends Server_Manager
]
);
}
public function _getPort()
{
return is_numeric($this->_config['port']) ? $this->_config['port'] : '2222';
}
public function getLoginUrl() : string
{
$protocol = $this->_config['secure'] ? 'https://' : 'http://';
return $protocol . $this->_config['host'] . ':2222';
return $protocol . $this->_config['host'] . ':'. $this -> _getPort();
}
public function getResellerLoginUrl()
{
return $this->getLoginUrl();
@@ -76,18 +81,18 @@ class Server_Manager_Directadmin extends Server_Manager
$randnum = random_int(0, 99);
return $username . $randnum;
}
public function testConnection()
{
$results = $this->_request('CMD_API_SHOW_RESELLER_IPS');
return is_array($results);
}
public function synchronizeAccount(Server_Account $a)
{
return $a;
}
public function createAccount(Server_Account $a)
{
$ips = $this->getIps();
@@ -95,10 +100,10 @@ class Server_Manager_Directadmin extends Server_Manager
throw new Server_Exception('There are no available IPs on this server.');
}
$ip = $ips[array_rand($ips)];
$package = $a->getPackage();
$client = $a->getClient();
$fields = array();
$fields['action'] = 'create';
$fields['add'] = 'Submit';
@@ -107,14 +112,14 @@ class Server_Manager_Directadmin extends Server_Manager
$fields['passwd'] = $a->getPassword();
$fields['passwd2'] = $a->getPassword();
$fields['domain'] = $a->getDomain();
// do not create new package
// $packages = $this->getPackages();
// $fields['package'] = $package->getName();
$fields['ip'] = $ip;
$fields['notify'] = 'no';
$fields['bandwidth'] = $package->getBandwidth(); //Amount of bandwidth User will be allowed to use. Number, in Megabytes
if($package->getBandwidth() == 'unlimited') {
$fields['ubandwidth'] = 'ON'; //ON or OFF. If ON, bandwidth is ignored and no limit is set
@@ -159,7 +164,7 @@ class Server_Manager_Directadmin extends Server_Manager
if($fields['nemailr'] == 'unlimited') {
$fields['unemailr'] = 'ON'; //ON or OFF Unlimited option for nemailr
}
$fields['aftp'] = $package->getCustomValue('aftp') ? 'ON' : 'OFF'; //ON or OFF If ON, the User will be able to have anonymous ftp accounts.
$fields['cgi'] = $package->getCustomValue('cgi') ? 'ON' : 'OFF'; //ON or OFF If ON, the User will have the ability to run cgi scripts in their cgi-bin.
$fields['php'] = $package->getCustomValue('php') ? 'ON' : 'OFF'; //ON or OFF If ON, the User will have the ability to run php scripts.
@@ -173,14 +178,14 @@ class Server_Manager_Directadmin extends Server_Manager
$fields['dnscontrol'] = $package->getCustomValue('dnscontrol') ? 'ON' : 'OFF'; //ON or OFF If ON, the User will be able to modify his/her dns records.
$fields['suspend_at_limit'] = $package->getCustomValue('suspend_at_limit') ? 'ON' : 'OFF'; //ON or OFF If ON, the User will be suspended if their User bandwidth limit is exceeded.
$command = 'CMD_API_ACCOUNT_USER';
if($a->getReseller()) {
$command = 'CMD_ACCOUNT_RESELLER';
$fields['ips'] = 1; //Number of ips that will be allocated to the Reseller upon account during account
$fields['ip'] = 'assign';
}
try {
$results = $this->_request($command, $fields);
}
@@ -191,41 +196,41 @@ class Server_Manager_Directadmin extends Server_Manager
throw new Server_Exception($e->getMessage());
}
}
if(str_contains(implode('', $results), 'Unable to assign the Reseller ANY ips')) {
throw new Server_Exception('Unable to assign the Reseller ANY ips. Make sure to have free, un-assigned ips.');
}
if(str_contains(implode('', $results), 'Error Creating User')) {
throw new Server_Exception('Error creating user');
}
return true;
}
public function suspendAccount(Server_Account $a)
{
$info = $this->getAccountInfo($a);
if($info['suspended'] == 'yes') {
return true;
}
$fields = array();
$fields['location'] = 'CMD_USER_SHOW';
$fields['suspend'] = 'Suspend';
$fields['select0'] = $a->getUsername();
$result = $this->_request('CMD_API_SELECT_USERS', $fields);
return true;
}
public function unsuspendAccount(Server_Account $a)
{
$info = $this->getAccountInfo($a);
if($info['suspended'] == 'no') {
throw new Server_Exception('Server Manager DirectAdmin Error: "Account is not suspended"');
}
$fields = array();
$fields['location'] = 'CMD_USER_SHOW';
$fields['suspend'] = 'Unsuspend';
@@ -233,7 +238,7 @@ class Server_Manager_Directadmin extends Server_Manager
$this->_request('CMD_API_SELECT_USERS', $fields);
return true;
}
public function cancelAccount(Server_Account $a)
{
$fields = array();
@@ -243,15 +248,15 @@ class Server_Manager_Directadmin extends Server_Manager
$this->_request('CMD_API_SELECT_USERS', $fields);
return true;
}
public function modifyAccount(Server_Account $a)
{
$package = $a->getPackage();
$fields = array();
$fields['action'] = 'customize';
$fields['user'] = $a->getUsername();
$fields['bandwidth'] = $package->getBandwidth(); //Amount of bandwidth User will be allowed to use. Number, in Megabytes
if($package->getBandwidth() == 'unlimited') {
$fields['ubandwidth'] = 'ON'; //ON or OFF. If ON, bandwidth is ignored and no limit is set
@@ -306,14 +311,14 @@ class Server_Manager_Directadmin extends Server_Manager
$fields['ssh'] = $package->getHasShell() ? 'ON' : 'OFF'; //ON or OFF If ON, the User will have an ssh account.
$fields['sysinfo'] = 'ON'; //ON or OFF If ON, the User will have access to a page that shows the system information.
$fields['dnscontrol'] = 'ON'; //ON or OFF If ON, the User will be able to modify his/her dns records.
$fields['ns1'] = $a->getNs1();
$fields['ns2'] = $a->getNs2();
$this->_request('CMD_API_MODIFY_USER', $fields);
return true;
}
public function changeAccountPassword(Server_Account $a, $newPassword)
{
$fields = array();
@@ -323,22 +328,22 @@ class Server_Manager_Directadmin extends Server_Manager
$this->_request('CMD_API_USER_PASSWD', $fields);
return true;
}
public function changeAccountUsername(Server_Account $a, $new)
{
throw new Server_Exception(':type: does not support :action:', [':type:' => 'DirectAdmin', ':action:' => __trans('username changes')]);
}
public function changeAccountDomain(Server_Account $a, $new)
{
throw new Server_Exception(':type: does not support :action:', [':type:' => 'DirectAdmin', ':action:' => __trans('changing the account domain')]);
}
public function changeAccountIp(Server_Account $a, $new)
{
throw new Server_Exception(':type: does not support :action:', [':type:' => 'DirectAdmin', ':action:' => __trans('changing the account IP')]);
}
public function changeAccountPackage(Server_Account $a, Server_Package $p)
{
$fields = array();
@@ -348,7 +353,7 @@ class Server_Manager_Directadmin extends Server_Manager
$this->_request('CMD_API_MODIFY_USER', $fields);
return true;
}
private function getAccountInfo(Server_Account $a)
{
$fields = array();
@@ -357,7 +362,7 @@ class Server_Manager_Directadmin extends Server_Manager
$fields['user'] = $a->getUsername();
return $this->_request('CMD_API_SHOW_USER_CONFIG', $fields);
}
private function getUserInfo(Server_Account $a)
{
$fields = array();
@@ -365,28 +370,28 @@ class Server_Manager_Directadmin extends Server_Manager
$result = $this->_request('CMD_API_SHOW_USER_CONFIG', $fields);
return;
}
private function checkAuth()
{
$r = $this->_request('CMD_API_VERIFY_PASSWORD', array(
'user' => $this->_config['username'],
'passwd' => $this->_config['password']
));
return true;
}
private function getPackages()
{
return $this->_request('CMD_API_PACKAGES_USER');
}
private function getIps()
{
$results = $this->_request('CMD_API_SHOW_RESELLER_IPS');
return $results['list'] ?? array();
}
/**
* @param string $command
*/
@@ -394,17 +399,17 @@ class Server_Manager_Directadmin extends Server_Manager
{
$host = $this->_config['host'];
$protocol = $this->_config['secure'] ? 'https://' : 'http://';
$fieldstring = http_build_query($fields);
$httpClient = $this->getHttpClient()->withOptions([
'verify_peer' => false,
'verify_host' => false,
'timeout' => 60,
'auth_basic' => [ $this->_config['username'], $this->_config['password'] ],
'auth_basic' => [ $this->_config['username'], $this->_config['password'] ],
]);
$url = $protocol . $host . ':2222/' . $command . '?' . $fieldstring;
$url = $protocol . $host . ':'. $this -> _getPort().'/' . $command . '?' . $fieldstring;
$this->getLog()->debug($url);
try {
@@ -423,38 +428,38 @@ class Server_Manager_Directadmin extends Server_Manager
$this->getLog()->err($e);
throw $e;
}
if(strlen(strstr($data, 'DirectAdmin Login')) > 0) {
throw new Server_Exception('Failed to connect to the :type: server. Please verify your credentials and configuration', [':type:' => 'DirectAdmin']);
}
if(strlen(strstr($data, "The request you've made cannot be executed because it does not exist in your authority level")) > 0) {
throw new Server_Exception('Server Manager DirectAdmin Error: "The request you have made cannot be executed because it does not exist in your authority level"');
}
$r = $this->_parseResponse($data);
if(isset($r['error']) && $r['error'] == 1) {
$placeholders = ['action' => $command, 'type' => 'DirectAdmin'];
throw new Server_Exception('Failed to :action: on the :type: server, check the error logs for further details', $placeholders);
}
$response = empty($r) ? array() : $r;
return $response;
}
private function _parseResponse($data)
{
$this->getLog()->debug('Raw Response: ' . $data);
// add more replacers if needed
$data = str_replace('&#39', '"', $data);
$data = preg_replace('|(\&\#\d+)|', '$1;', $data);
$data = html_entity_decode($data);
parse_str($data, $r);
$this->getLog()->debug('Parsed Response: ' . print_r($r, 1));
return $r;
}
@@ -213,6 +213,12 @@
</div>
</div>
<div id="credentials"></div>
<div class="mb-3 row">
<label class="form-label col-3 col-form-label">{{ 'Connection port'|trans }}:</label>
<div class="col">
<input class="form-control" type="text" name="port" value="" placeholder="{{ 'Custom port. Use blank to use default. Used to connect to the API'|trans }}">
</div>
</div>
<div class="mb-3 row">
<label class="form-label col-3 col-form-label">{{ 'Use Secure connection'|trans }}:</label>
<div class="col">
@@ -306,7 +312,7 @@
} else {
var data = defaults.form.credentials.fields;
}
for (var i = 0; i < data.length; i++) {
// Create a new form field and append it to the credentials element.
var field = data[i];
@@ -338,4 +344,4 @@
handleManagerChange(document.querySelector('select[name="manager"]'));
};
</script>
{% endblock %}
{% endblock %}