2014-09-02 15:39:21 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-09-02 15:39:21 +02:00
|
|
|
|
2014-11-14 10:57:14 +01:00
|
|
|
namespace Icinga\Forms\Config\Resource;
|
2014-09-02 15:39:21 +02:00
|
|
|
|
2014-09-29 14:46:27 +02:00
|
|
|
use Icinga\Application\Platform;
|
2015-09-28 16:30:48 +02:00
|
|
|
use Icinga\Web\Form;
|
2014-09-02 15:39:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form class for adding/modifying database resources
|
|
|
|
*/
|
|
|
|
class DbResourceForm extends Form
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Initialize this form
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->setName('form_config_resource_db');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-24 14:24:11 +02:00
|
|
|
* Create and add elements to this form
|
|
|
|
*
|
|
|
|
* @param array $formData The data sent by the user
|
2014-09-02 15:39:21 +02:00
|
|
|
*/
|
|
|
|
public function createElements(array $formData)
|
|
|
|
{
|
2014-09-29 14:46:27 +02:00
|
|
|
$dbChoices = array();
|
2014-12-01 15:38:10 +01:00
|
|
|
if (Platform::hasMysqlSupport()) {
|
2014-09-29 14:46:27 +02:00
|
|
|
$dbChoices['mysql'] = 'MySQL';
|
|
|
|
}
|
2015-09-17 12:46:49 +02:00
|
|
|
if (Platform::hasPostgresqlSupport()) {
|
|
|
|
$dbChoices['pgsql'] = 'PostgreSQL';
|
|
|
|
}
|
2015-09-07 17:23:12 +02:00
|
|
|
if (Platform::hasMssqlSupport()) {
|
|
|
|
$dbChoices['mssql'] = 'MSSQL';
|
|
|
|
}
|
2017-08-29 21:18:31 +02:00
|
|
|
if (Platform::hasIbmSupport()) {
|
|
|
|
$dbChoices['ibm'] = 'IBM (DB2)';
|
|
|
|
}
|
2015-09-07 16:44:35 +02:00
|
|
|
if (Platform::hasOracleSupport()) {
|
|
|
|
$dbChoices['oracle'] = 'Oracle';
|
|
|
|
}
|
2015-09-17 12:46:49 +02:00
|
|
|
if (Platform::hasOciSupport()) {
|
|
|
|
$dbChoices['oci'] = 'Oracle (OCI8)';
|
|
|
|
}
|
2018-05-23 19:50:31 +02:00
|
|
|
if (Platform::hasSqliteSupport()) {
|
|
|
|
$dbChoices['sqlite'] = 'SQLite';
|
|
|
|
}
|
2016-05-27 15:45:22 +02:00
|
|
|
|
2015-09-17 12:46:49 +02:00
|
|
|
$offerPostgres = false;
|
2015-09-28 16:29:01 +02:00
|
|
|
$offerMysql = false;
|
2016-12-08 17:30:16 +01:00
|
|
|
$dbChoice = isset($formData['db']) ? $formData['db'] : key($dbChoices);
|
|
|
|
if ($dbChoice === 'pgsql') {
|
|
|
|
$offerPostgres = true;
|
|
|
|
} elseif ($dbChoice === 'mysql') {
|
|
|
|
$offerMysql = true;
|
2015-09-28 16:29:01 +02:00
|
|
|
}
|
2016-05-27 15:45:22 +02:00
|
|
|
|
2018-12-04 15:50:53 +01:00
|
|
|
if ($dbChoice === 'oracle') {
|
|
|
|
$hostIsRequired = false;
|
|
|
|
} else {
|
|
|
|
$hostIsRequired = true;
|
|
|
|
}
|
|
|
|
|
2015-09-28 16:29:01 +02:00
|
|
|
$socketInfo = '';
|
|
|
|
if ($offerPostgres) {
|
|
|
|
$socketInfo = $this->translate(
|
|
|
|
'For using unix domain sockets, specify the path to the unix domain socket directory'
|
|
|
|
);
|
|
|
|
} elseif ($offerMysql) {
|
|
|
|
$socketInfo = $this->translate(
|
|
|
|
'For using unix domain sockets, specify localhost'
|
|
|
|
);
|
2014-09-29 14:46:27 +02:00
|
|
|
}
|
2016-05-27 15:45:22 +02:00
|
|
|
|
2014-09-29 11:20:39 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'name',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Resource Name'),
|
|
|
|
'description' => $this->translate('The unique name of this resource')
|
2014-09-29 11:20:39 +02:00
|
|
|
)
|
|
|
|
);
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'db',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-04-07 15:05:57 +02:00
|
|
|
'autosubmit' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Database Type'),
|
|
|
|
'description' => $this->translate('The type of SQL database'),
|
2014-09-29 14:46:27 +02:00
|
|
|
'multiOptions' => $dbChoices
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
2018-05-23 19:50:31 +02:00
|
|
|
if ($dbChoice === 'sqlite') {
|
2016-03-06 19:55:04 +01:00
|
|
|
$this->addElement(
|
2017-12-15 11:17:07 +01:00
|
|
|
'text',
|
2018-05-23 19:50:31 +02:00
|
|
|
'dbname',
|
2016-03-06 19:55:04 +01:00
|
|
|
array(
|
2018-05-23 19:50:31 +02:00
|
|
|
'required' => true,
|
|
|
|
'label' => $this->translate('Database Name'),
|
|
|
|
'description' => $this->translate('The name of the database to use')
|
2016-03-06 19:55:04 +01:00
|
|
|
)
|
|
|
|
);
|
2018-05-23 19:50:31 +02:00
|
|
|
} else {
|
2017-12-15 11:17:07 +01:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
2018-05-23 19:50:31 +02:00
|
|
|
'host',
|
|
|
|
array (
|
2018-12-04 15:50:53 +01:00
|
|
|
'required' => $hostIsRequired,
|
2018-05-23 19:50:31 +02:00
|
|
|
'label' => $this->translate('Host'),
|
|
|
|
'description' => $this->translate('The hostname of the database')
|
|
|
|
. ($socketInfo ? '. ' . $socketInfo : ''),
|
2018-12-12 11:09:10 +01:00
|
|
|
'value' => $hostIsRequired ? 'localhost' : ''
|
2018-05-23 19:50:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'number',
|
|
|
|
'port',
|
2017-12-15 11:17:07 +01:00
|
|
|
array(
|
2018-05-23 19:50:31 +02:00
|
|
|
'description' => $this->translate('The port to use'),
|
|
|
|
'label' => $this->translate('Port'),
|
|
|
|
'preserveDefault' => true,
|
|
|
|
'required' => $offerPostgres,
|
|
|
|
'value' => $offerPostgres ? 5432 : null
|
2017-12-15 11:17:07 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
2018-05-23 19:50:31 +02:00
|
|
|
'dbname',
|
2017-12-15 11:17:07 +01:00
|
|
|
array(
|
2018-05-23 19:50:31 +02:00
|
|
|
'required' => true,
|
|
|
|
'label' => $this->translate('Database Name'),
|
|
|
|
'description' => $this->translate('The name of the database to use')
|
2017-12-15 11:17:07 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
2018-05-23 19:50:31 +02:00
|
|
|
'username',
|
|
|
|
array (
|
|
|
|
'required' => true,
|
|
|
|
'label' => $this->translate('Username'),
|
|
|
|
'description' => $this->translate('The user name to use for authentication')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'password',
|
|
|
|
'password',
|
2017-12-15 11:17:07 +01:00
|
|
|
array(
|
2018-05-23 19:50:31 +02:00
|
|
|
'required' => true,
|
|
|
|
'renderPassword' => true,
|
|
|
|
'label' => $this->translate('Password'),
|
|
|
|
'description' => $this->translate('The password to use for authentication')
|
2017-12-15 11:17:07 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
2018-05-23 19:50:31 +02:00
|
|
|
'charset',
|
|
|
|
array (
|
|
|
|
'description' => $this->translate('The character set for the database'),
|
|
|
|
'label' => $this->translate('Character Set')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'checkbox',
|
|
|
|
'use_ssl',
|
2017-12-15 11:17:07 +01:00
|
|
|
array(
|
2018-05-23 19:50:31 +02:00
|
|
|
'autosubmit' => true,
|
|
|
|
'label' => $this->translate('Use SSL'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Whether to encrypt the connection or to authenticate using certificates'
|
|
|
|
)
|
2017-12-15 11:17:07 +01:00
|
|
|
)
|
|
|
|
);
|
2018-05-23 19:50:31 +02:00
|
|
|
if (isset($formData['use_ssl']) && $formData['use_ssl']) {
|
2018-07-13 19:50:18 +02:00
|
|
|
if (defined('\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT')) {
|
|
|
|
$this->addElement(
|
|
|
|
'checkbox',
|
|
|
|
'ssl_do_not_verify_server_cert',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('SSL Do Not Verify Server Certificate'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Whether to disable verification of the server certificate'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-05-23 19:50:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'ssl_key',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('SSL Key'),
|
|
|
|
'description' => $this->translate('The client key file path')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'ssl_cert',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('SSL Certificate'),
|
|
|
|
'description' => $this->translate('The certificate file path')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'ssl_ca',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('SSL CA'),
|
|
|
|
'description' => $this->translate('The CA certificate file path')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'ssl_capath',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('SSL CA Path'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'The trusted CA certificates in PEM format directory path'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'ssl_cipher',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('SSL Cipher'),
|
|
|
|
'description' => $this->translate('The list of permissible ciphers')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-03-06 19:55:04 +01:00
|
|
|
}
|
2014-09-03 12:21:31 +02:00
|
|
|
|
|
|
|
return $this;
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
}
|